• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/drivers/staging/batman-adv/
1/*
2 * Copyright (C) 2007-2010 B.A.T.M.A.N. contributors:
3 *
4 * Marek Lindner, Simon Wunderlich
5 *
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.
9 *
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.
14 *
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
19 *
20 */
21
22#include "main.h"
23#include "soft-interface.h"
24#include "hard-interface.h"
25#include "routing.h"
26#include "send.h"
27#include "translation-table.h"
28#include "types.h"
29#include "hash.h"
30#include <linux/slab.h>
31#include <linux/ethtool.h>
32#include <linux/etherdevice.h>
33
34static uint32_t bcast_seqno = 1; /* give own bcast messages seq numbers to avoid
35				  * broadcast storms */
36static int32_t skb_packets;
37static int32_t skb_bad_packets;
38
39unsigned char main_if_addr[ETH_ALEN];
40static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
41static void bat_get_drvinfo(struct net_device *dev,
42			    struct ethtool_drvinfo *info);
43static u32 bat_get_msglevel(struct net_device *dev);
44static void bat_set_msglevel(struct net_device *dev, u32 value);
45static u32 bat_get_link(struct net_device *dev);
46static u32 bat_get_rx_csum(struct net_device *dev);
47static int bat_set_rx_csum(struct net_device *dev, u32 data);
48
49static 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
57};
58
59void set_main_if_addr(uint8_t *addr)
60{
61	memcpy(main_if_addr, addr, ETH_ALEN);
62}
63
64int my_skb_push(struct sk_buff *skb, unsigned int len)
65{
66	int result = 0;
67
68	skb_packets++;
69	if (skb_headroom(skb) < len) {
70		skb_bad_packets++;
71		result = pskb_expand_head(skb, len, 0, GFP_ATOMIC);
72
73		if (result < 0)
74			return result;
75	}
76
77	skb_push(skb, len);
78	return 0;
79}
80
81static int interface_open(struct net_device *dev)
82{
83	netif_start_queue(dev);
84	return 0;
85}
86
87static int interface_release(struct net_device *dev)
88{
89	netif_stop_queue(dev);
90	return 0;
91}
92
93static struct net_device_stats *interface_stats(struct net_device *dev)
94{
95	struct bat_priv *priv = netdev_priv(dev);
96	return &priv->stats;
97}
98
99static int interface_set_mac_addr(struct net_device *dev, void *p)
100{
101	struct sockaddr *addr = p;
102
103	if (!is_valid_ether_addr(addr->sa_data))
104		return -EADDRNOTAVAIL;
105
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);
110	}
111
112	memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
113
114	return 0;
115}
116
117static int interface_change_mtu(struct net_device *dev, int new_mtu)
118{
119	/* check ranges */
120	if ((new_mtu < 68) || (new_mtu > hardif_min_mtu()))
121		return -EINVAL;
122
123	dev->mtu = new_mtu;
124
125	return 0;
126}
127
128int interface_tx(struct sk_buff *skb, struct net_device *dev)
129{
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;
138	uint8_t dstaddr[6];
139	int data_len = skb->len;
140	unsigned long flags;
141
142	if (atomic_read(&module_state) != MODULE_ACTIVE)
143		goto dropped;
144
145	bat_priv = netdev_priv(soft_device);
146
147	dev->trans_start = jiffies;
148	/* TODO: check this for locks */
149	hna_local_add(ethhdr->h_source);
150
151	/* ethernet packet should be broadcasted */
152	if (is_bcast(ethhdr->h_dest) || is_mcast(ethhdr->h_dest)) {
153
154		if (my_skb_push(skb, sizeof(struct bcast_packet)) < 0)
155			goto dropped;
156
157		bcast_packet = (struct bcast_packet *)skb->data;
158		bcast_packet->version = COMPAT_VERSION;
159		bcast_packet->ttl = TTL;
160
161		/* batman packet type: broadcast */
162		bcast_packet->packet_type = BAT_BCAST;
163
164		/* hw address of first interface is the orig mac because only
165		 * this mac is known throughout the mesh */
166		memcpy(bcast_packet->orig, main_if_addr, ETH_ALEN);
167
168		/* set broadcast sequence number */
169		bcast_packet->seqno = htonl(bcast_seqno);
170
171		/* broadcast packet. on success, increase seqno. */
172		if (add_bcast_packet_to_list(skb) == NETDEV_TX_OK)
173			bcast_seqno++;
174
175		/* a copy is stored in the bcast list, therefore removing
176		 * the original skb. */
177		kfree_skb(skb);
178
179	/* unicast packet */
180	} else {
181		spin_lock_irqsave(&orig_hash_lock, flags);
182		/* get routing information */
183		orig_node = ((struct orig_node *)hash_find(orig_hash,
184							   ethhdr->h_dest));
185
186		/* check for hna host */
187		if (!orig_node)
188			orig_node = transtable_search(ethhdr->h_dest);
189
190		router = find_router(orig_node, NULL);
191
192		if (!router)
193			goto unlock;
194
195		/* don't lock while sending the packets ... we therefore
196		 * copy the required data before sending */
197
198		batman_if = router->if_incoming;
199		memcpy(dstaddr, router->addr, ETH_ALEN);
200
201		spin_unlock_irqrestore(&orig_hash_lock, flags);
202
203		if (batman_if->if_status != IF_ACTIVE)
204			goto dropped;
205
206		if (my_skb_push(skb, sizeof(struct unicast_packet)) < 0)
207			goto dropped;
208
209		unicast_packet = (struct unicast_packet *)skb->data;
210
211		unicast_packet->version = COMPAT_VERSION;
212		/* batman packet type: unicast */
213		unicast_packet->packet_type = BAT_UNICAST;
214		/* set unicast ttl */
215		unicast_packet->ttl = TTL;
216		/* copy the destination for faster routing */
217		memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
218
219		send_skb_packet(skb, batman_if, dstaddr);
220	}
221
222	priv->stats.tx_packets++;
223	priv->stats.tx_bytes += data_len;
224	goto end;
225
226unlock:
227	spin_unlock_irqrestore(&orig_hash_lock, flags);
228dropped:
229	priv->stats.tx_dropped++;
230	kfree_skb(skb);
231end:
232	return NETDEV_TX_OK;
233}
234
235void interface_rx(struct sk_buff *skb, int hdr_size)
236{
237	struct net_device *dev = soft_device;
238	struct bat_priv *priv = netdev_priv(dev);
239
240	/* check if enough space is available for pulling, and pull */
241	if (!pskb_may_pull(skb, hdr_size)) {
242		kfree_skb(skb);
243		return;
244	}
245	skb_pull_rcsum(skb, hdr_size);
246/*	skb_set_mac_header(skb, -sizeof(struct ethhdr));*/
247
248	if (unlikely(!pskb_may_pull(skb, ETH_HLEN))) {
249		kfree_skb(skb);
250		return;
251	}
252	skb->dev = dev;
253	skb->protocol = eth_type_trans(skb, dev);
254
255	/* should not be neccesary anymore as we use skb_pull_rcsum()
256	 * TODO: please verify this and remove this TODO
257	 * -- Dec 21st 2009, Simon Wunderlich */
258
259/*	skb->ip_summed = CHECKSUM_UNNECESSARY;*/
260
261	/* TODO: set skb->pkt_type to PACKET_BROADCAST, PACKET_MULTICAST,
262	 * PACKET_OTHERHOST or PACKET_HOST */
263
264	priv->stats.rx_packets++;
265	priv->stats.rx_bytes += skb->len;
266
267	dev->last_rx = jiffies;
268
269	netif_rx(skb);
270}
271
272#ifdef HAVE_NET_DEVICE_OPS
273static const struct net_device_ops bat_netdev_ops = {
274	.ndo_open = interface_open,
275	.ndo_stop = interface_release,
276	.ndo_get_stats = interface_stats,
277	.ndo_set_mac_address = interface_set_mac_addr,
278	.ndo_change_mtu = interface_change_mtu,
279	.ndo_start_xmit = interface_tx,
280	.ndo_validate_addr = eth_validate_addr
281};
282#endif
283
284void interface_setup(struct net_device *dev)
285{
286	struct bat_priv *priv = netdev_priv(dev);
287	char dev_addr[ETH_ALEN];
288
289	ether_setup(dev);
290
291#ifdef HAVE_NET_DEVICE_OPS
292	dev->netdev_ops = &bat_netdev_ops;
293#else
294	dev->open = interface_open;
295	dev->stop = interface_release;
296	dev->get_stats = interface_stats;
297	dev->set_mac_address = interface_set_mac_addr;
298	dev->change_mtu = interface_change_mtu;
299	dev->hard_start_xmit = interface_tx;
300#endif
301	dev->destructor = free_netdev;
302
303	dev->mtu = hardif_min_mtu();
304	dev->hard_header_len = BAT_HEADER_LEN; /* reserve more space in the
305						* skbuff for our header */
306
307	/* generate random address */
308	random_ether_addr(dev_addr);
309	memcpy(dev->dev_addr, dev_addr, ETH_ALEN);
310
311	SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
312
313	memset(priv, 0, sizeof(struct bat_priv));
314}
315
316/* ethtool */
317static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
318{
319	cmd->supported = 0;
320	cmd->advertising = 0;
321	cmd->speed = SPEED_10;
322	cmd->duplex = DUPLEX_FULL;
323	cmd->port = PORT_TP;
324	cmd->phy_address = 0;
325	cmd->transceiver = XCVR_INTERNAL;
326	cmd->autoneg = AUTONEG_DISABLE;
327	cmd->maxtxpkt = 0;
328	cmd->maxrxpkt = 0;
329
330	return 0;
331}
332
333static void bat_get_drvinfo(struct net_device *dev,
334			    struct ethtool_drvinfo *info)
335{
336	strcpy(info->driver, "B.A.T.M.A.N. advanced");
337	strcpy(info->version, SOURCE_VERSION);
338	strcpy(info->fw_version, "N/A");
339	strcpy(info->bus_info, "batman");
340}
341
342static u32 bat_get_msglevel(struct net_device *dev)
343{
344	return -EOPNOTSUPP;
345}
346
347static void bat_set_msglevel(struct net_device *dev, u32 value)
348{
349}
350
351static u32 bat_get_link(struct net_device *dev)
352{
353	return 1;
354}
355
356static u32 bat_get_rx_csum(struct net_device *dev)
357{
358	return 0;
359}
360
361static int bat_set_rx_csum(struct net_device *dev, u32 data)
362{
363	return -EOPNOTSUPP;
364}
365