blob: fbda6b54baffccf798375cb8add49bb179738386 [file] [log] [blame]
Simon Wunderliche19f9752014-01-04 18:04:25 +01001/* Copyright (C) 2007-2014 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
18#include "main.h"
Antonio Quartulli785ea112011-11-23 11:35:44 +010019#include "distributed-arp-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000020#include "hard-interface.h"
21#include "soft-interface.h"
22#include "send.h"
23#include "translation-table.h"
24#include "routing.h"
Sven Eckelmannb706b132012-06-10 23:58:51 +020025#include "sysfs.h"
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +010026#include "debugfs.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000027#include "originator.h"
28#include "hash.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010029#include "bridge_loop_avoidance.h"
Antonio Quartulli8257f552013-08-19 18:39:59 +020030#include "gateway_client.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000031
32#include <linux/if_arp.h>
Antonio Quartulliaf5d4f72012-11-26 00:38:50 +010033#include <linux/if_ether.h>
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000034
Sven Eckelmann95638772012-05-12 02:09:31 +020035void batadv_hardif_free_rcu(struct rcu_head *rcu)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000036{
Sven Eckelmann56303d32012-06-05 22:31:31 +020037 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000038
Sven Eckelmann56303d32012-06-05 22:31:31 +020039 hard_iface = container_of(rcu, struct batadv_hard_iface, rcu);
Marek Lindnere6c10f42011-02-18 12:33:20 +000040 dev_put(hard_iface->net_dev);
41 kfree(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000042}
43
Sven Eckelmann56303d32012-06-05 22:31:31 +020044struct batadv_hard_iface *
45batadv_hardif_get_by_netdev(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046{
Sven Eckelmann56303d32012-06-05 22:31:31 +020047 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000048
49 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +020050 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +000051 if (hard_iface->net_dev == net_dev &&
52 atomic_inc_not_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000053 goto out;
54 }
55
Marek Lindnere6c10f42011-02-18 12:33:20 +000056 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000057
58out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000059 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +000060 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000061}
62
Antonio Quartullib7eddd02012-09-09 10:46:46 +020063/**
64 * batadv_is_on_batman_iface - check if a device is a batman iface descendant
65 * @net_dev: the device to check
66 *
67 * If the user creates any virtual device on top of a batman-adv interface, it
68 * is important to prevent this new interface to be used to create a new mesh
69 * network (this behaviour would lead to a batman-over-batman configuration).
70 * This function recursively checks all the fathers of the device passed as
71 * argument looking for a batman-adv soft interface.
72 *
73 * Returns true if the device is descendant of a batman-adv mesh interface (or
74 * if it is a batman-adv interface itself), false otherwise
75 */
76static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
77{
78 struct net_device *parent_dev;
79 bool ret;
80
81 /* check if this is a batman-adv mesh interface */
82 if (batadv_softif_is_valid(net_dev))
83 return true;
84
85 /* no more parents..stop recursion */
Cong Wangb6ed5492014-05-22 11:57:17 -070086 if (net_dev->iflink == 0 || net_dev->iflink == net_dev->ifindex)
Antonio Quartullib7eddd02012-09-09 10:46:46 +020087 return false;
88
89 /* recurse over the parent device */
Ying Xue16b77692014-01-15 10:23:42 +080090 parent_dev = __dev_get_by_index(&init_net, net_dev->iflink);
Antonio Quartullib7eddd02012-09-09 10:46:46 +020091 /* if we got a NULL parent_dev there is something broken.. */
92 if (WARN(!parent_dev, "Cannot find parent device"))
93 return false;
94
95 ret = batadv_is_on_batman_iface(parent_dev);
96
Antonio Quartullib7eddd02012-09-09 10:46:46 +020097 return ret;
98}
99
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200100static int batadv_is_valid_iface(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000101{
102 if (net_dev->flags & IFF_LOOPBACK)
103 return 0;
104
105 if (net_dev->type != ARPHRD_ETHER)
106 return 0;
107
108 if (net_dev->addr_len != ETH_ALEN)
109 return 0;
110
111 /* no batman over batman */
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200112 if (batadv_is_on_batman_iface(net_dev))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000113 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000114
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000115 return 1;
116}
117
Matthias Schifferde68d102013-03-09 23:14:22 +0100118/**
119 * batadv_is_wifi_netdev - check if the given net_device struct is a wifi
120 * interface
121 * @net_device: the device to check
122 *
123 * Returns true if the net device is a 802.11 wireless device, false otherwise.
124 */
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200125bool batadv_is_wifi_netdev(struct net_device *net_device)
Matthias Schifferde68d102013-03-09 23:14:22 +0100126{
Antonio Quartulli0c69aec2013-10-13 02:50:18 +0200127 if (!net_device)
128 return false;
129
Matthias Schifferde68d102013-03-09 23:14:22 +0100130#ifdef CONFIG_WIRELESS_EXT
131 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
132 * check for wireless_handlers != NULL
133 */
134 if (net_device->wireless_handlers)
135 return true;
136#endif
137
138 /* cfg80211 drivers have to set ieee80211_ptr */
139 if (net_device->ieee80211_ptr)
140 return true;
141
142 return false;
143}
144
Sven Eckelmann56303d32012-06-05 22:31:31 +0200145static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200146batadv_hardif_get_active(const struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000147{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200148 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000149
150 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200151 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000152 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000153 continue;
154
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200155 if (hard_iface->if_status == BATADV_IF_ACTIVE &&
Marek Lindnere6c10f42011-02-18 12:33:20 +0000156 atomic_inc_not_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000157 goto out;
158 }
159
Marek Lindnere6c10f42011-02-18 12:33:20 +0000160 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000161
162out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000163 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000164 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000165}
166
Sven Eckelmann56303d32012-06-05 22:31:31 +0200167static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
168 struct batadv_hard_iface *oldif)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000169{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200170 struct batadv_hard_iface *primary_if;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200171
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200172 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200173 if (!primary_if)
174 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000175
Antonio Quartulli785ea112011-11-23 11:35:44 +0100176 batadv_dat_init_own_addr(bat_priv, primary_if);
Sven Eckelmann08adf152012-05-12 13:38:47 +0200177 batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200178out:
179 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200180 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000181}
182
Sven Eckelmann56303d32012-06-05 22:31:31 +0200183static void batadv_primary_if_select(struct batadv_priv *bat_priv,
184 struct batadv_hard_iface *new_hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000185{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200186 struct batadv_hard_iface *curr_hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000187
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200188 ASSERT_RTNL();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000189
Marek Lindner32ae9b22011-04-20 15:40:58 +0200190 if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount))
191 new_hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000192
Sven Eckelmann728cbc62011-05-15 00:50:21 +0200193 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200194 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000195
Marek Lindner32ae9b22011-04-20 15:40:58 +0200196 if (!new_hard_iface)
Simon Wunderlich23721382012-01-22 20:00:19 +0100197 goto out;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200198
Marek Lindnercd8b78e2012-02-07 17:20:49 +0800199 bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200200 batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
Simon Wunderlich23721382012-01-22 20:00:19 +0100201
202out:
203 if (curr_hard_iface)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200204 batadv_hardif_free_ref(curr_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000205}
206
Sven Eckelmann56303d32012-06-05 22:31:31 +0200207static bool
208batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000209{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000210 if (hard_iface->net_dev->flags & IFF_UP)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000211 return true;
212
213 return false;
214}
215
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200216static void batadv_check_known_mac_addr(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000217{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200218 const struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000219
220 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200221 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200222 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
223 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000224 continue;
225
Marek Lindnere6c10f42011-02-18 12:33:20 +0000226 if (hard_iface->net_dev == net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000227 continue;
228
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200229 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
230 net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000231 continue;
232
Sven Eckelmann67969582012-03-26 16:22:45 +0200233 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
234 net_dev->dev_addr, hard_iface->net_dev->name);
235 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000236 }
237 rcu_read_unlock();
238}
239
Sven Eckelmann95638772012-05-12 02:09:31 +0200240int batadv_hardif_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000241{
Marek Lindnera19d3d82013-05-27 15:33:25 +0800242 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200243 const struct batadv_hard_iface *hard_iface;
Antonio Quartulli930cd6e2014-01-21 11:22:05 +0100244 int min_mtu = INT_MAX;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000245
246 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200247 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200248 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
249 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000250 continue;
251
Marek Lindnere6c10f42011-02-18 12:33:20 +0000252 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000253 continue;
254
Marek Lindnera19d3d82013-05-27 15:33:25 +0800255 min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000256 }
257 rcu_read_unlock();
Marek Lindnera19d3d82013-05-27 15:33:25 +0800258
Marek Lindnera19d3d82013-05-27 15:33:25 +0800259 if (atomic_read(&bat_priv->fragmentation) == 0)
260 goto out;
261
262 /* with fragmentation enabled the maximum size of internally generated
263 * packets such as translation table exchanges or tvlv containers, etc
264 * has to be calculated
265 */
266 min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
267 min_mtu -= sizeof(struct batadv_frag_packet);
268 min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
Marek Lindnera19d3d82013-05-27 15:33:25 +0800269
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000270out:
Antonio Quartulli930cd6e2014-01-21 11:22:05 +0100271 /* report to the other components the maximum amount of bytes that
272 * batman-adv can send over the wire (without considering the payload
273 * overhead). For example, this value is used by TT to compute the
274 * maximum local table table size
275 */
276 atomic_set(&bat_priv->packet_size_max, min_mtu);
277
278 /* the real soft-interface MTU is computed by removing the payload
279 * overhead from the maximum amount of bytes that was just computed.
280 *
281 * However batman-adv does not support MTUs bigger than ETH_DATA_LEN
282 */
283 return min_t(int, min_mtu - batadv_max_header_len(), ETH_DATA_LEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000284}
285
286/* adjusts the MTU if a new interface with a smaller MTU appeared. */
Sven Eckelmann95638772012-05-12 02:09:31 +0200287void batadv_update_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000288{
Marek Lindnera19d3d82013-05-27 15:33:25 +0800289 soft_iface->mtu = batadv_hardif_min_mtu(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000290
Marek Lindnera19d3d82013-05-27 15:33:25 +0800291 /* Check if the local translate table should be cleaned up to match a
292 * new (and smaller) MTU.
293 */
294 batadv_tt_local_resize_to_mtu(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000295}
296
Sven Eckelmann56303d32012-06-05 22:31:31 +0200297static void
298batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000299{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200300 struct batadv_priv *bat_priv;
301 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000302
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200303 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200304 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000305
Marek Lindnere6c10f42011-02-18 12:33:20 +0000306 bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000307
Marek Lindnerc3229392012-03-11 06:17:50 +0800308 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200309 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000310
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200311 /* the first active interface becomes our primary interface or
Antonio Quartulli015758d2011-07-09 17:52:13 +0200312 * the next active interface after the old primary interface was removed
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000313 */
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200314 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200315 if (!primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200316 batadv_primary_if_select(bat_priv, hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000317
Sven Eckelmann3e348192012-05-16 20:23:22 +0200318 batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
319 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000320
Sven Eckelmann95638772012-05-12 02:09:31 +0200321 batadv_update_min_mtu(hard_iface->soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200322
323out:
324 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200325 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000326}
327
Sven Eckelmann56303d32012-06-05 22:31:31 +0200328static void
329batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000330{
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200331 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
332 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000333 return;
334
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200335 hard_iface->if_status = BATADV_IF_INACTIVE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000336
Sven Eckelmann3e348192012-05-16 20:23:22 +0200337 batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
338 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000339
Sven Eckelmann95638772012-05-12 02:09:31 +0200340 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000341}
342
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100343/**
344 * batadv_master_del_slave - remove hard_iface from the current master interface
345 * @slave: the interface enslaved in another master
346 * @master: the master from which slave has to be removed
347 *
348 * Invoke ndo_del_slave on master passing slave as argument. In this way slave
349 * is free'd and master can correctly change its internal state.
350 * Return 0 on success, a negative value representing the error otherwise
351 */
352static int batadv_master_del_slave(struct batadv_hard_iface *slave,
353 struct net_device *master)
354{
355 int ret;
356
357 if (!master)
358 return 0;
359
360 ret = -EBUSY;
361 if (master->netdev_ops->ndo_del_slave)
362 ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev);
363
364 return ret;
365}
366
Sven Eckelmann56303d32012-06-05 22:31:31 +0200367int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
Sven Eckelmann95638772012-05-12 02:09:31 +0200368 const char *iface_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000369{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200370 struct batadv_priv *bat_priv;
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100371 struct net_device *soft_iface, *master;
Antonio Quartulli293e9332013-05-19 12:55:16 +0200372 __be16 ethertype = htons(ETH_P_BATMAN);
Marek Lindner411d6ed2013-05-08 13:31:59 +0800373 int max_header_len = batadv_max_header_len();
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000374 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200376 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377 goto out;
378
Marek Lindnere6c10f42011-02-18 12:33:20 +0000379 if (!atomic_inc_not_zero(&hard_iface->refcount))
Marek Lindnered75ccb2011-02-10 14:33:51 +0000380 goto out;
381
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000382 soft_iface = dev_get_by_name(&init_net, iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000384 if (!soft_iface) {
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200385 soft_iface = batadv_softif_create(iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000386
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000387 if (!soft_iface) {
388 ret = -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389 goto err;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000390 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000391
392 /* dev_get_by_name() increases the reference counter for us */
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000393 dev_hold(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000394 }
395
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200396 if (!batadv_softif_is_valid(soft_iface)) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100397 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000398 soft_iface->name);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000399 ret = -EINVAL;
Marek Lindner77af7572012-02-07 17:20:48 +0800400 goto err_dev;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000401 }
402
Antonio Quartullicb4b0d42013-02-16 14:42:39 +0100403 /* check if the interface is enslaved in another virtual one and
404 * in that case unlink it first
405 */
406 master = netdev_master_upper_dev_get(hard_iface->net_dev);
407 ret = batadv_master_del_slave(hard_iface, master);
408 if (ret)
409 goto err_dev;
410
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000411 hard_iface->soft_iface = soft_iface;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000412 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200413
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800414 ret = netdev_master_upper_dev_link(hard_iface->net_dev, soft_iface);
415 if (ret)
416 goto err_dev;
417
Marek Lindner77af7572012-02-07 17:20:48 +0800418 ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200419 if (ret < 0)
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800420 goto err_upper;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000421
Marek Lindnere6c10f42011-02-18 12:33:20 +0000422 hard_iface->if_num = bat_priv->num_ifaces;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423 bat_priv->num_ifaces++;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200424 hard_iface->if_status = BATADV_IF_INACTIVE;
Simon Wunderlich62446302012-07-01 22:51:55 +0200425 ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
426 if (ret < 0) {
427 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
428 bat_priv->num_ifaces--;
429 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800430 goto err_upper;
Simon Wunderlich62446302012-07-01 22:51:55 +0200431 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000432
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200433 hard_iface->batman_adv_ptype.type = ethertype;
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200434 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000435 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
436 dev_add_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437
Sven Eckelmann3e348192012-05-16 20:23:22 +0200438 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
439 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000440
Sven Eckelmann0aca2362012-06-19 20:26:30 +0200441 if (atomic_read(&bat_priv->fragmentation) &&
Marek Lindner411d6ed2013-05-08 13:31:59 +0800442 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200443 batadv_info(hard_iface->soft_iface,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800444 "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 +0200445 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800446 ETH_DATA_LEN + max_header_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000447
Sven Eckelmann0aca2362012-06-19 20:26:30 +0200448 if (!atomic_read(&bat_priv->fragmentation) &&
Marek Lindner411d6ed2013-05-08 13:31:59 +0800449 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200450 batadv_info(hard_iface->soft_iface,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800451 "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 +0200452 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Marek Lindner411d6ed2013-05-08 13:31:59 +0800453 ETH_DATA_LEN + max_header_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000454
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200455 if (batadv_hardif_is_iface_up(hard_iface))
456 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000457 else
Sven Eckelmann3e348192012-05-16 20:23:22 +0200458 batadv_err(hard_iface->soft_iface,
459 "Not using interface %s (retrying later): interface not active\n",
460 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000461
462 /* begin scheduling originator messages on that interface */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200463 batadv_schedule_bat_ogm(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464
465out:
466 return 0;
467
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800468err_upper:
469 netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface);
Marek Lindner77af7572012-02-07 17:20:48 +0800470err_dev:
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800471 hard_iface->soft_iface = NULL;
Marek Lindner77af7572012-02-07 17:20:48 +0800472 dev_put(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000473err:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200474 batadv_hardif_free_ref(hard_iface);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000475 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000476}
477
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800478void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface,
479 enum batadv_hard_if_cleanup autodel)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000480{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200481 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
482 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000483
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200484 if (hard_iface->if_status == BATADV_IF_ACTIVE)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200485 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000486
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200487 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200488 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000489
Sven Eckelmann3e348192012-05-16 20:23:22 +0200490 batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
491 hard_iface->net_dev->name);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000492 dev_remove_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000493
494 bat_priv->num_ifaces--;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200495 batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000496
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200497 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200498 if (hard_iface == primary_if) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200499 struct batadv_hard_iface *new_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000500
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200501 new_if = batadv_hardif_get_active(hard_iface->soft_iface);
502 batadv_primary_if_select(bat_priv, new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000503
504 if (new_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200505 batadv_hardif_free_ref(new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000506 }
507
Marek Lindner00a50072012-02-07 17:20:47 +0800508 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200509 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000510
Marek Lindnere6c10f42011-02-18 12:33:20 +0000511 /* delete all references to this hard_iface */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200512 batadv_purge_orig_ref(bat_priv);
Sven Eckelmann9455e342012-05-12 02:09:37 +0200513 batadv_purge_outstanding_packets(bat_priv, hard_iface);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000514 dev_put(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000515
516 /* nobody uses this interface anymore */
Antonio Quartulli8257f552013-08-19 18:39:59 +0200517 if (!bat_priv->num_ifaces) {
518 batadv_gw_check_client_stop(bat_priv);
519
520 if (autodel == BATADV_IF_CLEANUP_AUTO)
521 batadv_softif_destroy_sysfs(hard_iface->soft_iface);
522 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000523
Sven Eckelmann3dbd5502013-02-11 17:10:27 +0800524 netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000525 hard_iface->soft_iface = NULL;
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200526 batadv_hardif_free_ref(hard_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200527
528out:
529 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200530 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000531}
532
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100533/**
534 * batadv_hardif_remove_interface_finish - cleans up the remains of a hardif
535 * @work: work queue item
536 *
537 * Free the parts of the hard interface which can not be removed under
538 * rtnl lock (to prevent deadlock situations).
539 */
540static void batadv_hardif_remove_interface_finish(struct work_struct *work)
541{
542 struct batadv_hard_iface *hard_iface;
543
544 hard_iface = container_of(work, struct batadv_hard_iface,
545 cleanup_work);
546
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100547 batadv_debugfs_del_hardif(hard_iface);
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100548 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
549 batadv_hardif_free_ref(hard_iface);
550}
551
Sven Eckelmann56303d32012-06-05 22:31:31 +0200552static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200553batadv_hardif_add_interface(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000554{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200555 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000556 int ret;
557
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200558 ASSERT_RTNL();
559
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200560 ret = batadv_is_valid_iface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000561 if (ret != 1)
562 goto out;
563
564 dev_hold(net_dev);
565
Antonio Quartulli7db3fc22013-04-02 12:16:53 +0200566 hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC);
Joe Perches320f4222011-08-29 14:17:24 -0700567 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000568 goto release_dev;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000569
Sven Eckelmann5853e222012-05-12 02:09:24 +0200570 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000571 if (ret)
572 goto free_if;
573
Marek Lindnere6c10f42011-02-18 12:33:20 +0000574 hard_iface->if_num = -1;
575 hard_iface->net_dev = net_dev;
576 hard_iface->soft_iface = NULL;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200577 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100578
579 ret = batadv_debugfs_add_hardif(hard_iface);
580 if (ret)
581 goto free_sysfs;
582
Marek Lindnere6c10f42011-02-18 12:33:20 +0000583 INIT_LIST_HEAD(&hard_iface->list);
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100584 INIT_WORK(&hard_iface->cleanup_work,
585 batadv_hardif_remove_interface_finish);
586
Matthias Schiffercaf65bf2013-03-09 23:14:23 +0100587 hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
588 if (batadv_is_wifi_netdev(net_dev))
589 hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
590
Marek Lindnered75ccb2011-02-10 14:33:51 +0000591 /* extra reference for return */
Marek Lindnere6c10f42011-02-18 12:33:20 +0000592 atomic_set(&hard_iface->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000593
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200594 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200595 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000596
Marek Lindnere6c10f42011-02-18 12:33:20 +0000597 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000598
Simon Wunderlich5bc7c1e2013-11-21 14:16:12 +0100599free_sysfs:
600 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000601free_if:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000602 kfree(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000603release_dev:
604 dev_put(net_dev);
605out:
606 return NULL;
607}
608
Sven Eckelmann56303d32012-06-05 22:31:31 +0200609static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000610{
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200611 ASSERT_RTNL();
612
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000613 /* first deactivate interface */
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200614 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800615 batadv_hardif_disable_interface(hard_iface,
616 BATADV_IF_CLEANUP_AUTO);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000617
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200618 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000619 return;
620
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200621 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
Simon Wunderlich5bc44dc2013-01-11 10:19:51 +0100622 queue_work(batadv_event_workqueue, &hard_iface->cleanup_work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000623}
624
Sven Eckelmann95638772012-05-12 02:09:31 +0200625void batadv_hardif_remove_interfaces(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000626{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200627 struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000628
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200629 rtnl_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000630 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200631 &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000632 list_del_rcu(&hard_iface->list);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200633 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000634 }
635 rtnl_unlock();
636}
637
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200638static int batadv_hard_if_event(struct notifier_block *this,
639 unsigned long event, void *ptr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000640{
Jiri Pirko351638e2013-05-28 01:30:21 +0000641 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200642 struct batadv_hard_iface *hard_iface;
643 struct batadv_hard_iface *primary_if = NULL;
644 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000645
Sven Eckelmann37130292013-02-11 17:10:22 +0800646 if (batadv_softif_is_valid(net_dev) && event == NETDEV_REGISTER) {
647 batadv_sysfs_add_meshif(net_dev);
Antonio Quartulli5d2c05b2013-07-02 11:04:34 +0200648 bat_priv = netdev_priv(net_dev);
649 batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
Sven Eckelmann37130292013-02-11 17:10:22 +0800650 return NOTIFY_DONE;
651 }
652
Sven Eckelmann56303d32012-06-05 22:31:31 +0200653 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000654 if (!hard_iface && event == NETDEV_REGISTER)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200655 hard_iface = batadv_hardif_add_interface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000656
Marek Lindnere6c10f42011-02-18 12:33:20 +0000657 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000658 goto out;
659
660 switch (event) {
661 case NETDEV_UP:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200662 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000663 break;
664 case NETDEV_GOING_DOWN:
665 case NETDEV_DOWN:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200666 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000667 break;
668 case NETDEV_UNREGISTER:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000669 list_del_rcu(&hard_iface->list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000670
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200671 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000672 break;
673 case NETDEV_CHANGEMTU:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000674 if (hard_iface->soft_iface)
Sven Eckelmann95638772012-05-12 02:09:31 +0200675 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000676 break;
677 case NETDEV_CHANGEADDR:
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200678 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000679 goto hardif_put;
680
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200681 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000682
Marek Lindnere6c10f42011-02-18 12:33:20 +0000683 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerc3229392012-03-11 06:17:50 +0800684 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Marek Lindner01c42242011-11-28 21:31:55 +0800685
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200686 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200687 if (!primary_if)
688 goto hardif_put;
689
690 if (hard_iface == primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200691 batadv_primary_if_update_addr(bat_priv, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000692 break;
693 default:
694 break;
Joe Perchesf81c6222011-06-03 11:51:19 +0000695 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000696
697hardif_put:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200698 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000699out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200700 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200701 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000702 return NOTIFY_DONE;
703}
704
Sven Eckelmann95638772012-05-12 02:09:31 +0200705struct notifier_block batadv_hard_if_notifier = {
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200706 .notifier_call = batadv_hard_if_event,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000707};