2 * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include "hard-interface.h"
24 #include "soft-interface.h"
26 #include "translation-table.h"
28 #include "bat_sysfs.h"
29 #include "originator.h"
31 #include "bridge_loop_avoidance.h"
33 #include <linux/if_arp.h>
35 void hardif_free_rcu(struct rcu_head
*rcu
)
37 struct hard_iface
*hard_iface
;
39 hard_iface
= container_of(rcu
, struct hard_iface
, rcu
);
40 dev_put(hard_iface
->net_dev
);
44 struct hard_iface
*hardif_get_by_netdev(const struct net_device
*net_dev
)
46 struct hard_iface
*hard_iface
;
49 list_for_each_entry_rcu(hard_iface
, &hardif_list
, list
) {
50 if (hard_iface
->net_dev
== net_dev
&&
51 atomic_inc_not_zero(&hard_iface
->refcount
))
62 static int is_valid_iface(const struct net_device
*net_dev
)
64 if (net_dev
->flags
& IFF_LOOPBACK
)
67 if (net_dev
->type
!= ARPHRD_ETHER
)
70 if (net_dev
->addr_len
!= ETH_ALEN
)
73 /* no batman over batman */
74 if (softif_is_valid(net_dev
))
77 /* Device is being bridged */
78 /* if (net_dev->priv_flags & IFF_BRIDGE_PORT)
84 static struct hard_iface
*hardif_get_active(const struct net_device
*soft_iface
)
86 struct hard_iface
*hard_iface
;
89 list_for_each_entry_rcu(hard_iface
, &hardif_list
, list
) {
90 if (hard_iface
->soft_iface
!= soft_iface
)
93 if (hard_iface
->if_status
== IF_ACTIVE
&&
94 atomic_inc_not_zero(&hard_iface
->refcount
))
105 static void primary_if_update_addr(struct bat_priv
*bat_priv
,
106 struct hard_iface
*oldif
)
108 struct vis_packet
*vis_packet
;
109 struct hard_iface
*primary_if
;
111 primary_if
= primary_if_get_selected(bat_priv
);
115 vis_packet
= (struct vis_packet
*)
116 bat_priv
->my_vis_info
->skb_packet
->data
;
117 memcpy(vis_packet
->vis_orig
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
118 memcpy(vis_packet
->sender_orig
,
119 primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
121 bla_update_orig_address(bat_priv
, primary_if
, oldif
);
124 hardif_free_ref(primary_if
);
127 static void primary_if_select(struct bat_priv
*bat_priv
,
128 struct hard_iface
*new_hard_iface
)
130 struct hard_iface
*curr_hard_iface
;
134 if (new_hard_iface
&& !atomic_inc_not_zero(&new_hard_iface
->refcount
))
135 new_hard_iface
= NULL
;
137 curr_hard_iface
= rcu_dereference_protected(bat_priv
->primary_if
, 1);
138 rcu_assign_pointer(bat_priv
->primary_if
, new_hard_iface
);
143 bat_priv
->bat_algo_ops
->bat_primary_iface_set(new_hard_iface
);
144 primary_if_update_addr(bat_priv
, curr_hard_iface
);
148 hardif_free_ref(curr_hard_iface
);
151 static bool hardif_is_iface_up(const struct hard_iface
*hard_iface
)
153 if (hard_iface
->net_dev
->flags
& IFF_UP
)
159 static void check_known_mac_addr(const struct net_device
*net_dev
)
161 const struct hard_iface
*hard_iface
;
164 list_for_each_entry_rcu(hard_iface
, &hardif_list
, list
) {
165 if ((hard_iface
->if_status
!= IF_ACTIVE
) &&
166 (hard_iface
->if_status
!= IF_TO_BE_ACTIVATED
))
169 if (hard_iface
->net_dev
== net_dev
)
172 if (!compare_eth(hard_iface
->net_dev
->dev_addr
,
176 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
177 net_dev
->dev_addr
, hard_iface
->net_dev
->name
);
178 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
183 int hardif_min_mtu(struct net_device
*soft_iface
)
185 const struct bat_priv
*bat_priv
= netdev_priv(soft_iface
);
186 const struct hard_iface
*hard_iface
;
187 /* allow big frames if all devices are capable to do so
188 * (have MTU > 1500 + BAT_HEADER_LEN) */
189 int min_mtu
= ETH_DATA_LEN
;
191 if (atomic_read(&bat_priv
->fragmentation
))
195 list_for_each_entry_rcu(hard_iface
, &hardif_list
, list
) {
196 if ((hard_iface
->if_status
!= IF_ACTIVE
) &&
197 (hard_iface
->if_status
!= IF_TO_BE_ACTIVATED
))
200 if (hard_iface
->soft_iface
!= soft_iface
)
203 min_mtu
= min_t(int, hard_iface
->net_dev
->mtu
- BAT_HEADER_LEN
,
211 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
212 void update_min_mtu(struct net_device
*soft_iface
)
216 min_mtu
= hardif_min_mtu(soft_iface
);
217 if (soft_iface
->mtu
!= min_mtu
)
218 soft_iface
->mtu
= min_mtu
;
221 static void hardif_activate_interface(struct hard_iface
*hard_iface
)
223 struct bat_priv
*bat_priv
;
224 struct hard_iface
*primary_if
= NULL
;
226 if (hard_iface
->if_status
!= IF_INACTIVE
)
229 bat_priv
= netdev_priv(hard_iface
->soft_iface
);
231 bat_priv
->bat_algo_ops
->bat_iface_update_mac(hard_iface
);
232 hard_iface
->if_status
= IF_TO_BE_ACTIVATED
;
235 * the first active interface becomes our primary interface or
236 * the next active interface after the old primary interface was removed
238 primary_if
= primary_if_get_selected(bat_priv
);
240 primary_if_select(bat_priv
, hard_iface
);
242 bat_info(hard_iface
->soft_iface
, "Interface activated: %s\n",
243 hard_iface
->net_dev
->name
);
245 update_min_mtu(hard_iface
->soft_iface
);
249 hardif_free_ref(primary_if
);
252 static void hardif_deactivate_interface(struct hard_iface
*hard_iface
)
254 if ((hard_iface
->if_status
!= IF_ACTIVE
) &&
255 (hard_iface
->if_status
!= IF_TO_BE_ACTIVATED
))
258 hard_iface
->if_status
= IF_INACTIVE
;
260 bat_info(hard_iface
->soft_iface
, "Interface deactivated: %s\n",
261 hard_iface
->net_dev
->name
);
263 update_min_mtu(hard_iface
->soft_iface
);
266 int hardif_enable_interface(struct hard_iface
*hard_iface
,
267 const char *iface_name
)
269 struct bat_priv
*bat_priv
;
270 struct net_device
*soft_iface
;
273 if (hard_iface
->if_status
!= IF_NOT_IN_USE
)
276 if (!atomic_inc_not_zero(&hard_iface
->refcount
))
279 /* hard-interface is part of a bridge */
280 if (hard_iface
->net_dev
->priv_flags
& IFF_BRIDGE_PORT
)
281 pr_err("You are about to enable batman-adv on '%s' which already is part of a bridge. Unless you know exactly what you are doing this is probably wrong and won't work the way you think it would.\n",
282 hard_iface
->net_dev
->name
);
284 soft_iface
= dev_get_by_name(&init_net
, iface_name
);
287 soft_iface
= softif_create(iface_name
);
294 /* dev_get_by_name() increases the reference counter for us */
295 dev_hold(soft_iface
);
298 if (!softif_is_valid(soft_iface
)) {
299 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
305 hard_iface
->soft_iface
= soft_iface
;
306 bat_priv
= netdev_priv(hard_iface
->soft_iface
);
308 ret
= bat_priv
->bat_algo_ops
->bat_iface_enable(hard_iface
);
314 hard_iface
->if_num
= bat_priv
->num_ifaces
;
315 bat_priv
->num_ifaces
++;
316 hard_iface
->if_status
= IF_INACTIVE
;
317 orig_hash_add_if(hard_iface
, bat_priv
->num_ifaces
);
319 hard_iface
->batman_adv_ptype
.type
= __constant_htons(ETH_P_BATMAN
);
320 hard_iface
->batman_adv_ptype
.func
= batman_skb_recv
;
321 hard_iface
->batman_adv_ptype
.dev
= hard_iface
->net_dev
;
322 dev_add_pack(&hard_iface
->batman_adv_ptype
);
324 atomic_set(&hard_iface
->frag_seqno
, 1);
325 bat_info(hard_iface
->soft_iface
, "Adding interface: %s\n",
326 hard_iface
->net_dev
->name
);
328 if (atomic_read(&bat_priv
->fragmentation
) && hard_iface
->net_dev
->mtu
<
329 ETH_DATA_LEN
+ BAT_HEADER_LEN
)
330 bat_info(hard_iface
->soft_iface
,
331 "The MTU of interface %s is too small (%i) to handle the transport of batman-adv packets. Packets going over this interface will be fragmented on layer2 which could impact the performance. Setting the MTU to %zi would solve the problem.\n",
332 hard_iface
->net_dev
->name
, hard_iface
->net_dev
->mtu
,
333 ETH_DATA_LEN
+ BAT_HEADER_LEN
);
335 if (!atomic_read(&bat_priv
->fragmentation
) && hard_iface
->net_dev
->mtu
<
336 ETH_DATA_LEN
+ BAT_HEADER_LEN
)
337 bat_info(hard_iface
->soft_iface
,
338 "The MTU of interface %s is too small (%i) to handle the transport of batman-adv packets. If you experience problems getting traffic through try increasing the MTU to %zi.\n",
339 hard_iface
->net_dev
->name
, hard_iface
->net_dev
->mtu
,
340 ETH_DATA_LEN
+ BAT_HEADER_LEN
);
342 if (hardif_is_iface_up(hard_iface
))
343 hardif_activate_interface(hard_iface
);
345 bat_err(hard_iface
->soft_iface
,
346 "Not using interface %s (retrying later): interface not active\n",
347 hard_iface
->net_dev
->name
);
349 /* begin scheduling originator messages on that interface */
350 schedule_bat_ogm(hard_iface
);
358 hardif_free_ref(hard_iface
);
362 void hardif_disable_interface(struct hard_iface
*hard_iface
)
364 struct bat_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
365 struct hard_iface
*primary_if
= NULL
;
367 if (hard_iface
->if_status
== IF_ACTIVE
)
368 hardif_deactivate_interface(hard_iface
);
370 if (hard_iface
->if_status
!= IF_INACTIVE
)
373 bat_info(hard_iface
->soft_iface
, "Removing interface: %s\n",
374 hard_iface
->net_dev
->name
);
375 dev_remove_pack(&hard_iface
->batman_adv_ptype
);
377 bat_priv
->num_ifaces
--;
378 orig_hash_del_if(hard_iface
, bat_priv
->num_ifaces
);
380 primary_if
= primary_if_get_selected(bat_priv
);
381 if (hard_iface
== primary_if
) {
382 struct hard_iface
*new_if
;
384 new_if
= hardif_get_active(hard_iface
->soft_iface
);
385 primary_if_select(bat_priv
, new_if
);
388 hardif_free_ref(new_if
);
391 bat_priv
->bat_algo_ops
->bat_iface_disable(hard_iface
);
392 hard_iface
->if_status
= IF_NOT_IN_USE
;
394 /* delete all references to this hard_iface */
395 purge_orig_ref(bat_priv
);
396 purge_outstanding_packets(bat_priv
, hard_iface
);
397 dev_put(hard_iface
->soft_iface
);
399 /* nobody uses this interface anymore */
400 if (!bat_priv
->num_ifaces
)
401 softif_destroy(hard_iface
->soft_iface
);
403 hard_iface
->soft_iface
= NULL
;
404 hardif_free_ref(hard_iface
);
408 hardif_free_ref(primary_if
);
411 static struct hard_iface
*hardif_add_interface(struct net_device
*net_dev
)
413 struct hard_iface
*hard_iface
;
418 ret
= is_valid_iface(net_dev
);
424 hard_iface
= kmalloc(sizeof(*hard_iface
), GFP_ATOMIC
);
428 ret
= sysfs_add_hardif(&hard_iface
->hardif_obj
, net_dev
);
432 hard_iface
->if_num
= -1;
433 hard_iface
->net_dev
= net_dev
;
434 hard_iface
->soft_iface
= NULL
;
435 hard_iface
->if_status
= IF_NOT_IN_USE
;
436 INIT_LIST_HEAD(&hard_iface
->list
);
437 /* extra reference for return */
438 atomic_set(&hard_iface
->refcount
, 2);
440 check_known_mac_addr(hard_iface
->net_dev
);
441 list_add_tail_rcu(&hard_iface
->list
, &hardif_list
);
444 * This can't be called via a bat_priv callback because
445 * we have no bat_priv yet.
447 atomic_set(&hard_iface
->seqno
, 1);
448 hard_iface
->packet_buff
= NULL
;
460 static void hardif_remove_interface(struct hard_iface
*hard_iface
)
464 /* first deactivate interface */
465 if (hard_iface
->if_status
!= IF_NOT_IN_USE
)
466 hardif_disable_interface(hard_iface
);
468 if (hard_iface
->if_status
!= IF_NOT_IN_USE
)
471 hard_iface
->if_status
= IF_TO_BE_REMOVED
;
472 sysfs_del_hardif(&hard_iface
->hardif_obj
);
473 hardif_free_ref(hard_iface
);
476 void hardif_remove_interfaces(void)
478 struct hard_iface
*hard_iface
, *hard_iface_tmp
;
481 list_for_each_entry_safe(hard_iface
, hard_iface_tmp
,
482 &hardif_list
, list
) {
483 list_del_rcu(&hard_iface
->list
);
484 hardif_remove_interface(hard_iface
);
489 static int hard_if_event(struct notifier_block
*this,
490 unsigned long event
, void *ptr
)
492 struct net_device
*net_dev
= ptr
;
493 struct hard_iface
*hard_iface
= hardif_get_by_netdev(net_dev
);
494 struct hard_iface
*primary_if
= NULL
;
495 struct bat_priv
*bat_priv
;
497 if (!hard_iface
&& event
== NETDEV_REGISTER
)
498 hard_iface
= hardif_add_interface(net_dev
);
505 hardif_activate_interface(hard_iface
);
507 case NETDEV_GOING_DOWN
:
509 hardif_deactivate_interface(hard_iface
);
511 case NETDEV_UNREGISTER
:
512 list_del_rcu(&hard_iface
->list
);
514 hardif_remove_interface(hard_iface
);
516 case NETDEV_CHANGEMTU
:
517 if (hard_iface
->soft_iface
)
518 update_min_mtu(hard_iface
->soft_iface
);
520 case NETDEV_CHANGEADDR
:
521 if (hard_iface
->if_status
== IF_NOT_IN_USE
)
524 check_known_mac_addr(hard_iface
->net_dev
);
526 bat_priv
= netdev_priv(hard_iface
->soft_iface
);
527 bat_priv
->bat_algo_ops
->bat_iface_update_mac(hard_iface
);
529 primary_if
= primary_if_get_selected(bat_priv
);
533 if (hard_iface
== primary_if
)
534 primary_if_update_addr(bat_priv
, NULL
);
541 hardif_free_ref(hard_iface
);
544 hardif_free_ref(primary_if
);
548 /* This function returns true if the interface represented by ifindex is a
549 * 802.11 wireless device */
550 bool is_wifi_iface(int ifindex
)
552 struct net_device
*net_device
= NULL
;
555 if (ifindex
== NULL_IFINDEX
)
558 net_device
= dev_get_by_index(&init_net
, ifindex
);
562 #ifdef CONFIG_WIRELESS_EXT
563 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
564 * check for wireless_handlers != NULL */
565 if (net_device
->wireless_handlers
)
569 /* cfg80211 drivers have to set ieee80211_ptr */
570 if (net_device
->ieee80211_ptr
)
578 struct notifier_block hard_if_notifier
= {
579 .notifier_call
= hard_if_event
,