batman-adv: Reformat multiline comments to consistent style
[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 bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
41 static void bat_get_drvinfo(struct net_device *dev,
42                             struct ethtool_drvinfo *info);
43 static u32 bat_get_msglevel(struct net_device *dev);
44 static void bat_set_msglevel(struct net_device *dev, u32 value);
45 static u32 bat_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 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,
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 interface_open(struct net_device *dev)
82 {
83         netif_start_queue(dev);
84         return 0;
85 }
86
87 static int interface_release(struct net_device *dev)
88 {
89         netif_stop_queue(dev);
90         return 0;
91 }
92
93 static 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
99 static 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
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 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 interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
131 {
132         struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
133         struct bat_priv *bat_priv = netdev_priv(soft_iface);
134         struct hard_iface *primary_if = NULL;
135         struct bcast_packet *bcast_packet;
136         struct vlan_ethhdr *vhdr;
137         static const uint8_t stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 0x00,
138                                                    0x00};
139         unsigned int header_len = 0;
140         int data_len = skb->len, ret;
141         short vid __maybe_unused = -1;
142         bool do_bcast = false;
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:
159                 goto dropped;
160         }
161
162         if (batadv_bla_tx(bat_priv, skb, vid))
163                 goto dropped;
164
165         /* Register the client MAC in the transtable */
166         batadv_tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif);
167
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
174         if (is_multicast_ether_addr(ethhdr->h_dest)) {
175                 do_bcast = true;
176
177                 switch (atomic_read(&bat_priv->gw_mode)) {
178                 case GW_MODE_SERVER:
179                         /* gateway servers should not send dhcp
180                          * requests into the mesh
181                          */
182                         ret = batadv_gw_is_dhcp_target(skb, &header_len);
183                         if (ret)
184                                 goto dropped;
185                         break;
186                 case GW_MODE_CLIENT:
187                         /* gateway clients should send dhcp requests
188                          * via unicast to their gateway
189                          */
190                         ret = batadv_gw_is_dhcp_target(skb, &header_len);
191                         if (ret)
192                                 do_bcast = false;
193                         break;
194                 case GW_MODE_OFF:
195                 default:
196                         break;
197                 }
198         }
199
200         /* ethernet packet should be broadcasted */
201         if (do_bcast) {
202                 primary_if = primary_if_get_selected(bat_priv);
203                 if (!primary_if)
204                         goto dropped;
205
206                 if (batadv_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
207                         goto dropped;
208
209                 bcast_packet = (struct bcast_packet *)skb->data;
210                 bcast_packet->header.version = COMPAT_VERSION;
211                 bcast_packet->header.ttl = TTL;
212
213                 /* batman packet type: broadcast */
214                 bcast_packet->header.packet_type = BAT_BCAST;
215
216                 /* hw address of first interface is the orig mac because only
217                  * this mac is known throughout the mesh
218                  */
219                 memcpy(bcast_packet->orig,
220                        primary_if->net_dev->dev_addr, ETH_ALEN);
221
222                 /* set broadcast sequence number */
223                 bcast_packet->seqno =
224                         htonl(atomic_inc_return(&bat_priv->bcast_seqno));
225
226                 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
227
228                 /* a copy is stored in the bcast list, therefore removing
229                  * the original skb.
230                  */
231                 kfree_skb(skb);
232
233         /* unicast packet */
234         } else {
235                 if (atomic_read(&bat_priv->gw_mode) != GW_MODE_OFF) {
236                         ret = batadv_gw_out_of_range(bat_priv, skb, ethhdr);
237                         if (ret)
238                                 goto dropped;
239                 }
240
241                 ret = batadv_unicast_send_skb(skb, bat_priv);
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
250 dropped:
251         kfree_skb(skb);
252 dropped_freed:
253         bat_priv->stats.tx_dropped++;
254 end:
255         if (primary_if)
256                 hardif_free_ref(primary_if);
257         return NETDEV_TX_OK;
258 }
259
260 void batadv_interface_rx(struct net_device *soft_iface,
261                          struct sk_buff *skb, struct hard_iface *recv_if,
262                          int hdr_size)
263 {
264         struct bat_priv *bat_priv = netdev_priv(soft_iface);
265         struct ethhdr *ethhdr;
266         struct vlan_ethhdr *vhdr;
267         short vid __maybe_unused = -1;
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
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
296         /* should not be necessary anymore as we use skb_pull_rcsum()
297          * TODO: please verify this and remove this TODO
298          * -- Dec 21st 2009, Simon Wunderlich
299          */
300
301         /* skb->ip_summed = CHECKSUM_UNNECESSARY; */
302
303         bat_priv->stats.rx_packets++;
304         bat_priv->stats.rx_bytes += skb->len + ETH_HLEN;
305
306         soft_iface->last_rx = jiffies;
307
308         if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest))
309                 goto dropped;
310
311         /* Let the bridge loop avoidance check the packet. If will
312          * not handle it, we can safely push it up.
313          */
314         if (batadv_bla_rx(bat_priv, skb, vid))
315                 goto out;
316
317         netif_rx(skb);
318         goto out;
319
320 dropped:
321         kfree_skb(skb);
322 out:
323         return;
324 }
325
326 static 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 };
335
336 static void interface_setup(struct net_device *dev)
337 {
338         struct bat_priv *priv = netdev_priv(dev);
339
340         ether_setup(dev);
341
342         dev->netdev_ops = &bat_netdev_ops;
343         dev->destructor = free_netdev;
344         dev->tx_queue_len = 0;
345
346         /* can't call min_mtu, because the needed variables
347          * have not been initialized yet
348          */
349         dev->mtu = ETH_DATA_LEN;
350         /* reserve more space in the skbuff for our header */
351         dev->hard_header_len = BAT_HEADER_LEN;
352
353         /* generate random address */
354         eth_hw_addr_random(dev);
355
356         SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
357
358         memset(priv, 0, sizeof(*priv));
359 }
360
361 struct net_device *batadv_softif_create(const char *name)
362 {
363         struct net_device *soft_iface;
364         struct bat_priv *bat_priv;
365         int ret;
366
367         soft_iface = alloc_netdev(sizeof(*bat_priv), name, interface_setup);
368
369         if (!soft_iface)
370                 goto out;
371
372         ret = register_netdevice(soft_iface);
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);
383         atomic_set(&bat_priv->bridge_loop_avoidance, 0);
384         atomic_set(&bat_priv->ap_isolation, 0);
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);
390         atomic_set(&bat_priv->hop_penalty, 30);
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);
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);
401         atomic_set(&bat_priv->bla_num_requests, 0);
402
403         bat_priv->tt_buff = NULL;
404         bat_priv->tt_buff_len = 0;
405         bat_priv->tt_poss_change = false;
406
407         bat_priv->primary_if = NULL;
408         bat_priv->num_ifaces = 0;
409
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
415         ret = batadv_algo_select(bat_priv, batadv_routing_algo);
416         if (ret < 0)
417                 goto free_bat_counters;
418
419         ret = batadv_sysfs_add_meshif(soft_iface);
420         if (ret < 0)
421                 goto free_bat_counters;
422
423         ret = batadv_debugfs_add_meshif(soft_iface);
424         if (ret < 0)
425                 goto unreg_sysfs;
426
427         ret = batadv_mesh_init(soft_iface);
428         if (ret < 0)
429                 goto unreg_debugfs;
430
431         return soft_iface;
432
433 unreg_debugfs:
434         batadv_debugfs_del_meshif(soft_iface);
435 unreg_sysfs:
436         batadv_sysfs_del_meshif(soft_iface);
437 free_bat_counters:
438         free_percpu(bat_priv->bat_counters);
439 unreg_soft_iface:
440         unregister_netdevice(soft_iface);
441         return NULL;
442
443 free_soft_iface:
444         free_netdev(soft_iface);
445 out:
446         return NULL;
447 }
448
449 void batadv_softif_destroy(struct net_device *soft_iface)
450 {
451         batadv_debugfs_del_meshif(soft_iface);
452         batadv_sysfs_del_meshif(soft_iface);
453         batadv_mesh_free(soft_iface);
454         unregister_netdevice(soft_iface);
455 }
456
457 int batadv_softif_is_valid(const struct net_device *net_dev)
458 {
459         if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
460                 return 1;
461
462         return 0;
463 }
464
465 /* ethtool */
466 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
467 {
468         cmd->supported = 0;
469         cmd->advertising = 0;
470         ethtool_cmd_speed_set(cmd, SPEED_10);
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
482 static 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
491 static u32 bat_get_msglevel(struct net_device *dev)
492 {
493         return -EOPNOTSUPP;
494 }
495
496 static void bat_set_msglevel(struct net_device *dev, u32 value)
497 {
498 }
499
500 static u32 bat_get_link(struct net_device *dev)
501 {
502         return 1;
503 }
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  */
509 static 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
526 static 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
534 static 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
545 static 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 }