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"
29 #include <linux/ethtool.h>
30 #include <linux/etherdevice.h>
33 static uint16_t bcast_seqno = 1; /* give own bcast messages seq numbers to avoid
35 static int32_t skb_packets;
36 static int32_t skb_bad_packets;
37 static int32_t lock_dropped;
39 unsigned char mainIfAddr[ETH_ALEN];
40 static unsigned char mainIfAddr_default[ETH_ALEN];
41 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
42 static void bat_get_drvinfo(struct net_device *dev,
43 struct ethtool_drvinfo *info);
44 static u32 bat_get_msglevel(struct net_device *dev);
45 static void bat_set_msglevel(struct net_device *dev, u32 value);
46 static u32 bat_get_link(struct net_device *dev);
47 static u32 bat_get_rx_csum(struct net_device *dev);
48 static int bat_set_rx_csum(struct net_device *dev, u32 data);
50 static const struct ethtool_ops bat_ethtool_ops = {
51 .get_settings = bat_get_settings,
52 .get_drvinfo = bat_get_drvinfo,
53 .get_msglevel = bat_get_msglevel,
54 .set_msglevel = bat_set_msglevel,
55 .get_link = bat_get_link,
56 .get_rx_csum = bat_get_rx_csum,
57 .set_rx_csum = bat_set_rx_csum
60 void set_main_if_addr(uint8_t *addr)
62 memcpy(mainIfAddr, addr, ETH_ALEN);
65 int main_if_was_up(void)
67 return (memcmp(mainIfAddr, mainIfAddr_default, ETH_ALEN) != 0 ? 1 : 0);
70 static int my_skb_push(struct sk_buff *skb, unsigned int len)
75 if (skb->data - len < skb->head) {
77 result = pskb_expand_head(skb, len, 0, GFP_ATOMIC);
87 #ifdef HAVE_NET_DEVICE_OPS
88 static const struct net_device_ops bat_netdev_ops = {
89 .ndo_open = interface_open,
90 .ndo_stop = interface_release,
91 .ndo_get_stats = interface_stats,
92 .ndo_set_mac_address = interface_set_mac_addr,
93 .ndo_change_mtu = interface_change_mtu,
94 .ndo_start_xmit = interface_tx,
95 .ndo_validate_addr = eth_validate_addr
99 void interface_setup(struct net_device *dev)
101 struct bat_priv *priv = netdev_priv(dev);
102 char dev_addr[ETH_ALEN];
106 #ifdef HAVE_NET_DEVICE_OPS
107 dev->netdev_ops = &bat_netdev_ops;
109 dev->open = interface_open;
110 dev->stop = interface_release;
111 dev->get_stats = interface_stats;
112 dev->set_mac_address = interface_set_mac_addr;
113 dev->change_mtu = interface_change_mtu;
114 dev->hard_start_xmit = interface_tx;
116 dev->destructor = free_netdev;
118 dev->mtu = hardif_min_mtu();
119 dev->hard_header_len = BAT_HEADER_LEN; /* reserve more space in the
120 * skbuff for our header */
122 /* generate random address */
123 random_ether_addr(dev_addr);
124 memcpy(dev->dev_addr, dev_addr, sizeof(dev->dev_addr));
126 SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
128 memset(priv, 0, sizeof(struct bat_priv));
131 int interface_open(struct net_device *dev)
133 netif_start_queue(dev);
137 int interface_release(struct net_device *dev)
139 netif_stop_queue(dev);
143 struct net_device_stats *interface_stats(struct net_device *dev)
145 struct bat_priv *priv = netdev_priv(dev);
149 int interface_set_mac_addr(struct net_device *dev, void *addr)
154 int interface_change_mtu(struct net_device *dev, int new_mtu)
157 if ((new_mtu < 68) || (new_mtu > hardif_min_mtu()))
165 int interface_tx(struct sk_buff *skb, struct net_device *dev)
167 struct unicast_packet *unicast_packet;
168 struct bcast_packet *bcast_packet;
169 struct orig_node *orig_node;
170 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
171 struct bat_priv *priv = netdev_priv(dev);
172 int data_len = skb->len;
174 if (atomic_read(&module_state) != MODULE_ACTIVE)
177 dev->trans_start = jiffies;
178 /* TODO: check this for locks */
179 hna_local_add(ethhdr->h_source);
181 /* ethernet packet should be broadcasted */
182 if (is_bcast(ethhdr->h_dest) || is_mcast(ethhdr->h_dest)) {
184 if (my_skb_push(skb, sizeof(struct bcast_packet)) < 0)
187 bcast_packet = (struct bcast_packet *)skb->data;
189 bcast_packet->version = COMPAT_VERSION;
191 /* batman packet type: broadcast */
192 bcast_packet->packet_type = BAT_BCAST;
194 /* hw address of first interface is the orig mac because only
195 * this mac is known throughout the mesh */
196 memcpy(bcast_packet->orig, mainIfAddr, ETH_ALEN);
197 /* set broadcast sequence number */
198 bcast_packet->seqno = htons(bcast_seqno);
202 /* broadcast packet */
203 add_bcast_packet_to_list(skb->data, skb->len);
208 /* simply spin_lock()ing can deadlock when the lock is already
210 /* TODO: defer the work in a working queue instead of
212 if (!spin_trylock(&orig_hash_lock)) {
214 printk(KERN_WARNING "batman-adv:%d packets dropped because lock was hold\n", lock_dropped);
218 /* get routing information */
219 orig_node = ((struct orig_node *)hash_find(orig_hash,
222 /* check for hna host */
224 orig_node = transtable_search(ethhdr->h_dest);
227 (orig_node->batman_if) &&
228 (orig_node->router)) {
229 if (my_skb_push(skb, sizeof(struct unicast_packet)) < 0)
232 unicast_packet = (struct unicast_packet *)skb->data;
234 unicast_packet->version = COMPAT_VERSION;
235 /* batman packet type: unicast */
236 unicast_packet->packet_type = BAT_UNICAST;
237 /* set unicast ttl */
238 unicast_packet->ttl = TTL;
239 /* copy the destination for faster routing */
240 memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
242 /* net_dev won't be available when not active */
243 if (orig_node->batman_if->if_active != IF_ACTIVE)
246 send_raw_packet(skb->data, skb->len,
247 orig_node->batman_if,
248 orig_node->router->addr);
253 spin_unlock(&orig_hash_lock);
256 priv->stats.tx_packets++;
257 priv->stats.tx_bytes += data_len;
261 spin_unlock(&orig_hash_lock);
263 priv->stats.tx_dropped++;
269 void interface_rx(struct net_device *dev, void *packet, int packet_len)
272 struct bat_priv *priv = netdev_priv(dev);
274 skb = dev_alloc_skb(packet_len);
277 priv->stats.rx_dropped++;
281 memcpy(skb_put(skb, packet_len), packet, packet_len);
283 /* Write metadata, and then pass to the receive level */
285 skb->protocol = eth_type_trans(skb, dev);
286 skb->ip_summed = CHECKSUM_UNNECESSARY;
288 priv->stats.rx_packets++;
289 priv->stats.rx_bytes += packet_len;
291 dev->last_rx = jiffies;
300 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
303 cmd->advertising = 0;
304 cmd->speed = SPEED_10;
305 cmd->duplex = DUPLEX_FULL;
307 cmd->phy_address = 0;
308 cmd->transceiver = XCVR_INTERNAL;
309 cmd->autoneg = AUTONEG_DISABLE;
316 static void bat_get_drvinfo(struct net_device *dev,
317 struct ethtool_drvinfo *info)
319 strcpy(info->driver, "B.A.T.M.A.N. advanced");
320 strcpy(info->version, SOURCE_VERSION);
321 strcpy(info->fw_version, "N/A");
322 strcpy(info->bus_info, "batman");
325 static u32 bat_get_msglevel(struct net_device *dev)
330 static void bat_set_msglevel(struct net_device *dev, u32 value)
335 static u32 bat_get_link(struct net_device *dev)
340 static u32 bat_get_rx_csum(struct net_device *dev)
345 static int bat_set_rx_csum(struct net_device *dev, u32 data)