blob: d112fd6750b0564e1b08232482eb8c1261b749ad [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"
Sven Eckelmannb706b132012-06-10 23:58:51 +020026#include "sysfs.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000027#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{
Sven Eckelmann56303d32012-06-05 22:31:31 +020035 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000036
Sven Eckelmann56303d32012-06-05 22:31:31 +020037 hard_iface = container_of(rcu, struct batadv_hard_iface, rcu);
Marek Lindnere6c10f42011-02-18 12:33:20 +000038 dev_put(hard_iface->net_dev);
39 kfree(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000040}
41
Sven Eckelmann56303d32012-06-05 22:31:31 +020042struct batadv_hard_iface *
43batadv_hardif_get_by_netdev(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044{
Sven Eckelmann56303d32012-06-05 22:31:31 +020045 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046
47 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +020048 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +000049 if (hard_iface->net_dev == net_dev &&
50 atomic_inc_not_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000051 goto out;
52 }
53
Marek Lindnere6c10f42011-02-18 12:33:20 +000054 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000055
56out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000057 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +000058 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000059}
60
Sven Eckelmann18a1cb62012-05-12 18:33:57 +020061static int batadv_is_valid_iface(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000062{
63 if (net_dev->flags & IFF_LOOPBACK)
64 return 0;
65
66 if (net_dev->type != ARPHRD_ETHER)
67 return 0;
68
69 if (net_dev->addr_len != ETH_ALEN)
70 return 0;
71
72 /* no batman over batman */
Sven Eckelmann04b482a2012-05-12 02:09:38 +020073 if (batadv_softif_is_valid(net_dev))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000074 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000075
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000076 return 1;
77}
78
Sven Eckelmann56303d32012-06-05 22:31:31 +020079static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +020080batadv_hardif_get_active(const struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000081{
Sven Eckelmann56303d32012-06-05 22:31:31 +020082 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000083
84 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +020085 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +000086 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000087 continue;
88
Sven Eckelmanne9a4f292012-06-03 22:19:19 +020089 if (hard_iface->if_status == BATADV_IF_ACTIVE &&
Marek Lindnere6c10f42011-02-18 12:33:20 +000090 atomic_inc_not_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000091 goto out;
92 }
93
Marek Lindnere6c10f42011-02-18 12:33:20 +000094 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000095
96out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000097 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +000098 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000099}
100
Sven Eckelmann56303d32012-06-05 22:31:31 +0200101static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
102 struct batadv_hard_iface *oldif)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000103{
Sven Eckelmann96412692012-06-05 22:31:30 +0200104 struct batadv_vis_packet *vis_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200105 struct batadv_hard_iface *primary_if;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200106 struct sk_buff *skb;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200107
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200108 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200109 if (!primary_if)
110 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000111
Sven Eckelmann807736f2012-07-15 22:26:51 +0200112 skb = bat_priv->vis.my_info->skb_packet;
113 vis_packet = (struct batadv_vis_packet *)skb->data;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200114 memcpy(vis_packet->vis_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000115 memcpy(vis_packet->sender_orig,
Marek Lindner32ae9b22011-04-20 15:40:58 +0200116 primary_if->net_dev->dev_addr, ETH_ALEN);
117
Sven Eckelmann08adf152012-05-12 13:38:47 +0200118 batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200119out:
120 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200121 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000122}
123
Sven Eckelmann56303d32012-06-05 22:31:31 +0200124static void batadv_primary_if_select(struct batadv_priv *bat_priv,
125 struct batadv_hard_iface *new_hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000126{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200127 struct batadv_hard_iface *curr_hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000128
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200129 ASSERT_RTNL();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000130
Marek Lindner32ae9b22011-04-20 15:40:58 +0200131 if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount))
132 new_hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000133
Sven Eckelmann728cbc62011-05-15 00:50:21 +0200134 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200135 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000136
Marek Lindner32ae9b22011-04-20 15:40:58 +0200137 if (!new_hard_iface)
Simon Wunderlich23721382012-01-22 20:00:19 +0100138 goto out;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200139
Marek Lindnercd8b78e2012-02-07 17:20:49 +0800140 bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200141 batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
Simon Wunderlich23721382012-01-22 20:00:19 +0100142
143out:
144 if (curr_hard_iface)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200145 batadv_hardif_free_ref(curr_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000146}
147
Sven Eckelmann56303d32012-06-05 22:31:31 +0200148static bool
149batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000150{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000151 if (hard_iface->net_dev->flags & IFF_UP)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000152 return true;
153
154 return false;
155}
156
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200157static void batadv_check_known_mac_addr(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000158{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200159 const struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000160
161 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200162 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200163 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
164 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000165 continue;
166
Marek Lindnere6c10f42011-02-18 12:33:20 +0000167 if (hard_iface->net_dev == net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000168 continue;
169
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200170 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
171 net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000172 continue;
173
Sven Eckelmann67969582012-03-26 16:22:45 +0200174 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
175 net_dev->dev_addr, hard_iface->net_dev->name);
176 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000177 }
178 rcu_read_unlock();
179}
180
Sven Eckelmann95638772012-05-12 02:09:31 +0200181int batadv_hardif_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000182{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200183 const struct batadv_priv *bat_priv = netdev_priv(soft_iface);
184 const struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000185 /* allow big frames if all devices are capable to do so
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200186 * (have MTU > 1500 + BAT_HEADER_LEN)
187 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188 int min_mtu = ETH_DATA_LEN;
189
190 if (atomic_read(&bat_priv->fragmentation))
191 goto out;
192
193 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200194 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200195 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
196 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000197 continue;
198
Marek Lindnere6c10f42011-02-18 12:33:20 +0000199 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000200 continue;
201
Sven Eckelmannc11fdfa2012-06-03 22:19:14 +0200202 min_mtu = min_t(int,
203 hard_iface->net_dev->mtu - BATADV_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. */
Sven Eckelmann95638772012-05-12 02:09:31 +0200212void batadv_update_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000213{
214 int min_mtu;
215
Sven Eckelmann95638772012-05-12 02:09:31 +0200216 min_mtu = batadv_hardif_min_mtu(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000217 if (soft_iface->mtu != min_mtu)
218 soft_iface->mtu = min_mtu;
219}
220
Sven Eckelmann56303d32012-06-05 22:31:31 +0200221static void
222batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000223{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200224 struct batadv_priv *bat_priv;
225 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000226
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200227 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200228 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000229
Marek Lindnere6c10f42011-02-18 12:33:20 +0000230 bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000231
Marek Lindnerc3229392012-03-11 06:17:50 +0800232 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200233 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000234
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200235 /* 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 */
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200238 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200239 if (!primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200240 batadv_primary_if_select(bat_priv, hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000241
Sven Eckelmann3e348192012-05-16 20:23:22 +0200242 batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
243 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000244
Sven Eckelmann95638772012-05-12 02:09:31 +0200245 batadv_update_min_mtu(hard_iface->soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200246
247out:
248 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200249 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000250}
251
Sven Eckelmann56303d32012-06-05 22:31:31 +0200252static void
253batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000254{
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200255 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
256 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000257 return;
258
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200259 hard_iface->if_status = BATADV_IF_INACTIVE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000260
Sven Eckelmann3e348192012-05-16 20:23:22 +0200261 batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
262 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000263
Sven Eckelmann95638772012-05-12 02:09:31 +0200264 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000265}
266
Sven Eckelmann56303d32012-06-05 22:31:31 +0200267int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
Sven Eckelmann95638772012-05-12 02:09:31 +0200268 const char *iface_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000269{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200270 struct batadv_priv *bat_priv;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000271 struct net_device *soft_iface;
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200272 __be16 ethertype = __constant_htons(BATADV_ETH_P_BATMAN);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000273 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000274
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200275 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000276 goto out;
277
Marek Lindnere6c10f42011-02-18 12:33:20 +0000278 if (!atomic_inc_not_zero(&hard_iface->refcount))
Marek Lindnered75ccb2011-02-10 14:33:51 +0000279 goto out;
280
Marek Lindner6e242f92011-12-07 18:02:50 +0800281 /* hard-interface is part of a bridge */
282 if (hard_iface->net_dev->priv_flags & IFF_BRIDGE_PORT)
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100283 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 +0800284 hard_iface->net_dev->name);
285
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000286 soft_iface = dev_get_by_name(&init_net, iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000287
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000288 if (!soft_iface) {
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200289 soft_iface = batadv_softif_create(iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000290
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000291 if (!soft_iface) {
292 ret = -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000293 goto err;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000294 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000295
296 /* dev_get_by_name() increases the reference counter for us */
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000297 dev_hold(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000298 }
299
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200300 if (!batadv_softif_is_valid(soft_iface)) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100301 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000302 soft_iface->name);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000303 ret = -EINVAL;
Marek Lindner77af7572012-02-07 17:20:48 +0800304 goto err_dev;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000305 }
306
307 hard_iface->soft_iface = soft_iface;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000308 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200309
Marek Lindner77af7572012-02-07 17:20:48 +0800310 ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200311 if (ret < 0)
Marek Lindner77af7572012-02-07 17:20:48 +0800312 goto err_dev;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000313
Marek Lindnere6c10f42011-02-18 12:33:20 +0000314 hard_iface->if_num = bat_priv->num_ifaces;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000315 bat_priv->num_ifaces++;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200316 hard_iface->if_status = BATADV_IF_INACTIVE;
Simon Wunderlich62446302012-07-01 22:51:55 +0200317 ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
318 if (ret < 0) {
319 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
320 bat_priv->num_ifaces--;
321 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
322 goto err_dev;
323 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000324
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200325 hard_iface->batman_adv_ptype.type = ethertype;
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200326 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000327 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
328 dev_add_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000329
Marek Lindnere6c10f42011-02-18 12:33:20 +0000330 atomic_set(&hard_iface->frag_seqno, 1);
Sven Eckelmann3e348192012-05-16 20:23:22 +0200331 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
332 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000333
Sven Eckelmann0aca2362012-06-19 20:26:30 +0200334 if (atomic_read(&bat_priv->fragmentation) &&
335 hard_iface->net_dev->mtu < ETH_DATA_LEN + BATADV_HEADER_LEN)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200336 batadv_info(hard_iface->soft_iface,
337 "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",
338 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Sven Eckelmannc11fdfa2012-06-03 22:19:14 +0200339 ETH_DATA_LEN + BATADV_HEADER_LEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000340
Sven Eckelmann0aca2362012-06-19 20:26:30 +0200341 if (!atomic_read(&bat_priv->fragmentation) &&
342 hard_iface->net_dev->mtu < ETH_DATA_LEN + BATADV_HEADER_LEN)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200343 batadv_info(hard_iface->soft_iface,
344 "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",
345 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Sven Eckelmannc11fdfa2012-06-03 22:19:14 +0200346 ETH_DATA_LEN + BATADV_HEADER_LEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000347
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200348 if (batadv_hardif_is_iface_up(hard_iface))
349 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000350 else
Sven Eckelmann3e348192012-05-16 20:23:22 +0200351 batadv_err(hard_iface->soft_iface,
352 "Not using interface %s (retrying later): interface not active\n",
353 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000354
355 /* begin scheduling originator messages on that interface */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200356 batadv_schedule_bat_ogm(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000357
358out:
359 return 0;
360
Marek Lindner77af7572012-02-07 17:20:48 +0800361err_dev:
362 dev_put(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000363err:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200364 batadv_hardif_free_ref(hard_iface);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000365 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000366}
367
Sven Eckelmann56303d32012-06-05 22:31:31 +0200368void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000369{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200370 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
371 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000372
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200373 if (hard_iface->if_status == BATADV_IF_ACTIVE)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200374 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200376 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200377 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000378
Sven Eckelmann3e348192012-05-16 20:23:22 +0200379 batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
380 hard_iface->net_dev->name);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000381 dev_remove_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000382
383 bat_priv->num_ifaces--;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200384 batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000385
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200386 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200387 if (hard_iface == primary_if) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200388 struct batadv_hard_iface *new_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200390 new_if = batadv_hardif_get_active(hard_iface->soft_iface);
391 batadv_primary_if_select(bat_priv, new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000392
393 if (new_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200394 batadv_hardif_free_ref(new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000395 }
396
Marek Lindner00a50072012-02-07 17:20:47 +0800397 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200398 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000399
Marek Lindnere6c10f42011-02-18 12:33:20 +0000400 /* delete all references to this hard_iface */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200401 batadv_purge_orig_ref(bat_priv);
Sven Eckelmann9455e342012-05-12 02:09:37 +0200402 batadv_purge_outstanding_packets(bat_priv, hard_iface);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000403 dev_put(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000404
405 /* nobody uses this interface anymore */
406 if (!bat_priv->num_ifaces)
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200407 batadv_softif_destroy(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000408
Marek Lindnere6c10f42011-02-18 12:33:20 +0000409 hard_iface->soft_iface = NULL;
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200410 batadv_hardif_free_ref(hard_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200411
412out:
413 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200414 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415}
416
Sven Eckelmann56303d32012-06-05 22:31:31 +0200417static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200418batadv_hardif_add_interface(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000419{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200420 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000421 int ret;
422
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200423 ASSERT_RTNL();
424
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200425 ret = batadv_is_valid_iface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000426 if (ret != 1)
427 goto out;
428
429 dev_hold(net_dev);
430
Sven Eckelmann704509b2011-05-14 23:14:54 +0200431 hard_iface = kmalloc(sizeof(*hard_iface), GFP_ATOMIC);
Joe Perches320f4222011-08-29 14:17:24 -0700432 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000433 goto release_dev;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000434
Sven Eckelmann5853e222012-05-12 02:09:24 +0200435 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000436 if (ret)
437 goto free_if;
438
Marek Lindnere6c10f42011-02-18 12:33:20 +0000439 hard_iface->if_num = -1;
440 hard_iface->net_dev = net_dev;
441 hard_iface->soft_iface = NULL;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200442 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000443 INIT_LIST_HEAD(&hard_iface->list);
Marek Lindnered75ccb2011-02-10 14:33:51 +0000444 /* extra reference for return */
Marek Lindnere6c10f42011-02-18 12:33:20 +0000445 atomic_set(&hard_iface->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000446
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200447 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200448 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000449
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200450 /* This can't be called via a bat_priv callback because
Marek Lindner81406252012-02-07 17:19:58 +0800451 * we have no bat_priv yet.
452 */
453 atomic_set(&hard_iface->seqno, 1);
454 hard_iface->packet_buff = NULL;
455
Marek Lindnere6c10f42011-02-18 12:33:20 +0000456 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000457
458free_if:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000459 kfree(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000460release_dev:
461 dev_put(net_dev);
462out:
463 return NULL;
464}
465
Sven Eckelmann56303d32012-06-05 22:31:31 +0200466static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467{
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200468 ASSERT_RTNL();
469
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000470 /* first deactivate interface */
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200471 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmann95638772012-05-12 02:09:31 +0200472 batadv_hardif_disable_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000473
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200474 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000475 return;
476
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200477 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
Sven Eckelmann5853e222012-05-12 02:09:24 +0200478 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200479 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000480}
481
Sven Eckelmann95638772012-05-12 02:09:31 +0200482void batadv_hardif_remove_interfaces(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000483{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200484 struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000485
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200486 rtnl_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000487 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200488 &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000489 list_del_rcu(&hard_iface->list);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200490 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000491 }
492 rtnl_unlock();
493}
494
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200495static int batadv_hard_if_event(struct notifier_block *this,
496 unsigned long event, void *ptr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000497{
Sven Eckelmann5f718c22011-05-14 23:14:52 +0200498 struct net_device *net_dev = ptr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200499 struct batadv_hard_iface *hard_iface;
500 struct batadv_hard_iface *primary_if = NULL;
501 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000502
Sven Eckelmann56303d32012-06-05 22:31:31 +0200503 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000504 if (!hard_iface && event == NETDEV_REGISTER)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200505 hard_iface = batadv_hardif_add_interface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000506
Marek Lindnere6c10f42011-02-18 12:33:20 +0000507 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000508 goto out;
509
510 switch (event) {
511 case NETDEV_UP:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200512 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000513 break;
514 case NETDEV_GOING_DOWN:
515 case NETDEV_DOWN:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200516 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000517 break;
518 case NETDEV_UNREGISTER:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000519 list_del_rcu(&hard_iface->list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000520
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200521 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000522 break;
523 case NETDEV_CHANGEMTU:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000524 if (hard_iface->soft_iface)
Sven Eckelmann95638772012-05-12 02:09:31 +0200525 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000526 break;
527 case NETDEV_CHANGEADDR:
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200528 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000529 goto hardif_put;
530
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200531 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000532
Marek Lindnere6c10f42011-02-18 12:33:20 +0000533 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerc3229392012-03-11 06:17:50 +0800534 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Marek Lindner01c42242011-11-28 21:31:55 +0800535
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200536 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200537 if (!primary_if)
538 goto hardif_put;
539
540 if (hard_iface == primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200541 batadv_primary_if_update_addr(bat_priv, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000542 break;
543 default:
544 break;
Joe Perchesf81c6222011-06-03 11:51:19 +0000545 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000546
547hardif_put:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200548 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000549out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200550 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200551 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000552 return NOTIFY_DONE;
553}
554
Antonio Quartullibc279082011-07-07 15:35:35 +0200555/* This function returns true if the interface represented by ifindex is a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200556 * 802.11 wireless device
557 */
Sven Eckelmann95638772012-05-12 02:09:31 +0200558bool batadv_is_wifi_iface(int ifindex)
Antonio Quartullibc279082011-07-07 15:35:35 +0200559{
560 struct net_device *net_device = NULL;
561 bool ret = false;
562
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200563 if (ifindex == BATADV_NULL_IFINDEX)
Antonio Quartullibc279082011-07-07 15:35:35 +0200564 goto out;
565
566 net_device = dev_get_by_index(&init_net, ifindex);
567 if (!net_device)
568 goto out;
569
570#ifdef CONFIG_WIRELESS_EXT
571 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200572 * check for wireless_handlers != NULL
573 */
Antonio Quartullibc279082011-07-07 15:35:35 +0200574 if (net_device->wireless_handlers)
575 ret = true;
576 else
577#endif
578 /* cfg80211 drivers have to set ieee80211_ptr */
579 if (net_device->ieee80211_ptr)
580 ret = true;
581out:
582 if (net_device)
583 dev_put(net_device);
584 return ret;
585}
586
Sven Eckelmann95638772012-05-12 02:09:31 +0200587struct notifier_block batadv_hard_if_notifier = {
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200588 .notifier_call = batadv_hard_if_event,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000589};