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 set_primary_if(struct bat_priv *bat_priv,
110 struct batman_if *batman_if)
112 struct batman_packet *batman_packet;
113 struct vis_packet *vis_packet;
114 struct batman_if *old_if;
117 hardif_hold(batman_if);
119 old_if = bat_priv->primary_if;
120 bat_priv->primary_if = batman_if;
125 if (!bat_priv->primary_if)
128 batman_packet = (struct batman_packet *)(batman_if->packet_buff);
129 batman_packet->flags = PRIMARIES_FIRST_HOP;
130 batman_packet->ttl = TTL;
132 vis_packet = (struct vis_packet *)
133 bat_priv->my_vis_info->skb_packet->data;
134 memcpy(vis_packet->vis_orig,
135 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
136 memcpy(vis_packet->sender_orig,
137 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
140 * hacky trick to make sure that we send the HNA information via
141 * our new primary interface
143 atomic_set(&bat_priv->hna_local_changed, 1);
146 static bool hardif_is_iface_up(struct batman_if *batman_if)
148 if (batman_if->net_dev->flags & IFF_UP)
154 static void update_mac_addresses(struct batman_if *batman_if)
156 addr_to_string(batman_if->addr_str, batman_if->net_dev->dev_addr);
158 memcpy(((struct batman_packet *)(batman_if->packet_buff))->orig,
159 batman_if->net_dev->dev_addr, ETH_ALEN);
160 memcpy(((struct batman_packet *)(batman_if->packet_buff))->prev_sender,
161 batman_if->net_dev->dev_addr, ETH_ALEN);
164 static void check_known_mac_addr(uint8_t *addr)
166 struct batman_if *batman_if;
169 list_for_each_entry_rcu(batman_if, &if_list, list) {
170 if ((batman_if->if_status != IF_ACTIVE) &&
171 (batman_if->if_status != IF_TO_BE_ACTIVATED))
174 if (!compare_orig(batman_if->net_dev->dev_addr, addr))
177 pr_warning("The newly added mac address (%pM) already exists "
178 "on: %s\n", addr, batman_if->net_dev->name);
179 pr_warning("It is strongly recommended to keep mac addresses "
180 "unique to avoid problems!\n");
185 int hardif_min_mtu(struct net_device *soft_iface)
187 struct bat_priv *bat_priv = netdev_priv(soft_iface);
188 struct batman_if *batman_if;
189 /* allow big frames if all devices are capable to do so
190 * (have MTU > 1500 + BAT_HEADER_LEN) */
191 int min_mtu = ETH_DATA_LEN;
193 if (atomic_read(&bat_priv->frag_enabled))
197 list_for_each_entry_rcu(batman_if, &if_list, list) {
198 if ((batman_if->if_status != IF_ACTIVE) &&
199 (batman_if->if_status != IF_TO_BE_ACTIVATED))
202 if (batman_if->soft_iface != soft_iface)
205 min_mtu = MIN(batman_if->net_dev->mtu - BAT_HEADER_LEN,
213 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
214 void update_min_mtu(struct net_device *soft_iface)
218 min_mtu = hardif_min_mtu(soft_iface);
219 if (soft_iface->mtu != min_mtu)
220 soft_iface->mtu = min_mtu;
223 static void hardif_activate_interface(struct batman_if *batman_if)
225 struct bat_priv *bat_priv;
227 if (batman_if->if_status != IF_INACTIVE)
230 bat_priv = netdev_priv(batman_if->soft_iface);
232 update_mac_addresses(batman_if);
233 batman_if->if_status = IF_TO_BE_ACTIVATED;
236 * the first active interface becomes our primary interface or
237 * the next active interface after the old primay interface was removed
239 if (!bat_priv->primary_if)
240 set_primary_if(bat_priv, batman_if);
242 bat_info(batman_if->soft_iface, "Interface activated: %s\n",
243 batman_if->net_dev->name);
245 update_min_mtu(batman_if->soft_iface);
249 static void hardif_deactivate_interface(struct batman_if *batman_if)
251 if ((batman_if->if_status != IF_ACTIVE) &&
252 (batman_if->if_status != IF_TO_BE_ACTIVATED))
255 batman_if->if_status = IF_INACTIVE;
257 bat_info(batman_if->soft_iface, "Interface deactivated: %s\n",
258 batman_if->net_dev->name);
260 update_min_mtu(batman_if->soft_iface);
263 int hardif_enable_interface(struct batman_if *batman_if, char *iface_name)
265 struct bat_priv *bat_priv;
266 struct batman_packet *batman_packet;
268 if (batman_if->if_status != IF_NOT_IN_USE)
271 batman_if->soft_iface = dev_get_by_name(&init_net, iface_name);
273 if (!batman_if->soft_iface) {
274 batman_if->soft_iface = softif_create(iface_name);
276 if (!batman_if->soft_iface)
279 /* dev_get_by_name() increases the reference counter for us */
280 dev_hold(batman_if->soft_iface);
283 bat_priv = netdev_priv(batman_if->soft_iface);
284 batman_if->packet_len = BAT_PACKET_LEN;
285 batman_if->packet_buff = kmalloc(batman_if->packet_len, GFP_ATOMIC);
287 if (!batman_if->packet_buff) {
288 bat_err(batman_if->soft_iface, "Can't add interface packet "
289 "(%s): out of memory\n", batman_if->net_dev->name);
293 batman_packet = (struct batman_packet *)(batman_if->packet_buff);
294 batman_packet->packet_type = BAT_PACKET;
295 batman_packet->version = COMPAT_VERSION;
296 batman_packet->flags = 0;
297 batman_packet->ttl = 2;
298 batman_packet->tq = TQ_MAX_VALUE;
299 batman_packet->num_hna = 0;
301 batman_if->if_num = bat_priv->num_ifaces;
302 bat_priv->num_ifaces++;
303 batman_if->if_status = IF_INACTIVE;
304 orig_hash_add_if(batman_if, bat_priv->num_ifaces);
306 batman_if->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
307 batman_if->batman_adv_ptype.func = batman_skb_recv;
308 batman_if->batman_adv_ptype.dev = batman_if->net_dev;
309 hardif_hold(batman_if);
310 dev_add_pack(&batman_if->batman_adv_ptype);
312 atomic_set(&batman_if->seqno, 1);
313 atomic_set(&batman_if->frag_seqno, 1);
314 bat_info(batman_if->soft_iface, "Adding interface: %s\n",
315 batman_if->net_dev->name);
317 if (atomic_read(&bat_priv->frag_enabled) && batman_if->net_dev->mtu <
318 ETH_DATA_LEN + BAT_HEADER_LEN)
319 bat_info(batman_if->soft_iface,
320 "The MTU of interface %s is too small (%i) to handle "
321 "the transport of batman-adv packets. Packets going "
322 "over this interface will be fragmented on layer2 "
323 "which could impact the performance. Setting the MTU "
324 "to %zi would solve the problem.\n",
325 batman_if->net_dev->name, batman_if->net_dev->mtu,
326 ETH_DATA_LEN + BAT_HEADER_LEN);
328 if (!atomic_read(&bat_priv->frag_enabled) && batman_if->net_dev->mtu <
329 ETH_DATA_LEN + BAT_HEADER_LEN)
330 bat_info(batman_if->soft_iface,
331 "The MTU of interface %s is too small (%i) to handle "
332 "the transport of batman-adv packets. If you experience"
333 " problems getting traffic through try increasing the "
335 batman_if->net_dev->name, batman_if->net_dev->mtu,
336 ETH_DATA_LEN + BAT_HEADER_LEN);
338 if (hardif_is_iface_up(batman_if))
339 hardif_activate_interface(batman_if);
341 bat_err(batman_if->soft_iface, "Not using interface %s "
342 "(retrying later): interface not active\n",
343 batman_if->net_dev->name);
345 /* begin scheduling originator messages on that interface */
346 schedule_own_packet(batman_if);
355 void hardif_disable_interface(struct batman_if *batman_if)
357 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
359 if (batman_if->if_status == IF_ACTIVE)
360 hardif_deactivate_interface(batman_if);
362 if (batman_if->if_status != IF_INACTIVE)
365 bat_info(batman_if->soft_iface, "Removing interface: %s\n",
366 batman_if->net_dev->name);
367 dev_remove_pack(&batman_if->batman_adv_ptype);
368 hardif_put(batman_if);
370 bat_priv->num_ifaces--;
371 orig_hash_del_if(batman_if, bat_priv->num_ifaces);
373 if (batman_if == bat_priv->primary_if) {
374 struct batman_if *new_if;
376 new_if = get_active_batman_if(batman_if->soft_iface);
377 set_primary_if(bat_priv, new_if);
383 kfree(batman_if->packet_buff);
384 batman_if->packet_buff = NULL;
385 batman_if->if_status = IF_NOT_IN_USE;
387 /* delete all references to this batman_if */
388 purge_orig_ref(bat_priv);
389 purge_outstanding_packets(bat_priv, batman_if);
390 dev_put(batman_if->soft_iface);
392 /* nobody uses this interface anymore */
393 if (!bat_priv->num_ifaces)
394 softif_destroy(batman_if->soft_iface);
396 batman_if->soft_iface = NULL;
399 static struct batman_if *hardif_add_interface(struct net_device *net_dev)
401 struct batman_if *batman_if;
404 ret = is_valid_iface(net_dev);
410 batman_if = kmalloc(sizeof(struct batman_if), GFP_ATOMIC);
412 pr_err("Can't add interface (%s): out of memory\n",
417 ret = sysfs_add_hardif(&batman_if->hardif_obj, net_dev);
421 batman_if->if_num = -1;
422 batman_if->net_dev = net_dev;
423 batman_if->soft_iface = NULL;
424 batman_if->if_status = IF_NOT_IN_USE;
425 INIT_LIST_HEAD(&batman_if->list);
426 atomic_set(&batman_if->refcnt, 0);
427 hardif_hold(batman_if);
429 check_known_mac_addr(batman_if->net_dev->dev_addr);
431 spin_lock(&if_list_lock);
432 list_add_tail_rcu(&batman_if->list, &if_list);
433 spin_unlock(&if_list_lock);
435 /* extra reference for return */
436 hardif_hold(batman_if);
447 static void hardif_remove_interface(struct batman_if *batman_if)
449 /* first deactivate interface */
450 if (batman_if->if_status != IF_NOT_IN_USE)
451 hardif_disable_interface(batman_if);
453 if (batman_if->if_status != IF_NOT_IN_USE)
456 batman_if->if_status = IF_TO_BE_REMOVED;
458 /* caller must take if_list_lock */
459 list_del_rcu(&batman_if->list);
461 sysfs_del_hardif(&batman_if->hardif_obj);
462 hardif_put(batman_if);
465 void hardif_remove_interfaces(void)
467 struct batman_if *batman_if, *batman_if_tmp;
470 spin_lock(&if_list_lock);
471 list_for_each_entry_safe(batman_if, batman_if_tmp, &if_list, list) {
472 hardif_remove_interface(batman_if);
474 spin_unlock(&if_list_lock);
478 static int hard_if_event(struct notifier_block *this,
479 unsigned long event, void *ptr)
481 struct net_device *net_dev = (struct net_device *)ptr;
482 struct batman_if *batman_if = get_batman_if_by_netdev(net_dev);
483 struct bat_priv *bat_priv;
485 if (!batman_if && event == NETDEV_REGISTER)
486 batman_if = hardif_add_interface(net_dev);
493 hardif_activate_interface(batman_if);
495 case NETDEV_GOING_DOWN:
497 hardif_deactivate_interface(batman_if);
499 case NETDEV_UNREGISTER:
500 spin_lock(&if_list_lock);
501 hardif_remove_interface(batman_if);
502 spin_unlock(&if_list_lock);
504 case NETDEV_CHANGEMTU:
505 if (batman_if->soft_iface)
506 update_min_mtu(batman_if->soft_iface);
508 case NETDEV_CHANGEADDR:
509 if (batman_if->if_status == IF_NOT_IN_USE) {
510 hardif_put(batman_if);
514 check_known_mac_addr(batman_if->net_dev->dev_addr);
515 update_mac_addresses(batman_if);
517 bat_priv = netdev_priv(batman_if->soft_iface);
518 if (batman_if == bat_priv->primary_if)
519 set_primary_if(bat_priv, batman_if);
524 hardif_put(batman_if);
530 /* receive a packet with the batman ethertype coming on a hard
532 int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
533 struct packet_type *ptype, struct net_device *orig_dev)
535 struct bat_priv *bat_priv;
536 struct batman_packet *batman_packet;
537 struct batman_if *batman_if;
540 batman_if = container_of(ptype, struct batman_if, batman_adv_ptype);
541 skb = skb_share_check(skb, GFP_ATOMIC);
543 /* skb was released by skb_share_check() */
547 /* packet should hold at least type and version */
548 if (unlikely(!pskb_may_pull(skb, 2)))
551 /* expect a valid ethernet header here. */
552 if (unlikely(skb->mac_len != sizeof(struct ethhdr)
553 || !skb_mac_header(skb)))
556 if (!batman_if->soft_iface)
559 bat_priv = netdev_priv(batman_if->soft_iface);
561 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
564 /* discard frames on not active interfaces */
565 if (batman_if->if_status != IF_ACTIVE)
568 batman_packet = (struct batman_packet *)skb->data;
570 if (batman_packet->version != COMPAT_VERSION) {
571 bat_dbg(DBG_BATMAN, bat_priv,
572 "Drop packet: incompatible batman version (%i)\n",
573 batman_packet->version);
577 /* all receive handlers return whether they received or reused
578 * the supplied skb. if not, we have to free the skb. */
580 switch (batman_packet->packet_type) {
581 /* batman originator packet */
583 ret = recv_bat_packet(skb, batman_if);
586 /* batman icmp packet */
588 ret = recv_icmp_packet(skb, batman_if);
593 ret = recv_unicast_packet(skb, batman_if);
596 /* fragmented unicast packet */
597 case BAT_UNICAST_FRAG:
598 ret = recv_ucast_frag_packet(skb, batman_if);
601 /* broadcast packet */
603 ret = recv_bcast_packet(skb, batman_if);
608 ret = recv_vis_packet(skb, batman_if);
614 if (ret == NET_RX_DROP)
617 /* return NET_RX_SUCCESS in any case as we
618 * most probably dropped the packet for
619 * routing-logical reasons. */
621 return NET_RX_SUCCESS;
629 struct notifier_block hard_if_notifier = {
630 .notifier_call = hard_if_event,