1 /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich
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.
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.
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
21 #include "soft-interface.h"
22 #include "hard-interface.h"
25 #include "bat_debugfs.h"
26 #include "translation-table.h"
28 #include "gateway_common.h"
29 #include "gateway_client.h"
30 #include "bat_sysfs.h"
31 #include "originator.h"
32 #include <linux/slab.h>
33 #include <linux/ethtool.h>
34 #include <linux/etherdevice.h>
35 #include <linux/if_vlan.h>
37 #include "bridge_loop_avoidance.h"
40 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
41 static void bat_get_drvinfo(struct net_device *dev,
42 struct ethtool_drvinfo *info);
43 static u32 bat_get_msglevel(struct net_device *dev);
44 static void bat_set_msglevel(struct net_device *dev, u32 value);
45 static u32 bat_get_link(struct net_device *dev);
46 static void batadv_get_strings(struct net_device *dev, u32 stringset, u8 *data);
47 static void batadv_get_ethtool_stats(struct net_device *dev,
48 struct ethtool_stats *stats, u64 *data);
49 static int batadv_get_sset_count(struct net_device *dev, int stringset);
51 static 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,
57 .get_strings = batadv_get_strings,
58 .get_ethtool_stats = batadv_get_ethtool_stats,
59 .get_sset_count = batadv_get_sset_count,
62 int batadv_skb_head_push(struct sk_buff *skb, unsigned int len)
66 /* TODO: We must check if we can release all references to non-payload
67 * 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.
73 result = skb_cow_head(skb, len);
81 static int interface_open(struct net_device *dev)
83 netif_start_queue(dev);
87 static int interface_release(struct net_device *dev)
89 netif_stop_queue(dev);
93 static struct net_device_stats *interface_stats(struct net_device *dev)
95 struct bat_priv *bat_priv = netdev_priv(dev);
96 return &bat_priv->stats;
99 static int interface_set_mac_addr(struct net_device *dev, void *p)
101 struct bat_priv *bat_priv = netdev_priv(dev);
102 struct sockaddr *addr = p;
104 if (!is_valid_ether_addr(addr->sa_data))
105 return -EADDRNOTAVAIL;
107 /* only modify transtable if it has been initialized before */
108 if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) {
109 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);
114 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
115 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
119 static int interface_change_mtu(struct net_device *dev, int new_mtu)
122 if ((new_mtu < 68) || (new_mtu > batadv_hardif_min_mtu(dev)))
130 static int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
132 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
133 struct bat_priv *bat_priv = netdev_priv(soft_iface);
134 struct hard_iface *primary_if = NULL;
135 struct bcast_packet *bcast_packet;
136 struct vlan_ethhdr *vhdr;
137 static const uint8_t stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 0x00,
139 unsigned int header_len = 0;
140 int data_len = skb->len, ret;
141 short vid __maybe_unused = -1;
142 bool do_bcast = false;
144 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
147 soft_iface->trans_start = jiffies;
149 switch (ntohs(ethhdr->h_proto)) {
151 vhdr = (struct vlan_ethhdr *)skb->data;
152 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
154 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
162 if (batadv_bla_tx(bat_priv, skb, vid))
165 /* Register the client MAC in the transtable */
166 batadv_tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif);
168 /* don't accept stp packets. STP does not help in meshes.
169 * better use the bridge loop avoidance ...
171 if (compare_eth(ethhdr->h_dest, stp_addr))
174 if (is_multicast_ether_addr(ethhdr->h_dest)) {
177 switch (atomic_read(&bat_priv->gw_mode)) {
179 /* gateway servers should not send dhcp
180 * requests into the mesh
182 ret = batadv_gw_is_dhcp_target(skb, &header_len);
187 /* gateway clients should send dhcp requests
188 * via unicast to their gateway
190 ret = batadv_gw_is_dhcp_target(skb, &header_len);
200 /* ethernet packet should be broadcasted */
202 primary_if = primary_if_get_selected(bat_priv);
206 if (batadv_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
209 bcast_packet = (struct bcast_packet *)skb->data;
210 bcast_packet->header.version = COMPAT_VERSION;
211 bcast_packet->header.ttl = TTL;
213 /* batman packet type: broadcast */
214 bcast_packet->header.packet_type = BAT_BCAST;
216 /* hw address of first interface is the orig mac because only
217 * this mac is known throughout the mesh
219 memcpy(bcast_packet->orig,
220 primary_if->net_dev->dev_addr, ETH_ALEN);
222 /* set broadcast sequence number */
223 bcast_packet->seqno =
224 htonl(atomic_inc_return(&bat_priv->bcast_seqno));
226 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
228 /* a copy is stored in the bcast list, therefore removing
235 if (atomic_read(&bat_priv->gw_mode) != GW_MODE_OFF) {
236 ret = batadv_gw_out_of_range(bat_priv, skb, ethhdr);
241 ret = batadv_unicast_send_skb(skb, bat_priv);
246 bat_priv->stats.tx_packets++;
247 bat_priv->stats.tx_bytes += data_len;
253 bat_priv->stats.tx_dropped++;
256 hardif_free_ref(primary_if);
260 void batadv_interface_rx(struct net_device *soft_iface,
261 struct sk_buff *skb, struct hard_iface *recv_if,
264 struct bat_priv *bat_priv = netdev_priv(soft_iface);
265 struct ethhdr *ethhdr;
266 struct vlan_ethhdr *vhdr;
267 short vid __maybe_unused = -1;
269 /* check if enough space is available for pulling, and pull */
270 if (!pskb_may_pull(skb, hdr_size))
273 skb_pull_rcsum(skb, hdr_size);
274 skb_reset_mac_header(skb);
276 ethhdr = (struct ethhdr *)skb_mac_header(skb);
278 switch (ntohs(ethhdr->h_proto)) {
280 vhdr = (struct vlan_ethhdr *)skb->data;
281 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
283 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
291 /* skb->dev & skb->pkt_type are set here */
292 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
294 skb->protocol = eth_type_trans(skb, soft_iface);
296 /* should not be necessary anymore as we use skb_pull_rcsum()
297 * TODO: please verify this and remove this TODO
298 * -- Dec 21st 2009, Simon Wunderlich
301 /* skb->ip_summed = CHECKSUM_UNNECESSARY; */
303 bat_priv->stats.rx_packets++;
304 bat_priv->stats.rx_bytes += skb->len + ETH_HLEN;
306 soft_iface->last_rx = jiffies;
308 if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest))
311 /* Let the bridge loop avoidance check the packet. If will
312 * not handle it, we can safely push it up.
314 if (batadv_bla_rx(bat_priv, skb, vid))
326 static 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
336 static void interface_setup(struct net_device *dev)
338 struct bat_priv *priv = netdev_priv(dev);
342 dev->netdev_ops = &bat_netdev_ops;
343 dev->destructor = free_netdev;
344 dev->tx_queue_len = 0;
346 /* can't call min_mtu, because the needed variables
347 * have not been initialized yet
349 dev->mtu = ETH_DATA_LEN;
350 /* reserve more space in the skbuff for our header */
351 dev->hard_header_len = BAT_HEADER_LEN;
353 /* generate random address */
354 eth_hw_addr_random(dev);
356 SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
358 memset(priv, 0, sizeof(*priv));
361 struct net_device *batadv_softif_create(const char *name)
363 struct net_device *soft_iface;
364 struct bat_priv *bat_priv;
367 soft_iface = alloc_netdev(sizeof(*bat_priv), name, interface_setup);
372 ret = register_netdevice(soft_iface);
374 pr_err("Unable to register the batman interface '%s': %i\n",
376 goto free_soft_iface;
379 bat_priv = netdev_priv(soft_iface);
381 atomic_set(&bat_priv->aggregated_ogms, 1);
382 atomic_set(&bat_priv->bonding, 0);
383 atomic_set(&bat_priv->bridge_loop_avoidance, 0);
384 atomic_set(&bat_priv->ap_isolation, 0);
385 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);
390 atomic_set(&bat_priv->hop_penalty, 30);
391 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);
396 atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
397 atomic_set(&bat_priv->bcast_seqno, 1);
398 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);
401 atomic_set(&bat_priv->bla_num_requests, 0);
403 bat_priv->tt_buff = NULL;
404 bat_priv->tt_buff_len = 0;
405 bat_priv->tt_poss_change = false;
407 bat_priv->primary_if = NULL;
408 bat_priv->num_ifaces = 0;
410 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;
415 ret = batadv_algo_select(bat_priv, batadv_routing_algo);
417 goto free_bat_counters;
419 ret = batadv_sysfs_add_meshif(soft_iface);
421 goto free_bat_counters;
423 ret = batadv_debugfs_add_meshif(soft_iface);
427 ret = batadv_mesh_init(soft_iface);
434 batadv_debugfs_del_meshif(soft_iface);
436 batadv_sysfs_del_meshif(soft_iface);
438 free_percpu(bat_priv->bat_counters);
440 unregister_netdevice(soft_iface);
444 free_netdev(soft_iface);
449 void batadv_softif_destroy(struct net_device *soft_iface)
451 batadv_debugfs_del_meshif(soft_iface);
452 batadv_sysfs_del_meshif(soft_iface);
453 batadv_mesh_free(soft_iface);
454 unregister_netdevice(soft_iface);
457 int batadv_softif_is_valid(const struct net_device *net_dev)
459 if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
466 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
469 cmd->advertising = 0;
470 ethtool_cmd_speed_set(cmd, SPEED_10);
471 cmd->duplex = DUPLEX_FULL;
473 cmd->phy_address = 0;
474 cmd->transceiver = XCVR_INTERNAL;
475 cmd->autoneg = AUTONEG_DISABLE;
482 static void bat_get_drvinfo(struct net_device *dev,
483 struct ethtool_drvinfo *info)
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");
491 static u32 bat_get_msglevel(struct net_device *dev)
496 static void bat_set_msglevel(struct net_device *dev, u32 value)
500 static u32 bat_get_link(struct net_device *dev)
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.
509 static const struct {
510 const char name[ETH_GSTRING_LEN];
511 } bat_counters_strings[] = {
520 { "tt_response_tx" },
521 { "tt_response_rx" },
522 { "tt_roam_adv_tx" },
523 { "tt_roam_adv_rx" },
526 static void batadv_get_strings(struct net_device *dev, uint32_t stringset,
529 if (stringset == ETH_SS_STATS)
530 memcpy(data, bat_counters_strings,
531 sizeof(bat_counters_strings));
534 static void batadv_get_ethtool_stats(struct net_device *dev,
535 struct ethtool_stats *stats,
538 struct bat_priv *bat_priv = netdev_priv(dev);
541 for (i = 0; i < BAT_CNT_NUM; i++)
542 data[i] = batadv_sum_counter(bat_priv, i);
545 static int batadv_get_sset_count(struct net_device *dev, int stringset)
547 if (stringset == ETH_SS_STATS)