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 "bat_debugfs.h"
28 #include "translation-table.h"
32 #include "bat_sysfs.h"
33 #include <linux/slab.h>
34 #include <linux/ethtool.h>
35 #include <linux/etherdevice.h>
36 #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);
47 static u32 bat_get_rx_csum(struct net_device *dev);
48 static int bat_set_rx_csum(struct net_device *dev, u32 data);
50 static const struct ethtool_ops bat_ethtool_ops = {
51 .get_settings = bat_get_settings,
52 .get_drvinfo = bat_get_drvinfo,
53 .get_msglevel = bat_get_msglevel,
54 .set_msglevel = bat_set_msglevel,
55 .get_link = bat_get_link,
56 .get_rx_csum = bat_get_rx_csum,
57 .set_rx_csum = bat_set_rx_csum
60 int my_skb_head_push(struct sk_buff *skb, unsigned int len)
65 * TODO: We must check if we can release all references to non-payload
66 * data using skb_header_release in our skbs to allow skb_cow_header to
67 * work optimally. This means that those skbs are not allowed to read
68 * or write any data which is before the current position of skb->data
69 * after that call and thus allow other skbs with the same data buffer
70 * to write freely in that area.
72 result = skb_cow_head(skb, len);
80 static void softif_neigh_free_ref(struct kref *refcount)
82 struct softif_neigh *softif_neigh;
84 softif_neigh = container_of(refcount, struct softif_neigh, refcount);
88 static void softif_neigh_free_rcu(struct rcu_head *rcu)
90 struct softif_neigh *softif_neigh;
92 softif_neigh = container_of(rcu, struct softif_neigh, rcu);
93 kref_put(&softif_neigh->refcount, softif_neigh_free_ref);
96 void softif_neigh_purge(struct bat_priv *bat_priv)
98 struct softif_neigh *softif_neigh, *softif_neigh_tmp;
99 struct hlist_node *node, *node_tmp;
101 spin_lock_bh(&bat_priv->softif_neigh_lock);
103 hlist_for_each_entry_safe(softif_neigh, node, node_tmp,
104 &bat_priv->softif_neigh_list, list) {
106 if ((!time_after(jiffies, softif_neigh->last_seen +
107 msecs_to_jiffies(SOFTIF_NEIGH_TIMEOUT))) &&
108 (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE))
111 hlist_del_rcu(&softif_neigh->list);
113 if (bat_priv->softif_neigh == softif_neigh) {
114 bat_dbg(DBG_ROUTES, bat_priv,
115 "Current mesh exit point '%pM' vanished "
117 softif_neigh->addr, softif_neigh->vid);
118 softif_neigh_tmp = bat_priv->softif_neigh;
119 bat_priv->softif_neigh = NULL;
120 kref_put(&softif_neigh_tmp->refcount,
121 softif_neigh_free_ref);
124 call_rcu(&softif_neigh->rcu, softif_neigh_free_rcu);
127 spin_unlock_bh(&bat_priv->softif_neigh_lock);
130 static struct softif_neigh *softif_neigh_get(struct bat_priv *bat_priv,
131 uint8_t *addr, short vid)
133 struct softif_neigh *softif_neigh;
134 struct hlist_node *node;
137 hlist_for_each_entry_rcu(softif_neigh, node,
138 &bat_priv->softif_neigh_list, list) {
139 if (memcmp(softif_neigh->addr, addr, ETH_ALEN) != 0)
142 if (softif_neigh->vid != vid)
145 softif_neigh->last_seen = jiffies;
149 softif_neigh = kzalloc(sizeof(struct softif_neigh), GFP_ATOMIC);
153 memcpy(softif_neigh->addr, addr, ETH_ALEN);
154 softif_neigh->vid = vid;
155 softif_neigh->last_seen = jiffies;
156 kref_init(&softif_neigh->refcount);
158 INIT_HLIST_NODE(&softif_neigh->list);
159 spin_lock_bh(&bat_priv->softif_neigh_lock);
160 hlist_add_head_rcu(&softif_neigh->list, &bat_priv->softif_neigh_list);
161 spin_unlock_bh(&bat_priv->softif_neigh_lock);
164 kref_get(&softif_neigh->refcount);
170 int softif_neigh_seq_print_text(struct seq_file *seq, void *offset)
172 struct net_device *net_dev = (struct net_device *)seq->private;
173 struct bat_priv *bat_priv = netdev_priv(net_dev);
174 struct softif_neigh *softif_neigh;
175 struct hlist_node *node;
176 size_t buf_size, pos;
179 if (!bat_priv->primary_if) {
180 return seq_printf(seq, "BATMAN mesh %s disabled - "
181 "please specify interfaces to enable it\n",
185 seq_printf(seq, "Softif neighbor list (%s)\n", net_dev->name);
188 /* Estimate length for: " xx:xx:xx:xx:xx:xx\n" */
190 hlist_for_each_entry_rcu(softif_neigh, node,
191 &bat_priv->softif_neigh_list, list)
195 buff = kmalloc(buf_size, GFP_ATOMIC);
203 hlist_for_each_entry_rcu(softif_neigh, node,
204 &bat_priv->softif_neigh_list, list) {
205 pos += snprintf(buff + pos, 31, "%s %pM (vid: %d)\n",
206 bat_priv->softif_neigh == softif_neigh
207 ? "=>" : " ", softif_neigh->addr,
212 seq_printf(seq, "%s", buff);
217 static void softif_batman_recv(struct sk_buff *skb, struct net_device *dev,
220 struct bat_priv *bat_priv = netdev_priv(dev);
221 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
222 struct batman_packet *batman_packet;
223 struct softif_neigh *softif_neigh, *softif_neigh_tmp;
225 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q)
226 batman_packet = (struct batman_packet *)
227 (skb->data + ETH_HLEN + VLAN_HLEN);
229 batman_packet = (struct batman_packet *)(skb->data + ETH_HLEN);
231 if (batman_packet->version != COMPAT_VERSION)
234 if (batman_packet->packet_type != BAT_PACKET)
237 if (!(batman_packet->flags & PRIMARIES_FIRST_HOP))
240 if (is_my_mac(batman_packet->orig))
243 softif_neigh = softif_neigh_get(bat_priv, batman_packet->orig, vid);
248 if (bat_priv->softif_neigh == softif_neigh)
251 /* we got a neighbor but its mac is 'bigger' than ours */
252 if (memcmp(bat_priv->primary_if->net_dev->dev_addr,
253 softif_neigh->addr, ETH_ALEN) < 0)
256 /* switch to new 'smallest neighbor' */
257 if ((bat_priv->softif_neigh) &&
258 (memcmp(softif_neigh->addr, bat_priv->softif_neigh->addr,
260 bat_dbg(DBG_ROUTES, bat_priv,
261 "Changing mesh exit point from %pM (vid: %d) "
262 "to %pM (vid: %d).\n",
263 bat_priv->softif_neigh->addr,
264 bat_priv->softif_neigh->vid,
265 softif_neigh->addr, softif_neigh->vid);
266 softif_neigh_tmp = bat_priv->softif_neigh;
267 bat_priv->softif_neigh = softif_neigh;
268 kref_put(&softif_neigh_tmp->refcount, softif_neigh_free_ref);
269 /* we need to hold the additional reference */
273 /* close own batX device and use softif_neigh as exit node */
274 if ((!bat_priv->softif_neigh) &&
275 (memcmp(softif_neigh->addr,
276 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN) < 0)) {
277 bat_dbg(DBG_ROUTES, bat_priv,
278 "Setting mesh exit point to %pM (vid: %d).\n",
279 softif_neigh->addr, softif_neigh->vid);
280 bat_priv->softif_neigh = softif_neigh;
281 /* we need to hold the additional reference */
286 kref_put(&softif_neigh->refcount, softif_neigh_free_ref);
292 static int interface_open(struct net_device *dev)
294 netif_start_queue(dev);
298 static int interface_release(struct net_device *dev)
300 netif_stop_queue(dev);
304 static struct net_device_stats *interface_stats(struct net_device *dev)
306 struct bat_priv *bat_priv = netdev_priv(dev);
307 return &bat_priv->stats;
310 static int interface_set_mac_addr(struct net_device *dev, void *p)
312 struct bat_priv *bat_priv = netdev_priv(dev);
313 struct sockaddr *addr = p;
315 if (!is_valid_ether_addr(addr->sa_data))
316 return -EADDRNOTAVAIL;
318 /* only modify hna-table if it has been initialised before */
319 if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) {
320 hna_local_remove(bat_priv, dev->dev_addr,
321 "mac address changed");
322 hna_local_add(dev, addr->sa_data);
325 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
329 static int interface_change_mtu(struct net_device *dev, int new_mtu)
332 if ((new_mtu < 68) || (new_mtu > hardif_min_mtu(dev)))
340 int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
342 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
343 struct bat_priv *bat_priv = netdev_priv(soft_iface);
344 struct bcast_packet *bcast_packet;
345 struct vlan_ethhdr *vhdr;
346 int data_len = skb->len, ret;
349 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
352 soft_iface->trans_start = jiffies;
354 switch (ntohs(ethhdr->h_proto)) {
356 vhdr = (struct vlan_ethhdr *)skb->data;
357 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
359 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
364 softif_batman_recv(skb, soft_iface, vid);
369 * if we have a another chosen mesh exit node in range
370 * it will transport the packets to the mesh
372 if ((bat_priv->softif_neigh) && (bat_priv->softif_neigh->vid == vid))
375 /* TODO: check this for locks */
376 hna_local_add(soft_iface, ethhdr->h_source);
378 /* ethernet packet should be broadcasted */
379 if (is_bcast(ethhdr->h_dest) || is_mcast(ethhdr->h_dest)) {
380 if (!bat_priv->primary_if)
383 if (my_skb_head_push(skb, sizeof(struct bcast_packet)) < 0)
386 bcast_packet = (struct bcast_packet *)skb->data;
387 bcast_packet->version = COMPAT_VERSION;
388 bcast_packet->ttl = TTL;
390 /* batman packet type: broadcast */
391 bcast_packet->packet_type = BAT_BCAST;
393 /* hw address of first interface is the orig mac because only
394 * this mac is known throughout the mesh */
395 memcpy(bcast_packet->orig,
396 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
398 /* set broadcast sequence number */
399 bcast_packet->seqno =
400 htonl(atomic_inc_return(&bat_priv->bcast_seqno));
402 add_bcast_packet_to_list(bat_priv, skb);
404 /* a copy is stored in the bcast list, therefore removing
405 * the original skb. */
410 ret = unicast_send_skb(skb, bat_priv);
415 bat_priv->stats.tx_packets++;
416 bat_priv->stats.tx_bytes += data_len;
422 bat_priv->stats.tx_dropped++;
427 void interface_rx(struct net_device *soft_iface,
428 struct sk_buff *skb, struct batman_if *recv_if,
431 struct bat_priv *bat_priv = netdev_priv(soft_iface);
432 struct unicast_packet *unicast_packet;
433 struct ethhdr *ethhdr;
434 struct vlan_ethhdr *vhdr;
438 /* check if enough space is available for pulling, and pull */
439 if (!pskb_may_pull(skb, hdr_size))
442 skb_pull_rcsum(skb, hdr_size);
443 skb_reset_mac_header(skb);
445 ethhdr = (struct ethhdr *)skb_mac_header(skb);
447 switch (ntohs(ethhdr->h_proto)) {
449 vhdr = (struct vlan_ethhdr *)skb->data;
450 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
452 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
461 * if we have a another chosen mesh exit node in range
462 * it will transport the packets to the non-mesh network
464 if ((bat_priv->softif_neigh) && (bat_priv->softif_neigh->vid == vid)) {
465 skb_push(skb, hdr_size);
466 unicast_packet = (struct unicast_packet *)skb->data;
468 if ((unicast_packet->packet_type != BAT_UNICAST) &&
469 (unicast_packet->packet_type != BAT_UNICAST_FRAG))
472 skb_reset_mac_header(skb);
474 memcpy(unicast_packet->dest,
475 bat_priv->softif_neigh->addr, ETH_ALEN);
476 ret = route_unicast_packet(skb, recv_if, hdr_size);
477 if (ret == NET_RX_DROP)
483 /* skb->dev & skb->pkt_type are set here */
484 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
486 skb->protocol = eth_type_trans(skb, soft_iface);
488 /* should not be neccesary anymore as we use skb_pull_rcsum()
489 * TODO: please verify this and remove this TODO
490 * -- Dec 21st 2009, Simon Wunderlich */
492 /* skb->ip_summed = CHECKSUM_UNNECESSARY;*/
494 bat_priv->stats.rx_packets++;
495 bat_priv->stats.rx_bytes += skb->len + sizeof(struct ethhdr);
497 soft_iface->last_rx = jiffies;
508 #ifdef HAVE_NET_DEVICE_OPS
509 static const struct net_device_ops bat_netdev_ops = {
510 .ndo_open = interface_open,
511 .ndo_stop = interface_release,
512 .ndo_get_stats = interface_stats,
513 .ndo_set_mac_address = interface_set_mac_addr,
514 .ndo_change_mtu = interface_change_mtu,
515 .ndo_start_xmit = interface_tx,
516 .ndo_validate_addr = eth_validate_addr
520 static void interface_setup(struct net_device *dev)
522 struct bat_priv *priv = netdev_priv(dev);
523 char dev_addr[ETH_ALEN];
527 #ifdef HAVE_NET_DEVICE_OPS
528 dev->netdev_ops = &bat_netdev_ops;
530 dev->open = interface_open;
531 dev->stop = interface_release;
532 dev->get_stats = interface_stats;
533 dev->set_mac_address = interface_set_mac_addr;
534 dev->change_mtu = interface_change_mtu;
535 dev->hard_start_xmit = interface_tx;
537 dev->destructor = free_netdev;
540 * can't call min_mtu, because the needed variables
541 * have not been initialized yet
543 dev->mtu = ETH_DATA_LEN;
544 dev->hard_header_len = BAT_HEADER_LEN; /* reserve more space in the
545 * skbuff for our header */
547 /* generate random address */
548 random_ether_addr(dev_addr);
549 memcpy(dev->dev_addr, dev_addr, ETH_ALEN);
551 SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
553 memset(priv, 0, sizeof(struct bat_priv));
556 struct net_device *softif_create(char *name)
558 struct net_device *soft_iface;
559 struct bat_priv *bat_priv;
562 soft_iface = alloc_netdev(sizeof(struct bat_priv) , name,
566 pr_err("Unable to allocate the batman interface: %s\n", name);
570 ret = register_netdev(soft_iface);
572 pr_err("Unable to register the batman interface '%s': %i\n",
574 goto free_soft_iface;
577 bat_priv = netdev_priv(soft_iface);
579 atomic_set(&bat_priv->aggregated_ogms, 1);
580 atomic_set(&bat_priv->bonding, 0);
581 atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE);
582 atomic_set(&bat_priv->orig_interval, 1000);
583 atomic_set(&bat_priv->hop_penalty, 10);
584 atomic_set(&bat_priv->log_level, 0);
585 atomic_set(&bat_priv->fragmentation, 1);
586 atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN);
587 atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN);
589 atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
590 atomic_set(&bat_priv->bcast_seqno, 1);
591 atomic_set(&bat_priv->hna_local_changed, 0);
593 bat_priv->primary_if = NULL;
594 bat_priv->num_ifaces = 0;
595 bat_priv->softif_neigh = NULL;
597 ret = sysfs_add_meshif(soft_iface);
599 goto unreg_soft_iface;
601 ret = debugfs_add_meshif(soft_iface);
605 ret = mesh_init(soft_iface);
612 debugfs_del_meshif(soft_iface);
614 sysfs_del_meshif(soft_iface);
616 unregister_netdev(soft_iface);
620 free_netdev(soft_iface);
625 void softif_destroy(struct net_device *soft_iface)
627 debugfs_del_meshif(soft_iface);
628 sysfs_del_meshif(soft_iface);
629 mesh_free(soft_iface);
630 unregister_netdevice(soft_iface);
634 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
637 cmd->advertising = 0;
638 cmd->speed = SPEED_10;
639 cmd->duplex = DUPLEX_FULL;
641 cmd->phy_address = 0;
642 cmd->transceiver = XCVR_INTERNAL;
643 cmd->autoneg = AUTONEG_DISABLE;
650 static void bat_get_drvinfo(struct net_device *dev,
651 struct ethtool_drvinfo *info)
653 strcpy(info->driver, "B.A.T.M.A.N. advanced");
654 strcpy(info->version, SOURCE_VERSION);
655 strcpy(info->fw_version, "N/A");
656 strcpy(info->bus_info, "batman");
659 static u32 bat_get_msglevel(struct net_device *dev)
664 static void bat_set_msglevel(struct net_device *dev, u32 value)
668 static u32 bat_get_link(struct net_device *dev)
673 static u32 bat_get_rx_csum(struct net_device *dev)
678 static int bat_set_rx_csum(struct net_device *dev, u32 data)