2 * Copyright (C) 2007-2009 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"
30 #define MIN(x, y) ((x) < (y) ? (x) : (y))
32 static char avail_ifs
;
33 static char active_ifs
;
35 static void hardif_free_interface(struct rcu_head
*rcu
);
37 static struct batman_if
*get_batman_if_by_name(char *name
)
39 struct batman_if
*batman_if
;
42 list_for_each_entry_rcu(batman_if
, &if_list
, list
) {
43 if (strncmp(batman_if
->dev
, name
, IFNAMSIZ
) == 0)
54 int hardif_min_mtu(void)
56 struct batman_if
*batman_if
;
57 /* allow big frames if all devices are capable to do so
58 * (have MTU > 1500 + BAT_HEADER_LEN) */
59 int min_mtu
= ETH_DATA_LEN
;
62 list_for_each_entry_rcu(batman_if
, &if_list
, list
) {
63 if ((batman_if
->if_active
== IF_ACTIVE
) ||
64 (batman_if
->if_active
== IF_TO_BE_ACTIVATED
))
65 min_mtu
= MIN(batman_if
->net_dev
->mtu
- BAT_HEADER_LEN
,
73 static void check_known_mac_addr(uint8_t *addr
)
75 struct batman_if
*batman_if
;
78 list_for_each_entry_rcu(batman_if
, &if_list
, list
) {
79 if ((batman_if
->if_active
!= IF_ACTIVE
) &&
80 (batman_if
->if_active
!= IF_TO_BE_ACTIVATED
))
83 if (!compare_orig(batman_if
->net_dev
->dev_addr
, addr
))
86 printk(KERN_WARNING
"batman-adv:The newly added mac address (%pM) already exists on: %s\n",
87 addr
, batman_if
->dev
);
88 printk(KERN_WARNING
"batman-adv:It is strongly recommended to keep mac addresses unique to avoid problems!\n");
93 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
94 void update_min_mtu(void)
98 min_mtu
= hardif_min_mtu();
99 if (soft_device
->mtu
!= min_mtu
)
100 soft_device
->mtu
= min_mtu
;
103 /* checks if the interface is up. (returns 1 if it is) */
104 static int hardif_is_interface_up(char *dev
)
106 struct net_device
*net_dev
;
109 * if we already have an interface in our interface list and
110 * the current interface is not the primary interface and
111 * the primary interface is not up and
112 * the primary interface has never been up - don't activate any
113 * secondary interface !
117 if ((!list_empty(&if_list
)) &&
118 strncmp(((struct batman_if
*)if_list
.next
)->dev
, dev
, IFNAMSIZ
) &&
119 !(((struct batman_if
*)if_list
.next
)->if_active
== IF_ACTIVE
) &&
120 !(((struct batman_if
*)if_list
.next
)->if_active
== IF_TO_BE_ACTIVATED
) &&
121 (!main_if_was_up())) {
127 #ifdef __NET_NET_NAMESPACE_H
128 net_dev
= dev_get_by_name(&init_net
, dev
);
130 net_dev
= dev_get_by_name(dev
);
135 if (!(net_dev
->flags
& IFF_UP
))
147 /* deactivates the interface. */
148 void hardif_deactivate_interface(struct batman_if
*batman_if
)
150 if (batman_if
->if_active
!= IF_ACTIVE
)
154 * batman_if->net_dev has been acquired by dev_get_by_name() in
155 * proc_interfaces_write() and has to be unreferenced.
158 if (batman_if
->net_dev
)
159 dev_put(batman_if
->net_dev
);
161 batman_if
->if_active
= IF_INACTIVE
;
164 printk(KERN_INFO
"batman-adv:Interface deactivated: %s\n",
168 /* (re)activate given interface. */
169 static void hardif_activate_interface(struct batman_if
*batman_if
)
171 if (batman_if
->if_active
!= IF_INACTIVE
)
174 #ifdef __NET_NET_NAMESPACE_H
175 batman_if
->net_dev
= dev_get_by_name(&init_net
, batman_if
->dev
);
177 batman_if
->net_dev
= dev_get_by_name(batman_if
->dev
);
179 if (!batman_if
->net_dev
)
182 check_known_mac_addr(batman_if
->net_dev
->dev_addr
);
184 addr_to_string(batman_if
->addr_str
, batman_if
->net_dev
->dev_addr
);
186 memcpy(((struct batman_packet
*)(batman_if
->packet_buff
))->orig
,
187 batman_if
->net_dev
->dev_addr
, ETH_ALEN
);
188 memcpy(((struct batman_packet
*)(batman_if
->packet_buff
))->prev_sender
,
189 batman_if
->net_dev
->dev_addr
, ETH_ALEN
);
191 batman_if
->if_active
= IF_TO_BE_ACTIVATED
;
194 /* save the mac address if it is our primary interface */
195 if (batman_if
->if_num
== 0)
196 set_main_if_addr(batman_if
->net_dev
->dev_addr
);
198 printk(KERN_INFO
"batman-adv:Interface activated: %s\n",
204 batman_if
->net_dev
= NULL
;
207 static void hardif_free_interface(struct rcu_head
*rcu
)
209 struct batman_if
*batman_if
= container_of(rcu
, struct batman_if
, rcu
);
211 kfree(batman_if
->packet_buff
);
212 kfree(batman_if
->dev
);
218 * - echo '' > /proc/.../interfaces
219 * - modprobe -r batman-adv-core
221 /* removes and frees all interfaces */
222 void hardif_remove_interfaces(void)
224 struct batman_if
*batman_if
= NULL
;
228 /* no lock needed - we don't delete somewhere else */
229 list_for_each_entry(batman_if
, &if_list
, list
) {
231 list_del_rcu(&batman_if
->list
);
233 /* first deactivate interface */
234 if (batman_if
->if_active
!= IF_INACTIVE
)
235 hardif_deactivate_interface(batman_if
);
237 call_rcu(&batman_if
->rcu
, hardif_free_interface
);
241 static int resize_orig(struct orig_node
*orig_node
, int if_num
)
245 data_ptr
= kmalloc((if_num
+ 1) * sizeof(TYPE_OF_WORD
) * NUM_WORDS
,
248 printk(KERN_ERR
"batman-adv:Can't resize orig: out of memory\n");
252 memcpy(data_ptr
, orig_node
->bcast_own
,
253 if_num
* sizeof(TYPE_OF_WORD
) * NUM_WORDS
);
254 kfree(orig_node
->bcast_own
);
255 orig_node
->bcast_own
= data_ptr
;
257 data_ptr
= kmalloc((if_num
+ 1) * sizeof(uint8_t), GFP_ATOMIC
);
259 printk(KERN_ERR
"batman-adv:Can't resize orig: out of memory\n");
263 memcpy(data_ptr
, orig_node
->bcast_own_sum
, if_num
* sizeof(uint8_t));
264 kfree(orig_node
->bcast_own_sum
);
265 orig_node
->bcast_own_sum
= data_ptr
;
271 /* adds an interface the interface list and activate it, if possible */
272 int hardif_add_interface(char *dev
, int if_num
)
274 struct batman_if
*batman_if
;
275 struct batman_packet
*batman_packet
;
276 struct orig_node
*orig_node
;
280 batman_if
= kmalloc(sizeof(struct batman_if
), GFP_KERNEL
);
283 printk(KERN_ERR
"batman-adv:Can't add interface (%s): out of memory\n", dev
);
287 batman_if
->net_dev
= NULL
;
289 if ((if_num
== 0) && (num_hna
> 0))
290 batman_if
->packet_len
= BAT_PACKET_LEN
+ num_hna
* ETH_ALEN
;
292 batman_if
->packet_len
= BAT_PACKET_LEN
;
294 batman_if
->packet_buff
= kmalloc(batman_if
->packet_len
, GFP_KERNEL
);
296 if (!batman_if
->packet_buff
) {
297 printk(KERN_ERR
"batman-adv:Can't add interface packet (%s): out of memory\n", dev
);
301 batman_if
->if_num
= if_num
;
302 batman_if
->dev
= dev
;
303 batman_if
->if_active
= IF_INACTIVE
;
304 INIT_RCU_HEAD(&batman_if
->rcu
);
306 printk(KERN_INFO
"batman-adv:Adding interface: %s\n", dev
);
309 INIT_LIST_HEAD(&batman_if
->list
);
311 batman_packet
= (struct batman_packet
*)(batman_if
->packet_buff
);
312 batman_packet
->packet_type
= BAT_PACKET
;
313 batman_packet
->version
= COMPAT_VERSION
;
314 batman_packet
->flags
= 0x00;
315 batman_packet
->ttl
= (batman_if
->if_num
> 0 ? 2 : TTL
);
316 batman_packet
->flags
= 0;
317 batman_packet
->tq
= TQ_MAX_VALUE
;
318 batman_packet
->num_hna
= 0;
320 if (batman_if
->packet_len
!= BAT_PACKET_LEN
) {
321 unsigned char *hna_buff
;
324 hna_buff
= batman_if
->packet_buff
+ BAT_PACKET_LEN
;
325 hna_len
= batman_if
->packet_len
- BAT_PACKET_LEN
;
326 batman_packet
->num_hna
= hna_local_fill_buffer(hna_buff
,
330 atomic_set(&batman_if
->seqno
, 1);
332 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
334 spin_lock_irqsave(&orig_hash_lock
, flags
);
336 while (hash_iterate(orig_hash
, &hashit
)) {
337 orig_node
= hashit
.bucket
->data
;
338 if (resize_orig(orig_node
, if_num
) == -1) {
339 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
344 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
346 if (!hardif_is_interface_up(batman_if
->dev
))
347 printk(KERN_ERR
"batman-adv:Not using interface %s (retrying later): interface not active\n", batman_if
->dev
);
349 hardif_activate_interface(batman_if
);
351 list_add_tail_rcu(&batman_if
->list
, &if_list
);
353 /* begin sending originator messages on that interface */
354 schedule_own_packet(batman_if
);
358 kfree(batman_if
->packet_buff
);
364 char hardif_get_active_if_num(void)
369 static int hard_if_event(struct notifier_block
*this,
370 unsigned long event
, void *ptr
)
372 struct net_device
*dev
= (struct net_device
*)ptr
;
373 struct batman_if
*batman_if
= get_batman_if_by_name(dev
->name
);
379 case NETDEV_GOING_DOWN
:
381 case NETDEV_UNREGISTER
:
382 hardif_deactivate_interface(batman_if
);
385 hardif_activate_interface(batman_if
);
386 if ((atomic_read(&module_state
) == MODULE_INACTIVE
) &&
387 (hardif_get_active_if_num() > 0)) {
391 /* NETDEV_CHANGEADDR - mac address change - what are we doing here ? */
402 /* find batman interface by netdev. assumes rcu_read_lock on */
403 static struct batman_if
*find_batman_if(struct net_device
*dev
)
405 struct batman_if
*batman_if
;
408 list_for_each_entry_rcu(batman_if
, &if_list
, list
) {
409 if (batman_if
->net_dev
== dev
) {
419 /* receive a packet with the batman ethertype coming on a hard
421 int batman_skb_recv(struct sk_buff
*skb
, struct net_device
*dev
,
422 struct packet_type
*ptype
, struct net_device
*orig_dev
)
424 struct batman_packet
*batman_packet
;
425 struct batman_if
*batman_if
;
426 struct net_device_stats
*stats
;
429 skb
= skb_share_check(skb
, GFP_ATOMIC
);
431 /* skb was released by skb_share_check() */
435 if (atomic_read(&module_state
) != MODULE_ACTIVE
)
438 /* packet should hold at least type and version */
439 if (unlikely(skb_headlen(skb
) < 2))
442 /* expect a valid ethernet header here. */
443 if (unlikely(skb
->mac_len
!= sizeof(struct ethhdr
)
444 || !skb_mac_header(skb
)))
447 batman_if
= find_batman_if(skb
->dev
);
451 /* discard frames on not active interfaces */
452 if (batman_if
->if_active
!= IF_ACTIVE
)
455 stats
= (struct net_device_stats
*)dev_get_stats(skb
->dev
);
458 stats
->rx_bytes
+= skb
->len
;
461 batman_packet
= (struct batman_packet
*)skb
->data
;
463 if (batman_packet
->version
!= COMPAT_VERSION
) {
465 "Drop packet: incompatible batman version (%i)\n",
466 batman_packet
->version
);
470 /* all receive handlers return whether they received or reused
471 * the supplied skb. if not, we have to free the skb. */
473 switch (batman_packet
->packet_type
) {
474 /* batman originator packet */
476 ret
= recv_bat_packet(skb
, batman_if
);
479 /* batman icmp packet */
481 ret
= recv_icmp_packet(skb
);
486 ret
= recv_unicast_packet(skb
);
489 /* broadcast packet */
491 ret
= recv_bcast_packet(skb
);
496 ret
= recv_vis_packet(skb
);
502 if (ret
== NET_RX_DROP
)
505 /* return NET_RX_SUCCESS in any case as we
506 * most probably dropped the packet for
507 * routing-logical reasons. */
509 return NET_RX_SUCCESS
;
518 struct notifier_block hard_if_notifier
= {
519 .notifier_call
= hard_if_event
,