]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/batman-adv/hard-interface.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/kaber/nf-next-2.6
[net-next-2.6.git] / drivers / staging / batman-adv / hard-interface.c
CommitLineData
5beef3c9 1/*
9b6d10b7 2 * Copyright (C) 2007-2010 B.A.T.M.A.N. contributors:
5beef3c9
AL
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"
208e13e4
ML
28#include "bat_sysfs.h"
29#include "originator.h"
5beef3c9 30#include "hash.h"
5beef3c9 31
208e13e4 32#include <linux/if_arp.h>
5beef3c9 33
208e13e4 34#define MIN(x, y) ((x) < (y) ? (x) : (y))
5beef3c9 35
208e13e4 36struct batman_if *get_batman_if_by_netdev(struct net_device *net_dev)
5beef3c9
AL
37{
38 struct batman_if *batman_if;
39
40 rcu_read_lock();
41 list_for_each_entry_rcu(batman_if, &if_list, list) {
208e13e4 42 if (batman_if->net_dev == net_dev)
5beef3c9
AL
43 goto out;
44 }
45
46 batman_if = NULL;
47
48out:
49 rcu_read_unlock();
50 return batman_if;
51}
52
208e13e4
ML
53static int is_valid_iface(struct net_device *net_dev)
54{
55 if (net_dev->flags & IFF_LOOPBACK)
56 return 0;
57
58 if (net_dev->type != ARPHRD_ETHER)
59 return 0;
60
61 if (net_dev->addr_len != ETH_ALEN)
62 return 0;
63
64 /* no batman over batman */
65#ifdef HAVE_NET_DEVICE_OPS
66 if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
67 return 0;
68#else
69 if (net_dev->hard_start_xmit == interface_tx)
70 return 0;
71#endif
72
73 /* Device is being bridged */
f350a0a8 74 /* if (net_dev->priv_flags & IFF_BRIDGE_PORT)
208e13e4
ML
75 return 0; */
76
77 return 1;
78}
79
80static struct batman_if *get_active_batman_if(void)
5beef3c9
AL
81{
82 struct batman_if *batman_if;
5beef3c9 83
208e13e4 84 /* TODO: should check interfaces belonging to bat_priv */
5beef3c9
AL
85 rcu_read_lock();
86 list_for_each_entry_rcu(batman_if, &if_list, list) {
208e13e4
ML
87 if (batman_if->if_status == IF_ACTIVE)
88 goto out;
5beef3c9 89 }
208e13e4
ML
90
91 batman_if = NULL;
92
93out:
5beef3c9 94 rcu_read_unlock();
208e13e4
ML
95 return batman_if;
96}
5beef3c9 97
208e13e4
ML
98static void set_primary_if(struct bat_priv *bat_priv,
99 struct batman_if *batman_if)
100{
101 struct batman_packet *batman_packet;
102
103 bat_priv->primary_if = batman_if;
104
105 if (!bat_priv->primary_if)
106 return;
107
108 set_main_if_addr(batman_if->net_dev->dev_addr);
109
110 batman_packet = (struct batman_packet *)(batman_if->packet_buff);
111 batman_packet->flags = 0;
112 batman_packet->ttl = TTL;
113
114 /***
115 * hacky trick to make sure that we send the HNA information via
116 * our new primary interface
117 */
118 atomic_set(&hna_local_changed, 1);
119}
120
121static bool hardif_is_iface_up(struct batman_if *batman_if)
122{
123 if (batman_if->net_dev->flags & IFF_UP)
124 return true;
125
126 return false;
127}
128
129static void update_mac_addresses(struct batman_if *batman_if)
130{
131 addr_to_string(batman_if->addr_str, batman_if->net_dev->dev_addr);
132
133 memcpy(((struct batman_packet *)(batman_if->packet_buff))->orig,
134 batman_if->net_dev->dev_addr, ETH_ALEN);
135 memcpy(((struct batman_packet *)(batman_if->packet_buff))->prev_sender,
136 batman_if->net_dev->dev_addr, ETH_ALEN);
5beef3c9
AL
137}
138
139static void check_known_mac_addr(uint8_t *addr)
140{
141 struct batman_if *batman_if;
5beef3c9
AL
142
143 rcu_read_lock();
144 list_for_each_entry_rcu(batman_if, &if_list, list) {
208e13e4
ML
145 if ((batman_if->if_status != IF_ACTIVE) &&
146 (batman_if->if_status != IF_TO_BE_ACTIVATED))
5beef3c9
AL
147 continue;
148
149 if (!compare_orig(batman_if->net_dev->dev_addr, addr))
150 continue;
151
6d45d8df
SE
152 printk(KERN_WARNING "batman-adv:"
153 "The newly added mac address (%pM) already exists on: %s\n",
154 addr, batman_if->dev);
155 printk(KERN_WARNING "batman-adv:"
156 "It is strongly recommended to keep mac addresses unique"
157 "to avoid problems!\n");
5beef3c9
AL
158 }
159 rcu_read_unlock();
160}
161
208e13e4
ML
162int hardif_min_mtu(void)
163{
164 struct batman_if *batman_if;
165 /* allow big frames if all devices are capable to do so
166 * (have MTU > 1500 + BAT_HEADER_LEN) */
167 int min_mtu = ETH_DATA_LEN;
168
169 rcu_read_lock();
170 list_for_each_entry_rcu(batman_if, &if_list, list) {
171 if ((batman_if->if_status == IF_ACTIVE) ||
172 (batman_if->if_status == IF_TO_BE_ACTIVATED))
173 min_mtu = MIN(batman_if->net_dev->mtu - BAT_HEADER_LEN,
174 min_mtu);
175 }
176 rcu_read_unlock();
177
178 return min_mtu;
179}
180
5beef3c9
AL
181/* adjusts the MTU if a new interface with a smaller MTU appeared. */
182void update_min_mtu(void)
183{
184 int min_mtu;
185
186 min_mtu = hardif_min_mtu();
187 if (soft_device->mtu != min_mtu)
188 soft_device->mtu = min_mtu;
189}
190
208e13e4
ML
191static void hardif_activate_interface(struct bat_priv *bat_priv,
192 struct batman_if *batman_if)
5beef3c9 193{
208e13e4
ML
194 if (batman_if->if_status != IF_INACTIVE)
195 return;
5beef3c9 196
208e13e4 197 dev_hold(batman_if->net_dev);
5beef3c9 198
208e13e4
ML
199 update_mac_addresses(batman_if);
200 batman_if->if_status = IF_TO_BE_ACTIVATED;
5beef3c9 201
208e13e4
ML
202 /**
203 * the first active interface becomes our primary interface or
204 * the next active interface after the old primay interface was removed
205 */
206 if (!bat_priv->primary_if)
207 set_primary_if(bat_priv, batman_if);
5beef3c9 208
208e13e4
ML
209 printk(KERN_INFO "batman-adv:Interface activated: %s\n",
210 batman_if->dev);
5beef3c9 211
208e13e4
ML
212 if (atomic_read(&module_state) == MODULE_INACTIVE)
213 activate_module();
5beef3c9 214
208e13e4
ML
215 update_min_mtu();
216 return;
5beef3c9
AL
217}
218
208e13e4 219static void hardif_deactivate_interface(struct batman_if *batman_if)
5beef3c9 220{
208e13e4
ML
221 if ((batman_if->if_status != IF_ACTIVE) &&
222 (batman_if->if_status != IF_TO_BE_ACTIVATED))
5beef3c9
AL
223 return;
224
208e13e4 225 dev_put(batman_if->net_dev);
5beef3c9 226
208e13e4 227 batman_if->if_status = IF_INACTIVE;
5beef3c9 228
bad2239e 229 printk(KERN_INFO "batman-adv:Interface deactivated: %s\n",
208e13e4
ML
230 batman_if->dev);
231
232 update_min_mtu();
5beef3c9
AL
233}
234
208e13e4 235int hardif_enable_interface(struct batman_if *batman_if)
5beef3c9 236{
208e13e4
ML
237 /* FIXME: each batman_if will be attached to a softif */
238 struct bat_priv *bat_priv = netdev_priv(soft_device);
239 struct batman_packet *batman_packet;
5beef3c9 240
208e13e4
ML
241 if (batman_if->if_status != IF_NOT_IN_USE)
242 goto out;
5beef3c9 243
208e13e4
ML
244 batman_if->packet_len = BAT_PACKET_LEN;
245 batman_if->packet_buff = kmalloc(batman_if->packet_len, GFP_ATOMIC);
5beef3c9 246
208e13e4 247 if (!batman_if->packet_buff) {
6d45d8df
SE
248 printk(KERN_ERR "batman-adv:"
249 "Can't add interface packet (%s): out of memory\n",
208e13e4
ML
250 batman_if->dev);
251 goto err;
252 }
5beef3c9 253
208e13e4
ML
254 batman_packet = (struct batman_packet *)(batman_if->packet_buff);
255 batman_packet->packet_type = BAT_PACKET;
256 batman_packet->version = COMPAT_VERSION;
257 batman_packet->flags = 0;
258 batman_packet->ttl = 2;
259 batman_packet->tq = TQ_MAX_VALUE;
260 batman_packet->num_hna = 0;
5beef3c9 261
208e13e4
ML
262 batman_if->if_num = bat_priv->num_ifaces;
263 bat_priv->num_ifaces++;
264 batman_if->if_status = IF_INACTIVE;
265 orig_hash_add_if(batman_if, bat_priv->num_ifaces);
5beef3c9 266
208e13e4
ML
267 atomic_set(&batman_if->seqno, 1);
268 printk(KERN_INFO "batman-adv:Adding interface: %s\n", batman_if->dev);
5beef3c9 269
208e13e4
ML
270 if (hardif_is_iface_up(batman_if))
271 hardif_activate_interface(bat_priv, batman_if);
272 else
6d45d8df
SE
273 printk(KERN_ERR "batman-adv:"
274 "Not using interface %s "
275 "(retrying later): interface not active\n",
276 batman_if->dev);
5beef3c9 277
208e13e4
ML
278 /* begin scheduling originator messages on that interface */
279 schedule_own_packet(batman_if);
5beef3c9 280
208e13e4
ML
281out:
282 return 0;
5beef3c9 283
208e13e4
ML
284err:
285 return -ENOMEM;
5beef3c9
AL
286}
287
208e13e4 288void hardif_disable_interface(struct batman_if *batman_if)
5beef3c9 289{
208e13e4
ML
290 /* FIXME: each batman_if will be attached to a softif */
291 struct bat_priv *bat_priv = netdev_priv(soft_device);
5beef3c9 292
208e13e4
ML
293 if (batman_if->if_status == IF_ACTIVE)
294 hardif_deactivate_interface(batman_if);
5beef3c9 295
208e13e4
ML
296 if (batman_if->if_status != IF_INACTIVE)
297 return;
5beef3c9 298
208e13e4
ML
299 printk(KERN_INFO "batman-adv:Removing interface: %s\n", batman_if->dev);
300 bat_priv->num_ifaces--;
301 orig_hash_del_if(batman_if, bat_priv->num_ifaces);
5beef3c9 302
208e13e4
ML
303 if (batman_if == bat_priv->primary_if)
304 set_primary_if(bat_priv, get_active_batman_if());
5beef3c9 305
208e13e4
ML
306 kfree(batman_if->packet_buff);
307 batman_if->packet_buff = NULL;
308 batman_if->if_status = IF_NOT_IN_USE;
5beef3c9 309
208e13e4
ML
310 if ((atomic_read(&module_state) == MODULE_ACTIVE) &&
311 (bat_priv->num_ifaces == 0))
312 deactivate_module();
5beef3c9
AL
313}
314
208e13e4 315static struct batman_if *hardif_add_interface(struct net_device *net_dev)
5beef3c9
AL
316{
317 struct batman_if *batman_if;
208e13e4 318 int ret;
5beef3c9 319
208e13e4
ML
320 ret = is_valid_iface(net_dev);
321 if (ret != 1)
322 goto out;
5beef3c9 323
208e13e4 324 batman_if = kmalloc(sizeof(struct batman_if), GFP_ATOMIC);
5beef3c9 325 if (!batman_if) {
6d45d8df
SE
326 printk(KERN_ERR "batman-adv:"
327 "Can't add interface (%s): out of memory\n",
208e13e4 328 net_dev->name);
5beef3c9
AL
329 goto out;
330 }
331
208e13e4
ML
332 batman_if->dev = kstrdup(net_dev->name, GFP_ATOMIC);
333 if (!batman_if->dev)
334 goto free_if;
5beef3c9 335
208e13e4
ML
336 ret = sysfs_add_hardif(&batman_if->hardif_obj, net_dev);
337 if (ret)
338 goto free_dev;
5beef3c9 339
208e13e4
ML
340 batman_if->if_num = -1;
341 batman_if->net_dev = net_dev;
342 batman_if->if_status = IF_NOT_IN_USE;
5beef3c9
AL
343 INIT_LIST_HEAD(&batman_if->list);
344
208e13e4
ML
345 check_known_mac_addr(batman_if->net_dev->dev_addr);
346 list_add_tail_rcu(&batman_if->list, &if_list);
347 return batman_if;
5beef3c9 348
208e13e4
ML
349free_dev:
350 kfree(batman_if->dev);
351free_if:
352 kfree(batman_if);
353out:
354 return NULL;
355}
5beef3c9 356
208e13e4
ML
357static void hardif_free_interface(struct rcu_head *rcu)
358{
359 struct batman_if *batman_if = container_of(rcu, struct batman_if, rcu);
5beef3c9 360
208e13e4
ML
361 /* delete all references to this batman_if */
362 purge_orig(NULL);
363 purge_outstanding_packets(batman_if);
5beef3c9 364
208e13e4
ML
365 kfree(batman_if->dev);
366 kfree(batman_if);
367}
5beef3c9 368
208e13e4
ML
369static void hardif_remove_interface(struct batman_if *batman_if)
370{
371 /* first deactivate interface */
372 if (batman_if->if_status != IF_NOT_IN_USE)
373 hardif_disable_interface(batman_if);
5beef3c9 374
208e13e4
ML
375 if (batman_if->if_status != IF_NOT_IN_USE)
376 return;
5beef3c9 377
208e13e4
ML
378 batman_if->if_status = IF_TO_BE_REMOVED;
379 list_del_rcu(&batman_if->list);
380 sysfs_del_hardif(&batman_if->hardif_obj);
381 call_rcu(&batman_if->rcu, hardif_free_interface);
5beef3c9
AL
382}
383
208e13e4 384void hardif_remove_interfaces(void)
5beef3c9 385{
208e13e4
ML
386 struct batman_if *batman_if, *batman_if_tmp;
387
388 list_for_each_entry_safe(batman_if, batman_if_tmp, &if_list, list)
389 hardif_remove_interface(batman_if);
5beef3c9
AL
390}
391
392static int hard_if_event(struct notifier_block *this,
208e13e4 393 unsigned long event, void *ptr)
5beef3c9 394{
208e13e4
ML
395 struct net_device *net_dev = (struct net_device *)ptr;
396 struct batman_if *batman_if = get_batman_if_by_netdev(net_dev);
397 /* FIXME: each batman_if will be attached to a softif */
398 struct bat_priv *bat_priv = netdev_priv(soft_device);
399
400 if (!batman_if)
401 batman_if = hardif_add_interface(net_dev);
5beef3c9
AL
402
403 if (!batman_if)
404 goto out;
405
406 switch (event) {
208e13e4
ML
407 case NETDEV_REGISTER:
408 break;
409 case NETDEV_UP:
410 hardif_activate_interface(bat_priv, batman_if);
411 break;
5beef3c9
AL
412 case NETDEV_GOING_DOWN:
413 case NETDEV_DOWN:
5beef3c9
AL
414 hardif_deactivate_interface(batman_if);
415 break;
208e13e4
ML
416 case NETDEV_UNREGISTER:
417 hardif_remove_interface(batman_if);
418 break;
419 case NETDEV_CHANGENAME:
420 break;
421 case NETDEV_CHANGEADDR:
422 check_known_mac_addr(batman_if->net_dev->dev_addr);
423 update_mac_addresses(batman_if);
424 if (batman_if == bat_priv->primary_if)
425 set_primary_if(bat_priv, batman_if);
5beef3c9 426 break;
5beef3c9 427 default:
5beef3c9
AL
428 break;
429 };
430
5beef3c9
AL
431out:
432 return NOTIFY_DONE;
433}
434
e7017195
SW
435/* receive a packet with the batman ethertype coming on a hard
436 * interface */
437int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
438 struct packet_type *ptype, struct net_device *orig_dev)
439{
440 struct batman_packet *batman_packet;
441 struct batman_if *batman_if;
442 struct net_device_stats *stats;
443 int ret;
444
da6d6c7a 445 skb = skb_share_check(skb, GFP_ATOMIC);
e7017195 446
da6d6c7a
ML
447 /* skb was released by skb_share_check() */
448 if (!skb)
449 goto err_out;
e7017195 450
af71b816
ML
451 if (atomic_read(&module_state) != MODULE_ACTIVE)
452 goto err_free;
453
e7017195
SW
454 /* packet should hold at least type and version */
455 if (unlikely(skb_headlen(skb) < 2))
456 goto err_free;
457
458 /* expect a valid ethernet header here. */
459 if (unlikely(skb->mac_len != sizeof(struct ethhdr)
460 || !skb_mac_header(skb)))
461 goto err_free;
462
208e13e4 463 batman_if = get_batman_if_by_netdev(skb->dev);
e7017195
SW
464 if (!batman_if)
465 goto err_free;
466
af71b816 467 /* discard frames on not active interfaces */
208e13e4 468 if (batman_if->if_status != IF_ACTIVE)
af71b816
ML
469 goto err_free;
470
da6d6c7a 471 stats = (struct net_device_stats *)dev_get_stats(skb->dev);
b9b27e4e
AL
472 if (stats) {
473 stats->rx_packets++;
474 stats->rx_bytes += skb->len;
475 }
e7017195
SW
476
477 batman_packet = (struct batman_packet *)skb->data;
478
479 if (batman_packet->version != COMPAT_VERSION) {
480 bat_dbg(DBG_BATMAN,
481 "Drop packet: incompatible batman version (%i)\n",
482 batman_packet->version);
483 goto err_free;
484 }
485
486 /* all receive handlers return whether they received or reused
487 * the supplied skb. if not, we have to free the skb. */
488
489 switch (batman_packet->packet_type) {
490 /* batman originator packet */
491 case BAT_PACKET:
492 ret = recv_bat_packet(skb, batman_if);
493 break;
494
495 /* batman icmp packet */
496 case BAT_ICMP:
497 ret = recv_icmp_packet(skb);
498 break;
499
500 /* unicast packet */
501 case BAT_UNICAST:
502 ret = recv_unicast_packet(skb);
503 break;
504
505 /* broadcast packet */
506 case BAT_BCAST:
507 ret = recv_bcast_packet(skb);
508 break;
509
510 /* vis packet */
511 case BAT_VIS:
512 ret = recv_vis_packet(skb);
513 break;
514 default:
515 ret = NET_RX_DROP;
516 }
da6d6c7a 517
e7017195
SW
518 if (ret == NET_RX_DROP)
519 kfree_skb(skb);
520
521 /* return NET_RX_SUCCESS in any case as we
522 * most probably dropped the packet for
523 * routing-logical reasons. */
524
525 return NET_RX_SUCCESS;
526
527err_free:
da6d6c7a
ML
528 kfree_skb(skb);
529err_out:
530 return NET_RX_DROP;
e7017195
SW
531}
532
533
5beef3c9 534struct notifier_block hard_if_notifier = {
bad2239e 535 .notifier_call = hard_if_event,
5beef3c9 536};