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