]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/batman-adv/hard-interface.c
batman-adv: Prefix unicast static inline functions with batadv_
[mirror_ubuntu-bionic-kernel.git] / net / batman-adv / hard-interface.c
CommitLineData
9cfc7bd6 1/* Copyright (C) 2007-2012 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"
21#include "hard-interface.h"
22#include "soft-interface.h"
23#include "send.h"
24#include "translation-table.h"
25#include "routing.h"
26#include "bat_sysfs.h"
27#include "originator.h"
28#include "hash.h"
23721387 29#include "bridge_loop_avoidance.h"
c6c8fea2
SE
30
31#include <linux/if_arp.h>
32
9563877e 33void batadv_hardif_free_rcu(struct rcu_head *rcu)
c6c8fea2 34{
e6c10f43 35 struct hard_iface *hard_iface;
c6c8fea2 36
e6c10f43
ML
37 hard_iface = container_of(rcu, struct hard_iface, rcu);
38 dev_put(hard_iface->net_dev);
39 kfree(hard_iface);
c6c8fea2
SE
40}
41
9563877e 42struct hard_iface *batadv_hardif_get_by_netdev(const struct net_device *net_dev)
c6c8fea2 43{
e6c10f43 44 struct hard_iface *hard_iface;
c6c8fea2
SE
45
46 rcu_read_lock();
3193e8fd 47 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
e6c10f43
ML
48 if (hard_iface->net_dev == net_dev &&
49 atomic_inc_not_zero(&hard_iface->refcount))
c6c8fea2
SE
50 goto out;
51 }
52
e6c10f43 53 hard_iface = NULL;
c6c8fea2
SE
54
55out:
c6c8fea2 56 rcu_read_unlock();
e6c10f43 57 return hard_iface;
c6c8fea2
SE
58}
59
747e4221 60static int is_valid_iface(const struct net_device *net_dev)
c6c8fea2
SE
61{
62 if (net_dev->flags & IFF_LOOPBACK)
63 return 0;
64
65 if (net_dev->type != ARPHRD_ETHER)
66 return 0;
67
68 if (net_dev->addr_len != ETH_ALEN)
69 return 0;
70
71 /* no batman over batman */
04b482a2 72 if (batadv_softif_is_valid(net_dev))
c6c8fea2 73 return 0;
c6c8fea2 74
c6c8fea2
SE
75 return 1;
76}
77
747e4221 78static struct hard_iface *hardif_get_active(const struct net_device *soft_iface)
c6c8fea2 79{
e6c10f43 80 struct hard_iface *hard_iface;
c6c8fea2
SE
81
82 rcu_read_lock();
3193e8fd 83 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
e6c10f43 84 if (hard_iface->soft_iface != soft_iface)
c6c8fea2
SE
85 continue;
86
e6c10f43
ML
87 if (hard_iface->if_status == IF_ACTIVE &&
88 atomic_inc_not_zero(&hard_iface->refcount))
c6c8fea2
SE
89 goto out;
90 }
91
e6c10f43 92 hard_iface = NULL;
c6c8fea2
SE
93
94out:
c6c8fea2 95 rcu_read_unlock();
e6c10f43 96 return hard_iface;
c6c8fea2
SE
97}
98
23721387
SW
99static void primary_if_update_addr(struct bat_priv *bat_priv,
100 struct hard_iface *oldif)
c6c8fea2
SE
101{
102 struct vis_packet *vis_packet;
32ae9b22
ML
103 struct hard_iface *primary_if;
104
e5d89254 105 primary_if = batadv_primary_if_get_selected(bat_priv);
32ae9b22
ML
106 if (!primary_if)
107 goto out;
c6c8fea2
SE
108
109 vis_packet = (struct vis_packet *)
110 bat_priv->my_vis_info->skb_packet->data;
32ae9b22 111 memcpy(vis_packet->vis_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
c6c8fea2 112 memcpy(vis_packet->sender_orig,
32ae9b22
ML
113 primary_if->net_dev->dev_addr, ETH_ALEN);
114
08adf151 115 batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
32ae9b22
ML
116out:
117 if (primary_if)
e5d89254 118 batadv_hardif_free_ref(primary_if);
c6c8fea2
SE
119}
120
32ae9b22
ML
121static void primary_if_select(struct bat_priv *bat_priv,
122 struct hard_iface *new_hard_iface)
c6c8fea2 123{
32ae9b22 124 struct hard_iface *curr_hard_iface;
c6c8fea2 125
c3caf519 126 ASSERT_RTNL();
c6c8fea2 127
32ae9b22
ML
128 if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount))
129 new_hard_iface = NULL;
c6c8fea2 130
728cbc6a 131 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
32ae9b22 132 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
c6c8fea2 133
32ae9b22 134 if (!new_hard_iface)
23721387 135 goto out;
32ae9b22 136
cd8b78e7 137 bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface);
23721387
SW
138 primary_if_update_addr(bat_priv, curr_hard_iface);
139
140out:
141 if (curr_hard_iface)
e5d89254 142 batadv_hardif_free_ref(curr_hard_iface);
c6c8fea2
SE
143}
144
747e4221 145static bool hardif_is_iface_up(const struct hard_iface *hard_iface)
c6c8fea2 146{
e6c10f43 147 if (hard_iface->net_dev->flags & IFF_UP)
c6c8fea2
SE
148 return true;
149
150 return false;
151}
152
747e4221 153static void check_known_mac_addr(const struct net_device *net_dev)
c6c8fea2 154{
747e4221 155 const struct hard_iface *hard_iface;
c6c8fea2
SE
156
157 rcu_read_lock();
3193e8fd 158 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
e6c10f43
ML
159 if ((hard_iface->if_status != IF_ACTIVE) &&
160 (hard_iface->if_status != IF_TO_BE_ACTIVATED))
c6c8fea2
SE
161 continue;
162
e6c10f43 163 if (hard_iface->net_dev == net_dev)
c6c8fea2
SE
164 continue;
165
e6c10f43
ML
166 if (!compare_eth(hard_iface->net_dev->dev_addr,
167 net_dev->dev_addr))
c6c8fea2
SE
168 continue;
169
67969581
SE
170 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
171 net_dev->dev_addr, hard_iface->net_dev->name);
172 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
c6c8fea2
SE
173 }
174 rcu_read_unlock();
175}
176
9563877e 177int batadv_hardif_min_mtu(struct net_device *soft_iface)
c6c8fea2 178{
747e4221
SE
179 const struct bat_priv *bat_priv = netdev_priv(soft_iface);
180 const struct hard_iface *hard_iface;
c6c8fea2 181 /* allow big frames if all devices are capable to do so
9cfc7bd6
SE
182 * (have MTU > 1500 + BAT_HEADER_LEN)
183 */
c6c8fea2
SE
184 int min_mtu = ETH_DATA_LEN;
185
186 if (atomic_read(&bat_priv->fragmentation))
187 goto out;
188
189 rcu_read_lock();
3193e8fd 190 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
e6c10f43
ML
191 if ((hard_iface->if_status != IF_ACTIVE) &&
192 (hard_iface->if_status != IF_TO_BE_ACTIVATED))
c6c8fea2
SE
193 continue;
194
e6c10f43 195 if (hard_iface->soft_iface != soft_iface)
c6c8fea2
SE
196 continue;
197
e6c10f43 198 min_mtu = min_t(int, hard_iface->net_dev->mtu - BAT_HEADER_LEN,
c6c8fea2
SE
199 min_mtu);
200 }
201 rcu_read_unlock();
202out:
203 return min_mtu;
204}
205
206/* adjusts the MTU if a new interface with a smaller MTU appeared. */
9563877e 207void batadv_update_min_mtu(struct net_device *soft_iface)
c6c8fea2
SE
208{
209 int min_mtu;
210
9563877e 211 min_mtu = batadv_hardif_min_mtu(soft_iface);
c6c8fea2
SE
212 if (soft_iface->mtu != min_mtu)
213 soft_iface->mtu = min_mtu;
214}
215
e6c10f43 216static void hardif_activate_interface(struct hard_iface *hard_iface)
c6c8fea2
SE
217{
218 struct bat_priv *bat_priv;
32ae9b22 219 struct hard_iface *primary_if = NULL;
c6c8fea2 220
e6c10f43 221 if (hard_iface->if_status != IF_INACTIVE)
32ae9b22 222 goto out;
c6c8fea2 223
e6c10f43 224 bat_priv = netdev_priv(hard_iface->soft_iface);
c6c8fea2 225
c3229398 226 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
e6c10f43 227 hard_iface->if_status = IF_TO_BE_ACTIVATED;
c6c8fea2 228
9cfc7bd6 229 /* the first active interface becomes our primary interface or
015758d0 230 * the next active interface after the old primary interface was removed
c6c8fea2 231 */
e5d89254 232 primary_if = batadv_primary_if_get_selected(bat_priv);
32ae9b22
ML
233 if (!primary_if)
234 primary_if_select(bat_priv, hard_iface);
c6c8fea2 235
e6c10f43
ML
236 bat_info(hard_iface->soft_iface, "Interface activated: %s\n",
237 hard_iface->net_dev->name);
c6c8fea2 238
9563877e 239 batadv_update_min_mtu(hard_iface->soft_iface);
32ae9b22
ML
240
241out:
242 if (primary_if)
e5d89254 243 batadv_hardif_free_ref(primary_if);
c6c8fea2
SE
244}
245
e6c10f43 246static void hardif_deactivate_interface(struct hard_iface *hard_iface)
c6c8fea2 247{
e6c10f43
ML
248 if ((hard_iface->if_status != IF_ACTIVE) &&
249 (hard_iface->if_status != IF_TO_BE_ACTIVATED))
c6c8fea2
SE
250 return;
251
e6c10f43 252 hard_iface->if_status = IF_INACTIVE;
c6c8fea2 253
e6c10f43
ML
254 bat_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
255 hard_iface->net_dev->name);
c6c8fea2 256
9563877e 257 batadv_update_min_mtu(hard_iface->soft_iface);
c6c8fea2
SE
258}
259
9563877e
SE
260int batadv_hardif_enable_interface(struct hard_iface *hard_iface,
261 const char *iface_name)
c6c8fea2
SE
262{
263 struct bat_priv *bat_priv;
e44d8fe2
SE
264 struct net_device *soft_iface;
265 int ret;
c6c8fea2 266
e6c10f43 267 if (hard_iface->if_status != IF_NOT_IN_USE)
c6c8fea2
SE
268 goto out;
269
e6c10f43 270 if (!atomic_inc_not_zero(&hard_iface->refcount))
ed75ccbe
ML
271 goto out;
272
6e242f90
ML
273 /* hard-interface is part of a bridge */
274 if (hard_iface->net_dev->priv_flags & IFF_BRIDGE_PORT)
86ceb360 275 pr_err("You are about to enable batman-adv on '%s' which already is part of a bridge. Unless you know exactly what you are doing this is probably wrong and won't work the way you think it would.\n",
6e242f90
ML
276 hard_iface->net_dev->name);
277
e44d8fe2 278 soft_iface = dev_get_by_name(&init_net, iface_name);
c6c8fea2 279
e44d8fe2 280 if (!soft_iface) {
04b482a2 281 soft_iface = batadv_softif_create(iface_name);
c6c8fea2 282
e44d8fe2
SE
283 if (!soft_iface) {
284 ret = -ENOMEM;
c6c8fea2 285 goto err;
e44d8fe2 286 }
c6c8fea2
SE
287
288 /* dev_get_by_name() increases the reference counter for us */
e44d8fe2
SE
289 dev_hold(soft_iface);
290 }
291
04b482a2 292 if (!batadv_softif_is_valid(soft_iface)) {
86ceb360 293 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
e44d8fe2 294 soft_iface->name);
e44d8fe2 295 ret = -EINVAL;
77af7575 296 goto err_dev;
c6c8fea2
SE
297 }
298
e44d8fe2 299 hard_iface->soft_iface = soft_iface;
e6c10f43 300 bat_priv = netdev_priv(hard_iface->soft_iface);
d0b9fd89 301
77af7575 302 ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
5346c35e 303 if (ret < 0)
77af7575 304 goto err_dev;
c6c8fea2 305
e6c10f43 306 hard_iface->if_num = bat_priv->num_ifaces;
c6c8fea2 307 bat_priv->num_ifaces++;
e6c10f43 308 hard_iface->if_status = IF_INACTIVE;
7d211efc 309 batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
c6c8fea2 310
e6c10f43 311 hard_iface->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
3193e8fd 312 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
e6c10f43
ML
313 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
314 dev_add_pack(&hard_iface->batman_adv_ptype);
c6c8fea2 315
e6c10f43
ML
316 atomic_set(&hard_iface->frag_seqno, 1);
317 bat_info(hard_iface->soft_iface, "Adding interface: %s\n",
318 hard_iface->net_dev->name);
c6c8fea2 319
e6c10f43 320 if (atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
c6c8fea2 321 ETH_DATA_LEN + BAT_HEADER_LEN)
e6c10f43 322 bat_info(hard_iface->soft_iface,
86ceb360 323 "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 %zi would solve the problem.\n",
7c64fd98
SE
324 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
325 ETH_DATA_LEN + BAT_HEADER_LEN);
c6c8fea2 326
e6c10f43 327 if (!atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
c6c8fea2 328 ETH_DATA_LEN + BAT_HEADER_LEN)
e6c10f43 329 bat_info(hard_iface->soft_iface,
86ceb360 330 "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 %zi.\n",
7c64fd98
SE
331 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
332 ETH_DATA_LEN + BAT_HEADER_LEN);
c6c8fea2 333
e6c10f43
ML
334 if (hardif_is_iface_up(hard_iface))
335 hardif_activate_interface(hard_iface);
c6c8fea2 336 else
86ceb360
SE
337 bat_err(hard_iface->soft_iface,
338 "Not using interface %s (retrying later): interface not active\n",
e6c10f43 339 hard_iface->net_dev->name);
c6c8fea2
SE
340
341 /* begin scheduling originator messages on that interface */
9455e34c 342 batadv_schedule_bat_ogm(hard_iface);
c6c8fea2
SE
343
344out:
345 return 0;
346
77af7575
ML
347err_dev:
348 dev_put(soft_iface);
c6c8fea2 349err:
e5d89254 350 batadv_hardif_free_ref(hard_iface);
e44d8fe2 351 return ret;
c6c8fea2
SE
352}
353
9563877e 354void batadv_hardif_disable_interface(struct hard_iface *hard_iface)
c6c8fea2 355{
e6c10f43 356 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
32ae9b22 357 struct hard_iface *primary_if = NULL;
c6c8fea2 358
e6c10f43
ML
359 if (hard_iface->if_status == IF_ACTIVE)
360 hardif_deactivate_interface(hard_iface);
c6c8fea2 361
e6c10f43 362 if (hard_iface->if_status != IF_INACTIVE)
32ae9b22 363 goto out;
c6c8fea2 364
e6c10f43
ML
365 bat_info(hard_iface->soft_iface, "Removing interface: %s\n",
366 hard_iface->net_dev->name);
367 dev_remove_pack(&hard_iface->batman_adv_ptype);
c6c8fea2
SE
368
369 bat_priv->num_ifaces--;
7d211efc 370 batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
c6c8fea2 371
e5d89254 372 primary_if = batadv_primary_if_get_selected(bat_priv);
32ae9b22 373 if (hard_iface == primary_if) {
e6c10f43 374 struct hard_iface *new_if;
c6c8fea2 375
e6c10f43 376 new_if = hardif_get_active(hard_iface->soft_iface);
32ae9b22 377 primary_if_select(bat_priv, new_if);
c6c8fea2
SE
378
379 if (new_if)
e5d89254 380 batadv_hardif_free_ref(new_if);
c6c8fea2
SE
381 }
382
00a50076 383 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
e6c10f43 384 hard_iface->if_status = IF_NOT_IN_USE;
c6c8fea2 385
e6c10f43 386 /* delete all references to this hard_iface */
7d211efc 387 batadv_purge_orig_ref(bat_priv);
9455e34c 388 batadv_purge_outstanding_packets(bat_priv, hard_iface);
e6c10f43 389 dev_put(hard_iface->soft_iface);
c6c8fea2
SE
390
391 /* nobody uses this interface anymore */
392 if (!bat_priv->num_ifaces)
04b482a2 393 batadv_softif_destroy(hard_iface->soft_iface);
c6c8fea2 394
e6c10f43 395 hard_iface->soft_iface = NULL;
e5d89254 396 batadv_hardif_free_ref(hard_iface);
32ae9b22
ML
397
398out:
399 if (primary_if)
e5d89254 400 batadv_hardif_free_ref(primary_if);
c6c8fea2
SE
401}
402
e6c10f43 403static struct hard_iface *hardif_add_interface(struct net_device *net_dev)
c6c8fea2 404{
e6c10f43 405 struct hard_iface *hard_iface;
c6c8fea2
SE
406 int ret;
407
c3caf519
SE
408 ASSERT_RTNL();
409
c6c8fea2
SE
410 ret = is_valid_iface(net_dev);
411 if (ret != 1)
412 goto out;
413
414 dev_hold(net_dev);
415
704509b8 416 hard_iface = kmalloc(sizeof(*hard_iface), GFP_ATOMIC);
320f422f 417 if (!hard_iface)
c6c8fea2 418 goto release_dev;
c6c8fea2 419
5853e22c 420 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
c6c8fea2
SE
421 if (ret)
422 goto free_if;
423
e6c10f43
ML
424 hard_iface->if_num = -1;
425 hard_iface->net_dev = net_dev;
426 hard_iface->soft_iface = NULL;
427 hard_iface->if_status = IF_NOT_IN_USE;
428 INIT_LIST_HEAD(&hard_iface->list);
ed75ccbe 429 /* extra reference for return */
e6c10f43 430 atomic_set(&hard_iface->refcount, 2);
c6c8fea2 431
e6c10f43 432 check_known_mac_addr(hard_iface->net_dev);
3193e8fd 433 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
c6c8fea2 434
9cfc7bd6 435 /* This can't be called via a bat_priv callback because
8140625e
ML
436 * we have no bat_priv yet.
437 */
438 atomic_set(&hard_iface->seqno, 1);
439 hard_iface->packet_buff = NULL;
440
e6c10f43 441 return hard_iface;
c6c8fea2
SE
442
443free_if:
e6c10f43 444 kfree(hard_iface);
c6c8fea2
SE
445release_dev:
446 dev_put(net_dev);
447out:
448 return NULL;
449}
450
e6c10f43 451static void hardif_remove_interface(struct hard_iface *hard_iface)
c6c8fea2 452{
c3caf519
SE
453 ASSERT_RTNL();
454
c6c8fea2 455 /* first deactivate interface */
e6c10f43 456 if (hard_iface->if_status != IF_NOT_IN_USE)
9563877e 457 batadv_hardif_disable_interface(hard_iface);
c6c8fea2 458
e6c10f43 459 if (hard_iface->if_status != IF_NOT_IN_USE)
c6c8fea2
SE
460 return;
461
e6c10f43 462 hard_iface->if_status = IF_TO_BE_REMOVED;
5853e22c 463 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
e5d89254 464 batadv_hardif_free_ref(hard_iface);
c6c8fea2
SE
465}
466
9563877e 467void batadv_hardif_remove_interfaces(void)
c6c8fea2 468{
e6c10f43 469 struct hard_iface *hard_iface, *hard_iface_tmp;
c6c8fea2 470
c3caf519 471 rtnl_lock();
e6c10f43 472 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
3193e8fd 473 &batadv_hardif_list, list) {
e6c10f43 474 list_del_rcu(&hard_iface->list);
e6c10f43 475 hardif_remove_interface(hard_iface);
c6c8fea2
SE
476 }
477 rtnl_unlock();
478}
479
480static int hard_if_event(struct notifier_block *this,
481 unsigned long event, void *ptr)
482{
5f718c20 483 struct net_device *net_dev = ptr;
9563877e 484 struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
32ae9b22 485 struct hard_iface *primary_if = NULL;
c6c8fea2
SE
486 struct bat_priv *bat_priv;
487
e6c10f43
ML
488 if (!hard_iface && event == NETDEV_REGISTER)
489 hard_iface = hardif_add_interface(net_dev);
c6c8fea2 490
e6c10f43 491 if (!hard_iface)
c6c8fea2
SE
492 goto out;
493
494 switch (event) {
495 case NETDEV_UP:
e6c10f43 496 hardif_activate_interface(hard_iface);
c6c8fea2
SE
497 break;
498 case NETDEV_GOING_DOWN:
499 case NETDEV_DOWN:
e6c10f43 500 hardif_deactivate_interface(hard_iface);
c6c8fea2
SE
501 break;
502 case NETDEV_UNREGISTER:
e6c10f43 503 list_del_rcu(&hard_iface->list);
c6c8fea2 504
e6c10f43 505 hardif_remove_interface(hard_iface);
c6c8fea2
SE
506 break;
507 case NETDEV_CHANGEMTU:
e6c10f43 508 if (hard_iface->soft_iface)
9563877e 509 batadv_update_min_mtu(hard_iface->soft_iface);
c6c8fea2
SE
510 break;
511 case NETDEV_CHANGEADDR:
e6c10f43 512 if (hard_iface->if_status == IF_NOT_IN_USE)
c6c8fea2
SE
513 goto hardif_put;
514
e6c10f43 515 check_known_mac_addr(hard_iface->net_dev);
c6c8fea2 516
e6c10f43 517 bat_priv = netdev_priv(hard_iface->soft_iface);
c3229398 518 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
01c4224b 519
e5d89254 520 primary_if = batadv_primary_if_get_selected(bat_priv);
32ae9b22
ML
521 if (!primary_if)
522 goto hardif_put;
523
524 if (hard_iface == primary_if)
23721387 525 primary_if_update_addr(bat_priv, NULL);
c6c8fea2
SE
526 break;
527 default:
528 break;
f81c6224 529 }
c6c8fea2
SE
530
531hardif_put:
e5d89254 532 batadv_hardif_free_ref(hard_iface);
c6c8fea2 533out:
32ae9b22 534 if (primary_if)
e5d89254 535 batadv_hardif_free_ref(primary_if);
c6c8fea2
SE
536 return NOTIFY_DONE;
537}
538
bc279080 539/* This function returns true if the interface represented by ifindex is a
9cfc7bd6
SE
540 * 802.11 wireless device
541 */
9563877e 542bool batadv_is_wifi_iface(int ifindex)
bc279080
AQ
543{
544 struct net_device *net_device = NULL;
545 bool ret = false;
546
547 if (ifindex == NULL_IFINDEX)
548 goto out;
549
550 net_device = dev_get_by_index(&init_net, ifindex);
551 if (!net_device)
552 goto out;
553
554#ifdef CONFIG_WIRELESS_EXT
555 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
9cfc7bd6
SE
556 * check for wireless_handlers != NULL
557 */
bc279080
AQ
558 if (net_device->wireless_handlers)
559 ret = true;
560 else
561#endif
562 /* cfg80211 drivers have to set ieee80211_ptr */
563 if (net_device->ieee80211_ptr)
564 ret = true;
565out:
566 if (net_device)
567 dev_put(net_device);
568 return ret;
569}
570
9563877e 571struct notifier_block batadv_hard_if_notifier = {
c6c8fea2
SE
572 .notifier_call = hard_if_event,
573};