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