blob: 1643e7fca6c63d41eda507a5959cc2f1457819e7 [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann567db7b2012-01-01 00:41:38 +01002 * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner, Simon Wunderlich
5 *
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.
9 *
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.
14 *
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
18 * 02110-1301, USA
19 *
20 */
21
22#include "main.h"
23#include "hard-interface.h"
24#include "soft-interface.h"
25#include "send.h"
26#include "translation-table.h"
27#include "routing.h"
28#include "bat_sysfs.h"
29#include "originator.h"
30#include "hash.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010031#include "bridge_loop_avoidance.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000032
33#include <linux/if_arp.h>
34
Marek Lindnered75ccb2011-02-10 14:33:51 +000035void hardif_free_rcu(struct rcu_head *rcu)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000036{
Marek Lindnere6c10f42011-02-18 12:33:20 +000037 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000038
Marek Lindnere6c10f42011-02-18 12:33:20 +000039 hard_iface = container_of(rcu, struct hard_iface, rcu);
40 dev_put(hard_iface->net_dev);
41 kfree(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000042}
43
Sven Eckelmann747e4222011-05-14 23:14:50 +020044struct hard_iface *hardif_get_by_netdev(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000045{
Marek Lindnere6c10f42011-02-18 12:33:20 +000046 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000047
48 rcu_read_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +000049 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))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052 goto out;
53 }
54
Marek Lindnere6c10f42011-02-18 12:33:20 +000055 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000056
57out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000058 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +000059 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000060}
61
Sven Eckelmann747e4222011-05-14 23:14:50 +020062static int is_valid_iface(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000063{
64 if (net_dev->flags & IFF_LOOPBACK)
65 return 0;
66
67 if (net_dev->type != ARPHRD_ETHER)
68 return 0;
69
70 if (net_dev->addr_len != ETH_ALEN)
71 return 0;
72
73 /* no batman over batman */
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +000074 if (softif_is_valid(net_dev))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000075 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000076
77 /* Device is being bridged */
78 /* if (net_dev->priv_flags & IFF_BRIDGE_PORT)
79 return 0; */
80
81 return 1;
82}
83
Sven Eckelmann747e4222011-05-14 23:14:50 +020084static struct hard_iface *hardif_get_active(const struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000085{
Marek Lindnere6c10f42011-02-18 12:33:20 +000086 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000087
88 rcu_read_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +000089 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
90 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000091 continue;
92
Marek Lindnere6c10f42011-02-18 12:33:20 +000093 if (hard_iface->if_status == IF_ACTIVE &&
94 atomic_inc_not_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000095 goto out;
96 }
97
Marek Lindnere6c10f42011-02-18 12:33:20 +000098 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000099
100out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000101 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000102 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000103}
104
Simon Wunderlich23721382012-01-22 20:00:19 +0100105static void primary_if_update_addr(struct bat_priv *bat_priv,
106 struct hard_iface *oldif)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000107{
108 struct vis_packet *vis_packet;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200109 struct hard_iface *primary_if;
110
111 primary_if = primary_if_get_selected(bat_priv);
112 if (!primary_if)
113 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000114
115 vis_packet = (struct vis_packet *)
116 bat_priv->my_vis_info->skb_packet->data;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200117 memcpy(vis_packet->vis_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000118 memcpy(vis_packet->sender_orig,
Marek Lindner32ae9b22011-04-20 15:40:58 +0200119 primary_if->net_dev->dev_addr, ETH_ALEN);
120
Sven Eckelmann08adf152012-05-12 13:38:47 +0200121 batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200122out:
123 if (primary_if)
124 hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000125}
126
Marek Lindner32ae9b22011-04-20 15:40:58 +0200127static void primary_if_select(struct bat_priv *bat_priv,
128 struct hard_iface *new_hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000129{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200130 struct hard_iface *curr_hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000131
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200132 ASSERT_RTNL();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000133
Marek Lindner32ae9b22011-04-20 15:40:58 +0200134 if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount))
135 new_hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000136
Sven Eckelmann728cbc62011-05-15 00:50:21 +0200137 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200138 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000139
Marek Lindner32ae9b22011-04-20 15:40:58 +0200140 if (!new_hard_iface)
Simon Wunderlich23721382012-01-22 20:00:19 +0100141 goto out;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200142
Marek Lindnercd8b78e2012-02-07 17:20:49 +0800143 bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface);
Simon Wunderlich23721382012-01-22 20:00:19 +0100144 primary_if_update_addr(bat_priv, curr_hard_iface);
145
146out:
147 if (curr_hard_iface)
148 hardif_free_ref(curr_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000149}
150
Sven Eckelmann747e4222011-05-14 23:14:50 +0200151static bool hardif_is_iface_up(const struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000152{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000153 if (hard_iface->net_dev->flags & IFF_UP)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000154 return true;
155
156 return false;
157}
158
Sven Eckelmann747e4222011-05-14 23:14:50 +0200159static void check_known_mac_addr(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000160{
Sven Eckelmann747e4222011-05-14 23:14:50 +0200161 const struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000162
163 rcu_read_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000164 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))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000167 continue;
168
Marek Lindnere6c10f42011-02-18 12:33:20 +0000169 if (hard_iface->net_dev == net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000170 continue;
171
Marek Lindnere6c10f42011-02-18 12:33:20 +0000172 if (!compare_eth(hard_iface->net_dev->dev_addr,
173 net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000174 continue;
175
Sven Eckelmann67969582012-03-26 16:22:45 +0200176 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");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000179 }
180 rcu_read_unlock();
181}
182
183int hardif_min_mtu(struct net_device *soft_iface)
184{
Sven Eckelmann747e4222011-05-14 23:14:50 +0200185 const struct bat_priv *bat_priv = netdev_priv(soft_iface);
186 const struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000187 /* 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;
190
191 if (atomic_read(&bat_priv->fragmentation))
192 goto out;
193
194 rcu_read_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000195 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))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000198 continue;
199
Marek Lindnere6c10f42011-02-18 12:33:20 +0000200 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000201 continue;
202
Marek Lindnere6c10f42011-02-18 12:33:20 +0000203 min_mtu = min_t(int, hard_iface->net_dev->mtu - BAT_HEADER_LEN,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000204 min_mtu);
205 }
206 rcu_read_unlock();
207out:
208 return min_mtu;
209}
210
211/* adjusts the MTU if a new interface with a smaller MTU appeared. */
212void update_min_mtu(struct net_device *soft_iface)
213{
214 int min_mtu;
215
216 min_mtu = hardif_min_mtu(soft_iface);
217 if (soft_iface->mtu != min_mtu)
218 soft_iface->mtu = min_mtu;
219}
220
Marek Lindnere6c10f42011-02-18 12:33:20 +0000221static void hardif_activate_interface(struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000222{
223 struct bat_priv *bat_priv;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200224 struct hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000225
Marek Lindnere6c10f42011-02-18 12:33:20 +0000226 if (hard_iface->if_status != IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200227 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000228
Marek Lindnere6c10f42011-02-18 12:33:20 +0000229 bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000230
Marek Lindnerc3229392012-03-11 06:17:50 +0800231 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000232 hard_iface->if_status = IF_TO_BE_ACTIVATED;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000233
234 /**
235 * the first active interface becomes our primary interface or
Antonio Quartulli015758d2011-07-09 17:52:13 +0200236 * the next active interface after the old primary interface was removed
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000237 */
Marek Lindner32ae9b22011-04-20 15:40:58 +0200238 primary_if = primary_if_get_selected(bat_priv);
239 if (!primary_if)
240 primary_if_select(bat_priv, hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000241
Marek Lindnere6c10f42011-02-18 12:33:20 +0000242 bat_info(hard_iface->soft_iface, "Interface activated: %s\n",
243 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000244
Marek Lindnere6c10f42011-02-18 12:33:20 +0000245 update_min_mtu(hard_iface->soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200246
247out:
248 if (primary_if)
249 hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000250}
251
Marek Lindnere6c10f42011-02-18 12:33:20 +0000252static void hardif_deactivate_interface(struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000253{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000254 if ((hard_iface->if_status != IF_ACTIVE) &&
255 (hard_iface->if_status != IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000256 return;
257
Marek Lindnere6c10f42011-02-18 12:33:20 +0000258 hard_iface->if_status = IF_INACTIVE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259
Marek Lindnere6c10f42011-02-18 12:33:20 +0000260 bat_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
261 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000262
Marek Lindnere6c10f42011-02-18 12:33:20 +0000263 update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000264}
265
Sven Eckelmann747e4222011-05-14 23:14:50 +0200266int hardif_enable_interface(struct hard_iface *hard_iface,
267 const char *iface_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268{
269 struct bat_priv *bat_priv;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000270 struct net_device *soft_iface;
271 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000272
Marek Lindnere6c10f42011-02-18 12:33:20 +0000273 if (hard_iface->if_status != IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000274 goto out;
275
Marek Lindnere6c10f42011-02-18 12:33:20 +0000276 if (!atomic_inc_not_zero(&hard_iface->refcount))
Marek Lindnered75ccb2011-02-10 14:33:51 +0000277 goto out;
278
Marek Lindner6e242f92011-12-07 18:02:50 +0800279 /* hard-interface is part of a bridge */
280 if (hard_iface->net_dev->priv_flags & IFF_BRIDGE_PORT)
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100281 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 +0800282 hard_iface->net_dev->name);
283
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000284 soft_iface = dev_get_by_name(&init_net, iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000286 if (!soft_iface) {
287 soft_iface = softif_create(iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000288
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000289 if (!soft_iface) {
290 ret = -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000291 goto err;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000292 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000293
294 /* dev_get_by_name() increases the reference counter for us */
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000295 dev_hold(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000296 }
297
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000298 if (!softif_is_valid(soft_iface)) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100299 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000300 soft_iface->name);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000301 ret = -EINVAL;
Marek Lindner77af7572012-02-07 17:20:48 +0800302 goto err_dev;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000303 }
304
305 hard_iface->soft_iface = soft_iface;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000306 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200307
Marek Lindner77af7572012-02-07 17:20:48 +0800308 ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200309 if (ret < 0)
Marek Lindner77af7572012-02-07 17:20:48 +0800310 goto err_dev;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000311
Marek Lindnere6c10f42011-02-18 12:33:20 +0000312 hard_iface->if_num = bat_priv->num_ifaces;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000313 bat_priv->num_ifaces++;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000314 hard_iface->if_status = IF_INACTIVE;
315 orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000316
Marek Lindnere6c10f42011-02-18 12:33:20 +0000317 hard_iface->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
318 hard_iface->batman_adv_ptype.func = batman_skb_recv;
319 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
320 dev_add_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000321
Marek Lindnere6c10f42011-02-18 12:33:20 +0000322 atomic_set(&hard_iface->frag_seqno, 1);
323 bat_info(hard_iface->soft_iface, "Adding interface: %s\n",
324 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000325
Marek Lindnere6c10f42011-02-18 12:33:20 +0000326 if (atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000327 ETH_DATA_LEN + BAT_HEADER_LEN)
Marek Lindnere6c10f42011-02-18 12:33:20 +0000328 bat_info(hard_iface->soft_iface,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100329 "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",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100330 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
331 ETH_DATA_LEN + BAT_HEADER_LEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000332
Marek Lindnere6c10f42011-02-18 12:33:20 +0000333 if (!atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000334 ETH_DATA_LEN + BAT_HEADER_LEN)
Marek Lindnere6c10f42011-02-18 12:33:20 +0000335 bat_info(hard_iface->soft_iface,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100336 "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",
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100337 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
338 ETH_DATA_LEN + BAT_HEADER_LEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000339
Marek Lindnere6c10f42011-02-18 12:33:20 +0000340 if (hardif_is_iface_up(hard_iface))
341 hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000342 else
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100343 bat_err(hard_iface->soft_iface,
344 "Not using interface %s (retrying later): interface not active\n",
Marek Lindnere6c10f42011-02-18 12:33:20 +0000345 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000346
347 /* begin scheduling originator messages on that interface */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200348 schedule_bat_ogm(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000349
350out:
351 return 0;
352
Marek Lindner77af7572012-02-07 17:20:48 +0800353err_dev:
354 dev_put(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000355err:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000356 hardif_free_ref(hard_iface);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000357 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000358}
359
Marek Lindnere6c10f42011-02-18 12:33:20 +0000360void hardif_disable_interface(struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000361{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000362 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200363 struct hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000364
Marek Lindnere6c10f42011-02-18 12:33:20 +0000365 if (hard_iface->if_status == IF_ACTIVE)
366 hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367
Marek Lindnere6c10f42011-02-18 12:33:20 +0000368 if (hard_iface->if_status != IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200369 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370
Marek Lindnere6c10f42011-02-18 12:33:20 +0000371 bat_info(hard_iface->soft_iface, "Removing interface: %s\n",
372 hard_iface->net_dev->name);
373 dev_remove_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000374
375 bat_priv->num_ifaces--;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000376 orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377
Marek Lindner32ae9b22011-04-20 15:40:58 +0200378 primary_if = primary_if_get_selected(bat_priv);
379 if (hard_iface == primary_if) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000380 struct hard_iface *new_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000381
Marek Lindnere6c10f42011-02-18 12:33:20 +0000382 new_if = hardif_get_active(hard_iface->soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200383 primary_if_select(bat_priv, new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000384
385 if (new_if)
Marek Lindnered75ccb2011-02-10 14:33:51 +0000386 hardif_free_ref(new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000387 }
388
Marek Lindner00a50072012-02-07 17:20:47 +0800389 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000390 hard_iface->if_status = IF_NOT_IN_USE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000391
Marek Lindnere6c10f42011-02-18 12:33:20 +0000392 /* delete all references to this hard_iface */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000393 purge_orig_ref(bat_priv);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000394 purge_outstanding_packets(bat_priv, hard_iface);
395 dev_put(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000396
397 /* nobody uses this interface anymore */
398 if (!bat_priv->num_ifaces)
Marek Lindnere6c10f42011-02-18 12:33:20 +0000399 softif_destroy(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000400
Marek Lindnere6c10f42011-02-18 12:33:20 +0000401 hard_iface->soft_iface = NULL;
402 hardif_free_ref(hard_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200403
404out:
405 if (primary_if)
406 hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000407}
408
Marek Lindnere6c10f42011-02-18 12:33:20 +0000409static struct hard_iface *hardif_add_interface(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000410{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000411 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000412 int ret;
413
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200414 ASSERT_RTNL();
415
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000416 ret = is_valid_iface(net_dev);
417 if (ret != 1)
418 goto out;
419
420 dev_hold(net_dev);
421
Sven Eckelmann704509b2011-05-14 23:14:54 +0200422 hard_iface = kmalloc(sizeof(*hard_iface), GFP_ATOMIC);
Joe Perches320f4222011-08-29 14:17:24 -0700423 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000424 goto release_dev;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000425
Sven Eckelmann5853e222012-05-12 02:09:24 +0200426 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000427 if (ret)
428 goto free_if;
429
Marek Lindnere6c10f42011-02-18 12:33:20 +0000430 hard_iface->if_num = -1;
431 hard_iface->net_dev = net_dev;
432 hard_iface->soft_iface = NULL;
433 hard_iface->if_status = IF_NOT_IN_USE;
434 INIT_LIST_HEAD(&hard_iface->list);
Marek Lindnered75ccb2011-02-10 14:33:51 +0000435 /* extra reference for return */
Marek Lindnere6c10f42011-02-18 12:33:20 +0000436 atomic_set(&hard_iface->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437
Marek Lindnere6c10f42011-02-18 12:33:20 +0000438 check_known_mac_addr(hard_iface->net_dev);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000439 list_add_tail_rcu(&hard_iface->list, &hardif_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000440
Marek Lindner81406252012-02-07 17:19:58 +0800441 /**
442 * This can't be called via a bat_priv callback because
443 * we have no bat_priv yet.
444 */
445 atomic_set(&hard_iface->seqno, 1);
446 hard_iface->packet_buff = NULL;
447
Marek Lindnere6c10f42011-02-18 12:33:20 +0000448 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000449
450free_if:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000451 kfree(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000452release_dev:
453 dev_put(net_dev);
454out:
455 return NULL;
456}
457
Marek Lindnere6c10f42011-02-18 12:33:20 +0000458static void hardif_remove_interface(struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000459{
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200460 ASSERT_RTNL();
461
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000462 /* first deactivate interface */
Marek Lindnere6c10f42011-02-18 12:33:20 +0000463 if (hard_iface->if_status != IF_NOT_IN_USE)
464 hardif_disable_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000465
Marek Lindnere6c10f42011-02-18 12:33:20 +0000466 if (hard_iface->if_status != IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467 return;
468
Marek Lindnere6c10f42011-02-18 12:33:20 +0000469 hard_iface->if_status = IF_TO_BE_REMOVED;
Sven Eckelmann5853e222012-05-12 02:09:24 +0200470 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000471 hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000472}
473
474void hardif_remove_interfaces(void)
475{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000476 struct hard_iface *hard_iface, *hard_iface_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000477
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200478 rtnl_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000479 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
480 &hardif_list, list) {
481 list_del_rcu(&hard_iface->list);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000482 hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000483 }
484 rtnl_unlock();
485}
486
487static int hard_if_event(struct notifier_block *this,
488 unsigned long event, void *ptr)
489{
Sven Eckelmann5f718c22011-05-14 23:14:52 +0200490 struct net_device *net_dev = ptr;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000491 struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200492 struct hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000493 struct bat_priv *bat_priv;
494
Marek Lindnere6c10f42011-02-18 12:33:20 +0000495 if (!hard_iface && event == NETDEV_REGISTER)
496 hard_iface = hardif_add_interface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000497
Marek Lindnere6c10f42011-02-18 12:33:20 +0000498 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000499 goto out;
500
501 switch (event) {
502 case NETDEV_UP:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000503 hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000504 break;
505 case NETDEV_GOING_DOWN:
506 case NETDEV_DOWN:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000507 hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000508 break;
509 case NETDEV_UNREGISTER:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000510 list_del_rcu(&hard_iface->list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000511
Marek Lindnere6c10f42011-02-18 12:33:20 +0000512 hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000513 break;
514 case NETDEV_CHANGEMTU:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000515 if (hard_iface->soft_iface)
516 update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000517 break;
518 case NETDEV_CHANGEADDR:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000519 if (hard_iface->if_status == IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000520 goto hardif_put;
521
Marek Lindnere6c10f42011-02-18 12:33:20 +0000522 check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000523
Marek Lindnere6c10f42011-02-18 12:33:20 +0000524 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerc3229392012-03-11 06:17:50 +0800525 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Marek Lindner01c42242011-11-28 21:31:55 +0800526
Marek Lindner32ae9b22011-04-20 15:40:58 +0200527 primary_if = primary_if_get_selected(bat_priv);
528 if (!primary_if)
529 goto hardif_put;
530
531 if (hard_iface == primary_if)
Simon Wunderlich23721382012-01-22 20:00:19 +0100532 primary_if_update_addr(bat_priv, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000533 break;
534 default:
535 break;
Joe Perchesf81c6222011-06-03 11:51:19 +0000536 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000537
538hardif_put:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000539 hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000540out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200541 if (primary_if)
542 hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000543 return NOTIFY_DONE;
544}
545
Antonio Quartullibc279082011-07-07 15:35:35 +0200546/* This function returns true if the interface represented by ifindex is a
547 * 802.11 wireless device */
548bool is_wifi_iface(int ifindex)
549{
550 struct net_device *net_device = NULL;
551 bool ret = false;
552
553 if (ifindex == NULL_IFINDEX)
554 goto out;
555
556 net_device = dev_get_by_index(&init_net, ifindex);
557 if (!net_device)
558 goto out;
559
560#ifdef CONFIG_WIRELESS_EXT
561 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
562 * check for wireless_handlers != NULL */
563 if (net_device->wireless_handlers)
564 ret = true;
565 else
566#endif
567 /* cfg80211 drivers have to set ieee80211_ptr */
568 if (net_device->ieee80211_ptr)
569 ret = true;
570out:
571 if (net_device)
572 dev_put(net_device);
573 return ret;
574}
575
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000576struct notifier_block hard_if_notifier = {
577 .notifier_call = hard_if_event,
578};