blob: 340108411c4dea1544b5d2f64266ea4eb02c8752 [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
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, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
19
20#include "main.h"
21#include "hard-interface.h"
22#include "soft-interface.h"
23#include "send.h"
24#include "translation-table.h"
25#include "routing.h"
26#include "bat_sysfs.h"
27#include "originator.h"
28#include "hash.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010029#include "bridge_loop_avoidance.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000030
31#include <linux/if_arp.h>
32
Sven Eckelmann95638772012-05-12 02:09:31 +020033void batadv_hardif_free_rcu(struct rcu_head *rcu)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000034{
Marek Lindnere6c10f42011-02-18 12:33:20 +000035 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000036
Marek Lindnere6c10f42011-02-18 12:33:20 +000037 hard_iface = container_of(rcu, struct hard_iface, rcu);
38 dev_put(hard_iface->net_dev);
39 kfree(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000040}
41
Sven Eckelmann95638772012-05-12 02:09:31 +020042struct hard_iface *batadv_hardif_get_by_netdev(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000043{
Marek Lindnere6c10f42011-02-18 12:33:20 +000044 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000045
46 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +020047 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +000048 if (hard_iface->net_dev == net_dev &&
49 atomic_inc_not_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000050 goto out;
51 }
52
Marek Lindnere6c10f42011-02-18 12:33:20 +000053 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000054
55out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000056 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +000057 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000058}
59
Sven Eckelmann18a1cb62012-05-12 18:33:57 +020060static int batadv_is_valid_iface(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000061{
62 if (net_dev->flags & IFF_LOOPBACK)
63 return 0;
64
65 if (net_dev->type != ARPHRD_ETHER)
66 return 0;
67
68 if (net_dev->addr_len != ETH_ALEN)
69 return 0;
70
71 /* no batman over batman */
Sven Eckelmann04b482a2012-05-12 02:09:38 +020072 if (batadv_softif_is_valid(net_dev))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000073 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000074
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000075 return 1;
76}
77
Sven Eckelmann18a1cb62012-05-12 18:33:57 +020078static struct hard_iface *
79batadv_hardif_get_active(const struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000080{
Marek Lindnere6c10f42011-02-18 12:33:20 +000081 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000082
83 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +020084 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +000085 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000086 continue;
87
Marek Lindnere6c10f42011-02-18 12:33:20 +000088 if (hard_iface->if_status == IF_ACTIVE &&
89 atomic_inc_not_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000090 goto out;
91 }
92
Marek Lindnere6c10f42011-02-18 12:33:20 +000093 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000094
95out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000096 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +000097 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000098}
99
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200100static void batadv_primary_if_update_addr(struct bat_priv *bat_priv,
101 struct hard_iface *oldif)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000102{
103 struct vis_packet *vis_packet;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200104 struct hard_iface *primary_if;
105
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200106 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200107 if (!primary_if)
108 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000109
110 vis_packet = (struct vis_packet *)
111 bat_priv->my_vis_info->skb_packet->data;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200112 memcpy(vis_packet->vis_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000113 memcpy(vis_packet->sender_orig,
Marek Lindner32ae9b22011-04-20 15:40:58 +0200114 primary_if->net_dev->dev_addr, ETH_ALEN);
115
Sven Eckelmann08adf152012-05-12 13:38:47 +0200116 batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200117out:
118 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200119 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000120}
121
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200122static void batadv_primary_if_select(struct bat_priv *bat_priv,
123 struct hard_iface *new_hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000124{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200125 struct hard_iface *curr_hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000126
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200127 ASSERT_RTNL();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000128
Marek Lindner32ae9b22011-04-20 15:40:58 +0200129 if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount))
130 new_hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000131
Sven Eckelmann728cbc62011-05-15 00:50:21 +0200132 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200133 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000134
Marek Lindner32ae9b22011-04-20 15:40:58 +0200135 if (!new_hard_iface)
Simon Wunderlich23721382012-01-22 20:00:19 +0100136 goto out;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200137
Marek Lindnercd8b78e2012-02-07 17:20:49 +0800138 bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200139 batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
Simon Wunderlich23721382012-01-22 20:00:19 +0100140
141out:
142 if (curr_hard_iface)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200143 batadv_hardif_free_ref(curr_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000144}
145
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200146static bool batadv_hardif_is_iface_up(const struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000147{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000148 if (hard_iface->net_dev->flags & IFF_UP)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000149 return true;
150
151 return false;
152}
153
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200154static void batadv_check_known_mac_addr(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000155{
Sven Eckelmann747e4222011-05-14 23:14:50 +0200156 const struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000157
158 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200159 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000160 if ((hard_iface->if_status != IF_ACTIVE) &&
161 (hard_iface->if_status != IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000162 continue;
163
Marek Lindnere6c10f42011-02-18 12:33:20 +0000164 if (hard_iface->net_dev == net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000165 continue;
166
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200167 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
168 net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000169 continue;
170
Sven Eckelmann67969582012-03-26 16:22:45 +0200171 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");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000174 }
175 rcu_read_unlock();
176}
177
Sven Eckelmann95638772012-05-12 02:09:31 +0200178int batadv_hardif_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000179{
Sven Eckelmann747e4222011-05-14 23:14:50 +0200180 const struct bat_priv *bat_priv = netdev_priv(soft_iface);
181 const struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000182 /* allow big frames if all devices are capable to do so
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200183 * (have MTU > 1500 + BAT_HEADER_LEN)
184 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000185 int min_mtu = ETH_DATA_LEN;
186
187 if (atomic_read(&bat_priv->fragmentation))
188 goto out;
189
190 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200191 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000192 if ((hard_iface->if_status != IF_ACTIVE) &&
193 (hard_iface->if_status != IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000194 continue;
195
Marek Lindnere6c10f42011-02-18 12:33:20 +0000196 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000197 continue;
198
Marek Lindnere6c10f42011-02-18 12:33:20 +0000199 min_mtu = min_t(int, hard_iface->net_dev->mtu - BAT_HEADER_LEN,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000200 min_mtu);
201 }
202 rcu_read_unlock();
203out:
204 return min_mtu;
205}
206
207/* adjusts the MTU if a new interface with a smaller MTU appeared. */
Sven Eckelmann95638772012-05-12 02:09:31 +0200208void batadv_update_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000209{
210 int min_mtu;
211
Sven Eckelmann95638772012-05-12 02:09:31 +0200212 min_mtu = batadv_hardif_min_mtu(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000213 if (soft_iface->mtu != min_mtu)
214 soft_iface->mtu = min_mtu;
215}
216
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200217static void batadv_hardif_activate_interface(struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000218{
219 struct bat_priv *bat_priv;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200220 struct hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000221
Marek Lindnere6c10f42011-02-18 12:33:20 +0000222 if (hard_iface->if_status != IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200223 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000224
Marek Lindnere6c10f42011-02-18 12:33:20 +0000225 bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000226
Marek Lindnerc3229392012-03-11 06:17:50 +0800227 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000228 hard_iface->if_status = IF_TO_BE_ACTIVATED;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000229
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200230 /* the first active interface becomes our primary interface or
Antonio Quartulli015758d2011-07-09 17:52:13 +0200231 * the next active interface after the old primary interface was removed
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000232 */
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200233 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200234 if (!primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200235 batadv_primary_if_select(bat_priv, hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000236
Sven Eckelmann3e348192012-05-16 20:23:22 +0200237 batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
238 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000239
Sven Eckelmann95638772012-05-12 02:09:31 +0200240 batadv_update_min_mtu(hard_iface->soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200241
242out:
243 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200244 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000245}
246
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200247static void batadv_hardif_deactivate_interface(struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000248{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000249 if ((hard_iface->if_status != IF_ACTIVE) &&
250 (hard_iface->if_status != IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251 return;
252
Marek Lindnere6c10f42011-02-18 12:33:20 +0000253 hard_iface->if_status = IF_INACTIVE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000254
Sven Eckelmann3e348192012-05-16 20:23:22 +0200255 batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
256 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000257
Sven Eckelmann95638772012-05-12 02:09:31 +0200258 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259}
260
Sven Eckelmann95638772012-05-12 02:09:31 +0200261int batadv_hardif_enable_interface(struct hard_iface *hard_iface,
262 const char *iface_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000263{
264 struct bat_priv *bat_priv;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000265 struct net_device *soft_iface;
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200266 __be16 ethertype = __constant_htons(BATADV_ETH_P_BATMAN);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000267 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268
Marek Lindnere6c10f42011-02-18 12:33:20 +0000269 if (hard_iface->if_status != IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000270 goto out;
271
Marek Lindnere6c10f42011-02-18 12:33:20 +0000272 if (!atomic_inc_not_zero(&hard_iface->refcount))
Marek Lindnered75ccb2011-02-10 14:33:51 +0000273 goto out;
274
Marek Lindner6e242f92011-12-07 18:02:50 +0800275 /* hard-interface is part of a bridge */
276 if (hard_iface->net_dev->priv_flags & IFF_BRIDGE_PORT)
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100277 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",
Marek Lindner6e242f92011-12-07 18:02:50 +0800278 hard_iface->net_dev->name);
279
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000280 soft_iface = dev_get_by_name(&init_net, iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000281
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000282 if (!soft_iface) {
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200283 soft_iface = batadv_softif_create(iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000284
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000285 if (!soft_iface) {
286 ret = -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000287 goto err;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000288 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000289
290 /* dev_get_by_name() increases the reference counter for us */
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000291 dev_hold(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292 }
293
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200294 if (!batadv_softif_is_valid(soft_iface)) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100295 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000296 soft_iface->name);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000297 ret = -EINVAL;
Marek Lindner77af7572012-02-07 17:20:48 +0800298 goto err_dev;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000299 }
300
301 hard_iface->soft_iface = soft_iface;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000302 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200303
Marek Lindner77af7572012-02-07 17:20:48 +0800304 ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200305 if (ret < 0)
Marek Lindner77af7572012-02-07 17:20:48 +0800306 goto err_dev;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000307
Marek Lindnere6c10f42011-02-18 12:33:20 +0000308 hard_iface->if_num = bat_priv->num_ifaces;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000309 bat_priv->num_ifaces++;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000310 hard_iface->if_status = IF_INACTIVE;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200311 batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000312
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200313 hard_iface->batman_adv_ptype.type = ethertype;
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200314 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000315 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
316 dev_add_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000317
Marek Lindnere6c10f42011-02-18 12:33:20 +0000318 atomic_set(&hard_iface->frag_seqno, 1);
Sven Eckelmann3e348192012-05-16 20:23:22 +0200319 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
320 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000321
Marek Lindnere6c10f42011-02-18 12:33:20 +0000322 if (atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000323 ETH_DATA_LEN + BAT_HEADER_LEN)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200324 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);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000328
Marek Lindnere6c10f42011-02-18 12:33:20 +0000329 if (!atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000330 ETH_DATA_LEN + BAT_HEADER_LEN)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200331 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);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000335
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200336 if (batadv_hardif_is_iface_up(hard_iface))
337 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000338 else
Sven Eckelmann3e348192012-05-16 20:23:22 +0200339 batadv_err(hard_iface->soft_iface,
340 "Not using interface %s (retrying later): interface not active\n",
341 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000342
343 /* begin scheduling originator messages on that interface */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200344 batadv_schedule_bat_ogm(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000345
346out:
347 return 0;
348
Marek Lindner77af7572012-02-07 17:20:48 +0800349err_dev:
350 dev_put(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000351err:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200352 batadv_hardif_free_ref(hard_iface);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000353 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000354}
355
Sven Eckelmann95638772012-05-12 02:09:31 +0200356void batadv_hardif_disable_interface(struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000357{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000358 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200359 struct hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000360
Marek Lindnere6c10f42011-02-18 12:33:20 +0000361 if (hard_iface->if_status == IF_ACTIVE)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200362 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000363
Marek Lindnere6c10f42011-02-18 12:33:20 +0000364 if (hard_iface->if_status != IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200365 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000366
Sven Eckelmann3e348192012-05-16 20:23:22 +0200367 batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
368 hard_iface->net_dev->name);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000369 dev_remove_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370
371 bat_priv->num_ifaces--;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200372 batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000373
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200374 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200375 if (hard_iface == primary_if) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000376 struct hard_iface *new_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200378 new_if = batadv_hardif_get_active(hard_iface->soft_iface);
379 batadv_primary_if_select(bat_priv, new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000380
381 if (new_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200382 batadv_hardif_free_ref(new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383 }
384
Marek Lindner00a50072012-02-07 17:20:47 +0800385 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000386 hard_iface->if_status = IF_NOT_IN_USE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000387
Marek Lindnere6c10f42011-02-18 12:33:20 +0000388 /* delete all references to this hard_iface */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200389 batadv_purge_orig_ref(bat_priv);
Sven Eckelmann9455e342012-05-12 02:09:37 +0200390 batadv_purge_outstanding_packets(bat_priv, hard_iface);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000391 dev_put(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000392
393 /* nobody uses this interface anymore */
394 if (!bat_priv->num_ifaces)
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200395 batadv_softif_destroy(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000396
Marek Lindnere6c10f42011-02-18 12:33:20 +0000397 hard_iface->soft_iface = NULL;
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200398 batadv_hardif_free_ref(hard_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200399
400out:
401 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200402 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000403}
404
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200405static struct hard_iface *
406batadv_hardif_add_interface(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000407{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000408 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000409 int ret;
410
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200411 ASSERT_RTNL();
412
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200413 ret = batadv_is_valid_iface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000414 if (ret != 1)
415 goto out;
416
417 dev_hold(net_dev);
418
Sven Eckelmann704509b2011-05-14 23:14:54 +0200419 hard_iface = kmalloc(sizeof(*hard_iface), GFP_ATOMIC);
Joe Perches320f4222011-08-29 14:17:24 -0700420 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000421 goto release_dev;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000422
Sven Eckelmann5853e222012-05-12 02:09:24 +0200423 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000424 if (ret)
425 goto free_if;
426
Marek Lindnere6c10f42011-02-18 12:33:20 +0000427 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);
Marek Lindnered75ccb2011-02-10 14:33:51 +0000432 /* extra reference for return */
Marek Lindnere6c10f42011-02-18 12:33:20 +0000433 atomic_set(&hard_iface->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000434
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200435 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200436 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200438 /* This can't be called via a bat_priv callback because
Marek Lindner81406252012-02-07 17:19:58 +0800439 * we have no bat_priv yet.
440 */
441 atomic_set(&hard_iface->seqno, 1);
442 hard_iface->packet_buff = NULL;
443
Marek Lindnere6c10f42011-02-18 12:33:20 +0000444 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000445
446free_if:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000447 kfree(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448release_dev:
449 dev_put(net_dev);
450out:
451 return NULL;
452}
453
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200454static void batadv_hardif_remove_interface(struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000455{
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200456 ASSERT_RTNL();
457
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000458 /* first deactivate interface */
Marek Lindnere6c10f42011-02-18 12:33:20 +0000459 if (hard_iface->if_status != IF_NOT_IN_USE)
Sven Eckelmann95638772012-05-12 02:09:31 +0200460 batadv_hardif_disable_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000461
Marek Lindnere6c10f42011-02-18 12:33:20 +0000462 if (hard_iface->if_status != IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000463 return;
464
Marek Lindnere6c10f42011-02-18 12:33:20 +0000465 hard_iface->if_status = IF_TO_BE_REMOVED;
Sven Eckelmann5853e222012-05-12 02:09:24 +0200466 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200467 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000468}
469
Sven Eckelmann95638772012-05-12 02:09:31 +0200470void batadv_hardif_remove_interfaces(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000472 struct hard_iface *hard_iface, *hard_iface_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000473
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200474 rtnl_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000475 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200476 &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000477 list_del_rcu(&hard_iface->list);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200478 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000479 }
480 rtnl_unlock();
481}
482
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200483static int batadv_hard_if_event(struct notifier_block *this,
484 unsigned long event, void *ptr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000485{
Sven Eckelmann5f718c22011-05-14 23:14:52 +0200486 struct net_device *net_dev = ptr;
Sven Eckelmann95638772012-05-12 02:09:31 +0200487 struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200488 struct hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000489 struct bat_priv *bat_priv;
490
Marek Lindnere6c10f42011-02-18 12:33:20 +0000491 if (!hard_iface && event == NETDEV_REGISTER)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200492 hard_iface = batadv_hardif_add_interface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000493
Marek Lindnere6c10f42011-02-18 12:33:20 +0000494 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000495 goto out;
496
497 switch (event) {
498 case NETDEV_UP:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200499 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000500 break;
501 case NETDEV_GOING_DOWN:
502 case NETDEV_DOWN:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200503 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000504 break;
505 case NETDEV_UNREGISTER:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000506 list_del_rcu(&hard_iface->list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000507
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200508 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000509 break;
510 case NETDEV_CHANGEMTU:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000511 if (hard_iface->soft_iface)
Sven Eckelmann95638772012-05-12 02:09:31 +0200512 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000513 break;
514 case NETDEV_CHANGEADDR:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000515 if (hard_iface->if_status == IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000516 goto hardif_put;
517
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200518 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000519
Marek Lindnere6c10f42011-02-18 12:33:20 +0000520 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerc3229392012-03-11 06:17:50 +0800521 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Marek Lindner01c42242011-11-28 21:31:55 +0800522
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200523 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200524 if (!primary_if)
525 goto hardif_put;
526
527 if (hard_iface == primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200528 batadv_primary_if_update_addr(bat_priv, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000529 break;
530 default:
531 break;
Joe Perchesf81c6222011-06-03 11:51:19 +0000532 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000533
534hardif_put:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200535 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000536out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200537 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200538 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000539 return NOTIFY_DONE;
540}
541
Antonio Quartullibc279082011-07-07 15:35:35 +0200542/* This function returns true if the interface represented by ifindex is a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200543 * 802.11 wireless device
544 */
Sven Eckelmann95638772012-05-12 02:09:31 +0200545bool batadv_is_wifi_iface(int ifindex)
Antonio Quartullibc279082011-07-07 15:35:35 +0200546{
547 struct net_device *net_device = NULL;
548 bool ret = false;
549
550 if (ifindex == NULL_IFINDEX)
551 goto out;
552
553 net_device = dev_get_by_index(&init_net, ifindex);
554 if (!net_device)
555 goto out;
556
557#ifdef CONFIG_WIRELESS_EXT
558 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200559 * check for wireless_handlers != NULL
560 */
Antonio Quartullibc279082011-07-07 15:35:35 +0200561 if (net_device->wireless_handlers)
562 ret = true;
563 else
564#endif
565 /* cfg80211 drivers have to set ieee80211_ptr */
566 if (net_device->ieee80211_ptr)
567 ret = true;
568out:
569 if (net_device)
570 dev_put(net_device);
571 return ret;
572}
573
Sven Eckelmann95638772012-05-12 02:09:31 +0200574struct notifier_block batadv_hard_if_notifier = {
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200575 .notifier_call = batadv_hard_if_event,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000576};