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