1 /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich
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.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include "hard-interface.h"
22 #include "soft-interface.h"
24 #include "translation-table.h"
26 #include "bat_sysfs.h"
27 #include "originator.h"
29 #include "bridge_loop_avoidance.h"
31 #include <linux/if_arp.h>
33 void batadv_hardif_free_rcu(struct rcu_head
*rcu
)
35 struct hard_iface
*hard_iface
;
37 hard_iface
= container_of(rcu
, struct hard_iface
, rcu
);
38 dev_put(hard_iface
->net_dev
);
42 struct hard_iface
*batadv_hardif_get_by_netdev(const struct net_device
*net_dev
)
44 struct hard_iface
*hard_iface
;
47 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
48 if (hard_iface
->net_dev
== net_dev
&&
49 atomic_inc_not_zero(&hard_iface
->refcount
))
60 static int batadv_is_valid_iface(const struct net_device
*net_dev
)
62 if (net_dev
->flags
& IFF_LOOPBACK
)
65 if (net_dev
->type
!= ARPHRD_ETHER
)
68 if (net_dev
->addr_len
!= ETH_ALEN
)
71 /* no batman over batman */
72 if (batadv_softif_is_valid(net_dev
))
78 static struct hard_iface
*
79 batadv_hardif_get_active(const struct net_device
*soft_iface
)
81 struct hard_iface
*hard_iface
;
84 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
85 if (hard_iface
->soft_iface
!= soft_iface
)
88 if (hard_iface
->if_status
== IF_ACTIVE
&&
89 atomic_inc_not_zero(&hard_iface
->refcount
))
100 static void batadv_primary_if_update_addr(struct bat_priv
*bat_priv
,
101 struct hard_iface
*oldif
)
103 struct vis_packet
*vis_packet
;
104 struct hard_iface
*primary_if
;
106 primary_if
= batadv_primary_if_get_selected(bat_priv
);
110 vis_packet
= (struct vis_packet
*)
111 bat_priv
->my_vis_info
->skb_packet
->data
;
112 memcpy(vis_packet
->vis_orig
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
113 memcpy(vis_packet
->sender_orig
,
114 primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
116 batadv_bla_update_orig_address(bat_priv
, primary_if
, oldif
);
119 batadv_hardif_free_ref(primary_if
);
122 static void batadv_primary_if_select(struct bat_priv
*bat_priv
,
123 struct hard_iface
*new_hard_iface
)
125 struct hard_iface
*curr_hard_iface
;
129 if (new_hard_iface
&& !atomic_inc_not_zero(&new_hard_iface
->refcount
))
130 new_hard_iface
= NULL
;
132 curr_hard_iface
= rcu_dereference_protected(bat_priv
->primary_if
, 1);
133 rcu_assign_pointer(bat_priv
->primary_if
, new_hard_iface
);
138 bat_priv
->bat_algo_ops
->bat_primary_iface_set(new_hard_iface
);
139 batadv_primary_if_update_addr(bat_priv
, curr_hard_iface
);
143 batadv_hardif_free_ref(curr_hard_iface
);
146 static bool batadv_hardif_is_iface_up(const struct hard_iface
*hard_iface
)
148 if (hard_iface
->net_dev
->flags
& IFF_UP
)
154 static void batadv_check_known_mac_addr(const struct net_device
*net_dev
)
156 const struct hard_iface
*hard_iface
;
159 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
160 if ((hard_iface
->if_status
!= IF_ACTIVE
) &&
161 (hard_iface
->if_status
!= IF_TO_BE_ACTIVATED
))
164 if (hard_iface
->net_dev
== net_dev
)
167 if (!batadv_compare_eth(hard_iface
->net_dev
->dev_addr
,
171 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
172 net_dev
->dev_addr
, hard_iface
->net_dev
->name
);
173 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
178 int batadv_hardif_min_mtu(struct net_device
*soft_iface
)
180 const struct bat_priv
*bat_priv
= netdev_priv(soft_iface
);
181 const struct hard_iface
*hard_iface
;
182 /* allow big frames if all devices are capable to do so
183 * (have MTU > 1500 + BAT_HEADER_LEN)
185 int min_mtu
= ETH_DATA_LEN
;
187 if (atomic_read(&bat_priv
->fragmentation
))
191 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
192 if ((hard_iface
->if_status
!= IF_ACTIVE
) &&
193 (hard_iface
->if_status
!= IF_TO_BE_ACTIVATED
))
196 if (hard_iface
->soft_iface
!= soft_iface
)
199 min_mtu
= min_t(int, hard_iface
->net_dev
->mtu
- BAT_HEADER_LEN
,
207 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
208 void batadv_update_min_mtu(struct net_device
*soft_iface
)
212 min_mtu
= batadv_hardif_min_mtu(soft_iface
);
213 if (soft_iface
->mtu
!= min_mtu
)
214 soft_iface
->mtu
= min_mtu
;
217 static void batadv_hardif_activate_interface(struct hard_iface
*hard_iface
)
219 struct bat_priv
*bat_priv
;
220 struct hard_iface
*primary_if
= NULL
;
222 if (hard_iface
->if_status
!= IF_INACTIVE
)
225 bat_priv
= netdev_priv(hard_iface
->soft_iface
);
227 bat_priv
->bat_algo_ops
->bat_iface_update_mac(hard_iface
);
228 hard_iface
->if_status
= IF_TO_BE_ACTIVATED
;
230 /* the first active interface becomes our primary interface or
231 * the next active interface after the old primary interface was removed
233 primary_if
= batadv_primary_if_get_selected(bat_priv
);
235 batadv_primary_if_select(bat_priv
, hard_iface
);
237 batadv_info(hard_iface
->soft_iface
, "Interface activated: %s\n",
238 hard_iface
->net_dev
->name
);
240 batadv_update_min_mtu(hard_iface
->soft_iface
);
244 batadv_hardif_free_ref(primary_if
);
247 static void batadv_hardif_deactivate_interface(struct hard_iface
*hard_iface
)
249 if ((hard_iface
->if_status
!= IF_ACTIVE
) &&
250 (hard_iface
->if_status
!= IF_TO_BE_ACTIVATED
))
253 hard_iface
->if_status
= IF_INACTIVE
;
255 batadv_info(hard_iface
->soft_iface
, "Interface deactivated: %s\n",
256 hard_iface
->net_dev
->name
);
258 batadv_update_min_mtu(hard_iface
->soft_iface
);
261 int batadv_hardif_enable_interface(struct hard_iface
*hard_iface
,
262 const char *iface_name
)
264 struct bat_priv
*bat_priv
;
265 struct net_device
*soft_iface
;
266 __be16 ethertype
= __constant_htons(BATADV_ETH_P_BATMAN
);
269 if (hard_iface
->if_status
!= IF_NOT_IN_USE
)
272 if (!atomic_inc_not_zero(&hard_iface
->refcount
))
275 /* hard-interface is part of a bridge */
276 if (hard_iface
->net_dev
->priv_flags
& IFF_BRIDGE_PORT
)
277 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",
278 hard_iface
->net_dev
->name
);
280 soft_iface
= dev_get_by_name(&init_net
, iface_name
);
283 soft_iface
= batadv_softif_create(iface_name
);
290 /* dev_get_by_name() increases the reference counter for us */
291 dev_hold(soft_iface
);
294 if (!batadv_softif_is_valid(soft_iface
)) {
295 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
301 hard_iface
->soft_iface
= soft_iface
;
302 bat_priv
= netdev_priv(hard_iface
->soft_iface
);
304 ret
= bat_priv
->bat_algo_ops
->bat_iface_enable(hard_iface
);
308 hard_iface
->if_num
= bat_priv
->num_ifaces
;
309 bat_priv
->num_ifaces
++;
310 hard_iface
->if_status
= IF_INACTIVE
;
311 batadv_orig_hash_add_if(hard_iface
, bat_priv
->num_ifaces
);
313 hard_iface
->batman_adv_ptype
.type
= ethertype
;
314 hard_iface
->batman_adv_ptype
.func
= batadv_batman_skb_recv
;
315 hard_iface
->batman_adv_ptype
.dev
= hard_iface
->net_dev
;
316 dev_add_pack(&hard_iface
->batman_adv_ptype
);
318 atomic_set(&hard_iface
->frag_seqno
, 1);
319 batadv_info(hard_iface
->soft_iface
, "Adding interface: %s\n",
320 hard_iface
->net_dev
->name
);
322 if (atomic_read(&bat_priv
->fragmentation
) && hard_iface
->net_dev
->mtu
<
323 ETH_DATA_LEN
+ BAT_HEADER_LEN
)
324 batadv_info(hard_iface
->soft_iface
,
325 "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",
326 hard_iface
->net_dev
->name
, hard_iface
->net_dev
->mtu
,
327 ETH_DATA_LEN
+ BAT_HEADER_LEN
);
329 if (!atomic_read(&bat_priv
->fragmentation
) && hard_iface
->net_dev
->mtu
<
330 ETH_DATA_LEN
+ BAT_HEADER_LEN
)
331 batadv_info(hard_iface
->soft_iface
,
332 "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",
333 hard_iface
->net_dev
->name
, hard_iface
->net_dev
->mtu
,
334 ETH_DATA_LEN
+ BAT_HEADER_LEN
);
336 if (batadv_hardif_is_iface_up(hard_iface
))
337 batadv_hardif_activate_interface(hard_iface
);
339 batadv_err(hard_iface
->soft_iface
,
340 "Not using interface %s (retrying later): interface not active\n",
341 hard_iface
->net_dev
->name
);
343 /* begin scheduling originator messages on that interface */
344 batadv_schedule_bat_ogm(hard_iface
);
352 batadv_hardif_free_ref(hard_iface
);
356 void batadv_hardif_disable_interface(struct hard_iface
*hard_iface
)
358 struct bat_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
359 struct hard_iface
*primary_if
= NULL
;
361 if (hard_iface
->if_status
== IF_ACTIVE
)
362 batadv_hardif_deactivate_interface(hard_iface
);
364 if (hard_iface
->if_status
!= IF_INACTIVE
)
367 batadv_info(hard_iface
->soft_iface
, "Removing interface: %s\n",
368 hard_iface
->net_dev
->name
);
369 dev_remove_pack(&hard_iface
->batman_adv_ptype
);
371 bat_priv
->num_ifaces
--;
372 batadv_orig_hash_del_if(hard_iface
, bat_priv
->num_ifaces
);
374 primary_if
= batadv_primary_if_get_selected(bat_priv
);
375 if (hard_iface
== primary_if
) {
376 struct hard_iface
*new_if
;
378 new_if
= batadv_hardif_get_active(hard_iface
->soft_iface
);
379 batadv_primary_if_select(bat_priv
, new_if
);
382 batadv_hardif_free_ref(new_if
);
385 bat_priv
->bat_algo_ops
->bat_iface_disable(hard_iface
);
386 hard_iface
->if_status
= IF_NOT_IN_USE
;
388 /* delete all references to this hard_iface */
389 batadv_purge_orig_ref(bat_priv
);
390 batadv_purge_outstanding_packets(bat_priv
, hard_iface
);
391 dev_put(hard_iface
->soft_iface
);
393 /* nobody uses this interface anymore */
394 if (!bat_priv
->num_ifaces
)
395 batadv_softif_destroy(hard_iface
->soft_iface
);
397 hard_iface
->soft_iface
= NULL
;
398 batadv_hardif_free_ref(hard_iface
);
402 batadv_hardif_free_ref(primary_if
);
405 static struct hard_iface
*
406 batadv_hardif_add_interface(struct net_device
*net_dev
)
408 struct hard_iface
*hard_iface
;
413 ret
= batadv_is_valid_iface(net_dev
);
419 hard_iface
= kmalloc(sizeof(*hard_iface
), GFP_ATOMIC
);
423 ret
= batadv_sysfs_add_hardif(&hard_iface
->hardif_obj
, net_dev
);
427 hard_iface
->if_num
= -1;
428 hard_iface
->net_dev
= net_dev
;
429 hard_iface
->soft_iface
= NULL
;
430 hard_iface
->if_status
= IF_NOT_IN_USE
;
431 INIT_LIST_HEAD(&hard_iface
->list
);
432 /* extra reference for return */
433 atomic_set(&hard_iface
->refcount
, 2);
435 batadv_check_known_mac_addr(hard_iface
->net_dev
);
436 list_add_tail_rcu(&hard_iface
->list
, &batadv_hardif_list
);
438 /* This can't be called via a bat_priv callback because
439 * we have no bat_priv yet.
441 atomic_set(&hard_iface
->seqno
, 1);
442 hard_iface
->packet_buff
= NULL
;
454 static void batadv_hardif_remove_interface(struct hard_iface
*hard_iface
)
458 /* first deactivate interface */
459 if (hard_iface
->if_status
!= IF_NOT_IN_USE
)
460 batadv_hardif_disable_interface(hard_iface
);
462 if (hard_iface
->if_status
!= IF_NOT_IN_USE
)
465 hard_iface
->if_status
= IF_TO_BE_REMOVED
;
466 batadv_sysfs_del_hardif(&hard_iface
->hardif_obj
);
467 batadv_hardif_free_ref(hard_iface
);
470 void batadv_hardif_remove_interfaces(void)
472 struct hard_iface
*hard_iface
, *hard_iface_tmp
;
475 list_for_each_entry_safe(hard_iface
, hard_iface_tmp
,
476 &batadv_hardif_list
, list
) {
477 list_del_rcu(&hard_iface
->list
);
478 batadv_hardif_remove_interface(hard_iface
);
483 static int batadv_hard_if_event(struct notifier_block
*this,
484 unsigned long event
, void *ptr
)
486 struct net_device
*net_dev
= ptr
;
487 struct hard_iface
*hard_iface
= batadv_hardif_get_by_netdev(net_dev
);
488 struct hard_iface
*primary_if
= NULL
;
489 struct bat_priv
*bat_priv
;
491 if (!hard_iface
&& event
== NETDEV_REGISTER
)
492 hard_iface
= batadv_hardif_add_interface(net_dev
);
499 batadv_hardif_activate_interface(hard_iface
);
501 case NETDEV_GOING_DOWN
:
503 batadv_hardif_deactivate_interface(hard_iface
);
505 case NETDEV_UNREGISTER
:
506 list_del_rcu(&hard_iface
->list
);
508 batadv_hardif_remove_interface(hard_iface
);
510 case NETDEV_CHANGEMTU
:
511 if (hard_iface
->soft_iface
)
512 batadv_update_min_mtu(hard_iface
->soft_iface
);
514 case NETDEV_CHANGEADDR
:
515 if (hard_iface
->if_status
== IF_NOT_IN_USE
)
518 batadv_check_known_mac_addr(hard_iface
->net_dev
);
520 bat_priv
= netdev_priv(hard_iface
->soft_iface
);
521 bat_priv
->bat_algo_ops
->bat_iface_update_mac(hard_iface
);
523 primary_if
= batadv_primary_if_get_selected(bat_priv
);
527 if (hard_iface
== primary_if
)
528 batadv_primary_if_update_addr(bat_priv
, NULL
);
535 batadv_hardif_free_ref(hard_iface
);
538 batadv_hardif_free_ref(primary_if
);
542 /* This function returns true if the interface represented by ifindex is a
543 * 802.11 wireless device
545 bool batadv_is_wifi_iface(int ifindex
)
547 struct net_device
*net_device
= NULL
;
550 if (ifindex
== NULL_IFINDEX
)
553 net_device
= dev_get_by_index(&init_net
, ifindex
);
557 #ifdef CONFIG_WIRELESS_EXT
558 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
559 * check for wireless_handlers != NULL
561 if (net_device
->wireless_handlers
)
565 /* cfg80211 drivers have to set ieee80211_ptr */
566 if (net_device
->ieee80211_ptr
)
574 struct notifier_block batadv_hard_if_notifier
= {
575 .notifier_call
= batadv_hard_if_event
,