]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/batman-adv/soft-interface.c
batman-adv: Prefix bitarray static inline functions with batadv_
[mirror_ubuntu-artful-kernel.git] / net / batman-adv / soft-interface.c
CommitLineData
9cfc7bd6 1/* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
c6c8fea2
SE
2 *
3 * Marek Lindner, Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
c6c8fea2
SE
18 */
19
20#include "main.h"
21#include "soft-interface.h"
22#include "hard-interface.h"
23#include "routing.h"
24#include "send.h"
25#include "bat_debugfs.h"
26#include "translation-table.h"
c6c8fea2
SE
27#include "hash.h"
28#include "gateway_common.h"
29#include "gateway_client.h"
c6c8fea2 30#include "bat_sysfs.h"
43676ab5 31#include "originator.h"
c6c8fea2
SE
32#include <linux/slab.h>
33#include <linux/ethtool.h>
34#include <linux/etherdevice.h>
35#include <linux/if_vlan.h>
36#include "unicast.h"
23721387 37#include "bridge_loop_avoidance.h"
c6c8fea2
SE
38
39
40static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
41static void bat_get_drvinfo(struct net_device *dev,
42 struct ethtool_drvinfo *info);
43static u32 bat_get_msglevel(struct net_device *dev);
44static void bat_set_msglevel(struct net_device *dev, u32 value);
45static u32 bat_get_link(struct net_device *dev);
f8214865
MH
46static void batadv_get_strings(struct net_device *dev, u32 stringset, u8 *data);
47static void batadv_get_ethtool_stats(struct net_device *dev,
48 struct ethtool_stats *stats, u64 *data);
49static int batadv_get_sset_count(struct net_device *dev, int stringset);
c6c8fea2
SE
50
51static const struct ethtool_ops bat_ethtool_ops = {
52 .get_settings = bat_get_settings,
53 .get_drvinfo = bat_get_drvinfo,
54 .get_msglevel = bat_get_msglevel,
55 .set_msglevel = bat_set_msglevel,
56 .get_link = bat_get_link,
f8214865
MH
57 .get_strings = batadv_get_strings,
58 .get_ethtool_stats = batadv_get_ethtool_stats,
59 .get_sset_count = batadv_get_sset_count,
c6c8fea2
SE
60};
61
04b482a2 62int batadv_skb_head_push(struct sk_buff *skb, unsigned int len)
c6c8fea2
SE
63{
64 int result;
65
9cfc7bd6 66 /* TODO: We must check if we can release all references to non-payload
c6c8fea2
SE
67 * data using skb_header_release in our skbs to allow skb_cow_header to
68 * work optimally. This means that those skbs are not allowed to read
69 * or write any data which is before the current position of skb->data
70 * after that call and thus allow other skbs with the same data buffer
71 * to write freely in that area.
72 */
73 result = skb_cow_head(skb, len);
74 if (result < 0)
75 return result;
76
77 skb_push(skb, len);
78 return 0;
79}
80
c6c8fea2
SE
81static int interface_open(struct net_device *dev)
82{
83 netif_start_queue(dev);
84 return 0;
85}
86
87static int interface_release(struct net_device *dev)
88{
89 netif_stop_queue(dev);
90 return 0;
91}
92
93static struct net_device_stats *interface_stats(struct net_device *dev)
94{
95 struct bat_priv *bat_priv = netdev_priv(dev);
96 return &bat_priv->stats;
97}
98
99static int interface_set_mac_addr(struct net_device *dev, void *p)
100{
101 struct bat_priv *bat_priv = netdev_priv(dev);
102 struct sockaddr *addr = p;
103
104 if (!is_valid_ether_addr(addr->sa_data))
105 return -EADDRNOTAVAIL;
106
015758d0 107 /* only modify transtable if it has been initialized before */
c6c8fea2 108 if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) {
08c36d3e
SE
109 batadv_tt_local_remove(bat_priv, dev->dev_addr,
110 "mac address changed", false);
111 batadv_tt_local_add(dev, addr->sa_data, NULL_IFINDEX);
c6c8fea2
SE
112 }
113
114 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
28009a6c 115 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
c6c8fea2
SE
116 return 0;
117}
118
119static int interface_change_mtu(struct net_device *dev, int new_mtu)
120{
121 /* check ranges */
9563877e 122 if ((new_mtu < 68) || (new_mtu > batadv_hardif_min_mtu(dev)))
c6c8fea2
SE
123 return -EINVAL;
124
125 dev->mtu = new_mtu;
126
127 return 0;
128}
129
db69ecfc 130static int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
c6c8fea2
SE
131{
132 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
133 struct bat_priv *bat_priv = netdev_priv(soft_iface);
32ae9b22 134 struct hard_iface *primary_if = NULL;
c6c8fea2
SE
135 struct bcast_packet *bcast_packet;
136 struct vlan_ethhdr *vhdr;
b1a8c04b
SW
137 static const uint8_t stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 0x00,
138 0x00};
be7af5cf 139 unsigned int header_len = 0;
c6c8fea2 140 int data_len = skb->len, ret;
7a5cc242 141 short vid __maybe_unused = -1;
be7af5cf 142 bool do_bcast = false;
c6c8fea2
SE
143
144 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
145 goto dropped;
146
147 soft_iface->trans_start = jiffies;
148
149 switch (ntohs(ethhdr->h_proto)) {
150 case ETH_P_8021Q:
151 vhdr = (struct vlan_ethhdr *)skb->data;
152 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
153
154 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
155 break;
156
157 /* fall through */
158 case ETH_P_BATMAN:
c6c8fea2 159 goto dropped;
a7f6ee94 160 }
c6c8fea2 161
08adf151 162 if (batadv_bla_tx(bat_priv, skb, vid))
23721387
SW
163 goto dropped;
164
a73105b8 165 /* Register the client MAC in the transtable */
08c36d3e 166 batadv_tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif);
c6c8fea2 167
b1a8c04b
SW
168 /* don't accept stp packets. STP does not help in meshes.
169 * better use the bridge loop avoidance ...
170 */
171 if (compare_eth(ethhdr->h_dest, stp_addr))
172 goto dropped;
173
be7af5cf
ML
174 if (is_multicast_ether_addr(ethhdr->h_dest)) {
175 do_bcast = true;
c6c8fea2 176
be7af5cf
ML
177 switch (atomic_read(&bat_priv->gw_mode)) {
178 case GW_MODE_SERVER:
179 /* gateway servers should not send dhcp
9cfc7bd6
SE
180 * requests into the mesh
181 */
7cf06bc6 182 ret = batadv_gw_is_dhcp_target(skb, &header_len);
be7af5cf
ML
183 if (ret)
184 goto dropped;
185 break;
186 case GW_MODE_CLIENT:
187 /* gateway clients should send dhcp requests
9cfc7bd6
SE
188 * via unicast to their gateway
189 */
7cf06bc6 190 ret = batadv_gw_is_dhcp_target(skb, &header_len);
be7af5cf
ML
191 if (ret)
192 do_bcast = false;
193 break;
194 case GW_MODE_OFF:
195 default:
196 break;
197 }
c6c8fea2
SE
198 }
199
200 /* ethernet packet should be broadcasted */
201 if (do_bcast) {
32ae9b22
ML
202 primary_if = primary_if_get_selected(bat_priv);
203 if (!primary_if)
c6c8fea2
SE
204 goto dropped;
205
04b482a2 206 if (batadv_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
c6c8fea2
SE
207 goto dropped;
208
209 bcast_packet = (struct bcast_packet *)skb->data;
76543d14
SE
210 bcast_packet->header.version = COMPAT_VERSION;
211 bcast_packet->header.ttl = TTL;
c6c8fea2
SE
212
213 /* batman packet type: broadcast */
76543d14 214 bcast_packet->header.packet_type = BAT_BCAST;
c6c8fea2
SE
215
216 /* hw address of first interface is the orig mac because only
9cfc7bd6
SE
217 * this mac is known throughout the mesh
218 */
c6c8fea2 219 memcpy(bcast_packet->orig,
32ae9b22 220 primary_if->net_dev->dev_addr, ETH_ALEN);
c6c8fea2
SE
221
222 /* set broadcast sequence number */
223 bcast_packet->seqno =
224 htonl(atomic_inc_return(&bat_priv->bcast_seqno));
225
9455e34c 226 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
c6c8fea2
SE
227
228 /* a copy is stored in the bcast list, therefore removing
9cfc7bd6
SE
229 * the original skb.
230 */
c6c8fea2
SE
231 kfree_skb(skb);
232
233 /* unicast packet */
234 } else {
be7af5cf 235 if (atomic_read(&bat_priv->gw_mode) != GW_MODE_OFF) {
7cf06bc6 236 ret = batadv_gw_out_of_range(bat_priv, skb, ethhdr);
be7af5cf
ML
237 if (ret)
238 goto dropped;
239 }
240
88ed1e77 241 ret = batadv_unicast_send_skb(skb, bat_priv);
c6c8fea2
SE
242 if (ret != 0)
243 goto dropped_freed;
244 }
245
246 bat_priv->stats.tx_packets++;
247 bat_priv->stats.tx_bytes += data_len;
248 goto end;
249
250dropped:
251 kfree_skb(skb);
252dropped_freed:
253 bat_priv->stats.tx_dropped++;
254end:
32ae9b22
ML
255 if (primary_if)
256 hardif_free_ref(primary_if);
c6c8fea2
SE
257 return NETDEV_TX_OK;
258}
259
04b482a2
SE
260void batadv_interface_rx(struct net_device *soft_iface,
261 struct sk_buff *skb, struct hard_iface *recv_if,
262 int hdr_size)
c6c8fea2
SE
263{
264 struct bat_priv *bat_priv = netdev_priv(soft_iface);
c6c8fea2
SE
265 struct ethhdr *ethhdr;
266 struct vlan_ethhdr *vhdr;
7a5cc242 267 short vid __maybe_unused = -1;
c6c8fea2
SE
268
269 /* check if enough space is available for pulling, and pull */
270 if (!pskb_may_pull(skb, hdr_size))
271 goto dropped;
272
273 skb_pull_rcsum(skb, hdr_size);
274 skb_reset_mac_header(skb);
275
276 ethhdr = (struct ethhdr *)skb_mac_header(skb);
277
278 switch (ntohs(ethhdr->h_proto)) {
279 case ETH_P_8021Q:
280 vhdr = (struct vlan_ethhdr *)skb->data;
281 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
282
283 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
284 break;
285
286 /* fall through */
287 case ETH_P_BATMAN:
288 goto dropped;
289 }
290
c6c8fea2
SE
291 /* skb->dev & skb->pkt_type are set here */
292 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
293 goto dropped;
294 skb->protocol = eth_type_trans(skb, soft_iface);
295
25985edc 296 /* should not be necessary anymore as we use skb_pull_rcsum()
c6c8fea2 297 * TODO: please verify this and remove this TODO
9cfc7bd6
SE
298 * -- Dec 21st 2009, Simon Wunderlich
299 */
c6c8fea2 300
9cfc7bd6 301 /* skb->ip_summed = CHECKSUM_UNNECESSARY; */
c6c8fea2
SE
302
303 bat_priv->stats.rx_packets++;
0d125074 304 bat_priv->stats.rx_bytes += skb->len + ETH_HLEN;
c6c8fea2
SE
305
306 soft_iface->last_rx = jiffies;
307
08c36d3e 308 if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest))
59b699cd
AQ
309 goto dropped;
310
23721387
SW
311 /* Let the bridge loop avoidance check the packet. If will
312 * not handle it, we can safely push it up.
313 */
08adf151 314 if (batadv_bla_rx(bat_priv, skb, vid))
23721387
SW
315 goto out;
316
c6c8fea2 317 netif_rx(skb);
ba85fac2 318 goto out;
c6c8fea2
SE
319
320dropped:
321 kfree_skb(skb);
322out:
323 return;
324}
325
c6c8fea2
SE
326static const struct net_device_ops bat_netdev_ops = {
327 .ndo_open = interface_open,
328 .ndo_stop = interface_release,
329 .ndo_get_stats = interface_stats,
330 .ndo_set_mac_address = interface_set_mac_addr,
331 .ndo_change_mtu = interface_change_mtu,
332 .ndo_start_xmit = interface_tx,
333 .ndo_validate_addr = eth_validate_addr
334};
c6c8fea2
SE
335
336static void interface_setup(struct net_device *dev)
337{
338 struct bat_priv *priv = netdev_priv(dev);
c6c8fea2
SE
339
340 ether_setup(dev);
341
c6c8fea2 342 dev->netdev_ops = &bat_netdev_ops;
c6c8fea2 343 dev->destructor = free_netdev;
af20b710 344 dev->tx_queue_len = 0;
c6c8fea2 345
9cfc7bd6 346 /* can't call min_mtu, because the needed variables
c6c8fea2
SE
347 * have not been initialized yet
348 */
349 dev->mtu = ETH_DATA_LEN;
6e215fd8
SE
350 /* reserve more space in the skbuff for our header */
351 dev->hard_header_len = BAT_HEADER_LEN;
c6c8fea2
SE
352
353 /* generate random address */
28009a6c 354 eth_hw_addr_random(dev);
c6c8fea2
SE
355
356 SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
357
704509b8 358 memset(priv, 0, sizeof(*priv));
c6c8fea2
SE
359}
360
04b482a2 361struct net_device *batadv_softif_create(const char *name)
c6c8fea2
SE
362{
363 struct net_device *soft_iface;
364 struct bat_priv *bat_priv;
365 int ret;
366
704509b8 367 soft_iface = alloc_netdev(sizeof(*bat_priv), name, interface_setup);
c6c8fea2 368
320f422f 369 if (!soft_iface)
c6c8fea2 370 goto out;
c6c8fea2 371
c3caf519 372 ret = register_netdevice(soft_iface);
c6c8fea2
SE
373 if (ret < 0) {
374 pr_err("Unable to register the batman interface '%s': %i\n",
375 name, ret);
376 goto free_soft_iface;
377 }
378
379 bat_priv = netdev_priv(soft_iface);
380
381 atomic_set(&bat_priv->aggregated_ogms, 1);
382 atomic_set(&bat_priv->bonding, 0);
23721387 383 atomic_set(&bat_priv->bridge_loop_avoidance, 0);
59b699cd 384 atomic_set(&bat_priv->ap_isolation, 0);
c6c8fea2
SE
385 atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE);
386 atomic_set(&bat_priv->gw_mode, GW_MODE_OFF);
387 atomic_set(&bat_priv->gw_sel_class, 20);
388 atomic_set(&bat_priv->gw_bandwidth, 41);
389 atomic_set(&bat_priv->orig_interval, 1000);
8681a1c4 390 atomic_set(&bat_priv->hop_penalty, 30);
c6c8fea2
SE
391 atomic_set(&bat_priv->log_level, 0);
392 atomic_set(&bat_priv->fragmentation, 1);
393 atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN);
394 atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN);
395
396 atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
397 atomic_set(&bat_priv->bcast_seqno, 1);
a73105b8
AQ
398 atomic_set(&bat_priv->ttvn, 0);
399 atomic_set(&bat_priv->tt_local_changes, 0);
400 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
23721387 401 atomic_set(&bat_priv->bla_num_requests, 0);
a73105b8
AQ
402
403 bat_priv->tt_buff = NULL;
404 bat_priv->tt_buff_len = 0;
cc47f66e 405 bat_priv->tt_poss_change = false;
c6c8fea2
SE
406
407 bat_priv->primary_if = NULL;
408 bat_priv->num_ifaces = 0;
c6c8fea2 409
f8214865
MH
410 bat_priv->bat_counters = __alloc_percpu(sizeof(uint64_t) * BAT_CNT_NUM,
411 __alignof__(uint64_t));
412 if (!bat_priv->bat_counters)
413 goto unreg_soft_iface;
414
3193e8fd 415 ret = batadv_algo_select(bat_priv, batadv_routing_algo);
1c280471 416 if (ret < 0)
f8214865 417 goto free_bat_counters;
1c280471 418
5853e22c 419 ret = batadv_sysfs_add_meshif(soft_iface);
c6c8fea2 420 if (ret < 0)
f8214865 421 goto free_bat_counters;
c6c8fea2 422
40a072d7 423 ret = batadv_debugfs_add_meshif(soft_iface);
c6c8fea2
SE
424 if (ret < 0)
425 goto unreg_sysfs;
426
3193e8fd 427 ret = batadv_mesh_init(soft_iface);
c6c8fea2
SE
428 if (ret < 0)
429 goto unreg_debugfs;
430
431 return soft_iface;
432
433unreg_debugfs:
40a072d7 434 batadv_debugfs_del_meshif(soft_iface);
c6c8fea2 435unreg_sysfs:
5853e22c 436 batadv_sysfs_del_meshif(soft_iface);
f8214865
MH
437free_bat_counters:
438 free_percpu(bat_priv->bat_counters);
c6c8fea2 439unreg_soft_iface:
06ba7ce2 440 unregister_netdevice(soft_iface);
c6c8fea2
SE
441 return NULL;
442
443free_soft_iface:
444 free_netdev(soft_iface);
445out:
446 return NULL;
447}
448
04b482a2 449void batadv_softif_destroy(struct net_device *soft_iface)
c6c8fea2 450{
40a072d7 451 batadv_debugfs_del_meshif(soft_iface);
5853e22c 452 batadv_sysfs_del_meshif(soft_iface);
3193e8fd 453 batadv_mesh_free(soft_iface);
c6c8fea2
SE
454 unregister_netdevice(soft_iface);
455}
456
04b482a2 457int batadv_softif_is_valid(const struct net_device *net_dev)
e44d8fe2 458{
e44d8fe2
SE
459 if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
460 return 1;
e44d8fe2
SE
461
462 return 0;
463}
464
c6c8fea2
SE
465/* ethtool */
466static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
467{
468 cmd->supported = 0;
469 cmd->advertising = 0;
70739497 470 ethtool_cmd_speed_set(cmd, SPEED_10);
c6c8fea2
SE
471 cmd->duplex = DUPLEX_FULL;
472 cmd->port = PORT_TP;
473 cmd->phy_address = 0;
474 cmd->transceiver = XCVR_INTERNAL;
475 cmd->autoneg = AUTONEG_DISABLE;
476 cmd->maxtxpkt = 0;
477 cmd->maxrxpkt = 0;
478
479 return 0;
480}
481
482static void bat_get_drvinfo(struct net_device *dev,
483 struct ethtool_drvinfo *info)
484{
485 strcpy(info->driver, "B.A.T.M.A.N. advanced");
486 strcpy(info->version, SOURCE_VERSION);
487 strcpy(info->fw_version, "N/A");
488 strcpy(info->bus_info, "batman");
489}
490
491static u32 bat_get_msglevel(struct net_device *dev)
492{
493 return -EOPNOTSUPP;
494}
495
496static void bat_set_msglevel(struct net_device *dev, u32 value)
497{
498}
499
500static u32 bat_get_link(struct net_device *dev)
501{
502 return 1;
503}
f8214865
MH
504
505/* Inspired by drivers/net/ethernet/dlink/sundance.c:1702
506 * Declare each description string in struct.name[] to get fixed sized buffer
507 * and compile time checking for strings longer than ETH_GSTRING_LEN.
508 */
509static const struct {
510 const char name[ETH_GSTRING_LEN];
511} bat_counters_strings[] = {
512 { "forward" },
513 { "forward_bytes" },
514 { "mgmt_tx" },
515 { "mgmt_tx_bytes" },
516 { "mgmt_rx" },
517 { "mgmt_rx_bytes" },
518 { "tt_request_tx" },
519 { "tt_request_rx" },
520 { "tt_response_tx" },
521 { "tt_response_rx" },
522 { "tt_roam_adv_tx" },
523 { "tt_roam_adv_rx" },
524};
525
526static void batadv_get_strings(struct net_device *dev, uint32_t stringset,
527 uint8_t *data)
528{
529 if (stringset == ETH_SS_STATS)
530 memcpy(data, bat_counters_strings,
531 sizeof(bat_counters_strings));
532}
533
534static void batadv_get_ethtool_stats(struct net_device *dev,
535 struct ethtool_stats *stats,
536 uint64_t *data)
537{
538 struct bat_priv *bat_priv = netdev_priv(dev);
539 int i;
540
541 for (i = 0; i < BAT_CNT_NUM; i++)
542 data[i] = batadv_sum_counter(bat_priv, i);
543}
544
545static int batadv_get_sset_count(struct net_device *dev, int stringset)
546{
547 if (stringset == ETH_SS_STATS)
548 return BAT_CNT_NUM;
549
550 return -EOPNOTSUPP;
551}