batman-adv: Prefix icmp_socket defines with BATADV_
[deliverable/linux.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
0294ca0d
SE
40static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
41static void batadv_get_drvinfo(struct net_device *dev,
42 struct ethtool_drvinfo *info);
43static u32 batadv_get_msglevel(struct net_device *dev);
44static void batadv_set_msglevel(struct net_device *dev, u32 value);
45static u32 batadv_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 50
0294ca0d
SE
51static const struct ethtool_ops batadv_ethtool_ops = {
52 .get_settings = batadv_get_settings,
53 .get_drvinfo = batadv_get_drvinfo,
54 .get_msglevel = batadv_get_msglevel,
55 .set_msglevel = batadv_set_msglevel,
56 .get_link = batadv_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
0294ca0d 81static int batadv_interface_open(struct net_device *dev)
c6c8fea2
SE
82{
83 netif_start_queue(dev);
84 return 0;
85}
86
0294ca0d 87static int batadv_interface_release(struct net_device *dev)
c6c8fea2
SE
88{
89 netif_stop_queue(dev);
90 return 0;
91}
92
0294ca0d 93static struct net_device_stats *batadv_interface_stats(struct net_device *dev)
c6c8fea2
SE
94{
95 struct bat_priv *bat_priv = netdev_priv(dev);
96 return &bat_priv->stats;
97}
98
0294ca0d 99static int batadv_interface_set_mac_addr(struct net_device *dev, void *p)
c6c8fea2
SE
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
0294ca0d 119static int batadv_interface_change_mtu(struct net_device *dev, int new_mtu)
c6c8fea2
SE
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
0294ca0d
SE
130static int batadv_interface_tx(struct sk_buff *skb,
131 struct net_device *soft_iface)
c6c8fea2
SE
132{
133 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
134 struct bat_priv *bat_priv = netdev_priv(soft_iface);
32ae9b22 135 struct hard_iface *primary_if = NULL;
c6c8fea2
SE
136 struct bcast_packet *bcast_packet;
137 struct vlan_ethhdr *vhdr;
b1a8c04b
SW
138 static const uint8_t stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 0x00,
139 0x00};
be7af5cf 140 unsigned int header_len = 0;
c6c8fea2 141 int data_len = skb->len, ret;
7a5cc242 142 short vid __maybe_unused = -1;
be7af5cf 143 bool do_bcast = false;
c6c8fea2
SE
144
145 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
146 goto dropped;
147
148 soft_iface->trans_start = jiffies;
149
150 switch (ntohs(ethhdr->h_proto)) {
151 case ETH_P_8021Q:
152 vhdr = (struct vlan_ethhdr *)skb->data;
153 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
154
155 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
156 break;
157
158 /* fall through */
159 case ETH_P_BATMAN:
c6c8fea2 160 goto dropped;
a7f6ee94 161 }
c6c8fea2 162
08adf151 163 if (batadv_bla_tx(bat_priv, skb, vid))
23721387
SW
164 goto dropped;
165
a73105b8 166 /* Register the client MAC in the transtable */
08c36d3e 167 batadv_tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif);
c6c8fea2 168
b1a8c04b
SW
169 /* don't accept stp packets. STP does not help in meshes.
170 * better use the bridge loop avoidance ...
171 */
1eda58bf 172 if (batadv_compare_eth(ethhdr->h_dest, stp_addr))
b1a8c04b
SW
173 goto dropped;
174
be7af5cf
ML
175 if (is_multicast_ether_addr(ethhdr->h_dest)) {
176 do_bcast = true;
c6c8fea2 177
be7af5cf
ML
178 switch (atomic_read(&bat_priv->gw_mode)) {
179 case GW_MODE_SERVER:
180 /* gateway servers should not send dhcp
9cfc7bd6
SE
181 * requests into the mesh
182 */
7cf06bc6 183 ret = batadv_gw_is_dhcp_target(skb, &header_len);
be7af5cf
ML
184 if (ret)
185 goto dropped;
186 break;
187 case GW_MODE_CLIENT:
188 /* gateway clients should send dhcp requests
9cfc7bd6
SE
189 * via unicast to their gateway
190 */
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) {
e5d89254 203 primary_if = batadv_primary_if_get_selected(bat_priv);
32ae9b22 204 if (!primary_if)
c6c8fea2
SE
205 goto dropped;
206
04b482a2 207 if (batadv_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
9cfc7bd6
SE
218 * this mac is known throughout the mesh
219 */
c6c8fea2 220 memcpy(bcast_packet->orig,
32ae9b22 221 primary_if->net_dev->dev_addr, ETH_ALEN);
c6c8fea2
SE
222
223 /* set broadcast sequence number */
224 bcast_packet->seqno =
225 htonl(atomic_inc_return(&bat_priv->bcast_seqno));
226
9455e34c 227 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
c6c8fea2
SE
228
229 /* a copy is stored in the bcast list, therefore removing
9cfc7bd6
SE
230 * the original skb.
231 */
c6c8fea2
SE
232 kfree_skb(skb);
233
234 /* unicast packet */
235 } else {
be7af5cf 236 if (atomic_read(&bat_priv->gw_mode) != GW_MODE_OFF) {
7cf06bc6 237 ret = batadv_gw_out_of_range(bat_priv, skb, ethhdr);
be7af5cf
ML
238 if (ret)
239 goto dropped;
240 }
241
88ed1e77 242 ret = batadv_unicast_send_skb(skb, bat_priv);
c6c8fea2
SE
243 if (ret != 0)
244 goto dropped_freed;
245 }
246
247 bat_priv->stats.tx_packets++;
248 bat_priv->stats.tx_bytes += data_len;
249 goto end;
250
251dropped:
252 kfree_skb(skb);
253dropped_freed:
254 bat_priv->stats.tx_dropped++;
255end:
32ae9b22 256 if (primary_if)
e5d89254 257 batadv_hardif_free_ref(primary_if);
c6c8fea2
SE
258 return NETDEV_TX_OK;
259}
260
04b482a2
SE
261void batadv_interface_rx(struct net_device *soft_iface,
262 struct sk_buff *skb, struct hard_iface *recv_if,
263 int hdr_size)
c6c8fea2
SE
264{
265 struct bat_priv *bat_priv = netdev_priv(soft_iface);
c6c8fea2
SE
266 struct ethhdr *ethhdr;
267 struct vlan_ethhdr *vhdr;
7a5cc242 268 short vid __maybe_unused = -1;
c6c8fea2
SE
269
270 /* check if enough space is available for pulling, and pull */
271 if (!pskb_may_pull(skb, hdr_size))
272 goto dropped;
273
274 skb_pull_rcsum(skb, hdr_size);
275 skb_reset_mac_header(skb);
276
277 ethhdr = (struct ethhdr *)skb_mac_header(skb);
278
279 switch (ntohs(ethhdr->h_proto)) {
280 case ETH_P_8021Q:
281 vhdr = (struct vlan_ethhdr *)skb->data;
282 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
283
284 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
285 break;
286
287 /* fall through */
288 case ETH_P_BATMAN:
289 goto dropped;
290 }
291
c6c8fea2
SE
292 /* skb->dev & skb->pkt_type are set here */
293 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
294 goto dropped;
295 skb->protocol = eth_type_trans(skb, soft_iface);
296
25985edc 297 /* should not be necessary anymore as we use skb_pull_rcsum()
c6c8fea2 298 * TODO: please verify this and remove this TODO
9cfc7bd6
SE
299 * -- Dec 21st 2009, Simon Wunderlich
300 */
c6c8fea2 301
9cfc7bd6 302 /* skb->ip_summed = CHECKSUM_UNNECESSARY; */
c6c8fea2
SE
303
304 bat_priv->stats.rx_packets++;
0d125074 305 bat_priv->stats.rx_bytes += skb->len + ETH_HLEN;
c6c8fea2
SE
306
307 soft_iface->last_rx = jiffies;
308
08c36d3e 309 if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest))
59b699cd
AQ
310 goto dropped;
311
23721387
SW
312 /* Let the bridge loop avoidance check the packet. If will
313 * not handle it, we can safely push it up.
314 */
08adf151 315 if (batadv_bla_rx(bat_priv, skb, vid))
23721387
SW
316 goto out;
317
c6c8fea2 318 netif_rx(skb);
ba85fac2 319 goto out;
c6c8fea2
SE
320
321dropped:
322 kfree_skb(skb);
323out:
324 return;
325}
326
0294ca0d
SE
327static const struct net_device_ops batadv_netdev_ops = {
328 .ndo_open = batadv_interface_open,
329 .ndo_stop = batadv_interface_release,
330 .ndo_get_stats = batadv_interface_stats,
331 .ndo_set_mac_address = batadv_interface_set_mac_addr,
332 .ndo_change_mtu = batadv_interface_change_mtu,
333 .ndo_start_xmit = batadv_interface_tx,
c6c8fea2
SE
334 .ndo_validate_addr = eth_validate_addr
335};
c6c8fea2 336
0294ca0d 337static void batadv_interface_setup(struct net_device *dev)
c6c8fea2
SE
338{
339 struct bat_priv *priv = netdev_priv(dev);
c6c8fea2
SE
340
341 ether_setup(dev);
342
0294ca0d 343 dev->netdev_ops = &batadv_netdev_ops;
c6c8fea2 344 dev->destructor = free_netdev;
af20b710 345 dev->tx_queue_len = 0;
c6c8fea2 346
9cfc7bd6 347 /* can't call min_mtu, because the needed variables
c6c8fea2
SE
348 * have not been initialized yet
349 */
350 dev->mtu = ETH_DATA_LEN;
6e215fd8
SE
351 /* reserve more space in the skbuff for our header */
352 dev->hard_header_len = BAT_HEADER_LEN;
c6c8fea2
SE
353
354 /* generate random address */
28009a6c 355 eth_hw_addr_random(dev);
c6c8fea2 356
0294ca0d 357 SET_ETHTOOL_OPS(dev, &batadv_ethtool_ops);
c6c8fea2 358
704509b8 359 memset(priv, 0, sizeof(*priv));
c6c8fea2
SE
360}
361
04b482a2 362struct net_device *batadv_softif_create(const char *name)
c6c8fea2
SE
363{
364 struct net_device *soft_iface;
365 struct bat_priv *bat_priv;
366 int ret;
367
0294ca0d
SE
368 soft_iface = alloc_netdev(sizeof(*bat_priv), name,
369 batadv_interface_setup);
c6c8fea2 370
320f422f 371 if (!soft_iface)
c6c8fea2 372 goto out;
c6c8fea2 373
c3caf519 374 ret = register_netdevice(soft_iface);
c6c8fea2
SE
375 if (ret < 0) {
376 pr_err("Unable to register the batman interface '%s': %i\n",
377 name, ret);
378 goto free_soft_iface;
379 }
380
381 bat_priv = netdev_priv(soft_iface);
382
383 atomic_set(&bat_priv->aggregated_ogms, 1);
384 atomic_set(&bat_priv->bonding, 0);
23721387 385 atomic_set(&bat_priv->bridge_loop_avoidance, 0);
59b699cd 386 atomic_set(&bat_priv->ap_isolation, 0);
c6c8fea2
SE
387 atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE);
388 atomic_set(&bat_priv->gw_mode, GW_MODE_OFF);
389 atomic_set(&bat_priv->gw_sel_class, 20);
390 atomic_set(&bat_priv->gw_bandwidth, 41);
391 atomic_set(&bat_priv->orig_interval, 1000);
8681a1c4 392 atomic_set(&bat_priv->hop_penalty, 30);
c6c8fea2
SE
393 atomic_set(&bat_priv->log_level, 0);
394 atomic_set(&bat_priv->fragmentation, 1);
395 atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN);
396 atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN);
397
398 atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
399 atomic_set(&bat_priv->bcast_seqno, 1);
a73105b8
AQ
400 atomic_set(&bat_priv->ttvn, 0);
401 atomic_set(&bat_priv->tt_local_changes, 0);
402 atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
23721387 403 atomic_set(&bat_priv->bla_num_requests, 0);
a73105b8
AQ
404
405 bat_priv->tt_buff = NULL;
406 bat_priv->tt_buff_len = 0;
cc47f66e 407 bat_priv->tt_poss_change = false;
c6c8fea2
SE
408
409 bat_priv->primary_if = NULL;
410 bat_priv->num_ifaces = 0;
c6c8fea2 411
f8214865
MH
412 bat_priv->bat_counters = __alloc_percpu(sizeof(uint64_t) * BAT_CNT_NUM,
413 __alignof__(uint64_t));
414 if (!bat_priv->bat_counters)
415 goto unreg_soft_iface;
416
3193e8fd 417 ret = batadv_algo_select(bat_priv, batadv_routing_algo);
1c280471 418 if (ret < 0)
f8214865 419 goto free_bat_counters;
1c280471 420
5853e22c 421 ret = batadv_sysfs_add_meshif(soft_iface);
c6c8fea2 422 if (ret < 0)
f8214865 423 goto free_bat_counters;
c6c8fea2 424
40a072d7 425 ret = batadv_debugfs_add_meshif(soft_iface);
c6c8fea2
SE
426 if (ret < 0)
427 goto unreg_sysfs;
428
3193e8fd 429 ret = batadv_mesh_init(soft_iface);
c6c8fea2
SE
430 if (ret < 0)
431 goto unreg_debugfs;
432
433 return soft_iface;
434
435unreg_debugfs:
40a072d7 436 batadv_debugfs_del_meshif(soft_iface);
c6c8fea2 437unreg_sysfs:
5853e22c 438 batadv_sysfs_del_meshif(soft_iface);
f8214865
MH
439free_bat_counters:
440 free_percpu(bat_priv->bat_counters);
c6c8fea2 441unreg_soft_iface:
06ba7ce2 442 unregister_netdevice(soft_iface);
c6c8fea2
SE
443 return NULL;
444
445free_soft_iface:
446 free_netdev(soft_iface);
447out:
448 return NULL;
449}
450
04b482a2 451void batadv_softif_destroy(struct net_device *soft_iface)
c6c8fea2 452{
40a072d7 453 batadv_debugfs_del_meshif(soft_iface);
5853e22c 454 batadv_sysfs_del_meshif(soft_iface);
3193e8fd 455 batadv_mesh_free(soft_iface);
c6c8fea2
SE
456 unregister_netdevice(soft_iface);
457}
458
04b482a2 459int batadv_softif_is_valid(const struct net_device *net_dev)
e44d8fe2 460{
0294ca0d 461 if (net_dev->netdev_ops->ndo_start_xmit == batadv_interface_tx)
e44d8fe2 462 return 1;
e44d8fe2
SE
463
464 return 0;
465}
466
c6c8fea2 467/* ethtool */
0294ca0d 468static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
c6c8fea2
SE
469{
470 cmd->supported = 0;
471 cmd->advertising = 0;
70739497 472 ethtool_cmd_speed_set(cmd, SPEED_10);
c6c8fea2
SE
473 cmd->duplex = DUPLEX_FULL;
474 cmd->port = PORT_TP;
475 cmd->phy_address = 0;
476 cmd->transceiver = XCVR_INTERNAL;
477 cmd->autoneg = AUTONEG_DISABLE;
478 cmd->maxtxpkt = 0;
479 cmd->maxrxpkt = 0;
480
481 return 0;
482}
483
0294ca0d
SE
484static void batadv_get_drvinfo(struct net_device *dev,
485 struct ethtool_drvinfo *info)
c6c8fea2
SE
486{
487 strcpy(info->driver, "B.A.T.M.A.N. advanced");
488 strcpy(info->version, SOURCE_VERSION);
489 strcpy(info->fw_version, "N/A");
490 strcpy(info->bus_info, "batman");
491}
492
0294ca0d 493static u32 batadv_get_msglevel(struct net_device *dev)
c6c8fea2
SE
494{
495 return -EOPNOTSUPP;
496}
497
0294ca0d 498static void batadv_set_msglevel(struct net_device *dev, u32 value)
c6c8fea2
SE
499{
500}
501
0294ca0d 502static u32 batadv_get_link(struct net_device *dev)
c6c8fea2
SE
503{
504 return 1;
505}
f8214865
MH
506
507/* Inspired by drivers/net/ethernet/dlink/sundance.c:1702
508 * Declare each description string in struct.name[] to get fixed sized buffer
509 * and compile time checking for strings longer than ETH_GSTRING_LEN.
510 */
511static const struct {
512 const char name[ETH_GSTRING_LEN];
0294ca0d 513} batadv_counters_strings[] = {
f8214865
MH
514 { "forward" },
515 { "forward_bytes" },
516 { "mgmt_tx" },
517 { "mgmt_tx_bytes" },
518 { "mgmt_rx" },
519 { "mgmt_rx_bytes" },
520 { "tt_request_tx" },
521 { "tt_request_rx" },
522 { "tt_response_tx" },
523 { "tt_response_rx" },
524 { "tt_roam_adv_tx" },
525 { "tt_roam_adv_rx" },
526};
527
528static void batadv_get_strings(struct net_device *dev, uint32_t stringset,
529 uint8_t *data)
530{
531 if (stringset == ETH_SS_STATS)
0294ca0d
SE
532 memcpy(data, batadv_counters_strings,
533 sizeof(batadv_counters_strings));
f8214865
MH
534}
535
536static void batadv_get_ethtool_stats(struct net_device *dev,
537 struct ethtool_stats *stats,
538 uint64_t *data)
539{
540 struct bat_priv *bat_priv = netdev_priv(dev);
541 int i;
542
543 for (i = 0; i < BAT_CNT_NUM; i++)
544 data[i] = batadv_sum_counter(bat_priv, i);
545}
546
547static int batadv_get_sset_count(struct net_device *dev, int stringset)
548{
549 if (stringset == ETH_SS_STATS)
550 return BAT_CNT_NUM;
551
552 return -EOPNOTSUPP;
553}
This page took 0.263875 seconds and 5 git commands to generate.