blob: e56cb88ef2bafa76d67c7d32fc360741af22e7b1 [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann567db7b2012-01-01 00:41:38 +01002 * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
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"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000029#include "hash.h"
30#include "gateway_common.h"
31#include "gateway_client.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000032#include "bat_sysfs.h"
Antonio Quartulli43676ab2011-04-26 21:31:45 +020033#include "originator.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000034#include <linux/slab.h>
35#include <linux/ethtool.h>
36#include <linux/etherdevice.h>
37#include <linux/if_vlan.h>
38#include "unicast.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000039
40
41static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
42static void bat_get_drvinfo(struct net_device *dev,
43 struct ethtool_drvinfo *info);
44static u32 bat_get_msglevel(struct net_device *dev);
45static void bat_set_msglevel(struct net_device *dev, u32 value);
46static u32 bat_get_link(struct net_device *dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000047
48static 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,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000054};
55
56int 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
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000076static int interface_open(struct net_device *dev)
77{
78 netif_start_queue(dev);
79 return 0;
80}
81
82static int interface_release(struct net_device *dev)
83{
84 netif_stop_queue(dev);
85 return 0;
86}
87
88static 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
94static 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
Antonio Quartulli015758d2011-07-09 17:52:13 +0200102 /* only modify transtable if it has been initialized before */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000103 if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) {
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200104 tt_local_remove(bat_priv, dev->dev_addr,
Antonio Quartullicc47f662011-04-27 14:27:57 +0200105 "mac address changed", false);
Antonio Quartullibc279082011-07-07 15:35:35 +0200106 tt_local_add(dev, addr->sa_data, NULL_IFINDEX);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000107 }
108
109 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
Danny Kukawka28009a62012-02-17 05:43:27 +0000110 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000111 return 0;
112}
113
114static 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
Sven Eckelmanndb69ecf2011-06-15 15:17:21 +0200125static int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000126{
127 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
128 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200129 struct hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000130 struct bcast_packet *bcast_packet;
131 struct vlan_ethhdr *vhdr;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200132 unsigned int header_len = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000133 int data_len = skb->len, ret;
134 short vid = -1;
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200135 bool do_bcast = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000136
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:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000152 goto dropped;
Simon Wunderlicha7f6ee92012-01-22 20:00:18 +0100153 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000154
Antonio Quartullia73105b2011-04-27 14:27:44 +0200155 /* Register the client MAC in the transtable */
Antonio Quartullibc279082011-07-07 15:35:35 +0200156 tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000157
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200158 if (is_multicast_ether_addr(ethhdr->h_dest)) {
159 do_bcast = true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000160
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200161 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 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000180 }
181
182 /* ethernet packet should be broadcasted */
183 if (do_bcast) {
Marek Lindner32ae9b22011-04-20 15:40:58 +0200184 primary_if = primary_if_get_selected(bat_priv);
185 if (!primary_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000186 goto dropped;
187
Sven Eckelmann704509b2011-05-14 23:14:54 +0200188 if (my_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000189 goto dropped;
190
191 bcast_packet = (struct bcast_packet *)skb->data;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100192 bcast_packet->header.version = COMPAT_VERSION;
193 bcast_packet->header.ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000194
195 /* batman packet type: broadcast */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100196 bcast_packet->header.packet_type = BAT_BCAST;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000197
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,
Marek Lindner32ae9b22011-04-20 15:40:58 +0200201 primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000202
203 /* set broadcast sequence number */
204 bcast_packet->seqno =
205 htonl(atomic_inc_return(&bat_priv->bcast_seqno));
206
Antonio Quartulli86985292011-06-25 19:09:12 +0200207 add_bcast_packet_to_list(bat_priv, skb, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000208
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 {
Marek Lindnerbe7af5c2011-09-08 13:12:53 +0200215 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
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000221 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
230dropped:
231 kfree_skb(skb);
232dropped_freed:
233 bat_priv->stats.tx_dropped++;
234end:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200235 if (primary_if)
236 hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000237 return NETDEV_TX_OK;
238}
239
240void interface_rx(struct net_device *soft_iface,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000241 struct sk_buff *skb, struct hard_iface *recv_if,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000242 int hdr_size)
243{
244 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000245 struct ethhdr *ethhdr;
246 struct vlan_ethhdr *vhdr;
247 short vid = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000248
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
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000271 /* 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
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300276 /* should not be necessary anymore as we use skb_pull_rcsum()
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000277 * 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
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200287 if (is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest))
288 goto dropped;
289
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000290 netif_rx(skb);
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200291 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292
293dropped:
294 kfree_skb(skb);
295out:
296 return;
297}
298
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000299static 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};
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000308
309static void interface_setup(struct net_device *dev)
310{
311 struct bat_priv *priv = netdev_priv(dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000312
313 ether_setup(dev);
314
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000315 dev->netdev_ops = &bat_netdev_ops;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000316 dev->destructor = free_netdev;
Andrew Lunnaf20b712011-04-17 20:39:07 +0200317 dev->tx_queue_len = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000318
319 /**
320 * can't call min_mtu, because the needed variables
321 * have not been initialized yet
322 */
323 dev->mtu = ETH_DATA_LEN;
Sven Eckelmann6e215fd2011-05-08 12:45:45 +0200324 /* reserve more space in the skbuff for our header */
325 dev->hard_header_len = BAT_HEADER_LEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000326
327 /* generate random address */
Danny Kukawka28009a62012-02-17 05:43:27 +0000328 eth_hw_addr_random(dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000329
330 SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
331
Sven Eckelmann704509b2011-05-14 23:14:54 +0200332 memset(priv, 0, sizeof(*priv));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000333}
334
Sven Eckelmann747e4222011-05-14 23:14:50 +0200335struct net_device *softif_create(const char *name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000336{
337 struct net_device *soft_iface;
338 struct bat_priv *bat_priv;
339 int ret;
340
Sven Eckelmann704509b2011-05-14 23:14:54 +0200341 soft_iface = alloc_netdev(sizeof(*bat_priv), name, interface_setup);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000342
Joe Perches320f4222011-08-29 14:17:24 -0700343 if (!soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000344 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000345
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200346 ret = register_netdevice(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000347 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);
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200357 atomic_set(&bat_priv->ap_isolation, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000358 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);
Marek Lindner8681a1c2012-01-27 23:11:55 +0800363 atomic_set(&bat_priv->hop_penalty, 30);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000364 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);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200371 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;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200377 bat_priv->tt_poss_change = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000378
379 bat_priv->primary_if = NULL;
380 bat_priv->num_ifaces = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000381
Marek Lindner1c280472011-11-28 17:40:17 +0800382 ret = bat_algo_select(bat_priv, bat_routing_algo);
383 if (ret < 0)
384 goto unreg_soft_iface;
385
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000386 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
400unreg_debugfs:
401 debugfs_del_meshif(soft_iface);
402unreg_sysfs:
403 sysfs_del_meshif(soft_iface);
404unreg_soft_iface:
Simon Wunderlich06ba7ce2011-11-07 13:57:48 +0100405 unregister_netdevice(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000406 return NULL;
407
408free_soft_iface:
409 free_netdev(soft_iface);
410out:
411 return NULL;
412}
413
414void 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
Sven Eckelmann747e4222011-05-14 23:14:50 +0200422int softif_is_valid(const struct net_device *net_dev)
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000423{
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000424 if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
425 return 1;
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000426
427 return 0;
428}
429
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000430/* ethtool */
431static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
432{
433 cmd->supported = 0;
434 cmd->advertising = 0;
David Decotigny70739492011-04-27 18:32:40 +0000435 ethtool_cmd_speed_set(cmd, SPEED_10);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000436 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
447static 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
456static u32 bat_get_msglevel(struct net_device *dev)
457{
458 return -EOPNOTSUPP;
459}
460
461static void bat_set_msglevel(struct net_device *dev, u32 value)
462{
463}
464
465static u32 bat_get_link(struct net_device *dev)
466{
467 return 1;
468}