]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/batman-adv/hard-interface.c
Merge tag 'md/3.13' of git://neil.brown.name/md
[mirror_ubuntu-bionic-kernel.git] / net / batman-adv / hard-interface.c
CommitLineData
0b873931 1/* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors:
c6c8fea2
SE
2 *
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
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
c6c8fea2
SE
18 */
19
20#include "main.h"
785ea114 21#include "distributed-arp-table.h"
c6c8fea2
SE
22#include "hard-interface.h"
23#include "soft-interface.h"
24#include "send.h"
25#include "translation-table.h"
26#include "routing.h"
b706b13b 27#include "sysfs.h"
c6c8fea2
SE
28#include "originator.h"
29#include "hash.h"
23721387 30#include "bridge_loop_avoidance.h"
8257f55a 31#include "gateway_client.h"
c6c8fea2
SE
32
33#include <linux/if_arp.h>
af5d4f77 34#include <linux/if_ether.h>
c6c8fea2 35
9563877e 36void batadv_hardif_free_rcu(struct rcu_head *rcu)
c6c8fea2 37{
56303d34 38 struct batadv_hard_iface *hard_iface;
c6c8fea2 39
56303d34 40 hard_iface = container_of(rcu, struct batadv_hard_iface, rcu);
e6c10f43
ML
41 dev_put(hard_iface->net_dev);
42 kfree(hard_iface);
c6c8fea2
SE
43}
44
56303d34
SE
45struct batadv_hard_iface *
46batadv_hardif_get_by_netdev(const struct net_device *net_dev)
c6c8fea2 47{
56303d34 48 struct batadv_hard_iface *hard_iface;
c6c8fea2
SE
49
50 rcu_read_lock();
3193e8fd 51 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
e6c10f43
ML
52 if (hard_iface->net_dev == net_dev &&
53 atomic_inc_not_zero(&hard_iface->refcount))
c6c8fea2
SE
54 goto out;
55 }
56
e6c10f43 57 hard_iface = NULL;
c6c8fea2
SE
58
59out:
c6c8fea2 60 rcu_read_unlock();
e6c10f43 61 return hard_iface;
c6c8fea2
SE
62}
63
b7eddd0b
AQ
64/**
65 * batadv_is_on_batman_iface - check if a device is a batman iface descendant
66 * @net_dev: the device to check
67 *
68 * If the user creates any virtual device on top of a batman-adv interface, it
69 * is important to prevent this new interface to be used to create a new mesh
70 * network (this behaviour would lead to a batman-over-batman configuration).
71 * This function recursively checks all the fathers of the device passed as
72 * argument looking for a batman-adv soft interface.
73 *
74 * Returns true if the device is descendant of a batman-adv mesh interface (or
75 * if it is a batman-adv interface itself), false otherwise
76 */
77static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
78{
79 struct net_device *parent_dev;
80 bool ret;
81
82 /* check if this is a batman-adv mesh interface */
83 if (batadv_softif_is_valid(net_dev))
84 return true;
85
86 /* no more parents..stop recursion */
87 if (net_dev->iflink == net_dev->ifindex)
88 return false;
89
90 /* recurse over the parent device */
91 parent_dev = dev_get_by_index(&init_net, net_dev->iflink);
92 /* if we got a NULL parent_dev there is something broken.. */
93 if (WARN(!parent_dev, "Cannot find parent device"))
94 return false;
95
96 ret = batadv_is_on_batman_iface(parent_dev);
97
98 if (parent_dev)
99 dev_put(parent_dev);
100 return ret;
101}
102
18a1cb6e 103static int batadv_is_valid_iface(const struct net_device *net_dev)
c6c8fea2
SE
104{
105 if (net_dev->flags & IFF_LOOPBACK)
106 return 0;
107
108 if (net_dev->type != ARPHRD_ETHER)
109 return 0;
110
111 if (net_dev->addr_len != ETH_ALEN)
112 return 0;
113
114 /* no batman over batman */
b7eddd0b 115 if (batadv_is_on_batman_iface(net_dev))
c6c8fea2 116 return 0;
c6c8fea2 117
c6c8fea2
SE
118 return 1;
119}
120
de68d100
MS
121/**
122 * batadv_is_wifi_netdev - check if the given net_device struct is a wifi
123 * interface
124 * @net_device: the device to check
125 *
126 * Returns true if the net device is a 802.11 wireless device, false otherwise.
127 */
0c69aecc 128bool batadv_is_wifi_netdev(struct net_device *net_device)
de68d100 129{
0c69aecc
AQ
130 if (!net_device)
131 return false;
132
de68d100
MS
133#ifdef CONFIG_WIRELESS_EXT
134 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
135 * check for wireless_handlers != NULL
136 */
137 if (net_device->wireless_handlers)
138 return true;
139#endif
140
141 /* cfg80211 drivers have to set ieee80211_ptr */
142 if (net_device->ieee80211_ptr)
143 return true;
144
145 return false;
146}
147
56303d34 148static struct batadv_hard_iface *
18a1cb6e 149batadv_hardif_get_active(const struct net_device *soft_iface)
c6c8fea2 150{
56303d34 151 struct batadv_hard_iface *hard_iface;
c6c8fea2
SE
152
153 rcu_read_lock();
3193e8fd 154 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
e6c10f43 155 if (hard_iface->soft_iface != soft_iface)
c6c8fea2
SE
156 continue;
157
e9a4f295 158 if (hard_iface->if_status == BATADV_IF_ACTIVE &&
e6c10f43 159 atomic_inc_not_zero(&hard_iface->refcount))
c6c8fea2
SE
160 goto out;
161 }
162
e6c10f43 163 hard_iface = NULL;
c6c8fea2
SE
164
165out:
c6c8fea2 166 rcu_read_unlock();
e6c10f43 167 return hard_iface;
c6c8fea2
SE
168}
169
56303d34
SE
170static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
171 struct batadv_hard_iface *oldif)
c6c8fea2 172{
56303d34 173 struct batadv_hard_iface *primary_if;
32ae9b22 174
e5d89254 175 primary_if = batadv_primary_if_get_selected(bat_priv);
32ae9b22
ML
176 if (!primary_if)
177 goto out;
c6c8fea2 178
785ea114 179 batadv_dat_init_own_addr(bat_priv, primary_if);
08adf151 180 batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
32ae9b22
ML
181out:
182 if (primary_if)
e5d89254 183 batadv_hardif_free_ref(primary_if);
c6c8fea2
SE
184}
185
56303d34
SE
186static void batadv_primary_if_select(struct batadv_priv *bat_priv,
187 struct batadv_hard_iface *new_hard_iface)
c6c8fea2 188{
56303d34 189 struct batadv_hard_iface *curr_hard_iface;
c6c8fea2 190
c3caf519 191 ASSERT_RTNL();
c6c8fea2 192
32ae9b22
ML
193 if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount))
194 new_hard_iface = NULL;
c6c8fea2 195
728cbc6a 196 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
32ae9b22 197 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
c6c8fea2 198
32ae9b22 199 if (!new_hard_iface)
23721387 200 goto out;
32ae9b22 201
cd8b78e7 202 bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface);
18a1cb6e 203 batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
23721387
SW
204
205out:
206 if (curr_hard_iface)
e5d89254 207 batadv_hardif_free_ref(curr_hard_iface);
c6c8fea2
SE
208}
209
56303d34
SE
210static bool
211batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
c6c8fea2 212{
e6c10f43 213 if (hard_iface->net_dev->flags & IFF_UP)
c6c8fea2
SE
214 return true;
215
216 return false;
217}
218
18a1cb6e 219static void batadv_check_known_mac_addr(const struct net_device *net_dev)
c6c8fea2 220{
56303d34 221 const struct batadv_hard_iface *hard_iface;
c6c8fea2
SE
222
223 rcu_read_lock();
3193e8fd 224 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
e9a4f295
SE
225 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
226 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
c6c8fea2
SE
227 continue;
228
e6c10f43 229 if (hard_iface->net_dev == net_dev)
c6c8fea2
SE
230 continue;
231
1eda58bf
SE
232 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
233 net_dev->dev_addr))
c6c8fea2
SE
234 continue;
235
67969581
SE
236 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
237 net_dev->dev_addr, hard_iface->net_dev->name);
238 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
c6c8fea2
SE
239 }
240 rcu_read_unlock();
241}
242
9563877e 243int batadv_hardif_min_mtu(struct net_device *soft_iface)
c6c8fea2 244{
a19d3d85 245 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
56303d34 246 const struct batadv_hard_iface *hard_iface;
c6c8fea2 247 int min_mtu = ETH_DATA_LEN;
c6c8fea2
SE
248
249 rcu_read_lock();
3193e8fd 250 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
e9a4f295
SE
251 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
252 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
c6c8fea2
SE
253 continue;
254
e6c10f43 255 if (hard_iface->soft_iface != soft_iface)
c6c8fea2
SE
256 continue;
257
a19d3d85 258 min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
c6c8fea2
SE
259 }
260 rcu_read_unlock();
a19d3d85
ML
261
262 atomic_set(&bat_priv->packet_size_max, min_mtu);
263
264 if (atomic_read(&bat_priv->fragmentation) == 0)
265 goto out;
266
267 /* with fragmentation enabled the maximum size of internally generated
268 * packets such as translation table exchanges or tvlv containers, etc
269 * has to be calculated
270 */
271 min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
272 min_mtu -= sizeof(struct batadv_frag_packet);
273 min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
274 atomic_set(&bat_priv->packet_size_max, min_mtu);
275
276 /* with fragmentation enabled we can fragment external packets easily */
277 min_mtu = min_t(int, min_mtu, ETH_DATA_LEN);
278
c6c8fea2 279out:
a19d3d85 280 return min_mtu - batadv_max_header_len();
c6c8fea2
SE
281}
282
283/* adjusts the MTU if a new interface with a smaller MTU appeared. */
9563877e 284void batadv_update_min_mtu(struct net_device *soft_iface)
c6c8fea2 285{
a19d3d85 286 soft_iface->mtu = batadv_hardif_min_mtu(soft_iface);
c6c8fea2 287
a19d3d85
ML
288 /* Check if the local translate table should be cleaned up to match a
289 * new (and smaller) MTU.
290 */
291 batadv_tt_local_resize_to_mtu(soft_iface);
c6c8fea2
SE
292}
293
56303d34
SE
294static void
295batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
c6c8fea2 296{
56303d34
SE
297 struct batadv_priv *bat_priv;
298 struct batadv_hard_iface *primary_if = NULL;
c6c8fea2 299
e9a4f295 300 if (hard_iface->if_status != BATADV_IF_INACTIVE)
32ae9b22 301 goto out;
c6c8fea2 302
e6c10f43 303 bat_priv = netdev_priv(hard_iface->soft_iface);
c6c8fea2 304
c3229398 305 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
e9a4f295 306 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
c6c8fea2 307
9cfc7bd6 308 /* the first active interface becomes our primary interface or
015758d0 309 * the next active interface after the old primary interface was removed
c6c8fea2 310 */
e5d89254 311 primary_if = batadv_primary_if_get_selected(bat_priv);
32ae9b22 312 if (!primary_if)
18a1cb6e 313 batadv_primary_if_select(bat_priv, hard_iface);
c6c8fea2 314
3e34819e
SE
315 batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
316 hard_iface->net_dev->name);
c6c8fea2 317
9563877e 318 batadv_update_min_mtu(hard_iface->soft_iface);
32ae9b22
ML
319
320out:
321 if (primary_if)
e5d89254 322 batadv_hardif_free_ref(primary_if);
c6c8fea2
SE
323}
324
56303d34
SE
325static void
326batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
c6c8fea2 327{
e9a4f295
SE
328 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
329 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
c6c8fea2
SE
330 return;
331
e9a4f295 332 hard_iface->if_status = BATADV_IF_INACTIVE;
c6c8fea2 333
3e34819e
SE
334 batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
335 hard_iface->net_dev->name);
c6c8fea2 336
9563877e 337 batadv_update_min_mtu(hard_iface->soft_iface);
c6c8fea2
SE
338}
339
cb4b0d48
AQ
340/**
341 * batadv_master_del_slave - remove hard_iface from the current master interface
342 * @slave: the interface enslaved in another master
343 * @master: the master from which slave has to be removed
344 *
345 * Invoke ndo_del_slave on master passing slave as argument. In this way slave
346 * is free'd and master can correctly change its internal state.
347 * Return 0 on success, a negative value representing the error otherwise
348 */
349static int batadv_master_del_slave(struct batadv_hard_iface *slave,
350 struct net_device *master)
351{
352 int ret;
353
354 if (!master)
355 return 0;
356
357 ret = -EBUSY;
358 if (master->netdev_ops->ndo_del_slave)
359 ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev);
360
361 return ret;
362}
363
56303d34 364int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
9563877e 365 const char *iface_name)
c6c8fea2 366{
56303d34 367 struct batadv_priv *bat_priv;
cb4b0d48 368 struct net_device *soft_iface, *master;
293e9338 369 __be16 ethertype = htons(ETH_P_BATMAN);
411d6ed9 370 int max_header_len = batadv_max_header_len();
e44d8fe2 371 int ret;
c6c8fea2 372
e9a4f295 373 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
c6c8fea2
SE
374 goto out;
375
e6c10f43 376 if (!atomic_inc_not_zero(&hard_iface->refcount))
ed75ccbe
ML
377 goto out;
378
e44d8fe2 379 soft_iface = dev_get_by_name(&init_net, iface_name);
c6c8fea2 380
e44d8fe2 381 if (!soft_iface) {
04b482a2 382 soft_iface = batadv_softif_create(iface_name);
c6c8fea2 383
e44d8fe2
SE
384 if (!soft_iface) {
385 ret = -ENOMEM;
c6c8fea2 386 goto err;
e44d8fe2 387 }
c6c8fea2
SE
388
389 /* dev_get_by_name() increases the reference counter for us */
e44d8fe2
SE
390 dev_hold(soft_iface);
391 }
392
04b482a2 393 if (!batadv_softif_is_valid(soft_iface)) {
86ceb360 394 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
e44d8fe2 395 soft_iface->name);
e44d8fe2 396 ret = -EINVAL;
77af7575 397 goto err_dev;
c6c8fea2
SE
398 }
399
cb4b0d48
AQ
400 /* check if the interface is enslaved in another virtual one and
401 * in that case unlink it first
402 */
403 master = netdev_master_upper_dev_get(hard_iface->net_dev);
404 ret = batadv_master_del_slave(hard_iface, master);
405 if (ret)
406 goto err_dev;
407
e44d8fe2 408 hard_iface->soft_iface = soft_iface;
e6c10f43 409 bat_priv = netdev_priv(hard_iface->soft_iface);
d0b9fd89 410
3dbd550b
SE
411 ret = netdev_master_upper_dev_link(hard_iface->net_dev, soft_iface);
412 if (ret)
413 goto err_dev;
414
77af7575 415 ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
5346c35e 416 if (ret < 0)
3dbd550b 417 goto err_upper;
c6c8fea2 418
e6c10f43 419 hard_iface->if_num = bat_priv->num_ifaces;
c6c8fea2 420 bat_priv->num_ifaces++;
e9a4f295 421 hard_iface->if_status = BATADV_IF_INACTIVE;
62446307
SW
422 ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
423 if (ret < 0) {
424 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
425 bat_priv->num_ifaces--;
426 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
3dbd550b 427 goto err_upper;
62446307 428 }
c6c8fea2 429
7e071c79 430 hard_iface->batman_adv_ptype.type = ethertype;
3193e8fd 431 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
e6c10f43
ML
432 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
433 dev_add_pack(&hard_iface->batman_adv_ptype);
c6c8fea2 434
3e34819e
SE
435 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
436 hard_iface->net_dev->name);
c6c8fea2 437
0aca2369 438 if (atomic_read(&bat_priv->fragmentation) &&
411d6ed9 439 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
3e34819e 440 batadv_info(hard_iface->soft_iface,
411d6ed9 441 "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",
3e34819e 442 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
411d6ed9 443 ETH_DATA_LEN + max_header_len);
c6c8fea2 444
0aca2369 445 if (!atomic_read(&bat_priv->fragmentation) &&
411d6ed9 446 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
3e34819e 447 batadv_info(hard_iface->soft_iface,
411d6ed9 448 "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",
3e34819e 449 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
411d6ed9 450 ETH_DATA_LEN + max_header_len);
c6c8fea2 451
18a1cb6e
SE
452 if (batadv_hardif_is_iface_up(hard_iface))
453 batadv_hardif_activate_interface(hard_iface);
c6c8fea2 454 else
3e34819e
SE
455 batadv_err(hard_iface->soft_iface,
456 "Not using interface %s (retrying later): interface not active\n",
457 hard_iface->net_dev->name);
c6c8fea2
SE
458
459 /* begin scheduling originator messages on that interface */
9455e34c 460 batadv_schedule_bat_ogm(hard_iface);
c6c8fea2
SE
461
462out:
463 return 0;
464
3dbd550b
SE
465err_upper:
466 netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface);
77af7575 467err_dev:
3dbd550b 468 hard_iface->soft_iface = NULL;
77af7575 469 dev_put(soft_iface);
c6c8fea2 470err:
e5d89254 471 batadv_hardif_free_ref(hard_iface);
e44d8fe2 472 return ret;
c6c8fea2
SE
473}
474
a15fd361
SE
475void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface,
476 enum batadv_hard_if_cleanup autodel)
c6c8fea2 477{
56303d34
SE
478 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
479 struct batadv_hard_iface *primary_if = NULL;
c6c8fea2 480
e9a4f295 481 if (hard_iface->if_status == BATADV_IF_ACTIVE)
18a1cb6e 482 batadv_hardif_deactivate_interface(hard_iface);
c6c8fea2 483
e9a4f295 484 if (hard_iface->if_status != BATADV_IF_INACTIVE)
32ae9b22 485 goto out;
c6c8fea2 486
3e34819e
SE
487 batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
488 hard_iface->net_dev->name);
e6c10f43 489 dev_remove_pack(&hard_iface->batman_adv_ptype);
c6c8fea2
SE
490
491 bat_priv->num_ifaces--;
7d211efc 492 batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
c6c8fea2 493
e5d89254 494 primary_if = batadv_primary_if_get_selected(bat_priv);
32ae9b22 495 if (hard_iface == primary_if) {
56303d34 496 struct batadv_hard_iface *new_if;
c6c8fea2 497
18a1cb6e
SE
498 new_if = batadv_hardif_get_active(hard_iface->soft_iface);
499 batadv_primary_if_select(bat_priv, new_if);
c6c8fea2
SE
500
501 if (new_if)
e5d89254 502 batadv_hardif_free_ref(new_if);
c6c8fea2
SE
503 }
504
00a50076 505 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
e9a4f295 506 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
c6c8fea2 507
e6c10f43 508 /* delete all references to this hard_iface */
7d211efc 509 batadv_purge_orig_ref(bat_priv);
9455e34c 510 batadv_purge_outstanding_packets(bat_priv, hard_iface);
e6c10f43 511 dev_put(hard_iface->soft_iface);
c6c8fea2
SE
512
513 /* nobody uses this interface anymore */
8257f55a
AQ
514 if (!bat_priv->num_ifaces) {
515 batadv_gw_check_client_stop(bat_priv);
516
517 if (autodel == BATADV_IF_CLEANUP_AUTO)
518 batadv_softif_destroy_sysfs(hard_iface->soft_iface);
519 }
c6c8fea2 520
3dbd550b 521 netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
e6c10f43 522 hard_iface->soft_iface = NULL;
e5d89254 523 batadv_hardif_free_ref(hard_iface);
32ae9b22
ML
524
525out:
526 if (primary_if)
e5d89254 527 batadv_hardif_free_ref(primary_if);
c6c8fea2
SE
528}
529
5bc44dc8
SW
530/**
531 * batadv_hardif_remove_interface_finish - cleans up the remains of a hardif
532 * @work: work queue item
533 *
534 * Free the parts of the hard interface which can not be removed under
535 * rtnl lock (to prevent deadlock situations).
536 */
537static void batadv_hardif_remove_interface_finish(struct work_struct *work)
538{
539 struct batadv_hard_iface *hard_iface;
540
541 hard_iface = container_of(work, struct batadv_hard_iface,
542 cleanup_work);
543
544 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
545 batadv_hardif_free_ref(hard_iface);
546}
547
56303d34 548static struct batadv_hard_iface *
18a1cb6e 549batadv_hardif_add_interface(struct net_device *net_dev)
c6c8fea2 550{
56303d34 551 struct batadv_hard_iface *hard_iface;
c6c8fea2
SE
552 int ret;
553
c3caf519
SE
554 ASSERT_RTNL();
555
18a1cb6e 556 ret = batadv_is_valid_iface(net_dev);
c6c8fea2
SE
557 if (ret != 1)
558 goto out;
559
560 dev_hold(net_dev);
561
7db3fc29 562 hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC);
320f422f 563 if (!hard_iface)
c6c8fea2 564 goto release_dev;
c6c8fea2 565
5853e22c 566 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
c6c8fea2
SE
567 if (ret)
568 goto free_if;
569
e6c10f43
ML
570 hard_iface->if_num = -1;
571 hard_iface->net_dev = net_dev;
572 hard_iface->soft_iface = NULL;
e9a4f295 573 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
e6c10f43 574 INIT_LIST_HEAD(&hard_iface->list);
5bc44dc8
SW
575 INIT_WORK(&hard_iface->cleanup_work,
576 batadv_hardif_remove_interface_finish);
577
caf65bfc
MS
578 hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT;
579 if (batadv_is_wifi_netdev(net_dev))
580 hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS;
581
ed75ccbe 582 /* extra reference for return */
e6c10f43 583 atomic_set(&hard_iface->refcount, 2);
c6c8fea2 584
18a1cb6e 585 batadv_check_known_mac_addr(hard_iface->net_dev);
3193e8fd 586 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
c6c8fea2 587
e6c10f43 588 return hard_iface;
c6c8fea2
SE
589
590free_if:
e6c10f43 591 kfree(hard_iface);
c6c8fea2
SE
592release_dev:
593 dev_put(net_dev);
594out:
595 return NULL;
596}
597
56303d34 598static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
c6c8fea2 599{
c3caf519
SE
600 ASSERT_RTNL();
601
c6c8fea2 602 /* first deactivate interface */
e9a4f295 603 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
a15fd361
SE
604 batadv_hardif_disable_interface(hard_iface,
605 BATADV_IF_CLEANUP_AUTO);
c6c8fea2 606
e9a4f295 607 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
c6c8fea2
SE
608 return;
609
e9a4f295 610 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
5bc44dc8 611 queue_work(batadv_event_workqueue, &hard_iface->cleanup_work);
c6c8fea2
SE
612}
613
9563877e 614void batadv_hardif_remove_interfaces(void)
c6c8fea2 615{
56303d34 616 struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
c6c8fea2 617
c3caf519 618 rtnl_lock();
e6c10f43 619 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
3193e8fd 620 &batadv_hardif_list, list) {
e6c10f43 621 list_del_rcu(&hard_iface->list);
18a1cb6e 622 batadv_hardif_remove_interface(hard_iface);
c6c8fea2
SE
623 }
624 rtnl_unlock();
625}
626
18a1cb6e
SE
627static int batadv_hard_if_event(struct notifier_block *this,
628 unsigned long event, void *ptr)
c6c8fea2 629{
351638e7 630 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
56303d34
SE
631 struct batadv_hard_iface *hard_iface;
632 struct batadv_hard_iface *primary_if = NULL;
633 struct batadv_priv *bat_priv;
c6c8fea2 634
37130293
SE
635 if (batadv_softif_is_valid(net_dev) && event == NETDEV_REGISTER) {
636 batadv_sysfs_add_meshif(net_dev);
5d2c05b2
AQ
637 bat_priv = netdev_priv(net_dev);
638 batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
37130293
SE
639 return NOTIFY_DONE;
640 }
641
56303d34 642 hard_iface = batadv_hardif_get_by_netdev(net_dev);
e6c10f43 643 if (!hard_iface && event == NETDEV_REGISTER)
18a1cb6e 644 hard_iface = batadv_hardif_add_interface(net_dev);
c6c8fea2 645
e6c10f43 646 if (!hard_iface)
c6c8fea2
SE
647 goto out;
648
649 switch (event) {
650 case NETDEV_UP:
18a1cb6e 651 batadv_hardif_activate_interface(hard_iface);
c6c8fea2
SE
652 break;
653 case NETDEV_GOING_DOWN:
654 case NETDEV_DOWN:
18a1cb6e 655 batadv_hardif_deactivate_interface(hard_iface);
c6c8fea2
SE
656 break;
657 case NETDEV_UNREGISTER:
e6c10f43 658 list_del_rcu(&hard_iface->list);
c6c8fea2 659
18a1cb6e 660 batadv_hardif_remove_interface(hard_iface);
c6c8fea2
SE
661 break;
662 case NETDEV_CHANGEMTU:
e6c10f43 663 if (hard_iface->soft_iface)
9563877e 664 batadv_update_min_mtu(hard_iface->soft_iface);
c6c8fea2
SE
665 break;
666 case NETDEV_CHANGEADDR:
e9a4f295 667 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
c6c8fea2
SE
668 goto hardif_put;
669
18a1cb6e 670 batadv_check_known_mac_addr(hard_iface->net_dev);
c6c8fea2 671
e6c10f43 672 bat_priv = netdev_priv(hard_iface->soft_iface);
c3229398 673 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
01c4224b 674
e5d89254 675 primary_if = batadv_primary_if_get_selected(bat_priv);
32ae9b22
ML
676 if (!primary_if)
677 goto hardif_put;
678
679 if (hard_iface == primary_if)
18a1cb6e 680 batadv_primary_if_update_addr(bat_priv, NULL);
c6c8fea2
SE
681 break;
682 default:
683 break;
f81c6224 684 }
c6c8fea2
SE
685
686hardif_put:
e5d89254 687 batadv_hardif_free_ref(hard_iface);
c6c8fea2 688out:
32ae9b22 689 if (primary_if)
e5d89254 690 batadv_hardif_free_ref(primary_if);
c6c8fea2
SE
691 return NOTIFY_DONE;
692}
693
9563877e 694struct notifier_block batadv_hard_if_notifier = {
18a1cb6e 695 .notifier_call = batadv_hard_if_event,
c6c8fea2 696};