Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1 | /* |
Sven Eckelmann | 567db7b | 2012-01-01 00:41:38 +0100 | [diff] [blame] | 2 | * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 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 "bat_debugfs.h" |
| 28 | #include "translation-table.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 29 | #include "hash.h" |
| 30 | #include "gateway_common.h" |
| 31 | #include "gateway_client.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 32 | #include "bat_sysfs.h" |
Antonio Quartulli | 43676ab | 2011-04-26 21:31:45 +0200 | [diff] [blame] | 33 | #include "originator.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 34 | #include <linux/slab.h> |
| 35 | #include <linux/ethtool.h> |
| 36 | #include <linux/etherdevice.h> |
| 37 | #include <linux/if_vlan.h> |
| 38 | #include "unicast.h" |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 39 | #include "bridge_loop_avoidance.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 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); |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 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); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 52 | |
| 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, |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 59 | .get_strings = batadv_get_strings, |
| 60 | .get_ethtool_stats = batadv_get_ethtool_stats, |
| 61 | .get_sset_count = batadv_get_sset_count, |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
Sven Eckelmann | 04b482a | 2012-05-12 02:09:38 +0200 | [diff] [blame^] | 64 | int batadv_skb_head_push(struct sk_buff *skb, unsigned int len) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 65 | { |
| 66 | int result; |
| 67 | |
| 68 | /** |
| 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. |
| 75 | */ |
| 76 | result = skb_cow_head(skb, len); |
| 77 | if (result < 0) |
| 78 | return result; |
| 79 | |
| 80 | skb_push(skb, len); |
| 81 | return 0; |
| 82 | } |
| 83 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 84 | static int interface_open(struct net_device *dev) |
| 85 | { |
| 86 | netif_start_queue(dev); |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static int interface_release(struct net_device *dev) |
| 91 | { |
| 92 | netif_stop_queue(dev); |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static struct net_device_stats *interface_stats(struct net_device *dev) |
| 97 | { |
| 98 | struct bat_priv *bat_priv = netdev_priv(dev); |
| 99 | return &bat_priv->stats; |
| 100 | } |
| 101 | |
| 102 | static int interface_set_mac_addr(struct net_device *dev, void *p) |
| 103 | { |
| 104 | struct bat_priv *bat_priv = netdev_priv(dev); |
| 105 | struct sockaddr *addr = p; |
| 106 | |
| 107 | if (!is_valid_ether_addr(addr->sa_data)) |
| 108 | return -EADDRNOTAVAIL; |
| 109 | |
Antonio Quartulli | 015758d | 2011-07-09 17:52:13 +0200 | [diff] [blame] | 110 | /* only modify transtable if it has been initialized before */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 111 | if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) { |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 112 | tt_local_remove(bat_priv, dev->dev_addr, |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 113 | "mac address changed", false); |
Antonio Quartulli | bc27908 | 2011-07-07 15:35:35 +0200 | [diff] [blame] | 114 | tt_local_add(dev, addr->sa_data, NULL_IFINDEX); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN); |
Danny Kukawka | 28009a6 | 2012-02-17 05:43:27 +0000 | [diff] [blame] | 118 | dev->addr_assign_type &= ~NET_ADDR_RANDOM; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static int interface_change_mtu(struct net_device *dev, int new_mtu) |
| 123 | { |
| 124 | /* check ranges */ |
Sven Eckelmann | 9563877 | 2012-05-12 02:09:31 +0200 | [diff] [blame] | 125 | if ((new_mtu < 68) || (new_mtu > batadv_hardif_min_mtu(dev))) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 126 | return -EINVAL; |
| 127 | |
| 128 | dev->mtu = new_mtu; |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
Sven Eckelmann | db69ecf | 2011-06-15 15:17:21 +0200 | [diff] [blame] | 133 | static int interface_tx(struct sk_buff *skb, struct net_device *soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 134 | { |
| 135 | struct ethhdr *ethhdr = (struct ethhdr *)skb->data; |
| 136 | struct bat_priv *bat_priv = netdev_priv(soft_iface); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 137 | struct hard_iface *primary_if = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 138 | struct bcast_packet *bcast_packet; |
| 139 | struct vlan_ethhdr *vhdr; |
Simon Wunderlich | b1a8c04 | 2012-01-22 20:00:25 +0100 | [diff] [blame] | 140 | static const uint8_t stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 0x00, |
| 141 | 0x00}; |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 142 | unsigned int header_len = 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 143 | int data_len = skb->len, ret; |
Simon Wunderlich | 7a5cc24 | 2012-01-22 20:00:27 +0100 | [diff] [blame] | 144 | short vid __maybe_unused = -1; |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 145 | bool do_bcast = false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 146 | |
| 147 | if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE) |
| 148 | goto dropped; |
| 149 | |
| 150 | soft_iface->trans_start = jiffies; |
| 151 | |
| 152 | switch (ntohs(ethhdr->h_proto)) { |
| 153 | case ETH_P_8021Q: |
| 154 | vhdr = (struct vlan_ethhdr *)skb->data; |
| 155 | vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK; |
| 156 | |
| 157 | if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN) |
| 158 | break; |
| 159 | |
| 160 | /* fall through */ |
| 161 | case ETH_P_BATMAN: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 162 | goto dropped; |
Simon Wunderlich | a7f6ee9 | 2012-01-22 20:00:18 +0100 | [diff] [blame] | 163 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 164 | |
Sven Eckelmann | 08adf15 | 2012-05-12 13:38:47 +0200 | [diff] [blame] | 165 | if (batadv_bla_tx(bat_priv, skb, vid)) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 166 | goto dropped; |
| 167 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 168 | /* Register the client MAC in the transtable */ |
Antonio Quartulli | bc27908 | 2011-07-07 15:35:35 +0200 | [diff] [blame] | 169 | tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 170 | |
Simon Wunderlich | b1a8c04 | 2012-01-22 20:00:25 +0100 | [diff] [blame] | 171 | /* don't accept stp packets. STP does not help in meshes. |
| 172 | * better use the bridge loop avoidance ... |
| 173 | */ |
| 174 | if (compare_eth(ethhdr->h_dest, stp_addr)) |
| 175 | goto dropped; |
| 176 | |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 177 | if (is_multicast_ether_addr(ethhdr->h_dest)) { |
| 178 | do_bcast = true; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 179 | |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 180 | switch (atomic_read(&bat_priv->gw_mode)) { |
| 181 | case GW_MODE_SERVER: |
| 182 | /* gateway servers should not send dhcp |
| 183 | * requests into the mesh */ |
Sven Eckelmann | 7cf06bc | 2012-05-12 02:09:29 +0200 | [diff] [blame] | 184 | ret = batadv_gw_is_dhcp_target(skb, &header_len); |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 185 | if (ret) |
| 186 | goto dropped; |
| 187 | break; |
| 188 | case GW_MODE_CLIENT: |
| 189 | /* gateway clients should send dhcp requests |
| 190 | * via unicast to their gateway */ |
Sven Eckelmann | 7cf06bc | 2012-05-12 02:09:29 +0200 | [diff] [blame] | 191 | ret = batadv_gw_is_dhcp_target(skb, &header_len); |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 192 | if (ret) |
| 193 | do_bcast = false; |
| 194 | break; |
| 195 | case GW_MODE_OFF: |
| 196 | default: |
| 197 | break; |
| 198 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /* ethernet packet should be broadcasted */ |
| 202 | if (do_bcast) { |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 203 | primary_if = primary_if_get_selected(bat_priv); |
| 204 | if (!primary_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 205 | goto dropped; |
| 206 | |
Sven Eckelmann | 04b482a | 2012-05-12 02:09:38 +0200 | [diff] [blame^] | 207 | if (batadv_skb_head_push(skb, sizeof(*bcast_packet)) < 0) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 208 | goto dropped; |
| 209 | |
| 210 | bcast_packet = (struct bcast_packet *)skb->data; |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 211 | bcast_packet->header.version = COMPAT_VERSION; |
| 212 | bcast_packet->header.ttl = TTL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 213 | |
| 214 | /* batman packet type: broadcast */ |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 215 | bcast_packet->header.packet_type = BAT_BCAST; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 216 | |
| 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, |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 220 | primary_if->net_dev->dev_addr, ETH_ALEN); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 221 | |
| 222 | /* set broadcast sequence number */ |
| 223 | bcast_packet->seqno = |
| 224 | htonl(atomic_inc_return(&bat_priv->bcast_seqno)); |
| 225 | |
Sven Eckelmann | 9455e34cb | 2012-05-12 02:09:37 +0200 | [diff] [blame] | 226 | batadv_add_bcast_packet_to_list(bat_priv, skb, 1); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 227 | |
| 228 | /* a copy is stored in the bcast list, therefore removing |
| 229 | * the original skb. */ |
| 230 | kfree_skb(skb); |
| 231 | |
| 232 | /* unicast packet */ |
| 233 | } else { |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 234 | if (atomic_read(&bat_priv->gw_mode) != GW_MODE_OFF) { |
Sven Eckelmann | 7cf06bc | 2012-05-12 02:09:29 +0200 | [diff] [blame] | 235 | ret = batadv_gw_out_of_range(bat_priv, skb, ethhdr); |
Marek Lindner | be7af5c | 2011-09-08 13:12:53 +0200 | [diff] [blame] | 236 | if (ret) |
| 237 | goto dropped; |
| 238 | } |
| 239 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 240 | ret = unicast_send_skb(skb, bat_priv); |
| 241 | if (ret != 0) |
| 242 | goto dropped_freed; |
| 243 | } |
| 244 | |
| 245 | bat_priv->stats.tx_packets++; |
| 246 | bat_priv->stats.tx_bytes += data_len; |
| 247 | goto end; |
| 248 | |
| 249 | dropped: |
| 250 | kfree_skb(skb); |
| 251 | dropped_freed: |
| 252 | bat_priv->stats.tx_dropped++; |
| 253 | end: |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 254 | if (primary_if) |
| 255 | hardif_free_ref(primary_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 256 | return NETDEV_TX_OK; |
| 257 | } |
| 258 | |
Sven Eckelmann | 04b482a | 2012-05-12 02:09:38 +0200 | [diff] [blame^] | 259 | void batadv_interface_rx(struct net_device *soft_iface, |
| 260 | struct sk_buff *skb, struct hard_iface *recv_if, |
| 261 | int hdr_size) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 262 | { |
| 263 | struct bat_priv *bat_priv = netdev_priv(soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 264 | struct ethhdr *ethhdr; |
| 265 | struct vlan_ethhdr *vhdr; |
Simon Wunderlich | 7a5cc24 | 2012-01-22 20:00:27 +0100 | [diff] [blame] | 266 | short vid __maybe_unused = -1; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 267 | |
| 268 | /* check if enough space is available for pulling, and pull */ |
| 269 | if (!pskb_may_pull(skb, hdr_size)) |
| 270 | goto dropped; |
| 271 | |
| 272 | skb_pull_rcsum(skb, hdr_size); |
| 273 | skb_reset_mac_header(skb); |
| 274 | |
| 275 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 276 | |
| 277 | switch (ntohs(ethhdr->h_proto)) { |
| 278 | case ETH_P_8021Q: |
| 279 | vhdr = (struct vlan_ethhdr *)skb->data; |
| 280 | vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK; |
| 281 | |
| 282 | if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN) |
| 283 | break; |
| 284 | |
| 285 | /* fall through */ |
| 286 | case ETH_P_BATMAN: |
| 287 | goto dropped; |
| 288 | } |
| 289 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 290 | /* skb->dev & skb->pkt_type are set here */ |
| 291 | if (unlikely(!pskb_may_pull(skb, ETH_HLEN))) |
| 292 | goto dropped; |
| 293 | skb->protocol = eth_type_trans(skb, soft_iface); |
| 294 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 295 | /* should not be necessary anymore as we use skb_pull_rcsum() |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 296 | * TODO: please verify this and remove this TODO |
| 297 | * -- Dec 21st 2009, Simon Wunderlich */ |
| 298 | |
| 299 | /* skb->ip_summed = CHECKSUM_UNNECESSARY;*/ |
| 300 | |
| 301 | bat_priv->stats.rx_packets++; |
Antonio Quartulli | 0d12507 | 2012-02-18 11:27:34 +0100 | [diff] [blame] | 302 | bat_priv->stats.rx_bytes += skb->len + ETH_HLEN; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 303 | |
| 304 | soft_iface->last_rx = jiffies; |
| 305 | |
Antonio Quartulli | 59b699c | 2011-07-07 15:35:36 +0200 | [diff] [blame] | 306 | if (is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest)) |
| 307 | goto dropped; |
| 308 | |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 309 | /* Let the bridge loop avoidance check the packet. If will |
| 310 | * not handle it, we can safely push it up. |
| 311 | */ |
Sven Eckelmann | 08adf15 | 2012-05-12 13:38:47 +0200 | [diff] [blame] | 312 | if (batadv_bla_rx(bat_priv, skb, vid)) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 313 | goto out; |
| 314 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 315 | netif_rx(skb); |
Simon Wunderlich | ba85fac | 2011-04-17 20:34:27 +0200 | [diff] [blame] | 316 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 317 | |
| 318 | dropped: |
| 319 | kfree_skb(skb); |
| 320 | out: |
| 321 | return; |
| 322 | } |
| 323 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 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 |
| 332 | }; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 333 | |
| 334 | static void interface_setup(struct net_device *dev) |
| 335 | { |
| 336 | struct bat_priv *priv = netdev_priv(dev); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 337 | |
| 338 | ether_setup(dev); |
| 339 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 340 | dev->netdev_ops = &bat_netdev_ops; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 341 | dev->destructor = free_netdev; |
Andrew Lunn | af20b71 | 2011-04-17 20:39:07 +0200 | [diff] [blame] | 342 | dev->tx_queue_len = 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 343 | |
| 344 | /** |
| 345 | * can't call min_mtu, because the needed variables |
| 346 | * have not been initialized yet |
| 347 | */ |
| 348 | dev->mtu = ETH_DATA_LEN; |
Sven Eckelmann | 6e215fd | 2011-05-08 12:45:45 +0200 | [diff] [blame] | 349 | /* reserve more space in the skbuff for our header */ |
| 350 | dev->hard_header_len = BAT_HEADER_LEN; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 351 | |
| 352 | /* generate random address */ |
Danny Kukawka | 28009a6 | 2012-02-17 05:43:27 +0000 | [diff] [blame] | 353 | eth_hw_addr_random(dev); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 354 | |
| 355 | SET_ETHTOOL_OPS(dev, &bat_ethtool_ops); |
| 356 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 357 | memset(priv, 0, sizeof(*priv)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Sven Eckelmann | 04b482a | 2012-05-12 02:09:38 +0200 | [diff] [blame^] | 360 | struct net_device *batadv_softif_create(const char *name) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 361 | { |
| 362 | struct net_device *soft_iface; |
| 363 | struct bat_priv *bat_priv; |
| 364 | int ret; |
| 365 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 366 | soft_iface = alloc_netdev(sizeof(*bat_priv), name, interface_setup); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 367 | |
Joe Perches | 320f422 | 2011-08-29 14:17:24 -0700 | [diff] [blame] | 368 | if (!soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 369 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 370 | |
Sven Eckelmann | c3caf51 | 2011-05-03 11:51:38 +0200 | [diff] [blame] | 371 | ret = register_netdevice(soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 372 | if (ret < 0) { |
| 373 | pr_err("Unable to register the batman interface '%s': %i\n", |
| 374 | name, ret); |
| 375 | goto free_soft_iface; |
| 376 | } |
| 377 | |
| 378 | bat_priv = netdev_priv(soft_iface); |
| 379 | |
| 380 | atomic_set(&bat_priv->aggregated_ogms, 1); |
| 381 | atomic_set(&bat_priv->bonding, 0); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 382 | atomic_set(&bat_priv->bridge_loop_avoidance, 0); |
Antonio Quartulli | 59b699c | 2011-07-07 15:35:36 +0200 | [diff] [blame] | 383 | atomic_set(&bat_priv->ap_isolation, 0); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 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); |
Marek Lindner | 8681a1c | 2012-01-27 23:11:55 +0800 | [diff] [blame] | 389 | atomic_set(&bat_priv->hop_penalty, 30); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 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); |
| 394 | |
| 395 | atomic_set(&bat_priv->mesh_state, MESH_INACTIVE); |
| 396 | atomic_set(&bat_priv->bcast_seqno, 1); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 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); |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 400 | atomic_set(&bat_priv->bla_num_requests, 0); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 401 | |
| 402 | bat_priv->tt_buff = NULL; |
| 403 | bat_priv->tt_buff_len = 0; |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 404 | bat_priv->tt_poss_change = false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 405 | |
| 406 | bat_priv->primary_if = NULL; |
| 407 | bat_priv->num_ifaces = 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 408 | |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 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; |
| 413 | |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 414 | ret = bat_algo_select(bat_priv, bat_routing_algo); |
| 415 | if (ret < 0) |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 416 | goto free_bat_counters; |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 417 | |
Sven Eckelmann | 5853e22 | 2012-05-12 02:09:24 +0200 | [diff] [blame] | 418 | ret = batadv_sysfs_add_meshif(soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 419 | if (ret < 0) |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 420 | goto free_bat_counters; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 421 | |
Sven Eckelmann | 40a072d | 2012-05-12 02:09:23 +0200 | [diff] [blame] | 422 | ret = batadv_debugfs_add_meshif(soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 423 | if (ret < 0) |
| 424 | goto unreg_sysfs; |
| 425 | |
| 426 | ret = mesh_init(soft_iface); |
| 427 | if (ret < 0) |
| 428 | goto unreg_debugfs; |
| 429 | |
| 430 | return soft_iface; |
| 431 | |
| 432 | unreg_debugfs: |
Sven Eckelmann | 40a072d | 2012-05-12 02:09:23 +0200 | [diff] [blame] | 433 | batadv_debugfs_del_meshif(soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 434 | unreg_sysfs: |
Sven Eckelmann | 5853e22 | 2012-05-12 02:09:24 +0200 | [diff] [blame] | 435 | batadv_sysfs_del_meshif(soft_iface); |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 436 | free_bat_counters: |
| 437 | free_percpu(bat_priv->bat_counters); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 438 | unreg_soft_iface: |
Simon Wunderlich | 06ba7ce | 2011-11-07 13:57:48 +0100 | [diff] [blame] | 439 | unregister_netdevice(soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 440 | return NULL; |
| 441 | |
| 442 | free_soft_iface: |
| 443 | free_netdev(soft_iface); |
| 444 | out: |
| 445 | return NULL; |
| 446 | } |
| 447 | |
Sven Eckelmann | 04b482a | 2012-05-12 02:09:38 +0200 | [diff] [blame^] | 448 | void batadv_softif_destroy(struct net_device *soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 449 | { |
Sven Eckelmann | 40a072d | 2012-05-12 02:09:23 +0200 | [diff] [blame] | 450 | batadv_debugfs_del_meshif(soft_iface); |
Sven Eckelmann | 5853e22 | 2012-05-12 02:09:24 +0200 | [diff] [blame] | 451 | batadv_sysfs_del_meshif(soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 452 | mesh_free(soft_iface); |
| 453 | unregister_netdevice(soft_iface); |
| 454 | } |
| 455 | |
Sven Eckelmann | 04b482a | 2012-05-12 02:09:38 +0200 | [diff] [blame^] | 456 | int batadv_softif_is_valid(const struct net_device *net_dev) |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 457 | { |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 458 | if (net_dev->netdev_ops->ndo_start_xmit == interface_tx) |
| 459 | return 1; |
Sven Eckelmann | e44d8fe | 2011-03-04 21:36:41 +0000 | [diff] [blame] | 460 | |
| 461 | return 0; |
| 462 | } |
| 463 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 464 | /* ethtool */ |
| 465 | static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 466 | { |
| 467 | cmd->supported = 0; |
| 468 | cmd->advertising = 0; |
David Decotigny | 7073949 | 2011-04-27 18:32:40 +0000 | [diff] [blame] | 469 | ethtool_cmd_speed_set(cmd, SPEED_10); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 470 | cmd->duplex = DUPLEX_FULL; |
| 471 | cmd->port = PORT_TP; |
| 472 | cmd->phy_address = 0; |
| 473 | cmd->transceiver = XCVR_INTERNAL; |
| 474 | cmd->autoneg = AUTONEG_DISABLE; |
| 475 | cmd->maxtxpkt = 0; |
| 476 | cmd->maxrxpkt = 0; |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | static void bat_get_drvinfo(struct net_device *dev, |
| 482 | struct ethtool_drvinfo *info) |
| 483 | { |
| 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"); |
| 488 | } |
| 489 | |
| 490 | static u32 bat_get_msglevel(struct net_device *dev) |
| 491 | { |
| 492 | return -EOPNOTSUPP; |
| 493 | } |
| 494 | |
| 495 | static void bat_set_msglevel(struct net_device *dev, u32 value) |
| 496 | { |
| 497 | } |
| 498 | |
| 499 | static u32 bat_get_link(struct net_device *dev) |
| 500 | { |
| 501 | return 1; |
| 502 | } |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 503 | |
| 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. |
| 507 | */ |
| 508 | static const struct { |
| 509 | const char name[ETH_GSTRING_LEN]; |
| 510 | } bat_counters_strings[] = { |
| 511 | { "forward" }, |
| 512 | { "forward_bytes" }, |
| 513 | { "mgmt_tx" }, |
| 514 | { "mgmt_tx_bytes" }, |
| 515 | { "mgmt_rx" }, |
| 516 | { "mgmt_rx_bytes" }, |
| 517 | { "tt_request_tx" }, |
| 518 | { "tt_request_rx" }, |
| 519 | { "tt_response_tx" }, |
| 520 | { "tt_response_rx" }, |
| 521 | { "tt_roam_adv_tx" }, |
| 522 | { "tt_roam_adv_rx" }, |
| 523 | }; |
| 524 | |
| 525 | static void batadv_get_strings(struct net_device *dev, uint32_t stringset, |
| 526 | uint8_t *data) |
| 527 | { |
| 528 | if (stringset == ETH_SS_STATS) |
| 529 | memcpy(data, bat_counters_strings, |
| 530 | sizeof(bat_counters_strings)); |
| 531 | } |
| 532 | |
| 533 | static void batadv_get_ethtool_stats(struct net_device *dev, |
| 534 | struct ethtool_stats *stats, |
| 535 | uint64_t *data) |
| 536 | { |
| 537 | struct bat_priv *bat_priv = netdev_priv(dev); |
| 538 | int i; |
| 539 | |
| 540 | for (i = 0; i < BAT_CNT_NUM; i++) |
| 541 | data[i] = batadv_sum_counter(bat_priv, i); |
| 542 | } |
| 543 | |
| 544 | static int batadv_get_sset_count(struct net_device *dev, int stringset) |
| 545 | { |
| 546 | if (stringset == ETH_SS_STATS) |
| 547 | return BAT_CNT_NUM; |
| 548 | |
| 549 | return -EOPNOTSUPP; |
| 550 | } |