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"
31 #include "gateway_common.h"
32 #include "gateway_client.h"
34 #include "bat_sysfs.h"
35 #include <linux/slab.h>
36 #include <linux/ethtool.h>
37 #include <linux/etherdevice.h>
38 #include <linux/if_vlan.h>
43 static int bat_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
);
44 static void bat_get_drvinfo(struct net_device
*dev
,
45 struct ethtool_drvinfo
*info
);
46 static u32
bat_get_msglevel(struct net_device
*dev
);
47 static void bat_set_msglevel(struct net_device
*dev
, u32 value
);
48 static u32
bat_get_link(struct net_device
*dev
);
49 static u32
bat_get_rx_csum(struct net_device
*dev
);
50 static int bat_set_rx_csum(struct net_device
*dev
, u32 data
);
52 static const struct ethtool_ops bat_ethtool_ops
= {
53 .get_settings
= bat_get_settings
,
54 .get_drvinfo
= bat_get_drvinfo
,
55 .get_msglevel
= bat_get_msglevel
,
56 .set_msglevel
= bat_set_msglevel
,
57 .get_link
= bat_get_link
,
58 .get_rx_csum
= bat_get_rx_csum
,
59 .set_rx_csum
= bat_set_rx_csum
62 int my_skb_head_push(struct sk_buff
*skb
, unsigned int len
)
67 * TODO: We must check if we can release all references to non-payload
68 * data using skb_header_release in our skbs to allow skb_cow_header to
69 * work optimally. This means that those skbs are not allowed to read
70 * or write any data which is before the current position of skb->data
71 * after that call and thus allow other skbs with the same data buffer
72 * to write freely in that area.
74 result
= skb_cow_head(skb
, len
);
82 static void softif_neigh_free_ref(struct kref
*refcount
)
84 struct softif_neigh
*softif_neigh
;
86 softif_neigh
= container_of(refcount
, struct softif_neigh
, refcount
);
90 static void softif_neigh_free_rcu(struct rcu_head
*rcu
)
92 struct softif_neigh
*softif_neigh
;
94 softif_neigh
= container_of(rcu
, struct softif_neigh
, rcu
);
95 kref_put(&softif_neigh
->refcount
, softif_neigh_free_ref
);
98 void softif_neigh_purge(struct bat_priv
*bat_priv
)
100 struct softif_neigh
*softif_neigh
, *softif_neigh_tmp
;
101 struct hlist_node
*node
, *node_tmp
;
103 spin_lock_bh(&bat_priv
->softif_neigh_lock
);
105 hlist_for_each_entry_safe(softif_neigh
, node
, node_tmp
,
106 &bat_priv
->softif_neigh_list
, list
) {
108 if ((!time_after(jiffies
, softif_neigh
->last_seen
+
109 msecs_to_jiffies(SOFTIF_NEIGH_TIMEOUT
))) &&
110 (atomic_read(&bat_priv
->mesh_state
) == MESH_ACTIVE
))
113 hlist_del_rcu(&softif_neigh
->list
);
115 if (bat_priv
->softif_neigh
== softif_neigh
) {
116 bat_dbg(DBG_ROUTES
, bat_priv
,
117 "Current mesh exit point '%pM' vanished "
119 softif_neigh
->addr
, softif_neigh
->vid
);
120 softif_neigh_tmp
= bat_priv
->softif_neigh
;
121 bat_priv
->softif_neigh
= NULL
;
122 kref_put(&softif_neigh_tmp
->refcount
,
123 softif_neigh_free_ref
);
126 call_rcu(&softif_neigh
->rcu
, softif_neigh_free_rcu
);
129 spin_unlock_bh(&bat_priv
->softif_neigh_lock
);
132 static struct softif_neigh
*softif_neigh_get(struct bat_priv
*bat_priv
,
133 uint8_t *addr
, short vid
)
135 struct softif_neigh
*softif_neigh
;
136 struct hlist_node
*node
;
139 hlist_for_each_entry_rcu(softif_neigh
, node
,
140 &bat_priv
->softif_neigh_list
, list
) {
141 if (memcmp(softif_neigh
->addr
, addr
, ETH_ALEN
) != 0)
144 if (softif_neigh
->vid
!= vid
)
147 softif_neigh
->last_seen
= jiffies
;
151 softif_neigh
= kzalloc(sizeof(struct softif_neigh
), GFP_ATOMIC
);
155 memcpy(softif_neigh
->addr
, addr
, ETH_ALEN
);
156 softif_neigh
->vid
= vid
;
157 softif_neigh
->last_seen
= jiffies
;
158 kref_init(&softif_neigh
->refcount
);
160 INIT_HLIST_NODE(&softif_neigh
->list
);
161 spin_lock_bh(&bat_priv
->softif_neigh_lock
);
162 hlist_add_head_rcu(&softif_neigh
->list
, &bat_priv
->softif_neigh_list
);
163 spin_unlock_bh(&bat_priv
->softif_neigh_lock
);
166 kref_get(&softif_neigh
->refcount
);
172 int softif_neigh_seq_print_text(struct seq_file
*seq
, void *offset
)
174 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
175 struct bat_priv
*bat_priv
= netdev_priv(net_dev
);
176 struct softif_neigh
*softif_neigh
;
177 struct hlist_node
*node
;
178 size_t buf_size
, pos
;
181 if (!bat_priv
->primary_if
) {
182 return seq_printf(seq
, "BATMAN mesh %s disabled - "
183 "please specify interfaces to enable it\n",
187 seq_printf(seq
, "Softif neighbor list (%s)\n", net_dev
->name
);
190 /* Estimate length for: " xx:xx:xx:xx:xx:xx\n" */
192 hlist_for_each_entry_rcu(softif_neigh
, node
,
193 &bat_priv
->softif_neigh_list
, list
)
197 buff
= kmalloc(buf_size
, GFP_ATOMIC
);
205 hlist_for_each_entry_rcu(softif_neigh
, node
,
206 &bat_priv
->softif_neigh_list
, list
) {
207 pos
+= snprintf(buff
+ pos
, 31, "%s %pM (vid: %d)\n",
208 bat_priv
->softif_neigh
== softif_neigh
209 ? "=>" : " ", softif_neigh
->addr
,
214 seq_printf(seq
, "%s", buff
);
219 static void softif_batman_recv(struct sk_buff
*skb
, struct net_device
*dev
,
222 struct bat_priv
*bat_priv
= netdev_priv(dev
);
223 struct ethhdr
*ethhdr
= (struct ethhdr
*)skb
->data
;
224 struct batman_packet
*batman_packet
;
225 struct softif_neigh
*softif_neigh
, *softif_neigh_tmp
;
227 if (ntohs(ethhdr
->h_proto
) == ETH_P_8021Q
)
228 batman_packet
= (struct batman_packet
*)
229 (skb
->data
+ ETH_HLEN
+ VLAN_HLEN
);
231 batman_packet
= (struct batman_packet
*)(skb
->data
+ ETH_HLEN
);
233 if (batman_packet
->version
!= COMPAT_VERSION
)
236 if (batman_packet
->packet_type
!= BAT_PACKET
)
239 if (!(batman_packet
->flags
& PRIMARIES_FIRST_HOP
))
242 if (is_my_mac(batman_packet
->orig
))
245 softif_neigh
= softif_neigh_get(bat_priv
, batman_packet
->orig
, vid
);
250 if (bat_priv
->softif_neigh
== softif_neigh
)
253 /* we got a neighbor but its mac is 'bigger' than ours */
254 if (memcmp(bat_priv
->primary_if
->net_dev
->dev_addr
,
255 softif_neigh
->addr
, ETH_ALEN
) < 0)
258 /* switch to new 'smallest neighbor' */
259 if ((bat_priv
->softif_neigh
) &&
260 (memcmp(softif_neigh
->addr
, bat_priv
->softif_neigh
->addr
,
262 bat_dbg(DBG_ROUTES
, bat_priv
,
263 "Changing mesh exit point from %pM (vid: %d) "
264 "to %pM (vid: %d).\n",
265 bat_priv
->softif_neigh
->addr
,
266 bat_priv
->softif_neigh
->vid
,
267 softif_neigh
->addr
, softif_neigh
->vid
);
268 softif_neigh_tmp
= bat_priv
->softif_neigh
;
269 bat_priv
->softif_neigh
= softif_neigh
;
270 kref_put(&softif_neigh_tmp
->refcount
, softif_neigh_free_ref
);
271 /* we need to hold the additional reference */
275 /* close own batX device and use softif_neigh as exit node */
276 if ((!bat_priv
->softif_neigh
) &&
277 (memcmp(softif_neigh
->addr
,
278 bat_priv
->primary_if
->net_dev
->dev_addr
, ETH_ALEN
) < 0)) {
279 bat_dbg(DBG_ROUTES
, bat_priv
,
280 "Setting mesh exit point to %pM (vid: %d).\n",
281 softif_neigh
->addr
, softif_neigh
->vid
);
282 bat_priv
->softif_neigh
= softif_neigh
;
283 /* we need to hold the additional reference */
288 kref_put(&softif_neigh
->refcount
, softif_neigh_free_ref
);
294 static int interface_open(struct net_device
*dev
)
296 netif_start_queue(dev
);
300 static int interface_release(struct net_device
*dev
)
302 netif_stop_queue(dev
);
306 static struct net_device_stats
*interface_stats(struct net_device
*dev
)
308 struct bat_priv
*bat_priv
= netdev_priv(dev
);
309 return &bat_priv
->stats
;
312 static int interface_set_mac_addr(struct net_device
*dev
, void *p
)
314 struct bat_priv
*bat_priv
= netdev_priv(dev
);
315 struct sockaddr
*addr
= p
;
317 if (!is_valid_ether_addr(addr
->sa_data
))
318 return -EADDRNOTAVAIL
;
320 /* only modify hna-table if it has been initialised before */
321 if (atomic_read(&bat_priv
->mesh_state
) == MESH_ACTIVE
) {
322 hna_local_remove(bat_priv
, dev
->dev_addr
,
323 "mac address changed");
324 hna_local_add(dev
, addr
->sa_data
);
327 memcpy(dev
->dev_addr
, addr
->sa_data
, ETH_ALEN
);
331 static int interface_change_mtu(struct net_device
*dev
, int new_mtu
)
334 if ((new_mtu
< 68) || (new_mtu
> hardif_min_mtu(dev
)))
342 int interface_tx(struct sk_buff
*skb
, struct net_device
*soft_iface
)
344 struct ethhdr
*ethhdr
= (struct ethhdr
*)skb
->data
;
345 struct bat_priv
*bat_priv
= netdev_priv(soft_iface
);
346 struct bcast_packet
*bcast_packet
;
347 struct vlan_ethhdr
*vhdr
;
348 int data_len
= skb
->len
, ret
;
350 bool do_bcast
= false;
352 if (atomic_read(&bat_priv
->mesh_state
) != MESH_ACTIVE
)
355 soft_iface
->trans_start
= jiffies
;
357 switch (ntohs(ethhdr
->h_proto
)) {
359 vhdr
= (struct vlan_ethhdr
*)skb
->data
;
360 vid
= ntohs(vhdr
->h_vlan_TCI
) & VLAN_VID_MASK
;
362 if (ntohs(vhdr
->h_vlan_encapsulated_proto
) != ETH_P_BATMAN
)
367 softif_batman_recv(skb
, soft_iface
, vid
);
372 * if we have a another chosen mesh exit node in range
373 * it will transport the packets to the mesh
375 if ((bat_priv
->softif_neigh
) && (bat_priv
->softif_neigh
->vid
== vid
))
378 /* TODO: check this for locks */
379 hna_local_add(soft_iface
, ethhdr
->h_source
);
381 if (is_multicast_ether_addr(ethhdr
->h_dest
)) {
382 ret
= gw_is_target(bat_priv
, skb
);
391 /* ethernet packet should be broadcasted */
393 if (!bat_priv
->primary_if
)
396 if (my_skb_head_push(skb
, sizeof(struct bcast_packet
)) < 0)
399 bcast_packet
= (struct bcast_packet
*)skb
->data
;
400 bcast_packet
->version
= COMPAT_VERSION
;
401 bcast_packet
->ttl
= TTL
;
403 /* batman packet type: broadcast */
404 bcast_packet
->packet_type
= BAT_BCAST
;
406 /* hw address of first interface is the orig mac because only
407 * this mac is known throughout the mesh */
408 memcpy(bcast_packet
->orig
,
409 bat_priv
->primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
411 /* set broadcast sequence number */
412 bcast_packet
->seqno
=
413 htonl(atomic_inc_return(&bat_priv
->bcast_seqno
));
415 add_bcast_packet_to_list(bat_priv
, skb
);
417 /* a copy is stored in the bcast list, therefore removing
418 * the original skb. */
423 ret
= unicast_send_skb(skb
, bat_priv
);
428 bat_priv
->stats
.tx_packets
++;
429 bat_priv
->stats
.tx_bytes
+= data_len
;
435 bat_priv
->stats
.tx_dropped
++;
440 void interface_rx(struct net_device
*soft_iface
,
441 struct sk_buff
*skb
, struct batman_if
*recv_if
,
444 struct bat_priv
*bat_priv
= netdev_priv(soft_iface
);
445 struct unicast_packet
*unicast_packet
;
446 struct ethhdr
*ethhdr
;
447 struct vlan_ethhdr
*vhdr
;
451 /* check if enough space is available for pulling, and pull */
452 if (!pskb_may_pull(skb
, hdr_size
))
455 skb_pull_rcsum(skb
, hdr_size
);
456 skb_reset_mac_header(skb
);
458 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
460 switch (ntohs(ethhdr
->h_proto
)) {
462 vhdr
= (struct vlan_ethhdr
*)skb
->data
;
463 vid
= ntohs(vhdr
->h_vlan_TCI
) & VLAN_VID_MASK
;
465 if (ntohs(vhdr
->h_vlan_encapsulated_proto
) != ETH_P_BATMAN
)
474 * if we have a another chosen mesh exit node in range
475 * it will transport the packets to the non-mesh network
477 if ((bat_priv
->softif_neigh
) && (bat_priv
->softif_neigh
->vid
== vid
)) {
478 skb_push(skb
, hdr_size
);
479 unicast_packet
= (struct unicast_packet
*)skb
->data
;
481 if ((unicast_packet
->packet_type
!= BAT_UNICAST
) &&
482 (unicast_packet
->packet_type
!= BAT_UNICAST_FRAG
))
485 skb_reset_mac_header(skb
);
487 memcpy(unicast_packet
->dest
,
488 bat_priv
->softif_neigh
->addr
, ETH_ALEN
);
489 ret
= route_unicast_packet(skb
, recv_if
, hdr_size
);
490 if (ret
== NET_RX_DROP
)
496 /* skb->dev & skb->pkt_type are set here */
497 if (unlikely(!pskb_may_pull(skb
, ETH_HLEN
)))
499 skb
->protocol
= eth_type_trans(skb
, soft_iface
);
501 /* should not be neccesary anymore as we use skb_pull_rcsum()
502 * TODO: please verify this and remove this TODO
503 * -- Dec 21st 2009, Simon Wunderlich */
505 /* skb->ip_summed = CHECKSUM_UNNECESSARY;*/
507 bat_priv
->stats
.rx_packets
++;
508 bat_priv
->stats
.rx_bytes
+= skb
->len
+ sizeof(struct ethhdr
);
510 soft_iface
->last_rx
= jiffies
;
521 #ifdef HAVE_NET_DEVICE_OPS
522 static const struct net_device_ops bat_netdev_ops
= {
523 .ndo_open
= interface_open
,
524 .ndo_stop
= interface_release
,
525 .ndo_get_stats
= interface_stats
,
526 .ndo_set_mac_address
= interface_set_mac_addr
,
527 .ndo_change_mtu
= interface_change_mtu
,
528 .ndo_start_xmit
= interface_tx
,
529 .ndo_validate_addr
= eth_validate_addr
533 static void interface_setup(struct net_device
*dev
)
535 struct bat_priv
*priv
= netdev_priv(dev
);
536 char dev_addr
[ETH_ALEN
];
540 #ifdef HAVE_NET_DEVICE_OPS
541 dev
->netdev_ops
= &bat_netdev_ops
;
543 dev
->open
= interface_open
;
544 dev
->stop
= interface_release
;
545 dev
->get_stats
= interface_stats
;
546 dev
->set_mac_address
= interface_set_mac_addr
;
547 dev
->change_mtu
= interface_change_mtu
;
548 dev
->hard_start_xmit
= interface_tx
;
550 dev
->destructor
= free_netdev
;
553 * can't call min_mtu, because the needed variables
554 * have not been initialized yet
556 dev
->mtu
= ETH_DATA_LEN
;
557 dev
->hard_header_len
= BAT_HEADER_LEN
; /* reserve more space in the
558 * skbuff for our header */
560 /* generate random address */
561 random_ether_addr(dev_addr
);
562 memcpy(dev
->dev_addr
, dev_addr
, ETH_ALEN
);
564 SET_ETHTOOL_OPS(dev
, &bat_ethtool_ops
);
566 memset(priv
, 0, sizeof(struct bat_priv
));
569 struct net_device
*softif_create(char *name
)
571 struct net_device
*soft_iface
;
572 struct bat_priv
*bat_priv
;
575 soft_iface
= alloc_netdev(sizeof(struct bat_priv
) , name
,
579 pr_err("Unable to allocate the batman interface: %s\n", name
);
583 ret
= register_netdev(soft_iface
);
585 pr_err("Unable to register the batman interface '%s': %i\n",
587 goto free_soft_iface
;
590 bat_priv
= netdev_priv(soft_iface
);
592 atomic_set(&bat_priv
->aggregated_ogms
, 1);
593 atomic_set(&bat_priv
->bonding
, 0);
594 atomic_set(&bat_priv
->vis_mode
, VIS_TYPE_CLIENT_UPDATE
);
595 atomic_set(&bat_priv
->gw_mode
, GW_MODE_OFF
);
596 atomic_set(&bat_priv
->gw_sel_class
, 20);
597 atomic_set(&bat_priv
->gw_bandwidth
, 41);
598 atomic_set(&bat_priv
->orig_interval
, 1000);
599 atomic_set(&bat_priv
->hop_penalty
, 10);
600 atomic_set(&bat_priv
->log_level
, 0);
601 atomic_set(&bat_priv
->fragmentation
, 1);
602 atomic_set(&bat_priv
->bcast_queue_left
, BCAST_QUEUE_LEN
);
603 atomic_set(&bat_priv
->batman_queue_left
, BATMAN_QUEUE_LEN
);
605 atomic_set(&bat_priv
->mesh_state
, MESH_INACTIVE
);
606 atomic_set(&bat_priv
->bcast_seqno
, 1);
607 atomic_set(&bat_priv
->hna_local_changed
, 0);
609 bat_priv
->primary_if
= NULL
;
610 bat_priv
->num_ifaces
= 0;
611 bat_priv
->softif_neigh
= NULL
;
613 ret
= sysfs_add_meshif(soft_iface
);
615 goto unreg_soft_iface
;
617 ret
= debugfs_add_meshif(soft_iface
);
621 ret
= mesh_init(soft_iface
);
628 debugfs_del_meshif(soft_iface
);
630 sysfs_del_meshif(soft_iface
);
632 unregister_netdev(soft_iface
);
636 free_netdev(soft_iface
);
641 void softif_destroy(struct net_device
*soft_iface
)
643 debugfs_del_meshif(soft_iface
);
644 sysfs_del_meshif(soft_iface
);
645 mesh_free(soft_iface
);
646 unregister_netdevice(soft_iface
);
650 static int bat_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
653 cmd
->advertising
= 0;
654 cmd
->speed
= SPEED_10
;
655 cmd
->duplex
= DUPLEX_FULL
;
657 cmd
->phy_address
= 0;
658 cmd
->transceiver
= XCVR_INTERNAL
;
659 cmd
->autoneg
= AUTONEG_DISABLE
;
666 static void bat_get_drvinfo(struct net_device
*dev
,
667 struct ethtool_drvinfo
*info
)
669 strcpy(info
->driver
, "B.A.T.M.A.N. advanced");
670 strcpy(info
->version
, SOURCE_VERSION
);
671 strcpy(info
->fw_version
, "N/A");
672 strcpy(info
->bus_info
, "batman");
675 static u32
bat_get_msglevel(struct net_device
*dev
)
680 static void bat_set_msglevel(struct net_device
*dev
, u32 value
)
684 static u32
bat_get_link(struct net_device
*dev
)
689 static u32
bat_get_rx_csum(struct net_device
*dev
)
694 static int bat_set_rx_csum(struct net_device
*dev
, u32 data
)