]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - net/batman-adv/soft-interface.c
batman-adv: Add get_ethtool_stats() support
[mirror_ubuntu-zesty-kernel.git] / net / batman-adv / soft-interface.c
1 /*
2 * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
3 *
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 "soft-interface.h"
24 #include "hard-interface.h"
25 #include "routing.h"
26 #include "send.h"
27 #include "bat_debugfs.h"
28 #include "translation-table.h"
29 #include "hash.h"
30 #include "gateway_common.h"
31 #include "gateway_client.h"
32 #include "bat_sysfs.h"
33 #include "originator.h"
34 #include <linux/slab.h>
35 #include <linux/ethtool.h>
36 #include <linux/etherdevice.h>
37 #include <linux/if_vlan.h>
38 #include "unicast.h"
39 #include "bridge_loop_avoidance.h"
40
41
42 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
43 static void bat_get_drvinfo(struct net_device *dev,
44 struct ethtool_drvinfo *info);
45 static u32 bat_get_msglevel(struct net_device *dev);
46 static void bat_set_msglevel(struct net_device *dev, u32 value);
47 static u32 bat_get_link(struct net_device *dev);
48 static void batadv_get_strings(struct net_device *dev, u32 stringset, u8 *data);
49 static void batadv_get_ethtool_stats(struct net_device *dev,
50 struct ethtool_stats *stats, u64 *data);
51 static int batadv_get_sset_count(struct net_device *dev, int stringset);
52
53 static const struct ethtool_ops bat_ethtool_ops = {
54 .get_settings = bat_get_settings,
55 .get_drvinfo = bat_get_drvinfo,
56 .get_msglevel = bat_get_msglevel,
57 .set_msglevel = bat_set_msglevel,
58 .get_link = bat_get_link,
59 .get_strings = batadv_get_strings,
60 .get_ethtool_stats = batadv_get_ethtool_stats,
61 .get_sset_count = batadv_get_sset_count,
62 };
63
64 int my_skb_head_push(struct sk_buff *skb, unsigned int len)
65 {
66 int result;
67
68 /**
69 * TODO: We must check if we can release all references to non-payload
70 * data using skb_header_release in our skbs to allow skb_cow_header to
71 * work optimally. This means that those skbs are not allowed to read
72 * or write any data which is before the current position of skb->data
73 * after that call and thus allow other skbs with the same data buffer
74 * to write freely in that area.
75 */
76 result = skb_cow_head(skb, len);
77 if (result < 0)
78 return result;
79
80 skb_push(skb, len);
81 return 0;
82 }
83
84 static int interface_open(struct net_device *dev)
85 {
86 netif_start_queue(dev);
87 return 0;
88 }
89
90 static int interface_release(struct net_device *dev)
91 {
92 netif_stop_queue(dev);
93 return 0;
94 }
95
96 static struct net_device_stats *interface_stats(struct net_device *dev)
97 {
98 struct bat_priv *bat_priv = netdev_priv(dev);
99 return &bat_priv->stats;
100 }
101
102 static int interface_set_mac_addr(struct net_device *dev, void *p)
103 {
104 struct bat_priv *bat_priv = netdev_priv(dev);
105 struct sockaddr *addr = p;
106
107 if (!is_valid_ether_addr(addr->sa_data))
108 return -EADDRNOTAVAIL;
109
110 /* only modify transtable if it has been initialized before */
111 if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) {
112 tt_local_remove(bat_priv, dev->dev_addr,
113 "mac address changed", false);
114 tt_local_add(dev, addr->sa_data, NULL_IFINDEX);
115 }
116
117 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
118 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
119 return 0;
120 }
121
122 static int interface_change_mtu(struct net_device *dev, int new_mtu)
123 {
124 /* check ranges */
125 if ((new_mtu < 68) || (new_mtu > hardif_min_mtu(dev)))
126 return -EINVAL;
127
128 dev->mtu = new_mtu;
129
130 return 0;
131 }
132
133 static int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
134 {
135 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
136 struct bat_priv *bat_priv = netdev_priv(soft_iface);
137 struct hard_iface *primary_if = NULL;
138 struct bcast_packet *bcast_packet;
139 struct vlan_ethhdr *vhdr;
140 static const uint8_t stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 0x00,
141 0x00};
142 unsigned int header_len = 0;
143 int data_len = skb->len, ret;
144 short vid __maybe_unused = -1;
145 bool do_bcast = false;
146
147 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
148 goto dropped;
149
150 soft_iface->trans_start = jiffies;
151
152 switch (ntohs(ethhdr->h_proto)) {
153 case ETH_P_8021Q:
154 vhdr = (struct vlan_ethhdr *)skb->data;
155 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
156
157 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
158 break;
159
160 /* fall through */
161 case ETH_P_BATMAN:
162 goto dropped;
163 }
164
165 if (bla_tx(bat_priv, skb, vid))
166 goto dropped;
167
168 /* Register the client MAC in the transtable */
169 tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif);
170
171 /* don't accept stp packets. STP does not help in meshes.
172 * better use the bridge loop avoidance ...
173 */
174 if (compare_eth(ethhdr->h_dest, stp_addr))
175 goto dropped;
176
177 if (is_multicast_ether_addr(ethhdr->h_dest)) {
178 do_bcast = true;
179
180 switch (atomic_read(&bat_priv->gw_mode)) {
181 case GW_MODE_SERVER:
182 /* gateway servers should not send dhcp
183 * requests into the mesh */
184 ret = gw_is_dhcp_target(skb, &header_len);
185 if (ret)
186 goto dropped;
187 break;
188 case GW_MODE_CLIENT:
189 /* gateway clients should send dhcp requests
190 * via unicast to their gateway */
191 ret = gw_is_dhcp_target(skb, &header_len);
192 if (ret)
193 do_bcast = false;
194 break;
195 case GW_MODE_OFF:
196 default:
197 break;
198 }
199 }
200
201 /* ethernet packet should be broadcasted */
202 if (do_bcast) {
203 primary_if = primary_if_get_selected(bat_priv);
204 if (!primary_if)
205 goto dropped;
206
207 if (my_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
208 goto dropped;
209
210 bcast_packet = (struct bcast_packet *)skb->data;
211 bcast_packet->header.version = COMPAT_VERSION;
212 bcast_packet->header.ttl = TTL;
213
214 /* batman packet type: broadcast */
215 bcast_packet->header.packet_type = BAT_BCAST;
216
217 /* hw address of first interface is the orig mac because only
218 * this mac is known throughout the mesh */
219 memcpy(bcast_packet->orig,
220 primary_if->net_dev->dev_addr, ETH_ALEN);
221
222 /* set broadcast sequence number */
223 bcast_packet->seqno =
224 htonl(atomic_inc_return(&bat_priv->bcast_seqno));
225
226 add_bcast_packet_to_list(bat_priv, skb, 1);
227
228 /* a copy is stored in the bcast list, therefore removing
229 * the original skb. */
230 kfree_skb(skb);
231
232 /* unicast packet */
233 } else {
234 if (atomic_read(&bat_priv->gw_mode) != GW_MODE_OFF) {
235 ret = gw_out_of_range(bat_priv, skb, ethhdr);
236 if (ret)
237 goto dropped;
238 }
239
240 ret = unicast_send_skb(skb, bat_priv);
241 if (ret != 0)
242 goto dropped_freed;
243 }
244
245 bat_priv->stats.tx_packets++;
246 bat_priv->stats.tx_bytes += data_len;
247 goto end;
248
249 dropped:
250 kfree_skb(skb);
251 dropped_freed:
252 bat_priv->stats.tx_dropped++;
253 end:
254 if (primary_if)
255 hardif_free_ref(primary_if);
256 return NETDEV_TX_OK;
257 }
258
259 void interface_rx(struct net_device *soft_iface,
260 struct sk_buff *skb, struct hard_iface *recv_if,
261 int hdr_size)
262 {
263 struct bat_priv *bat_priv = netdev_priv(soft_iface);
264 struct ethhdr *ethhdr;
265 struct vlan_ethhdr *vhdr;
266 short vid __maybe_unused = -1;
267
268 /* check if enough space is available for pulling, and pull */
269 if (!pskb_may_pull(skb, hdr_size))
270 goto dropped;
271
272 skb_pull_rcsum(skb, hdr_size);
273 skb_reset_mac_header(skb);
274
275 ethhdr = (struct ethhdr *)skb_mac_header(skb);
276
277 switch (ntohs(ethhdr->h_proto)) {
278 case ETH_P_8021Q:
279 vhdr = (struct vlan_ethhdr *)skb->data;
280 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
281
282 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
283 break;
284
285 /* fall through */
286 case ETH_P_BATMAN:
287 goto dropped;
288 }
289
290 /* skb->dev & skb->pkt_type are set here */
291 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
292 goto dropped;
293 skb->protocol = eth_type_trans(skb, soft_iface);
294
295 /* should not be necessary anymore as we use skb_pull_rcsum()
296 * TODO: please verify this and remove this TODO
297 * -- Dec 21st 2009, Simon Wunderlich */
298
299 /* skb->ip_summed = CHECKSUM_UNNECESSARY;*/
300
301 bat_priv->stats.rx_packets++;
302 bat_priv->stats.rx_bytes += skb->len + ETH_HLEN;
303
304 soft_iface->last_rx = jiffies;
305
306 if (is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest))
307 goto dropped;
308
309 /* Let the bridge loop avoidance check the packet. If will
310 * not handle it, we can safely push it up.
311 */
312 if (bla_rx(bat_priv, skb, vid))
313 goto out;
314
315 netif_rx(skb);
316 goto out;
317
318 dropped:
319 kfree_skb(skb);
320 out:
321 return;
322 }
323
324 static const struct net_device_ops bat_netdev_ops = {
325 .ndo_open = interface_open,
326 .ndo_stop = interface_release,
327 .ndo_get_stats = interface_stats,
328 .ndo_set_mac_address = interface_set_mac_addr,
329 .ndo_change_mtu = interface_change_mtu,
330 .ndo_start_xmit = interface_tx,
331 .ndo_validate_addr = eth_validate_addr
332 };
333
334 static void interface_setup(struct net_device *dev)
335 {
336 struct bat_priv *priv = netdev_priv(dev);
337
338 ether_setup(dev);
339
340 dev->netdev_ops = &bat_netdev_ops;
341 dev->destructor = free_netdev;
342 dev->tx_queue_len = 0;
343
344 /**
345 * can't call min_mtu, because the needed variables
346 * have not been initialized yet
347 */
348 dev->mtu = ETH_DATA_LEN;
349 /* reserve more space in the skbuff for our header */
350 dev->hard_header_len = BAT_HEADER_LEN;
351
352 /* generate random address */
353 eth_hw_addr_random(dev);
354
355 SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
356
357 memset(priv, 0, sizeof(*priv));
358 }
359
360 struct net_device *softif_create(const char *name)
361 {
362 struct net_device *soft_iface;
363 struct bat_priv *bat_priv;
364 int ret;
365
366 soft_iface = alloc_netdev(sizeof(*bat_priv), name, interface_setup);
367
368 if (!soft_iface)
369 goto out;
370
371 ret = register_netdevice(soft_iface);
372 if (ret < 0) {
373 pr_err("Unable to register the batman interface '%s': %i\n",
374 name, ret);
375 goto free_soft_iface;
376 }
377
378 bat_priv = netdev_priv(soft_iface);
379
380 atomic_set(&bat_priv->aggregated_ogms, 1);
381 atomic_set(&bat_priv->bonding, 0);
382 atomic_set(&bat_priv->bridge_loop_avoidance, 0);
383 atomic_set(&bat_priv->ap_isolation, 0);
384 atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE);
385 atomic_set(&bat_priv->gw_mode, GW_MODE_OFF);
386 atomic_set(&bat_priv->gw_sel_class, 20);
387 atomic_set(&bat_priv->gw_bandwidth, 41);
388 atomic_set(&bat_priv->orig_interval, 1000);
389 atomic_set(&bat_priv->hop_penalty, 30);
390 atomic_set(&bat_priv->log_level, 0);
391 atomic_set(&bat_priv->fragmentation, 1);
392 atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN);
393 atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN);
394
395 atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
396 atomic_set(&bat_priv->bcast_seqno, 1);
397 atomic_set(&bat_priv->ttvn, 0);
398 atomic_set(&bat_priv->tt_local_changes, 0);
399 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
400 atomic_set(&bat_priv->bla_num_requests, 0);
401
402 bat_priv->tt_buff = NULL;
403 bat_priv->tt_buff_len = 0;
404 bat_priv->tt_poss_change = false;
405
406 bat_priv->primary_if = NULL;
407 bat_priv->num_ifaces = 0;
408
409 bat_priv->bat_counters = __alloc_percpu(sizeof(uint64_t) * BAT_CNT_NUM,
410 __alignof__(uint64_t));
411 if (!bat_priv->bat_counters)
412 goto unreg_soft_iface;
413
414 ret = bat_algo_select(bat_priv, bat_routing_algo);
415 if (ret < 0)
416 goto free_bat_counters;
417
418 ret = sysfs_add_meshif(soft_iface);
419 if (ret < 0)
420 goto free_bat_counters;
421
422 ret = debugfs_add_meshif(soft_iface);
423 if (ret < 0)
424 goto unreg_sysfs;
425
426 ret = mesh_init(soft_iface);
427 if (ret < 0)
428 goto unreg_debugfs;
429
430 return soft_iface;
431
432 unreg_debugfs:
433 debugfs_del_meshif(soft_iface);
434 unreg_sysfs:
435 sysfs_del_meshif(soft_iface);
436 free_bat_counters:
437 free_percpu(bat_priv->bat_counters);
438 unreg_soft_iface:
439 unregister_netdevice(soft_iface);
440 return NULL;
441
442 free_soft_iface:
443 free_netdev(soft_iface);
444 out:
445 return NULL;
446 }
447
448 void softif_destroy(struct net_device *soft_iface)
449 {
450 debugfs_del_meshif(soft_iface);
451 sysfs_del_meshif(soft_iface);
452 mesh_free(soft_iface);
453 unregister_netdevice(soft_iface);
454 }
455
456 int softif_is_valid(const struct net_device *net_dev)
457 {
458 if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
459 return 1;
460
461 return 0;
462 }
463
464 /* ethtool */
465 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
466 {
467 cmd->supported = 0;
468 cmd->advertising = 0;
469 ethtool_cmd_speed_set(cmd, SPEED_10);
470 cmd->duplex = DUPLEX_FULL;
471 cmd->port = PORT_TP;
472 cmd->phy_address = 0;
473 cmd->transceiver = XCVR_INTERNAL;
474 cmd->autoneg = AUTONEG_DISABLE;
475 cmd->maxtxpkt = 0;
476 cmd->maxrxpkt = 0;
477
478 return 0;
479 }
480
481 static void bat_get_drvinfo(struct net_device *dev,
482 struct ethtool_drvinfo *info)
483 {
484 strcpy(info->driver, "B.A.T.M.A.N. advanced");
485 strcpy(info->version, SOURCE_VERSION);
486 strcpy(info->fw_version, "N/A");
487 strcpy(info->bus_info, "batman");
488 }
489
490 static u32 bat_get_msglevel(struct net_device *dev)
491 {
492 return -EOPNOTSUPP;
493 }
494
495 static void bat_set_msglevel(struct net_device *dev, u32 value)
496 {
497 }
498
499 static u32 bat_get_link(struct net_device *dev)
500 {
501 return 1;
502 }
503
504 /* Inspired by drivers/net/ethernet/dlink/sundance.c:1702
505 * Declare each description string in struct.name[] to get fixed sized buffer
506 * and compile time checking for strings longer than ETH_GSTRING_LEN.
507 */
508 static const struct {
509 const char name[ETH_GSTRING_LEN];
510 } bat_counters_strings[] = {
511 { "forward" },
512 { "forward_bytes" },
513 { "mgmt_tx" },
514 { "mgmt_tx_bytes" },
515 { "mgmt_rx" },
516 { "mgmt_rx_bytes" },
517 { "tt_request_tx" },
518 { "tt_request_rx" },
519 { "tt_response_tx" },
520 { "tt_response_rx" },
521 { "tt_roam_adv_tx" },
522 { "tt_roam_adv_rx" },
523 };
524
525 static void batadv_get_strings(struct net_device *dev, uint32_t stringset,
526 uint8_t *data)
527 {
528 if (stringset == ETH_SS_STATS)
529 memcpy(data, bat_counters_strings,
530 sizeof(bat_counters_strings));
531 }
532
533 static void batadv_get_ethtool_stats(struct net_device *dev,
534 struct ethtool_stats *stats,
535 uint64_t *data)
536 {
537 struct bat_priv *bat_priv = netdev_priv(dev);
538 int i;
539
540 for (i = 0; i < BAT_CNT_NUM; i++)
541 data[i] = batadv_sum_counter(bat_priv, i);
542 }
543
544 static int batadv_get_sset_count(struct net_device *dev, int stringset)
545 {
546 if (stringset == ETH_SS_STATS)
547 return BAT_CNT_NUM;
548
549 return -EOPNOTSUPP;
550 }