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