864f85d11c77f1cb94d80b495c06c74bec18f63f
[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 static uint32_t bcast_seqno = 1; /* give own bcast messages seq numbers to avoid
39                                   * broadcast storms */
40
41 unsigned char main_if_addr[ETH_ALEN];
42 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
43 static void bat_get_drvinfo(struct net_device *dev,
44                             struct ethtool_drvinfo *info);
45 static u32 bat_get_msglevel(struct net_device *dev);
46 static void bat_set_msglevel(struct net_device *dev, u32 value);
47 static u32 bat_get_link(struct net_device *dev);
48 static u32 bat_get_rx_csum(struct net_device *dev);
49 static int bat_set_rx_csum(struct net_device *dev, u32 data);
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_rx_csum = bat_get_rx_csum,
58         .set_rx_csum = bat_set_rx_csum
59 };
60
61 void set_main_if_addr(uint8_t *addr)
62 {
63         memcpy(main_if_addr, addr, ETH_ALEN);
64 }
65
66 int my_skb_head_push(struct sk_buff *skb, unsigned int len)
67 {
68         int result;
69
70         /**
71          * TODO: We must check if we can release all references to non-payload
72          * data using skb_header_release in our skbs to allow skb_cow_header to
73          * work optimally. This means that those skbs are not allowed to read
74          * or write any data which is before the current position of skb->data
75          * after that call and thus allow other skbs with the same data buffer
76          * to write freely in that area.
77          */
78         result = skb_cow_head(skb, len);
79
80         if (result < 0)
81                 return result;
82
83         skb_push(skb, len);
84         return 0;
85 }
86
87 static int interface_open(struct net_device *dev)
88 {
89         netif_start_queue(dev);
90         return 0;
91 }
92
93 static int interface_release(struct net_device *dev)
94 {
95         netif_stop_queue(dev);
96         return 0;
97 }
98
99 static struct net_device_stats *interface_stats(struct net_device *dev)
100 {
101         struct bat_priv *bat_priv = netdev_priv(dev);
102         return &bat_priv->stats;
103 }
104
105 static int interface_set_mac_addr(struct net_device *dev, void *p)
106 {
107         struct bat_priv *bat_priv = netdev_priv(dev);
108         struct sockaddr *addr = p;
109
110         if (!is_valid_ether_addr(addr->sa_data))
111                 return -EADDRNOTAVAIL;
112
113         /* only modify hna-table if it has been initialised before */
114         if (atomic_read(&module_state) == MODULE_ACTIVE) {
115                 hna_local_remove(bat_priv, dev->dev_addr,
116                                  "mac address changed");
117                 hna_local_add(dev, addr->sa_data);
118         }
119
120         memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
121
122         return 0;
123 }
124
125 static int interface_change_mtu(struct net_device *dev, int new_mtu)
126 {
127         /* check ranges */
128         if ((new_mtu < 68) || (new_mtu > hardif_min_mtu(dev)))
129                 return -EINVAL;
130
131         dev->mtu = new_mtu;
132
133         return 0;
134 }
135
136 int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
137 {
138         struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
139         struct bat_priv *bat_priv = netdev_priv(soft_iface);
140         struct bcast_packet *bcast_packet;
141         int data_len = skb->len, ret;
142
143         if (atomic_read(&module_state) != MODULE_ACTIVE)
144                 goto dropped;
145
146         soft_iface->trans_start = jiffies;
147
148         /* TODO: check this for locks */
149         hna_local_add(soft_iface, ethhdr->h_source);
150
151         /* ethernet packet should be broadcasted */
152         if (is_bcast(ethhdr->h_dest) || is_mcast(ethhdr->h_dest)) {
153
154                 if (my_skb_head_push(skb, sizeof(struct bcast_packet)) < 0)
155                         goto dropped;
156
157                 bcast_packet = (struct bcast_packet *)skb->data;
158                 bcast_packet->version = COMPAT_VERSION;
159                 bcast_packet->ttl = TTL;
160
161                 /* batman packet type: broadcast */
162                 bcast_packet->packet_type = BAT_BCAST;
163
164                 /* hw address of first interface is the orig mac because only
165                  * this mac is known throughout the mesh */
166                 memcpy(bcast_packet->orig, main_if_addr, ETH_ALEN);
167
168                 /* set broadcast sequence number */
169                 bcast_packet->seqno = htonl(bcast_seqno);
170
171                 /* broadcast packet. on success, increase seqno. */
172                 if (add_bcast_packet_to_list(bat_priv, skb) == NETDEV_TX_OK)
173                         bcast_seqno++;
174
175                 /* a copy is stored in the bcast list, therefore removing
176                  * the original skb. */
177                 kfree_skb(skb);
178
179         /* unicast packet */
180         } else {
181                 ret = unicast_send_skb(skb, bat_priv);
182                 if (ret != 0) {
183                         bat_priv->stats.tx_dropped++;
184                         goto end;
185                 }
186         }
187
188         bat_priv->stats.tx_packets++;
189         bat_priv->stats.tx_bytes += data_len;
190         goto end;
191
192 dropped:
193         bat_priv->stats.tx_dropped++;
194         kfree_skb(skb);
195 end:
196         return NETDEV_TX_OK;
197 }
198
199 void interface_rx(struct net_device *soft_iface,
200                   struct sk_buff *skb, int hdr_size)
201 {
202         struct bat_priv *priv = netdev_priv(soft_iface);
203
204         /* check if enough space is available for pulling, and pull */
205         if (!pskb_may_pull(skb, hdr_size)) {
206                 kfree_skb(skb);
207                 return;
208         }
209         skb_pull_rcsum(skb, hdr_size);
210 /*      skb_set_mac_header(skb, -sizeof(struct ethhdr));*/
211
212         skb->dev = soft_iface;
213         skb->protocol = eth_type_trans(skb, soft_iface);
214
215         /* should not be neccesary anymore as we use skb_pull_rcsum()
216          * TODO: please verify this and remove this TODO
217          * -- Dec 21st 2009, Simon Wunderlich */
218
219 /*      skb->ip_summed = CHECKSUM_UNNECESSARY;*/
220
221         /* TODO: set skb->pkt_type to PACKET_BROADCAST, PACKET_MULTICAST,
222          * PACKET_OTHERHOST or PACKET_HOST */
223
224         priv->stats.rx_packets++;
225         priv->stats.rx_bytes += skb->len + sizeof(struct ethhdr);
226
227         soft_iface->last_rx = jiffies;
228
229         netif_rx(skb);
230 }
231
232 #ifdef HAVE_NET_DEVICE_OPS
233 static const struct net_device_ops bat_netdev_ops = {
234         .ndo_open = interface_open,
235         .ndo_stop = interface_release,
236         .ndo_get_stats = interface_stats,
237         .ndo_set_mac_address = interface_set_mac_addr,
238         .ndo_change_mtu = interface_change_mtu,
239         .ndo_start_xmit = interface_tx,
240         .ndo_validate_addr = eth_validate_addr
241 };
242 #endif
243
244 static void interface_setup(struct net_device *dev)
245 {
246         struct bat_priv *priv = netdev_priv(dev);
247         char dev_addr[ETH_ALEN];
248
249         ether_setup(dev);
250
251 #ifdef HAVE_NET_DEVICE_OPS
252         dev->netdev_ops = &bat_netdev_ops;
253 #else
254         dev->open = interface_open;
255         dev->stop = interface_release;
256         dev->get_stats = interface_stats;
257         dev->set_mac_address = interface_set_mac_addr;
258         dev->change_mtu = interface_change_mtu;
259         dev->hard_start_xmit = interface_tx;
260 #endif
261         dev->destructor = free_netdev;
262
263         /**
264          * can't call min_mtu, because the needed variables
265          * have not been initialized yet
266          */
267         dev->mtu = ETH_DATA_LEN;
268         dev->hard_header_len = BAT_HEADER_LEN; /* reserve more space in the
269                                                 * skbuff for our header */
270
271         /* generate random address */
272         random_ether_addr(dev_addr);
273         memcpy(dev->dev_addr, dev_addr, ETH_ALEN);
274
275         SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
276
277         memset(priv, 0, sizeof(struct bat_priv));
278 }
279
280 struct net_device *softif_create(char *name)
281 {
282         struct net_device *soft_iface;
283         struct bat_priv *bat_priv;
284         int ret;
285
286         soft_iface = alloc_netdev(sizeof(struct bat_priv) , name,
287                                    interface_setup);
288
289         if (!soft_iface) {
290                 pr_err("Unable to allocate the batman interface: %s\n", name);
291                 goto out;
292         }
293
294         ret = register_netdev(soft_iface);
295
296         if (ret < 0) {
297                 pr_err("Unable to register the batman interface '%s': %i\n",
298                        name, ret);
299                 goto free_soft_iface;
300         }
301
302         bat_priv = netdev_priv(soft_iface);
303
304         atomic_set(&bat_priv->aggregation_enabled, 1);
305         atomic_set(&bat_priv->bonding_enabled, 0);
306         atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE);
307         atomic_set(&bat_priv->orig_interval, 1000);
308         atomic_set(&bat_priv->log_level, 0);
309         atomic_set(&bat_priv->frag_enabled, 1);
310         atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN);
311         atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN);
312
313         bat_priv->primary_if = NULL;
314         bat_priv->num_ifaces = 0;
315
316         ret = sysfs_add_meshif(soft_iface);
317
318         if (ret < 0)
319                 goto unreg_soft_iface;
320
321         ret = debugfs_add_meshif(soft_iface);
322
323         if (ret < 0)
324                 goto unreg_sysfs;
325
326         return soft_iface;
327
328 unreg_sysfs:
329         sysfs_del_meshif(soft_iface);
330 unreg_soft_iface:
331         unregister_netdev(soft_iface);
332         return NULL;
333
334 free_soft_iface:
335         free_netdev(soft_iface);
336 out:
337         return NULL;
338 }
339
340 void softif_destroy(struct net_device *soft_iface)
341 {
342         debugfs_del_meshif(soft_iface);
343         sysfs_del_meshif(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 }