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"
25 #include "soft-interface.h"
27 #include "translation-table.h"
32 #define MIN(x, y) ((x) < (y) ? (x) : (y))
34 static char avail_ifs;
35 static char active_ifs;
37 static void hardif_free_interface(struct rcu_head *rcu);
39 static struct batman_if *get_batman_if_by_name(char *name)
41 struct batman_if *batman_if;
44 list_for_each_entry_rcu(batman_if, &if_list, list) {
45 if (strncmp(batman_if->dev, name, IFNAMSIZ) == 0)
56 int hardif_min_mtu(void)
58 struct batman_if *batman_if;
59 /* allow big frames if all devices are capable to do so
60 * (have MTU > 1500 + BAT_HEADER_LEN) */
61 int min_mtu = ETH_DATA_LEN;
64 list_for_each_entry_rcu(batman_if, &if_list, list) {
65 if ((batman_if->if_active == IF_ACTIVE) ||
66 (batman_if->if_active == IF_TO_BE_ACTIVATED))
67 min_mtu = MIN(batman_if->net_dev->mtu - BAT_HEADER_LEN,
75 static void check_known_mac_addr(uint8_t *addr)
77 struct batman_if *batman_if;
78 char mac_string[ETH_STR_LEN];
81 list_for_each_entry_rcu(batman_if, &if_list, list) {
82 if ((batman_if->if_active != IF_ACTIVE) &&
83 (batman_if->if_active != IF_TO_BE_ACTIVATED))
86 if (!compare_orig(batman_if->net_dev->dev_addr, addr))
89 addr_to_string(mac_string, addr);
90 debug_log(LOG_TYPE_WARN, "The newly added mac address (%s) already exists on: %s\n",
91 mac_string, batman_if->dev);
92 debug_log(LOG_TYPE_WARN, "It is strongly recommended to keep mac addresses unique to avoid problems!\n");
97 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
98 void update_min_mtu(void)
102 min_mtu = hardif_min_mtu();
103 if (soft_device->mtu != min_mtu)
104 soft_device->mtu = min_mtu;
107 /* checks if the interface is up. (returns 1 if it is) */
108 static int hardif_is_interface_up(char *dev)
110 struct net_device *net_dev;
113 * if we already have an interface in our interface list and
114 * the current interface is not the primary interface and
115 * the primary interface is not up and
116 * the primary interface has never been up - don't activate any
117 * secondary interface !
121 if ((!list_empty(&if_list)) &&
122 strncmp(((struct batman_if *)if_list.next)->dev, dev, IFNAMSIZ) &&
123 !(((struct batman_if *)if_list.next)->if_active == IF_ACTIVE) &&
124 !(((struct batman_if *)if_list.next)->if_active == IF_TO_BE_ACTIVATED) &&
125 (!main_if_was_up())) {
131 #ifdef __NET_NET_NAMESPACE_H
132 net_dev = dev_get_by_name(&init_net, dev);
134 net_dev = dev_get_by_name(dev);
139 if (!(net_dev->flags & IFF_UP))
151 /* deactivates the interface. */
152 void hardif_deactivate_interface(struct batman_if *batman_if)
154 if (batman_if->if_active != IF_ACTIVE)
157 if (batman_if->raw_sock)
158 sock_release(batman_if->raw_sock);
161 * batman_if->net_dev has been acquired by dev_get_by_name() in
162 * proc_interfaces_write() and has to be unreferenced.
165 if (batman_if->net_dev)
166 dev_put(batman_if->net_dev);
168 batman_if->raw_sock = NULL;
169 batman_if->net_dev = NULL;
171 batman_if->if_active = IF_INACTIVE;
174 debug_log(LOG_TYPE_NOTICE, "Interface deactivated: %s\n",
178 /* (re)activate given interface. */
179 static void hardif_activate_interface(struct batman_if *batman_if)
181 struct sockaddr_ll bind_addr;
184 if (batman_if->if_active != IF_INACTIVE)
187 #ifdef __NET_NET_NAMESPACE_H
188 batman_if->net_dev = dev_get_by_name(&init_net, batman_if->dev);
190 batman_if->net_dev = dev_get_by_name(batman_if->dev);
192 if (!batman_if->net_dev)
195 retval = sock_create_kern(PF_PACKET, SOCK_RAW,
196 __constant_htons(ETH_P_BATMAN),
197 &batman_if->raw_sock);
200 debug_log(LOG_TYPE_WARN, "Can't create raw socket: %i\n",
205 bind_addr.sll_family = AF_PACKET;
206 bind_addr.sll_ifindex = batman_if->net_dev->ifindex;
207 bind_addr.sll_protocol = 0; /* is set by the kernel */
209 retval = kernel_bind(batman_if->raw_sock,
210 (struct sockaddr *)&bind_addr, sizeof(bind_addr));
213 debug_log(LOG_TYPE_WARN, "Can't create bind raw socket: %i\n",
218 check_known_mac_addr(batman_if->net_dev->dev_addr);
220 batman_if->raw_sock->sk->sk_user_data =
221 batman_if->raw_sock->sk->sk_data_ready;
222 batman_if->raw_sock->sk->sk_data_ready = batman_data_ready;
224 addr_to_string(batman_if->addr_str, batman_if->net_dev->dev_addr);
226 memcpy(((struct batman_packet *)(batman_if->packet_buff))->orig,
227 batman_if->net_dev->dev_addr, ETH_ALEN);
228 memcpy(((struct batman_packet *)(batman_if->packet_buff))->prev_sender,
229 batman_if->net_dev->dev_addr, ETH_ALEN);
231 batman_if->if_active = IF_TO_BE_ACTIVATED;
234 /* save the mac address if it is our primary interface */
235 if (batman_if->if_num == 0)
236 set_main_if_addr(batman_if->net_dev->dev_addr);
238 debug_log(LOG_TYPE_NOTICE, "Interface activated: %s\n",
244 sock_release(batman_if->raw_sock);
246 dev_put(batman_if->net_dev);
248 batman_if->raw_sock = NULL;
249 batman_if->net_dev = NULL;
252 static void hardif_free_interface(struct rcu_head *rcu)
254 struct batman_if *batman_if = container_of(rcu, struct batman_if, rcu);
256 kfree(batman_if->packet_buff);
257 kfree(batman_if->dev);
263 * - echo '' > /proc/.../interfaces
264 * - modprobe -r batman-adv-core
266 /* removes and frees all interfaces */
267 void hardif_remove_interfaces(void)
269 struct batman_if *batman_if = NULL;
273 /* no lock needed - we don't delete somewhere else */
274 list_for_each_entry(batman_if, &if_list, list) {
276 list_del_rcu(&batman_if->list);
278 /* first deactivate interface */
279 if (batman_if->if_active != IF_INACTIVE)
280 hardif_deactivate_interface(batman_if);
282 call_rcu(&batman_if->rcu, hardif_free_interface);
286 static int resize_orig(struct orig_node *orig_node, int if_num)
290 data_ptr = kmalloc((if_num + 1) * sizeof(TYPE_OF_WORD) * NUM_WORDS,
293 debug_log(LOG_TYPE_WARN, "Can't resize orig: out of memory\n");
297 memcpy(data_ptr, orig_node->bcast_own,
298 if_num * sizeof(TYPE_OF_WORD) * NUM_WORDS);
299 kfree(orig_node->bcast_own);
300 orig_node->bcast_own = data_ptr;
302 data_ptr = kmalloc((if_num + 1) * sizeof(uint8_t), GFP_ATOMIC);
304 debug_log(LOG_TYPE_WARN, "Can't resize orig: out of memory\n");
308 memcpy(data_ptr, orig_node->bcast_own_sum, if_num * sizeof(uint8_t));
309 kfree(orig_node->bcast_own_sum);
310 orig_node->bcast_own_sum = data_ptr;
316 /* adds an interface the interface list and activate it, if possible */
317 int hardif_add_interface(char *dev, int if_num)
319 struct batman_if *batman_if;
320 struct batman_packet *batman_packet;
321 struct orig_node *orig_node;
322 struct hash_it_t *hashit = NULL;
324 batman_if = kmalloc(sizeof(struct batman_if), GFP_KERNEL);
327 debug_log(LOG_TYPE_WARN, "Can't add interface (%s): out of memory\n", dev);
331 batman_if->raw_sock = NULL;
332 batman_if->net_dev = NULL;
334 if ((if_num == 0) && (num_hna > 0))
335 batman_if->packet_len = BAT_PACKET_LEN + num_hna * ETH_ALEN;
337 batman_if->packet_len = BAT_PACKET_LEN;
339 batman_if->packet_buff = kmalloc(batman_if->packet_len, GFP_KERNEL);
341 if (!batman_if->packet_buff) {
342 debug_log(LOG_TYPE_WARN, "Can't add interface packet (%s): out of memory\n", dev);
346 batman_if->if_num = if_num;
347 batman_if->dev = dev;
348 batman_if->if_active = IF_INACTIVE;
349 INIT_RCU_HEAD(&batman_if->rcu);
351 debug_log(LOG_TYPE_NOTICE, "Adding interface: %s\n", dev);
354 INIT_LIST_HEAD(&batman_if->list);
356 batman_packet = (struct batman_packet *)(batman_if->packet_buff);
357 batman_packet->packet_type = BAT_PACKET;
358 batman_packet->version = COMPAT_VERSION;
359 batman_packet->flags = 0x00;
360 batman_packet->ttl = (batman_if->if_num > 0 ? 2 : TTL);
361 batman_packet->flags = 0;
362 batman_packet->tq = TQ_MAX_VALUE;
363 batman_packet->num_hna = 0;
365 if (batman_if->packet_len != BAT_PACKET_LEN) {
366 unsigned char *hna_buff;
369 hna_buff = batman_if->packet_buff + BAT_PACKET_LEN;
370 hna_len = batman_if->packet_len - BAT_PACKET_LEN;
371 batman_packet->num_hna = hna_local_fill_buffer(hna_buff,
375 atomic_set(&batman_if->seqno, 1);
377 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
379 spin_lock(&orig_hash_lock);
381 while (NULL != (hashit = hash_iterate(orig_hash, hashit))) {
382 orig_node = hashit->bucket->data;
383 if (resize_orig(orig_node, if_num) == -1) {
384 spin_unlock(&orig_hash_lock);
389 spin_unlock(&orig_hash_lock);
391 if (!hardif_is_interface_up(batman_if->dev))
392 debug_log(LOG_TYPE_WARN, "Not using interface %s (retrying later): interface not active\n", batman_if->dev);
394 hardif_activate_interface(batman_if);
396 list_add_tail_rcu(&batman_if->list, &if_list);
398 /* begin sending originator messages on that interface */
399 schedule_own_packet(batman_if);
403 if (batman_if->packet_buff)
404 kfree(batman_if->packet_buff);
410 char hardif_get_active_if_num(void)
415 static int hard_if_event(struct notifier_block *this,
416 unsigned long event, void *ptr)
418 struct net_device *dev = (struct net_device *)ptr;
419 struct batman_if *batman_if = get_batman_if_by_name(dev->name);
425 case NETDEV_GOING_DOWN:
427 case NETDEV_UNREGISTER:
428 hardif_deactivate_interface(batman_if);
431 hardif_activate_interface(batman_if);
432 if ((atomic_read(&module_state) == MODULE_INACTIVE) &&
433 (hardif_get_active_if_num() > 0)) {
437 /* NETDEV_CHANGEADDR - mac address change - what are we doing here ? */
439 /* debug_log(LOG_TYPE_CRIT, "hard_if_event: %s %i\n", dev->name, event); */
449 struct notifier_block hard_if_notifier = {
450 .notifier_call = hard_if_event,