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"
26 #include "translation-table.h"
29 #include <linux/slab.h>
30 #include <linux/ethtool.h>
31 #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;
38 unsigned char mainIfAddr[ETH_ALEN];
39 static unsigned char mainIfAddr_default[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(mainIfAddr, addr, ETH_ALEN);
64 int main_if_was_up(void)
66 return (memcmp(mainIfAddr, mainIfAddr_default, ETH_ALEN) != 0 ? 1 : 0);
69 int my_skb_push(struct sk_buff *skb, unsigned int len)
74 if (skb_headroom(skb) < len) {
76 result = pskb_expand_head(skb, len, 0, GFP_ATOMIC);
86 #ifdef HAVE_NET_DEVICE_OPS
87 static const struct net_device_ops bat_netdev_ops = {
88 .ndo_open = interface_open,
89 .ndo_stop = interface_release,
90 .ndo_get_stats = interface_stats,
91 .ndo_set_mac_address = interface_set_mac_addr,
92 .ndo_change_mtu = interface_change_mtu,
93 .ndo_start_xmit = interface_tx,
94 .ndo_validate_addr = eth_validate_addr
98 void interface_setup(struct net_device *dev)
100 struct bat_priv *priv = netdev_priv(dev);
101 char dev_addr[ETH_ALEN];
105 #ifdef HAVE_NET_DEVICE_OPS
106 dev->netdev_ops = &bat_netdev_ops;
108 dev->open = interface_open;
109 dev->stop = interface_release;
110 dev->get_stats = interface_stats;
111 dev->set_mac_address = interface_set_mac_addr;
112 dev->change_mtu = interface_change_mtu;
113 dev->hard_start_xmit = interface_tx;
115 dev->destructor = free_netdev;
117 dev->mtu = hardif_min_mtu();
118 dev->hard_header_len = BAT_HEADER_LEN; /* reserve more space in the
119 * skbuff for our header */
121 /* generate random address */
122 random_ether_addr(dev_addr);
123 memcpy(dev->dev_addr, dev_addr, ETH_ALEN);
125 SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
127 memset(priv, 0, sizeof(struct bat_priv));
130 int interface_open(struct net_device *dev)
132 netif_start_queue(dev);
136 int interface_release(struct net_device *dev)
138 netif_stop_queue(dev);
142 struct net_device_stats *interface_stats(struct net_device *dev)
144 struct bat_priv *priv = netdev_priv(dev);
148 int interface_set_mac_addr(struct net_device *dev, void *p)
150 struct sockaddr *addr = p;
152 if (!is_valid_ether_addr(addr->sa_data))
153 return -EADDRNOTAVAIL;
155 /* only modify hna-table if it has been initialised before */
156 if (atomic_read(&module_state) == MODULE_ACTIVE) {
157 hna_local_remove(dev->dev_addr, "mac address changed");
158 hna_local_add(addr->sa_data);
161 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
166 int interface_change_mtu(struct net_device *dev, int new_mtu)
169 if ((new_mtu < 68) || (new_mtu > hardif_min_mtu()))
177 int interface_tx(struct sk_buff *skb, struct net_device *dev)
179 struct unicast_packet *unicast_packet;
180 struct bcast_packet *bcast_packet;
181 struct orig_node *orig_node;
182 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
183 struct bat_priv *priv = netdev_priv(dev);
184 struct batman_if *batman_if;
185 struct bat_priv *bat_priv;
187 int data_len = skb->len;
190 if (atomic_read(&module_state) != MODULE_ACTIVE)
193 /* FIXME: each batman_if will be attached to a softif */
194 bat_priv = netdev_priv(soft_device);
196 dev->trans_start = jiffies;
197 /* TODO: check this for locks */
198 hna_local_add(ethhdr->h_source);
200 /* ethernet packet should be broadcasted */
201 if (is_bcast(ethhdr->h_dest) || is_mcast(ethhdr->h_dest)) {
203 if (my_skb_push(skb, sizeof(struct bcast_packet)) < 0)
206 bcast_packet = (struct bcast_packet *)skb->data;
207 bcast_packet->version = COMPAT_VERSION;
209 /* batman packet type: broadcast */
210 bcast_packet->packet_type = BAT_BCAST;
212 /* hw address of first interface is the orig mac because only
213 * this mac is known throughout the mesh */
214 memcpy(bcast_packet->orig, mainIfAddr, ETH_ALEN);
216 /* set broadcast sequence number */
217 bcast_packet->seqno = htons(bcast_seqno);
219 /* broadcast packet. on success, increase seqno. */
220 if (add_bcast_packet_to_list(skb) == NETDEV_TX_OK)
223 /* a copy is stored in the bcast list, therefore removing
224 * the original skb. */
229 spin_lock_irqsave(&orig_hash_lock, flags);
230 /* get routing information */
231 orig_node = ((struct orig_node *)hash_find(orig_hash,
234 /* check for hna host */
236 orig_node = transtable_search(ethhdr->h_dest);
239 (orig_node->router)) {
240 struct neigh_node *router = orig_node->router;
242 if (my_skb_push(skb, sizeof(struct unicast_packet)) < 0)
245 unicast_packet = (struct unicast_packet *)skb->data;
247 unicast_packet->version = COMPAT_VERSION;
248 /* batman packet type: unicast */
249 unicast_packet->packet_type = BAT_UNICAST;
250 /* set unicast ttl */
251 unicast_packet->ttl = TTL;
252 /* copy the destination for faster routing */
253 memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
255 /* net_dev won't be available when not active */
256 if (router->if_incoming->if_status != IF_ACTIVE)
259 /* don't lock while sending the packets ... we therefore
260 * copy the required data before sending */
262 batman_if = router->if_incoming;
263 memcpy(dstaddr, router->addr, ETH_ALEN);
264 spin_unlock_irqrestore(&orig_hash_lock, flags);
266 send_skb_packet(skb, batman_if, dstaddr);
272 priv->stats.tx_packets++;
273 priv->stats.tx_bytes += data_len;
277 spin_unlock_irqrestore(&orig_hash_lock, flags);
279 priv->stats.tx_dropped++;
285 void interface_rx(struct sk_buff *skb, int hdr_size)
287 struct net_device *dev = soft_device;
288 struct bat_priv *priv = netdev_priv(dev);
290 /* check if enough space is available for pulling, and pull */
291 if (!pskb_may_pull(skb, hdr_size)) {
295 skb_pull_rcsum(skb, hdr_size);
296 /* skb_set_mac_header(skb, -sizeof(struct ethhdr));*/
299 skb->protocol = eth_type_trans(skb, dev);
301 /* should not be neccesary anymore as we use skb_pull_rcsum()
302 * TODO: please verify this and remove this TODO
303 * -- Dec 21st 2009, Simon Wunderlich */
305 /* skb->ip_summed = CHECKSUM_UNNECESSARY;*/
307 /* TODO: set skb->pkt_type to PACKET_BROADCAST, PACKET_MULTICAST,
308 * PACKET_OTHERHOST or PACKET_HOST */
310 priv->stats.rx_packets++;
311 priv->stats.rx_bytes += skb->len;
313 dev->last_rx = jiffies;
319 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
322 cmd->advertising = 0;
323 cmd->speed = SPEED_10;
324 cmd->duplex = DUPLEX_FULL;
326 cmd->phy_address = 0;
327 cmd->transceiver = XCVR_INTERNAL;
328 cmd->autoneg = AUTONEG_DISABLE;
335 static void bat_get_drvinfo(struct net_device *dev,
336 struct ethtool_drvinfo *info)
338 strcpy(info->driver, "B.A.T.M.A.N. advanced");
339 strcpy(info->version, SOURCE_VERSION);
340 strcpy(info->fw_version, "N/A");
341 strcpy(info->bus_info, "batman");
344 static u32 bat_get_msglevel(struct net_device *dev)
349 static void bat_set_msglevel(struct net_device *dev, u32 value)
353 static u32 bat_get_link(struct net_device *dev)
358 static u32 bat_get_rx_csum(struct net_device *dev)
363 static int bat_set_rx_csum(struct net_device *dev, u32 data)