Staging: batman-adv: replace internal logging mechanism.
[firefly-linux-kernel-4.4.55.git] / drivers / staging / batman-adv / soft-interface.c
1 /*
2  * Copyright (C) 2007-2009 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 "send.h"
26 #include "translation-table.h"
27 #include "types.h"
28 #include "hash.h"
29 #include <linux/ethtool.h>
30 #include <linux/etherdevice.h>
31 #include "compat.h"
32
33 static uint16_t bcast_seqno = 1; /* give own bcast messages seq numbers to avoid
34                                   * broadcast storms */
35 static int32_t skb_packets;
36 static int32_t skb_bad_packets;
37 static int32_t lock_dropped;
38
39 unsigned char mainIfAddr[ETH_ALEN];
40 static unsigned char mainIfAddr_default[ETH_ALEN];
41 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
42 static void bat_get_drvinfo(struct net_device *dev,
43                             struct ethtool_drvinfo *info);
44 static u32 bat_get_msglevel(struct net_device *dev);
45 static void bat_set_msglevel(struct net_device *dev, u32 value);
46 static u32 bat_get_link(struct net_device *dev);
47 static u32 bat_get_rx_csum(struct net_device *dev);
48 static int bat_set_rx_csum(struct net_device *dev, u32 data);
49
50 static const struct ethtool_ops bat_ethtool_ops = {
51         .get_settings = bat_get_settings,
52         .get_drvinfo = bat_get_drvinfo,
53         .get_msglevel = bat_get_msglevel,
54         .set_msglevel = bat_set_msglevel,
55         .get_link = bat_get_link,
56         .get_rx_csum = bat_get_rx_csum,
57         .set_rx_csum = bat_set_rx_csum
58 };
59
60 void set_main_if_addr(uint8_t *addr)
61 {
62         memcpy(mainIfAddr, addr, ETH_ALEN);
63 }
64
65 int main_if_was_up(void)
66 {
67         return (memcmp(mainIfAddr, mainIfAddr_default, ETH_ALEN) != 0 ? 1 : 0);
68 }
69
70 static int my_skb_push(struct sk_buff *skb, unsigned int len)
71 {
72         int result = 0;
73
74         skb_packets++;
75         if (skb->data - len < skb->head) {
76                 skb_bad_packets++;
77                 result = pskb_expand_head(skb, len, 0, GFP_ATOMIC);
78
79                 if (result < 0)
80                         return result;
81         }
82
83         skb_push(skb, len);
84         return 0;
85 }
86
87 #ifdef HAVE_NET_DEVICE_OPS
88 static const struct net_device_ops bat_netdev_ops = {
89         .ndo_open = interface_open,
90         .ndo_stop = interface_release,
91         .ndo_get_stats = interface_stats,
92         .ndo_set_mac_address = interface_set_mac_addr,
93         .ndo_change_mtu = interface_change_mtu,
94         .ndo_start_xmit = interface_tx,
95         .ndo_validate_addr = eth_validate_addr
96 };
97 #endif
98
99 void interface_setup(struct net_device *dev)
100 {
101         struct bat_priv *priv = netdev_priv(dev);
102         char dev_addr[ETH_ALEN];
103
104         ether_setup(dev);
105
106 #ifdef HAVE_NET_DEVICE_OPS
107         dev->netdev_ops = &bat_netdev_ops;
108 #else
109         dev->open = interface_open;
110         dev->stop = interface_release;
111         dev->get_stats = interface_stats;
112         dev->set_mac_address = interface_set_mac_addr;
113         dev->change_mtu = interface_change_mtu;
114         dev->hard_start_xmit = interface_tx;
115 #endif
116         dev->destructor = free_netdev;
117
118         dev->mtu = hardif_min_mtu();
119         dev->hard_header_len = BAT_HEADER_LEN; /* reserve more space in the
120                                                 * skbuff for our header */
121
122         /* generate random address */
123         random_ether_addr(dev_addr);
124         memcpy(dev->dev_addr, dev_addr, sizeof(dev->dev_addr));
125
126         SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
127
128         memset(priv, 0, sizeof(struct bat_priv));
129 }
130
131 int interface_open(struct net_device *dev)
132 {
133         netif_start_queue(dev);
134         return 0;
135 }
136
137 int interface_release(struct net_device *dev)
138 {
139         netif_stop_queue(dev);
140         return 0;
141 }
142
143 struct net_device_stats *interface_stats(struct net_device *dev)
144 {
145         struct bat_priv *priv = netdev_priv(dev);
146         return &priv->stats;
147 }
148
149 int interface_set_mac_addr(struct net_device *dev, void *addr)
150 {
151         return -EBUSY;
152 }
153
154 int interface_change_mtu(struct net_device *dev, int new_mtu)
155 {
156         /* check ranges */
157         if ((new_mtu < 68) || (new_mtu > hardif_min_mtu()))
158                 return -EINVAL;
159
160         dev->mtu = new_mtu;
161
162         return 0;
163 }
164
165 int interface_tx(struct sk_buff *skb, struct net_device *dev)
166 {
167         struct unicast_packet *unicast_packet;
168         struct bcast_packet *bcast_packet;
169         struct orig_node *orig_node;
170         struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
171         struct bat_priv *priv = netdev_priv(dev);
172         int data_len = skb->len;
173
174         if (atomic_read(&module_state) != MODULE_ACTIVE)
175                 goto dropped;
176
177         dev->trans_start = jiffies;
178         /* TODO: check this for locks */
179         hna_local_add(ethhdr->h_source);
180
181         /* ethernet packet should be broadcasted */
182         if (is_bcast(ethhdr->h_dest) || is_mcast(ethhdr->h_dest)) {
183
184                 if (my_skb_push(skb, sizeof(struct bcast_packet)) < 0)
185                         goto dropped;
186
187                 bcast_packet = (struct bcast_packet *)skb->data;
188
189                 bcast_packet->version = COMPAT_VERSION;
190
191                 /* batman packet type: broadcast */
192                 bcast_packet->packet_type = BAT_BCAST;
193
194                 /* hw address of first interface is the orig mac because only
195                  * this mac is known throughout the mesh */
196                 memcpy(bcast_packet->orig, mainIfAddr, ETH_ALEN);
197                 /* set broadcast sequence number */
198                 bcast_packet->seqno = htons(bcast_seqno);
199
200                 bcast_seqno++;
201
202                 /* broadcast packet */
203                 add_bcast_packet_to_list(skb->data, skb->len);
204
205         /* unicast packet */
206         } else {
207
208                 /* simply spin_lock()ing can deadlock when the lock is already
209                  * hold. */
210                 /* TODO: defer the work in a working queue instead of
211                  * dropping */
212                 if (!spin_trylock(&orig_hash_lock)) {
213                         lock_dropped++;
214                         printk(KERN_WARNING "batman-adv:%d packets dropped because lock was hold\n", lock_dropped);
215                         goto dropped;
216                 }
217
218                 /* get routing information */
219                 orig_node = ((struct orig_node *)hash_find(orig_hash,
220                                                            ethhdr->h_dest));
221
222                 /* check for hna host */
223                 if (!orig_node)
224                         orig_node = transtable_search(ethhdr->h_dest);
225
226                 if ((orig_node) &&
227                     (orig_node->batman_if) &&
228                     (orig_node->router)) {
229                         if (my_skb_push(skb, sizeof(struct unicast_packet)) < 0)
230                                 goto unlock;
231
232                         unicast_packet = (struct unicast_packet *)skb->data;
233
234                         unicast_packet->version = COMPAT_VERSION;
235                         /* batman packet type: unicast */
236                         unicast_packet->packet_type = BAT_UNICAST;
237                         /* set unicast ttl */
238                         unicast_packet->ttl = TTL;
239                         /* copy the destination for faster routing */
240                         memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
241
242                         /* net_dev won't be available when not active */
243                         if (orig_node->batman_if->if_active != IF_ACTIVE)
244                                 goto unlock;
245
246                         send_raw_packet(skb->data, skb->len,
247                                         orig_node->batman_if,
248                                         orig_node->router->addr);
249                 } else {
250                         goto unlock;
251                 }
252
253                 spin_unlock(&orig_hash_lock);
254         }
255
256         priv->stats.tx_packets++;
257         priv->stats.tx_bytes += data_len;
258         goto end;
259
260 unlock:
261         spin_unlock(&orig_hash_lock);
262 dropped:
263         priv->stats.tx_dropped++;
264 end:
265         kfree_skb(skb);
266         return 0;
267 }
268
269 void interface_rx(struct net_device *dev, void *packet, int packet_len)
270 {
271         struct sk_buff *skb;
272         struct bat_priv *priv = netdev_priv(dev);
273
274         skb = dev_alloc_skb(packet_len);
275
276         if (!skb) {
277                 priv->stats.rx_dropped++;
278                 goto out;
279         }
280
281         memcpy(skb_put(skb, packet_len), packet, packet_len);
282
283         /* Write metadata, and then pass to the receive level */
284         skb->dev = dev;
285         skb->protocol = eth_type_trans(skb, dev);
286         skb->ip_summed = CHECKSUM_UNNECESSARY;
287
288         priv->stats.rx_packets++;
289         priv->stats.rx_bytes += packet_len;
290
291         dev->last_rx = jiffies;
292
293         netif_rx(skb);
294
295 out:
296         return;
297 }
298
299 /* ethtool */
300 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
301 {
302         cmd->supported = 0;
303         cmd->advertising = 0;
304         cmd->speed = SPEED_10;
305         cmd->duplex = DUPLEX_FULL;
306         cmd->port = PORT_TP;
307         cmd->phy_address = 0;
308         cmd->transceiver = XCVR_INTERNAL;
309         cmd->autoneg = AUTONEG_DISABLE;
310         cmd->maxtxpkt = 0;
311         cmd->maxrxpkt = 0;
312
313         return 0;
314 }
315
316 static void bat_get_drvinfo(struct net_device *dev,
317                             struct ethtool_drvinfo *info)
318 {
319         strcpy(info->driver, "B.A.T.M.A.N. advanced");
320         strcpy(info->version, SOURCE_VERSION);
321         strcpy(info->fw_version, "N/A");
322         strcpy(info->bus_info, "batman");
323 }
324
325 static u32 bat_get_msglevel(struct net_device *dev)
326 {
327         return -EOPNOTSUPP;
328 }
329
330 static void bat_set_msglevel(struct net_device *dev, u32 value)
331 {
332         return;
333 }
334
335 static u32 bat_get_link(struct net_device *dev)
336 {
337         return 1;
338 }
339
340 static u32 bat_get_rx_csum(struct net_device *dev)
341 {
342         return 0;
343 }
344
345 static int bat_set_rx_csum(struct net_device *dev, u32 data)
346 {
347         return -EOPNOTSUPP;
348 }