batman-adv: Prefix soft-interface local static functions with batadv_
[cascardo/linux.git] / net / batman-adv / soft-interface.c
1 /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
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
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"
27 #include "hash.h"
28 #include "gateway_common.h"
29 #include "gateway_client.h"
30 #include "bat_sysfs.h"
31 #include "originator.h"
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"
37 #include "bridge_loop_avoidance.h"
38
39
40 static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
41 static void batadv_get_drvinfo(struct net_device *dev,
42                                struct ethtool_drvinfo *info);
43 static u32 batadv_get_msglevel(struct net_device *dev);
44 static void batadv_set_msglevel(struct net_device *dev, u32 value);
45 static u32 batadv_get_link(struct net_device *dev);
46 static void batadv_get_strings(struct net_device *dev, u32 stringset, u8 *data);
47 static void batadv_get_ethtool_stats(struct net_device *dev,
48                                      struct ethtool_stats *stats, u64 *data);
49 static int batadv_get_sset_count(struct net_device *dev, int stringset);
50
51 static 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,
57         .get_strings = batadv_get_strings,
58         .get_ethtool_stats = batadv_get_ethtool_stats,
59         .get_sset_count = batadv_get_sset_count,
60 };
61
62 int batadv_skb_head_push(struct sk_buff *skb, unsigned int len)
63 {
64         int result;
65
66         /* TODO: We must check if we can release all references to non-payload
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
81 static int batadv_interface_open(struct net_device *dev)
82 {
83         netif_start_queue(dev);
84         return 0;
85 }
86
87 static int batadv_interface_release(struct net_device *dev)
88 {
89         netif_stop_queue(dev);
90         return 0;
91 }
92
93 static struct net_device_stats *batadv_interface_stats(struct net_device *dev)
94 {
95         struct bat_priv *bat_priv = netdev_priv(dev);
96         return &bat_priv->stats;
97 }
98
99 static int batadv_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
107         /* only modify transtable if it has been initialized before */
108         if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) {
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);
112         }
113
114         memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
115         dev->addr_assign_type &= ~NET_ADDR_RANDOM;
116         return 0;
117 }
118
119 static int batadv_interface_change_mtu(struct net_device *dev, int new_mtu)
120 {
121         /* check ranges */
122         if ((new_mtu < 68) || (new_mtu > batadv_hardif_min_mtu(dev)))
123                 return -EINVAL;
124
125         dev->mtu = new_mtu;
126
127         return 0;
128 }
129
130 static int batadv_interface_tx(struct sk_buff *skb,
131                                struct net_device *soft_iface)
132 {
133         struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
134         struct bat_priv *bat_priv = netdev_priv(soft_iface);
135         struct hard_iface *primary_if = NULL;
136         struct bcast_packet *bcast_packet;
137         struct vlan_ethhdr *vhdr;
138         static const uint8_t stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 0x00,
139                                                    0x00};
140         unsigned int header_len = 0;
141         int data_len = skb->len, ret;
142         short vid __maybe_unused = -1;
143         bool do_bcast = false;
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:
160                 goto dropped;
161         }
162
163         if (batadv_bla_tx(bat_priv, skb, vid))
164                 goto dropped;
165
166         /* Register the client MAC in the transtable */
167         batadv_tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif);
168
169         /* don't accept stp packets. STP does not help in meshes.
170          * better use the bridge loop avoidance ...
171          */
172         if (batadv_compare_eth(ethhdr->h_dest, stp_addr))
173                 goto dropped;
174
175         if (is_multicast_ether_addr(ethhdr->h_dest)) {
176                 do_bcast = true;
177
178                 switch (atomic_read(&bat_priv->gw_mode)) {
179                 case GW_MODE_SERVER:
180                         /* gateway servers should not send dhcp
181                          * requests into the mesh
182                          */
183                         ret = batadv_gw_is_dhcp_target(skb, &header_len);
184                         if (ret)
185                                 goto dropped;
186                         break;
187                 case GW_MODE_CLIENT:
188                         /* gateway clients should send dhcp requests
189                          * via unicast to their gateway
190                          */
191                         ret = batadv_gw_is_dhcp_target(skb, &header_len);
192                         if (ret)
193                                 do_bcast = false;
194                         break;
195                 case GW_MODE_OFF:
196                 default:
197                         break;
198                 }
199         }
200
201         /* ethernet packet should be broadcasted */
202         if (do_bcast) {
203                 primary_if = batadv_primary_if_get_selected(bat_priv);
204                 if (!primary_if)
205                         goto dropped;
206
207                 if (batadv_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
208                         goto dropped;
209
210                 bcast_packet = (struct bcast_packet *)skb->data;
211                 bcast_packet->header.version = COMPAT_VERSION;
212                 bcast_packet->header.ttl = TTL;
213
214                 /* batman packet type: broadcast */
215                 bcast_packet->header.packet_type = BAT_BCAST;
216
217                 /* hw address of first interface is the orig mac because only
218                  * this mac is known throughout the mesh
219                  */
220                 memcpy(bcast_packet->orig,
221                        primary_if->net_dev->dev_addr, ETH_ALEN);
222
223                 /* set broadcast sequence number */
224                 bcast_packet->seqno =
225                         htonl(atomic_inc_return(&bat_priv->bcast_seqno));
226
227                 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
228
229                 /* a copy is stored in the bcast list, therefore removing
230                  * the original skb.
231                  */
232                 kfree_skb(skb);
233
234         /* unicast packet */
235         } else {
236                 if (atomic_read(&bat_priv->gw_mode) != GW_MODE_OFF) {
237                         ret = batadv_gw_out_of_range(bat_priv, skb, ethhdr);
238                         if (ret)
239                                 goto dropped;
240                 }
241
242                 ret = batadv_unicast_send_skb(skb, bat_priv);
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
251 dropped:
252         kfree_skb(skb);
253 dropped_freed:
254         bat_priv->stats.tx_dropped++;
255 end:
256         if (primary_if)
257                 batadv_hardif_free_ref(primary_if);
258         return NETDEV_TX_OK;
259 }
260
261 void batadv_interface_rx(struct net_device *soft_iface,
262                          struct sk_buff *skb, struct hard_iface *recv_if,
263                          int hdr_size)
264 {
265         struct bat_priv *bat_priv = netdev_priv(soft_iface);
266         struct ethhdr *ethhdr;
267         struct vlan_ethhdr *vhdr;
268         short vid __maybe_unused = -1;
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
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
297         /* should not be necessary anymore as we use skb_pull_rcsum()
298          * TODO: please verify this and remove this TODO
299          * -- Dec 21st 2009, Simon Wunderlich
300          */
301
302         /* skb->ip_summed = CHECKSUM_UNNECESSARY; */
303
304         bat_priv->stats.rx_packets++;
305         bat_priv->stats.rx_bytes += skb->len + ETH_HLEN;
306
307         soft_iface->last_rx = jiffies;
308
309         if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest))
310                 goto dropped;
311
312         /* Let the bridge loop avoidance check the packet. If will
313          * not handle it, we can safely push it up.
314          */
315         if (batadv_bla_rx(bat_priv, skb, vid))
316                 goto out;
317
318         netif_rx(skb);
319         goto out;
320
321 dropped:
322         kfree_skb(skb);
323 out:
324         return;
325 }
326
327 static 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,
334         .ndo_validate_addr = eth_validate_addr
335 };
336
337 static void batadv_interface_setup(struct net_device *dev)
338 {
339         struct bat_priv *priv = netdev_priv(dev);
340
341         ether_setup(dev);
342
343         dev->netdev_ops = &batadv_netdev_ops;
344         dev->destructor = free_netdev;
345         dev->tx_queue_len = 0;
346
347         /* can't call min_mtu, because the needed variables
348          * have not been initialized yet
349          */
350         dev->mtu = ETH_DATA_LEN;
351         /* reserve more space in the skbuff for our header */
352         dev->hard_header_len = BAT_HEADER_LEN;
353
354         /* generate random address */
355         eth_hw_addr_random(dev);
356
357         SET_ETHTOOL_OPS(dev, &batadv_ethtool_ops);
358
359         memset(priv, 0, sizeof(*priv));
360 }
361
362 struct net_device *batadv_softif_create(const char *name)
363 {
364         struct net_device *soft_iface;
365         struct bat_priv *bat_priv;
366         int ret;
367
368         soft_iface = alloc_netdev(sizeof(*bat_priv), name,
369                                   batadv_interface_setup);
370
371         if (!soft_iface)
372                 goto out;
373
374         ret = register_netdevice(soft_iface);
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);
385         atomic_set(&bat_priv->bridge_loop_avoidance, 0);
386         atomic_set(&bat_priv->ap_isolation, 0);
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);
392         atomic_set(&bat_priv->hop_penalty, 30);
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);
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);
403         atomic_set(&bat_priv->bla_num_requests, 0);
404
405         bat_priv->tt_buff = NULL;
406         bat_priv->tt_buff_len = 0;
407         bat_priv->tt_poss_change = false;
408
409         bat_priv->primary_if = NULL;
410         bat_priv->num_ifaces = 0;
411
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
417         ret = batadv_algo_select(bat_priv, batadv_routing_algo);
418         if (ret < 0)
419                 goto free_bat_counters;
420
421         ret = batadv_sysfs_add_meshif(soft_iface);
422         if (ret < 0)
423                 goto free_bat_counters;
424
425         ret = batadv_debugfs_add_meshif(soft_iface);
426         if (ret < 0)
427                 goto unreg_sysfs;
428
429         ret = batadv_mesh_init(soft_iface);
430         if (ret < 0)
431                 goto unreg_debugfs;
432
433         return soft_iface;
434
435 unreg_debugfs:
436         batadv_debugfs_del_meshif(soft_iface);
437 unreg_sysfs:
438         batadv_sysfs_del_meshif(soft_iface);
439 free_bat_counters:
440         free_percpu(bat_priv->bat_counters);
441 unreg_soft_iface:
442         unregister_netdevice(soft_iface);
443         return NULL;
444
445 free_soft_iface:
446         free_netdev(soft_iface);
447 out:
448         return NULL;
449 }
450
451 void batadv_softif_destroy(struct net_device *soft_iface)
452 {
453         batadv_debugfs_del_meshif(soft_iface);
454         batadv_sysfs_del_meshif(soft_iface);
455         batadv_mesh_free(soft_iface);
456         unregister_netdevice(soft_iface);
457 }
458
459 int batadv_softif_is_valid(const struct net_device *net_dev)
460 {
461         if (net_dev->netdev_ops->ndo_start_xmit == batadv_interface_tx)
462                 return 1;
463
464         return 0;
465 }
466
467 /* ethtool */
468 static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
469 {
470         cmd->supported = 0;
471         cmd->advertising = 0;
472         ethtool_cmd_speed_set(cmd, SPEED_10);
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
484 static void batadv_get_drvinfo(struct net_device *dev,
485                                struct ethtool_drvinfo *info)
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
493 static u32 batadv_get_msglevel(struct net_device *dev)
494 {
495         return -EOPNOTSUPP;
496 }
497
498 static void batadv_set_msglevel(struct net_device *dev, u32 value)
499 {
500 }
501
502 static u32 batadv_get_link(struct net_device *dev)
503 {
504         return 1;
505 }
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  */
511 static const struct {
512         const char name[ETH_GSTRING_LEN];
513 } batadv_counters_strings[] = {
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
528 static void batadv_get_strings(struct net_device *dev, uint32_t stringset,
529                                uint8_t *data)
530 {
531         if (stringset == ETH_SS_STATS)
532                 memcpy(data, batadv_counters_strings,
533                        sizeof(batadv_counters_strings));
534 }
535
536 static 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
547 static 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 }