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
;
115 bat_priv
->primary_if
= batman_if
;
117 if (!bat_priv
->primary_if
)
120 batman_packet
= (struct batman_packet
*)(batman_if
->packet_buff
);
121 batman_packet
->flags
= PRIMARIES_FIRST_HOP
;
122 batman_packet
->ttl
= TTL
;
124 vis_packet
= (struct vis_packet
*)
125 bat_priv
->my_vis_info
->skb_packet
->data
;
126 memcpy(vis_packet
->vis_orig
,
127 bat_priv
->primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
128 memcpy(vis_packet
->sender_orig
,
129 bat_priv
->primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
132 * hacky trick to make sure that we send the HNA information via
133 * our new primary interface
135 atomic_set(&bat_priv
->hna_local_changed
, 1);
138 static bool hardif_is_iface_up(struct batman_if
*batman_if
)
140 if (batman_if
->net_dev
->flags
& IFF_UP
)
146 static void update_mac_addresses(struct batman_if
*batman_if
)
148 addr_to_string(batman_if
->addr_str
, batman_if
->net_dev
->dev_addr
);
150 memcpy(((struct batman_packet
*)(batman_if
->packet_buff
))->orig
,
151 batman_if
->net_dev
->dev_addr
, ETH_ALEN
);
152 memcpy(((struct batman_packet
*)(batman_if
->packet_buff
))->prev_sender
,
153 batman_if
->net_dev
->dev_addr
, ETH_ALEN
);
156 static void check_known_mac_addr(uint8_t *addr
)
158 struct batman_if
*batman_if
;
161 list_for_each_entry_rcu(batman_if
, &if_list
, list
) {
162 if ((batman_if
->if_status
!= IF_ACTIVE
) &&
163 (batman_if
->if_status
!= IF_TO_BE_ACTIVATED
))
166 if (!compare_orig(batman_if
->net_dev
->dev_addr
, addr
))
169 pr_warning("The newly added mac address (%pM) already exists "
170 "on: %s\n", addr
, batman_if
->net_dev
->name
);
171 pr_warning("It is strongly recommended to keep mac addresses "
172 "unique to avoid problems!\n");
177 int hardif_min_mtu(struct net_device
*soft_iface
)
179 struct bat_priv
*bat_priv
= netdev_priv(soft_iface
);
180 struct batman_if
*batman_if
;
181 /* allow big frames if all devices are capable to do so
182 * (have MTU > 1500 + BAT_HEADER_LEN) */
183 int min_mtu
= ETH_DATA_LEN
;
185 if (atomic_read(&bat_priv
->frag_enabled
))
189 list_for_each_entry_rcu(batman_if
, &if_list
, list
) {
190 if ((batman_if
->if_status
!= IF_ACTIVE
) &&
191 (batman_if
->if_status
!= IF_TO_BE_ACTIVATED
))
194 if (batman_if
->soft_iface
!= soft_iface
)
197 min_mtu
= MIN(batman_if
->net_dev
->mtu
- BAT_HEADER_LEN
,
205 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
206 void update_min_mtu(struct net_device
*soft_iface
)
210 min_mtu
= hardif_min_mtu(soft_iface
);
211 if (soft_iface
->mtu
!= min_mtu
)
212 soft_iface
->mtu
= min_mtu
;
215 static void hardif_activate_interface(struct batman_if
*batman_if
)
217 struct bat_priv
*bat_priv
;
219 if (batman_if
->if_status
!= IF_INACTIVE
)
222 bat_priv
= netdev_priv(batman_if
->soft_iface
);
224 update_mac_addresses(batman_if
);
225 batman_if
->if_status
= IF_TO_BE_ACTIVATED
;
228 * the first active interface becomes our primary interface or
229 * the next active interface after the old primay interface was removed
231 if (!bat_priv
->primary_if
)
232 set_primary_if(bat_priv
, batman_if
);
234 bat_info(batman_if
->soft_iface
, "Interface activated: %s\n",
235 batman_if
->net_dev
->name
);
237 update_min_mtu(batman_if
->soft_iface
);
241 static void hardif_deactivate_interface(struct batman_if
*batman_if
)
243 if ((batman_if
->if_status
!= IF_ACTIVE
) &&
244 (batman_if
->if_status
!= IF_TO_BE_ACTIVATED
))
247 batman_if
->if_status
= IF_INACTIVE
;
249 bat_info(batman_if
->soft_iface
, "Interface deactivated: %s\n",
250 batman_if
->net_dev
->name
);
252 update_min_mtu(batman_if
->soft_iface
);
255 int hardif_enable_interface(struct batman_if
*batman_if
, char *iface_name
)
257 struct bat_priv
*bat_priv
;
258 struct batman_packet
*batman_packet
;
260 if (batman_if
->if_status
!= IF_NOT_IN_USE
)
263 batman_if
->soft_iface
= dev_get_by_name(&init_net
, iface_name
);
265 if (!batman_if
->soft_iface
) {
266 batman_if
->soft_iface
= softif_create(iface_name
);
268 if (!batman_if
->soft_iface
)
271 /* dev_get_by_name() increases the reference counter for us */
272 dev_hold(batman_if
->soft_iface
);
275 bat_priv
= netdev_priv(batman_if
->soft_iface
);
276 batman_if
->packet_len
= BAT_PACKET_LEN
;
277 batman_if
->packet_buff
= kmalloc(batman_if
->packet_len
, GFP_ATOMIC
);
279 if (!batman_if
->packet_buff
) {
280 bat_err(batman_if
->soft_iface
, "Can't add interface packet "
281 "(%s): out of memory\n", batman_if
->net_dev
->name
);
285 batman_packet
= (struct batman_packet
*)(batman_if
->packet_buff
);
286 batman_packet
->packet_type
= BAT_PACKET
;
287 batman_packet
->version
= COMPAT_VERSION
;
288 batman_packet
->flags
= 0;
289 batman_packet
->ttl
= 2;
290 batman_packet
->tq
= TQ_MAX_VALUE
;
291 batman_packet
->num_hna
= 0;
293 batman_if
->if_num
= bat_priv
->num_ifaces
;
294 bat_priv
->num_ifaces
++;
295 batman_if
->if_status
= IF_INACTIVE
;
296 orig_hash_add_if(batman_if
, bat_priv
->num_ifaces
);
298 batman_if
->batman_adv_ptype
.type
= __constant_htons(ETH_P_BATMAN
);
299 batman_if
->batman_adv_ptype
.func
= batman_skb_recv
;
300 batman_if
->batman_adv_ptype
.dev
= batman_if
->net_dev
;
301 hardif_hold(batman_if
);
302 dev_add_pack(&batman_if
->batman_adv_ptype
);
304 atomic_set(&batman_if
->seqno
, 1);
305 atomic_set(&batman_if
->frag_seqno
, 1);
306 bat_info(batman_if
->soft_iface
, "Adding interface: %s\n",
307 batman_if
->net_dev
->name
);
309 if (atomic_read(&bat_priv
->frag_enabled
) && batman_if
->net_dev
->mtu
<
310 ETH_DATA_LEN
+ BAT_HEADER_LEN
)
311 bat_info(batman_if
->soft_iface
,
312 "The MTU of interface %s is too small (%i) to handle "
313 "the transport of batman-adv packets. Packets going "
314 "over this interface will be fragmented on layer2 "
315 "which could impact the performance. Setting the MTU "
316 "to %zi would solve the problem.\n",
317 batman_if
->net_dev
->name
, batman_if
->net_dev
->mtu
,
318 ETH_DATA_LEN
+ BAT_HEADER_LEN
);
320 if (!atomic_read(&bat_priv
->frag_enabled
) && batman_if
->net_dev
->mtu
<
321 ETH_DATA_LEN
+ BAT_HEADER_LEN
)
322 bat_info(batman_if
->soft_iface
,
323 "The MTU of interface %s is too small (%i) to handle "
324 "the transport of batman-adv packets. If you experience"
325 " problems getting traffic through try increasing the "
327 batman_if
->net_dev
->name
, batman_if
->net_dev
->mtu
,
328 ETH_DATA_LEN
+ BAT_HEADER_LEN
);
330 if (hardif_is_iface_up(batman_if
))
331 hardif_activate_interface(batman_if
);
333 bat_err(batman_if
->soft_iface
, "Not using interface %s "
334 "(retrying later): interface not active\n",
335 batman_if
->net_dev
->name
);
337 /* begin scheduling originator messages on that interface */
338 schedule_own_packet(batman_if
);
347 void hardif_disable_interface(struct batman_if
*batman_if
)
349 struct bat_priv
*bat_priv
= netdev_priv(batman_if
->soft_iface
);
351 if (batman_if
->if_status
== IF_ACTIVE
)
352 hardif_deactivate_interface(batman_if
);
354 if (batman_if
->if_status
!= IF_INACTIVE
)
357 bat_info(batman_if
->soft_iface
, "Removing interface: %s\n",
358 batman_if
->net_dev
->name
);
359 dev_remove_pack(&batman_if
->batman_adv_ptype
);
360 hardif_put(batman_if
);
362 bat_priv
->num_ifaces
--;
363 orig_hash_del_if(batman_if
, bat_priv
->num_ifaces
);
365 if (batman_if
== bat_priv
->primary_if
) {
366 struct batman_if
*new_if
;
368 new_if
= get_active_batman_if(batman_if
->soft_iface
);
369 set_primary_if(bat_priv
, new_if
);
375 kfree(batman_if
->packet_buff
);
376 batman_if
->packet_buff
= NULL
;
377 batman_if
->if_status
= IF_NOT_IN_USE
;
379 /* delete all references to this batman_if */
380 purge_orig_ref(bat_priv
);
381 purge_outstanding_packets(bat_priv
, batman_if
);
382 dev_put(batman_if
->soft_iface
);
384 /* nobody uses this interface anymore */
385 if (!bat_priv
->num_ifaces
)
386 softif_destroy(batman_if
->soft_iface
);
388 batman_if
->soft_iface
= NULL
;
391 static struct batman_if
*hardif_add_interface(struct net_device
*net_dev
)
393 struct batman_if
*batman_if
;
396 ret
= is_valid_iface(net_dev
);
402 batman_if
= kmalloc(sizeof(struct batman_if
), GFP_ATOMIC
);
404 pr_err("Can't add interface (%s): out of memory\n",
409 ret
= sysfs_add_hardif(&batman_if
->hardif_obj
, net_dev
);
413 batman_if
->if_num
= -1;
414 batman_if
->net_dev
= net_dev
;
415 batman_if
->soft_iface
= NULL
;
416 batman_if
->if_status
= IF_NOT_IN_USE
;
417 INIT_LIST_HEAD(&batman_if
->list
);
418 atomic_set(&batman_if
->refcnt
, 0);
419 hardif_hold(batman_if
);
421 check_known_mac_addr(batman_if
->net_dev
->dev_addr
);
423 spin_lock(&if_list_lock
);
424 list_add_tail_rcu(&batman_if
->list
, &if_list
);
425 spin_unlock(&if_list_lock
);
427 /* extra reference for return */
428 hardif_hold(batman_if
);
439 static void hardif_remove_interface(struct batman_if
*batman_if
)
441 /* first deactivate interface */
442 if (batman_if
->if_status
!= IF_NOT_IN_USE
)
443 hardif_disable_interface(batman_if
);
445 if (batman_if
->if_status
!= IF_NOT_IN_USE
)
448 batman_if
->if_status
= IF_TO_BE_REMOVED
;
450 /* caller must take if_list_lock */
451 list_del_rcu(&batman_if
->list
);
453 sysfs_del_hardif(&batman_if
->hardif_obj
);
454 hardif_put(batman_if
);
457 void hardif_remove_interfaces(void)
459 struct batman_if
*batman_if
, *batman_if_tmp
;
462 spin_lock(&if_list_lock
);
463 list_for_each_entry_safe(batman_if
, batman_if_tmp
, &if_list
, list
) {
464 hardif_remove_interface(batman_if
);
466 spin_unlock(&if_list_lock
);
470 static int hard_if_event(struct notifier_block
*this,
471 unsigned long event
, void *ptr
)
473 struct net_device
*net_dev
= (struct net_device
*)ptr
;
474 struct batman_if
*batman_if
= get_batman_if_by_netdev(net_dev
);
475 struct bat_priv
*bat_priv
;
477 if (!batman_if
&& event
== NETDEV_REGISTER
)
478 batman_if
= hardif_add_interface(net_dev
);
485 hardif_activate_interface(batman_if
);
487 case NETDEV_GOING_DOWN
:
489 hardif_deactivate_interface(batman_if
);
491 case NETDEV_UNREGISTER
:
492 spin_lock(&if_list_lock
);
493 hardif_remove_interface(batman_if
);
494 spin_unlock(&if_list_lock
);
496 case NETDEV_CHANGEMTU
:
497 if (batman_if
->soft_iface
)
498 update_min_mtu(batman_if
->soft_iface
);
500 case NETDEV_CHANGEADDR
:
501 if (batman_if
->if_status
== IF_NOT_IN_USE
) {
502 hardif_put(batman_if
);
506 check_known_mac_addr(batman_if
->net_dev
->dev_addr
);
507 update_mac_addresses(batman_if
);
509 bat_priv
= netdev_priv(batman_if
->soft_iface
);
510 if (batman_if
== bat_priv
->primary_if
)
511 set_primary_if(bat_priv
, batman_if
);
516 hardif_put(batman_if
);
522 /* receive a packet with the batman ethertype coming on a hard
524 int batman_skb_recv(struct sk_buff
*skb
, struct net_device
*dev
,
525 struct packet_type
*ptype
, struct net_device
*orig_dev
)
527 struct bat_priv
*bat_priv
;
528 struct batman_packet
*batman_packet
;
529 struct batman_if
*batman_if
;
532 batman_if
= container_of(ptype
, struct batman_if
, batman_adv_ptype
);
533 skb
= skb_share_check(skb
, GFP_ATOMIC
);
535 /* skb was released by skb_share_check() */
539 /* packet should hold at least type and version */
540 if (unlikely(!pskb_may_pull(skb
, 2)))
543 /* expect a valid ethernet header here. */
544 if (unlikely(skb
->mac_len
!= sizeof(struct ethhdr
)
545 || !skb_mac_header(skb
)))
548 if (!batman_if
->soft_iface
)
551 bat_priv
= netdev_priv(batman_if
->soft_iface
);
553 if (atomic_read(&bat_priv
->mesh_state
) != MESH_ACTIVE
)
556 /* discard frames on not active interfaces */
557 if (batman_if
->if_status
!= IF_ACTIVE
)
560 batman_packet
= (struct batman_packet
*)skb
->data
;
562 if (batman_packet
->version
!= COMPAT_VERSION
) {
563 bat_dbg(DBG_BATMAN
, bat_priv
,
564 "Drop packet: incompatible batman version (%i)\n",
565 batman_packet
->version
);
569 /* all receive handlers return whether they received or reused
570 * the supplied skb. if not, we have to free the skb. */
572 switch (batman_packet
->packet_type
) {
573 /* batman originator packet */
575 ret
= recv_bat_packet(skb
, batman_if
);
578 /* batman icmp packet */
580 ret
= recv_icmp_packet(skb
, batman_if
);
585 ret
= recv_unicast_packet(skb
, batman_if
);
588 /* fragmented unicast packet */
589 case BAT_UNICAST_FRAG
:
590 ret
= recv_ucast_frag_packet(skb
, batman_if
);
593 /* broadcast packet */
595 ret
= recv_bcast_packet(skb
, batman_if
);
600 ret
= recv_vis_packet(skb
, batman_if
);
606 if (ret
== NET_RX_DROP
)
609 /* return NET_RX_SUCCESS in any case as we
610 * most probably dropped the packet for
611 * routing-logical reasons. */
613 return NET_RX_SUCCESS
;
621 struct notifier_block hard_if_notifier
= {
622 .notifier_call
= hard_if_event
,