2 * Copyright (C) 2007-2011 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 /* protect update critical side of if_list - but not the content */
35 static DEFINE_SPINLOCK(if_list_lock
);
38 static int batman_skb_recv(struct sk_buff
*skb
,
39 struct net_device
*dev
,
40 struct packet_type
*ptype
,
41 struct net_device
*orig_dev
);
43 static void hardif_free_rcu(struct rcu_head
*rcu
)
45 struct batman_if
*batman_if
;
47 batman_if
= container_of(rcu
, struct batman_if
, rcu
);
48 dev_put(batman_if
->net_dev
);
49 kref_put(&batman_if
->refcount
, hardif_free_ref
);
52 struct batman_if
*get_batman_if_by_netdev(struct net_device
*net_dev
)
54 struct batman_if
*batman_if
;
57 list_for_each_entry_rcu(batman_if
, &if_list
, list
) {
58 if (batman_if
->net_dev
== net_dev
)
66 kref_get(&batman_if
->refcount
);
72 static int is_valid_iface(struct net_device
*net_dev
)
74 if (net_dev
->flags
& IFF_LOOPBACK
)
77 if (net_dev
->type
!= ARPHRD_ETHER
)
80 if (net_dev
->addr_len
!= ETH_ALEN
)
83 /* no batman over batman */
84 #ifdef HAVE_NET_DEVICE_OPS
85 if (net_dev
->netdev_ops
->ndo_start_xmit
== interface_tx
)
88 if (net_dev
->hard_start_xmit
== interface_tx
)
92 /* Device is being bridged */
93 /* if (net_dev->priv_flags & IFF_BRIDGE_PORT)
99 static struct batman_if
*get_active_batman_if(struct net_device
*soft_iface
)
101 struct batman_if
*batman_if
;
104 list_for_each_entry_rcu(batman_if
, &if_list
, list
) {
105 if (batman_if
->soft_iface
!= soft_iface
)
108 if (batman_if
->if_status
== IF_ACTIVE
)
116 kref_get(&batman_if
->refcount
);
122 static void update_primary_addr(struct bat_priv
*bat_priv
)
124 struct vis_packet
*vis_packet
;
126 vis_packet
= (struct vis_packet
*)
127 bat_priv
->my_vis_info
->skb_packet
->data
;
128 memcpy(vis_packet
->vis_orig
,
129 bat_priv
->primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
130 memcpy(vis_packet
->sender_orig
,
131 bat_priv
->primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
134 static void set_primary_if(struct bat_priv
*bat_priv
,
135 struct batman_if
*batman_if
)
137 struct batman_packet
*batman_packet
;
138 struct batman_if
*old_if
;
141 kref_get(&batman_if
->refcount
);
143 old_if
= bat_priv
->primary_if
;
144 bat_priv
->primary_if
= batman_if
;
147 kref_put(&old_if
->refcount
, hardif_free_ref
);
149 if (!bat_priv
->primary_if
)
152 batman_packet
= (struct batman_packet
*)(batman_if
->packet_buff
);
153 batman_packet
->flags
= PRIMARIES_FIRST_HOP
;
154 batman_packet
->ttl
= TTL
;
156 update_primary_addr(bat_priv
);
159 * hacky trick to make sure that we send the HNA information via
160 * our new primary interface
162 atomic_set(&bat_priv
->hna_local_changed
, 1);
165 static bool hardif_is_iface_up(struct batman_if
*batman_if
)
167 if (batman_if
->net_dev
->flags
& IFF_UP
)
173 static void update_mac_addresses(struct batman_if
*batman_if
)
175 memcpy(((struct batman_packet
*)(batman_if
->packet_buff
))->orig
,
176 batman_if
->net_dev
->dev_addr
, ETH_ALEN
);
177 memcpy(((struct batman_packet
*)(batman_if
->packet_buff
))->prev_sender
,
178 batman_if
->net_dev
->dev_addr
, ETH_ALEN
);
181 static void check_known_mac_addr(struct net_device
*net_dev
)
183 struct batman_if
*batman_if
;
186 list_for_each_entry_rcu(batman_if
, &if_list
, list
) {
187 if ((batman_if
->if_status
!= IF_ACTIVE
) &&
188 (batman_if
->if_status
!= IF_TO_BE_ACTIVATED
))
191 if (batman_if
->net_dev
== net_dev
)
194 if (!compare_orig(batman_if
->net_dev
->dev_addr
,
198 pr_warning("The newly added mac address (%pM) already exists "
199 "on: %s\n", net_dev
->dev_addr
,
200 batman_if
->net_dev
->name
);
201 pr_warning("It is strongly recommended to keep mac addresses "
202 "unique to avoid problems!\n");
207 int hardif_min_mtu(struct net_device
*soft_iface
)
209 struct bat_priv
*bat_priv
= netdev_priv(soft_iface
);
210 struct batman_if
*batman_if
;
211 /* allow big frames if all devices are capable to do so
212 * (have MTU > 1500 + BAT_HEADER_LEN) */
213 int min_mtu
= ETH_DATA_LEN
;
215 if (atomic_read(&bat_priv
->fragmentation
))
219 list_for_each_entry_rcu(batman_if
, &if_list
, list
) {
220 if ((batman_if
->if_status
!= IF_ACTIVE
) &&
221 (batman_if
->if_status
!= IF_TO_BE_ACTIVATED
))
224 if (batman_if
->soft_iface
!= soft_iface
)
227 min_mtu
= min_t(int, batman_if
->net_dev
->mtu
- BAT_HEADER_LEN
,
235 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
236 void update_min_mtu(struct net_device
*soft_iface
)
240 min_mtu
= hardif_min_mtu(soft_iface
);
241 if (soft_iface
->mtu
!= min_mtu
)
242 soft_iface
->mtu
= min_mtu
;
245 static void hardif_activate_interface(struct batman_if
*batman_if
)
247 struct bat_priv
*bat_priv
;
249 if (batman_if
->if_status
!= IF_INACTIVE
)
252 bat_priv
= netdev_priv(batman_if
->soft_iface
);
254 update_mac_addresses(batman_if
);
255 batman_if
->if_status
= IF_TO_BE_ACTIVATED
;
258 * the first active interface becomes our primary interface or
259 * the next active interface after the old primay interface was removed
261 if (!bat_priv
->primary_if
)
262 set_primary_if(bat_priv
, batman_if
);
264 bat_info(batman_if
->soft_iface
, "Interface activated: %s\n",
265 batman_if
->net_dev
->name
);
267 update_min_mtu(batman_if
->soft_iface
);
271 static void hardif_deactivate_interface(struct batman_if
*batman_if
)
273 if ((batman_if
->if_status
!= IF_ACTIVE
) &&
274 (batman_if
->if_status
!= IF_TO_BE_ACTIVATED
))
277 batman_if
->if_status
= IF_INACTIVE
;
279 bat_info(batman_if
->soft_iface
, "Interface deactivated: %s\n",
280 batman_if
->net_dev
->name
);
282 update_min_mtu(batman_if
->soft_iface
);
285 int hardif_enable_interface(struct batman_if
*batman_if
, char *iface_name
)
287 struct bat_priv
*bat_priv
;
288 struct batman_packet
*batman_packet
;
290 if (batman_if
->if_status
!= IF_NOT_IN_USE
)
293 batman_if
->soft_iface
= dev_get_by_name(&init_net
, iface_name
);
295 if (!batman_if
->soft_iface
) {
296 batman_if
->soft_iface
= softif_create(iface_name
);
298 if (!batman_if
->soft_iface
)
301 /* dev_get_by_name() increases the reference counter for us */
302 dev_hold(batman_if
->soft_iface
);
305 bat_priv
= netdev_priv(batman_if
->soft_iface
);
306 batman_if
->packet_len
= BAT_PACKET_LEN
;
307 batman_if
->packet_buff
= kmalloc(batman_if
->packet_len
, GFP_ATOMIC
);
309 if (!batman_if
->packet_buff
) {
310 bat_err(batman_if
->soft_iface
, "Can't add interface packet "
311 "(%s): out of memory\n", batman_if
->net_dev
->name
);
315 batman_packet
= (struct batman_packet
*)(batman_if
->packet_buff
);
316 batman_packet
->packet_type
= BAT_PACKET
;
317 batman_packet
->version
= COMPAT_VERSION
;
318 batman_packet
->flags
= 0;
319 batman_packet
->ttl
= 2;
320 batman_packet
->tq
= TQ_MAX_VALUE
;
321 batman_packet
->num_hna
= 0;
323 batman_if
->if_num
= bat_priv
->num_ifaces
;
324 bat_priv
->num_ifaces
++;
325 batman_if
->if_status
= IF_INACTIVE
;
326 orig_hash_add_if(batman_if
, bat_priv
->num_ifaces
);
328 batman_if
->batman_adv_ptype
.type
= __constant_htons(ETH_P_BATMAN
);
329 batman_if
->batman_adv_ptype
.func
= batman_skb_recv
;
330 batman_if
->batman_adv_ptype
.dev
= batman_if
->net_dev
;
331 kref_get(&batman_if
->refcount
);
332 dev_add_pack(&batman_if
->batman_adv_ptype
);
334 atomic_set(&batman_if
->seqno
, 1);
335 atomic_set(&batman_if
->frag_seqno
, 1);
336 bat_info(batman_if
->soft_iface
, "Adding interface: %s\n",
337 batman_if
->net_dev
->name
);
339 if (atomic_read(&bat_priv
->fragmentation
) && batman_if
->net_dev
->mtu
<
340 ETH_DATA_LEN
+ BAT_HEADER_LEN
)
341 bat_info(batman_if
->soft_iface
,
342 "The MTU of interface %s is too small (%i) to handle "
343 "the transport of batman-adv packets. Packets going "
344 "over this interface will be fragmented on layer2 "
345 "which could impact the performance. Setting the MTU "
346 "to %zi would solve the problem.\n",
347 batman_if
->net_dev
->name
, batman_if
->net_dev
->mtu
,
348 ETH_DATA_LEN
+ BAT_HEADER_LEN
);
350 if (!atomic_read(&bat_priv
->fragmentation
) && batman_if
->net_dev
->mtu
<
351 ETH_DATA_LEN
+ BAT_HEADER_LEN
)
352 bat_info(batman_if
->soft_iface
,
353 "The MTU of interface %s is too small (%i) to handle "
354 "the transport of batman-adv packets. If you experience"
355 " problems getting traffic through try increasing the "
357 batman_if
->net_dev
->name
, batman_if
->net_dev
->mtu
,
358 ETH_DATA_LEN
+ BAT_HEADER_LEN
);
360 if (hardif_is_iface_up(batman_if
))
361 hardif_activate_interface(batman_if
);
363 bat_err(batman_if
->soft_iface
, "Not using interface %s "
364 "(retrying later): interface not active\n",
365 batman_if
->net_dev
->name
);
367 /* begin scheduling originator messages on that interface */
368 schedule_own_packet(batman_if
);
377 void hardif_disable_interface(struct batman_if
*batman_if
)
379 struct bat_priv
*bat_priv
= netdev_priv(batman_if
->soft_iface
);
381 if (batman_if
->if_status
== IF_ACTIVE
)
382 hardif_deactivate_interface(batman_if
);
384 if (batman_if
->if_status
!= IF_INACTIVE
)
387 bat_info(batman_if
->soft_iface
, "Removing interface: %s\n",
388 batman_if
->net_dev
->name
);
389 dev_remove_pack(&batman_if
->batman_adv_ptype
);
390 kref_put(&batman_if
->refcount
, hardif_free_ref
);
392 bat_priv
->num_ifaces
--;
393 orig_hash_del_if(batman_if
, bat_priv
->num_ifaces
);
395 if (batman_if
== bat_priv
->primary_if
) {
396 struct batman_if
*new_if
;
398 new_if
= get_active_batman_if(batman_if
->soft_iface
);
399 set_primary_if(bat_priv
, new_if
);
402 kref_put(&new_if
->refcount
, hardif_free_ref
);
405 kfree(batman_if
->packet_buff
);
406 batman_if
->packet_buff
= NULL
;
407 batman_if
->if_status
= IF_NOT_IN_USE
;
409 /* delete all references to this batman_if */
410 purge_orig_ref(bat_priv
);
411 purge_outstanding_packets(bat_priv
, batman_if
);
412 dev_put(batman_if
->soft_iface
);
414 /* nobody uses this interface anymore */
415 if (!bat_priv
->num_ifaces
)
416 softif_destroy(batman_if
->soft_iface
);
418 batman_if
->soft_iface
= NULL
;
421 static struct batman_if
*hardif_add_interface(struct net_device
*net_dev
)
423 struct batman_if
*batman_if
;
426 ret
= is_valid_iface(net_dev
);
432 batman_if
= kmalloc(sizeof(struct batman_if
), GFP_ATOMIC
);
434 pr_err("Can't add interface (%s): out of memory\n",
439 ret
= sysfs_add_hardif(&batman_if
->hardif_obj
, net_dev
);
443 batman_if
->if_num
= -1;
444 batman_if
->net_dev
= net_dev
;
445 batman_if
->soft_iface
= NULL
;
446 batman_if
->if_status
= IF_NOT_IN_USE
;
447 INIT_LIST_HEAD(&batman_if
->list
);
448 kref_init(&batman_if
->refcount
);
450 check_known_mac_addr(batman_if
->net_dev
);
452 spin_lock(&if_list_lock
);
453 list_add_tail_rcu(&batman_if
->list
, &if_list
);
454 spin_unlock(&if_list_lock
);
456 /* extra reference for return */
457 kref_get(&batman_if
->refcount
);
468 static void hardif_remove_interface(struct batman_if
*batman_if
)
470 /* first deactivate interface */
471 if (batman_if
->if_status
!= IF_NOT_IN_USE
)
472 hardif_disable_interface(batman_if
);
474 if (batman_if
->if_status
!= IF_NOT_IN_USE
)
477 batman_if
->if_status
= IF_TO_BE_REMOVED
;
478 sysfs_del_hardif(&batman_if
->hardif_obj
);
479 call_rcu(&batman_if
->rcu
, hardif_free_rcu
);
482 void hardif_remove_interfaces(void)
484 struct batman_if
*batman_if
, *batman_if_tmp
;
485 struct list_head if_queue
;
487 INIT_LIST_HEAD(&if_queue
);
489 spin_lock(&if_list_lock
);
490 list_for_each_entry_safe(batman_if
, batman_if_tmp
, &if_list
, list
) {
491 list_del_rcu(&batman_if
->list
);
492 list_add_tail(&batman_if
->list
, &if_queue
);
494 spin_unlock(&if_list_lock
);
497 list_for_each_entry_safe(batman_if
, batman_if_tmp
, &if_queue
, list
) {
498 hardif_remove_interface(batman_if
);
503 static int hard_if_event(struct notifier_block
*this,
504 unsigned long event
, void *ptr
)
506 struct net_device
*net_dev
= (struct net_device
*)ptr
;
507 struct batman_if
*batman_if
= get_batman_if_by_netdev(net_dev
);
508 struct bat_priv
*bat_priv
;
510 if (!batman_if
&& event
== NETDEV_REGISTER
)
511 batman_if
= hardif_add_interface(net_dev
);
518 hardif_activate_interface(batman_if
);
520 case NETDEV_GOING_DOWN
:
522 hardif_deactivate_interface(batman_if
);
524 case NETDEV_UNREGISTER
:
525 spin_lock(&if_list_lock
);
526 list_del_rcu(&batman_if
->list
);
527 spin_unlock(&if_list_lock
);
529 hardif_remove_interface(batman_if
);
531 case NETDEV_CHANGEMTU
:
532 if (batman_if
->soft_iface
)
533 update_min_mtu(batman_if
->soft_iface
);
535 case NETDEV_CHANGEADDR
:
536 if (batman_if
->if_status
== IF_NOT_IN_USE
)
539 check_known_mac_addr(batman_if
->net_dev
);
540 update_mac_addresses(batman_if
);
542 bat_priv
= netdev_priv(batman_if
->soft_iface
);
543 if (batman_if
== bat_priv
->primary_if
)
544 update_primary_addr(bat_priv
);
551 kref_put(&batman_if
->refcount
, hardif_free_ref
);
556 /* receive a packet with the batman ethertype coming on a hard
558 static int batman_skb_recv(struct sk_buff
*skb
, struct net_device
*dev
,
559 struct packet_type
*ptype
,
560 struct net_device
*orig_dev
)
562 struct bat_priv
*bat_priv
;
563 struct batman_packet
*batman_packet
;
564 struct batman_if
*batman_if
;
567 batman_if
= container_of(ptype
, struct batman_if
, batman_adv_ptype
);
568 skb
= skb_share_check(skb
, GFP_ATOMIC
);
570 /* skb was released by skb_share_check() */
574 /* packet should hold at least type and version */
575 if (unlikely(!pskb_may_pull(skb
, 2)))
578 /* expect a valid ethernet header here. */
579 if (unlikely(skb
->mac_len
!= sizeof(struct ethhdr
)
580 || !skb_mac_header(skb
)))
583 if (!batman_if
->soft_iface
)
586 bat_priv
= netdev_priv(batman_if
->soft_iface
);
588 if (atomic_read(&bat_priv
->mesh_state
) != MESH_ACTIVE
)
591 /* discard frames on not active interfaces */
592 if (batman_if
->if_status
!= IF_ACTIVE
)
595 batman_packet
= (struct batman_packet
*)skb
->data
;
597 if (batman_packet
->version
!= COMPAT_VERSION
) {
598 bat_dbg(DBG_BATMAN
, bat_priv
,
599 "Drop packet: incompatible batman version (%i)\n",
600 batman_packet
->version
);
604 /* all receive handlers return whether they received or reused
605 * the supplied skb. if not, we have to free the skb. */
607 switch (batman_packet
->packet_type
) {
608 /* batman originator packet */
610 ret
= recv_bat_packet(skb
, batman_if
);
613 /* batman icmp packet */
615 ret
= recv_icmp_packet(skb
, batman_if
);
620 ret
= recv_unicast_packet(skb
, batman_if
);
623 /* fragmented unicast packet */
624 case BAT_UNICAST_FRAG
:
625 ret
= recv_ucast_frag_packet(skb
, batman_if
);
628 /* broadcast packet */
630 ret
= recv_bcast_packet(skb
, batman_if
);
635 ret
= recv_vis_packet(skb
, batman_if
);
641 if (ret
== NET_RX_DROP
)
644 /* return NET_RX_SUCCESS in any case as we
645 * most probably dropped the packet for
646 * routing-logical reasons. */
648 return NET_RX_SUCCESS
;
656 struct notifier_block hard_if_notifier
= {
657 .notifier_call
= hard_if_event
,