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"
31 #define MIN(x, y) ((x) < (y) ? (x) : (y))
33 static char avail_ifs;
34 static char active_ifs;
36 static void hardif_free_interface(struct rcu_head *rcu);
38 static struct batman_if *get_batman_if_by_name(char *name)
40 struct batman_if *batman_if;
43 list_for_each_entry_rcu(batman_if, &if_list, list) {
44 if (strncmp(batman_if->dev, name, IFNAMSIZ) == 0)
55 int hardif_min_mtu(void)
57 struct batman_if *batman_if;
58 /* allow big frames if all devices are capable to do so
59 * (have MTU > 1500 + BAT_HEADER_LEN) */
60 int min_mtu = ETH_DATA_LEN;
63 list_for_each_entry_rcu(batman_if, &if_list, list) {
64 if ((batman_if->if_active == IF_ACTIVE) ||
65 (batman_if->if_active == IF_TO_BE_ACTIVATED))
66 min_mtu = MIN(batman_if->net_dev->mtu - BAT_HEADER_LEN,
74 static void check_known_mac_addr(uint8_t *addr)
76 struct batman_if *batman_if;
77 char mac_string[ETH_STR_LEN];
80 list_for_each_entry_rcu(batman_if, &if_list, list) {
81 if ((batman_if->if_active != IF_ACTIVE) &&
82 (batman_if->if_active != IF_TO_BE_ACTIVATED))
85 if (!compare_orig(batman_if->net_dev->dev_addr, addr))
88 addr_to_string(mac_string, addr);
89 printk(KERN_WARNING "batman-adv:The newly added mac address (%s) already exists on: %s\n",
90 mac_string, batman_if->dev);
91 printk(KERN_WARNING "batman-adv:It is strongly recommended to keep mac addresses unique to avoid problems!\n");
96 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
97 void update_min_mtu(void)
101 min_mtu = hardif_min_mtu();
102 if (soft_device->mtu != min_mtu)
103 soft_device->mtu = min_mtu;
106 /* checks if the interface is up. (returns 1 if it is) */
107 static int hardif_is_interface_up(char *dev)
109 struct net_device *net_dev;
112 * if we already have an interface in our interface list and
113 * the current interface is not the primary interface and
114 * the primary interface is not up and
115 * the primary interface has never been up - don't activate any
116 * secondary interface !
120 if ((!list_empty(&if_list)) &&
121 strncmp(((struct batman_if *)if_list.next)->dev, dev, IFNAMSIZ) &&
122 !(((struct batman_if *)if_list.next)->if_active == IF_ACTIVE) &&
123 !(((struct batman_if *)if_list.next)->if_active == IF_TO_BE_ACTIVATED) &&
124 (!main_if_was_up())) {
130 #ifdef __NET_NET_NAMESPACE_H
131 net_dev = dev_get_by_name(&init_net, dev);
133 net_dev = dev_get_by_name(dev);
138 if (!(net_dev->flags & IFF_UP))
150 /* deactivates the interface. */
151 void hardif_deactivate_interface(struct batman_if *batman_if)
153 if (batman_if->if_active != IF_ACTIVE)
156 if (batman_if->raw_sock)
157 sock_release(batman_if->raw_sock);
160 * batman_if->net_dev has been acquired by dev_get_by_name() in
161 * proc_interfaces_write() and has to be unreferenced.
164 if (batman_if->net_dev)
165 dev_put(batman_if->net_dev);
167 batman_if->raw_sock = NULL;
168 batman_if->net_dev = NULL;
170 batman_if->if_active = IF_INACTIVE;
173 printk(KERN_INFO "batman-adv:Interface deactivated: %s\n",
177 /* (re)activate given interface. */
178 static void hardif_activate_interface(struct batman_if *batman_if)
180 struct sockaddr_ll bind_addr;
183 if (batman_if->if_active != IF_INACTIVE)
186 #ifdef __NET_NET_NAMESPACE_H
187 batman_if->net_dev = dev_get_by_name(&init_net, batman_if->dev);
189 batman_if->net_dev = dev_get_by_name(batman_if->dev);
191 if (!batman_if->net_dev)
194 retval = sock_create_kern(PF_PACKET, SOCK_RAW,
195 __constant_htons(ETH_P_BATMAN),
196 &batman_if->raw_sock);
199 printk(KERN_ERR "batman-adv:Can't create raw socket: %i\n",
204 bind_addr.sll_family = AF_PACKET;
205 bind_addr.sll_ifindex = batman_if->net_dev->ifindex;
206 bind_addr.sll_protocol = 0; /* is set by the kernel */
208 retval = kernel_bind(batman_if->raw_sock,
209 (struct sockaddr *)&bind_addr, sizeof(bind_addr));
212 printk(KERN_ERR "batman-adv:Can't create bind raw socket: %i\n",
217 check_known_mac_addr(batman_if->net_dev->dev_addr);
219 batman_if->raw_sock->sk->sk_user_data =
220 batman_if->raw_sock->sk->sk_data_ready;
221 batman_if->raw_sock->sk->sk_data_ready = batman_data_ready;
223 addr_to_string(batman_if->addr_str, batman_if->net_dev->dev_addr);
225 memcpy(((struct batman_packet *)(batman_if->packet_buff))->orig,
226 batman_if->net_dev->dev_addr, ETH_ALEN);
227 memcpy(((struct batman_packet *)(batman_if->packet_buff))->prev_sender,
228 batman_if->net_dev->dev_addr, ETH_ALEN);
230 batman_if->if_active = IF_TO_BE_ACTIVATED;
233 /* save the mac address if it is our primary interface */
234 if (batman_if->if_num == 0)
235 set_main_if_addr(batman_if->net_dev->dev_addr);
237 printk(KERN_INFO "batman-adv:Interface activated: %s\n",
243 sock_release(batman_if->raw_sock);
245 dev_put(batman_if->net_dev);
247 batman_if->raw_sock = NULL;
248 batman_if->net_dev = NULL;
251 static void hardif_free_interface(struct rcu_head *rcu)
253 struct batman_if *batman_if = container_of(rcu, struct batman_if, rcu);
255 kfree(batman_if->packet_buff);
256 kfree(batman_if->dev);
262 * - echo '' > /proc/.../interfaces
263 * - modprobe -r batman-adv-core
265 /* removes and frees all interfaces */
266 void hardif_remove_interfaces(void)
268 struct batman_if *batman_if = NULL;
272 /* no lock needed - we don't delete somewhere else */
273 list_for_each_entry(batman_if, &if_list, list) {
275 list_del_rcu(&batman_if->list);
277 /* first deactivate interface */
278 if (batman_if->if_active != IF_INACTIVE)
279 hardif_deactivate_interface(batman_if);
281 call_rcu(&batman_if->rcu, hardif_free_interface);
285 static int resize_orig(struct orig_node *orig_node, int if_num)
289 data_ptr = kmalloc((if_num + 1) * sizeof(TYPE_OF_WORD) * NUM_WORDS,
292 printk(KERN_ERR "batman-adv:Can't resize orig: out of memory\n");
296 memcpy(data_ptr, orig_node->bcast_own,
297 if_num * sizeof(TYPE_OF_WORD) * NUM_WORDS);
298 kfree(orig_node->bcast_own);
299 orig_node->bcast_own = data_ptr;
301 data_ptr = kmalloc((if_num + 1) * sizeof(uint8_t), GFP_ATOMIC);
303 printk(KERN_ERR "batman-adv:Can't resize orig: out of memory\n");
307 memcpy(data_ptr, orig_node->bcast_own_sum, if_num * sizeof(uint8_t));
308 kfree(orig_node->bcast_own_sum);
309 orig_node->bcast_own_sum = data_ptr;
315 /* adds an interface the interface list and activate it, if possible */
316 int hardif_add_interface(char *dev, int if_num)
318 struct batman_if *batman_if;
319 struct batman_packet *batman_packet;
320 struct orig_node *orig_node;
323 batman_if = kmalloc(sizeof(struct batman_if), GFP_KERNEL);
326 printk(KERN_ERR "batman-adv:Can't add interface (%s): out of memory\n", dev);
330 batman_if->raw_sock = NULL;
331 batman_if->net_dev = NULL;
333 if ((if_num == 0) && (num_hna > 0))
334 batman_if->packet_len = BAT_PACKET_LEN + num_hna * ETH_ALEN;
336 batman_if->packet_len = BAT_PACKET_LEN;
338 batman_if->packet_buff = kmalloc(batman_if->packet_len, GFP_KERNEL);
340 if (!batman_if->packet_buff) {
341 printk(KERN_ERR "batman-adv:Can't add interface packet (%s): out of memory\n", dev);
345 batman_if->if_num = if_num;
346 batman_if->dev = dev;
347 batman_if->if_active = IF_INACTIVE;
348 INIT_RCU_HEAD(&batman_if->rcu);
350 printk(KERN_INFO "batman-adv:Adding interface: %s\n", dev);
353 INIT_LIST_HEAD(&batman_if->list);
355 batman_packet = (struct batman_packet *)(batman_if->packet_buff);
356 batman_packet->packet_type = BAT_PACKET;
357 batman_packet->version = COMPAT_VERSION;
358 batman_packet->flags = 0x00;
359 batman_packet->ttl = (batman_if->if_num > 0 ? 2 : TTL);
360 batman_packet->flags = 0;
361 batman_packet->tq = TQ_MAX_VALUE;
362 batman_packet->num_hna = 0;
364 if (batman_if->packet_len != BAT_PACKET_LEN) {
365 unsigned char *hna_buff;
368 hna_buff = batman_if->packet_buff + BAT_PACKET_LEN;
369 hna_len = batman_if->packet_len - BAT_PACKET_LEN;
370 batman_packet->num_hna = hna_local_fill_buffer(hna_buff,
374 atomic_set(&batman_if->seqno, 1);
376 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
378 spin_lock(&orig_hash_lock);
380 while (hash_iterate(orig_hash, &hashit)) {
381 orig_node = hashit.bucket->data;
382 if (resize_orig(orig_node, if_num) == -1) {
383 spin_unlock(&orig_hash_lock);
388 spin_unlock(&orig_hash_lock);
390 if (!hardif_is_interface_up(batman_if->dev))
391 printk(KERN_ERR "batman-adv:Not using interface %s (retrying later): interface not active\n", batman_if->dev);
393 hardif_activate_interface(batman_if);
395 list_add_tail_rcu(&batman_if->list, &if_list);
397 /* begin sending originator messages on that interface */
398 schedule_own_packet(batman_if);
402 kfree(batman_if->packet_buff);
408 char hardif_get_active_if_num(void)
413 static int hard_if_event(struct notifier_block *this,
414 unsigned long event, void *ptr)
416 struct net_device *dev = (struct net_device *)ptr;
417 struct batman_if *batman_if = get_batman_if_by_name(dev->name);
423 case NETDEV_GOING_DOWN:
425 case NETDEV_UNREGISTER:
426 hardif_deactivate_interface(batman_if);
429 hardif_activate_interface(batman_if);
430 if ((atomic_read(&module_state) == MODULE_INACTIVE) &&
431 (hardif_get_active_if_num() > 0)) {
435 /* NETDEV_CHANGEADDR - mac address change - what are we doing here ? */
446 struct notifier_block hard_if_notifier = {
447 .notifier_call = hard_if_event,