batman-adv: remove old bridge loop avoidance code
[firefly-linux-kernel-4.4.55.git] / net / batman-adv / soft-interface.c
1 /*
2  * Copyright (C) 2007-2012 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 "hash.h"
30 #include "gateway_common.h"
31 #include "gateway_client.h"
32 #include "bat_sysfs.h"
33 #include "originator.h"
34 #include <linux/slab.h>
35 #include <linux/ethtool.h>
36 #include <linux/etherdevice.h>
37 #include <linux/if_vlan.h>
38 #include "unicast.h"
39
40
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
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 };
55
56 int my_skb_head_push(struct sk_buff *skb, unsigned int len)
57 {
58         int result;
59
60         /**
61          * TODO: We must check if we can release all references to non-payload
62          * data using skb_header_release in our skbs to allow skb_cow_header to
63          * work optimally. This means that those skbs are not allowed to read
64          * or write any data which is before the current position of skb->data
65          * after that call and thus allow other skbs with the same data buffer
66          * to write freely in that area.
67          */
68         result = skb_cow_head(skb, len);
69         if (result < 0)
70                 return result;
71
72         skb_push(skb, len);
73         return 0;
74 }
75
76 static int interface_open(struct net_device *dev)
77 {
78         netif_start_queue(dev);
79         return 0;
80 }
81
82 static int interface_release(struct net_device *dev)
83 {
84         netif_stop_queue(dev);
85         return 0;
86 }
87
88 static struct net_device_stats *interface_stats(struct net_device *dev)
89 {
90         struct bat_priv *bat_priv = netdev_priv(dev);
91         return &bat_priv->stats;
92 }
93
94 static int interface_set_mac_addr(struct net_device *dev, void *p)
95 {
96         struct bat_priv *bat_priv = netdev_priv(dev);
97         struct sockaddr *addr = p;
98
99         if (!is_valid_ether_addr(addr->sa_data))
100                 return -EADDRNOTAVAIL;
101
102         /* only modify transtable if it has been initialized before */
103         if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) {
104                 tt_local_remove(bat_priv, dev->dev_addr,
105                                 "mac address changed", false);
106                 tt_local_add(dev, addr->sa_data, NULL_IFINDEX);
107         }
108
109         memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
110         dev->addr_assign_type &= ~NET_ADDR_RANDOM;
111         return 0;
112 }
113
114 static int interface_change_mtu(struct net_device *dev, int new_mtu)
115 {
116         /* check ranges */
117         if ((new_mtu < 68) || (new_mtu > hardif_min_mtu(dev)))
118                 return -EINVAL;
119
120         dev->mtu = new_mtu;
121
122         return 0;
123 }
124
125 static int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
126 {
127         struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
128         struct bat_priv *bat_priv = netdev_priv(soft_iface);
129         struct hard_iface *primary_if = NULL;
130         struct bcast_packet *bcast_packet;
131         struct vlan_ethhdr *vhdr;
132         unsigned int header_len = 0;
133         int data_len = skb->len, ret;
134         short vid = -1;
135         bool do_bcast = false;
136
137         if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
138                 goto dropped;
139
140         soft_iface->trans_start = jiffies;
141
142         switch (ntohs(ethhdr->h_proto)) {
143         case ETH_P_8021Q:
144                 vhdr = (struct vlan_ethhdr *)skb->data;
145                 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
146
147                 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
148                         break;
149
150                 /* fall through */
151         case ETH_P_BATMAN:
152                 goto dropped;
153         }
154
155         /* Register the client MAC in the transtable */
156         tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif);
157
158         if (is_multicast_ether_addr(ethhdr->h_dest)) {
159                 do_bcast = true;
160
161                 switch (atomic_read(&bat_priv->gw_mode)) {
162                 case GW_MODE_SERVER:
163                         /* gateway servers should not send dhcp
164                          * requests into the mesh */
165                         ret = gw_is_dhcp_target(skb, &header_len);
166                         if (ret)
167                                 goto dropped;
168                         break;
169                 case GW_MODE_CLIENT:
170                         /* gateway clients should send dhcp requests
171                          * via unicast to their gateway */
172                         ret = gw_is_dhcp_target(skb, &header_len);
173                         if (ret)
174                                 do_bcast = false;
175                         break;
176                 case GW_MODE_OFF:
177                 default:
178                         break;
179                 }
180         }
181
182         /* ethernet packet should be broadcasted */
183         if (do_bcast) {
184                 primary_if = primary_if_get_selected(bat_priv);
185                 if (!primary_if)
186                         goto dropped;
187
188                 if (my_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
189                         goto dropped;
190
191                 bcast_packet = (struct bcast_packet *)skb->data;
192                 bcast_packet->header.version = COMPAT_VERSION;
193                 bcast_packet->header.ttl = TTL;
194
195                 /* batman packet type: broadcast */
196                 bcast_packet->header.packet_type = BAT_BCAST;
197
198                 /* hw address of first interface is the orig mac because only
199                  * this mac is known throughout the mesh */
200                 memcpy(bcast_packet->orig,
201                        primary_if->net_dev->dev_addr, ETH_ALEN);
202
203                 /* set broadcast sequence number */
204                 bcast_packet->seqno =
205                         htonl(atomic_inc_return(&bat_priv->bcast_seqno));
206
207                 add_bcast_packet_to_list(bat_priv, skb, 1);
208
209                 /* a copy is stored in the bcast list, therefore removing
210                  * the original skb. */
211                 kfree_skb(skb);
212
213         /* unicast packet */
214         } else {
215                 if (atomic_read(&bat_priv->gw_mode) != GW_MODE_OFF) {
216                         ret = gw_out_of_range(bat_priv, skb, ethhdr);
217                         if (ret)
218                                 goto dropped;
219                 }
220
221                 ret = unicast_send_skb(skb, bat_priv);
222                 if (ret != 0)
223                         goto dropped_freed;
224         }
225
226         bat_priv->stats.tx_packets++;
227         bat_priv->stats.tx_bytes += data_len;
228         goto end;
229
230 dropped:
231         kfree_skb(skb);
232 dropped_freed:
233         bat_priv->stats.tx_dropped++;
234 end:
235         if (primary_if)
236                 hardif_free_ref(primary_if);
237         return NETDEV_TX_OK;
238 }
239
240 void interface_rx(struct net_device *soft_iface,
241                   struct sk_buff *skb, struct hard_iface *recv_if,
242                   int hdr_size)
243 {
244         struct bat_priv *bat_priv = netdev_priv(soft_iface);
245         struct ethhdr *ethhdr;
246         struct vlan_ethhdr *vhdr;
247         short vid = -1;
248
249         /* check if enough space is available for pulling, and pull */
250         if (!pskb_may_pull(skb, hdr_size))
251                 goto dropped;
252
253         skb_pull_rcsum(skb, hdr_size);
254         skb_reset_mac_header(skb);
255
256         ethhdr = (struct ethhdr *)skb_mac_header(skb);
257
258         switch (ntohs(ethhdr->h_proto)) {
259         case ETH_P_8021Q:
260                 vhdr = (struct vlan_ethhdr *)skb->data;
261                 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
262
263                 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
264                         break;
265
266                 /* fall through */
267         case ETH_P_BATMAN:
268                 goto dropped;
269         }
270
271         /* skb->dev & skb->pkt_type are set here */
272         if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
273                 goto dropped;
274         skb->protocol = eth_type_trans(skb, soft_iface);
275
276         /* should not be necessary anymore as we use skb_pull_rcsum()
277          * TODO: please verify this and remove this TODO
278          * -- Dec 21st 2009, Simon Wunderlich */
279
280 /*      skb->ip_summed = CHECKSUM_UNNECESSARY;*/
281
282         bat_priv->stats.rx_packets++;
283         bat_priv->stats.rx_bytes += skb->len + sizeof(struct ethhdr);
284
285         soft_iface->last_rx = jiffies;
286
287         if (is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest))
288                 goto dropped;
289
290         netif_rx(skb);
291         goto out;
292
293 dropped:
294         kfree_skb(skb);
295 out:
296         return;
297 }
298
299 static const struct net_device_ops bat_netdev_ops = {
300         .ndo_open = interface_open,
301         .ndo_stop = interface_release,
302         .ndo_get_stats = interface_stats,
303         .ndo_set_mac_address = interface_set_mac_addr,
304         .ndo_change_mtu = interface_change_mtu,
305         .ndo_start_xmit = interface_tx,
306         .ndo_validate_addr = eth_validate_addr
307 };
308
309 static void interface_setup(struct net_device *dev)
310 {
311         struct bat_priv *priv = netdev_priv(dev);
312
313         ether_setup(dev);
314
315         dev->netdev_ops = &bat_netdev_ops;
316         dev->destructor = free_netdev;
317         dev->tx_queue_len = 0;
318
319         /**
320          * can't call min_mtu, because the needed variables
321          * have not been initialized yet
322          */
323         dev->mtu = ETH_DATA_LEN;
324         /* reserve more space in the skbuff for our header */
325         dev->hard_header_len = BAT_HEADER_LEN;
326
327         /* generate random address */
328         eth_hw_addr_random(dev);
329
330         SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
331
332         memset(priv, 0, sizeof(*priv));
333 }
334
335 struct net_device *softif_create(const char *name)
336 {
337         struct net_device *soft_iface;
338         struct bat_priv *bat_priv;
339         int ret;
340
341         soft_iface = alloc_netdev(sizeof(*bat_priv), name, interface_setup);
342
343         if (!soft_iface)
344                 goto out;
345
346         ret = register_netdevice(soft_iface);
347         if (ret < 0) {
348                 pr_err("Unable to register the batman interface '%s': %i\n",
349                        name, ret);
350                 goto free_soft_iface;
351         }
352
353         bat_priv = netdev_priv(soft_iface);
354
355         atomic_set(&bat_priv->aggregated_ogms, 1);
356         atomic_set(&bat_priv->bonding, 0);
357         atomic_set(&bat_priv->ap_isolation, 0);
358         atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE);
359         atomic_set(&bat_priv->gw_mode, GW_MODE_OFF);
360         atomic_set(&bat_priv->gw_sel_class, 20);
361         atomic_set(&bat_priv->gw_bandwidth, 41);
362         atomic_set(&bat_priv->orig_interval, 1000);
363         atomic_set(&bat_priv->hop_penalty, 30);
364         atomic_set(&bat_priv->log_level, 0);
365         atomic_set(&bat_priv->fragmentation, 1);
366         atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN);
367         atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN);
368
369         atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
370         atomic_set(&bat_priv->bcast_seqno, 1);
371         atomic_set(&bat_priv->ttvn, 0);
372         atomic_set(&bat_priv->tt_local_changes, 0);
373         atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
374
375         bat_priv->tt_buff = NULL;
376         bat_priv->tt_buff_len = 0;
377         bat_priv->tt_poss_change = false;
378
379         bat_priv->primary_if = NULL;
380         bat_priv->num_ifaces = 0;
381
382         ret = bat_algo_select(bat_priv, bat_routing_algo);
383         if (ret < 0)
384                 goto unreg_soft_iface;
385
386         ret = sysfs_add_meshif(soft_iface);
387         if (ret < 0)
388                 goto unreg_soft_iface;
389
390         ret = debugfs_add_meshif(soft_iface);
391         if (ret < 0)
392                 goto unreg_sysfs;
393
394         ret = mesh_init(soft_iface);
395         if (ret < 0)
396                 goto unreg_debugfs;
397
398         return soft_iface;
399
400 unreg_debugfs:
401         debugfs_del_meshif(soft_iface);
402 unreg_sysfs:
403         sysfs_del_meshif(soft_iface);
404 unreg_soft_iface:
405         unregister_netdevice(soft_iface);
406         return NULL;
407
408 free_soft_iface:
409         free_netdev(soft_iface);
410 out:
411         return NULL;
412 }
413
414 void softif_destroy(struct net_device *soft_iface)
415 {
416         debugfs_del_meshif(soft_iface);
417         sysfs_del_meshif(soft_iface);
418         mesh_free(soft_iface);
419         unregister_netdevice(soft_iface);
420 }
421
422 int softif_is_valid(const struct net_device *net_dev)
423 {
424         if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
425                 return 1;
426
427         return 0;
428 }
429
430 /* ethtool */
431 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
432 {
433         cmd->supported = 0;
434         cmd->advertising = 0;
435         ethtool_cmd_speed_set(cmd, SPEED_10);
436         cmd->duplex = DUPLEX_FULL;
437         cmd->port = PORT_TP;
438         cmd->phy_address = 0;
439         cmd->transceiver = XCVR_INTERNAL;
440         cmd->autoneg = AUTONEG_DISABLE;
441         cmd->maxtxpkt = 0;
442         cmd->maxrxpkt = 0;
443
444         return 0;
445 }
446
447 static void bat_get_drvinfo(struct net_device *dev,
448                             struct ethtool_drvinfo *info)
449 {
450         strcpy(info->driver, "B.A.T.M.A.N. advanced");
451         strcpy(info->version, SOURCE_VERSION);
452         strcpy(info->fw_version, "N/A");
453         strcpy(info->bus_info, "batman");
454 }
455
456 static u32 bat_get_msglevel(struct net_device *dev)
457 {
458         return -EOPNOTSUPP;
459 }
460
461 static void bat_set_msglevel(struct net_device *dev, u32 value)
462 {
463 }
464
465 static u32 bat_get_link(struct net_device *dev)
466 {
467         return 1;
468 }