blob: 2de1d742119f026cc5e915813a4c2390b59db951 [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 Quartulli43676ab52011-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
Sven Eckelmann0294ca02012-05-16 20:23:15 +020040static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
41static void batadv_get_drvinfo(struct net_device *dev,
42 struct ethtool_drvinfo *info);
43static u32 batadv_get_msglevel(struct net_device *dev);
44static void batadv_set_msglevel(struct net_device *dev, u32 value);
45static u32 batadv_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
Sven Eckelmann0294ca02012-05-16 20:23:15 +020051static const struct ethtool_ops batadv_ethtool_ops = {
52 .get_settings = batadv_get_settings,
53 .get_drvinfo = batadv_get_drvinfo,
54 .get_msglevel = batadv_get_msglevel,
55 .set_msglevel = batadv_set_msglevel,
56 .get_link = batadv_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 Eckelmann0294ca02012-05-16 20:23:15 +020081static int batadv_interface_open(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000082{
83 netif_start_queue(dev);
84 return 0;
85}
86
Sven Eckelmann0294ca02012-05-16 20:23:15 +020087static int batadv_interface_release(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000088{
89 netif_stop_queue(dev);
90 return 0;
91}
92
Sven Eckelmann0294ca02012-05-16 20:23:15 +020093static struct net_device_stats *batadv_interface_stats(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000094{
95 struct bat_priv *bat_priv = netdev_priv(dev);
96 return &bat_priv->stats;
97}
98
Sven Eckelmann0294ca02012-05-16 20:23:15 +020099static int batadv_interface_set_mac_addr(struct net_device *dev, void *p)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000100{
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
Sven Eckelmann0294ca02012-05-16 20:23:15 +0200119static int batadv_interface_change_mtu(struct net_device *dev, int new_mtu)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000120{
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 Eckelmann0294ca02012-05-16 20:23:15 +0200130static int batadv_interface_tx(struct sk_buff *skb,
131 struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000132{
133 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
134 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200135 struct hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000136 struct bcast_packet *bcast_packet;
137 struct vlan_ethhdr *vhdr;
Simon Wunderlichb1a8c042012-01-22 20:00:25 +0100138 static const uint8_t stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 0x00,
139 0x00};
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200140 unsigned int header_len = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000141 int data_len = skb->len, ret;
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100142 short vid __maybe_unused = -1;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200143 bool do_bcast = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000144
145 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
146 goto dropped;
147
148 soft_iface->trans_start = jiffies;
149
150 switch (ntohs(ethhdr->h_proto)) {
151 case ETH_P_8021Q:
152 vhdr = (struct vlan_ethhdr *)skb->data;
153 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
154
155 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
156 break;
157
158 /* fall through */
159 case ETH_P_BATMAN:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000160 goto dropped;
Simon Wunderlicha7f6ee92012-01-22 20:00:18 +0100161 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000162
Sven Eckelmann08adf152012-05-12 13:38:47 +0200163 if (batadv_bla_tx(bat_priv, skb, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100164 goto dropped;
165
Antonio Quartullia73105b2011-04-27 14:27:44 +0200166 /* Register the client MAC in the transtable */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200167 batadv_tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000168
Simon Wunderlichb1a8c042012-01-22 20:00:25 +0100169 /* don't accept stp packets. STP does not help in meshes.
170 * better use the bridge loop avoidance ...
171 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200172 if (batadv_compare_eth(ethhdr->h_dest, stp_addr))
Simon Wunderlichb1a8c042012-01-22 20:00:25 +0100173 goto dropped;
174
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200175 if (is_multicast_ether_addr(ethhdr->h_dest)) {
176 do_bcast = true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000177
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200178 switch (atomic_read(&bat_priv->gw_mode)) {
179 case GW_MODE_SERVER:
180 /* gateway servers should not send dhcp
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200181 * requests into the mesh
182 */
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200183 ret = batadv_gw_is_dhcp_target(skb, &header_len);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200184 if (ret)
185 goto dropped;
186 break;
187 case GW_MODE_CLIENT:
188 /* gateway clients should send dhcp requests
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200189 * via unicast to their gateway
190 */
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200191 ret = batadv_gw_is_dhcp_target(skb, &header_len);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200192 if (ret)
193 do_bcast = false;
194 break;
195 case GW_MODE_OFF:
196 default:
197 break;
198 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000199 }
200
201 /* ethernet packet should be broadcasted */
202 if (do_bcast) {
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200203 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200204 if (!primary_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000205 goto dropped;
206
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200207 if (batadv_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000208 goto dropped;
209
210 bcast_packet = (struct bcast_packet *)skb->data;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100211 bcast_packet->header.version = COMPAT_VERSION;
212 bcast_packet->header.ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000213
214 /* batman packet type: broadcast */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100215 bcast_packet->header.packet_type = BAT_BCAST;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000216
217 /* hw address of first interface is the orig mac because only
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200218 * this mac is known throughout the mesh
219 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000220 memcpy(bcast_packet->orig,
Marek Lindner32ae9b22011-04-20 15:40:58 +0200221 primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000222
223 /* set broadcast sequence number */
224 bcast_packet->seqno =
225 htonl(atomic_inc_return(&bat_priv->bcast_seqno));
226
Sven Eckelmann9455e342012-05-12 02:09:37 +0200227 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000228
229 /* a copy is stored in the bcast list, therefore removing
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200230 * the original skb.
231 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000232 kfree_skb(skb);
233
234 /* unicast packet */
235 } else {
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200236 if (atomic_read(&bat_priv->gw_mode) != GW_MODE_OFF) {
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200237 ret = batadv_gw_out_of_range(bat_priv, skb, ethhdr);
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200238 if (ret)
239 goto dropped;
240 }
241
Sven Eckelmann88ed1e772012-05-12 02:09:40 +0200242 ret = batadv_unicast_send_skb(skb, bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000243 if (ret != 0)
244 goto dropped_freed;
245 }
246
247 bat_priv->stats.tx_packets++;
248 bat_priv->stats.tx_bytes += data_len;
249 goto end;
250
251dropped:
252 kfree_skb(skb);
253dropped_freed:
254 bat_priv->stats.tx_dropped++;
255end:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200256 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200257 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000258 return NETDEV_TX_OK;
259}
260
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200261void batadv_interface_rx(struct net_device *soft_iface,
262 struct sk_buff *skb, struct hard_iface *recv_if,
263 int hdr_size)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000264{
265 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000266 struct ethhdr *ethhdr;
267 struct vlan_ethhdr *vhdr;
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100268 short vid __maybe_unused = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000269
270 /* check if enough space is available for pulling, and pull */
271 if (!pskb_may_pull(skb, hdr_size))
272 goto dropped;
273
274 skb_pull_rcsum(skb, hdr_size);
275 skb_reset_mac_header(skb);
276
277 ethhdr = (struct ethhdr *)skb_mac_header(skb);
278
279 switch (ntohs(ethhdr->h_proto)) {
280 case ETH_P_8021Q:
281 vhdr = (struct vlan_ethhdr *)skb->data;
282 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
283
284 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
285 break;
286
287 /* fall through */
288 case ETH_P_BATMAN:
289 goto dropped;
290 }
291
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292 /* skb->dev & skb->pkt_type are set here */
293 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
294 goto dropped;
295 skb->protocol = eth_type_trans(skb, soft_iface);
296
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300297 /* should not be necessary anymore as we use skb_pull_rcsum()
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000298 * TODO: please verify this and remove this TODO
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200299 * -- Dec 21st 2009, Simon Wunderlich
300 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000301
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200302 /* skb->ip_summed = CHECKSUM_UNNECESSARY; */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000303
304 bat_priv->stats.rx_packets++;
Antonio Quartulli0d125072012-02-18 11:27:34 +0100305 bat_priv->stats.rx_bytes += skb->len + ETH_HLEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000306
307 soft_iface->last_rx = jiffies;
308
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200309 if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest))
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200310 goto dropped;
311
Simon Wunderlich23721382012-01-22 20:00:19 +0100312 /* Let the bridge loop avoidance check the packet. If will
313 * not handle it, we can safely push it up.
314 */
Sven Eckelmann08adf152012-05-12 13:38:47 +0200315 if (batadv_bla_rx(bat_priv, skb, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100316 goto out;
317
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000318 netif_rx(skb);
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200319 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000320
321dropped:
322 kfree_skb(skb);
323out:
324 return;
325}
326
Sven Eckelmann0294ca02012-05-16 20:23:15 +0200327static const struct net_device_ops batadv_netdev_ops = {
328 .ndo_open = batadv_interface_open,
329 .ndo_stop = batadv_interface_release,
330 .ndo_get_stats = batadv_interface_stats,
331 .ndo_set_mac_address = batadv_interface_set_mac_addr,
332 .ndo_change_mtu = batadv_interface_change_mtu,
333 .ndo_start_xmit = batadv_interface_tx,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000334 .ndo_validate_addr = eth_validate_addr
335};
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000336
Sven Eckelmann0294ca02012-05-16 20:23:15 +0200337static void batadv_interface_setup(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000338{
339 struct bat_priv *priv = netdev_priv(dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000340
341 ether_setup(dev);
342
Sven Eckelmann0294ca02012-05-16 20:23:15 +0200343 dev->netdev_ops = &batadv_netdev_ops;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000344 dev->destructor = free_netdev;
Andrew Lunnaf20b712011-04-17 20:39:07 +0200345 dev->tx_queue_len = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000346
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200347 /* can't call min_mtu, because the needed variables
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000348 * have not been initialized yet
349 */
350 dev->mtu = ETH_DATA_LEN;
Sven Eckelmann6e215fd2011-05-08 12:45:45 +0200351 /* reserve more space in the skbuff for our header */
352 dev->hard_header_len = BAT_HEADER_LEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000353
354 /* generate random address */
Danny Kukawka28009a62012-02-17 05:43:27 +0000355 eth_hw_addr_random(dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000356
Sven Eckelmann0294ca02012-05-16 20:23:15 +0200357 SET_ETHTOOL_OPS(dev, &batadv_ethtool_ops);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000358
Sven Eckelmann704509b2011-05-14 23:14:54 +0200359 memset(priv, 0, sizeof(*priv));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000360}
361
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200362struct net_device *batadv_softif_create(const char *name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000363{
364 struct net_device *soft_iface;
365 struct bat_priv *bat_priv;
366 int ret;
367
Sven Eckelmann0294ca02012-05-16 20:23:15 +0200368 soft_iface = alloc_netdev(sizeof(*bat_priv), name,
369 batadv_interface_setup);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370
Joe Perches320f4222011-08-29 14:17:24 -0700371 if (!soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000372 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000373
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200374 ret = register_netdevice(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375 if (ret < 0) {
376 pr_err("Unable to register the batman interface '%s': %i\n",
377 name, ret);
378 goto free_soft_iface;
379 }
380
381 bat_priv = netdev_priv(soft_iface);
382
383 atomic_set(&bat_priv->aggregated_ogms, 1);
384 atomic_set(&bat_priv->bonding, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +0100385 atomic_set(&bat_priv->bridge_loop_avoidance, 0);
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200386 atomic_set(&bat_priv->ap_isolation, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000387 atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE);
388 atomic_set(&bat_priv->gw_mode, GW_MODE_OFF);
389 atomic_set(&bat_priv->gw_sel_class, 20);
390 atomic_set(&bat_priv->gw_bandwidth, 41);
391 atomic_set(&bat_priv->orig_interval, 1000);
Marek Lindner8681a1c2012-01-27 23:11:55 +0800392 atomic_set(&bat_priv->hop_penalty, 30);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000393 atomic_set(&bat_priv->log_level, 0);
394 atomic_set(&bat_priv->fragmentation, 1);
395 atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN);
396 atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN);
397
398 atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
399 atomic_set(&bat_priv->bcast_seqno, 1);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200400 atomic_set(&bat_priv->ttvn, 0);
401 atomic_set(&bat_priv->tt_local_changes, 0);
402 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +0100403 atomic_set(&bat_priv->bla_num_requests, 0);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200404
405 bat_priv->tt_buff = NULL;
406 bat_priv->tt_buff_len = 0;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200407 bat_priv->tt_poss_change = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000408
409 bat_priv->primary_if = NULL;
410 bat_priv->num_ifaces = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000411
Martin Hundebøllf8214862012-04-20 17:02:45 +0200412 bat_priv->bat_counters = __alloc_percpu(sizeof(uint64_t) * BAT_CNT_NUM,
413 __alignof__(uint64_t));
414 if (!bat_priv->bat_counters)
415 goto unreg_soft_iface;
416
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200417 ret = batadv_algo_select(bat_priv, batadv_routing_algo);
Marek Lindner1c280472011-11-28 17:40:17 +0800418 if (ret < 0)
Martin Hundebøllf8214862012-04-20 17:02:45 +0200419 goto free_bat_counters;
Marek Lindner1c280472011-11-28 17:40:17 +0800420
Sven Eckelmann5853e222012-05-12 02:09:24 +0200421 ret = batadv_sysfs_add_meshif(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000422 if (ret < 0)
Martin Hundebøllf8214862012-04-20 17:02:45 +0200423 goto free_bat_counters;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000424
Sven Eckelmann40a072d2012-05-12 02:09:23 +0200425 ret = batadv_debugfs_add_meshif(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000426 if (ret < 0)
427 goto unreg_sysfs;
428
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200429 ret = batadv_mesh_init(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000430 if (ret < 0)
431 goto unreg_debugfs;
432
433 return soft_iface;
434
435unreg_debugfs:
Sven Eckelmann40a072d2012-05-12 02:09:23 +0200436 batadv_debugfs_del_meshif(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437unreg_sysfs:
Sven Eckelmann5853e222012-05-12 02:09:24 +0200438 batadv_sysfs_del_meshif(soft_iface);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200439free_bat_counters:
440 free_percpu(bat_priv->bat_counters);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441unreg_soft_iface:
Simon Wunderlich06ba7ce2011-11-07 13:57:48 +0100442 unregister_netdevice(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000443 return NULL;
444
445free_soft_iface:
446 free_netdev(soft_iface);
447out:
448 return NULL;
449}
450
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200451void batadv_softif_destroy(struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000452{
Sven Eckelmann40a072d2012-05-12 02:09:23 +0200453 batadv_debugfs_del_meshif(soft_iface);
Sven Eckelmann5853e222012-05-12 02:09:24 +0200454 batadv_sysfs_del_meshif(soft_iface);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200455 batadv_mesh_free(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000456 unregister_netdevice(soft_iface);
457}
458
Sven Eckelmann04b482a2012-05-12 02:09:38 +0200459int batadv_softif_is_valid(const struct net_device *net_dev)
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000460{
Sven Eckelmann0294ca02012-05-16 20:23:15 +0200461 if (net_dev->netdev_ops->ndo_start_xmit == batadv_interface_tx)
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000462 return 1;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000463
464 return 0;
465}
466
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000467/* ethtool */
Sven Eckelmann0294ca02012-05-16 20:23:15 +0200468static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000469{
470 cmd->supported = 0;
471 cmd->advertising = 0;
David Decotigny70739492011-04-27 18:32:40 +0000472 ethtool_cmd_speed_set(cmd, SPEED_10);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000473 cmd->duplex = DUPLEX_FULL;
474 cmd->port = PORT_TP;
475 cmd->phy_address = 0;
476 cmd->transceiver = XCVR_INTERNAL;
477 cmd->autoneg = AUTONEG_DISABLE;
478 cmd->maxtxpkt = 0;
479 cmd->maxrxpkt = 0;
480
481 return 0;
482}
483
Sven Eckelmann0294ca02012-05-16 20:23:15 +0200484static void batadv_get_drvinfo(struct net_device *dev,
485 struct ethtool_drvinfo *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000486{
487 strcpy(info->driver, "B.A.T.M.A.N. advanced");
488 strcpy(info->version, SOURCE_VERSION);
489 strcpy(info->fw_version, "N/A");
490 strcpy(info->bus_info, "batman");
491}
492
Sven Eckelmann0294ca02012-05-16 20:23:15 +0200493static u32 batadv_get_msglevel(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000494{
495 return -EOPNOTSUPP;
496}
497
Sven Eckelmann0294ca02012-05-16 20:23:15 +0200498static void batadv_set_msglevel(struct net_device *dev, u32 value)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000499{
500}
501
Sven Eckelmann0294ca02012-05-16 20:23:15 +0200502static u32 batadv_get_link(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000503{
504 return 1;
505}
Martin Hundebøllf8214862012-04-20 17:02:45 +0200506
507/* Inspired by drivers/net/ethernet/dlink/sundance.c:1702
508 * Declare each description string in struct.name[] to get fixed sized buffer
509 * and compile time checking for strings longer than ETH_GSTRING_LEN.
510 */
511static const struct {
512 const char name[ETH_GSTRING_LEN];
Sven Eckelmann0294ca02012-05-16 20:23:15 +0200513} batadv_counters_strings[] = {
Martin Hundebøllf8214862012-04-20 17:02:45 +0200514 { "forward" },
515 { "forward_bytes" },
516 { "mgmt_tx" },
517 { "mgmt_tx_bytes" },
518 { "mgmt_rx" },
519 { "mgmt_rx_bytes" },
520 { "tt_request_tx" },
521 { "tt_request_rx" },
522 { "tt_response_tx" },
523 { "tt_response_rx" },
524 { "tt_roam_adv_tx" },
525 { "tt_roam_adv_rx" },
526};
527
528static void batadv_get_strings(struct net_device *dev, uint32_t stringset,
529 uint8_t *data)
530{
531 if (stringset == ETH_SS_STATS)
Sven Eckelmann0294ca02012-05-16 20:23:15 +0200532 memcpy(data, batadv_counters_strings,
533 sizeof(batadv_counters_strings));
Martin Hundebøllf8214862012-04-20 17:02:45 +0200534}
535
536static void batadv_get_ethtool_stats(struct net_device *dev,
537 struct ethtool_stats *stats,
538 uint64_t *data)
539{
540 struct bat_priv *bat_priv = netdev_priv(dev);
541 int i;
542
543 for (i = 0; i < BAT_CNT_NUM; i++)
544 data[i] = batadv_sum_counter(bat_priv, i);
545}
546
547static int batadv_get_sset_count(struct net_device *dev, int stringset)
548{
549 if (stringset == ETH_SS_STATS)
550 return BAT_CNT_NUM;
551
552 return -EOPNOTSUPP;
553}