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>
34 #define MIN(x, y) ((x) < (y) ? (x) : (y))
36 struct batman_if *get_batman_if_by_netdev(struct net_device *net_dev)
38 struct batman_if *batman_if;
41 list_for_each_entry_rcu(batman_if, &if_list, list) {
42 if (batman_if->net_dev == net_dev)
53 static int is_valid_iface(struct net_device *net_dev)
55 if (net_dev->flags & IFF_LOOPBACK)
58 if (net_dev->type != ARPHRD_ETHER)
61 if (net_dev->addr_len != ETH_ALEN)
64 /* no batman over batman */
65 #ifdef HAVE_NET_DEVICE_OPS
66 if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
69 if (net_dev->hard_start_xmit == interface_tx)
73 /* Device is being bridged */
74 /* if (net_dev->br_port != NULL)
80 static struct batman_if *get_active_batman_if(void)
82 struct batman_if *batman_if;
84 /* TODO: should check interfaces belonging to bat_priv */
86 list_for_each_entry_rcu(batman_if, &if_list, list) {
87 if (batman_if->if_status == IF_ACTIVE)
98 static void set_primary_if(struct bat_priv *bat_priv,
99 struct batman_if *batman_if)
101 struct batman_packet *batman_packet;
103 bat_priv->primary_if = batman_if;
105 if (!bat_priv->primary_if)
108 set_main_if_addr(batman_if->net_dev->dev_addr);
110 batman_packet = (struct batman_packet *)(batman_if->packet_buff);
111 batman_packet->flags = 0;
112 batman_packet->ttl = TTL;
115 * hacky trick to make sure that we send the HNA information via
116 * our new primary interface
118 atomic_set(&hna_local_changed, 1);
121 static bool hardif_is_iface_up(struct batman_if *batman_if)
123 if (batman_if->net_dev->flags & IFF_UP)
129 static void update_mac_addresses(struct batman_if *batman_if)
131 addr_to_string(batman_if->addr_str, batman_if->net_dev->dev_addr);
133 memcpy(((struct batman_packet *)(batman_if->packet_buff))->orig,
134 batman_if->net_dev->dev_addr, ETH_ALEN);
135 memcpy(((struct batman_packet *)(batman_if->packet_buff))->prev_sender,
136 batman_if->net_dev->dev_addr, ETH_ALEN);
139 static void check_known_mac_addr(uint8_t *addr)
141 struct batman_if *batman_if;
144 list_for_each_entry_rcu(batman_if, &if_list, list) {
145 if ((batman_if->if_status != IF_ACTIVE) &&
146 (batman_if->if_status != IF_TO_BE_ACTIVATED))
149 if (!compare_orig(batman_if->net_dev->dev_addr, addr))
152 printk(KERN_WARNING "batman-adv:"
153 "The newly added mac address (%pM) already exists on: %s\n",
154 addr, batman_if->dev);
155 printk(KERN_WARNING "batman-adv:"
156 "It is strongly recommended to keep mac addresses unique"
157 "to avoid problems!\n");
162 int hardif_min_mtu(void)
164 struct batman_if *batman_if;
165 /* allow big frames if all devices are capable to do so
166 * (have MTU > 1500 + BAT_HEADER_LEN) */
167 int min_mtu = ETH_DATA_LEN;
170 list_for_each_entry_rcu(batman_if, &if_list, list) {
171 if ((batman_if->if_status == IF_ACTIVE) ||
172 (batman_if->if_status == IF_TO_BE_ACTIVATED))
173 min_mtu = MIN(batman_if->net_dev->mtu - BAT_HEADER_LEN,
181 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
182 void update_min_mtu(void)
186 min_mtu = hardif_min_mtu();
187 if (soft_device->mtu != min_mtu)
188 soft_device->mtu = min_mtu;
191 static void hardif_activate_interface(struct bat_priv *bat_priv,
192 struct batman_if *batman_if)
194 if (batman_if->if_status != IF_INACTIVE)
197 dev_hold(batman_if->net_dev);
199 update_mac_addresses(batman_if);
200 batman_if->if_status = IF_TO_BE_ACTIVATED;
203 * the first active interface becomes our primary interface or
204 * the next active interface after the old primay interface was removed
206 if (!bat_priv->primary_if)
207 set_primary_if(bat_priv, batman_if);
209 printk(KERN_INFO "batman-adv:Interface activated: %s\n",
212 if (atomic_read(&module_state) == MODULE_INACTIVE)
219 static void hardif_deactivate_interface(struct batman_if *batman_if)
221 if ((batman_if->if_status != IF_ACTIVE) &&
222 (batman_if->if_status != IF_TO_BE_ACTIVATED))
225 dev_put(batman_if->net_dev);
227 batman_if->if_status = IF_INACTIVE;
229 printk(KERN_INFO "batman-adv:Interface deactivated: %s\n",
235 int hardif_enable_interface(struct batman_if *batman_if)
237 /* FIXME: each batman_if will be attached to a softif */
238 struct bat_priv *bat_priv = netdev_priv(soft_device);
239 struct batman_packet *batman_packet;
241 if (batman_if->if_status != IF_NOT_IN_USE)
244 batman_if->packet_len = BAT_PACKET_LEN;
245 batman_if->packet_buff = kmalloc(batman_if->packet_len, GFP_ATOMIC);
247 if (!batman_if->packet_buff) {
248 printk(KERN_ERR "batman-adv:"
249 "Can't add interface packet (%s): out of memory\n",
254 batman_packet = (struct batman_packet *)(batman_if->packet_buff);
255 batman_packet->packet_type = BAT_PACKET;
256 batman_packet->version = COMPAT_VERSION;
257 batman_packet->flags = 0;
258 batman_packet->ttl = 2;
259 batman_packet->tq = TQ_MAX_VALUE;
260 batman_packet->num_hna = 0;
262 batman_if->if_num = bat_priv->num_ifaces;
263 bat_priv->num_ifaces++;
264 batman_if->if_status = IF_INACTIVE;
265 orig_hash_add_if(batman_if, bat_priv->num_ifaces);
267 atomic_set(&batman_if->seqno, 1);
268 printk(KERN_INFO "batman-adv:Adding interface: %s\n", batman_if->dev);
270 if (hardif_is_iface_up(batman_if))
271 hardif_activate_interface(bat_priv, batman_if);
273 printk(KERN_ERR "batman-adv:"
274 "Not using interface %s "
275 "(retrying later): interface not active\n",
278 /* begin scheduling originator messages on that interface */
279 schedule_own_packet(batman_if);
288 void hardif_disable_interface(struct batman_if *batman_if)
290 /* FIXME: each batman_if will be attached to a softif */
291 struct bat_priv *bat_priv = netdev_priv(soft_device);
293 if (batman_if->if_status == IF_ACTIVE)
294 hardif_deactivate_interface(batman_if);
296 if (batman_if->if_status != IF_INACTIVE)
299 printk(KERN_INFO "batman-adv:Removing interface: %s\n", batman_if->dev);
300 bat_priv->num_ifaces--;
301 orig_hash_del_if(batman_if, bat_priv->num_ifaces);
303 if (batman_if == bat_priv->primary_if)
304 set_primary_if(bat_priv, get_active_batman_if());
306 kfree(batman_if->packet_buff);
307 batman_if->packet_buff = NULL;
308 batman_if->if_status = IF_NOT_IN_USE;
310 if ((atomic_read(&module_state) == MODULE_ACTIVE) &&
311 (bat_priv->num_ifaces == 0))
315 static struct batman_if *hardif_add_interface(struct net_device *net_dev)
317 struct batman_if *batman_if;
320 ret = is_valid_iface(net_dev);
324 batman_if = kmalloc(sizeof(struct batman_if), GFP_ATOMIC);
326 printk(KERN_ERR "batman-adv:"
327 "Can't add interface (%s): out of memory\n",
332 batman_if->dev = kstrdup(net_dev->name, GFP_ATOMIC);
336 ret = sysfs_add_hardif(&batman_if->hardif_obj, net_dev);
340 batman_if->if_num = -1;
341 batman_if->net_dev = net_dev;
342 batman_if->if_status = IF_NOT_IN_USE;
343 INIT_RCU_HEAD(&batman_if->rcu);
344 INIT_LIST_HEAD(&batman_if->list);
346 check_known_mac_addr(batman_if->net_dev->dev_addr);
347 list_add_tail_rcu(&batman_if->list, &if_list);
351 kfree(batman_if->dev);
358 static void hardif_free_interface(struct rcu_head *rcu)
360 struct batman_if *batman_if = container_of(rcu, struct batman_if, rcu);
362 /* delete all references to this batman_if */
364 purge_outstanding_packets(batman_if);
366 kfree(batman_if->dev);
370 static void hardif_remove_interface(struct batman_if *batman_if)
372 /* first deactivate interface */
373 if (batman_if->if_status != IF_NOT_IN_USE)
374 hardif_disable_interface(batman_if);
376 if (batman_if->if_status != IF_NOT_IN_USE)
379 batman_if->if_status = IF_TO_BE_REMOVED;
380 list_del_rcu(&batman_if->list);
381 sysfs_del_hardif(&batman_if->hardif_obj);
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);
402 batman_if = hardif_add_interface(net_dev);
408 case NETDEV_REGISTER:
411 hardif_activate_interface(bat_priv, batman_if);
413 case NETDEV_GOING_DOWN:
415 hardif_deactivate_interface(batman_if);
417 case NETDEV_UNREGISTER:
418 hardif_remove_interface(batman_if);
420 case NETDEV_CHANGENAME:
422 case NETDEV_CHANGEADDR:
423 check_known_mac_addr(batman_if->net_dev->dev_addr);
424 update_mac_addresses(batman_if);
425 if (batman_if == bat_priv->primary_if)
426 set_primary_if(bat_priv, batman_if);
436 /* receive a packet with the batman ethertype coming on a hard
438 int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
439 struct packet_type *ptype, struct net_device *orig_dev)
441 struct batman_packet *batman_packet;
442 struct batman_if *batman_if;
443 struct net_device_stats *stats;
446 skb = skb_share_check(skb, GFP_ATOMIC);
448 /* skb was released by skb_share_check() */
452 if (atomic_read(&module_state) != MODULE_ACTIVE)
455 /* packet should hold at least type and version */
456 if (unlikely(skb_headlen(skb) < 2))
459 /* expect a valid ethernet header here. */
460 if (unlikely(skb->mac_len != sizeof(struct ethhdr)
461 || !skb_mac_header(skb)))
464 batman_if = get_batman_if_by_netdev(skb->dev);
468 /* discard frames on not active interfaces */
469 if (batman_if->if_status != IF_ACTIVE)
472 stats = (struct net_device_stats *)dev_get_stats(skb->dev);
475 stats->rx_bytes += skb->len;
478 batman_packet = (struct batman_packet *)skb->data;
480 if (batman_packet->version != COMPAT_VERSION) {
482 "Drop packet: incompatible batman version (%i)\n",
483 batman_packet->version);
487 /* all receive handlers return whether they received or reused
488 * the supplied skb. if not, we have to free the skb. */
490 switch (batman_packet->packet_type) {
491 /* batman originator packet */
493 ret = recv_bat_packet(skb, batman_if);
496 /* batman icmp packet */
498 ret = recv_icmp_packet(skb);
503 ret = recv_unicast_packet(skb);
506 /* broadcast packet */
508 ret = recv_bcast_packet(skb);
513 ret = recv_vis_packet(skb);
519 if (ret == NET_RX_DROP)
522 /* return NET_RX_SUCCESS in any case as we
523 * most probably dropped the packet for
524 * routing-logical reasons. */
526 return NET_RX_SUCCESS;
535 struct notifier_block hard_if_notifier = {
536 .notifier_call = hard_if_event,