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(uint8_t *addr)
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 (!compare_orig(batman_if->net_dev->dev_addr, addr))
181 pr_warning("The newly added mac address (%pM) already exists "
182 "on: %s\n", addr, batman_if->net_dev->name);
183 pr_warning("It is strongly recommended to keep mac addresses "
184 "unique to avoid problems!\n");
189 int hardif_min_mtu(struct net_device *soft_iface)
191 struct bat_priv *bat_priv = netdev_priv(soft_iface);
192 struct batman_if *batman_if;
193 /* allow big frames if all devices are capable to do so
194 * (have MTU > 1500 + BAT_HEADER_LEN) */
195 int min_mtu = ETH_DATA_LEN;
197 if (atomic_read(&bat_priv->frag_enabled))
201 list_for_each_entry_rcu(batman_if, &if_list, list) {
202 if ((batman_if->if_status != IF_ACTIVE) &&
203 (batman_if->if_status != IF_TO_BE_ACTIVATED))
206 if (batman_if->soft_iface != soft_iface)
209 min_mtu = MIN(batman_if->net_dev->mtu - BAT_HEADER_LEN,
217 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
218 void update_min_mtu(struct net_device *soft_iface)
222 min_mtu = hardif_min_mtu(soft_iface);
223 if (soft_iface->mtu != min_mtu)
224 soft_iface->mtu = min_mtu;
227 static void hardif_activate_interface(struct batman_if *batman_if)
229 struct bat_priv *bat_priv;
231 if (batman_if->if_status != IF_INACTIVE)
234 bat_priv = netdev_priv(batman_if->soft_iface);
236 update_mac_addresses(batman_if);
237 batman_if->if_status = IF_TO_BE_ACTIVATED;
240 * the first active interface becomes our primary interface or
241 * the next active interface after the old primay interface was removed
243 if (!bat_priv->primary_if)
244 set_primary_if(bat_priv, batman_if);
246 bat_info(batman_if->soft_iface, "Interface activated: %s\n",
247 batman_if->net_dev->name);
249 update_min_mtu(batman_if->soft_iface);
253 static void hardif_deactivate_interface(struct batman_if *batman_if)
255 if ((batman_if->if_status != IF_ACTIVE) &&
256 (batman_if->if_status != IF_TO_BE_ACTIVATED))
259 batman_if->if_status = IF_INACTIVE;
261 bat_info(batman_if->soft_iface, "Interface deactivated: %s\n",
262 batman_if->net_dev->name);
264 update_min_mtu(batman_if->soft_iface);
267 int hardif_enable_interface(struct batman_if *batman_if, char *iface_name)
269 struct bat_priv *bat_priv;
270 struct batman_packet *batman_packet;
272 if (batman_if->if_status != IF_NOT_IN_USE)
275 batman_if->soft_iface = dev_get_by_name(&init_net, iface_name);
277 if (!batman_if->soft_iface) {
278 batman_if->soft_iface = softif_create(iface_name);
280 if (!batman_if->soft_iface)
283 /* dev_get_by_name() increases the reference counter for us */
284 dev_hold(batman_if->soft_iface);
287 bat_priv = netdev_priv(batman_if->soft_iface);
288 batman_if->packet_len = BAT_PACKET_LEN;
289 batman_if->packet_buff = kmalloc(batman_if->packet_len, GFP_ATOMIC);
291 if (!batman_if->packet_buff) {
292 bat_err(batman_if->soft_iface, "Can't add interface packet "
293 "(%s): out of memory\n", batman_if->net_dev->name);
297 batman_packet = (struct batman_packet *)(batman_if->packet_buff);
298 batman_packet->packet_type = BAT_PACKET;
299 batman_packet->version = COMPAT_VERSION;
300 batman_packet->flags = 0;
301 batman_packet->ttl = 2;
302 batman_packet->tq = TQ_MAX_VALUE;
303 batman_packet->num_hna = 0;
305 batman_if->if_num = bat_priv->num_ifaces;
306 bat_priv->num_ifaces++;
307 batman_if->if_status = IF_INACTIVE;
308 orig_hash_add_if(batman_if, bat_priv->num_ifaces);
310 batman_if->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
311 batman_if->batman_adv_ptype.func = batman_skb_recv;
312 batman_if->batman_adv_ptype.dev = batman_if->net_dev;
313 hardif_hold(batman_if);
314 dev_add_pack(&batman_if->batman_adv_ptype);
316 atomic_set(&batman_if->seqno, 1);
317 atomic_set(&batman_if->frag_seqno, 1);
318 bat_info(batman_if->soft_iface, "Adding interface: %s\n",
319 batman_if->net_dev->name);
321 if (atomic_read(&bat_priv->frag_enabled) && batman_if->net_dev->mtu <
322 ETH_DATA_LEN + BAT_HEADER_LEN)
323 bat_info(batman_if->soft_iface,
324 "The MTU of interface %s is too small (%i) to handle "
325 "the transport of batman-adv packets. Packets going "
326 "over this interface will be fragmented on layer2 "
327 "which could impact the performance. Setting the MTU "
328 "to %zi would solve the problem.\n",
329 batman_if->net_dev->name, batman_if->net_dev->mtu,
330 ETH_DATA_LEN + BAT_HEADER_LEN);
332 if (!atomic_read(&bat_priv->frag_enabled) && batman_if->net_dev->mtu <
333 ETH_DATA_LEN + BAT_HEADER_LEN)
334 bat_info(batman_if->soft_iface,
335 "The MTU of interface %s is too small (%i) to handle "
336 "the transport of batman-adv packets. If you experience"
337 " problems getting traffic through try increasing the "
339 batman_if->net_dev->name, batman_if->net_dev->mtu,
340 ETH_DATA_LEN + BAT_HEADER_LEN);
342 if (hardif_is_iface_up(batman_if))
343 hardif_activate_interface(batman_if);
345 bat_err(batman_if->soft_iface, "Not using interface %s "
346 "(retrying later): interface not active\n",
347 batman_if->net_dev->name);
349 /* begin scheduling originator messages on that interface */
350 schedule_own_packet(batman_if);
359 void hardif_disable_interface(struct batman_if *batman_if)
361 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
363 if (batman_if->if_status == IF_ACTIVE)
364 hardif_deactivate_interface(batman_if);
366 if (batman_if->if_status != IF_INACTIVE)
369 bat_info(batman_if->soft_iface, "Removing interface: %s\n",
370 batman_if->net_dev->name);
371 dev_remove_pack(&batman_if->batman_adv_ptype);
372 hardif_put(batman_if);
374 bat_priv->num_ifaces--;
375 orig_hash_del_if(batman_if, bat_priv->num_ifaces);
377 if (batman_if == bat_priv->primary_if) {
378 struct batman_if *new_if;
380 new_if = get_active_batman_if(batman_if->soft_iface);
381 set_primary_if(bat_priv, new_if);
387 kfree(batman_if->packet_buff);
388 batman_if->packet_buff = NULL;
389 batman_if->if_status = IF_NOT_IN_USE;
391 /* delete all references to this batman_if */
392 purge_orig_ref(bat_priv);
393 purge_outstanding_packets(bat_priv, batman_if);
394 dev_put(batman_if->soft_iface);
396 /* nobody uses this interface anymore */
397 if (!bat_priv->num_ifaces)
398 softif_destroy(batman_if->soft_iface);
400 batman_if->soft_iface = NULL;
403 static struct batman_if *hardif_add_interface(struct net_device *net_dev)
405 struct batman_if *batman_if;
408 ret = is_valid_iface(net_dev);
414 batman_if = kmalloc(sizeof(struct batman_if), GFP_ATOMIC);
416 pr_err("Can't add interface (%s): out of memory\n",
421 ret = sysfs_add_hardif(&batman_if->hardif_obj, net_dev);
425 batman_if->if_num = -1;
426 batman_if->net_dev = net_dev;
427 batman_if->soft_iface = NULL;
428 batman_if->if_status = IF_NOT_IN_USE;
429 INIT_LIST_HEAD(&batman_if->list);
430 atomic_set(&batman_if->refcnt, 0);
431 hardif_hold(batman_if);
433 check_known_mac_addr(batman_if->net_dev->dev_addr);
435 spin_lock(&if_list_lock);
436 list_add_tail_rcu(&batman_if->list, &if_list);
437 spin_unlock(&if_list_lock);
439 /* extra reference for return */
440 hardif_hold(batman_if);
451 static void hardif_remove_interface(struct batman_if *batman_if)
453 /* first deactivate interface */
454 if (batman_if->if_status != IF_NOT_IN_USE)
455 hardif_disable_interface(batman_if);
457 if (batman_if->if_status != IF_NOT_IN_USE)
460 batman_if->if_status = IF_TO_BE_REMOVED;
462 /* caller must take if_list_lock */
463 list_del_rcu(&batman_if->list);
465 sysfs_del_hardif(&batman_if->hardif_obj);
466 hardif_put(batman_if);
469 void hardif_remove_interfaces(void)
471 struct batman_if *batman_if, *batman_if_tmp;
474 spin_lock(&if_list_lock);
475 list_for_each_entry_safe(batman_if, batman_if_tmp, &if_list, list) {
476 hardif_remove_interface(batman_if);
478 spin_unlock(&if_list_lock);
482 static int hard_if_event(struct notifier_block *this,
483 unsigned long event, void *ptr)
485 struct net_device *net_dev = (struct net_device *)ptr;
486 struct batman_if *batman_if = get_batman_if_by_netdev(net_dev);
487 struct bat_priv *bat_priv;
489 if (!batman_if && event == NETDEV_REGISTER)
490 batman_if = hardif_add_interface(net_dev);
497 hardif_activate_interface(batman_if);
499 case NETDEV_GOING_DOWN:
501 hardif_deactivate_interface(batman_if);
503 case NETDEV_UNREGISTER:
504 spin_lock(&if_list_lock);
505 hardif_remove_interface(batman_if);
506 spin_unlock(&if_list_lock);
508 case NETDEV_CHANGEMTU:
509 if (batman_if->soft_iface)
510 update_min_mtu(batman_if->soft_iface);
512 case NETDEV_CHANGEADDR:
513 if (batman_if->if_status == IF_NOT_IN_USE) {
514 hardif_put(batman_if);
518 check_known_mac_addr(batman_if->net_dev->dev_addr);
519 update_mac_addresses(batman_if);
521 bat_priv = netdev_priv(batman_if->soft_iface);
522 if (batman_if == bat_priv->primary_if)
523 update_primary_addr(bat_priv);
528 hardif_put(batman_if);
534 /* receive a packet with the batman ethertype coming on a hard
536 int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
537 struct packet_type *ptype, struct net_device *orig_dev)
539 struct bat_priv *bat_priv;
540 struct batman_packet *batman_packet;
541 struct batman_if *batman_if;
544 batman_if = container_of(ptype, struct batman_if, batman_adv_ptype);
545 skb = skb_share_check(skb, GFP_ATOMIC);
547 /* skb was released by skb_share_check() */
551 /* packet should hold at least type and version */
552 if (unlikely(!pskb_may_pull(skb, 2)))
555 /* expect a valid ethernet header here. */
556 if (unlikely(skb->mac_len != sizeof(struct ethhdr)
557 || !skb_mac_header(skb)))
560 if (!batman_if->soft_iface)
563 bat_priv = netdev_priv(batman_if->soft_iface);
565 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
568 /* discard frames on not active interfaces */
569 if (batman_if->if_status != IF_ACTIVE)
572 batman_packet = (struct batman_packet *)skb->data;
574 if (batman_packet->version != COMPAT_VERSION) {
575 bat_dbg(DBG_BATMAN, bat_priv,
576 "Drop packet: incompatible batman version (%i)\n",
577 batman_packet->version);
581 /* all receive handlers return whether they received or reused
582 * the supplied skb. if not, we have to free the skb. */
584 switch (batman_packet->packet_type) {
585 /* batman originator packet */
587 ret = recv_bat_packet(skb, batman_if);
590 /* batman icmp packet */
592 ret = recv_icmp_packet(skb, batman_if);
597 ret = recv_unicast_packet(skb, batman_if);
600 /* fragmented unicast packet */
601 case BAT_UNICAST_FRAG:
602 ret = recv_ucast_frag_packet(skb, batman_if);
605 /* broadcast packet */
607 ret = recv_bcast_packet(skb, batman_if);
612 ret = recv_vis_packet(skb, batman_if);
618 if (ret == NET_RX_DROP)
621 /* return NET_RX_SUCCESS in any case as we
622 * most probably dropped the packet for
623 * routing-logical reasons. */
625 return NET_RX_SUCCESS;
633 struct notifier_block hard_if_notifier = {
634 .notifier_call = hard_if_event,