2 * Copyright (C) 2007-2009 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"
26 #include "translation-table.h"
30 #include <linux/ethtool.h>
31 #include <linux/etherdevice.h>
34 static uint16_t bcast_seqno = 1; /* give own bcast messages seq numbers to avoid
36 static int32_t skb_packets;
37 static int32_t skb_bad_packets;
38 static int32_t lock_dropped;
40 unsigned char mainIfAddr[ETH_ALEN];
41 static unsigned char mainIfAddr_default[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(mainIfAddr, addr, ETH_ALEN);
66 int main_if_was_up(void)
68 return (memcmp(mainIfAddr, mainIfAddr_default, ETH_ALEN) != 0 ? 1 : 0);
71 static int my_skb_push(struct sk_buff *skb, unsigned int len)
76 if (skb->data - len < skb->head) {
78 result = pskb_expand_head(skb, len, 0, GFP_ATOMIC);
88 #ifdef HAVE_NET_DEVICE_OPS
89 static const struct net_device_ops bat_netdev_ops = {
90 .ndo_open = interface_open,
91 .ndo_stop = interface_release,
92 .ndo_get_stats = interface_stats,
93 .ndo_set_mac_address = interface_set_mac_addr,
94 .ndo_change_mtu = interface_change_mtu,
95 .ndo_start_xmit = interface_tx,
96 .ndo_validate_addr = eth_validate_addr
100 void interface_setup(struct net_device *dev)
102 struct bat_priv *priv = netdev_priv(dev);
103 char dev_addr[ETH_ALEN];
107 #ifdef HAVE_NET_DEVICE_OPS
108 dev->netdev_ops = &bat_netdev_ops;
110 dev->open = interface_open;
111 dev->stop = interface_release;
112 dev->get_stats = interface_stats;
113 dev->set_mac_address = interface_set_mac_addr;
114 dev->change_mtu = interface_change_mtu;
115 dev->hard_start_xmit = interface_tx;
117 dev->destructor = free_netdev;
119 dev->mtu = hardif_min_mtu();
120 dev->hard_header_len = BAT_HEADER_LEN; /* reserve more space in the
121 * skbuff for our header */
123 /* generate random address */
124 random_ether_addr(dev_addr);
125 memcpy(dev->dev_addr, dev_addr, sizeof(dev->dev_addr));
127 SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
129 memset(priv, 0, sizeof(struct bat_priv));
132 int interface_open(struct net_device *dev)
134 netif_start_queue(dev);
138 int interface_release(struct net_device *dev)
140 netif_stop_queue(dev);
144 struct net_device_stats *interface_stats(struct net_device *dev)
146 struct bat_priv *priv = netdev_priv(dev);
150 int interface_set_mac_addr(struct net_device *dev, void *addr)
155 int interface_change_mtu(struct net_device *dev, int new_mtu)
158 if ((new_mtu < 68) || (new_mtu > hardif_min_mtu()))
166 int interface_tx(struct sk_buff *skb, struct net_device *dev)
168 struct unicast_packet *unicast_packet;
169 struct bcast_packet *bcast_packet;
170 struct orig_node *orig_node;
171 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
172 struct bat_priv *priv = netdev_priv(dev);
173 int data_len = skb->len;
175 if (atomic_read(&module_state) != MODULE_ACTIVE)
178 dev->trans_start = jiffies;
179 /* TODO: check this for locks */
180 hna_local_add(ethhdr->h_source);
182 /* ethernet packet should be broadcasted */
183 if (is_bcast(ethhdr->h_dest) || is_mcast(ethhdr->h_dest)) {
185 if (my_skb_push(skb, sizeof(struct bcast_packet)) < 0)
188 bcast_packet = (struct bcast_packet *)skb->data;
190 bcast_packet->version = COMPAT_VERSION;
192 /* batman packet type: broadcast */
193 bcast_packet->packet_type = BAT_BCAST;
195 /* hw address of first interface is the orig mac because only
196 * this mac is known throughout the mesh */
197 memcpy(bcast_packet->orig, mainIfAddr, ETH_ALEN);
198 /* set broadcast sequence number */
199 bcast_packet->seqno = htons(bcast_seqno);
203 /* broadcast packet */
204 add_bcast_packet_to_list(skb->data, skb->len);
209 /* simply spin_lock()ing can deadlock when the lock is already
211 /* TODO: defer the work in a working queue instead of
213 if (!spin_trylock(&orig_hash_lock)) {
215 debug_log(LOG_TYPE_NOTICE, "%d packets dropped because lock was hold\n", lock_dropped);
219 /* get routing information */
220 orig_node = ((struct orig_node *)hash_find(orig_hash,
223 /* check for hna host */
225 orig_node = transtable_search(ethhdr->h_dest);
228 (orig_node->batman_if) &&
229 (orig_node->router)) {
230 if (my_skb_push(skb, sizeof(struct unicast_packet)) < 0)
233 unicast_packet = (struct unicast_packet *)skb->data;
235 unicast_packet->version = COMPAT_VERSION;
236 /* batman packet type: unicast */
237 unicast_packet->packet_type = BAT_UNICAST;
238 /* set unicast ttl */
239 unicast_packet->ttl = TTL;
240 /* copy the destination for faster routing */
241 memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
243 /* net_dev won't be available when not active */
244 if (orig_node->batman_if->if_active != IF_ACTIVE)
247 send_raw_packet(skb->data, skb->len,
248 orig_node->batman_if,
249 orig_node->router->addr);
254 spin_unlock(&orig_hash_lock);
257 priv->stats.tx_packets++;
258 priv->stats.tx_bytes += data_len;
262 spin_unlock(&orig_hash_lock);
264 priv->stats.tx_dropped++;
270 void interface_rx(struct net_device *dev, void *packet, int packet_len)
273 struct bat_priv *priv = netdev_priv(dev);
275 skb = dev_alloc_skb(packet_len);
278 priv->stats.rx_dropped++;
282 memcpy(skb_put(skb, packet_len), packet, packet_len);
284 /* Write metadata, and then pass to the receive level */
286 skb->protocol = eth_type_trans(skb, dev);
287 skb->ip_summed = CHECKSUM_UNNECESSARY;
289 priv->stats.rx_packets++;
290 priv->stats.rx_bytes += packet_len;
292 dev->last_rx = jiffies;
301 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
304 cmd->advertising = 0;
305 cmd->speed = SPEED_10;
306 cmd->duplex = DUPLEX_FULL;
308 cmd->phy_address = 0;
309 cmd->transceiver = XCVR_INTERNAL;
310 cmd->autoneg = AUTONEG_DISABLE;
317 static void bat_get_drvinfo(struct net_device *dev,
318 struct ethtool_drvinfo *info)
320 strcpy(info->driver, "B.A.T.M.A.N. advanced");
321 strcpy(info->version, SOURCE_VERSION);
322 strcpy(info->fw_version, "N/A");
323 strcpy(info->bus_info, "batman");
326 static u32 bat_get_msglevel(struct net_device *dev)
331 static void bat_set_msglevel(struct net_device *dev, u32 value)
336 static u32 bat_get_link(struct net_device *dev)
341 static u32 bat_get_rx_csum(struct net_device *dev)
346 static int bat_set_rx_csum(struct net_device *dev, u32 data)