blob: 6b7a5d3eeb7778db8deefe7067a5ac52a014a1e4 [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"
Antonio Quartulli785ea112011-11-23 11:35:44 +010021#include "distributed-arp-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000022#include "hard-interface.h"
23#include "soft-interface.h"
24#include "send.h"
25#include "translation-table.h"
26#include "routing.h"
Sven Eckelmannb706b132012-06-10 23:58:51 +020027#include "sysfs.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000028#include "originator.h"
29#include "hash.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010030#include "bridge_loop_avoidance.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000031
32#include <linux/if_arp.h>
33
Sven Eckelmann95638772012-05-12 02:09:31 +020034void batadv_hardif_free_rcu(struct rcu_head *rcu)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000035{
Sven Eckelmann56303d32012-06-05 22:31:31 +020036 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000037
Sven Eckelmann56303d32012-06-05 22:31:31 +020038 hard_iface = container_of(rcu, struct batadv_hard_iface, rcu);
Marek Lindnere6c10f42011-02-18 12:33:20 +000039 dev_put(hard_iface->net_dev);
40 kfree(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000041}
42
Sven Eckelmann56303d32012-06-05 22:31:31 +020043struct batadv_hard_iface *
44batadv_hardif_get_by_netdev(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000045{
Sven Eckelmann56303d32012-06-05 22:31:31 +020046 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000047
48 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +020049 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +000050 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 Eckelmann18a1cb62012-05-12 18:33:57 +020062static int batadv_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 Eckelmann04b482a2012-05-12 02:09:38 +020074 if (batadv_softif_is_valid(net_dev))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000075 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000076
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000077 return 1;
78}
79
Sven Eckelmann56303d32012-06-05 22:31:31 +020080static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +020081batadv_hardif_get_active(const struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000082{
Sven Eckelmann56303d32012-06-05 22:31:31 +020083 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000084
85 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +020086 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +000087 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000088 continue;
89
Sven Eckelmanne9a4f292012-06-03 22:19:19 +020090 if (hard_iface->if_status == BATADV_IF_ACTIVE &&
Marek Lindnere6c10f42011-02-18 12:33:20 +000091 atomic_inc_not_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000092 goto out;
93 }
94
Marek Lindnere6c10f42011-02-18 12:33:20 +000095 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000096
97out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000098 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +000099 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000100}
101
Sven Eckelmann56303d32012-06-05 22:31:31 +0200102static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
103 struct batadv_hard_iface *oldif)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000104{
Sven Eckelmann96412692012-06-05 22:31:30 +0200105 struct batadv_vis_packet *vis_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200106 struct batadv_hard_iface *primary_if;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200107 struct sk_buff *skb;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200108
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200109 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200110 if (!primary_if)
111 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000112
Antonio Quartulli785ea112011-11-23 11:35:44 +0100113 batadv_dat_init_own_addr(bat_priv, primary_if);
114
Sven Eckelmann807736f2012-07-15 22:26:51 +0200115 skb = bat_priv->vis.my_info->skb_packet;
116 vis_packet = (struct batadv_vis_packet *)skb->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)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200124 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000125}
126
Sven Eckelmann56303d32012-06-05 22:31:31 +0200127static void batadv_primary_if_select(struct batadv_priv *bat_priv,
128 struct batadv_hard_iface *new_hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000129{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200130 struct batadv_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);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200144 batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
Simon Wunderlich23721382012-01-22 20:00:19 +0100145
146out:
147 if (curr_hard_iface)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200148 batadv_hardif_free_ref(curr_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000149}
150
Sven Eckelmann56303d32012-06-05 22:31:31 +0200151static bool
152batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000153{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000154 if (hard_iface->net_dev->flags & IFF_UP)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000155 return true;
156
157 return false;
158}
159
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200160static void batadv_check_known_mac_addr(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000161{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200162 const struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000163
164 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200165 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200166 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
167 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000168 continue;
169
Marek Lindnere6c10f42011-02-18 12:33:20 +0000170 if (hard_iface->net_dev == net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000171 continue;
172
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200173 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
174 net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000175 continue;
176
Sven Eckelmann67969582012-03-26 16:22:45 +0200177 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
178 net_dev->dev_addr, hard_iface->net_dev->name);
179 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000180 }
181 rcu_read_unlock();
182}
183
Sven Eckelmann95638772012-05-12 02:09:31 +0200184int batadv_hardif_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000185{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200186 const struct batadv_priv *bat_priv = netdev_priv(soft_iface);
187 const struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188 /* allow big frames if all devices are capable to do so
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200189 * (have MTU > 1500 + BAT_HEADER_LEN)
190 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000191 int min_mtu = ETH_DATA_LEN;
192
193 if (atomic_read(&bat_priv->fragmentation))
194 goto out;
195
196 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200197 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200198 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
199 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000200 continue;
201
Marek Lindnere6c10f42011-02-18 12:33:20 +0000202 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000203 continue;
204
Sven Eckelmannc11fdfa2012-06-03 22:19:14 +0200205 min_mtu = min_t(int,
206 hard_iface->net_dev->mtu - BATADV_HEADER_LEN,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000207 min_mtu);
208 }
209 rcu_read_unlock();
210out:
211 return min_mtu;
212}
213
214/* adjusts the MTU if a new interface with a smaller MTU appeared. */
Sven Eckelmann95638772012-05-12 02:09:31 +0200215void batadv_update_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000216{
217 int min_mtu;
218
Sven Eckelmann95638772012-05-12 02:09:31 +0200219 min_mtu = batadv_hardif_min_mtu(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000220 if (soft_iface->mtu != min_mtu)
221 soft_iface->mtu = min_mtu;
222}
223
Sven Eckelmann56303d32012-06-05 22:31:31 +0200224static void
225batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000226{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200227 struct batadv_priv *bat_priv;
228 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000229
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200230 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200231 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000232
Marek Lindnere6c10f42011-02-18 12:33:20 +0000233 bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000234
Marek Lindnerc3229392012-03-11 06:17:50 +0800235 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200236 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000237
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200238 /* the first active interface becomes our primary interface or
Antonio Quartulli015758d2011-07-09 17:52:13 +0200239 * the next active interface after the old primary interface was removed
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000240 */
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200241 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200242 if (!primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200243 batadv_primary_if_select(bat_priv, hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000244
Sven Eckelmann3e348192012-05-16 20:23:22 +0200245 batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
246 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000247
Sven Eckelmann95638772012-05-12 02:09:31 +0200248 batadv_update_min_mtu(hard_iface->soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200249
250out:
251 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200252 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000253}
254
Sven Eckelmann56303d32012-06-05 22:31:31 +0200255static void
256batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000257{
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200258 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
259 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000260 return;
261
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200262 hard_iface->if_status = BATADV_IF_INACTIVE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000263
Sven Eckelmann3e348192012-05-16 20:23:22 +0200264 batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
265 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000266
Sven Eckelmann95638772012-05-12 02:09:31 +0200267 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268}
269
Sven Eckelmann56303d32012-06-05 22:31:31 +0200270int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
Sven Eckelmann95638772012-05-12 02:09:31 +0200271 const char *iface_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000272{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200273 struct batadv_priv *bat_priv;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000274 struct net_device *soft_iface;
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200275 __be16 ethertype = __constant_htons(BATADV_ETH_P_BATMAN);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000276 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000277
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200278 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000279 goto out;
280
Marek Lindnere6c10f42011-02-18 12:33:20 +0000281 if (!atomic_inc_not_zero(&hard_iface->refcount))
Marek Lindnered75ccb2011-02-10 14:33:51 +0000282 goto out;
283
Marek Lindner6e242f92011-12-07 18:02:50 +0800284 /* hard-interface is part of a bridge */
285 if (hard_iface->net_dev->priv_flags & IFF_BRIDGE_PORT)
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100286 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 +0800287 hard_iface->net_dev->name);
288
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000289 soft_iface = dev_get_by_name(&init_net, iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000290
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000291 if (!soft_iface) {
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200292 soft_iface = batadv_softif_create(iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000293
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000294 if (!soft_iface) {
295 ret = -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000296 goto err;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000297 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000298
299 /* dev_get_by_name() increases the reference counter for us */
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000300 dev_hold(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000301 }
302
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200303 if (!batadv_softif_is_valid(soft_iface)) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100304 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000305 soft_iface->name);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000306 ret = -EINVAL;
Marek Lindner77af7572012-02-07 17:20:48 +0800307 goto err_dev;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000308 }
309
310 hard_iface->soft_iface = soft_iface;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000311 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200312
Marek Lindner77af7572012-02-07 17:20:48 +0800313 ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200314 if (ret < 0)
Marek Lindner77af7572012-02-07 17:20:48 +0800315 goto err_dev;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000316
Marek Lindnere6c10f42011-02-18 12:33:20 +0000317 hard_iface->if_num = bat_priv->num_ifaces;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000318 bat_priv->num_ifaces++;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200319 hard_iface->if_status = BATADV_IF_INACTIVE;
Simon Wunderlich62446302012-07-01 22:51:55 +0200320 ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
321 if (ret < 0) {
322 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
323 bat_priv->num_ifaces--;
324 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
325 goto err_dev;
326 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000327
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200328 hard_iface->batman_adv_ptype.type = ethertype;
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200329 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000330 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
331 dev_add_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000332
Marek Lindnere6c10f42011-02-18 12:33:20 +0000333 atomic_set(&hard_iface->frag_seqno, 1);
Sven Eckelmann3e348192012-05-16 20:23:22 +0200334 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
335 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000336
Sven Eckelmann0aca2362012-06-19 20:26:30 +0200337 if (atomic_read(&bat_priv->fragmentation) &&
338 hard_iface->net_dev->mtu < ETH_DATA_LEN + BATADV_HEADER_LEN)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200339 batadv_info(hard_iface->soft_iface,
340 "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",
341 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Sven Eckelmannc11fdfa2012-06-03 22:19:14 +0200342 ETH_DATA_LEN + BATADV_HEADER_LEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000343
Sven Eckelmann0aca2362012-06-19 20:26:30 +0200344 if (!atomic_read(&bat_priv->fragmentation) &&
345 hard_iface->net_dev->mtu < ETH_DATA_LEN + BATADV_HEADER_LEN)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200346 batadv_info(hard_iface->soft_iface,
347 "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",
348 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Sven Eckelmannc11fdfa2012-06-03 22:19:14 +0200349 ETH_DATA_LEN + BATADV_HEADER_LEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000350
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200351 if (batadv_hardif_is_iface_up(hard_iface))
352 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000353 else
Sven Eckelmann3e348192012-05-16 20:23:22 +0200354 batadv_err(hard_iface->soft_iface,
355 "Not using interface %s (retrying later): interface not active\n",
356 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000357
358 /* begin scheduling originator messages on that interface */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200359 batadv_schedule_bat_ogm(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000360
361out:
362 return 0;
363
Marek Lindner77af7572012-02-07 17:20:48 +0800364err_dev:
365 dev_put(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000366err:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200367 batadv_hardif_free_ref(hard_iface);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000368 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000369}
370
Sven Eckelmann56303d32012-06-05 22:31:31 +0200371void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000372{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200373 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
374 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200376 if (hard_iface->if_status == BATADV_IF_ACTIVE)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200377 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000378
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200379 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200380 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000381
Sven Eckelmann3e348192012-05-16 20:23:22 +0200382 batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
383 hard_iface->net_dev->name);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000384 dev_remove_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000385
386 bat_priv->num_ifaces--;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200387 batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000388
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200389 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200390 if (hard_iface == primary_if) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200391 struct batadv_hard_iface *new_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000392
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200393 new_if = batadv_hardif_get_active(hard_iface->soft_iface);
394 batadv_primary_if_select(bat_priv, new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000395
396 if (new_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200397 batadv_hardif_free_ref(new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000398 }
399
Marek Lindner00a50072012-02-07 17:20:47 +0800400 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200401 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000402
Marek Lindnere6c10f42011-02-18 12:33:20 +0000403 /* delete all references to this hard_iface */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200404 batadv_purge_orig_ref(bat_priv);
Sven Eckelmann9455e342012-05-12 02:09:37 +0200405 batadv_purge_outstanding_packets(bat_priv, hard_iface);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000406 dev_put(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000407
408 /* nobody uses this interface anymore */
409 if (!bat_priv->num_ifaces)
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200410 batadv_softif_destroy(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000411
Marek Lindnere6c10f42011-02-18 12:33:20 +0000412 hard_iface->soft_iface = NULL;
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200413 batadv_hardif_free_ref(hard_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200414
415out:
416 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200417 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000418}
419
Sven Eckelmann56303d32012-06-05 22:31:31 +0200420static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200421batadv_hardif_add_interface(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000422{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200423 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000424 int ret;
425
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200426 ASSERT_RTNL();
427
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200428 ret = batadv_is_valid_iface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000429 if (ret != 1)
430 goto out;
431
432 dev_hold(net_dev);
433
Sven Eckelmann704509b2011-05-14 23:14:54 +0200434 hard_iface = kmalloc(sizeof(*hard_iface), GFP_ATOMIC);
Joe Perches320f4222011-08-29 14:17:24 -0700435 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000436 goto release_dev;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437
Sven Eckelmann5853e222012-05-12 02:09:24 +0200438 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000439 if (ret)
440 goto free_if;
441
Marek Lindnere6c10f42011-02-18 12:33:20 +0000442 hard_iface->if_num = -1;
443 hard_iface->net_dev = net_dev;
444 hard_iface->soft_iface = NULL;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200445 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000446 INIT_LIST_HEAD(&hard_iface->list);
Marek Lindnered75ccb2011-02-10 14:33:51 +0000447 /* extra reference for return */
Marek Lindnere6c10f42011-02-18 12:33:20 +0000448 atomic_set(&hard_iface->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000449
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200450 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200451 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000452
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200453 /* This can't be called via a bat_priv callback because
Marek Lindner81406252012-02-07 17:19:58 +0800454 * we have no bat_priv yet.
455 */
Marek Lindner14511512012-08-02 17:20:26 +0200456 atomic_set(&hard_iface->bat_iv.ogm_seqno, 1);
457 hard_iface->bat_iv.ogm_buff = NULL;
Marek Lindner81406252012-02-07 17:19:58 +0800458
Marek Lindnere6c10f42011-02-18 12:33:20 +0000459 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000460
461free_if:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000462 kfree(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000463release_dev:
464 dev_put(net_dev);
465out:
466 return NULL;
467}
468
Sven Eckelmann56303d32012-06-05 22:31:31 +0200469static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000470{
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200471 ASSERT_RTNL();
472
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000473 /* first deactivate interface */
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200474 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmann95638772012-05-12 02:09:31 +0200475 batadv_hardif_disable_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000476
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200477 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000478 return;
479
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200480 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
Sven Eckelmann5853e222012-05-12 02:09:24 +0200481 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200482 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000483}
484
Sven Eckelmann95638772012-05-12 02:09:31 +0200485void batadv_hardif_remove_interfaces(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000486{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200487 struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000488
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200489 rtnl_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000490 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200491 &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000492 list_del_rcu(&hard_iface->list);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200493 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000494 }
495 rtnl_unlock();
496}
497
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200498static int batadv_hard_if_event(struct notifier_block *this,
499 unsigned long event, void *ptr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000500{
Sven Eckelmann5f718c22011-05-14 23:14:52 +0200501 struct net_device *net_dev = ptr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200502 struct batadv_hard_iface *hard_iface;
503 struct batadv_hard_iface *primary_if = NULL;
504 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000505
Sven Eckelmann56303d32012-06-05 22:31:31 +0200506 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000507 if (!hard_iface && event == NETDEV_REGISTER)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200508 hard_iface = batadv_hardif_add_interface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000509
Marek Lindnere6c10f42011-02-18 12:33:20 +0000510 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000511 goto out;
512
513 switch (event) {
514 case NETDEV_UP:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200515 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000516 break;
517 case NETDEV_GOING_DOWN:
518 case NETDEV_DOWN:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200519 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000520 break;
521 case NETDEV_UNREGISTER:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000522 list_del_rcu(&hard_iface->list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000523
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200524 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000525 break;
526 case NETDEV_CHANGEMTU:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000527 if (hard_iface->soft_iface)
Sven Eckelmann95638772012-05-12 02:09:31 +0200528 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000529 break;
530 case NETDEV_CHANGEADDR:
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200531 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000532 goto hardif_put;
533
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200534 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000535
Marek Lindnere6c10f42011-02-18 12:33:20 +0000536 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerc3229392012-03-11 06:17:50 +0800537 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Marek Lindner01c42242011-11-28 21:31:55 +0800538
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200539 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200540 if (!primary_if)
541 goto hardif_put;
542
543 if (hard_iface == primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200544 batadv_primary_if_update_addr(bat_priv, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000545 break;
546 default:
547 break;
Joe Perchesf81c6222011-06-03 11:51:19 +0000548 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000549
550hardif_put:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200551 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000552out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200553 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200554 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000555 return NOTIFY_DONE;
556}
557
Antonio Quartullibc279082011-07-07 15:35:35 +0200558/* This function returns true if the interface represented by ifindex is a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200559 * 802.11 wireless device
560 */
Sven Eckelmann95638772012-05-12 02:09:31 +0200561bool batadv_is_wifi_iface(int ifindex)
Antonio Quartullibc279082011-07-07 15:35:35 +0200562{
563 struct net_device *net_device = NULL;
564 bool ret = false;
565
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200566 if (ifindex == BATADV_NULL_IFINDEX)
Antonio Quartullibc279082011-07-07 15:35:35 +0200567 goto out;
568
569 net_device = dev_get_by_index(&init_net, ifindex);
570 if (!net_device)
571 goto out;
572
573#ifdef CONFIG_WIRELESS_EXT
574 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200575 * check for wireless_handlers != NULL
576 */
Antonio Quartullibc279082011-07-07 15:35:35 +0200577 if (net_device->wireless_handlers)
578 ret = true;
579 else
580#endif
581 /* cfg80211 drivers have to set ieee80211_ptr */
582 if (net_device->ieee80211_ptr)
583 ret = true;
584out:
585 if (net_device)
586 dev_put(net_device);
587 return ret;
588}
589
Sven Eckelmann95638772012-05-12 02:09:31 +0200590struct notifier_block batadv_hard_if_notifier = {
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200591 .notifier_call = batadv_hard_if_event,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000592};