Documentation: rename PCI/PCI-DMA-mapping.txt to DMA-API-HOWTO.txt
[nv-tegra-linux-2.6.git] / drivers / staging / batman-adv / hard-interface.c
blobbefd4883951926926f69eec9a04ddb8ad7a64601
1 /*
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
18 * 02110-1301, USA
22 #include "main.h"
23 #include "hard-interface.h"
24 #include "soft-interface.h"
25 #include "send.h"
26 #include "translation-table.h"
27 #include "routing.h"
28 #include "hash.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;
41 rcu_read_lock();
42 list_for_each_entry_rcu(batman_if, &if_list, list) {
43 if (strncmp(batman_if->dev, name, IFNAMSIZ) == 0)
44 goto out;
47 batman_if = NULL;
49 out:
50 rcu_read_unlock();
51 return batman_if;
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;
61 rcu_read_lock();
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,
66 min_mtu);
68 rcu_read_unlock();
70 return min_mtu;
73 static void check_known_mac_addr(uint8_t *addr)
75 struct batman_if *batman_if;
77 rcu_read_lock();
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))
81 continue;
83 if (!compare_orig(batman_if->net_dev->dev_addr, addr))
84 continue;
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");
90 rcu_read_unlock();
93 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
94 void update_min_mtu(void)
96 int min_mtu;
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 !
116 rcu_read_lock();
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())) {
122 rcu_read_unlock();
123 goto end;
125 rcu_read_unlock();
127 #ifdef __NET_NET_NAMESPACE_H
128 net_dev = dev_get_by_name(&init_net, dev);
129 #else
130 net_dev = dev_get_by_name(dev);
131 #endif
132 if (!net_dev)
133 goto end;
135 if (!(net_dev->flags & IFF_UP))
136 goto failure;
138 dev_put(net_dev);
139 return 1;
141 failure:
142 dev_put(net_dev);
143 end:
144 return 0;
147 /* deactivates the interface. */
148 void hardif_deactivate_interface(struct batman_if *batman_if)
150 if (batman_if->if_active != IF_ACTIVE)
151 return;
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;
162 active_ifs--;
164 printk(KERN_INFO "batman-adv:Interface deactivated: %s\n",
165 batman_if->dev);
168 /* (re)activate given interface. */
169 static void hardif_activate_interface(struct batman_if *batman_if)
171 if (batman_if->if_active != IF_INACTIVE)
172 return;
174 #ifdef __NET_NET_NAMESPACE_H
175 batman_if->net_dev = dev_get_by_name(&init_net, batman_if->dev);
176 #else
177 batman_if->net_dev = dev_get_by_name(batman_if->dev);
178 #endif
179 if (!batman_if->net_dev)
180 goto dev_err;
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;
192 active_ifs++;
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",
199 batman_if->dev);
201 return;
203 dev_err:
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);
213 kfree(batman_if);
217 * called by
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;
226 avail_ifs = 0;
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)
243 void *data_ptr;
245 data_ptr = kmalloc((if_num + 1) * sizeof(TYPE_OF_WORD) * NUM_WORDS,
246 GFP_ATOMIC);
247 if (!data_ptr) {
248 printk(KERN_ERR "batman-adv:Can't resize orig: out of memory\n");
249 return -1;
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);
258 if (!data_ptr) {
259 printk(KERN_ERR "batman-adv:Can't resize orig: out of memory\n");
260 return -1;
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;
267 return 0;
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;
277 unsigned long flags;
278 HASHIT(hashit);
280 batman_if = kmalloc(sizeof(struct batman_if), GFP_KERNEL);
282 if (!batman_if) {
283 printk(KERN_ERR "batman-adv:Can't add interface (%s): out of memory\n", dev);
284 return -1;
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;
291 else
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);
298 goto out;
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);
307 avail_ifs++;
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;
322 int hna_len;
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,
327 hna_len);
330 atomic_set(&batman_if->seqno, 1);
332 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
333 * if_num */
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);
340 goto out;
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);
348 else
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);
355 return 1;
357 out:
358 kfree(batman_if->packet_buff);
359 kfree(batman_if);
360 kfree(dev);
361 return -1;
364 char hardif_get_active_if_num(void)
366 return active_ifs;
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);
375 if (!batman_if)
376 goto out;
378 switch (event) {
379 case NETDEV_GOING_DOWN:
380 case NETDEV_DOWN:
381 case NETDEV_UNREGISTER:
382 hardif_deactivate_interface(batman_if);
383 break;
384 case NETDEV_UP:
385 hardif_activate_interface(batman_if);
386 if ((atomic_read(&module_state) == MODULE_INACTIVE) &&
387 (hardif_get_active_if_num() > 0)) {
388 activate_module();
390 break;
391 /* NETDEV_CHANGEADDR - mac address change - what are we doing here ? */
392 default:
393 break;
396 update_min_mtu();
398 out:
399 return NOTIFY_DONE;
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;
407 rcu_read_lock();
408 list_for_each_entry_rcu(batman_if, &if_list, list) {
409 if (batman_if->net_dev == dev) {
410 rcu_read_unlock();
411 return batman_if;
414 rcu_read_unlock();
415 return NULL;
419 /* receive a packet with the batman ethertype coming on a hard
420 * interface */
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;
427 int ret;
429 skb = skb_share_check(skb, GFP_ATOMIC);
431 /* skb was released by skb_share_check() */
432 if (!skb)
433 goto err_out;
435 if (atomic_read(&module_state) != MODULE_ACTIVE)
436 goto err_free;
438 /* packet should hold at least type and version */
439 if (unlikely(skb_headlen(skb) < 2))
440 goto err_free;
442 /* expect a valid ethernet header here. */
443 if (unlikely(skb->mac_len != sizeof(struct ethhdr)
444 || !skb_mac_header(skb)))
445 goto err_free;
447 batman_if = find_batman_if(skb->dev);
448 if (!batman_if)
449 goto err_free;
451 /* discard frames on not active interfaces */
452 if (batman_if->if_active != IF_ACTIVE)
453 goto err_free;
455 stats = (struct net_device_stats *)dev_get_stats(skb->dev);
456 if (stats) {
457 stats->rx_packets++;
458 stats->rx_bytes += skb->len;
461 batman_packet = (struct batman_packet *)skb->data;
463 if (batman_packet->version != COMPAT_VERSION) {
464 bat_dbg(DBG_BATMAN,
465 "Drop packet: incompatible batman version (%i)\n",
466 batman_packet->version);
467 goto err_free;
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 */
475 case BAT_PACKET:
476 ret = recv_bat_packet(skb, batman_if);
477 break;
479 /* batman icmp packet */
480 case BAT_ICMP:
481 ret = recv_icmp_packet(skb);
482 break;
484 /* unicast packet */
485 case BAT_UNICAST:
486 ret = recv_unicast_packet(skb);
487 break;
489 /* broadcast packet */
490 case BAT_BCAST:
491 ret = recv_bcast_packet(skb);
492 break;
494 /* vis packet */
495 case BAT_VIS:
496 ret = recv_vis_packet(skb);
497 break;
498 default:
499 ret = NET_RX_DROP;
502 if (ret == NET_RX_DROP)
503 kfree_skb(skb);
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;
511 err_free:
512 kfree_skb(skb);
513 err_out:
514 return NET_RX_DROP;
518 struct notifier_block hard_if_notifier = {
519 .notifier_call = hard_if_event,