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>
39 #include "bridge_loop_avoidance.h"
42 static int bat_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
);
43 static void bat_get_drvinfo(struct net_device
*dev
,
44 struct ethtool_drvinfo
*info
);
45 static u32
bat_get_msglevel(struct net_device
*dev
);
46 static void bat_set_msglevel(struct net_device
*dev
, u32 value
);
47 static u32
bat_get_link(struct net_device
*dev
);
48 static void batadv_get_strings(struct net_device
*dev
, u32 stringset
, u8
*data
);
49 static void batadv_get_ethtool_stats(struct net_device
*dev
,
50 struct ethtool_stats
*stats
, u64
*data
);
51 static int batadv_get_sset_count(struct net_device
*dev
, int stringset
);
53 static const struct ethtool_ops bat_ethtool_ops
= {
54 .get_settings
= bat_get_settings
,
55 .get_drvinfo
= bat_get_drvinfo
,
56 .get_msglevel
= bat_get_msglevel
,
57 .set_msglevel
= bat_set_msglevel
,
58 .get_link
= bat_get_link
,
59 .get_strings
= batadv_get_strings
,
60 .get_ethtool_stats
= batadv_get_ethtool_stats
,
61 .get_sset_count
= batadv_get_sset_count
,
64 int my_skb_head_push(struct sk_buff
*skb
, unsigned int len
)
69 * TODO: We must check if we can release all references to non-payload
70 * data using skb_header_release in our skbs to allow skb_cow_header to
71 * work optimally. This means that those skbs are not allowed to read
72 * or write any data which is before the current position of skb->data
73 * after that call and thus allow other skbs with the same data buffer
74 * to write freely in that area.
76 result
= skb_cow_head(skb
, len
);
84 static int interface_open(struct net_device
*dev
)
86 netif_start_queue(dev
);
90 static int interface_release(struct net_device
*dev
)
92 netif_stop_queue(dev
);
96 static struct net_device_stats
*interface_stats(struct net_device
*dev
)
98 struct bat_priv
*bat_priv
= netdev_priv(dev
);
99 return &bat_priv
->stats
;
102 static int interface_set_mac_addr(struct net_device
*dev
, void *p
)
104 struct bat_priv
*bat_priv
= netdev_priv(dev
);
105 struct sockaddr
*addr
= p
;
107 if (!is_valid_ether_addr(addr
->sa_data
))
108 return -EADDRNOTAVAIL
;
110 /* only modify transtable if it has been initialized before */
111 if (atomic_read(&bat_priv
->mesh_state
) == MESH_ACTIVE
) {
112 tt_local_remove(bat_priv
, dev
->dev_addr
,
113 "mac address changed", false);
114 tt_local_add(dev
, addr
->sa_data
, NULL_IFINDEX
);
117 memcpy(dev
->dev_addr
, addr
->sa_data
, ETH_ALEN
);
118 dev
->addr_assign_type
&= ~NET_ADDR_RANDOM
;
122 static int interface_change_mtu(struct net_device
*dev
, int new_mtu
)
125 if ((new_mtu
< 68) || (new_mtu
> hardif_min_mtu(dev
)))
133 static int interface_tx(struct sk_buff
*skb
, struct net_device
*soft_iface
)
135 struct ethhdr
*ethhdr
= (struct ethhdr
*)skb
->data
;
136 struct bat_priv
*bat_priv
= netdev_priv(soft_iface
);
137 struct hard_iface
*primary_if
= NULL
;
138 struct bcast_packet
*bcast_packet
;
139 struct vlan_ethhdr
*vhdr
;
140 static const uint8_t stp_addr
[ETH_ALEN
] = {0x01, 0x80, 0xC2, 0x00, 0x00,
142 unsigned int header_len
= 0;
143 int data_len
= skb
->len
, ret
;
144 short vid __maybe_unused
= -1;
145 bool do_bcast
= false;
147 if (atomic_read(&bat_priv
->mesh_state
) != MESH_ACTIVE
)
150 soft_iface
->trans_start
= jiffies
;
152 switch (ntohs(ethhdr
->h_proto
)) {
154 vhdr
= (struct vlan_ethhdr
*)skb
->data
;
155 vid
= ntohs(vhdr
->h_vlan_TCI
) & VLAN_VID_MASK
;
157 if (ntohs(vhdr
->h_vlan_encapsulated_proto
) != ETH_P_BATMAN
)
165 if (bla_tx(bat_priv
, skb
, vid
))
168 /* Register the client MAC in the transtable */
169 tt_local_add(soft_iface
, ethhdr
->h_source
, skb
->skb_iif
);
171 /* don't accept stp packets. STP does not help in meshes.
172 * better use the bridge loop avoidance ...
174 if (compare_eth(ethhdr
->h_dest
, stp_addr
))
177 if (is_multicast_ether_addr(ethhdr
->h_dest
)) {
180 switch (atomic_read(&bat_priv
->gw_mode
)) {
182 /* gateway servers should not send dhcp
183 * requests into the mesh */
184 ret
= gw_is_dhcp_target(skb
, &header_len
);
189 /* gateway clients should send dhcp requests
190 * via unicast to their gateway */
191 ret
= gw_is_dhcp_target(skb
, &header_len
);
201 /* ethernet packet should be broadcasted */
203 primary_if
= primary_if_get_selected(bat_priv
);
207 if (my_skb_head_push(skb
, sizeof(*bcast_packet
)) < 0)
210 bcast_packet
= (struct bcast_packet
*)skb
->data
;
211 bcast_packet
->header
.version
= COMPAT_VERSION
;
212 bcast_packet
->header
.ttl
= TTL
;
214 /* batman packet type: broadcast */
215 bcast_packet
->header
.packet_type
= BAT_BCAST
;
217 /* hw address of first interface is the orig mac because only
218 * this mac is known throughout the mesh */
219 memcpy(bcast_packet
->orig
,
220 primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
222 /* set broadcast sequence number */
223 bcast_packet
->seqno
=
224 htonl(atomic_inc_return(&bat_priv
->bcast_seqno
));
226 add_bcast_packet_to_list(bat_priv
, skb
, 1);
228 /* a copy is stored in the bcast list, therefore removing
229 * the original skb. */
234 if (atomic_read(&bat_priv
->gw_mode
) != GW_MODE_OFF
) {
235 ret
= gw_out_of_range(bat_priv
, skb
, ethhdr
);
240 ret
= unicast_send_skb(skb
, bat_priv
);
245 bat_priv
->stats
.tx_packets
++;
246 bat_priv
->stats
.tx_bytes
+= data_len
;
252 bat_priv
->stats
.tx_dropped
++;
255 hardif_free_ref(primary_if
);
259 void interface_rx(struct net_device
*soft_iface
,
260 struct sk_buff
*skb
, struct hard_iface
*recv_if
,
263 struct bat_priv
*bat_priv
= netdev_priv(soft_iface
);
264 struct ethhdr
*ethhdr
;
265 struct vlan_ethhdr
*vhdr
;
266 short vid __maybe_unused
= -1;
268 /* check if enough space is available for pulling, and pull */
269 if (!pskb_may_pull(skb
, hdr_size
))
272 skb_pull_rcsum(skb
, hdr_size
);
273 skb_reset_mac_header(skb
);
275 ethhdr
= (struct ethhdr
*)skb_mac_header(skb
);
277 switch (ntohs(ethhdr
->h_proto
)) {
279 vhdr
= (struct vlan_ethhdr
*)skb
->data
;
280 vid
= ntohs(vhdr
->h_vlan_TCI
) & VLAN_VID_MASK
;
282 if (ntohs(vhdr
->h_vlan_encapsulated_proto
) != ETH_P_BATMAN
)
290 /* skb->dev & skb->pkt_type are set here */
291 if (unlikely(!pskb_may_pull(skb
, ETH_HLEN
)))
293 skb
->protocol
= eth_type_trans(skb
, soft_iface
);
295 /* should not be necessary anymore as we use skb_pull_rcsum()
296 * TODO: please verify this and remove this TODO
297 * -- Dec 21st 2009, Simon Wunderlich */
299 /* skb->ip_summed = CHECKSUM_UNNECESSARY;*/
301 bat_priv
->stats
.rx_packets
++;
302 bat_priv
->stats
.rx_bytes
+= skb
->len
+ ETH_HLEN
;
304 soft_iface
->last_rx
= jiffies
;
306 if (is_ap_isolated(bat_priv
, ethhdr
->h_source
, ethhdr
->h_dest
))
309 /* Let the bridge loop avoidance check the packet. If will
310 * not handle it, we can safely push it up.
312 if (bla_rx(bat_priv
, skb
, vid
))
324 static const struct net_device_ops bat_netdev_ops
= {
325 .ndo_open
= interface_open
,
326 .ndo_stop
= interface_release
,
327 .ndo_get_stats
= interface_stats
,
328 .ndo_set_mac_address
= interface_set_mac_addr
,
329 .ndo_change_mtu
= interface_change_mtu
,
330 .ndo_start_xmit
= interface_tx
,
331 .ndo_validate_addr
= eth_validate_addr
334 static void interface_setup(struct net_device
*dev
)
336 struct bat_priv
*priv
= netdev_priv(dev
);
340 dev
->netdev_ops
= &bat_netdev_ops
;
341 dev
->destructor
= free_netdev
;
342 dev
->tx_queue_len
= 0;
345 * can't call min_mtu, because the needed variables
346 * have not been initialized yet
348 dev
->mtu
= ETH_DATA_LEN
;
349 /* reserve more space in the skbuff for our header */
350 dev
->hard_header_len
= BAT_HEADER_LEN
;
352 /* generate random address */
353 eth_hw_addr_random(dev
);
355 SET_ETHTOOL_OPS(dev
, &bat_ethtool_ops
);
357 memset(priv
, 0, sizeof(*priv
));
360 struct net_device
*softif_create(const char *name
)
362 struct net_device
*soft_iface
;
363 struct bat_priv
*bat_priv
;
366 soft_iface
= alloc_netdev(sizeof(*bat_priv
), name
, interface_setup
);
371 ret
= register_netdevice(soft_iface
);
373 pr_err("Unable to register the batman interface '%s': %i\n",
375 goto free_soft_iface
;
378 bat_priv
= netdev_priv(soft_iface
);
380 atomic_set(&bat_priv
->aggregated_ogms
, 1);
381 atomic_set(&bat_priv
->bonding
, 0);
382 atomic_set(&bat_priv
->bridge_loop_avoidance
, 0);
383 atomic_set(&bat_priv
->ap_isolation
, 0);
384 atomic_set(&bat_priv
->vis_mode
, VIS_TYPE_CLIENT_UPDATE
);
385 atomic_set(&bat_priv
->gw_mode
, GW_MODE_OFF
);
386 atomic_set(&bat_priv
->gw_sel_class
, 20);
387 atomic_set(&bat_priv
->gw_bandwidth
, 41);
388 atomic_set(&bat_priv
->orig_interval
, 1000);
389 atomic_set(&bat_priv
->hop_penalty
, 30);
390 atomic_set(&bat_priv
->log_level
, 0);
391 atomic_set(&bat_priv
->fragmentation
, 1);
392 atomic_set(&bat_priv
->bcast_queue_left
, BCAST_QUEUE_LEN
);
393 atomic_set(&bat_priv
->batman_queue_left
, BATMAN_QUEUE_LEN
);
395 atomic_set(&bat_priv
->mesh_state
, MESH_INACTIVE
);
396 atomic_set(&bat_priv
->bcast_seqno
, 1);
397 atomic_set(&bat_priv
->ttvn
, 0);
398 atomic_set(&bat_priv
->tt_local_changes
, 0);
399 atomic_set(&bat_priv
->tt_ogm_append_cnt
, 0);
400 atomic_set(&bat_priv
->bla_num_requests
, 0);
402 bat_priv
->tt_buff
= NULL
;
403 bat_priv
->tt_buff_len
= 0;
404 bat_priv
->tt_poss_change
= false;
406 bat_priv
->primary_if
= NULL
;
407 bat_priv
->num_ifaces
= 0;
409 bat_priv
->bat_counters
= __alloc_percpu(sizeof(uint64_t) * BAT_CNT_NUM
,
410 __alignof__(uint64_t));
411 if (!bat_priv
->bat_counters
)
412 goto unreg_soft_iface
;
414 ret
= bat_algo_select(bat_priv
, bat_routing_algo
);
416 goto free_bat_counters
;
418 ret
= sysfs_add_meshif(soft_iface
);
420 goto free_bat_counters
;
422 ret
= debugfs_add_meshif(soft_iface
);
426 ret
= mesh_init(soft_iface
);
433 debugfs_del_meshif(soft_iface
);
435 sysfs_del_meshif(soft_iface
);
437 free_percpu(bat_priv
->bat_counters
);
439 unregister_netdevice(soft_iface
);
443 free_netdev(soft_iface
);
448 void softif_destroy(struct net_device
*soft_iface
)
450 debugfs_del_meshif(soft_iface
);
451 sysfs_del_meshif(soft_iface
);
452 mesh_free(soft_iface
);
453 unregister_netdevice(soft_iface
);
456 int softif_is_valid(const struct net_device
*net_dev
)
458 if (net_dev
->netdev_ops
->ndo_start_xmit
== interface_tx
)
465 static int bat_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
468 cmd
->advertising
= 0;
469 ethtool_cmd_speed_set(cmd
, SPEED_10
);
470 cmd
->duplex
= DUPLEX_FULL
;
472 cmd
->phy_address
= 0;
473 cmd
->transceiver
= XCVR_INTERNAL
;
474 cmd
->autoneg
= AUTONEG_DISABLE
;
481 static void bat_get_drvinfo(struct net_device
*dev
,
482 struct ethtool_drvinfo
*info
)
484 strcpy(info
->driver
, "B.A.T.M.A.N. advanced");
485 strcpy(info
->version
, SOURCE_VERSION
);
486 strcpy(info
->fw_version
, "N/A");
487 strcpy(info
->bus_info
, "batman");
490 static u32
bat_get_msglevel(struct net_device
*dev
)
495 static void bat_set_msglevel(struct net_device
*dev
, u32 value
)
499 static u32
bat_get_link(struct net_device
*dev
)
504 /* Inspired by drivers/net/ethernet/dlink/sundance.c:1702
505 * Declare each description string in struct.name[] to get fixed sized buffer
506 * and compile time checking for strings longer than ETH_GSTRING_LEN.
508 static const struct {
509 const char name
[ETH_GSTRING_LEN
];
510 } bat_counters_strings
[] = {
519 { "tt_response_tx" },
520 { "tt_response_rx" },
521 { "tt_roam_adv_tx" },
522 { "tt_roam_adv_rx" },
525 static void batadv_get_strings(struct net_device
*dev
, uint32_t stringset
,
528 if (stringset
== ETH_SS_STATS
)
529 memcpy(data
, bat_counters_strings
,
530 sizeof(bat_counters_strings
));
533 static void batadv_get_ethtool_stats(struct net_device
*dev
,
534 struct ethtool_stats
*stats
,
537 struct bat_priv
*bat_priv
= netdev_priv(dev
);
540 for (i
= 0; i
< BAT_CNT_NUM
; i
++)
541 data
[i
] = batadv_sum_counter(bat_priv
, i
);
544 static int batadv_get_sset_count(struct net_device
*dev
, int stringset
)
546 if (stringset
== ETH_SS_STATS
)