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 /* protect update critical side of if_list - but not the content */
37 static DEFINE_SPINLOCK(if_list_lock);
39 static void hardif_free_rcu(struct rcu_head *rcu)
41 struct batman_if *batman_if;
43 batman_if = container_of(rcu, struct batman_if, rcu);
44 dev_put(batman_if->net_dev);
45 kref_put(&batman_if->refcount, hardif_free_ref);
48 struct batman_if *get_batman_if_by_netdev(struct net_device *net_dev)
50 struct batman_if *batman_if;
53 list_for_each_entry_rcu(batman_if, &if_list, list) {
54 if (batman_if->net_dev == net_dev)
62 kref_get(&batman_if->refcount);
68 static int is_valid_iface(struct net_device *net_dev)
70 if (net_dev->flags & IFF_LOOPBACK)
73 if (net_dev->type != ARPHRD_ETHER)
76 if (net_dev->addr_len != ETH_ALEN)
79 /* no batman over batman */
80 #ifdef HAVE_NET_DEVICE_OPS
81 if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
84 if (net_dev->hard_start_xmit == interface_tx)
88 /* Device is being bridged */
89 /* if (net_dev->priv_flags & IFF_BRIDGE_PORT)
95 static struct batman_if *get_active_batman_if(struct net_device *soft_iface)
97 struct batman_if *batman_if;
100 list_for_each_entry_rcu(batman_if, &if_list, list) {
101 if (batman_if->soft_iface != soft_iface)
104 if (batman_if->if_status == IF_ACTIVE)
112 kref_get(&batman_if->refcount);
118 static void update_primary_addr(struct bat_priv *bat_priv)
120 struct vis_packet *vis_packet;
122 vis_packet = (struct vis_packet *)
123 bat_priv->my_vis_info->skb_packet->data;
124 memcpy(vis_packet->vis_orig,
125 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
126 memcpy(vis_packet->sender_orig,
127 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
130 static void set_primary_if(struct bat_priv *bat_priv,
131 struct batman_if *batman_if)
133 struct batman_packet *batman_packet;
134 struct batman_if *old_if;
137 kref_get(&batman_if->refcount);
139 old_if = bat_priv->primary_if;
140 bat_priv->primary_if = batman_if;
143 kref_put(&old_if->refcount, hardif_free_ref);
145 if (!bat_priv->primary_if)
148 batman_packet = (struct batman_packet *)(batman_if->packet_buff);
149 batman_packet->flags = PRIMARIES_FIRST_HOP;
150 batman_packet->ttl = TTL;
152 update_primary_addr(bat_priv);
155 * hacky trick to make sure that we send the HNA information via
156 * our new primary interface
158 atomic_set(&bat_priv->hna_local_changed, 1);
161 static bool hardif_is_iface_up(struct batman_if *batman_if)
163 if (batman_if->net_dev->flags & IFF_UP)
169 static void update_mac_addresses(struct batman_if *batman_if)
171 memcpy(((struct batman_packet *)(batman_if->packet_buff))->orig,
172 batman_if->net_dev->dev_addr, ETH_ALEN);
173 memcpy(((struct batman_packet *)(batman_if->packet_buff))->prev_sender,
174 batman_if->net_dev->dev_addr, ETH_ALEN);
177 static void check_known_mac_addr(struct net_device *net_dev)
179 struct batman_if *batman_if;
182 list_for_each_entry_rcu(batman_if, &if_list, list) {
183 if ((batman_if->if_status != IF_ACTIVE) &&
184 (batman_if->if_status != IF_TO_BE_ACTIVATED))
187 if (batman_if->net_dev == net_dev)
190 if (!compare_orig(batman_if->net_dev->dev_addr,
194 pr_warning("The newly added mac address (%pM) already exists "
195 "on: %s\n", net_dev->dev_addr,
196 batman_if->net_dev->name);
197 pr_warning("It is strongly recommended to keep mac addresses "
198 "unique to avoid problems!\n");
203 int hardif_min_mtu(struct net_device *soft_iface)
205 struct bat_priv *bat_priv = netdev_priv(soft_iface);
206 struct batman_if *batman_if;
207 /* allow big frames if all devices are capable to do so
208 * (have MTU > 1500 + BAT_HEADER_LEN) */
209 int min_mtu = ETH_DATA_LEN;
211 if (atomic_read(&bat_priv->fragmentation))
215 list_for_each_entry_rcu(batman_if, &if_list, list) {
216 if ((batman_if->if_status != IF_ACTIVE) &&
217 (batman_if->if_status != IF_TO_BE_ACTIVATED))
220 if (batman_if->soft_iface != soft_iface)
223 min_mtu = MIN(batman_if->net_dev->mtu - BAT_HEADER_LEN,
231 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
232 void update_min_mtu(struct net_device *soft_iface)
236 min_mtu = hardif_min_mtu(soft_iface);
237 if (soft_iface->mtu != min_mtu)
238 soft_iface->mtu = min_mtu;
241 static void hardif_activate_interface(struct batman_if *batman_if)
243 struct bat_priv *bat_priv;
245 if (batman_if->if_status != IF_INACTIVE)
248 bat_priv = netdev_priv(batman_if->soft_iface);
250 update_mac_addresses(batman_if);
251 batman_if->if_status = IF_TO_BE_ACTIVATED;
254 * the first active interface becomes our primary interface or
255 * the next active interface after the old primay interface was removed
257 if (!bat_priv->primary_if)
258 set_primary_if(bat_priv, batman_if);
260 bat_info(batman_if->soft_iface, "Interface activated: %s\n",
261 batman_if->net_dev->name);
263 update_min_mtu(batman_if->soft_iface);
267 static void hardif_deactivate_interface(struct batman_if *batman_if)
269 if ((batman_if->if_status != IF_ACTIVE) &&
270 (batman_if->if_status != IF_TO_BE_ACTIVATED))
273 batman_if->if_status = IF_INACTIVE;
275 bat_info(batman_if->soft_iface, "Interface deactivated: %s\n",
276 batman_if->net_dev->name);
278 update_min_mtu(batman_if->soft_iface);
281 int hardif_enable_interface(struct batman_if *batman_if, char *iface_name)
283 struct bat_priv *bat_priv;
284 struct batman_packet *batman_packet;
286 if (batman_if->if_status != IF_NOT_IN_USE)
289 batman_if->soft_iface = dev_get_by_name(&init_net, iface_name);
291 if (!batman_if->soft_iface) {
292 batman_if->soft_iface = softif_create(iface_name);
294 if (!batman_if->soft_iface)
297 /* dev_get_by_name() increases the reference counter for us */
298 dev_hold(batman_if->soft_iface);
301 bat_priv = netdev_priv(batman_if->soft_iface);
302 batman_if->packet_len = BAT_PACKET_LEN;
303 batman_if->packet_buff = kmalloc(batman_if->packet_len, GFP_ATOMIC);
305 if (!batman_if->packet_buff) {
306 bat_err(batman_if->soft_iface, "Can't add interface packet "
307 "(%s): out of memory\n", batman_if->net_dev->name);
311 batman_packet = (struct batman_packet *)(batman_if->packet_buff);
312 batman_packet->packet_type = BAT_PACKET;
313 batman_packet->version = COMPAT_VERSION;
314 batman_packet->flags = 0;
315 batman_packet->ttl = 2;
316 batman_packet->tq = TQ_MAX_VALUE;
317 batman_packet->num_hna = 0;
319 batman_if->if_num = bat_priv->num_ifaces;
320 bat_priv->num_ifaces++;
321 batman_if->if_status = IF_INACTIVE;
322 orig_hash_add_if(batman_if, bat_priv->num_ifaces);
324 batman_if->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
325 batman_if->batman_adv_ptype.func = batman_skb_recv;
326 batman_if->batman_adv_ptype.dev = batman_if->net_dev;
327 kref_get(&batman_if->refcount);
328 dev_add_pack(&batman_if->batman_adv_ptype);
330 atomic_set(&batman_if->seqno, 1);
331 atomic_set(&batman_if->frag_seqno, 1);
332 bat_info(batman_if->soft_iface, "Adding interface: %s\n",
333 batman_if->net_dev->name);
335 if (atomic_read(&bat_priv->fragmentation) && batman_if->net_dev->mtu <
336 ETH_DATA_LEN + BAT_HEADER_LEN)
337 bat_info(batman_if->soft_iface,
338 "The MTU of interface %s is too small (%i) to handle "
339 "the transport of batman-adv packets. Packets going "
340 "over this interface will be fragmented on layer2 "
341 "which could impact the performance. Setting the MTU "
342 "to %zi would solve the problem.\n",
343 batman_if->net_dev->name, batman_if->net_dev->mtu,
344 ETH_DATA_LEN + BAT_HEADER_LEN);
346 if (!atomic_read(&bat_priv->fragmentation) && batman_if->net_dev->mtu <
347 ETH_DATA_LEN + BAT_HEADER_LEN)
348 bat_info(batman_if->soft_iface,
349 "The MTU of interface %s is too small (%i) to handle "
350 "the transport of batman-adv packets. If you experience"
351 " problems getting traffic through try increasing the "
353 batman_if->net_dev->name, batman_if->net_dev->mtu,
354 ETH_DATA_LEN + BAT_HEADER_LEN);
356 if (hardif_is_iface_up(batman_if))
357 hardif_activate_interface(batman_if);
359 bat_err(batman_if->soft_iface, "Not using interface %s "
360 "(retrying later): interface not active\n",
361 batman_if->net_dev->name);
363 /* begin scheduling originator messages on that interface */
364 schedule_own_packet(batman_if);
373 void hardif_disable_interface(struct batman_if *batman_if)
375 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
377 if (batman_if->if_status == IF_ACTIVE)
378 hardif_deactivate_interface(batman_if);
380 if (batman_if->if_status != IF_INACTIVE)
383 bat_info(batman_if->soft_iface, "Removing interface: %s\n",
384 batman_if->net_dev->name);
385 dev_remove_pack(&batman_if->batman_adv_ptype);
386 kref_put(&batman_if->refcount, hardif_free_ref);
388 bat_priv->num_ifaces--;
389 orig_hash_del_if(batman_if, bat_priv->num_ifaces);
391 if (batman_if == bat_priv->primary_if) {
392 struct batman_if *new_if;
394 new_if = get_active_batman_if(batman_if->soft_iface);
395 set_primary_if(bat_priv, new_if);
398 kref_put(&new_if->refcount, hardif_free_ref);
401 kfree(batman_if->packet_buff);
402 batman_if->packet_buff = NULL;
403 batman_if->if_status = IF_NOT_IN_USE;
405 /* delete all references to this batman_if */
406 purge_orig_ref(bat_priv);
407 purge_outstanding_packets(bat_priv, batman_if);
408 dev_put(batman_if->soft_iface);
410 /* nobody uses this interface anymore */
411 if (!bat_priv->num_ifaces)
412 softif_destroy(batman_if->soft_iface);
414 batman_if->soft_iface = NULL;
417 static struct batman_if *hardif_add_interface(struct net_device *net_dev)
419 struct batman_if *batman_if;
422 ret = is_valid_iface(net_dev);
428 batman_if = kmalloc(sizeof(struct batman_if), GFP_ATOMIC);
430 pr_err("Can't add interface (%s): out of memory\n",
435 ret = sysfs_add_hardif(&batman_if->hardif_obj, net_dev);
439 batman_if->if_num = -1;
440 batman_if->net_dev = net_dev;
441 batman_if->soft_iface = NULL;
442 batman_if->if_status = IF_NOT_IN_USE;
443 INIT_LIST_HEAD(&batman_if->list);
444 kref_init(&batman_if->refcount);
446 check_known_mac_addr(batman_if->net_dev);
448 spin_lock(&if_list_lock);
449 list_add_tail_rcu(&batman_if->list, &if_list);
450 spin_unlock(&if_list_lock);
452 /* extra reference for return */
453 kref_get(&batman_if->refcount);
464 static void hardif_remove_interface(struct batman_if *batman_if)
466 /* first deactivate interface */
467 if (batman_if->if_status != IF_NOT_IN_USE)
468 hardif_disable_interface(batman_if);
470 if (batman_if->if_status != IF_NOT_IN_USE)
473 batman_if->if_status = IF_TO_BE_REMOVED;
474 sysfs_del_hardif(&batman_if->hardif_obj);
475 call_rcu(&batman_if->rcu, hardif_free_rcu);
478 void hardif_remove_interfaces(void)
480 struct batman_if *batman_if, *batman_if_tmp;
481 struct list_head if_queue;
483 INIT_LIST_HEAD(&if_queue);
485 spin_lock(&if_list_lock);
486 list_for_each_entry_safe(batman_if, batman_if_tmp, &if_list, list) {
487 list_del_rcu(&batman_if->list);
488 list_add_tail(&batman_if->list, &if_queue);
490 spin_unlock(&if_list_lock);
493 list_for_each_entry_safe(batman_if, batman_if_tmp, &if_queue, list) {
494 hardif_remove_interface(batman_if);
499 static int hard_if_event(struct notifier_block *this,
500 unsigned long event, void *ptr)
502 struct net_device *net_dev = (struct net_device *)ptr;
503 struct batman_if *batman_if = get_batman_if_by_netdev(net_dev);
504 struct bat_priv *bat_priv;
506 if (!batman_if && event == NETDEV_REGISTER)
507 batman_if = hardif_add_interface(net_dev);
514 hardif_activate_interface(batman_if);
516 case NETDEV_GOING_DOWN:
518 hardif_deactivate_interface(batman_if);
520 case NETDEV_UNREGISTER:
521 spin_lock(&if_list_lock);
522 list_del_rcu(&batman_if->list);
523 spin_unlock(&if_list_lock);
525 hardif_remove_interface(batman_if);
527 case NETDEV_CHANGEMTU:
528 if (batman_if->soft_iface)
529 update_min_mtu(batman_if->soft_iface);
531 case NETDEV_CHANGEADDR:
532 if (batman_if->if_status == IF_NOT_IN_USE)
535 check_known_mac_addr(batman_if->net_dev);
536 update_mac_addresses(batman_if);
538 bat_priv = netdev_priv(batman_if->soft_iface);
539 if (batman_if == bat_priv->primary_if)
540 update_primary_addr(bat_priv);
547 kref_put(&batman_if->refcount, hardif_free_ref);
552 /* receive a packet with the batman ethertype coming on a hard
554 int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
555 struct packet_type *ptype, struct net_device *orig_dev)
557 struct bat_priv *bat_priv;
558 struct batman_packet *batman_packet;
559 struct batman_if *batman_if;
562 batman_if = container_of(ptype, struct batman_if, batman_adv_ptype);
563 skb = skb_share_check(skb, GFP_ATOMIC);
565 /* skb was released by skb_share_check() */
569 /* packet should hold at least type and version */
570 if (unlikely(!pskb_may_pull(skb, 2)))
573 /* expect a valid ethernet header here. */
574 if (unlikely(skb->mac_len != sizeof(struct ethhdr)
575 || !skb_mac_header(skb)))
578 if (!batman_if->soft_iface)
581 bat_priv = netdev_priv(batman_if->soft_iface);
583 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
586 /* discard frames on not active interfaces */
587 if (batman_if->if_status != IF_ACTIVE)
590 batman_packet = (struct batman_packet *)skb->data;
592 if (batman_packet->version != COMPAT_VERSION) {
593 bat_dbg(DBG_BATMAN, bat_priv,
594 "Drop packet: incompatible batman version (%i)\n",
595 batman_packet->version);
599 /* all receive handlers return whether they received or reused
600 * the supplied skb. if not, we have to free the skb. */
602 switch (batman_packet->packet_type) {
603 /* batman originator packet */
605 ret = recv_bat_packet(skb, batman_if);
608 /* batman icmp packet */
610 ret = recv_icmp_packet(skb, batman_if);
615 ret = recv_unicast_packet(skb, batman_if);
618 /* fragmented unicast packet */
619 case BAT_UNICAST_FRAG:
620 ret = recv_ucast_frag_packet(skb, batman_if);
623 /* broadcast packet */
625 ret = recv_bcast_packet(skb, batman_if);
630 ret = recv_vis_packet(skb, batman_if);
636 if (ret == NET_RX_DROP)
639 /* return NET_RX_SUCCESS in any case as we
640 * most probably dropped the packet for
641 * routing-logical reasons. */
643 return NET_RX_SUCCESS;
651 struct notifier_block hard_if_notifier = {
652 .notifier_call = hard_if_event,