blob: 95a35b695700dd8ce8225449d2f33e56aca47d4d [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann64afe352011-01-27 10:38:15 +01002 * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner, Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
19 *
20 */
21
22#include "main.h"
23#include "hard-interface.h"
24#include "soft-interface.h"
25#include "send.h"
26#include "translation-table.h"
27#include "routing.h"
28#include "bat_sysfs.h"
29#include "originator.h"
30#include "hash.h"
31
32#include <linux/if_arp.h>
33
Marek Lindner4389e472011-02-18 12:33:19 +000034/* protect update critical side of hardif_list - but not the content */
35static DEFINE_SPINLOCK(hardif_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000036
Sven Eckelmannfb86d762011-01-27 13:16:08 +010037
38static int batman_skb_recv(struct sk_buff *skb,
39 struct net_device *dev,
40 struct packet_type *ptype,
41 struct net_device *orig_dev);
42
Marek Lindnered75ccb2011-02-10 14:33:51 +000043void hardif_free_rcu(struct rcu_head *rcu)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044{
Marek Lindnere6c10f42011-02-18 12:33:20 +000045 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046
Marek Lindnere6c10f42011-02-18 12:33:20 +000047 hard_iface = container_of(rcu, struct hard_iface, rcu);
48 dev_put(hard_iface->net_dev);
49 kfree(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000050}
51
Marek Lindnere6c10f42011-02-18 12:33:20 +000052struct hard_iface *hardif_get_by_netdev(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000053{
Marek Lindnere6c10f42011-02-18 12:33:20 +000054 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000055
56 rcu_read_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +000057 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
58 if (hard_iface->net_dev == net_dev &&
59 atomic_inc_not_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000060 goto out;
61 }
62
Marek Lindnere6c10f42011-02-18 12:33:20 +000063 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000064
65out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000066 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +000067 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000068}
69
70static int is_valid_iface(struct net_device *net_dev)
71{
72 if (net_dev->flags & IFF_LOOPBACK)
73 return 0;
74
75 if (net_dev->type != ARPHRD_ETHER)
76 return 0;
77
78 if (net_dev->addr_len != ETH_ALEN)
79 return 0;
80
81 /* no batman over batman */
82#ifdef HAVE_NET_DEVICE_OPS
83 if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
84 return 0;
85#else
86 if (net_dev->hard_start_xmit == interface_tx)
87 return 0;
88#endif
89
90 /* Device is being bridged */
91 /* if (net_dev->priv_flags & IFF_BRIDGE_PORT)
92 return 0; */
93
94 return 1;
95}
96
Marek Lindnere6c10f42011-02-18 12:33:20 +000097static struct hard_iface *hardif_get_active(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000098{
Marek Lindnere6c10f42011-02-18 12:33:20 +000099 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000100
101 rcu_read_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000102 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
103 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000104 continue;
105
Marek Lindnere6c10f42011-02-18 12:33:20 +0000106 if (hard_iface->if_status == IF_ACTIVE &&
107 atomic_inc_not_zero(&hard_iface->refcount))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000108 goto out;
109 }
110
Marek Lindnere6c10f42011-02-18 12:33:20 +0000111 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000112
113out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000114 rcu_read_unlock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000115 return hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000116}
117
118static void update_primary_addr(struct bat_priv *bat_priv)
119{
120 struct vis_packet *vis_packet;
121
122 vis_packet = (struct vis_packet *)
123 bat_priv->my_vis_info->skb_packet->data;
124 memcpy(vis_packet->vis_orig,
125 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
126 memcpy(vis_packet->sender_orig,
127 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
128}
129
130static void set_primary_if(struct bat_priv *bat_priv,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000131 struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000132{
133 struct batman_packet *batman_packet;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000134 struct hard_iface *old_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000135
Marek Lindnere6c10f42011-02-18 12:33:20 +0000136 if (hard_iface && !atomic_inc_not_zero(&hard_iface->refcount))
137 hard_iface = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000138
139 old_if = bat_priv->primary_if;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000140 bat_priv->primary_if = hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000141
142 if (old_if)
Marek Lindnered75ccb2011-02-10 14:33:51 +0000143 hardif_free_ref(old_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000144
145 if (!bat_priv->primary_if)
146 return;
147
Marek Lindnere6c10f42011-02-18 12:33:20 +0000148 batman_packet = (struct batman_packet *)(hard_iface->packet_buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000149 batman_packet->flags = PRIMARIES_FIRST_HOP;
150 batman_packet->ttl = TTL;
151
152 update_primary_addr(bat_priv);
153
154 /***
155 * hacky trick to make sure that we send the HNA information via
156 * our new primary interface
157 */
158 atomic_set(&bat_priv->hna_local_changed, 1);
159}
160
Marek Lindnere6c10f42011-02-18 12:33:20 +0000161static bool hardif_is_iface_up(struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000162{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000163 if (hard_iface->net_dev->flags & IFF_UP)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000164 return true;
165
166 return false;
167}
168
Marek Lindnere6c10f42011-02-18 12:33:20 +0000169static void update_mac_addresses(struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000170{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000171 memcpy(((struct batman_packet *)(hard_iface->packet_buff))->orig,
172 hard_iface->net_dev->dev_addr, ETH_ALEN);
173 memcpy(((struct batman_packet *)(hard_iface->packet_buff))->prev_sender,
174 hard_iface->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000175}
176
177static void check_known_mac_addr(struct net_device *net_dev)
178{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000179 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000180
181 rcu_read_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000182 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
183 if ((hard_iface->if_status != IF_ACTIVE) &&
184 (hard_iface->if_status != IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000185 continue;
186
Marek Lindnere6c10f42011-02-18 12:33:20 +0000187 if (hard_iface->net_dev == net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188 continue;
189
Marek Lindnere6c10f42011-02-18 12:33:20 +0000190 if (!compare_eth(hard_iface->net_dev->dev_addr,
191 net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000192 continue;
193
194 pr_warning("The newly added mac address (%pM) already exists "
195 "on: %s\n", net_dev->dev_addr,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000196 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000197 pr_warning("It is strongly recommended to keep mac addresses "
198 "unique to avoid problems!\n");
199 }
200 rcu_read_unlock();
201}
202
203int hardif_min_mtu(struct net_device *soft_iface)
204{
205 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000206 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000207 /* allow big frames if all devices are capable to do so
208 * (have MTU > 1500 + BAT_HEADER_LEN) */
209 int min_mtu = ETH_DATA_LEN;
210
211 if (atomic_read(&bat_priv->fragmentation))
212 goto out;
213
214 rcu_read_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000215 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
216 if ((hard_iface->if_status != IF_ACTIVE) &&
217 (hard_iface->if_status != IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000218 continue;
219
Marek Lindnere6c10f42011-02-18 12:33:20 +0000220 if (hard_iface->soft_iface != soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000221 continue;
222
Marek Lindnere6c10f42011-02-18 12:33:20 +0000223 min_mtu = min_t(int, hard_iface->net_dev->mtu - BAT_HEADER_LEN,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000224 min_mtu);
225 }
226 rcu_read_unlock();
227out:
228 return min_mtu;
229}
230
231/* adjusts the MTU if a new interface with a smaller MTU appeared. */
232void update_min_mtu(struct net_device *soft_iface)
233{
234 int min_mtu;
235
236 min_mtu = hardif_min_mtu(soft_iface);
237 if (soft_iface->mtu != min_mtu)
238 soft_iface->mtu = min_mtu;
239}
240
Marek Lindnere6c10f42011-02-18 12:33:20 +0000241static void hardif_activate_interface(struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000242{
243 struct bat_priv *bat_priv;
244
Marek Lindnere6c10f42011-02-18 12:33:20 +0000245 if (hard_iface->if_status != IF_INACTIVE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000246 return;
247
Marek Lindnere6c10f42011-02-18 12:33:20 +0000248 bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000249
Marek Lindnere6c10f42011-02-18 12:33:20 +0000250 update_mac_addresses(hard_iface);
251 hard_iface->if_status = IF_TO_BE_ACTIVATED;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000252
253 /**
254 * the first active interface becomes our primary interface or
255 * the next active interface after the old primay interface was removed
256 */
257 if (!bat_priv->primary_if)
Marek Lindnere6c10f42011-02-18 12:33:20 +0000258 set_primary_if(bat_priv, hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259
Marek Lindnere6c10f42011-02-18 12:33:20 +0000260 bat_info(hard_iface->soft_iface, "Interface activated: %s\n",
261 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000262
Marek Lindnere6c10f42011-02-18 12:33:20 +0000263 update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000264 return;
265}
266
Marek Lindnere6c10f42011-02-18 12:33:20 +0000267static void hardif_deactivate_interface(struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000269 if ((hard_iface->if_status != IF_ACTIVE) &&
270 (hard_iface->if_status != IF_TO_BE_ACTIVATED))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000271 return;
272
Marek Lindnere6c10f42011-02-18 12:33:20 +0000273 hard_iface->if_status = IF_INACTIVE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000274
Marek Lindnere6c10f42011-02-18 12:33:20 +0000275 bat_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
276 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000277
Marek Lindnere6c10f42011-02-18 12:33:20 +0000278 update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000279}
280
Marek Lindnere6c10f42011-02-18 12:33:20 +0000281int hardif_enable_interface(struct hard_iface *hard_iface, char *iface_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000282{
283 struct bat_priv *bat_priv;
284 struct batman_packet *batman_packet;
285
Marek Lindnere6c10f42011-02-18 12:33:20 +0000286 if (hard_iface->if_status != IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000287 goto out;
288
Marek Lindnere6c10f42011-02-18 12:33:20 +0000289 if (!atomic_inc_not_zero(&hard_iface->refcount))
Marek Lindnered75ccb2011-02-10 14:33:51 +0000290 goto out;
291
Marek Lindnere6c10f42011-02-18 12:33:20 +0000292 hard_iface->soft_iface = dev_get_by_name(&init_net, iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000293
Marek Lindnere6c10f42011-02-18 12:33:20 +0000294 if (!hard_iface->soft_iface) {
295 hard_iface->soft_iface = softif_create(iface_name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000296
Marek Lindnere6c10f42011-02-18 12:33:20 +0000297 if (!hard_iface->soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000298 goto err;
299
300 /* dev_get_by_name() increases the reference counter for us */
Marek Lindnere6c10f42011-02-18 12:33:20 +0000301 dev_hold(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000302 }
303
Marek Lindnere6c10f42011-02-18 12:33:20 +0000304 bat_priv = netdev_priv(hard_iface->soft_iface);
305 hard_iface->packet_len = BAT_PACKET_LEN;
306 hard_iface->packet_buff = kmalloc(hard_iface->packet_len, GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000307
Marek Lindnere6c10f42011-02-18 12:33:20 +0000308 if (!hard_iface->packet_buff) {
309 bat_err(hard_iface->soft_iface, "Can't add interface packet "
310 "(%s): out of memory\n", hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000311 goto err;
312 }
313
Marek Lindnere6c10f42011-02-18 12:33:20 +0000314 batman_packet = (struct batman_packet *)(hard_iface->packet_buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000315 batman_packet->packet_type = BAT_PACKET;
316 batman_packet->version = COMPAT_VERSION;
317 batman_packet->flags = 0;
318 batman_packet->ttl = 2;
319 batman_packet->tq = TQ_MAX_VALUE;
320 batman_packet->num_hna = 0;
321
Marek Lindnere6c10f42011-02-18 12:33:20 +0000322 hard_iface->if_num = bat_priv->num_ifaces;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000323 bat_priv->num_ifaces++;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000324 hard_iface->if_status = IF_INACTIVE;
325 orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000326
Marek Lindnere6c10f42011-02-18 12:33:20 +0000327 hard_iface->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
328 hard_iface->batman_adv_ptype.func = batman_skb_recv;
329 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
330 dev_add_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000331
Marek Lindnere6c10f42011-02-18 12:33:20 +0000332 atomic_set(&hard_iface->seqno, 1);
333 atomic_set(&hard_iface->frag_seqno, 1);
334 bat_info(hard_iface->soft_iface, "Adding interface: %s\n",
335 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000336
Marek Lindnere6c10f42011-02-18 12:33:20 +0000337 if (atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000338 ETH_DATA_LEN + BAT_HEADER_LEN)
Marek Lindnere6c10f42011-02-18 12:33:20 +0000339 bat_info(hard_iface->soft_iface,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000340 "The MTU of interface %s is too small (%i) to handle "
341 "the transport of batman-adv packets. Packets going "
342 "over this interface will be fragmented on layer2 "
343 "which could impact the performance. Setting the MTU "
344 "to %zi would solve the problem.\n",
Marek Lindnere6c10f42011-02-18 12:33:20 +0000345 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000346 ETH_DATA_LEN + BAT_HEADER_LEN);
347
Marek Lindnere6c10f42011-02-18 12:33:20 +0000348 if (!atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000349 ETH_DATA_LEN + BAT_HEADER_LEN)
Marek Lindnere6c10f42011-02-18 12:33:20 +0000350 bat_info(hard_iface->soft_iface,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000351 "The MTU of interface %s is too small (%i) to handle "
352 "the transport of batman-adv packets. If you experience"
353 " problems getting traffic through try increasing the "
354 "MTU to %zi.\n",
Marek Lindnere6c10f42011-02-18 12:33:20 +0000355 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000356 ETH_DATA_LEN + BAT_HEADER_LEN);
357
Marek Lindnere6c10f42011-02-18 12:33:20 +0000358 if (hardif_is_iface_up(hard_iface))
359 hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000360 else
Marek Lindnere6c10f42011-02-18 12:33:20 +0000361 bat_err(hard_iface->soft_iface, "Not using interface %s "
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000362 "(retrying later): interface not active\n",
Marek Lindnere6c10f42011-02-18 12:33:20 +0000363 hard_iface->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000364
365 /* begin scheduling originator messages on that interface */
Marek Lindnere6c10f42011-02-18 12:33:20 +0000366 schedule_own_packet(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367
368out:
369 return 0;
370
371err:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000372 hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000373 return -ENOMEM;
374}
375
Marek Lindnere6c10f42011-02-18 12:33:20 +0000376void hardif_disable_interface(struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000378 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379
Marek Lindnere6c10f42011-02-18 12:33:20 +0000380 if (hard_iface->if_status == IF_ACTIVE)
381 hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000382
Marek Lindnere6c10f42011-02-18 12:33:20 +0000383 if (hard_iface->if_status != IF_INACTIVE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000384 return;
385
Marek Lindnere6c10f42011-02-18 12:33:20 +0000386 bat_info(hard_iface->soft_iface, "Removing interface: %s\n",
387 hard_iface->net_dev->name);
388 dev_remove_pack(&hard_iface->batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389
390 bat_priv->num_ifaces--;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000391 orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000392
Marek Lindnere6c10f42011-02-18 12:33:20 +0000393 if (hard_iface == bat_priv->primary_if) {
394 struct hard_iface *new_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000395
Marek Lindnere6c10f42011-02-18 12:33:20 +0000396 new_if = hardif_get_active(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000397 set_primary_if(bat_priv, new_if);
398
399 if (new_if)
Marek Lindnered75ccb2011-02-10 14:33:51 +0000400 hardif_free_ref(new_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000401 }
402
Marek Lindnere6c10f42011-02-18 12:33:20 +0000403 kfree(hard_iface->packet_buff);
404 hard_iface->packet_buff = NULL;
405 hard_iface->if_status = IF_NOT_IN_USE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000406
Marek Lindnere6c10f42011-02-18 12:33:20 +0000407 /* delete all references to this hard_iface */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000408 purge_orig_ref(bat_priv);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000409 purge_outstanding_packets(bat_priv, hard_iface);
410 dev_put(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000411
412 /* nobody uses this interface anymore */
413 if (!bat_priv->num_ifaces)
Marek Lindnere6c10f42011-02-18 12:33:20 +0000414 softif_destroy(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415
Marek Lindnere6c10f42011-02-18 12:33:20 +0000416 hard_iface->soft_iface = NULL;
417 hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000418}
419
Marek Lindnere6c10f42011-02-18 12:33:20 +0000420static struct hard_iface *hardif_add_interface(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000421{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000422 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423 int ret;
424
425 ret = is_valid_iface(net_dev);
426 if (ret != 1)
427 goto out;
428
429 dev_hold(net_dev);
430
Marek Lindnere6c10f42011-02-18 12:33:20 +0000431 hard_iface = kmalloc(sizeof(struct hard_iface), GFP_ATOMIC);
432 if (!hard_iface) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000433 pr_err("Can't add interface (%s): out of memory\n",
434 net_dev->name);
435 goto release_dev;
436 }
437
Marek Lindnere6c10f42011-02-18 12:33:20 +0000438 ret = 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;
445 hard_iface->if_status = IF_NOT_IN_USE;
446 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
Marek Lindnere6c10f42011-02-18 12:33:20 +0000450 check_known_mac_addr(hard_iface->net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000451
Marek Lindner4389e472011-02-18 12:33:19 +0000452 spin_lock(&hardif_list_lock);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000453 list_add_tail_rcu(&hard_iface->list, &hardif_list);
Marek Lindner4389e472011-02-18 12:33:19 +0000454 spin_unlock(&hardif_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000455
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
Marek Lindnere6c10f42011-02-18 12:33:20 +0000466static void hardif_remove_interface(struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467{
468 /* first deactivate interface */
Marek Lindnere6c10f42011-02-18 12:33:20 +0000469 if (hard_iface->if_status != IF_NOT_IN_USE)
470 hardif_disable_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471
Marek Lindnere6c10f42011-02-18 12:33:20 +0000472 if (hard_iface->if_status != IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000473 return;
474
Marek Lindnere6c10f42011-02-18 12:33:20 +0000475 hard_iface->if_status = IF_TO_BE_REMOVED;
476 sysfs_del_hardif(&hard_iface->hardif_obj);
477 hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000478}
479
480void hardif_remove_interfaces(void)
481{
Marek Lindnere6c10f42011-02-18 12:33:20 +0000482 struct hard_iface *hard_iface, *hard_iface_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000483 struct list_head if_queue;
484
485 INIT_LIST_HEAD(&if_queue);
486
Marek Lindner4389e472011-02-18 12:33:19 +0000487 spin_lock(&hardif_list_lock);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000488 list_for_each_entry_safe(hard_iface, hard_iface_tmp,
489 &hardif_list, list) {
490 list_del_rcu(&hard_iface->list);
491 list_add_tail(&hard_iface->list, &if_queue);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000492 }
Marek Lindner4389e472011-02-18 12:33:19 +0000493 spin_unlock(&hardif_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000494
495 rtnl_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000496 list_for_each_entry_safe(hard_iface, hard_iface_tmp, &if_queue, list) {
497 hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000498 }
499 rtnl_unlock();
500}
501
502static int hard_if_event(struct notifier_block *this,
503 unsigned long event, void *ptr)
504{
505 struct net_device *net_dev = (struct net_device *)ptr;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000506 struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000507 struct bat_priv *bat_priv;
508
Marek Lindnere6c10f42011-02-18 12:33:20 +0000509 if (!hard_iface && event == NETDEV_REGISTER)
510 hard_iface = hardif_add_interface(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000511
Marek Lindnere6c10f42011-02-18 12:33:20 +0000512 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000513 goto out;
514
515 switch (event) {
516 case NETDEV_UP:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000517 hardif_activate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000518 break;
519 case NETDEV_GOING_DOWN:
520 case NETDEV_DOWN:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000521 hardif_deactivate_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000522 break;
523 case NETDEV_UNREGISTER:
Marek Lindner4389e472011-02-18 12:33:19 +0000524 spin_lock(&hardif_list_lock);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000525 list_del_rcu(&hard_iface->list);
Marek Lindner4389e472011-02-18 12:33:19 +0000526 spin_unlock(&hardif_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000527
Marek Lindnere6c10f42011-02-18 12:33:20 +0000528 hardif_remove_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000529 break;
530 case NETDEV_CHANGEMTU:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000531 if (hard_iface->soft_iface)
532 update_min_mtu(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000533 break;
534 case NETDEV_CHANGEADDR:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000535 if (hard_iface->if_status == IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000536 goto hardif_put;
537
Marek Lindnere6c10f42011-02-18 12:33:20 +0000538 check_known_mac_addr(hard_iface->net_dev);
539 update_mac_addresses(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000540
Marek Lindnere6c10f42011-02-18 12:33:20 +0000541 bat_priv = netdev_priv(hard_iface->soft_iface);
542 if (hard_iface == bat_priv->primary_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000543 update_primary_addr(bat_priv);
544 break;
545 default:
546 break;
547 };
548
549hardif_put:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000550 hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000551out:
552 return NOTIFY_DONE;
553}
554
555/* receive a packet with the batman ethertype coming on a hard
556 * interface */
Sven Eckelmannfb86d762011-01-27 13:16:08 +0100557static int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
558 struct packet_type *ptype,
559 struct net_device *orig_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000560{
561 struct bat_priv *bat_priv;
562 struct batman_packet *batman_packet;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000563 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000564 int ret;
565
Marek Lindnere6c10f42011-02-18 12:33:20 +0000566 hard_iface = container_of(ptype, struct hard_iface, batman_adv_ptype);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000567 skb = skb_share_check(skb, GFP_ATOMIC);
568
569 /* skb was released by skb_share_check() */
570 if (!skb)
571 goto err_out;
572
573 /* packet should hold at least type and version */
574 if (unlikely(!pskb_may_pull(skb, 2)))
575 goto err_free;
576
577 /* expect a valid ethernet header here. */
578 if (unlikely(skb->mac_len != sizeof(struct ethhdr)
579 || !skb_mac_header(skb)))
580 goto err_free;
581
Marek Lindnere6c10f42011-02-18 12:33:20 +0000582 if (!hard_iface->soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000583 goto err_free;
584
Marek Lindnere6c10f42011-02-18 12:33:20 +0000585 bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000586
587 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
588 goto err_free;
589
590 /* discard frames on not active interfaces */
Marek Lindnere6c10f42011-02-18 12:33:20 +0000591 if (hard_iface->if_status != IF_ACTIVE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000592 goto err_free;
593
594 batman_packet = (struct batman_packet *)skb->data;
595
596 if (batman_packet->version != COMPAT_VERSION) {
597 bat_dbg(DBG_BATMAN, bat_priv,
598 "Drop packet: incompatible batman version (%i)\n",
599 batman_packet->version);
600 goto err_free;
601 }
602
603 /* all receive handlers return whether they received or reused
604 * the supplied skb. if not, we have to free the skb. */
605
606 switch (batman_packet->packet_type) {
607 /* batman originator packet */
608 case BAT_PACKET:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000609 ret = recv_bat_packet(skb, hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000610 break;
611
612 /* batman icmp packet */
613 case BAT_ICMP:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000614 ret = recv_icmp_packet(skb, hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000615 break;
616
617 /* unicast packet */
618 case BAT_UNICAST:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000619 ret = recv_unicast_packet(skb, hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000620 break;
621
622 /* fragmented unicast packet */
623 case BAT_UNICAST_FRAG:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000624 ret = recv_ucast_frag_packet(skb, hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000625 break;
626
627 /* broadcast packet */
628 case BAT_BCAST:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000629 ret = recv_bcast_packet(skb, hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000630 break;
631
632 /* vis packet */
633 case BAT_VIS:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000634 ret = recv_vis_packet(skb, hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000635 break;
636 default:
637 ret = NET_RX_DROP;
638 }
639
640 if (ret == NET_RX_DROP)
641 kfree_skb(skb);
642
643 /* return NET_RX_SUCCESS in any case as we
644 * most probably dropped the packet for
645 * routing-logical reasons. */
646
647 return NET_RX_SUCCESS;
648
649err_free:
650 kfree_skb(skb);
651err_out:
652 return NET_RX_DROP;
653}
654
655struct notifier_block hard_if_notifier = {
656 .notifier_call = hard_if_event,
657};