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