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