Staging: batman-adv: multiple mesh clouds
[firefly-linux-kernel-4.4.55.git] / drivers / staging / batman-adv / soft-interface.c
1 /*
2  * Copyright (C) 2007-2010 B.A.T.M.A.N. contributors:
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"
29 #include "types.h"
30 #include "hash.h"
31 #include "send.h"
32 #include "bat_sysfs.h"
33 #include <linux/slab.h>
34 #include <linux/ethtool.h>
35 #include <linux/etherdevice.h>
36 #include "unicast.h"
37
38
39 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
40 static void bat_get_drvinfo(struct net_device *dev,
41                             struct ethtool_drvinfo *info);
42 static u32 bat_get_msglevel(struct net_device *dev);
43 static void bat_set_msglevel(struct net_device *dev, u32 value);
44 static u32 bat_get_link(struct net_device *dev);
45 static u32 bat_get_rx_csum(struct net_device *dev);
46 static int bat_set_rx_csum(struct net_device *dev, u32 data);
47
48 static const struct ethtool_ops bat_ethtool_ops = {
49         .get_settings = bat_get_settings,
50         .get_drvinfo = bat_get_drvinfo,
51         .get_msglevel = bat_get_msglevel,
52         .set_msglevel = bat_set_msglevel,
53         .get_link = bat_get_link,
54         .get_rx_csum = bat_get_rx_csum,
55         .set_rx_csum = bat_set_rx_csum
56 };
57
58 int my_skb_head_push(struct sk_buff *skb, unsigned int len)
59 {
60         int result;
61
62         /**
63          * TODO: We must check if we can release all references to non-payload
64          * data using skb_header_release in our skbs to allow skb_cow_header to
65          * work optimally. This means that those skbs are not allowed to read
66          * or write any data which is before the current position of skb->data
67          * after that call and thus allow other skbs with the same data buffer
68          * to write freely in that area.
69          */
70         result = skb_cow_head(skb, len);
71         if (result < 0)
72                 return result;
73
74         skb_push(skb, len);
75         return 0;
76 }
77
78 static int interface_open(struct net_device *dev)
79 {
80         netif_start_queue(dev);
81         return 0;
82 }
83
84 static int interface_release(struct net_device *dev)
85 {
86         netif_stop_queue(dev);
87         return 0;
88 }
89
90 static struct net_device_stats *interface_stats(struct net_device *dev)
91 {
92         struct bat_priv *bat_priv = netdev_priv(dev);
93         return &bat_priv->stats;
94 }
95
96 static int interface_set_mac_addr(struct net_device *dev, void *p)
97 {
98         struct bat_priv *bat_priv = netdev_priv(dev);
99         struct sockaddr *addr = p;
100
101         if (!is_valid_ether_addr(addr->sa_data))
102                 return -EADDRNOTAVAIL;
103
104         /* only modify hna-table if it has been initialised before */
105         if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) {
106                 hna_local_remove(bat_priv, dev->dev_addr,
107                                  "mac address changed");
108                 hna_local_add(dev, addr->sa_data);
109         }
110
111         memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
112
113         return 0;
114 }
115
116 static int interface_change_mtu(struct net_device *dev, int new_mtu)
117 {
118         /* check ranges */
119         if ((new_mtu < 68) || (new_mtu > hardif_min_mtu(dev)))
120                 return -EINVAL;
121
122         dev->mtu = new_mtu;
123
124         return 0;
125 }
126
127 int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
128 {
129         struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
130         struct bat_priv *bat_priv = netdev_priv(soft_iface);
131         struct bcast_packet *bcast_packet;
132         int data_len = skb->len, ret;
133
134         if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
135                 goto dropped;
136
137         soft_iface->trans_start = jiffies;
138
139         /* TODO: check this for locks */
140         hna_local_add(soft_iface, ethhdr->h_source);
141
142         /* ethernet packet should be broadcasted */
143         if (is_bcast(ethhdr->h_dest) || is_mcast(ethhdr->h_dest)) {
144                 if (!bat_priv->primary_if)
145                         goto dropped;
146
147                 if (my_skb_head_push(skb, sizeof(struct bcast_packet)) < 0)
148                         goto dropped;
149
150                 bcast_packet = (struct bcast_packet *)skb->data;
151                 bcast_packet->version = COMPAT_VERSION;
152                 bcast_packet->ttl = TTL;
153
154                 /* batman packet type: broadcast */
155                 bcast_packet->packet_type = BAT_BCAST;
156
157                 /* hw address of first interface is the orig mac because only
158                  * this mac is known throughout the mesh */
159                 memcpy(bcast_packet->orig,
160                        bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
161
162                 /* set broadcast sequence number */
163                 bcast_packet->seqno =
164                         htonl(atomic_inc_return(&bat_priv->bcast_seqno));
165
166                 add_bcast_packet_to_list(bat_priv, skb);
167
168                 /* a copy is stored in the bcast list, therefore removing
169                  * the original skb. */
170                 kfree_skb(skb);
171
172         /* unicast packet */
173         } else {
174                 ret = unicast_send_skb(skb, bat_priv);
175                 if (ret != 0)
176                         goto dropped_freed;
177         }
178
179         bat_priv->stats.tx_packets++;
180         bat_priv->stats.tx_bytes += data_len;
181         goto end;
182
183 dropped:
184         kfree_skb(skb);
185 dropped_freed:
186         bat_priv->stats.tx_dropped++;
187 end:
188         return NETDEV_TX_OK;
189 }
190
191 void interface_rx(struct net_device *soft_iface,
192                   struct sk_buff *skb, int hdr_size)
193 {
194         struct bat_priv *priv = netdev_priv(soft_iface);
195
196         /* check if enough space is available for pulling, and pull */
197         if (!pskb_may_pull(skb, hdr_size)) {
198                 kfree_skb(skb);
199                 return;
200         }
201         skb_pull_rcsum(skb, hdr_size);
202 /*      skb_set_mac_header(skb, -sizeof(struct ethhdr));*/
203
204         skb->dev = soft_iface;
205         skb->protocol = eth_type_trans(skb, soft_iface);
206
207         /* should not be neccesary anymore as we use skb_pull_rcsum()
208          * TODO: please verify this and remove this TODO
209          * -- Dec 21st 2009, Simon Wunderlich */
210
211 /*      skb->ip_summed = CHECKSUM_UNNECESSARY;*/
212
213         /* TODO: set skb->pkt_type to PACKET_BROADCAST, PACKET_MULTICAST,
214          * PACKET_OTHERHOST or PACKET_HOST */
215
216         priv->stats.rx_packets++;
217         priv->stats.rx_bytes += skb->len + sizeof(struct ethhdr);
218
219         soft_iface->last_rx = jiffies;
220
221         netif_rx(skb);
222 }
223
224 #ifdef HAVE_NET_DEVICE_OPS
225 static const struct net_device_ops bat_netdev_ops = {
226         .ndo_open = interface_open,
227         .ndo_stop = interface_release,
228         .ndo_get_stats = interface_stats,
229         .ndo_set_mac_address = interface_set_mac_addr,
230         .ndo_change_mtu = interface_change_mtu,
231         .ndo_start_xmit = interface_tx,
232         .ndo_validate_addr = eth_validate_addr
233 };
234 #endif
235
236 static void interface_setup(struct net_device *dev)
237 {
238         struct bat_priv *priv = netdev_priv(dev);
239         char dev_addr[ETH_ALEN];
240
241         ether_setup(dev);
242
243 #ifdef HAVE_NET_DEVICE_OPS
244         dev->netdev_ops = &bat_netdev_ops;
245 #else
246         dev->open = interface_open;
247         dev->stop = interface_release;
248         dev->get_stats = interface_stats;
249         dev->set_mac_address = interface_set_mac_addr;
250         dev->change_mtu = interface_change_mtu;
251         dev->hard_start_xmit = interface_tx;
252 #endif
253         dev->destructor = free_netdev;
254
255         /**
256          * can't call min_mtu, because the needed variables
257          * have not been initialized yet
258          */
259         dev->mtu = ETH_DATA_LEN;
260         dev->hard_header_len = BAT_HEADER_LEN; /* reserve more space in the
261                                                 * skbuff for our header */
262
263         /* generate random address */
264         random_ether_addr(dev_addr);
265         memcpy(dev->dev_addr, dev_addr, ETH_ALEN);
266
267         SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
268
269         memset(priv, 0, sizeof(struct bat_priv));
270 }
271
272 struct net_device *softif_create(char *name)
273 {
274         struct net_device *soft_iface;
275         struct bat_priv *bat_priv;
276         int ret;
277
278         soft_iface = alloc_netdev(sizeof(struct bat_priv) , name,
279                                    interface_setup);
280
281         if (!soft_iface) {
282                 pr_err("Unable to allocate the batman interface: %s\n", name);
283                 goto out;
284         }
285
286         ret = register_netdev(soft_iface);
287         if (ret < 0) {
288                 pr_err("Unable to register the batman interface '%s': %i\n",
289                        name, ret);
290                 goto free_soft_iface;
291         }
292
293         bat_priv = netdev_priv(soft_iface);
294
295         atomic_set(&bat_priv->aggregation_enabled, 1);
296         atomic_set(&bat_priv->bonding_enabled, 0);
297         atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE);
298         atomic_set(&bat_priv->orig_interval, 1000);
299         atomic_set(&bat_priv->log_level, 0);
300         atomic_set(&bat_priv->frag_enabled, 1);
301         atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN);
302         atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN);
303
304         atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
305         atomic_set(&bat_priv->bcast_seqno, 1);
306         atomic_set(&bat_priv->hna_local_changed, 0);
307
308         bat_priv->primary_if = NULL;
309         bat_priv->num_ifaces = 0;
310
311         ret = sysfs_add_meshif(soft_iface);
312         if (ret < 0)
313                 goto unreg_soft_iface;
314
315         ret = debugfs_add_meshif(soft_iface);
316         if (ret < 0)
317                 goto unreg_sysfs;
318
319         ret = mesh_init(soft_iface);
320         if (ret < 0)
321                 goto unreg_debugfs;
322
323         return soft_iface;
324
325 unreg_debugfs:
326         debugfs_del_meshif(soft_iface);
327 unreg_sysfs:
328         sysfs_del_meshif(soft_iface);
329 unreg_soft_iface:
330         unregister_netdev(soft_iface);
331         return NULL;
332
333 free_soft_iface:
334         free_netdev(soft_iface);
335 out:
336         return NULL;
337 }
338
339 void softif_destroy(struct net_device *soft_iface)
340 {
341         debugfs_del_meshif(soft_iface);
342         sysfs_del_meshif(soft_iface);
343         mesh_free(soft_iface);
344         unregister_netdevice(soft_iface);
345 }
346
347 /* ethtool */
348 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
349 {
350         cmd->supported = 0;
351         cmd->advertising = 0;
352         cmd->speed = SPEED_10;
353         cmd->duplex = DUPLEX_FULL;
354         cmd->port = PORT_TP;
355         cmd->phy_address = 0;
356         cmd->transceiver = XCVR_INTERNAL;
357         cmd->autoneg = AUTONEG_DISABLE;
358         cmd->maxtxpkt = 0;
359         cmd->maxrxpkt = 0;
360
361         return 0;
362 }
363
364 static void bat_get_drvinfo(struct net_device *dev,
365                             struct ethtool_drvinfo *info)
366 {
367         strcpy(info->driver, "B.A.T.M.A.N. advanced");
368         strcpy(info->version, SOURCE_VERSION);
369         strcpy(info->fw_version, "N/A");
370         strcpy(info->bus_info, "batman");
371 }
372
373 static u32 bat_get_msglevel(struct net_device *dev)
374 {
375         return -EOPNOTSUPP;
376 }
377
378 static void bat_set_msglevel(struct net_device *dev, u32 value)
379 {
380 }
381
382 static u32 bat_get_link(struct net_device *dev)
383 {
384         return 1;
385 }
386
387 static u32 bat_get_rx_csum(struct net_device *dev)
388 {
389         return 0;
390 }
391
392 static int bat_set_rx_csum(struct net_device *dev, u32 data)
393 {
394         return -EOPNOTSUPP;
395 }