blob: 0658781febdeb1ecfbc96920a469c4c1fd74617b [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 "soft-interface.h"
22#include "hard-interface.h"
23#include "routing.h"
24#include "send.h"
25#include "bat_debugfs.h"
26#include "translation-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000027#include "hash.h"
28#include "gateway_common.h"
29#include "gateway_client.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000030#include "bat_sysfs.h"
Antonio Quartulli43676ab2011-04-26 21:31:45 +020031#include "originator.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000032#include <linux/slab.h>
33#include <linux/ethtool.h>
34#include <linux/etherdevice.h>
35#include <linux/if_vlan.h>
36#include "unicast.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010037#include "bridge_loop_avoidance.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000038
39
40static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
41static void bat_get_drvinfo(struct net_device *dev,
42 struct ethtool_drvinfo *info);
43static u32 bat_get_msglevel(struct net_device *dev);
44static void bat_set_msglevel(struct net_device *dev, u32 value);
45static u32 bat_get_link(struct net_device *dev);
Martin Hundebøllf8214862012-04-20 17:02:45 +020046static void batadv_get_strings(struct net_device *dev, u32 stringset, u8 *data);
47static void batadv_get_ethtool_stats(struct net_device *dev,
48 struct ethtool_stats *stats, u64 *data);
49static int batadv_get_sset_count(struct net_device *dev, int stringset);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000050
51static const struct ethtool_ops bat_ethtool_ops = {
52 .get_settings = bat_get_settings,
53 .get_drvinfo = bat_get_drvinfo,
54 .get_msglevel = bat_get_msglevel,
55 .set_msglevel = bat_set_msglevel,
56 .get_link = bat_get_link,
Martin Hundebøllf8214862012-04-20 17:02:45 +020057 .get_strings = batadv_get_strings,
58 .get_ethtool_stats = batadv_get_ethtool_stats,
59 .get_sset_count = batadv_get_sset_count,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000060};
61
Sven Eckelmann04b482a2012-05-12 02:09:38 +020062int batadv_skb_head_push(struct sk_buff *skb, unsigned int len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000063{
64 int result;
65
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020066 /* TODO: We must check if we can release all references to non-payload
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000067 * data using skb_header_release in our skbs to allow skb_cow_header to
68 * work optimally. This means that those skbs are not allowed to read
69 * or write any data which is before the current position of skb->data
70 * after that call and thus allow other skbs with the same data buffer
71 * to write freely in that area.
72 */
73 result = skb_cow_head(skb, len);
74 if (result < 0)
75 return result;
76
77 skb_push(skb, len);
78 return 0;
79}
80
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000081static int interface_open(struct net_device *dev)
82{
83 netif_start_queue(dev);
84 return 0;
85}
86
87static int interface_release(struct net_device *dev)
88{
89 netif_stop_queue(dev);
90 return 0;
91}
92
93static struct net_device_stats *interface_stats(struct net_device *dev)
94{
95 struct bat_priv *bat_priv = netdev_priv(dev);
96 return &bat_priv->stats;
97}
98
99static int interface_set_mac_addr(struct net_device *dev, void *p)
100{
101 struct bat_priv *bat_priv = netdev_priv(dev);
102 struct sockaddr *addr = p;
103
104 if (!is_valid_ether_addr(addr->sa_data))
105 return -EADDRNOTAVAIL;
106
Antonio Quartulli015758d2011-07-09 17:52:13 +0200107 /* only modify transtable if it has been initialized before */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000108 if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200109 batadv_tt_local_remove(bat_priv, dev->dev_addr,
110 "mac address changed", false);
111 batadv_tt_local_add(dev, addr->sa_data, NULL_IFINDEX);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000112 }
113
114 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
Danny Kukawka28009a62012-02-17 05:43:27 +0000115 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000116 return 0;
117}
118
119static int interface_change_mtu(struct net_device *dev, int new_mtu)
120{
121 /* check ranges */
Sven Eckelmann95638772012-05-12 02:09:31 +0200122 if ((new_mtu < 68) || (new_mtu > batadv_hardif_min_mtu(dev)))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000123 return -EINVAL;
124
125 dev->mtu = new_mtu;
126
127 return 0;
128}
129
Sven Eckelmanndb69ecf2011-06-15 15:17:21 +0200130static int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000131{
132 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
133 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200134 struct hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000135 struct bcast_packet *bcast_packet;
136 struct vlan_ethhdr *vhdr;
Simon Wunderlichb1a8c042012-01-22 20:00:25 +0100137 static const uint8_t stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 0x00,
138 0x00};
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200139 unsigned int header_len = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000140 int data_len = skb->len, ret;
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100141 short vid __maybe_unused = -1;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200142 bool do_bcast = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000143
144 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
145 goto dropped;
146
147 soft_iface->trans_start = jiffies;
148
149 switch (ntohs(ethhdr->h_proto)) {
150 case ETH_P_8021Q:
151 vhdr = (struct vlan_ethhdr *)skb->data;
152 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
153
154 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
155 break;
156
157 /* fall through */
158 case ETH_P_BATMAN:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000159 goto dropped;
Simon Wunderlicha7f6ee92012-01-22 20:00:18 +0100160 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000161
Sven Eckelmann08adf152012-05-12 13:38:47 +0200162 if (batadv_bla_tx(bat_priv, skb, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100163 goto dropped;
164
Antonio Quartullia73105b2011-04-27 14:27:44 +0200165 /* Register the client MAC in the transtable */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200166 batadv_tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000167
Simon Wunderlichb1a8c042012-01-22 20:00:25 +0100168 /* don't accept stp packets. STP does not help in meshes.
169 * better use the bridge loop avoidance ...
170 */
171 if (compare_eth(ethhdr->h_dest, stp_addr))
172 goto dropped;
173
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200174 if (is_multicast_ether_addr(ethhdr->h_dest)) {
175 do_bcast = true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000176
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200177 switch (atomic_read(&bat_priv->gw_mode)) {
178 case GW_MODE_SERVER:
179 /* gateway servers should not send dhcp
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200180 * requests into the mesh
181 */
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200182 ret = batadv_gw_is_dhcp_target(skb, &header_len);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200183 if (ret)
184 goto dropped;
185 break;
186 case GW_MODE_CLIENT:
187 /* gateway clients should send dhcp requests
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200188 * via unicast to their gateway
189 */
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200190 ret = batadv_gw_is_dhcp_target(skb, &header_len);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200191 if (ret)
192 do_bcast = false;
193 break;
194 case GW_MODE_OFF:
195 default:
196 break;
197 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000198 }
199
200 /* ethernet packet should be broadcasted */
201 if (do_bcast) {
Marek Lindner32ae9b22011-04-20 15:40:58 +0200202 primary_if = primary_if_get_selected(bat_priv);
203 if (!primary_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000204 goto dropped;
205
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200206 if (batadv_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000207 goto dropped;
208
209 bcast_packet = (struct bcast_packet *)skb->data;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100210 bcast_packet->header.version = COMPAT_VERSION;
211 bcast_packet->header.ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000212
213 /* batman packet type: broadcast */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100214 bcast_packet->header.packet_type = BAT_BCAST;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000215
216 /* hw address of first interface is the orig mac because only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200217 * this mac is known throughout the mesh
218 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000219 memcpy(bcast_packet->orig,
Marek Lindner32ae9b22011-04-20 15:40:58 +0200220 primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000221
222 /* set broadcast sequence number */
223 bcast_packet->seqno =
224 htonl(atomic_inc_return(&bat_priv->bcast_seqno));
225
Sven Eckelmann9455e342012-05-12 02:09:37 +0200226 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000227
228 /* a copy is stored in the bcast list, therefore removing
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200229 * the original skb.
230 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000231 kfree_skb(skb);
232
233 /* unicast packet */
234 } else {
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200235 if (atomic_read(&bat_priv->gw_mode) != GW_MODE_OFF) {
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200236 ret = batadv_gw_out_of_range(bat_priv, skb, ethhdr);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200237 if (ret)
238 goto dropped;
239 }
240
Sven Eckelmann88ed1e772012-05-12 02:09:40 +0200241 ret = batadv_unicast_send_skb(skb, bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000242 if (ret != 0)
243 goto dropped_freed;
244 }
245
246 bat_priv->stats.tx_packets++;
247 bat_priv->stats.tx_bytes += data_len;
248 goto end;
249
250dropped:
251 kfree_skb(skb);
252dropped_freed:
253 bat_priv->stats.tx_dropped++;
254end:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200255 if (primary_if)
256 hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000257 return NETDEV_TX_OK;
258}
259
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200260void batadv_interface_rx(struct net_device *soft_iface,
261 struct sk_buff *skb, struct hard_iface *recv_if,
262 int hdr_size)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000263{
264 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000265 struct ethhdr *ethhdr;
266 struct vlan_ethhdr *vhdr;
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100267 short vid __maybe_unused = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268
269 /* check if enough space is available for pulling, and pull */
270 if (!pskb_may_pull(skb, hdr_size))
271 goto dropped;
272
273 skb_pull_rcsum(skb, hdr_size);
274 skb_reset_mac_header(skb);
275
276 ethhdr = (struct ethhdr *)skb_mac_header(skb);
277
278 switch (ntohs(ethhdr->h_proto)) {
279 case ETH_P_8021Q:
280 vhdr = (struct vlan_ethhdr *)skb->data;
281 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
282
283 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
284 break;
285
286 /* fall through */
287 case ETH_P_BATMAN:
288 goto dropped;
289 }
290
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000291 /* skb->dev & skb->pkt_type are set here */
292 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
293 goto dropped;
294 skb->protocol = eth_type_trans(skb, soft_iface);
295
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300296 /* should not be necessary anymore as we use skb_pull_rcsum()
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000297 * TODO: please verify this and remove this TODO
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200298 * -- Dec 21st 2009, Simon Wunderlich
299 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000300
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200301 /* skb->ip_summed = CHECKSUM_UNNECESSARY; */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000302
303 bat_priv->stats.rx_packets++;
Antonio Quartulli0d125072012-02-18 11:27:34 +0100304 bat_priv->stats.rx_bytes += skb->len + ETH_HLEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000305
306 soft_iface->last_rx = jiffies;
307
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200308 if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest))
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200309 goto dropped;
310
Simon Wunderlich23721382012-01-22 20:00:19 +0100311 /* Let the bridge loop avoidance check the packet. If will
312 * not handle it, we can safely push it up.
313 */
Sven Eckelmann08adf152012-05-12 13:38:47 +0200314 if (batadv_bla_rx(bat_priv, skb, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100315 goto out;
316
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000317 netif_rx(skb);
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200318 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000319
320dropped:
321 kfree_skb(skb);
322out:
323 return;
324}
325
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000326static const struct net_device_ops bat_netdev_ops = {
327 .ndo_open = interface_open,
328 .ndo_stop = interface_release,
329 .ndo_get_stats = interface_stats,
330 .ndo_set_mac_address = interface_set_mac_addr,
331 .ndo_change_mtu = interface_change_mtu,
332 .ndo_start_xmit = interface_tx,
333 .ndo_validate_addr = eth_validate_addr
334};
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000335
336static void interface_setup(struct net_device *dev)
337{
338 struct bat_priv *priv = netdev_priv(dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000339
340 ether_setup(dev);
341
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000342 dev->netdev_ops = &bat_netdev_ops;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000343 dev->destructor = free_netdev;
Andrew Lunnaf20b712011-04-17 20:39:07 +0200344 dev->tx_queue_len = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000345
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200346 /* can't call min_mtu, because the needed variables
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000347 * have not been initialized yet
348 */
349 dev->mtu = ETH_DATA_LEN;
Sven Eckelmann6e215fd2011-05-08 12:45:45 +0200350 /* reserve more space in the skbuff for our header */
351 dev->hard_header_len = BAT_HEADER_LEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000352
353 /* generate random address */
Danny Kukawka28009a62012-02-17 05:43:27 +0000354 eth_hw_addr_random(dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000355
356 SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
357
Sven Eckelmann704509b2011-05-14 23:14:54 +0200358 memset(priv, 0, sizeof(*priv));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000359}
360
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200361struct net_device *batadv_softif_create(const char *name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000362{
363 struct net_device *soft_iface;
364 struct bat_priv *bat_priv;
365 int ret;
366
Sven Eckelmann704509b2011-05-14 23:14:54 +0200367 soft_iface = alloc_netdev(sizeof(*bat_priv), name, interface_setup);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000368
Joe Perches320f4222011-08-29 14:17:24 -0700369 if (!soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000371
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200372 ret = register_netdevice(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000373 if (ret < 0) {
374 pr_err("Unable to register the batman interface '%s': %i\n",
375 name, ret);
376 goto free_soft_iface;
377 }
378
379 bat_priv = netdev_priv(soft_iface);
380
381 atomic_set(&bat_priv->aggregated_ogms, 1);
382 atomic_set(&bat_priv->bonding, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +0100383 atomic_set(&bat_priv->bridge_loop_avoidance, 0);
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200384 atomic_set(&bat_priv->ap_isolation, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000385 atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE);
386 atomic_set(&bat_priv->gw_mode, GW_MODE_OFF);
387 atomic_set(&bat_priv->gw_sel_class, 20);
388 atomic_set(&bat_priv->gw_bandwidth, 41);
389 atomic_set(&bat_priv->orig_interval, 1000);
Marek Lindner8681a1c2012-01-27 23:11:55 +0800390 atomic_set(&bat_priv->hop_penalty, 30);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000391 atomic_set(&bat_priv->log_level, 0);
392 atomic_set(&bat_priv->fragmentation, 1);
393 atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN);
394 atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN);
395
396 atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
397 atomic_set(&bat_priv->bcast_seqno, 1);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200398 atomic_set(&bat_priv->ttvn, 0);
399 atomic_set(&bat_priv->tt_local_changes, 0);
400 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +0100401 atomic_set(&bat_priv->bla_num_requests, 0);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200402
403 bat_priv->tt_buff = NULL;
404 bat_priv->tt_buff_len = 0;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200405 bat_priv->tt_poss_change = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000406
407 bat_priv->primary_if = NULL;
408 bat_priv->num_ifaces = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000409
Martin Hundebøllf8214862012-04-20 17:02:45 +0200410 bat_priv->bat_counters = __alloc_percpu(sizeof(uint64_t) * BAT_CNT_NUM,
411 __alignof__(uint64_t));
412 if (!bat_priv->bat_counters)
413 goto unreg_soft_iface;
414
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200415 ret = batadv_algo_select(bat_priv, batadv_routing_algo);
Marek Lindner1c280472011-11-28 17:40:17 +0800416 if (ret < 0)
Martin Hundebøllf8214862012-04-20 17:02:45 +0200417 goto free_bat_counters;
Marek Lindner1c280472011-11-28 17:40:17 +0800418
Sven Eckelmann5853e222012-05-12 02:09:24 +0200419 ret = batadv_sysfs_add_meshif(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000420 if (ret < 0)
Martin Hundebøllf8214862012-04-20 17:02:45 +0200421 goto free_bat_counters;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000422
Sven Eckelmann40a072d2012-05-12 02:09:23 +0200423 ret = batadv_debugfs_add_meshif(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000424 if (ret < 0)
425 goto unreg_sysfs;
426
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200427 ret = batadv_mesh_init(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428 if (ret < 0)
429 goto unreg_debugfs;
430
431 return soft_iface;
432
433unreg_debugfs:
Sven Eckelmann40a072d2012-05-12 02:09:23 +0200434 batadv_debugfs_del_meshif(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000435unreg_sysfs:
Sven Eckelmann5853e222012-05-12 02:09:24 +0200436 batadv_sysfs_del_meshif(soft_iface);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200437free_bat_counters:
438 free_percpu(bat_priv->bat_counters);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000439unreg_soft_iface:
Simon Wunderlich06ba7ce2011-11-07 13:57:48 +0100440 unregister_netdevice(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441 return NULL;
442
443free_soft_iface:
444 free_netdev(soft_iface);
445out:
446 return NULL;
447}
448
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200449void batadv_softif_destroy(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000450{
Sven Eckelmann40a072d2012-05-12 02:09:23 +0200451 batadv_debugfs_del_meshif(soft_iface);
Sven Eckelmann5853e222012-05-12 02:09:24 +0200452 batadv_sysfs_del_meshif(soft_iface);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200453 batadv_mesh_free(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000454 unregister_netdevice(soft_iface);
455}
456
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200457int batadv_softif_is_valid(const struct net_device *net_dev)
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000458{
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000459 if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
460 return 1;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000461
462 return 0;
463}
464
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000465/* ethtool */
466static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
467{
468 cmd->supported = 0;
469 cmd->advertising = 0;
David Decotigny70739492011-04-27 18:32:40 +0000470 ethtool_cmd_speed_set(cmd, SPEED_10);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471 cmd->duplex = DUPLEX_FULL;
472 cmd->port = PORT_TP;
473 cmd->phy_address = 0;
474 cmd->transceiver = XCVR_INTERNAL;
475 cmd->autoneg = AUTONEG_DISABLE;
476 cmd->maxtxpkt = 0;
477 cmd->maxrxpkt = 0;
478
479 return 0;
480}
481
482static void bat_get_drvinfo(struct net_device *dev,
483 struct ethtool_drvinfo *info)
484{
485 strcpy(info->driver, "B.A.T.M.A.N. advanced");
486 strcpy(info->version, SOURCE_VERSION);
487 strcpy(info->fw_version, "N/A");
488 strcpy(info->bus_info, "batman");
489}
490
491static u32 bat_get_msglevel(struct net_device *dev)
492{
493 return -EOPNOTSUPP;
494}
495
496static void bat_set_msglevel(struct net_device *dev, u32 value)
497{
498}
499
500static u32 bat_get_link(struct net_device *dev)
501{
502 return 1;
503}
Martin Hundebøllf8214862012-04-20 17:02:45 +0200504
505/* Inspired by drivers/net/ethernet/dlink/sundance.c:1702
506 * Declare each description string in struct.name[] to get fixed sized buffer
507 * and compile time checking for strings longer than ETH_GSTRING_LEN.
508 */
509static const struct {
510 const char name[ETH_GSTRING_LEN];
511} bat_counters_strings[] = {
512 { "forward" },
513 { "forward_bytes" },
514 { "mgmt_tx" },
515 { "mgmt_tx_bytes" },
516 { "mgmt_rx" },
517 { "mgmt_rx_bytes" },
518 { "tt_request_tx" },
519 { "tt_request_rx" },
520 { "tt_response_tx" },
521 { "tt_response_rx" },
522 { "tt_roam_adv_tx" },
523 { "tt_roam_adv_rx" },
524};
525
526static void batadv_get_strings(struct net_device *dev, uint32_t stringset,
527 uint8_t *data)
528{
529 if (stringset == ETH_SS_STATS)
530 memcpy(data, bat_counters_strings,
531 sizeof(bat_counters_strings));
532}
533
534static void batadv_get_ethtool_stats(struct net_device *dev,
535 struct ethtool_stats *stats,
536 uint64_t *data)
537{
538 struct bat_priv *bat_priv = netdev_priv(dev);
539 int i;
540
541 for (i = 0; i < BAT_CNT_NUM; i++)
542 data[i] = batadv_sum_counter(bat_priv, i);
543}
544
545static int batadv_get_sset_count(struct net_device *dev, int stringset)
546{
547 if (stringset == ETH_SS_STATS)
548 return BAT_CNT_NUM;
549
550 return -EOPNOTSUPP;
551}