blob: 365ed74f3946185abe8d550508270ba6e51e537e [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
Antonio Quartullib7eddd02012-09-09 10:46:46 +020062/**
63 * batadv_is_on_batman_iface - check if a device is a batman iface descendant
64 * @net_dev: the device to check
65 *
66 * If the user creates any virtual device on top of a batman-adv interface, it
67 * is important to prevent this new interface to be used to create a new mesh
68 * network (this behaviour would lead to a batman-over-batman configuration).
69 * This function recursively checks all the fathers of the device passed as
70 * argument looking for a batman-adv soft interface.
71 *
72 * Returns true if the device is descendant of a batman-adv mesh interface (or
73 * if it is a batman-adv interface itself), false otherwise
74 */
75static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
76{
77 struct net_device *parent_dev;
78 bool ret;
79
80 /* check if this is a batman-adv mesh interface */
81 if (batadv_softif_is_valid(net_dev))
82 return true;
83
84 /* no more parents..stop recursion */
85 if (net_dev->iflink == net_dev->ifindex)
86 return false;
87
88 /* recurse over the parent device */
89 parent_dev = dev_get_by_index(&init_net, net_dev->iflink);
90 /* if we got a NULL parent_dev there is something broken.. */
91 if (WARN(!parent_dev, "Cannot find parent device"))
92 return false;
93
94 ret = batadv_is_on_batman_iface(parent_dev);
95
96 if (parent_dev)
97 dev_put(parent_dev);
98 return ret;
99}
100
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200101static int batadv_is_valid_iface(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000102{
103 if (net_dev->flags & IFF_LOOPBACK)
104 return 0;
105
106 if (net_dev->type != ARPHRD_ETHER)
107 return 0;
108
109 if (net_dev->addr_len != ETH_ALEN)
110 return 0;
111
112 /* no batman over batman */
Antonio Quartullib7eddd02012-09-09 10:46:46 +0200113 if (batadv_is_on_batman_iface(net_dev))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000114 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000115
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000116 return 1;
117}
118
Sven Eckelmann56303d32012-06-05 22:31:31 +0200119static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200120batadv_hardif_get_active(const struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000121{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200122 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000123
124 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200125 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000126 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000127 continue;
128
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200129 if (hard_iface->if_status == BATADV_IF_ACTIVE &&
Marek Lindnere6c10f42011-02-18 12:33:20 +0000130 atomic_inc_not_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000131 goto out;
132 }
133
Marek Lindnere6c10f42011-02-18 12:33:20 +0000134 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000135
136out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000137 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000138 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000139}
140
Sven Eckelmann56303d32012-06-05 22:31:31 +0200141static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
142 struct batadv_hard_iface *oldif)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000143{
Sven Eckelmann96412692012-06-05 22:31:30 +0200144 struct batadv_vis_packet *vis_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200145 struct batadv_hard_iface *primary_if;
Sven Eckelmann807736f2012-07-15 22:26:51 +0200146 struct sk_buff *skb;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200147
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200148 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200149 if (!primary_if)
150 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000151
Antonio Quartulli785ea112011-11-23 11:35:44 +0100152 batadv_dat_init_own_addr(bat_priv, primary_if);
153
Sven Eckelmann807736f2012-07-15 22:26:51 +0200154 skb = bat_priv->vis.my_info->skb_packet;
155 vis_packet = (struct batadv_vis_packet *)skb->data;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200156 memcpy(vis_packet->vis_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000157 memcpy(vis_packet->sender_orig,
Marek Lindner32ae9b22011-04-20 15:40:58 +0200158 primary_if->net_dev->dev_addr, ETH_ALEN);
159
Sven Eckelmann08adf152012-05-12 13:38:47 +0200160 batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200161out:
162 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200163 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000164}
165
Sven Eckelmann56303d32012-06-05 22:31:31 +0200166static void batadv_primary_if_select(struct batadv_priv *bat_priv,
167 struct batadv_hard_iface *new_hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000168{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200169 struct batadv_hard_iface *curr_hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000170
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200171 ASSERT_RTNL();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000172
Marek Lindner32ae9b22011-04-20 15:40:58 +0200173 if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount))
174 new_hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000175
Sven Eckelmann728cbc62011-05-15 00:50:21 +0200176 curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200177 rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000178
Marek Lindner32ae9b22011-04-20 15:40:58 +0200179 if (!new_hard_iface)
Simon Wunderlich23721382012-01-22 20:00:19 +0100180 goto out;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200181
Marek Lindnercd8b78e2012-02-07 17:20:49 +0800182 bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200183 batadv_primary_if_update_addr(bat_priv, curr_hard_iface);
Simon Wunderlich23721382012-01-22 20:00:19 +0100184
185out:
186 if (curr_hard_iface)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200187 batadv_hardif_free_ref(curr_hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188}
189
Sven Eckelmann56303d32012-06-05 22:31:31 +0200190static bool
191batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000192{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000193 if (hard_iface->net_dev->flags & IFF_UP)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000194 return true;
195
196 return false;
197}
198
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200199static void batadv_check_known_mac_addr(const struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000200{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200201 const struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000202
203 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200204 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200205 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
206 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000207 continue;
208
Marek Lindnere6c10f42011-02-18 12:33:20 +0000209 if (hard_iface->net_dev == net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000210 continue;
211
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200212 if (!batadv_compare_eth(hard_iface->net_dev->dev_addr,
213 net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000214 continue;
215
Sven Eckelmann67969582012-03-26 16:22:45 +0200216 pr_warn("The newly added mac address (%pM) already exists on: %s\n",
217 net_dev->dev_addr, hard_iface->net_dev->name);
218 pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000219 }
220 rcu_read_unlock();
221}
222
Sven Eckelmann95638772012-05-12 02:09:31 +0200223int batadv_hardif_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000224{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200225 const struct batadv_priv *bat_priv = netdev_priv(soft_iface);
226 const struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000227 /* allow big frames if all devices are capable to do so
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200228 * (have MTU > 1500 + BAT_HEADER_LEN)
229 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000230 int min_mtu = ETH_DATA_LEN;
231
232 if (atomic_read(&bat_priv->fragmentation))
233 goto out;
234
235 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200236 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200237 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
238 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000239 continue;
240
Marek Lindnere6c10f42011-02-18 12:33:20 +0000241 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000242 continue;
243
Sven Eckelmannc11fdfa2012-06-03 22:19:14 +0200244 min_mtu = min_t(int,
245 hard_iface->net_dev->mtu - BATADV_HEADER_LEN,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000246 min_mtu);
247 }
248 rcu_read_unlock();
249out:
250 return min_mtu;
251}
252
253/* adjusts the MTU if a new interface with a smaller MTU appeared. */
Sven Eckelmann95638772012-05-12 02:09:31 +0200254void batadv_update_min_mtu(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000255{
256 int min_mtu;
257
Sven Eckelmann95638772012-05-12 02:09:31 +0200258 min_mtu = batadv_hardif_min_mtu(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259 if (soft_iface->mtu != min_mtu)
260 soft_iface->mtu = min_mtu;
261}
262
Sven Eckelmann56303d32012-06-05 22:31:31 +0200263static void
264batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000265{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200266 struct batadv_priv *bat_priv;
267 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200269 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200270 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000271
Marek Lindnere6c10f42011-02-18 12:33:20 +0000272 bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000273
Marek Lindnerc3229392012-03-11 06:17:50 +0800274 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200275 hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000276
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200277 /* the first active interface becomes our primary interface or
Antonio Quartulli015758d2011-07-09 17:52:13 +0200278 * the next active interface after the old primary interface was removed
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000279 */
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200280 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200281 if (!primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200282 batadv_primary_if_select(bat_priv, hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000283
Sven Eckelmann3e348192012-05-16 20:23:22 +0200284 batadv_info(hard_iface->soft_iface, "Interface activated: %s\n",
285 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000286
Sven Eckelmann95638772012-05-12 02:09:31 +0200287 batadv_update_min_mtu(hard_iface->soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200288
289out:
290 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200291 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292}
293
Sven Eckelmann56303d32012-06-05 22:31:31 +0200294static void
295batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000296{
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200297 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
298 (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000299 return;
300
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200301 hard_iface->if_status = BATADV_IF_INACTIVE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000302
Sven Eckelmann3e348192012-05-16 20:23:22 +0200303 batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
304 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000305
Sven Eckelmann95638772012-05-12 02:09:31 +0200306 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000307}
308
Sven Eckelmann56303d32012-06-05 22:31:31 +0200309int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
Sven Eckelmann95638772012-05-12 02:09:31 +0200310 const char *iface_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000311{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200312 struct batadv_priv *bat_priv;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000313 struct net_device *soft_iface;
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200314 __be16 ethertype = __constant_htons(BATADV_ETH_P_BATMAN);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000315 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000316
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200317 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000318 goto out;
319
Marek Lindnere6c10f42011-02-18 12:33:20 +0000320 if (!atomic_inc_not_zero(&hard_iface->refcount))
Marek Lindnered75ccb2011-02-10 14:33:51 +0000321 goto out;
322
Marek Lindner6e242f92011-12-07 18:02:50 +0800323 /* hard-interface is part of a bridge */
324 if (hard_iface->net_dev->priv_flags & IFF_BRIDGE_PORT)
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100325 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 +0800326 hard_iface->net_dev->name);
327
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000328 soft_iface = dev_get_by_name(&init_net, iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000329
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000330 if (!soft_iface) {
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200331 soft_iface = batadv_softif_create(iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000332
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000333 if (!soft_iface) {
334 ret = -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000335 goto err;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000336 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000337
338 /* dev_get_by_name() increases the reference counter for us */
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000339 dev_hold(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000340 }
341
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200342 if (!batadv_softif_is_valid(soft_iface)) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100343 pr_err("Can't create batman mesh interface %s: already exists as regular interface\n",
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000344 soft_iface->name);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000345 ret = -EINVAL;
Marek Lindner77af7572012-02-07 17:20:48 +0800346 goto err_dev;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000347 }
348
349 hard_iface->soft_iface = soft_iface;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000350 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200351
Marek Lindner77af7572012-02-07 17:20:48 +0800352 ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200353 if (ret < 0)
Marek Lindner77af7572012-02-07 17:20:48 +0800354 goto err_dev;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000355
Marek Lindnere6c10f42011-02-18 12:33:20 +0000356 hard_iface->if_num = bat_priv->num_ifaces;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000357 bat_priv->num_ifaces++;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200358 hard_iface->if_status = BATADV_IF_INACTIVE;
Simon Wunderlich62446302012-07-01 22:51:55 +0200359 ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
360 if (ret < 0) {
361 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
362 bat_priv->num_ifaces--;
363 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
364 goto err_dev;
365 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000366
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200367 hard_iface->batman_adv_ptype.type = ethertype;
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200368 hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000369 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
370 dev_add_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000371
Marek Lindnere6c10f42011-02-18 12:33:20 +0000372 atomic_set(&hard_iface->frag_seqno, 1);
Sven Eckelmann3e348192012-05-16 20:23:22 +0200373 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
374 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375
Sven Eckelmann0aca2362012-06-19 20:26:30 +0200376 if (atomic_read(&bat_priv->fragmentation) &&
377 hard_iface->net_dev->mtu < ETH_DATA_LEN + BATADV_HEADER_LEN)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200378 batadv_info(hard_iface->soft_iface,
379 "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",
380 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Sven Eckelmannc11fdfa2012-06-03 22:19:14 +0200381 ETH_DATA_LEN + BATADV_HEADER_LEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000382
Sven Eckelmann0aca2362012-06-19 20:26:30 +0200383 if (!atomic_read(&bat_priv->fragmentation) &&
384 hard_iface->net_dev->mtu < ETH_DATA_LEN + BATADV_HEADER_LEN)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200385 batadv_info(hard_iface->soft_iface,
386 "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",
387 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Sven Eckelmannc11fdfa2012-06-03 22:19:14 +0200388 ETH_DATA_LEN + BATADV_HEADER_LEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200390 if (batadv_hardif_is_iface_up(hard_iface))
391 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000392 else
Sven Eckelmann3e348192012-05-16 20:23:22 +0200393 batadv_err(hard_iface->soft_iface,
394 "Not using interface %s (retrying later): interface not active\n",
395 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000396
397 /* begin scheduling originator messages on that interface */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200398 batadv_schedule_bat_ogm(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000399
400out:
401 return 0;
402
Marek Lindner77af7572012-02-07 17:20:48 +0800403err_dev:
404 dev_put(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405err:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200406 batadv_hardif_free_ref(hard_iface);
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000407 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000408}
409
Sven Eckelmann56303d32012-06-05 22:31:31 +0200410void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000411{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200412 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
413 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000414
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200415 if (hard_iface->if_status == BATADV_IF_ACTIVE)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200416 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000417
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200418 if (hard_iface->if_status != BATADV_IF_INACTIVE)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200419 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000420
Sven Eckelmann3e348192012-05-16 20:23:22 +0200421 batadv_info(hard_iface->soft_iface, "Removing interface: %s\n",
422 hard_iface->net_dev->name);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000423 dev_remove_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000424
425 bat_priv->num_ifaces--;
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200426 batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000427
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200428 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200429 if (hard_iface == primary_if) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200430 struct batadv_hard_iface *new_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000431
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200432 new_if = batadv_hardif_get_active(hard_iface->soft_iface);
433 batadv_primary_if_select(bat_priv, new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000434
435 if (new_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200436 batadv_hardif_free_ref(new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437 }
438
Marek Lindner00a50072012-02-07 17:20:47 +0800439 bat_priv->bat_algo_ops->bat_iface_disable(hard_iface);
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200440 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441
Marek Lindnere6c10f42011-02-18 12:33:20 +0000442 /* delete all references to this hard_iface */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200443 batadv_purge_orig_ref(bat_priv);
Sven Eckelmann9455e342012-05-12 02:09:37 +0200444 batadv_purge_outstanding_packets(bat_priv, hard_iface);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000445 dev_put(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000446
447 /* nobody uses this interface anymore */
448 if (!bat_priv->num_ifaces)
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200449 batadv_softif_destroy(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000450
Marek Lindnere6c10f42011-02-18 12:33:20 +0000451 hard_iface->soft_iface = NULL;
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200452 batadv_hardif_free_ref(hard_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200453
454out:
455 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200456 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000457}
458
Sven Eckelmann56303d32012-06-05 22:31:31 +0200459static struct batadv_hard_iface *
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200460batadv_hardif_add_interface(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000461{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200462 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000463 int ret;
464
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200465 ASSERT_RTNL();
466
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200467 ret = batadv_is_valid_iface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000468 if (ret != 1)
469 goto out;
470
471 dev_hold(net_dev);
472
Sven Eckelmann704509b2011-05-14 23:14:54 +0200473 hard_iface = kmalloc(sizeof(*hard_iface), GFP_ATOMIC);
Joe Perches320f4222011-08-29 14:17:24 -0700474 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000475 goto release_dev;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000476
Sven Eckelmann5853e222012-05-12 02:09:24 +0200477 ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000478 if (ret)
479 goto free_if;
480
Marek Lindnere6c10f42011-02-18 12:33:20 +0000481 hard_iface->if_num = -1;
482 hard_iface->net_dev = net_dev;
483 hard_iface->soft_iface = NULL;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200484 hard_iface->if_status = BATADV_IF_NOT_IN_USE;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000485 INIT_LIST_HEAD(&hard_iface->list);
Marek Lindnered75ccb2011-02-10 14:33:51 +0000486 /* extra reference for return */
Marek Lindnere6c10f42011-02-18 12:33:20 +0000487 atomic_set(&hard_iface->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000488
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200489 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200490 list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000491
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200492 /* This can't be called via a bat_priv callback because
Marek Lindner81406252012-02-07 17:19:58 +0800493 * we have no bat_priv yet.
494 */
Marek Lindner14511512012-08-02 17:20:26 +0200495 atomic_set(&hard_iface->bat_iv.ogm_seqno, 1);
496 hard_iface->bat_iv.ogm_buff = NULL;
Marek Lindner81406252012-02-07 17:19:58 +0800497
Marek Lindnere6c10f42011-02-18 12:33:20 +0000498 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000499
500free_if:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000501 kfree(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000502release_dev:
503 dev_put(net_dev);
504out:
505 return NULL;
506}
507
Sven Eckelmann56303d32012-06-05 22:31:31 +0200508static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000509{
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200510 ASSERT_RTNL();
511
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000512 /* first deactivate interface */
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200513 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmann95638772012-05-12 02:09:31 +0200514 batadv_hardif_disable_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000515
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200516 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000517 return;
518
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200519 hard_iface->if_status = BATADV_IF_TO_BE_REMOVED;
Sven Eckelmann5853e222012-05-12 02:09:24 +0200520 batadv_sysfs_del_hardif(&hard_iface->hardif_obj);
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200521 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000522}
523
Sven Eckelmann95638772012-05-12 02:09:31 +0200524void batadv_hardif_remove_interfaces(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000525{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200526 struct batadv_hard_iface *hard_iface, *hard_iface_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000527
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200528 rtnl_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000529 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200530 &batadv_hardif_list, list) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000531 list_del_rcu(&hard_iface->list);
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200532 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000533 }
534 rtnl_unlock();
535}
536
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200537static int batadv_hard_if_event(struct notifier_block *this,
538 unsigned long event, void *ptr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000539{
Sven Eckelmann5f718c22011-05-14 23:14:52 +0200540 struct net_device *net_dev = ptr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200541 struct batadv_hard_iface *hard_iface;
542 struct batadv_hard_iface *primary_if = NULL;
543 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000544
Sven Eckelmann56303d32012-06-05 22:31:31 +0200545 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000546 if (!hard_iface && event == NETDEV_REGISTER)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200547 hard_iface = batadv_hardif_add_interface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000548
Marek Lindnere6c10f42011-02-18 12:33:20 +0000549 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550 goto out;
551
552 switch (event) {
553 case NETDEV_UP:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200554 batadv_hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000555 break;
556 case NETDEV_GOING_DOWN:
557 case NETDEV_DOWN:
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200558 batadv_hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000559 break;
560 case NETDEV_UNREGISTER:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000561 list_del_rcu(&hard_iface->list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000562
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200563 batadv_hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000564 break;
565 case NETDEV_CHANGEMTU:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000566 if (hard_iface->soft_iface)
Sven Eckelmann95638772012-05-12 02:09:31 +0200567 batadv_update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000568 break;
569 case NETDEV_CHANGEADDR:
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200570 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000571 goto hardif_put;
572
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200573 batadv_check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000574
Marek Lindnere6c10f42011-02-18 12:33:20 +0000575 bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerc3229392012-03-11 06:17:50 +0800576 bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface);
Marek Lindner01c42242011-11-28 21:31:55 +0800577
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200578 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200579 if (!primary_if)
580 goto hardif_put;
581
582 if (hard_iface == primary_if)
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200583 batadv_primary_if_update_addr(bat_priv, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000584 break;
585 default:
586 break;
Joe Perchesf81c6222011-06-03 11:51:19 +0000587 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000588
589hardif_put:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200590 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000591out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200592 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200593 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000594 return NOTIFY_DONE;
595}
596
Antonio Quartullibc279082011-07-07 15:35:35 +0200597/* This function returns true if the interface represented by ifindex is a
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200598 * 802.11 wireless device
599 */
Sven Eckelmann95638772012-05-12 02:09:31 +0200600bool batadv_is_wifi_iface(int ifindex)
Antonio Quartullibc279082011-07-07 15:35:35 +0200601{
602 struct net_device *net_device = NULL;
603 bool ret = false;
604
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200605 if (ifindex == BATADV_NULL_IFINDEX)
Antonio Quartullibc279082011-07-07 15:35:35 +0200606 goto out;
607
608 net_device = dev_get_by_index(&init_net, ifindex);
609 if (!net_device)
610 goto out;
611
612#ifdef CONFIG_WIRELESS_EXT
613 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200614 * check for wireless_handlers != NULL
615 */
Antonio Quartullibc279082011-07-07 15:35:35 +0200616 if (net_device->wireless_handlers)
617 ret = true;
618 else
619#endif
620 /* cfg80211 drivers have to set ieee80211_ptr */
621 if (net_device->ieee80211_ptr)
622 ret = true;
623out:
624 if (net_device)
625 dev_put(net_device);
626 return ret;
627}
628
Sven Eckelmann95638772012-05-12 02:09:31 +0200629struct notifier_block batadv_hard_if_notifier = {
Sven Eckelmann18a1cb62012-05-12 18:33:57 +0200630 .notifier_call = batadv_hard_if_event,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000631};