2 * Copyright (C) 2007-2010 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
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.
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.
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
23 #include "soft-interface.h"
24 #include "hard-interface.h"
27 #include "bat_debugfs.h"
28 #include "translation-table.h"
32 #include "bat_sysfs.h"
33 #include <linux/slab.h>
34 #include <linux/ethtool.h>
35 #include <linux/etherdevice.h>
38 static uint32_t bcast_seqno = 1; /* give own bcast messages seq numbers to avoid
41 unsigned char main_if_addr[ETH_ALEN];
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 u32 bat_get_rx_csum(struct net_device *dev);
49 static int bat_set_rx_csum(struct net_device *dev, u32 data);
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_rx_csum = bat_get_rx_csum,
58 .set_rx_csum = bat_set_rx_csum
61 void set_main_if_addr(uint8_t *addr)
63 memcpy(main_if_addr, addr, ETH_ALEN);
66 int my_skb_head_push(struct sk_buff *skb, unsigned int len)
71 * TODO: We must check if we can release all references to non-payload
72 * data using skb_header_release in our skbs to allow skb_cow_header to
73 * work optimally. This means that those skbs are not allowed to read
74 * or write any data which is before the current position of skb->data
75 * after that call and thus allow other skbs with the same data buffer
76 * to write freely in that area.
78 result = skb_cow_head(skb, len);
87 static int interface_open(struct net_device *dev)
89 netif_start_queue(dev);
93 static int interface_release(struct net_device *dev)
95 netif_stop_queue(dev);
99 static struct net_device_stats *interface_stats(struct net_device *dev)
101 struct bat_priv *bat_priv = netdev_priv(dev);
102 return &bat_priv->stats;
105 static int interface_set_mac_addr(struct net_device *dev, void *p)
107 struct bat_priv *bat_priv = netdev_priv(dev);
108 struct sockaddr *addr = p;
110 if (!is_valid_ether_addr(addr->sa_data))
111 return -EADDRNOTAVAIL;
113 /* only modify hna-table if it has been initialised before */
114 if (atomic_read(&module_state) == MODULE_ACTIVE) {
115 hna_local_remove(bat_priv, dev->dev_addr,
116 "mac address changed");
117 hna_local_add(dev, addr->sa_data);
120 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
125 static int interface_change_mtu(struct net_device *dev, int new_mtu)
128 if ((new_mtu < 68) || (new_mtu > hardif_min_mtu(dev)))
136 int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
138 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
139 struct bat_priv *bat_priv = netdev_priv(soft_iface);
140 struct bcast_packet *bcast_packet;
141 int data_len = skb->len, ret;
143 if (atomic_read(&module_state) != MODULE_ACTIVE)
146 soft_iface->trans_start = jiffies;
148 /* TODO: check this for locks */
149 hna_local_add(soft_iface, ethhdr->h_source);
151 /* ethernet packet should be broadcasted */
152 if (is_bcast(ethhdr->h_dest) || is_mcast(ethhdr->h_dest)) {
154 if (my_skb_head_push(skb, sizeof(struct bcast_packet)) < 0)
157 bcast_packet = (struct bcast_packet *)skb->data;
158 bcast_packet->version = COMPAT_VERSION;
159 bcast_packet->ttl = TTL;
161 /* batman packet type: broadcast */
162 bcast_packet->packet_type = BAT_BCAST;
164 /* hw address of first interface is the orig mac because only
165 * this mac is known throughout the mesh */
166 memcpy(bcast_packet->orig, main_if_addr, ETH_ALEN);
168 /* set broadcast sequence number */
169 bcast_packet->seqno = htonl(bcast_seqno);
171 /* broadcast packet. on success, increase seqno. */
172 if (add_bcast_packet_to_list(bat_priv, skb) == NETDEV_TX_OK)
175 /* a copy is stored in the bcast list, therefore removing
176 * the original skb. */
181 ret = unicast_send_skb(skb, bat_priv);
183 bat_priv->stats.tx_dropped++;
188 bat_priv->stats.tx_packets++;
189 bat_priv->stats.tx_bytes += data_len;
193 bat_priv->stats.tx_dropped++;
199 void interface_rx(struct net_device *soft_iface,
200 struct sk_buff *skb, int hdr_size)
202 struct bat_priv *priv = netdev_priv(soft_iface);
204 /* check if enough space is available for pulling, and pull */
205 if (!pskb_may_pull(skb, hdr_size)) {
209 skb_pull_rcsum(skb, hdr_size);
210 /* skb_set_mac_header(skb, -sizeof(struct ethhdr));*/
212 skb->dev = soft_iface;
213 skb->protocol = eth_type_trans(skb, soft_iface);
215 /* should not be neccesary anymore as we use skb_pull_rcsum()
216 * TODO: please verify this and remove this TODO
217 * -- Dec 21st 2009, Simon Wunderlich */
219 /* skb->ip_summed = CHECKSUM_UNNECESSARY;*/
221 /* TODO: set skb->pkt_type to PACKET_BROADCAST, PACKET_MULTICAST,
222 * PACKET_OTHERHOST or PACKET_HOST */
224 priv->stats.rx_packets++;
225 priv->stats.rx_bytes += skb->len + sizeof(struct ethhdr);
227 soft_iface->last_rx = jiffies;
232 #ifdef HAVE_NET_DEVICE_OPS
233 static const struct net_device_ops bat_netdev_ops = {
234 .ndo_open = interface_open,
235 .ndo_stop = interface_release,
236 .ndo_get_stats = interface_stats,
237 .ndo_set_mac_address = interface_set_mac_addr,
238 .ndo_change_mtu = interface_change_mtu,
239 .ndo_start_xmit = interface_tx,
240 .ndo_validate_addr = eth_validate_addr
244 static void interface_setup(struct net_device *dev)
246 struct bat_priv *priv = netdev_priv(dev);
247 char dev_addr[ETH_ALEN];
251 #ifdef HAVE_NET_DEVICE_OPS
252 dev->netdev_ops = &bat_netdev_ops;
254 dev->open = interface_open;
255 dev->stop = interface_release;
256 dev->get_stats = interface_stats;
257 dev->set_mac_address = interface_set_mac_addr;
258 dev->change_mtu = interface_change_mtu;
259 dev->hard_start_xmit = interface_tx;
261 dev->destructor = free_netdev;
264 * can't call min_mtu, because the needed variables
265 * have not been initialized yet
267 dev->mtu = ETH_DATA_LEN;
268 dev->hard_header_len = BAT_HEADER_LEN; /* reserve more space in the
269 * skbuff for our header */
271 /* generate random address */
272 random_ether_addr(dev_addr);
273 memcpy(dev->dev_addr, dev_addr, ETH_ALEN);
275 SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
277 memset(priv, 0, sizeof(struct bat_priv));
280 struct net_device *softif_create(char *name)
282 struct net_device *soft_iface;
283 struct bat_priv *bat_priv;
286 soft_iface = alloc_netdev(sizeof(struct bat_priv) , name,
290 pr_err("Unable to allocate the batman interface: %s\n", name);
294 ret = register_netdev(soft_iface);
297 pr_err("Unable to register the batman interface '%s': %i\n",
299 goto free_soft_iface;
302 bat_priv = netdev_priv(soft_iface);
304 atomic_set(&bat_priv->aggregation_enabled, 1);
305 atomic_set(&bat_priv->bonding_enabled, 0);
306 atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE);
307 atomic_set(&bat_priv->orig_interval, 1000);
308 atomic_set(&bat_priv->log_level, 0);
309 atomic_set(&bat_priv->frag_enabled, 1);
310 atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN);
311 atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN);
313 bat_priv->primary_if = NULL;
314 bat_priv->num_ifaces = 0;
316 ret = sysfs_add_meshif(soft_iface);
319 goto unreg_soft_iface;
321 ret = debugfs_add_meshif(soft_iface);
329 sysfs_del_meshif(soft_iface);
331 unregister_netdev(soft_iface);
335 free_netdev(soft_iface);
340 void softif_destroy(struct net_device *soft_iface)
342 debugfs_del_meshif(soft_iface);
343 sysfs_del_meshif(soft_iface);
344 unregister_netdevice(soft_iface);
348 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
351 cmd->advertising = 0;
352 cmd->speed = SPEED_10;
353 cmd->duplex = DUPLEX_FULL;
355 cmd->phy_address = 0;
356 cmd->transceiver = XCVR_INTERNAL;
357 cmd->autoneg = AUTONEG_DISABLE;
364 static void bat_get_drvinfo(struct net_device *dev,
365 struct ethtool_drvinfo *info)
367 strcpy(info->driver, "B.A.T.M.A.N. advanced");
368 strcpy(info->version, SOURCE_VERSION);
369 strcpy(info->fw_version, "N/A");
370 strcpy(info->bus_info, "batman");
373 static u32 bat_get_msglevel(struct net_device *dev)
378 static void bat_set_msglevel(struct net_device *dev, u32 value)
382 static u32 bat_get_link(struct net_device *dev)
387 static u32 bat_get_rx_csum(struct net_device *dev)
392 static int bat_set_rx_csum(struct net_device *dev, u32 data)