iwlwifi: avoid too frequent recover from statistics
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / batman-adv / hard-interface.c
blob4f95777ce0803d79cdbcb285b92efcb77b77fb53
1 /*
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
18 * 02110-1301, USA
22 #include "main.h"
23 #include "hard-interface.h"
24 #include "soft-interface.h"
25 #include "send.h"
26 #include "translation-table.h"
27 #include "routing.h"
28 #include "bat_sysfs.h"
29 #include "originator.h"
30 #include "hash.h"
32 #include <linux/if_arp.h>
34 /* protect update critical side of if_list - but not the content */
35 static DEFINE_SPINLOCK(if_list_lock);
37 static void hardif_free_rcu(struct rcu_head *rcu)
39 struct batman_if *batman_if;
41 batman_if = container_of(rcu, struct batman_if, rcu);
42 dev_put(batman_if->net_dev);
43 kref_put(&batman_if->refcount, hardif_free_ref);
46 struct batman_if *get_batman_if_by_netdev(struct net_device *net_dev)
48 struct batman_if *batman_if;
50 rcu_read_lock();
51 list_for_each_entry_rcu(batman_if, &if_list, list) {
52 if (batman_if->net_dev == net_dev)
53 goto out;
56 batman_if = NULL;
58 out:
59 if (batman_if)
60 kref_get(&batman_if->refcount);
62 rcu_read_unlock();
63 return batman_if;
66 static int is_valid_iface(struct net_device *net_dev)
68 if (net_dev->flags & IFF_LOOPBACK)
69 return 0;
71 if (net_dev->type != ARPHRD_ETHER)
72 return 0;
74 if (net_dev->addr_len != ETH_ALEN)
75 return 0;
77 /* no batman over batman */
78 #ifdef HAVE_NET_DEVICE_OPS
79 if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
80 return 0;
81 #else
82 if (net_dev->hard_start_xmit == interface_tx)
83 return 0;
84 #endif
86 /* Device is being bridged */
87 /* if (net_dev->priv_flags & IFF_BRIDGE_PORT)
88 return 0; */
90 return 1;
93 static struct batman_if *get_active_batman_if(struct net_device *soft_iface)
95 struct batman_if *batman_if;
97 rcu_read_lock();
98 list_for_each_entry_rcu(batman_if, &if_list, list) {
99 if (batman_if->soft_iface != soft_iface)
100 continue;
102 if (batman_if->if_status == IF_ACTIVE)
103 goto out;
106 batman_if = NULL;
108 out:
109 if (batman_if)
110 kref_get(&batman_if->refcount);
112 rcu_read_unlock();
113 return batman_if;
116 static void update_primary_addr(struct bat_priv *bat_priv)
118 struct vis_packet *vis_packet;
120 vis_packet = (struct vis_packet *)
121 bat_priv->my_vis_info->skb_packet->data;
122 memcpy(vis_packet->vis_orig,
123 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
124 memcpy(vis_packet->sender_orig,
125 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
128 static void set_primary_if(struct bat_priv *bat_priv,
129 struct batman_if *batman_if)
131 struct batman_packet *batman_packet;
132 struct batman_if *old_if;
134 if (batman_if)
135 kref_get(&batman_if->refcount);
137 old_if = bat_priv->primary_if;
138 bat_priv->primary_if = batman_if;
140 if (old_if)
141 kref_put(&old_if->refcount, hardif_free_ref);
143 if (!bat_priv->primary_if)
144 return;
146 batman_packet = (struct batman_packet *)(batman_if->packet_buff);
147 batman_packet->flags = PRIMARIES_FIRST_HOP;
148 batman_packet->ttl = TTL;
150 update_primary_addr(bat_priv);
152 /***
153 * hacky trick to make sure that we send the HNA information via
154 * our new primary interface
156 atomic_set(&bat_priv->hna_local_changed, 1);
159 static bool hardif_is_iface_up(struct batman_if *batman_if)
161 if (batman_if->net_dev->flags & IFF_UP)
162 return true;
164 return false;
167 static void update_mac_addresses(struct batman_if *batman_if)
169 memcpy(((struct batman_packet *)(batman_if->packet_buff))->orig,
170 batman_if->net_dev->dev_addr, ETH_ALEN);
171 memcpy(((struct batman_packet *)(batman_if->packet_buff))->prev_sender,
172 batman_if->net_dev->dev_addr, ETH_ALEN);
175 static void check_known_mac_addr(struct net_device *net_dev)
177 struct batman_if *batman_if;
179 rcu_read_lock();
180 list_for_each_entry_rcu(batman_if, &if_list, list) {
181 if ((batman_if->if_status != IF_ACTIVE) &&
182 (batman_if->if_status != IF_TO_BE_ACTIVATED))
183 continue;
185 if (batman_if->net_dev == net_dev)
186 continue;
188 if (!compare_orig(batman_if->net_dev->dev_addr,
189 net_dev->dev_addr))
190 continue;
192 pr_warning("The newly added mac address (%pM) already exists "
193 "on: %s\n", net_dev->dev_addr,
194 batman_if->net_dev->name);
195 pr_warning("It is strongly recommended to keep mac addresses "
196 "unique to avoid problems!\n");
198 rcu_read_unlock();
201 int hardif_min_mtu(struct net_device *soft_iface)
203 struct bat_priv *bat_priv = netdev_priv(soft_iface);
204 struct batman_if *batman_if;
205 /* allow big frames if all devices are capable to do so
206 * (have MTU > 1500 + BAT_HEADER_LEN) */
207 int min_mtu = ETH_DATA_LEN;
209 if (atomic_read(&bat_priv->fragmentation))
210 goto out;
212 rcu_read_lock();
213 list_for_each_entry_rcu(batman_if, &if_list, list) {
214 if ((batman_if->if_status != IF_ACTIVE) &&
215 (batman_if->if_status != IF_TO_BE_ACTIVATED))
216 continue;
218 if (batman_if->soft_iface != soft_iface)
219 continue;
221 min_mtu = min_t(int, batman_if->net_dev->mtu - BAT_HEADER_LEN,
222 min_mtu);
224 rcu_read_unlock();
225 out:
226 return min_mtu;
229 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
230 void update_min_mtu(struct net_device *soft_iface)
232 int min_mtu;
234 min_mtu = hardif_min_mtu(soft_iface);
235 if (soft_iface->mtu != min_mtu)
236 soft_iface->mtu = min_mtu;
239 static void hardif_activate_interface(struct batman_if *batman_if)
241 struct bat_priv *bat_priv;
243 if (batman_if->if_status != IF_INACTIVE)
244 return;
246 bat_priv = netdev_priv(batman_if->soft_iface);
248 update_mac_addresses(batman_if);
249 batman_if->if_status = IF_TO_BE_ACTIVATED;
252 * the first active interface becomes our primary interface or
253 * the next active interface after the old primay interface was removed
255 if (!bat_priv->primary_if)
256 set_primary_if(bat_priv, batman_if);
258 bat_info(batman_if->soft_iface, "Interface activated: %s\n",
259 batman_if->net_dev->name);
261 update_min_mtu(batman_if->soft_iface);
262 return;
265 static void hardif_deactivate_interface(struct batman_if *batman_if)
267 if ((batman_if->if_status != IF_ACTIVE) &&
268 (batman_if->if_status != IF_TO_BE_ACTIVATED))
269 return;
271 batman_if->if_status = IF_INACTIVE;
273 bat_info(batman_if->soft_iface, "Interface deactivated: %s\n",
274 batman_if->net_dev->name);
276 update_min_mtu(batman_if->soft_iface);
279 int hardif_enable_interface(struct batman_if *batman_if, char *iface_name)
281 struct bat_priv *bat_priv;
282 struct batman_packet *batman_packet;
284 if (batman_if->if_status != IF_NOT_IN_USE)
285 goto out;
287 batman_if->soft_iface = dev_get_by_name(&init_net, iface_name);
289 if (!batman_if->soft_iface) {
290 batman_if->soft_iface = softif_create(iface_name);
292 if (!batman_if->soft_iface)
293 goto err;
295 /* dev_get_by_name() increases the reference counter for us */
296 dev_hold(batman_if->soft_iface);
299 bat_priv = netdev_priv(batman_if->soft_iface);
300 batman_if->packet_len = BAT_PACKET_LEN;
301 batman_if->packet_buff = kmalloc(batman_if->packet_len, GFP_ATOMIC);
303 if (!batman_if->packet_buff) {
304 bat_err(batman_if->soft_iface, "Can't add interface packet "
305 "(%s): out of memory\n", batman_if->net_dev->name);
306 goto err;
309 batman_packet = (struct batman_packet *)(batman_if->packet_buff);
310 batman_packet->packet_type = BAT_PACKET;
311 batman_packet->version = COMPAT_VERSION;
312 batman_packet->flags = 0;
313 batman_packet->ttl = 2;
314 batman_packet->tq = TQ_MAX_VALUE;
315 batman_packet->num_hna = 0;
317 batman_if->if_num = bat_priv->num_ifaces;
318 bat_priv->num_ifaces++;
319 batman_if->if_status = IF_INACTIVE;
320 orig_hash_add_if(batman_if, bat_priv->num_ifaces);
322 batman_if->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
323 batman_if->batman_adv_ptype.func = batman_skb_recv;
324 batman_if->batman_adv_ptype.dev = batman_if->net_dev;
325 kref_get(&batman_if->refcount);
326 dev_add_pack(&batman_if->batman_adv_ptype);
328 atomic_set(&batman_if->seqno, 1);
329 atomic_set(&batman_if->frag_seqno, 1);
330 bat_info(batman_if->soft_iface, "Adding interface: %s\n",
331 batman_if->net_dev->name);
333 if (atomic_read(&bat_priv->fragmentation) && batman_if->net_dev->mtu <
334 ETH_DATA_LEN + BAT_HEADER_LEN)
335 bat_info(batman_if->soft_iface,
336 "The MTU of interface %s is too small (%i) to handle "
337 "the transport of batman-adv packets. Packets going "
338 "over this interface will be fragmented on layer2 "
339 "which could impact the performance. Setting the MTU "
340 "to %zi would solve the problem.\n",
341 batman_if->net_dev->name, batman_if->net_dev->mtu,
342 ETH_DATA_LEN + BAT_HEADER_LEN);
344 if (!atomic_read(&bat_priv->fragmentation) && batman_if->net_dev->mtu <
345 ETH_DATA_LEN + BAT_HEADER_LEN)
346 bat_info(batman_if->soft_iface,
347 "The MTU of interface %s is too small (%i) to handle "
348 "the transport of batman-adv packets. If you experience"
349 " problems getting traffic through try increasing the "
350 "MTU to %zi.\n",
351 batman_if->net_dev->name, batman_if->net_dev->mtu,
352 ETH_DATA_LEN + BAT_HEADER_LEN);
354 if (hardif_is_iface_up(batman_if))
355 hardif_activate_interface(batman_if);
356 else
357 bat_err(batman_if->soft_iface, "Not using interface %s "
358 "(retrying later): interface not active\n",
359 batman_if->net_dev->name);
361 /* begin scheduling originator messages on that interface */
362 schedule_own_packet(batman_if);
364 out:
365 return 0;
367 err:
368 return -ENOMEM;
371 void hardif_disable_interface(struct batman_if *batman_if)
373 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
375 if (batman_if->if_status == IF_ACTIVE)
376 hardif_deactivate_interface(batman_if);
378 if (batman_if->if_status != IF_INACTIVE)
379 return;
381 bat_info(batman_if->soft_iface, "Removing interface: %s\n",
382 batman_if->net_dev->name);
383 dev_remove_pack(&batman_if->batman_adv_ptype);
384 kref_put(&batman_if->refcount, hardif_free_ref);
386 bat_priv->num_ifaces--;
387 orig_hash_del_if(batman_if, bat_priv->num_ifaces);
389 if (batman_if == bat_priv->primary_if) {
390 struct batman_if *new_if;
392 new_if = get_active_batman_if(batman_if->soft_iface);
393 set_primary_if(bat_priv, new_if);
395 if (new_if)
396 kref_put(&new_if->refcount, hardif_free_ref);
399 kfree(batman_if->packet_buff);
400 batman_if->packet_buff = NULL;
401 batman_if->if_status = IF_NOT_IN_USE;
403 /* delete all references to this batman_if */
404 purge_orig_ref(bat_priv);
405 purge_outstanding_packets(bat_priv, batman_if);
406 dev_put(batman_if->soft_iface);
408 /* nobody uses this interface anymore */
409 if (!bat_priv->num_ifaces)
410 softif_destroy(batman_if->soft_iface);
412 batman_if->soft_iface = NULL;
415 static struct batman_if *hardif_add_interface(struct net_device *net_dev)
417 struct batman_if *batman_if;
418 int ret;
420 ret = is_valid_iface(net_dev);
421 if (ret != 1)
422 goto out;
424 dev_hold(net_dev);
426 batman_if = kmalloc(sizeof(struct batman_if), GFP_ATOMIC);
427 if (!batman_if) {
428 pr_err("Can't add interface (%s): out of memory\n",
429 net_dev->name);
430 goto release_dev;
433 ret = sysfs_add_hardif(&batman_if->hardif_obj, net_dev);
434 if (ret)
435 goto free_if;
437 batman_if->if_num = -1;
438 batman_if->net_dev = net_dev;
439 batman_if->soft_iface = NULL;
440 batman_if->if_status = IF_NOT_IN_USE;
441 INIT_LIST_HEAD(&batman_if->list);
442 kref_init(&batman_if->refcount);
444 check_known_mac_addr(batman_if->net_dev);
446 spin_lock(&if_list_lock);
447 list_add_tail_rcu(&batman_if->list, &if_list);
448 spin_unlock(&if_list_lock);
450 /* extra reference for return */
451 kref_get(&batman_if->refcount);
452 return batman_if;
454 free_if:
455 kfree(batman_if);
456 release_dev:
457 dev_put(net_dev);
458 out:
459 return NULL;
462 static void hardif_remove_interface(struct batman_if *batman_if)
464 /* first deactivate interface */
465 if (batman_if->if_status != IF_NOT_IN_USE)
466 hardif_disable_interface(batman_if);
468 if (batman_if->if_status != IF_NOT_IN_USE)
469 return;
471 batman_if->if_status = IF_TO_BE_REMOVED;
472 sysfs_del_hardif(&batman_if->hardif_obj);
473 call_rcu(&batman_if->rcu, hardif_free_rcu);
476 void hardif_remove_interfaces(void)
478 struct batman_if *batman_if, *batman_if_tmp;
479 struct list_head if_queue;
481 INIT_LIST_HEAD(&if_queue);
483 spin_lock(&if_list_lock);
484 list_for_each_entry_safe(batman_if, batman_if_tmp, &if_list, list) {
485 list_del_rcu(&batman_if->list);
486 list_add_tail(&batman_if->list, &if_queue);
488 spin_unlock(&if_list_lock);
490 rtnl_lock();
491 list_for_each_entry_safe(batman_if, batman_if_tmp, &if_queue, list) {
492 hardif_remove_interface(batman_if);
494 rtnl_unlock();
497 static int hard_if_event(struct notifier_block *this,
498 unsigned long event, void *ptr)
500 struct net_device *net_dev = (struct net_device *)ptr;
501 struct batman_if *batman_if = get_batman_if_by_netdev(net_dev);
502 struct bat_priv *bat_priv;
504 if (!batman_if && event == NETDEV_REGISTER)
505 batman_if = hardif_add_interface(net_dev);
507 if (!batman_if)
508 goto out;
510 switch (event) {
511 case NETDEV_UP:
512 hardif_activate_interface(batman_if);
513 break;
514 case NETDEV_GOING_DOWN:
515 case NETDEV_DOWN:
516 hardif_deactivate_interface(batman_if);
517 break;
518 case NETDEV_UNREGISTER:
519 spin_lock(&if_list_lock);
520 list_del_rcu(&batman_if->list);
521 spin_unlock(&if_list_lock);
523 hardif_remove_interface(batman_if);
524 break;
525 case NETDEV_CHANGEMTU:
526 if (batman_if->soft_iface)
527 update_min_mtu(batman_if->soft_iface);
528 break;
529 case NETDEV_CHANGEADDR:
530 if (batman_if->if_status == IF_NOT_IN_USE)
531 goto hardif_put;
533 check_known_mac_addr(batman_if->net_dev);
534 update_mac_addresses(batman_if);
536 bat_priv = netdev_priv(batman_if->soft_iface);
537 if (batman_if == bat_priv->primary_if)
538 update_primary_addr(bat_priv);
539 break;
540 default:
541 break;
544 hardif_put:
545 kref_put(&batman_if->refcount, hardif_free_ref);
546 out:
547 return NOTIFY_DONE;
550 /* receive a packet with the batman ethertype coming on a hard
551 * interface */
552 int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
553 struct packet_type *ptype, struct net_device *orig_dev)
555 struct bat_priv *bat_priv;
556 struct batman_packet *batman_packet;
557 struct batman_if *batman_if;
558 int ret;
560 batman_if = container_of(ptype, struct batman_if, batman_adv_ptype);
561 skb = skb_share_check(skb, GFP_ATOMIC);
563 /* skb was released by skb_share_check() */
564 if (!skb)
565 goto err_out;
567 /* packet should hold at least type and version */
568 if (unlikely(!pskb_may_pull(skb, 2)))
569 goto err_free;
571 /* expect a valid ethernet header here. */
572 if (unlikely(skb->mac_len != sizeof(struct ethhdr)
573 || !skb_mac_header(skb)))
574 goto err_free;
576 if (!batman_if->soft_iface)
577 goto err_free;
579 bat_priv = netdev_priv(batman_if->soft_iface);
581 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
582 goto err_free;
584 /* discard frames on not active interfaces */
585 if (batman_if->if_status != IF_ACTIVE)
586 goto err_free;
588 batman_packet = (struct batman_packet *)skb->data;
590 if (batman_packet->version != COMPAT_VERSION) {
591 bat_dbg(DBG_BATMAN, bat_priv,
592 "Drop packet: incompatible batman version (%i)\n",
593 batman_packet->version);
594 goto err_free;
597 /* all receive handlers return whether they received or reused
598 * the supplied skb. if not, we have to free the skb. */
600 switch (batman_packet->packet_type) {
601 /* batman originator packet */
602 case BAT_PACKET:
603 ret = recv_bat_packet(skb, batman_if);
604 break;
606 /* batman icmp packet */
607 case BAT_ICMP:
608 ret = recv_icmp_packet(skb, batman_if);
609 break;
611 /* unicast packet */
612 case BAT_UNICAST:
613 ret = recv_unicast_packet(skb, batman_if);
614 break;
616 /* fragmented unicast packet */
617 case BAT_UNICAST_FRAG:
618 ret = recv_ucast_frag_packet(skb, batman_if);
619 break;
621 /* broadcast packet */
622 case BAT_BCAST:
623 ret = recv_bcast_packet(skb, batman_if);
624 break;
626 /* vis packet */
627 case BAT_VIS:
628 ret = recv_vis_packet(skb, batman_if);
629 break;
630 default:
631 ret = NET_RX_DROP;
634 if (ret == NET_RX_DROP)
635 kfree_skb(skb);
637 /* return NET_RX_SUCCESS in any case as we
638 * most probably dropped the packet for
639 * routing-logical reasons. */
641 return NET_RX_SUCCESS;
643 err_free:
644 kfree_skb(skb);
645 err_out:
646 return NET_RX_DROP;
649 struct notifier_block hard_if_notifier = {
650 .notifier_call = hard_if_event,