2 * Copyright (C) 2007-2010 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
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.
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.
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
23 #include "hard-interface.h"
24 #include "soft-interface.h"
26 #include "translation-table.h"
28 #include "bat_sysfs.h"
29 #include "originator.h"
32 #include <linux/if_arp.h>
33 #include <linux/netfilter_bridge.h>
35 #define MIN(x, y) ((x) < (y) ? (x) : (y))
37 struct batman_if *get_batman_if_by_netdev(struct net_device *net_dev)
39 struct batman_if *batman_if;
42 list_for_each_entry_rcu(batman_if, &if_list, list) {
43 if (batman_if->net_dev == net_dev)
54 static int is_valid_iface(struct net_device *net_dev)
56 if (net_dev->flags & IFF_LOOPBACK)
59 if (net_dev->type != ARPHRD_ETHER)
62 if (net_dev->addr_len != ETH_ALEN)
65 /* no batman over batman */
66 #ifdef HAVE_NET_DEVICE_OPS
67 if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
70 if (net_dev->hard_start_xmit == interface_tx)
74 /* Device is being bridged */
75 /* if (net_dev->priv_flags & IFF_BRIDGE_PORT)
81 static struct batman_if *get_active_batman_if(void)
83 struct batman_if *batman_if;
85 /* TODO: should check interfaces belonging to bat_priv */
87 list_for_each_entry_rcu(batman_if, &if_list, list) {
88 if (batman_if->if_status == IF_ACTIVE)
99 static void set_primary_if(struct bat_priv *bat_priv,
100 struct batman_if *batman_if)
102 struct batman_packet *batman_packet;
104 bat_priv->primary_if = batman_if;
106 if (!bat_priv->primary_if)
109 set_main_if_addr(batman_if->net_dev->dev_addr);
111 batman_packet = (struct batman_packet *)(batman_if->packet_buff);
112 batman_packet->flags = PRIMARIES_FIRST_HOP;
113 batman_packet->ttl = TTL;
116 * hacky trick to make sure that we send the HNA information via
117 * our new primary interface
119 atomic_set(&hna_local_changed, 1);
122 static bool hardif_is_iface_up(struct batman_if *batman_if)
124 if (batman_if->net_dev->flags & IFF_UP)
130 static void update_mac_addresses(struct batman_if *batman_if)
132 if (!batman_if || !batman_if->packet_buff)
135 addr_to_string(batman_if->addr_str, batman_if->net_dev->dev_addr);
137 memcpy(((struct batman_packet *)(batman_if->packet_buff))->orig,
138 batman_if->net_dev->dev_addr, ETH_ALEN);
139 memcpy(((struct batman_packet *)(batman_if->packet_buff))->prev_sender,
140 batman_if->net_dev->dev_addr, ETH_ALEN);
143 static void check_known_mac_addr(uint8_t *addr)
145 struct batman_if *batman_if;
148 list_for_each_entry_rcu(batman_if, &if_list, list) {
149 if ((batman_if->if_status != IF_ACTIVE) &&
150 (batman_if->if_status != IF_TO_BE_ACTIVATED))
153 if (!compare_orig(batman_if->net_dev->dev_addr, addr))
156 pr_warning("The newly added mac address (%pM) already exists "
157 "on: %s\n", addr, batman_if->dev);
158 pr_warning("It is strongly recommended to keep mac addresses "
159 "unique to avoid problems!\n");
164 int hardif_min_mtu(void)
166 struct batman_if *batman_if;
167 /* allow big frames if all devices are capable to do so
168 * (have MTU > 1500 + BAT_HEADER_LEN) */
169 int min_mtu = ETH_DATA_LEN;
172 list_for_each_entry_rcu(batman_if, &if_list, list) {
173 if ((batman_if->if_status == IF_ACTIVE) ||
174 (batman_if->if_status == IF_TO_BE_ACTIVATED))
175 min_mtu = MIN(batman_if->net_dev->mtu - BAT_HEADER_LEN,
183 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
184 void update_min_mtu(void)
188 min_mtu = hardif_min_mtu();
189 if (soft_device->mtu != min_mtu)
190 soft_device->mtu = min_mtu;
193 static void hardif_activate_interface(struct net_device *net_dev,
194 struct bat_priv *bat_priv,
195 struct batman_if *batman_if)
197 if (batman_if->if_status != IF_INACTIVE)
200 update_mac_addresses(batman_if);
201 batman_if->if_status = IF_TO_BE_ACTIVATED;
204 * the first active interface becomes our primary interface or
205 * the next active interface after the old primay interface was removed
207 if (!bat_priv->primary_if)
208 set_primary_if(bat_priv, batman_if);
210 bat_info(net_dev, "Interface activated: %s\n", batman_if->dev);
212 if (atomic_read(&module_state) == MODULE_INACTIVE)
219 static void hardif_deactivate_interface(struct net_device *net_dev,
220 struct batman_if *batman_if)
222 if ((batman_if->if_status != IF_ACTIVE) &&
223 (batman_if->if_status != IF_TO_BE_ACTIVATED))
226 batman_if->if_status = IF_INACTIVE;
228 bat_info(net_dev, "Interface deactivated: %s\n", batman_if->dev);
233 int hardif_enable_interface(struct batman_if *batman_if)
235 /* FIXME: each batman_if will be attached to a softif */
236 struct bat_priv *bat_priv = netdev_priv(soft_device);
237 struct batman_packet *batman_packet;
239 if (batman_if->if_status != IF_NOT_IN_USE)
242 batman_if->packet_len = BAT_PACKET_LEN;
243 batman_if->packet_buff = kmalloc(batman_if->packet_len, GFP_ATOMIC);
245 if (!batman_if->packet_buff) {
246 bat_err(soft_device, "Can't add interface packet (%s): "
247 "out of memory\n", batman_if->dev);
251 batman_packet = (struct batman_packet *)(batman_if->packet_buff);
252 batman_packet->packet_type = BAT_PACKET;
253 batman_packet->version = COMPAT_VERSION;
254 batman_packet->flags = 0;
255 batman_packet->ttl = 2;
256 batman_packet->tq = TQ_MAX_VALUE;
257 batman_packet->num_hna = 0;
259 batman_if->if_num = bat_priv->num_ifaces;
260 bat_priv->num_ifaces++;
261 batman_if->if_status = IF_INACTIVE;
262 orig_hash_add_if(batman_if, bat_priv->num_ifaces);
264 atomic_set(&batman_if->seqno, 1);
265 bat_info(soft_device, "Adding interface: %s\n", batman_if->dev);
267 if (hardif_is_iface_up(batman_if))
268 hardif_activate_interface(soft_device, bat_priv, batman_if);
270 bat_err(soft_device, "Not using interface %s "
271 "(retrying later): interface not active\n",
274 /* begin scheduling originator messages on that interface */
275 schedule_own_packet(batman_if);
284 void hardif_disable_interface(struct batman_if *batman_if)
286 /* FIXME: each batman_if will be attached to a softif */
287 struct bat_priv *bat_priv = netdev_priv(soft_device);
289 if (batman_if->if_status == IF_ACTIVE)
290 hardif_deactivate_interface(soft_device, batman_if);
292 if (batman_if->if_status != IF_INACTIVE)
295 bat_info(soft_device, "Removing interface: %s\n", batman_if->dev);
296 bat_priv->num_ifaces--;
297 orig_hash_del_if(batman_if, bat_priv->num_ifaces);
299 if (batman_if == bat_priv->primary_if)
300 set_primary_if(bat_priv, get_active_batman_if());
302 kfree(batman_if->packet_buff);
303 batman_if->packet_buff = NULL;
304 batman_if->if_status = IF_NOT_IN_USE;
306 if ((atomic_read(&module_state) == MODULE_ACTIVE) &&
307 (bat_priv->num_ifaces == 0))
311 static struct batman_if *hardif_add_interface(struct net_device *net_dev)
313 struct batman_if *batman_if;
316 ret = is_valid_iface(net_dev);
322 batman_if = kmalloc(sizeof(struct batman_if), GFP_ATOMIC);
324 pr_err("Can't add interface (%s): out of memory\n",
329 batman_if->dev = kstrdup(net_dev->name, GFP_ATOMIC);
333 ret = sysfs_add_hardif(&batman_if->hardif_obj, net_dev);
337 batman_if->if_num = -1;
338 batman_if->net_dev = net_dev;
339 batman_if->if_status = IF_NOT_IN_USE;
340 batman_if->packet_buff = NULL;
341 INIT_LIST_HEAD(&batman_if->list);
343 check_known_mac_addr(batman_if->net_dev->dev_addr);
344 list_add_tail_rcu(&batman_if->list, &if_list);
348 kfree(batman_if->dev);
357 static void hardif_free_interface(struct rcu_head *rcu)
359 struct batman_if *batman_if = container_of(rcu, struct batman_if, rcu);
361 /* delete all references to this batman_if */
363 purge_outstanding_packets(batman_if);
365 kfree(batman_if->dev);
369 static void hardif_remove_interface(struct batman_if *batman_if)
371 /* first deactivate interface */
372 if (batman_if->if_status != IF_NOT_IN_USE)
373 hardif_disable_interface(batman_if);
375 if (batman_if->if_status != IF_NOT_IN_USE)
378 batman_if->if_status = IF_TO_BE_REMOVED;
379 list_del_rcu(&batman_if->list);
380 sysfs_del_hardif(&batman_if->hardif_obj);
381 dev_put(batman_if->net_dev);
382 call_rcu(&batman_if->rcu, hardif_free_interface);
385 void hardif_remove_interfaces(void)
387 struct batman_if *batman_if, *batman_if_tmp;
389 list_for_each_entry_safe(batman_if, batman_if_tmp, &if_list, list)
390 hardif_remove_interface(batman_if);
393 static int hard_if_event(struct notifier_block *this,
394 unsigned long event, void *ptr)
396 struct net_device *net_dev = (struct net_device *)ptr;
397 struct batman_if *batman_if = get_batman_if_by_netdev(net_dev);
398 /* FIXME: each batman_if will be attached to a softif */
399 struct bat_priv *bat_priv = netdev_priv(soft_device);
401 if (!batman_if && event == NETDEV_REGISTER)
402 batman_if = hardif_add_interface(net_dev);
409 hardif_activate_interface(soft_device, bat_priv, batman_if);
411 case NETDEV_GOING_DOWN:
413 hardif_deactivate_interface(soft_device, batman_if);
415 case NETDEV_UNREGISTER:
416 hardif_remove_interface(batman_if);
418 case NETDEV_CHANGENAME:
420 case NETDEV_CHANGEADDR:
421 check_known_mac_addr(batman_if->net_dev->dev_addr);
422 update_mac_addresses(batman_if);
423 if (batman_if == bat_priv->primary_if)
424 set_primary_if(bat_priv, batman_if);
434 static int batman_skb_recv_finish(struct sk_buff *skb)
439 /* receive a packet with the batman ethertype coming on a hard
441 int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
442 struct packet_type *ptype, struct net_device *orig_dev)
444 /* FIXME: each orig_node->batman_if will be attached to a softif */
445 struct bat_priv *bat_priv = netdev_priv(soft_device);
446 struct batman_packet *batman_packet;
447 struct batman_if *batman_if;
450 skb = skb_share_check(skb, GFP_ATOMIC);
452 /* skb was released by skb_share_check() */
456 if (atomic_read(&module_state) != MODULE_ACTIVE)
459 /* if netfilter/ebtables wants to block incoming batman
460 * packets then give them a chance to do so here */
461 ret = NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_IN, skb, dev, NULL,
462 batman_skb_recv_finish);
466 /* packet should hold at least type and version */
467 if (unlikely(skb_headlen(skb) < 2))
470 /* expect a valid ethernet header here. */
471 if (unlikely(skb->mac_len != sizeof(struct ethhdr)
472 || !skb_mac_header(skb)))
475 batman_if = get_batman_if_by_netdev(skb->dev);
479 /* discard frames on not active interfaces */
480 if (batman_if->if_status != IF_ACTIVE)
483 batman_packet = (struct batman_packet *)skb->data;
485 if (batman_packet->version != COMPAT_VERSION) {
486 bat_dbg(DBG_BATMAN, bat_priv,
487 "Drop packet: incompatible batman version (%i)\n",
488 batman_packet->version);
492 /* all receive handlers return whether they received or reused
493 * the supplied skb. if not, we have to free the skb. */
495 switch (batman_packet->packet_type) {
496 /* batman originator packet */
498 ret = recv_bat_packet(skb, batman_if);
501 /* batman icmp packet */
503 ret = recv_icmp_packet(skb);
508 ret = recv_unicast_packet(skb, batman_if);
511 /* broadcast packet */
513 ret = recv_bcast_packet(skb);
518 ret = recv_vis_packet(skb);
524 if (ret == NET_RX_DROP)
527 /* return NET_RX_SUCCESS in any case as we
528 * most probably dropped the packet for
529 * routing-logical reasons. */
531 return NET_RX_SUCCESS;
539 struct notifier_block hard_if_notifier = {
540 .notifier_call = hard_if_event,