batman-adv: Prefix send non-static functions with batadv_
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / batman-adv / soft-interface.c
CommitLineData
c6c8fea2 1/*
567db7b0 2 * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
c6c8fea2
SE
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"
c6c8fea2
SE
29#include "hash.h"
30#include "gateway_common.h"
31#include "gateway_client.h"
c6c8fea2 32#include "bat_sysfs.h"
43676ab5 33#include "originator.h"
c6c8fea2
SE
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"
23721387 39#include "bridge_loop_avoidance.h"
c6c8fea2
SE
40
41
42static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
43static void bat_get_drvinfo(struct net_device *dev,
44 struct ethtool_drvinfo *info);
45static u32 bat_get_msglevel(struct net_device *dev);
46static void bat_set_msglevel(struct net_device *dev, u32 value);
47static u32 bat_get_link(struct net_device *dev);
f8214865
MH
48static void batadv_get_strings(struct net_device *dev, u32 stringset, u8 *data);
49static void batadv_get_ethtool_stats(struct net_device *dev,
50 struct ethtool_stats *stats, u64 *data);
51static int batadv_get_sset_count(struct net_device *dev, int stringset);
c6c8fea2
SE
52
53static 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,
f8214865
MH
59 .get_strings = batadv_get_strings,
60 .get_ethtool_stats = batadv_get_ethtool_stats,
61 .get_sset_count = batadv_get_sset_count,
c6c8fea2
SE
62};
63
64int my_skb_head_push(struct sk_buff *skb, unsigned int len)
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
c6c8fea2
SE
84static int interface_open(struct net_device *dev)
85{
86 netif_start_queue(dev);
87 return 0;
88}
89
90static int interface_release(struct net_device *dev)
91{
92 netif_stop_queue(dev);
93 return 0;
94}
95
96static 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
102static 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
015758d0 110 /* only modify transtable if it has been initialized before */
c6c8fea2 111 if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) {
2dafb49d 112 tt_local_remove(bat_priv, dev->dev_addr,
cc47f66e 113 "mac address changed", false);
bc279080 114 tt_local_add(dev, addr->sa_data, NULL_IFINDEX);
c6c8fea2
SE
115 }
116
117 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
28009a6c 118 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
c6c8fea2
SE
119 return 0;
120}
121
122static int interface_change_mtu(struct net_device *dev, int new_mtu)
123{
124 /* check ranges */
9563877e 125 if ((new_mtu < 68) || (new_mtu > batadv_hardif_min_mtu(dev)))
c6c8fea2
SE
126 return -EINVAL;
127
128 dev->mtu = new_mtu;
129
130 return 0;
131}
132
db69ecfc 133static int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
c6c8fea2
SE
134{
135 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
136 struct bat_priv *bat_priv = netdev_priv(soft_iface);
32ae9b22 137 struct hard_iface *primary_if = NULL;
c6c8fea2
SE
138 struct bcast_packet *bcast_packet;
139 struct vlan_ethhdr *vhdr;
b1a8c04b
SW
140 static const uint8_t stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 0x00,
141 0x00};
be7af5cf 142 unsigned int header_len = 0;
c6c8fea2 143 int data_len = skb->len, ret;
7a5cc242 144 short vid __maybe_unused = -1;
be7af5cf 145 bool do_bcast = false;
c6c8fea2
SE
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:
c6c8fea2 162 goto dropped;
a7f6ee94 163 }
c6c8fea2 164
08adf151 165 if (batadv_bla_tx(bat_priv, skb, vid))
23721387
SW
166 goto dropped;
167
a73105b8 168 /* Register the client MAC in the transtable */
bc279080 169 tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif);
c6c8fea2 170
b1a8c04b
SW
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
be7af5cf
ML
177 if (is_multicast_ether_addr(ethhdr->h_dest)) {
178 do_bcast = true;
c6c8fea2 179
be7af5cf
ML
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 */
7cf06bc6 184 ret = batadv_gw_is_dhcp_target(skb, &header_len);
be7af5cf
ML
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 */
7cf06bc6 191 ret = batadv_gw_is_dhcp_target(skb, &header_len);
be7af5cf
ML
192 if (ret)
193 do_bcast = false;
194 break;
195 case GW_MODE_OFF:
196 default:
197 break;
198 }
c6c8fea2
SE
199 }
200
201 /* ethernet packet should be broadcasted */
202 if (do_bcast) {
32ae9b22
ML
203 primary_if = primary_if_get_selected(bat_priv);
204 if (!primary_if)
c6c8fea2
SE
205 goto dropped;
206
704509b8 207 if (my_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
c6c8fea2
SE
208 goto dropped;
209
210 bcast_packet = (struct bcast_packet *)skb->data;
76543d14
SE
211 bcast_packet->header.version = COMPAT_VERSION;
212 bcast_packet->header.ttl = TTL;
c6c8fea2
SE
213
214 /* batman packet type: broadcast */
76543d14 215 bcast_packet->header.packet_type = BAT_BCAST;
c6c8fea2
SE
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,
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
229 * the original skb. */
230 kfree_skb(skb);
231
232 /* unicast packet */
233 } else {
be7af5cf 234 if (atomic_read(&bat_priv->gw_mode) != GW_MODE_OFF) {
7cf06bc6 235 ret = batadv_gw_out_of_range(bat_priv, skb, ethhdr);
be7af5cf
ML
236 if (ret)
237 goto dropped;
238 }
239
c6c8fea2
SE
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
249dropped:
250 kfree_skb(skb);
251dropped_freed:
252 bat_priv->stats.tx_dropped++;
253end:
32ae9b22
ML
254 if (primary_if)
255 hardif_free_ref(primary_if);
c6c8fea2
SE
256 return NETDEV_TX_OK;
257}
258
259void interface_rx(struct net_device *soft_iface,
e6c10f43 260 struct sk_buff *skb, struct hard_iface *recv_if,
c6c8fea2
SE
261 int hdr_size)
262{
263 struct bat_priv *bat_priv = netdev_priv(soft_iface);
c6c8fea2
SE
264 struct ethhdr *ethhdr;
265 struct vlan_ethhdr *vhdr;
7a5cc242 266 short vid __maybe_unused = -1;
c6c8fea2
SE
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
c6c8fea2
SE
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
25985edc 295 /* should not be necessary anymore as we use skb_pull_rcsum()
c6c8fea2
SE
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++;
0d125074 302 bat_priv->stats.rx_bytes += skb->len + ETH_HLEN;
c6c8fea2
SE
303
304 soft_iface->last_rx = jiffies;
305
59b699cd
AQ
306 if (is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest))
307 goto dropped;
308
23721387
SW
309 /* Let the bridge loop avoidance check the packet. If will
310 * not handle it, we can safely push it up.
311 */
08adf151 312 if (batadv_bla_rx(bat_priv, skb, vid))
23721387
SW
313 goto out;
314
c6c8fea2 315 netif_rx(skb);
ba85fac2 316 goto out;
c6c8fea2
SE
317
318dropped:
319 kfree_skb(skb);
320out:
321 return;
322}
323
c6c8fea2
SE
324static 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};
c6c8fea2
SE
333
334static void interface_setup(struct net_device *dev)
335{
336 struct bat_priv *priv = netdev_priv(dev);
c6c8fea2
SE
337
338 ether_setup(dev);
339
c6c8fea2 340 dev->netdev_ops = &bat_netdev_ops;
c6c8fea2 341 dev->destructor = free_netdev;
af20b710 342 dev->tx_queue_len = 0;
c6c8fea2
SE
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;
6e215fd8
SE
349 /* reserve more space in the skbuff for our header */
350 dev->hard_header_len = BAT_HEADER_LEN;
c6c8fea2
SE
351
352 /* generate random address */
28009a6c 353 eth_hw_addr_random(dev);
c6c8fea2
SE
354
355 SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
356
704509b8 357 memset(priv, 0, sizeof(*priv));
c6c8fea2
SE
358}
359
747e4221 360struct net_device *softif_create(const char *name)
c6c8fea2
SE
361{
362 struct net_device *soft_iface;
363 struct bat_priv *bat_priv;
364 int ret;
365
704509b8 366 soft_iface = alloc_netdev(sizeof(*bat_priv), name, interface_setup);
c6c8fea2 367
320f422f 368 if (!soft_iface)
c6c8fea2 369 goto out;
c6c8fea2 370
c3caf519 371 ret = register_netdevice(soft_iface);
c6c8fea2
SE
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);
23721387 382 atomic_set(&bat_priv->bridge_loop_avoidance, 0);
59b699cd 383 atomic_set(&bat_priv->ap_isolation, 0);
c6c8fea2
SE
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);
8681a1c4 389 atomic_set(&bat_priv->hop_penalty, 30);
c6c8fea2
SE
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);
a73105b8
AQ
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);
23721387 400 atomic_set(&bat_priv->bla_num_requests, 0);
a73105b8
AQ
401
402 bat_priv->tt_buff = NULL;
403 bat_priv->tt_buff_len = 0;
cc47f66e 404 bat_priv->tt_poss_change = false;
c6c8fea2
SE
405
406 bat_priv->primary_if = NULL;
407 bat_priv->num_ifaces = 0;
c6c8fea2 408
f8214865
MH
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
1c280471
ML
414 ret = bat_algo_select(bat_priv, bat_routing_algo);
415 if (ret < 0)
f8214865 416 goto free_bat_counters;
1c280471 417
5853e22c 418 ret = batadv_sysfs_add_meshif(soft_iface);
c6c8fea2 419 if (ret < 0)
f8214865 420 goto free_bat_counters;
c6c8fea2 421
40a072d7 422 ret = batadv_debugfs_add_meshif(soft_iface);
c6c8fea2
SE
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
432unreg_debugfs:
40a072d7 433 batadv_debugfs_del_meshif(soft_iface);
c6c8fea2 434unreg_sysfs:
5853e22c 435 batadv_sysfs_del_meshif(soft_iface);
f8214865
MH
436free_bat_counters:
437 free_percpu(bat_priv->bat_counters);
c6c8fea2 438unreg_soft_iface:
06ba7ce2 439 unregister_netdevice(soft_iface);
c6c8fea2
SE
440 return NULL;
441
442free_soft_iface:
443 free_netdev(soft_iface);
444out:
445 return NULL;
446}
447
448void softif_destroy(struct net_device *soft_iface)
449{
40a072d7 450 batadv_debugfs_del_meshif(soft_iface);
5853e22c 451 batadv_sysfs_del_meshif(soft_iface);
c6c8fea2
SE
452 mesh_free(soft_iface);
453 unregister_netdevice(soft_iface);
454}
455
747e4221 456int softif_is_valid(const struct net_device *net_dev)
e44d8fe2 457{
e44d8fe2
SE
458 if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
459 return 1;
e44d8fe2
SE
460
461 return 0;
462}
463
c6c8fea2
SE
464/* ethtool */
465static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
466{
467 cmd->supported = 0;
468 cmd->advertising = 0;
70739497 469 ethtool_cmd_speed_set(cmd, SPEED_10);
c6c8fea2
SE
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
481static 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
490static u32 bat_get_msglevel(struct net_device *dev)
491{
492 return -EOPNOTSUPP;
493}
494
495static void bat_set_msglevel(struct net_device *dev, u32 value)
496{
497}
498
499static u32 bat_get_link(struct net_device *dev)
500{
501 return 1;
502}
f8214865
MH
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 */
508static 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
525static 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
533static 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
544static 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}