]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/batman-adv/hard-interface.c
Staging: batman-adv: Fix skbuff leak in VIS code.
[net-next-2.6.git] / drivers / staging / batman-adv / hard-interface.c
CommitLineData
5beef3c9
AL
1/*
2 * Copyright (C) 2007-2009 B.A.T.M.A.N. contributors:
3 *
4 * Marek Lindner, Simon Wunderlich
5 *
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.
9 *
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.
14 *
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
19 *
20 */
21
22#include "main.h"
23#include "hard-interface.h"
5beef3c9
AL
24#include "soft-interface.h"
25#include "send.h"
26#include "translation-table.h"
27#include "routing.h"
28#include "hash.h"
5beef3c9
AL
29
30#define MIN(x, y) ((x) < (y) ? (x) : (y))
31
32static char avail_ifs;
33static char active_ifs;
34
35static void hardif_free_interface(struct rcu_head *rcu);
36
37static struct batman_if *get_batman_if_by_name(char *name)
38{
39 struct batman_if *batman_if;
40
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;
45 }
46
47 batman_if = NULL;
48
49out:
50 rcu_read_unlock();
51 return batman_if;
52}
53
54int hardif_min_mtu(void)
55{
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;
60
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);
67 }
68 rcu_read_unlock();
69
70 return min_mtu;
71}
72
73static void check_known_mac_addr(uint8_t *addr)
74{
75 struct batman_if *batman_if;
5beef3c9
AL
76
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;
82
83 if (!compare_orig(batman_if->net_dev->dev_addr, addr))
84 continue;
85
b9b27e4e
AL
86 printk(KERN_WARNING "batman-adv:The newly added mac address (%pM) already exists on: %s\n",
87 addr, batman_if->dev);
bad2239e 88 printk(KERN_WARNING "batman-adv:It is strongly recommended to keep mac addresses unique to avoid problems!\n");
5beef3c9
AL
89 }
90 rcu_read_unlock();
91}
92
93/* adjusts the MTU if a new interface with a smaller MTU appeared. */
94void update_min_mtu(void)
95{
96 int min_mtu;
97
98 min_mtu = hardif_min_mtu();
99 if (soft_device->mtu != min_mtu)
100 soft_device->mtu = min_mtu;
101}
102
103/* checks if the interface is up. (returns 1 if it is) */
104static int hardif_is_interface_up(char *dev)
105{
106 struct net_device *net_dev;
107
108 /**
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 !
114 */
115
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;
124 }
125 rcu_read_unlock();
126
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;
134
135 if (!(net_dev->flags & IFF_UP))
136 goto failure;
137
138 dev_put(net_dev);
139 return 1;
140
141failure:
142 dev_put(net_dev);
143end:
144 return 0;
145}
146
147/* deactivates the interface. */
148void hardif_deactivate_interface(struct batman_if *batman_if)
149{
150 if (batman_if->if_active != IF_ACTIVE)
151 return;
152
5beef3c9
AL
153 /**
154 * batman_if->net_dev has been acquired by dev_get_by_name() in
155 * proc_interfaces_write() and has to be unreferenced.
156 */
157
158 if (batman_if->net_dev)
159 dev_put(batman_if->net_dev);
160
5beef3c9
AL
161 batman_if->if_active = IF_INACTIVE;
162 active_ifs--;
163
bad2239e
AL
164 printk(KERN_INFO "batman-adv:Interface deactivated: %s\n",
165 batman_if->dev);
5beef3c9
AL
166}
167
168/* (re)activate given interface. */
169static void hardif_activate_interface(struct batman_if *batman_if)
170{
5beef3c9
AL
171 if (batman_if->if_active != IF_INACTIVE)
172 return;
173
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;
181
5beef3c9
AL
182 check_known_mac_addr(batman_if->net_dev->dev_addr);
183
5beef3c9
AL
184 addr_to_string(batman_if->addr_str, batman_if->net_dev->dev_addr);
185
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);
190
191 batman_if->if_active = IF_TO_BE_ACTIVATED;
192 active_ifs++;
193
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);
197
bad2239e
AL
198 printk(KERN_INFO "batman-adv:Interface activated: %s\n",
199 batman_if->dev);
5beef3c9
AL
200
201 return;
202
5beef3c9 203dev_err:
5beef3c9
AL
204 batman_if->net_dev = NULL;
205}
206
207static void hardif_free_interface(struct rcu_head *rcu)
208{
209 struct batman_if *batman_if = container_of(rcu, struct batman_if, rcu);
210
211 kfree(batman_if->packet_buff);
212 kfree(batman_if->dev);
213 kfree(batman_if);
214}
215
216/**
217 * called by
218 * - echo '' > /proc/.../interfaces
219 * - modprobe -r batman-adv-core
220 */
221/* removes and frees all interfaces */
222void hardif_remove_interfaces(void)
223{
224 struct batman_if *batman_if = NULL;
225
226 avail_ifs = 0;
227
228 /* no lock needed - we don't delete somewhere else */
229 list_for_each_entry(batman_if, &if_list, list) {
230
231 list_del_rcu(&batman_if->list);
232
233 /* first deactivate interface */
234 if (batman_if->if_active != IF_INACTIVE)
235 hardif_deactivate_interface(batman_if);
236
237 call_rcu(&batman_if->rcu, hardif_free_interface);
238 }
239}
240
241static int resize_orig(struct orig_node *orig_node, int if_num)
242{
243 void *data_ptr;
244
245 data_ptr = kmalloc((if_num + 1) * sizeof(TYPE_OF_WORD) * NUM_WORDS,
246 GFP_ATOMIC);
247 if (!data_ptr) {
bad2239e 248 printk(KERN_ERR "batman-adv:Can't resize orig: out of memory\n");
5beef3c9
AL
249 return -1;
250 }
251
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;
256
257 data_ptr = kmalloc((if_num + 1) * sizeof(uint8_t), GFP_ATOMIC);
258 if (!data_ptr) {
bad2239e 259 printk(KERN_ERR "batman-adv:Can't resize orig: out of memory\n");
5beef3c9
AL
260 return -1;
261 }
262
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;
266
267 return 0;
268}
269
270
271/* adds an interface the interface list and activate it, if possible */
272int hardif_add_interface(char *dev, int if_num)
273{
274 struct batman_if *batman_if;
275 struct batman_packet *batman_packet;
276 struct orig_node *orig_node;
e7017195 277 unsigned long flags;
b6c35976 278 HASHIT(hashit);
5beef3c9
AL
279
280 batman_if = kmalloc(sizeof(struct batman_if), GFP_KERNEL);
281
282 if (!batman_if) {
bad2239e 283 printk(KERN_ERR "batman-adv:Can't add interface (%s): out of memory\n", dev);
5beef3c9
AL
284 return -1;
285 }
286
5beef3c9
AL
287 batman_if->net_dev = NULL;
288
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;
293
294 batman_if->packet_buff = kmalloc(batman_if->packet_len, GFP_KERNEL);
295
296 if (!batman_if->packet_buff) {
bad2239e 297 printk(KERN_ERR "batman-adv:Can't add interface packet (%s): out of memory\n", dev);
5beef3c9
AL
298 goto out;
299 }
300
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);
305
bad2239e 306 printk(KERN_INFO "batman-adv:Adding interface: %s\n", dev);
5beef3c9
AL
307 avail_ifs++;
308
309 INIT_LIST_HEAD(&batman_if->list);
310
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;
319
320 if (batman_if->packet_len != BAT_PACKET_LEN) {
321 unsigned char *hna_buff;
322 int hna_len;
323
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);
328 }
329
330 atomic_set(&batman_if->seqno, 1);
331
332 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
333 * if_num */
e7017195 334 spin_lock_irqsave(&orig_hash_lock, flags);
5beef3c9 335
b6c35976
SW
336 while (hash_iterate(orig_hash, &hashit)) {
337 orig_node = hashit.bucket->data;
5beef3c9 338 if (resize_orig(orig_node, if_num) == -1) {
e7017195 339 spin_unlock_irqrestore(&orig_hash_lock, flags);
5beef3c9
AL
340 goto out;
341 }
342 }
343
e7017195 344 spin_unlock_irqrestore(&orig_hash_lock, flags);
5beef3c9
AL
345
346 if (!hardif_is_interface_up(batman_if->dev))
bad2239e 347 printk(KERN_ERR "batman-adv:Not using interface %s (retrying later): interface not active\n", batman_if->dev);
5beef3c9
AL
348 else
349 hardif_activate_interface(batman_if);
350
351 list_add_tail_rcu(&batman_if->list, &if_list);
352
353 /* begin sending originator messages on that interface */
354 schedule_own_packet(batman_if);
355 return 1;
356
357out:
acdfd0e0 358 kfree(batman_if->packet_buff);
5beef3c9
AL
359 kfree(batman_if);
360 kfree(dev);
361 return -1;
362}
363
364char hardif_get_active_if_num(void)
365{
366 return active_ifs;
367}
368
369static int hard_if_event(struct notifier_block *this,
bad2239e 370 unsigned long event, void *ptr)
5beef3c9
AL
371{
372 struct net_device *dev = (struct net_device *)ptr;
373 struct batman_if *batman_if = get_batman_if_by_name(dev->name);
374
375 if (!batman_if)
376 goto out;
377
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();
389 }
390 break;
391 /* NETDEV_CHANGEADDR - mac address change - what are we doing here ? */
392 default:
5beef3c9
AL
393 break;
394 };
395
396 update_min_mtu();
397
398out:
399 return NOTIFY_DONE;
400}
401
e7017195
SW
402/* find batman interface by netdev. assumes rcu_read_lock on */
403static struct batman_if *find_batman_if(struct net_device *dev)
404{
405 struct batman_if *batman_if;
406
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;
412 }
413 }
414 rcu_read_unlock();
415 return NULL;
416}
417
418
419/* receive a packet with the batman ethertype coming on a hard
420 * interface */
421int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
422 struct packet_type *ptype, struct net_device *orig_dev)
423{
424 struct batman_packet *batman_packet;
425 struct batman_if *batman_if;
426 struct net_device_stats *stats;
427 int ret;
428
429 skb = skb_share_check(skb, GFP_ATOMIC);
430
431 if (skb == NULL)
432 goto err_free;
433
434 /* packet should hold at least type and version */
435 if (unlikely(skb_headlen(skb) < 2))
436 goto err_free;
437
438 /* expect a valid ethernet header here. */
439 if (unlikely(skb->mac_len != sizeof(struct ethhdr)
440 || !skb_mac_header(skb)))
441 goto err_free;
442
443 batman_if = find_batman_if(skb->dev);
444 if (!batman_if)
445 goto err_free;
446
b9b27e4e
AL
447 stats = (struct net_device_stats *) dev_get_stats(skb->dev);
448 if (stats) {
449 stats->rx_packets++;
450 stats->rx_bytes += skb->len;
451 }
e7017195
SW
452
453 batman_packet = (struct batman_packet *)skb->data;
454
455 if (batman_packet->version != COMPAT_VERSION) {
456 bat_dbg(DBG_BATMAN,
457 "Drop packet: incompatible batman version (%i)\n",
458 batman_packet->version);
459 goto err_free;
460 }
461
462 /* all receive handlers return whether they received or reused
463 * the supplied skb. if not, we have to free the skb. */
464
465 switch (batman_packet->packet_type) {
466 /* batman originator packet */
467 case BAT_PACKET:
468 ret = recv_bat_packet(skb, batman_if);
469 break;
470
471 /* batman icmp packet */
472 case BAT_ICMP:
473 ret = recv_icmp_packet(skb);
474 break;
475
476 /* unicast packet */
477 case BAT_UNICAST:
478 ret = recv_unicast_packet(skb);
479 break;
480
481 /* broadcast packet */
482 case BAT_BCAST:
483 ret = recv_bcast_packet(skb);
484 break;
485
486 /* vis packet */
487 case BAT_VIS:
488 ret = recv_vis_packet(skb);
489 break;
490 default:
491 ret = NET_RX_DROP;
492 }
493 if (ret == NET_RX_DROP)
494 kfree_skb(skb);
495
496 /* return NET_RX_SUCCESS in any case as we
497 * most probably dropped the packet for
498 * routing-logical reasons. */
499
500 return NET_RX_SUCCESS;
501
502err_free:
503 kfree_skb(skb);
504 return NET_RX_DROP;
505
506}
507
508
5beef3c9 509struct notifier_block hard_if_notifier = {
bad2239e 510 .notifier_call = hard_if_event,
5beef3c9 511};