Staging: batman-adv: receive packets directly using skbs
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / batman-adv / soft-interface.c
blob8ae3483a625906c95f4c9b00702c6c1f299f83ae
1 /*
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
18 * 02110-1301, USA
22 #include "main.h"
23 #include "soft-interface.h"
24 #include "hard-interface.h"
25 #include "send.h"
26 #include "translation-table.h"
27 #include "types.h"
28 #include "hash.h"
29 #include <linux/ethtool.h>
30 #include <linux/etherdevice.h>
31 #include "compat.h"
33 static uint16_t bcast_seqno = 1; /* give own bcast messages seq numbers to avoid
34 * broadcast storms */
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)
71 int result = 0;
73 skb_packets++;
74 if (skb_headroom(skb) < len) {
75 skb_bad_packets++;
76 result = pskb_expand_head(skb, len, 0, GFP_ATOMIC);
78 if (result < 0)
79 return result;
82 skb_push(skb, len);
83 return 0;
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
96 #endif
98 void interface_setup(struct net_device *dev)
100 struct bat_priv *priv = netdev_priv(dev);
101 char dev_addr[ETH_ALEN];
103 ether_setup(dev);
105 #ifdef HAVE_NET_DEVICE_OPS
106 dev->netdev_ops = &bat_netdev_ops;
107 #else
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;
114 #endif
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, sizeof(dev->dev_addr));
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);
133 return 0;
136 int interface_release(struct net_device *dev)
138 netif_stop_queue(dev);
139 return 0;
142 struct net_device_stats *interface_stats(struct net_device *dev)
144 struct bat_priv *priv = netdev_priv(dev);
145 return &priv->stats;
148 int interface_set_mac_addr(struct net_device *dev, void *addr)
150 return -EBUSY;
153 int interface_change_mtu(struct net_device *dev, int new_mtu)
155 /* check ranges */
156 if ((new_mtu < 68) || (new_mtu > hardif_min_mtu()))
157 return -EINVAL;
159 dev->mtu = new_mtu;
161 return 0;
164 int interface_tx(struct sk_buff *skb, struct net_device *dev)
166 struct unicast_packet *unicast_packet;
167 struct bcast_packet *bcast_packet;
168 struct orig_node *orig_node;
169 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
170 struct bat_priv *priv = netdev_priv(dev);
171 struct batman_if *batman_if;
172 uint8_t dstaddr[6];
173 int data_len = skb->len;
174 unsigned long flags;
176 if (atomic_read(&module_state) != MODULE_ACTIVE)
177 goto dropped;
179 dev->trans_start = jiffies;
180 /* TODO: check this for locks */
181 hna_local_add(ethhdr->h_source);
183 /* ethernet packet should be broadcasted */
184 if (is_bcast(ethhdr->h_dest) || is_mcast(ethhdr->h_dest)) {
186 if (my_skb_push(skb, sizeof(struct bcast_packet)) < 0)
187 goto dropped;
189 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);
199 /* set broadcast sequence number */
200 bcast_packet->seqno = htons(bcast_seqno);
202 bcast_seqno++;
204 /* broadcast packet */
205 add_bcast_packet_to_list(skb);
206 /* a copy is stored in the bcast list, therefore removing
207 * the original skb. */
208 kfree_skb(skb);
210 /* unicast packet */
211 } else {
212 spin_lock_irqsave(&orig_hash_lock, flags);
213 /* get routing information */
214 orig_node = ((struct orig_node *)hash_find(orig_hash,
215 ethhdr->h_dest));
217 /* check for hna host */
218 if (!orig_node)
219 orig_node = transtable_search(ethhdr->h_dest);
221 if ((orig_node) &&
222 (orig_node->batman_if) &&
223 (orig_node->router)) {
224 if (my_skb_push(skb, sizeof(struct unicast_packet)) < 0)
225 goto unlock;
227 unicast_packet = (struct unicast_packet *)skb->data;
229 unicast_packet->version = COMPAT_VERSION;
230 /* batman packet type: unicast */
231 unicast_packet->packet_type = BAT_UNICAST;
232 /* set unicast ttl */
233 unicast_packet->ttl = TTL;
234 /* copy the destination for faster routing */
235 memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
237 /* net_dev won't be available when not active */
238 if (orig_node->batman_if->if_active != IF_ACTIVE)
239 goto unlock;
241 /* don't lock while sending the packets ... we therefore
242 * copy the required data before sending */
244 batman_if = orig_node->batman_if;
245 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
246 spin_unlock_irqrestore(&orig_hash_lock, flags);
248 send_skb_packet(skb, batman_if, dstaddr);
249 } else {
250 goto unlock;
254 priv->stats.tx_packets++;
255 priv->stats.tx_bytes += data_len;
256 goto end;
258 unlock:
259 spin_unlock_irqrestore(&orig_hash_lock, flags);
260 dropped:
261 priv->stats.tx_dropped++;
262 end:
263 return 0;
266 void interface_rx(struct sk_buff *skb, int hdr_size)
268 struct net_device *dev = soft_device;
269 struct bat_priv *priv = netdev_priv(dev);
271 /* check if enough space is available for pulling, and pull */
272 if (!pskb_may_pull(skb, hdr_size)) {
273 kfree_skb(skb);
274 return;
276 skb_pull_rcsum(skb, hdr_size);
277 /* skb_set_mac_header(skb, -sizeof(struct ethhdr));*/
279 skb->dev = dev;
280 skb->protocol = eth_type_trans(skb, dev);
282 /* should not be neccesary anymore as we use skb_pull_rcsum()
283 * TODO: please verify this and remove this TODO
284 * -- Dec 21st 2009, Simon Wunderlich */
286 /* skb->ip_summed = CHECKSUM_UNNECESSARY;*/
288 /* TODO: set skb->pkt_type to PACKET_BROADCAST, PACKET_MULTICAST,
289 * PACKET_OTHERHOST or PACKET_HOST */
291 priv->stats.rx_packets++;
292 priv->stats.rx_bytes += skb->len;
294 dev->last_rx = jiffies;
296 netif_rx(skb);
299 /* ethtool */
300 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
302 cmd->supported = 0;
303 cmd->advertising = 0;
304 cmd->speed = SPEED_10;
305 cmd->duplex = DUPLEX_FULL;
306 cmd->port = PORT_TP;
307 cmd->phy_address = 0;
308 cmd->transceiver = XCVR_INTERNAL;
309 cmd->autoneg = AUTONEG_DISABLE;
310 cmd->maxtxpkt = 0;
311 cmd->maxrxpkt = 0;
313 return 0;
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)
327 return -EOPNOTSUPP;
330 static void bat_set_msglevel(struct net_device *dev, u32 value)
332 return;
335 static u32 bat_get_link(struct net_device *dev)
337 return 1;
340 static u32 bat_get_rx_csum(struct net_device *dev)
342 return 0;
345 static int bat_set_rx_csum(struct net_device *dev, u32 data)
347 return -EOPNOTSUPP;