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 struct batman_if *get_batman_if_by_netdev(struct net_device *net_dev)
41 struct batman_if *batman_if;
44 list_for_each_entry_rcu(batman_if, &if_list, list) {
45 if (batman_if->net_dev == net_dev)
53 hardif_hold(batman_if);
59 static int is_valid_iface(struct net_device *net_dev)
61 if (net_dev->flags & IFF_LOOPBACK)
64 if (net_dev->type != ARPHRD_ETHER)
67 if (net_dev->addr_len != ETH_ALEN)
70 /* no batman over batman */
71 #ifdef HAVE_NET_DEVICE_OPS
72 if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
75 if (net_dev->hard_start_xmit == interface_tx)
79 /* Device is being bridged */
80 /* if (net_dev->priv_flags & IFF_BRIDGE_PORT)
86 static struct batman_if *get_active_batman_if(struct net_device *soft_iface)
88 struct batman_if *batman_if;
91 list_for_each_entry_rcu(batman_if, &if_list, list) {
92 if (batman_if->soft_iface != soft_iface)
95 if (batman_if->if_status == IF_ACTIVE)
103 hardif_hold(batman_if);
109 static void update_primary_addr(struct bat_priv *bat_priv)
111 struct vis_packet *vis_packet;
113 vis_packet = (struct vis_packet *)
114 bat_priv->my_vis_info->skb_packet->data;
115 memcpy(vis_packet->vis_orig,
116 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
117 memcpy(vis_packet->sender_orig,
118 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
121 static void set_primary_if(struct bat_priv *bat_priv,
122 struct batman_if *batman_if)
124 struct batman_packet *batman_packet;
125 struct batman_if *old_if;
128 hardif_hold(batman_if);
130 old_if = bat_priv->primary_if;
131 bat_priv->primary_if = batman_if;
136 if (!bat_priv->primary_if)
139 batman_packet = (struct batman_packet *)(batman_if->packet_buff);
140 batman_packet->flags = PRIMARIES_FIRST_HOP;
141 batman_packet->ttl = TTL;
143 update_primary_addr(bat_priv);
146 * hacky trick to make sure that we send the HNA information via
147 * our new primary interface
149 atomic_set(&bat_priv->hna_local_changed, 1);
152 static bool hardif_is_iface_up(struct batman_if *batman_if)
154 if (batman_if->net_dev->flags & IFF_UP)
160 static void update_mac_addresses(struct batman_if *batman_if)
162 memcpy(((struct batman_packet *)(batman_if->packet_buff))->orig,
163 batman_if->net_dev->dev_addr, ETH_ALEN);
164 memcpy(((struct batman_packet *)(batman_if->packet_buff))->prev_sender,
165 batman_if->net_dev->dev_addr, ETH_ALEN);
168 static void check_known_mac_addr(struct net_device *net_dev)
170 struct batman_if *batman_if;
173 list_for_each_entry_rcu(batman_if, &if_list, list) {
174 if ((batman_if->if_status != IF_ACTIVE) &&
175 (batman_if->if_status != IF_TO_BE_ACTIVATED))
178 if (batman_if->net_dev == net_dev)
181 if (!compare_orig(batman_if->net_dev->dev_addr,
185 pr_warning("The newly added mac address (%pM) already exists "
186 "on: %s\n", net_dev->dev_addr,
187 batman_if->net_dev->name);
188 pr_warning("It is strongly recommended to keep mac addresses "
189 "unique to avoid problems!\n");
194 int hardif_min_mtu(struct net_device *soft_iface)
196 struct bat_priv *bat_priv = netdev_priv(soft_iface);
197 struct batman_if *batman_if;
198 /* allow big frames if all devices are capable to do so
199 * (have MTU > 1500 + BAT_HEADER_LEN) */
200 int min_mtu = ETH_DATA_LEN;
202 if (atomic_read(&bat_priv->frag_enabled))
206 list_for_each_entry_rcu(batman_if, &if_list, list) {
207 if ((batman_if->if_status != IF_ACTIVE) &&
208 (batman_if->if_status != IF_TO_BE_ACTIVATED))
211 if (batman_if->soft_iface != soft_iface)
214 min_mtu = MIN(batman_if->net_dev->mtu - BAT_HEADER_LEN,
222 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
223 void update_min_mtu(struct net_device *soft_iface)
227 min_mtu = hardif_min_mtu(soft_iface);
228 if (soft_iface->mtu != min_mtu)
229 soft_iface->mtu = min_mtu;
232 static void hardif_activate_interface(struct batman_if *batman_if)
234 struct bat_priv *bat_priv;
236 if (batman_if->if_status != IF_INACTIVE)
239 bat_priv = netdev_priv(batman_if->soft_iface);
241 update_mac_addresses(batman_if);
242 batman_if->if_status = IF_TO_BE_ACTIVATED;
245 * the first active interface becomes our primary interface or
246 * the next active interface after the old primay interface was removed
248 if (!bat_priv->primary_if)
249 set_primary_if(bat_priv, batman_if);
251 bat_info(batman_if->soft_iface, "Interface activated: %s\n",
252 batman_if->net_dev->name);
254 update_min_mtu(batman_if->soft_iface);
258 static void hardif_deactivate_interface(struct batman_if *batman_if)
260 if ((batman_if->if_status != IF_ACTIVE) &&
261 (batman_if->if_status != IF_TO_BE_ACTIVATED))
264 batman_if->if_status = IF_INACTIVE;
266 bat_info(batman_if->soft_iface, "Interface deactivated: %s\n",
267 batman_if->net_dev->name);
269 update_min_mtu(batman_if->soft_iface);
272 int hardif_enable_interface(struct batman_if *batman_if, char *iface_name)
274 struct bat_priv *bat_priv;
275 struct batman_packet *batman_packet;
277 if (batman_if->if_status != IF_NOT_IN_USE)
280 batman_if->soft_iface = dev_get_by_name(&init_net, iface_name);
282 if (!batman_if->soft_iface) {
283 batman_if->soft_iface = softif_create(iface_name);
285 if (!batman_if->soft_iface)
288 /* dev_get_by_name() increases the reference counter for us */
289 dev_hold(batman_if->soft_iface);
292 bat_priv = netdev_priv(batman_if->soft_iface);
293 batman_if->packet_len = BAT_PACKET_LEN;
294 batman_if->packet_buff = kmalloc(batman_if->packet_len, GFP_ATOMIC);
296 if (!batman_if->packet_buff) {
297 bat_err(batman_if->soft_iface, "Can't add interface packet "
298 "(%s): out of memory\n", batman_if->net_dev->name);
302 batman_packet = (struct batman_packet *)(batman_if->packet_buff);
303 batman_packet->packet_type = BAT_PACKET;
304 batman_packet->version = COMPAT_VERSION;
305 batman_packet->flags = 0;
306 batman_packet->ttl = 2;
307 batman_packet->tq = TQ_MAX_VALUE;
308 batman_packet->num_hna = 0;
310 batman_if->if_num = bat_priv->num_ifaces;
311 bat_priv->num_ifaces++;
312 batman_if->if_status = IF_INACTIVE;
313 orig_hash_add_if(batman_if, bat_priv->num_ifaces);
315 batman_if->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
316 batman_if->batman_adv_ptype.func = batman_skb_recv;
317 batman_if->batman_adv_ptype.dev = batman_if->net_dev;
318 hardif_hold(batman_if);
319 dev_add_pack(&batman_if->batman_adv_ptype);
321 atomic_set(&batman_if->seqno, 1);
322 atomic_set(&batman_if->frag_seqno, 1);
323 bat_info(batman_if->soft_iface, "Adding interface: %s\n",
324 batman_if->net_dev->name);
326 if (atomic_read(&bat_priv->frag_enabled) && batman_if->net_dev->mtu <
327 ETH_DATA_LEN + BAT_HEADER_LEN)
328 bat_info(batman_if->soft_iface,
329 "The MTU of interface %s is too small (%i) to handle "
330 "the transport of batman-adv packets. Packets going "
331 "over this interface will be fragmented on layer2 "
332 "which could impact the performance. Setting the MTU "
333 "to %zi would solve the problem.\n",
334 batman_if->net_dev->name, batman_if->net_dev->mtu,
335 ETH_DATA_LEN + BAT_HEADER_LEN);
337 if (!atomic_read(&bat_priv->frag_enabled) && batman_if->net_dev->mtu <
338 ETH_DATA_LEN + BAT_HEADER_LEN)
339 bat_info(batman_if->soft_iface,
340 "The MTU of interface %s is too small (%i) to handle "
341 "the transport of batman-adv packets. If you experience"
342 " problems getting traffic through try increasing the "
344 batman_if->net_dev->name, batman_if->net_dev->mtu,
345 ETH_DATA_LEN + BAT_HEADER_LEN);
347 if (hardif_is_iface_up(batman_if))
348 hardif_activate_interface(batman_if);
350 bat_err(batman_if->soft_iface, "Not using interface %s "
351 "(retrying later): interface not active\n",
352 batman_if->net_dev->name);
354 /* begin scheduling originator messages on that interface */
355 schedule_own_packet(batman_if);
364 void hardif_disable_interface(struct batman_if *batman_if)
366 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
368 if (batman_if->if_status == IF_ACTIVE)
369 hardif_deactivate_interface(batman_if);
371 if (batman_if->if_status != IF_INACTIVE)
374 bat_info(batman_if->soft_iface, "Removing interface: %s\n",
375 batman_if->net_dev->name);
376 dev_remove_pack(&batman_if->batman_adv_ptype);
377 hardif_put(batman_if);
379 bat_priv->num_ifaces--;
380 orig_hash_del_if(batman_if, bat_priv->num_ifaces);
382 if (batman_if == bat_priv->primary_if) {
383 struct batman_if *new_if;
385 new_if = get_active_batman_if(batman_if->soft_iface);
386 set_primary_if(bat_priv, new_if);
392 kfree(batman_if->packet_buff);
393 batman_if->packet_buff = NULL;
394 batman_if->if_status = IF_NOT_IN_USE;
396 /* delete all references to this batman_if */
397 purge_orig_ref(bat_priv);
398 purge_outstanding_packets(bat_priv, batman_if);
399 dev_put(batman_if->soft_iface);
401 /* nobody uses this interface anymore */
402 if (!bat_priv->num_ifaces)
403 softif_destroy(batman_if->soft_iface);
405 batman_if->soft_iface = NULL;
408 static struct batman_if *hardif_add_interface(struct net_device *net_dev)
410 struct batman_if *batman_if;
413 ret = is_valid_iface(net_dev);
419 batman_if = kmalloc(sizeof(struct batman_if), GFP_ATOMIC);
421 pr_err("Can't add interface (%s): out of memory\n",
426 ret = sysfs_add_hardif(&batman_if->hardif_obj, net_dev);
430 batman_if->if_num = -1;
431 batman_if->net_dev = net_dev;
432 batman_if->soft_iface = NULL;
433 batman_if->if_status = IF_NOT_IN_USE;
434 INIT_LIST_HEAD(&batman_if->list);
435 atomic_set(&batman_if->refcnt, 0);
436 hardif_hold(batman_if);
438 check_known_mac_addr(batman_if->net_dev);
440 spin_lock(&if_list_lock);
441 list_add_tail_rcu(&batman_if->list, &if_list);
442 spin_unlock(&if_list_lock);
444 /* extra reference for return */
445 hardif_hold(batman_if);
456 static void hardif_remove_interface(struct batman_if *batman_if)
458 /* first deactivate interface */
459 if (batman_if->if_status != IF_NOT_IN_USE)
460 hardif_disable_interface(batman_if);
462 if (batman_if->if_status != IF_NOT_IN_USE)
465 batman_if->if_status = IF_TO_BE_REMOVED;
467 sysfs_del_hardif(&batman_if->hardif_obj);
468 hardif_put(batman_if);
471 void hardif_remove_interfaces(void)
473 struct batman_if *batman_if, *batman_if_tmp;
474 struct list_head if_queue;
476 INIT_LIST_HEAD(&if_queue);
478 spin_lock(&if_list_lock);
479 list_for_each_entry_safe(batman_if, batman_if_tmp, &if_list, list) {
480 list_del_rcu(&batman_if->list);
481 list_add_tail(&batman_if->list, &if_queue);
483 spin_unlock(&if_list_lock);
486 list_for_each_entry_safe(batman_if, batman_if_tmp, &if_queue, list) {
487 hardif_remove_interface(batman_if);
492 static int hard_if_event(struct notifier_block *this,
493 unsigned long event, void *ptr)
495 struct net_device *net_dev = (struct net_device *)ptr;
496 struct batman_if *batman_if = get_batman_if_by_netdev(net_dev);
497 struct bat_priv *bat_priv;
499 if (!batman_if && event == NETDEV_REGISTER)
500 batman_if = hardif_add_interface(net_dev);
507 hardif_activate_interface(batman_if);
509 case NETDEV_GOING_DOWN:
511 hardif_deactivate_interface(batman_if);
513 case NETDEV_UNREGISTER:
514 spin_lock(&if_list_lock);
515 list_del_rcu(&batman_if->list);
516 spin_unlock(&if_list_lock);
518 hardif_remove_interface(batman_if);
520 case NETDEV_CHANGEMTU:
521 if (batman_if->soft_iface)
522 update_min_mtu(batman_if->soft_iface);
524 case NETDEV_CHANGEADDR:
525 if (batman_if->if_status == IF_NOT_IN_USE) {
526 hardif_put(batman_if);
530 check_known_mac_addr(batman_if->net_dev);
531 update_mac_addresses(batman_if);
533 bat_priv = netdev_priv(batman_if->soft_iface);
534 if (batman_if == bat_priv->primary_if)
535 update_primary_addr(bat_priv);
540 hardif_put(batman_if);
546 /* receive a packet with the batman ethertype coming on a hard
548 int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
549 struct packet_type *ptype, struct net_device *orig_dev)
551 struct bat_priv *bat_priv;
552 struct batman_packet *batman_packet;
553 struct batman_if *batman_if;
556 batman_if = container_of(ptype, struct batman_if, batman_adv_ptype);
557 skb = skb_share_check(skb, GFP_ATOMIC);
559 /* skb was released by skb_share_check() */
563 /* packet should hold at least type and version */
564 if (unlikely(!pskb_may_pull(skb, 2)))
567 /* expect a valid ethernet header here. */
568 if (unlikely(skb->mac_len != sizeof(struct ethhdr)
569 || !skb_mac_header(skb)))
572 if (!batman_if->soft_iface)
575 bat_priv = netdev_priv(batman_if->soft_iface);
577 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
580 /* discard frames on not active interfaces */
581 if (batman_if->if_status != IF_ACTIVE)
584 batman_packet = (struct batman_packet *)skb->data;
586 if (batman_packet->version != COMPAT_VERSION) {
587 bat_dbg(DBG_BATMAN, bat_priv,
588 "Drop packet: incompatible batman version (%i)\n",
589 batman_packet->version);
593 /* all receive handlers return whether they received or reused
594 * the supplied skb. if not, we have to free the skb. */
596 switch (batman_packet->packet_type) {
597 /* batman originator packet */
599 ret = recv_bat_packet(skb, batman_if);
602 /* batman icmp packet */
604 ret = recv_icmp_packet(skb, batman_if);
609 ret = recv_unicast_packet(skb, batman_if);
612 /* fragmented unicast packet */
613 case BAT_UNICAST_FRAG:
614 ret = recv_ucast_frag_packet(skb, batman_if);
617 /* broadcast packet */
619 ret = recv_bcast_packet(skb, batman_if);
624 ret = recv_vis_packet(skb, batman_if);
630 if (ret == NET_RX_DROP)
633 /* return NET_RX_SUCCESS in any case as we
634 * most probably dropped the packet for
635 * routing-logical reasons. */
637 return NET_RX_SUCCESS;
645 struct notifier_block hard_if_notifier = {
646 .notifier_call = hard_if_event,