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