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 struct batman_if
*get_batman_if_by_netdev(struct net_device
*net_dev
)
38 struct batman_if
*batman_if
;
41 list_for_each_entry_rcu(batman_if
, &if_list
, list
) {
42 if (batman_if
->net_dev
== net_dev
)
53 static int is_valid_iface(struct net_device
*net_dev
)
55 if (net_dev
->flags
& IFF_LOOPBACK
)
58 if (net_dev
->type
!= ARPHRD_ETHER
)
61 if (net_dev
->addr_len
!= ETH_ALEN
)
64 /* no batman over batman */
65 #ifdef HAVE_NET_DEVICE_OPS
66 if (net_dev
->netdev_ops
->ndo_start_xmit
== interface_tx
)
69 if (net_dev
->hard_start_xmit
== interface_tx
)
73 /* Device is being bridged */
74 /* if (net_dev->priv_flags & IFF_BRIDGE_PORT)
80 static struct batman_if
*get_active_batman_if(void)
82 struct batman_if
*batman_if
;
84 /* TODO: should check interfaces belonging to bat_priv */
86 list_for_each_entry_rcu(batman_if
, &if_list
, list
) {
87 if (batman_if
->if_status
== IF_ACTIVE
)
98 static void set_primary_if(struct bat_priv
*bat_priv
,
99 struct batman_if
*batman_if
)
101 struct batman_packet
*batman_packet
;
103 bat_priv
->primary_if
= batman_if
;
105 if (!bat_priv
->primary_if
)
108 set_main_if_addr(batman_if
->net_dev
->dev_addr
);
110 batman_packet
= (struct batman_packet
*)(batman_if
->packet_buff
);
111 batman_packet
->flags
= PRIMARIES_FIRST_HOP
;
112 batman_packet
->ttl
= TTL
;
115 * hacky trick to make sure that we send the HNA information via
116 * our new primary interface
118 atomic_set(&hna_local_changed
, 1);
121 static bool hardif_is_iface_up(struct batman_if
*batman_if
)
123 if (batman_if
->net_dev
->flags
& IFF_UP
)
129 static void update_mac_addresses(struct batman_if
*batman_if
)
131 if (!batman_if
|| !batman_if
->packet_buff
)
134 addr_to_string(batman_if
->addr_str
, batman_if
->net_dev
->dev_addr
);
136 memcpy(((struct batman_packet
*)(batman_if
->packet_buff
))->orig
,
137 batman_if
->net_dev
->dev_addr
, ETH_ALEN
);
138 memcpy(((struct batman_packet
*)(batman_if
->packet_buff
))->prev_sender
,
139 batman_if
->net_dev
->dev_addr
, ETH_ALEN
);
142 static void check_known_mac_addr(uint8_t *addr
)
144 struct batman_if
*batman_if
;
147 list_for_each_entry_rcu(batman_if
, &if_list
, list
) {
148 if ((batman_if
->if_status
!= IF_ACTIVE
) &&
149 (batman_if
->if_status
!= IF_TO_BE_ACTIVATED
))
152 if (!compare_orig(batman_if
->net_dev
->dev_addr
, addr
))
155 pr_warning("The newly added mac address (%pM) already exists "
156 "on: %s\n", addr
, batman_if
->dev
);
157 pr_warning("It is strongly recommended to keep mac addresses "
158 "unique to avoid problems!\n");
163 int hardif_min_mtu(void)
165 struct batman_if
*batman_if
;
166 /* allow big frames if all devices are capable to do so
167 * (have MTU > 1500 + BAT_HEADER_LEN) */
168 int min_mtu
= ETH_DATA_LEN
;
169 /* FIXME: each batman_if will be attached to a softif */
170 struct bat_priv
*bat_priv
= netdev_priv(soft_device
);
172 if (atomic_read(&bat_priv
->frag_enabled
))
176 list_for_each_entry_rcu(batman_if
, &if_list
, list
) {
177 if ((batman_if
->if_status
== IF_ACTIVE
) ||
178 (batman_if
->if_status
== IF_TO_BE_ACTIVATED
))
179 min_mtu
= MIN(batman_if
->net_dev
->mtu
- BAT_HEADER_LEN
,
187 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
188 void update_min_mtu(void)
192 min_mtu
= hardif_min_mtu();
193 if (soft_device
->mtu
!= min_mtu
)
194 soft_device
->mtu
= min_mtu
;
197 static void hardif_activate_interface(struct net_device
*net_dev
,
198 struct bat_priv
*bat_priv
,
199 struct batman_if
*batman_if
)
201 if (batman_if
->if_status
!= IF_INACTIVE
)
204 update_mac_addresses(batman_if
);
205 batman_if
->if_status
= IF_TO_BE_ACTIVATED
;
208 * the first active interface becomes our primary interface or
209 * the next active interface after the old primay interface was removed
211 if (!bat_priv
->primary_if
)
212 set_primary_if(bat_priv
, batman_if
);
214 bat_info(net_dev
, "Interface activated: %s\n", batman_if
->dev
);
216 if (atomic_read(&module_state
) == MODULE_INACTIVE
)
223 static void hardif_deactivate_interface(struct net_device
*net_dev
,
224 struct batman_if
*batman_if
)
226 if ((batman_if
->if_status
!= IF_ACTIVE
) &&
227 (batman_if
->if_status
!= IF_TO_BE_ACTIVATED
))
230 batman_if
->if_status
= IF_INACTIVE
;
232 bat_info(net_dev
, "Interface deactivated: %s\n", batman_if
->dev
);
237 int hardif_enable_interface(struct batman_if
*batman_if
)
239 /* FIXME: each batman_if will be attached to a softif */
240 struct bat_priv
*bat_priv
= netdev_priv(soft_device
);
241 struct batman_packet
*batman_packet
;
243 if (batman_if
->if_status
!= IF_NOT_IN_USE
)
246 batman_if
->packet_len
= BAT_PACKET_LEN
;
247 batman_if
->packet_buff
= kmalloc(batman_if
->packet_len
, GFP_ATOMIC
);
249 if (!batman_if
->packet_buff
) {
250 bat_err(soft_device
, "Can't add interface packet (%s): "
251 "out of memory\n", batman_if
->dev
);
255 batman_packet
= (struct batman_packet
*)(batman_if
->packet_buff
);
256 batman_packet
->packet_type
= BAT_PACKET
;
257 batman_packet
->version
= COMPAT_VERSION
;
258 batman_packet
->flags
= 0;
259 batman_packet
->ttl
= 2;
260 batman_packet
->tq
= TQ_MAX_VALUE
;
261 batman_packet
->num_hna
= 0;
263 batman_if
->if_num
= bat_priv
->num_ifaces
;
264 bat_priv
->num_ifaces
++;
265 batman_if
->if_status
= IF_INACTIVE
;
266 orig_hash_add_if(batman_if
, bat_priv
->num_ifaces
);
268 batman_if
->batman_adv_ptype
.type
= __constant_htons(ETH_P_BATMAN
);
269 batman_if
->batman_adv_ptype
.func
= batman_skb_recv
;
270 batman_if
->batman_adv_ptype
.dev
= batman_if
->net_dev
;
271 dev_add_pack(&batman_if
->batman_adv_ptype
);
273 atomic_set(&batman_if
->seqno
, 1);
274 atomic_set(&batman_if
->frag_seqno
, 1);
275 bat_info(soft_device
, "Adding interface: %s\n", batman_if
->dev
);
277 if (atomic_read(&bat_priv
->frag_enabled
) && batman_if
->net_dev
->mtu
<
278 ETH_DATA_LEN
+ BAT_HEADER_LEN
)
279 bat_info(soft_device
,
280 "The MTU of interface %s is too small (%i) to handle "
281 "the transport of batman-adv packets. Packets going "
282 "over this interface will be fragmented on layer2 "
283 "which could impact the performance. Setting the MTU "
284 "to %zi would solve the problem.\n",
285 batman_if
->dev
, batman_if
->net_dev
->mtu
,
286 ETH_DATA_LEN
+ BAT_HEADER_LEN
);
288 if (!atomic_read(&bat_priv
->frag_enabled
) && batman_if
->net_dev
->mtu
<
289 ETH_DATA_LEN
+ BAT_HEADER_LEN
)
290 bat_info(soft_device
,
291 "The MTU of interface %s is too small (%i) to handle "
292 "the transport of batman-adv packets. If you experience"
293 " problems getting traffic through try increasing the "
295 batman_if
->dev
, batman_if
->net_dev
->mtu
,
296 ETH_DATA_LEN
+ BAT_HEADER_LEN
);
298 if (hardif_is_iface_up(batman_if
))
299 hardif_activate_interface(soft_device
, bat_priv
, batman_if
);
301 bat_err(soft_device
, "Not using interface %s "
302 "(retrying later): interface not active\n",
305 /* begin scheduling originator messages on that interface */
306 schedule_own_packet(batman_if
);
315 void hardif_disable_interface(struct batman_if
*batman_if
)
317 /* FIXME: each batman_if will be attached to a softif */
318 struct bat_priv
*bat_priv
= netdev_priv(soft_device
);
320 if (batman_if
->if_status
== IF_ACTIVE
)
321 hardif_deactivate_interface(soft_device
, batman_if
);
323 if (batman_if
->if_status
!= IF_INACTIVE
)
326 bat_info(soft_device
, "Removing interface: %s\n", batman_if
->dev
);
327 dev_remove_pack(&batman_if
->batman_adv_ptype
);
329 bat_priv
->num_ifaces
--;
330 orig_hash_del_if(batman_if
, bat_priv
->num_ifaces
);
332 if (batman_if
== bat_priv
->primary_if
)
333 set_primary_if(bat_priv
, get_active_batman_if());
335 kfree(batman_if
->packet_buff
);
336 batman_if
->packet_buff
= NULL
;
337 batman_if
->if_status
= IF_NOT_IN_USE
;
339 if ((atomic_read(&module_state
) == MODULE_ACTIVE
) &&
340 (bat_priv
->num_ifaces
== 0))
344 static struct batman_if
*hardif_add_interface(struct net_device
*net_dev
)
346 struct batman_if
*batman_if
;
349 ret
= is_valid_iface(net_dev
);
355 batman_if
= kmalloc(sizeof(struct batman_if
), GFP_ATOMIC
);
357 pr_err("Can't add interface (%s): out of memory\n",
362 batman_if
->dev
= kstrdup(net_dev
->name
, GFP_ATOMIC
);
366 ret
= sysfs_add_hardif(&batman_if
->hardif_obj
, net_dev
);
370 batman_if
->if_num
= -1;
371 batman_if
->net_dev
= net_dev
;
372 batman_if
->if_status
= IF_NOT_IN_USE
;
373 batman_if
->packet_buff
= NULL
;
374 INIT_LIST_HEAD(&batman_if
->list
);
376 check_known_mac_addr(batman_if
->net_dev
->dev_addr
);
377 list_add_tail_rcu(&batman_if
->list
, &if_list
);
381 kfree(batman_if
->dev
);
390 static void hardif_free_interface(struct rcu_head
*rcu
)
392 struct batman_if
*batman_if
= container_of(rcu
, struct batman_if
, rcu
);
394 /* delete all references to this batman_if */
396 purge_outstanding_packets(batman_if
);
398 kfree(batman_if
->dev
);
402 static void hardif_remove_interface(struct batman_if
*batman_if
)
404 /* first deactivate interface */
405 if (batman_if
->if_status
!= IF_NOT_IN_USE
)
406 hardif_disable_interface(batman_if
);
408 if (batman_if
->if_status
!= IF_NOT_IN_USE
)
411 batman_if
->if_status
= IF_TO_BE_REMOVED
;
412 list_del_rcu(&batman_if
->list
);
413 sysfs_del_hardif(&batman_if
->hardif_obj
);
414 dev_put(batman_if
->net_dev
);
415 call_rcu(&batman_if
->rcu
, hardif_free_interface
);
418 void hardif_remove_interfaces(void)
420 struct batman_if
*batman_if
, *batman_if_tmp
;
422 list_for_each_entry_safe(batman_if
, batman_if_tmp
, &if_list
, list
)
423 hardif_remove_interface(batman_if
);
426 static int hard_if_event(struct notifier_block
*this,
427 unsigned long event
, void *ptr
)
429 struct net_device
*net_dev
= (struct net_device
*)ptr
;
430 struct batman_if
*batman_if
= get_batman_if_by_netdev(net_dev
);
431 /* FIXME: each batman_if will be attached to a softif */
432 struct bat_priv
*bat_priv
= netdev_priv(soft_device
);
434 if (!batman_if
&& event
== NETDEV_REGISTER
)
435 batman_if
= hardif_add_interface(net_dev
);
442 hardif_activate_interface(soft_device
, bat_priv
, batman_if
);
444 case NETDEV_GOING_DOWN
:
446 hardif_deactivate_interface(soft_device
, batman_if
);
448 case NETDEV_UNREGISTER
:
449 hardif_remove_interface(batman_if
);
451 case NETDEV_CHANGENAME
:
453 case NETDEV_CHANGEADDR
:
454 check_known_mac_addr(batman_if
->net_dev
->dev_addr
);
455 update_mac_addresses(batman_if
);
456 if (batman_if
== bat_priv
->primary_if
)
457 set_primary_if(bat_priv
, batman_if
);
467 /* receive a packet with the batman ethertype coming on a hard
469 int batman_skb_recv(struct sk_buff
*skb
, struct net_device
*dev
,
470 struct packet_type
*ptype
, struct net_device
*orig_dev
)
472 /* FIXME: each orig_node->batman_if will be attached to a softif */
473 struct bat_priv
*bat_priv
= netdev_priv(soft_device
);
474 struct batman_packet
*batman_packet
;
475 struct batman_if
*batman_if
;
478 batman_if
= container_of(ptype
, struct batman_if
, batman_adv_ptype
);
479 skb
= skb_share_check(skb
, GFP_ATOMIC
);
481 /* skb was released by skb_share_check() */
485 if (atomic_read(&module_state
) != MODULE_ACTIVE
)
488 /* packet should hold at least type and version */
489 if (unlikely(skb_headlen(skb
) < 2))
492 /* expect a valid ethernet header here. */
493 if (unlikely(skb
->mac_len
!= sizeof(struct ethhdr
)
494 || !skb_mac_header(skb
)))
497 /* discard frames on not active interfaces */
498 if (batman_if
->if_status
!= IF_ACTIVE
)
501 batman_packet
= (struct batman_packet
*)skb
->data
;
503 if (batman_packet
->version
!= COMPAT_VERSION
) {
504 bat_dbg(DBG_BATMAN
, bat_priv
,
505 "Drop packet: incompatible batman version (%i)\n",
506 batman_packet
->version
);
510 /* all receive handlers return whether they received or reused
511 * the supplied skb. if not, we have to free the skb. */
513 switch (batman_packet
->packet_type
) {
514 /* batman originator packet */
516 ret
= recv_bat_packet(skb
, batman_if
);
519 /* batman icmp packet */
521 ret
= recv_icmp_packet(skb
);
526 ret
= recv_unicast_packet(skb
, batman_if
);
529 /* fragmented unicast packet */
530 case BAT_UNICAST_FRAG
:
531 ret
= recv_ucast_frag_packet(skb
, batman_if
);
534 /* broadcast packet */
536 ret
= recv_bcast_packet(skb
);
541 ret
= recv_vis_packet(skb
);
547 if (ret
== NET_RX_DROP
)
550 /* return NET_RX_SUCCESS in any case as we
551 * most probably dropped the packet for
552 * routing-logical reasons. */
554 return NET_RX_SUCCESS
;
562 struct notifier_block hard_if_notifier
= {
563 .notifier_call
= hard_if_event
,