2 * Copyright (C) 2007-2012 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 "bat_debugfs.h"
28 #include "translation-table.h"
30 #include "gateway_common.h"
31 #include "gateway_client.h"
32 #include "bat_sysfs.h"
33 #include "originator.h"
34 #include <linux/slab.h>
35 #include <linux/ethtool.h>
36 #include <linux/etherdevice.h>
37 #include <linux/if_vlan.h>
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);
48 static const struct ethtool_ops bat_ethtool_ops = {
49 .get_settings = bat_get_settings,
50 .get_drvinfo = bat_get_drvinfo,
51 .get_msglevel = bat_get_msglevel,
52 .set_msglevel = bat_set_msglevel,
53 .get_link = bat_get_link,
56 int my_skb_head_push(struct sk_buff *skb, unsigned int len)
61 * TODO: We must check if we can release all references to non-payload
62 * data using skb_header_release in our skbs to allow skb_cow_header to
63 * work optimally. This means that those skbs are not allowed to read
64 * or write any data which is before the current position of skb->data
65 * after that call and thus allow other skbs with the same data buffer
66 * to write freely in that area.
68 result = skb_cow_head(skb, len);
76 static int interface_open(struct net_device *dev)
78 netif_start_queue(dev);
82 static int interface_release(struct net_device *dev)
84 netif_stop_queue(dev);
88 static struct net_device_stats *interface_stats(struct net_device *dev)
90 struct bat_priv *bat_priv = netdev_priv(dev);
91 return &bat_priv->stats;
94 static int interface_set_mac_addr(struct net_device *dev, void *p)
96 struct bat_priv *bat_priv = netdev_priv(dev);
97 struct sockaddr *addr = p;
99 if (!is_valid_ether_addr(addr->sa_data))
100 return -EADDRNOTAVAIL;
102 /* only modify transtable if it has been initialized before */
103 if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) {
104 tt_local_remove(bat_priv, dev->dev_addr,
105 "mac address changed", false);
106 tt_local_add(dev, addr->sa_data, NULL_IFINDEX);
109 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
110 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
114 static int interface_change_mtu(struct net_device *dev, int new_mtu)
117 if ((new_mtu < 68) || (new_mtu > hardif_min_mtu(dev)))
125 static int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
127 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
128 struct bat_priv *bat_priv = netdev_priv(soft_iface);
129 struct hard_iface *primary_if = NULL;
130 struct bcast_packet *bcast_packet;
131 struct vlan_ethhdr *vhdr;
132 unsigned int header_len = 0;
133 int data_len = skb->len, ret;
135 bool do_bcast = false;
137 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
140 soft_iface->trans_start = jiffies;
142 switch (ntohs(ethhdr->h_proto)) {
144 vhdr = (struct vlan_ethhdr *)skb->data;
145 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
147 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
155 /* Register the client MAC in the transtable */
156 tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif);
158 if (is_multicast_ether_addr(ethhdr->h_dest)) {
161 switch (atomic_read(&bat_priv->gw_mode)) {
163 /* gateway servers should not send dhcp
164 * requests into the mesh */
165 ret = gw_is_dhcp_target(skb, &header_len);
170 /* gateway clients should send dhcp requests
171 * via unicast to their gateway */
172 ret = gw_is_dhcp_target(skb, &header_len);
182 /* ethernet packet should be broadcasted */
184 primary_if = primary_if_get_selected(bat_priv);
188 if (my_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
191 bcast_packet = (struct bcast_packet *)skb->data;
192 bcast_packet->header.version = COMPAT_VERSION;
193 bcast_packet->header.ttl = TTL;
195 /* batman packet type: broadcast */
196 bcast_packet->header.packet_type = BAT_BCAST;
198 /* hw address of first interface is the orig mac because only
199 * this mac is known throughout the mesh */
200 memcpy(bcast_packet->orig,
201 primary_if->net_dev->dev_addr, ETH_ALEN);
203 /* set broadcast sequence number */
204 bcast_packet->seqno =
205 htonl(atomic_inc_return(&bat_priv->bcast_seqno));
207 add_bcast_packet_to_list(bat_priv, skb, 1);
209 /* a copy is stored in the bcast list, therefore removing
210 * the original skb. */
215 if (atomic_read(&bat_priv->gw_mode) != GW_MODE_OFF) {
216 ret = gw_out_of_range(bat_priv, skb, ethhdr);
221 ret = unicast_send_skb(skb, bat_priv);
226 bat_priv->stats.tx_packets++;
227 bat_priv->stats.tx_bytes += data_len;
233 bat_priv->stats.tx_dropped++;
236 hardif_free_ref(primary_if);
240 void interface_rx(struct net_device *soft_iface,
241 struct sk_buff *skb, struct hard_iface *recv_if,
244 struct bat_priv *bat_priv = netdev_priv(soft_iface);
245 struct ethhdr *ethhdr;
246 struct vlan_ethhdr *vhdr;
249 /* check if enough space is available for pulling, and pull */
250 if (!pskb_may_pull(skb, hdr_size))
253 skb_pull_rcsum(skb, hdr_size);
254 skb_reset_mac_header(skb);
256 ethhdr = (struct ethhdr *)skb_mac_header(skb);
258 switch (ntohs(ethhdr->h_proto)) {
260 vhdr = (struct vlan_ethhdr *)skb->data;
261 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
263 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
271 /* skb->dev & skb->pkt_type are set here */
272 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
274 skb->protocol = eth_type_trans(skb, soft_iface);
276 /* should not be necessary anymore as we use skb_pull_rcsum()
277 * TODO: please verify this and remove this TODO
278 * -- Dec 21st 2009, Simon Wunderlich */
280 /* skb->ip_summed = CHECKSUM_UNNECESSARY;*/
282 bat_priv->stats.rx_packets++;
283 bat_priv->stats.rx_bytes += skb->len + sizeof(struct ethhdr);
285 soft_iface->last_rx = jiffies;
287 if (is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest))
299 static const struct net_device_ops bat_netdev_ops = {
300 .ndo_open = interface_open,
301 .ndo_stop = interface_release,
302 .ndo_get_stats = interface_stats,
303 .ndo_set_mac_address = interface_set_mac_addr,
304 .ndo_change_mtu = interface_change_mtu,
305 .ndo_start_xmit = interface_tx,
306 .ndo_validate_addr = eth_validate_addr
309 static void interface_setup(struct net_device *dev)
311 struct bat_priv *priv = netdev_priv(dev);
315 dev->netdev_ops = &bat_netdev_ops;
316 dev->destructor = free_netdev;
317 dev->tx_queue_len = 0;
320 * can't call min_mtu, because the needed variables
321 * have not been initialized yet
323 dev->mtu = ETH_DATA_LEN;
324 /* reserve more space in the skbuff for our header */
325 dev->hard_header_len = BAT_HEADER_LEN;
327 /* generate random address */
328 eth_hw_addr_random(dev);
330 SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
332 memset(priv, 0, sizeof(*priv));
335 struct net_device *softif_create(const char *name)
337 struct net_device *soft_iface;
338 struct bat_priv *bat_priv;
341 soft_iface = alloc_netdev(sizeof(*bat_priv), name, interface_setup);
346 ret = register_netdevice(soft_iface);
348 pr_err("Unable to register the batman interface '%s': %i\n",
350 goto free_soft_iface;
353 bat_priv = netdev_priv(soft_iface);
355 atomic_set(&bat_priv->aggregated_ogms, 1);
356 atomic_set(&bat_priv->bonding, 0);
357 atomic_set(&bat_priv->ap_isolation, 0);
358 atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE);
359 atomic_set(&bat_priv->gw_mode, GW_MODE_OFF);
360 atomic_set(&bat_priv->gw_sel_class, 20);
361 atomic_set(&bat_priv->gw_bandwidth, 41);
362 atomic_set(&bat_priv->orig_interval, 1000);
363 atomic_set(&bat_priv->hop_penalty, 30);
364 atomic_set(&bat_priv->log_level, 0);
365 atomic_set(&bat_priv->fragmentation, 1);
366 atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN);
367 atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN);
369 atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
370 atomic_set(&bat_priv->bcast_seqno, 1);
371 atomic_set(&bat_priv->ttvn, 0);
372 atomic_set(&bat_priv->tt_local_changes, 0);
373 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
375 bat_priv->tt_buff = NULL;
376 bat_priv->tt_buff_len = 0;
377 bat_priv->tt_poss_change = false;
379 bat_priv->primary_if = NULL;
380 bat_priv->num_ifaces = 0;
382 ret = bat_algo_select(bat_priv, bat_routing_algo);
384 goto unreg_soft_iface;
386 ret = sysfs_add_meshif(soft_iface);
388 goto unreg_soft_iface;
390 ret = debugfs_add_meshif(soft_iface);
394 ret = mesh_init(soft_iface);
401 debugfs_del_meshif(soft_iface);
403 sysfs_del_meshif(soft_iface);
405 unregister_netdevice(soft_iface);
409 free_netdev(soft_iface);
414 void softif_destroy(struct net_device *soft_iface)
416 debugfs_del_meshif(soft_iface);
417 sysfs_del_meshif(soft_iface);
418 mesh_free(soft_iface);
419 unregister_netdevice(soft_iface);
422 int softif_is_valid(const struct net_device *net_dev)
424 if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
431 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
434 cmd->advertising = 0;
435 ethtool_cmd_speed_set(cmd, SPEED_10);
436 cmd->duplex = DUPLEX_FULL;
438 cmd->phy_address = 0;
439 cmd->transceiver = XCVR_INTERNAL;
440 cmd->autoneg = AUTONEG_DISABLE;
447 static void bat_get_drvinfo(struct net_device *dev,
448 struct ethtool_drvinfo *info)
450 strcpy(info->driver, "B.A.T.M.A.N. advanced");
451 strcpy(info->version, SOURCE_VERSION);
452 strcpy(info->fw_version, "N/A");
453 strcpy(info->bus_info, "batman");
456 static u32 bat_get_msglevel(struct net_device *dev)
461 static void bat_set_msglevel(struct net_device *dev, u32 value)
465 static u32 bat_get_link(struct net_device *dev)