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 "translation-table.h"
30 #include <linux/slab.h>
31 #include <linux/ethtool.h>
32 #include <linux/etherdevice.h>
34 static uint32_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;
39 unsigned char main_if_addr[ETH_ALEN];
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 u32 bat_get_rx_csum(struct net_device *dev);
47 static int bat_set_rx_csum(struct net_device *dev, u32 data);
49 static const struct ethtool_ops bat_ethtool_ops = {
50 .get_settings = bat_get_settings,
51 .get_drvinfo = bat_get_drvinfo,
52 .get_msglevel = bat_get_msglevel,
53 .set_msglevel = bat_set_msglevel,
54 .get_link = bat_get_link,
55 .get_rx_csum = bat_get_rx_csum,
56 .set_rx_csum = bat_set_rx_csum
59 void set_main_if_addr(uint8_t *addr)
61 memcpy(main_if_addr, addr, ETH_ALEN);
64 int my_skb_push(struct sk_buff *skb, unsigned int len)
69 if (skb_headroom(skb) < len) {
71 result = pskb_expand_head(skb, len, 0, GFP_ATOMIC);
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 *priv = netdev_priv(dev);
99 static int interface_set_mac_addr(struct net_device *dev, void *p)
101 struct sockaddr *addr = p;
103 if (!is_valid_ether_addr(addr->sa_data))
104 return -EADDRNOTAVAIL;
106 /* only modify hna-table if it has been initialised before */
107 if (atomic_read(&module_state) == MODULE_ACTIVE) {
108 hna_local_remove(dev->dev_addr, "mac address changed");
109 hna_local_add(addr->sa_data);
112 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
117 static int interface_change_mtu(struct net_device *dev, int new_mtu)
120 if ((new_mtu < 68) || (new_mtu > hardif_min_mtu()))
128 int interface_tx(struct sk_buff *skb, struct net_device *dev)
130 struct unicast_packet *unicast_packet;
131 struct bcast_packet *bcast_packet;
132 struct orig_node *orig_node;
133 struct neigh_node *router;
134 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
135 struct bat_priv *priv = netdev_priv(dev);
136 struct batman_if *batman_if;
137 struct bat_priv *bat_priv;
139 int data_len = skb->len;
142 if (atomic_read(&module_state) != MODULE_ACTIVE)
145 /* FIXME: each batman_if will be attached to a softif */
146 bat_priv = netdev_priv(soft_device);
148 dev->trans_start = jiffies;
149 /* TODO: check this for locks */
150 hna_local_add(ethhdr->h_source);
152 /* ethernet packet should be broadcasted */
153 if (is_bcast(ethhdr->h_dest) || is_mcast(ethhdr->h_dest)) {
155 if (my_skb_push(skb, sizeof(struct bcast_packet)) < 0)
158 bcast_packet = (struct bcast_packet *)skb->data;
159 bcast_packet->version = COMPAT_VERSION;
160 bcast_packet->ttl = TTL;
162 /* batman packet type: broadcast */
163 bcast_packet->packet_type = BAT_BCAST;
165 /* hw address of first interface is the orig mac because only
166 * this mac is known throughout the mesh */
167 memcpy(bcast_packet->orig, main_if_addr, ETH_ALEN);
169 /* set broadcast sequence number */
170 bcast_packet->seqno = htonl(bcast_seqno);
172 /* broadcast packet. on success, increase seqno. */
173 if (add_bcast_packet_to_list(skb) == NETDEV_TX_OK)
176 /* a copy is stored in the bcast list, therefore removing
177 * the original skb. */
182 spin_lock_irqsave(&orig_hash_lock, flags);
183 /* get routing information */
184 orig_node = ((struct orig_node *)hash_find(orig_hash,
187 /* check for hna host */
189 orig_node = transtable_search(ethhdr->h_dest);
191 router = find_router(orig_node, NULL);
196 /* don't lock while sending the packets ... we therefore
197 * copy the required data before sending */
199 batman_if = router->if_incoming;
200 memcpy(dstaddr, router->addr, ETH_ALEN);
202 spin_unlock_irqrestore(&orig_hash_lock, flags);
204 if (batman_if->if_status != IF_ACTIVE)
207 if (my_skb_push(skb, sizeof(struct unicast_packet)) < 0)
210 unicast_packet = (struct unicast_packet *)skb->data;
212 unicast_packet->version = COMPAT_VERSION;
213 /* batman packet type: unicast */
214 unicast_packet->packet_type = BAT_UNICAST;
215 /* set unicast ttl */
216 unicast_packet->ttl = TTL;
217 /* copy the destination for faster routing */
218 memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
220 send_skb_packet(skb, batman_if, dstaddr);
223 priv->stats.tx_packets++;
224 priv->stats.tx_bytes += data_len;
228 spin_unlock_irqrestore(&orig_hash_lock, flags);
230 priv->stats.tx_dropped++;
236 void interface_rx(struct sk_buff *skb, int hdr_size)
238 struct net_device *dev = soft_device;
239 struct bat_priv *priv = netdev_priv(dev);
241 /* check if enough space is available for pulling, and pull */
242 if (!pskb_may_pull(skb, hdr_size)) {
246 skb_pull_rcsum(skb, hdr_size);
247 /* skb_set_mac_header(skb, -sizeof(struct ethhdr));*/
250 skb->protocol = eth_type_trans(skb, dev);
252 /* should not be neccesary anymore as we use skb_pull_rcsum()
253 * TODO: please verify this and remove this TODO
254 * -- Dec 21st 2009, Simon Wunderlich */
256 /* skb->ip_summed = CHECKSUM_UNNECESSARY;*/
258 /* TODO: set skb->pkt_type to PACKET_BROADCAST, PACKET_MULTICAST,
259 * PACKET_OTHERHOST or PACKET_HOST */
261 priv->stats.rx_packets++;
262 priv->stats.rx_bytes += skb->len;
264 dev->last_rx = jiffies;
269 #ifdef HAVE_NET_DEVICE_OPS
270 static const struct net_device_ops bat_netdev_ops = {
271 .ndo_open = interface_open,
272 .ndo_stop = interface_release,
273 .ndo_get_stats = interface_stats,
274 .ndo_set_mac_address = interface_set_mac_addr,
275 .ndo_change_mtu = interface_change_mtu,
276 .ndo_start_xmit = interface_tx,
277 .ndo_validate_addr = eth_validate_addr
281 void interface_setup(struct net_device *dev)
283 struct bat_priv *priv = netdev_priv(dev);
284 char dev_addr[ETH_ALEN];
288 #ifdef HAVE_NET_DEVICE_OPS
289 dev->netdev_ops = &bat_netdev_ops;
291 dev->open = interface_open;
292 dev->stop = interface_release;
293 dev->get_stats = interface_stats;
294 dev->set_mac_address = interface_set_mac_addr;
295 dev->change_mtu = interface_change_mtu;
296 dev->hard_start_xmit = interface_tx;
298 dev->destructor = free_netdev;
300 dev->mtu = hardif_min_mtu();
301 dev->hard_header_len = BAT_HEADER_LEN; /* reserve more space in the
302 * skbuff for our header */
304 /* generate random address */
305 random_ether_addr(dev_addr);
306 memcpy(dev->dev_addr, dev_addr, ETH_ALEN);
308 SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
310 memset(priv, 0, sizeof(struct bat_priv));
314 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
317 cmd->advertising = 0;
318 cmd->speed = SPEED_10;
319 cmd->duplex = DUPLEX_FULL;
321 cmd->phy_address = 0;
322 cmd->transceiver = XCVR_INTERNAL;
323 cmd->autoneg = AUTONEG_DISABLE;
330 static void bat_get_drvinfo(struct net_device *dev,
331 struct ethtool_drvinfo *info)
333 strcpy(info->driver, "B.A.T.M.A.N. advanced");
334 strcpy(info->version, SOURCE_VERSION);
335 strcpy(info->fw_version, "N/A");
336 strcpy(info->bus_info, "batman");
339 static u32 bat_get_msglevel(struct net_device *dev)
344 static void bat_set_msglevel(struct net_device *dev, u32 value)
348 static u32 bat_get_link(struct net_device *dev)
353 static u32 bat_get_rx_csum(struct net_device *dev)
358 static int bat_set_rx_csum(struct net_device *dev, u32 data)