]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/batman-adv/hard-interface.c
Revert: "Staging: batman-adv: Adding netfilter-bridge hooks"
[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);
e35fd5ec 111 batman_packet->flags = PRIMARIES_FIRST_HOP;
208e13e4
ML
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{
f86b9984
SE
131 if (!batman_if || !batman_if->packet_buff)
132 return;
133
208e13e4
ML
134 addr_to_string(batman_if->addr_str, batman_if->net_dev->dev_addr);
135
136 memcpy(((struct batman_packet *)(batman_if->packet_buff))->orig,
137 batman_if->net_dev->dev_addr, ETH_ALEN);
138 memcpy(((struct batman_packet *)(batman_if->packet_buff))->prev_sender,
139 batman_if->net_dev->dev_addr, ETH_ALEN);
5beef3c9
AL
140}
141
142static void check_known_mac_addr(uint8_t *addr)
143{
144 struct batman_if *batman_if;
5beef3c9
AL
145
146 rcu_read_lock();
147 list_for_each_entry_rcu(batman_if, &if_list, list) {
208e13e4
ML
148 if ((batman_if->if_status != IF_ACTIVE) &&
149 (batman_if->if_status != IF_TO_BE_ACTIVATED))
5beef3c9
AL
150 continue;
151
152 if (!compare_orig(batman_if->net_dev->dev_addr, addr))
153 continue;
154
c1641862
SE
155 pr_warning("The newly added mac address (%pM) already exists "
156 "on: %s\n", addr, batman_if->dev);
157 pr_warning("It is strongly recommended to keep mac addresses "
158 "unique 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
c1641862
SE
192static void hardif_activate_interface(struct net_device *net_dev,
193 struct bat_priv *bat_priv,
208e13e4 194 struct batman_if *batman_if)
5beef3c9 195{
208e13e4
ML
196 if (batman_if->if_status != IF_INACTIVE)
197 return;
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
c1641862 209 bat_info(net_dev, "Interface activated: %s\n", batman_if->dev);
5beef3c9 210
208e13e4
ML
211 if (atomic_read(&module_state) == MODULE_INACTIVE)
212 activate_module();
5beef3c9 213
208e13e4
ML
214 update_min_mtu();
215 return;
5beef3c9
AL
216}
217
c1641862
SE
218static void hardif_deactivate_interface(struct net_device *net_dev,
219 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 batman_if->if_status = IF_INACTIVE;
5beef3c9 226
c1641862 227 bat_info(net_dev, "Interface deactivated: %s\n", batman_if->dev);
208e13e4
ML
228
229 update_min_mtu();
5beef3c9
AL
230}
231
208e13e4 232int hardif_enable_interface(struct batman_if *batman_if)
5beef3c9 233{
208e13e4
ML
234 /* FIXME: each batman_if will be attached to a softif */
235 struct bat_priv *bat_priv = netdev_priv(soft_device);
236 struct batman_packet *batman_packet;
5beef3c9 237
208e13e4
ML
238 if (batman_if->if_status != IF_NOT_IN_USE)
239 goto out;
5beef3c9 240
208e13e4
ML
241 batman_if->packet_len = BAT_PACKET_LEN;
242 batman_if->packet_buff = kmalloc(batman_if->packet_len, GFP_ATOMIC);
5beef3c9 243
208e13e4 244 if (!batman_if->packet_buff) {
c1641862
SE
245 bat_err(soft_device, "Can't add interface packet (%s): "
246 "out of memory\n", batman_if->dev);
208e13e4
ML
247 goto err;
248 }
5beef3c9 249
208e13e4
ML
250 batman_packet = (struct batman_packet *)(batman_if->packet_buff);
251 batman_packet->packet_type = BAT_PACKET;
252 batman_packet->version = COMPAT_VERSION;
253 batman_packet->flags = 0;
254 batman_packet->ttl = 2;
255 batman_packet->tq = TQ_MAX_VALUE;
256 batman_packet->num_hna = 0;
5beef3c9 257
208e13e4
ML
258 batman_if->if_num = bat_priv->num_ifaces;
259 bat_priv->num_ifaces++;
260 batman_if->if_status = IF_INACTIVE;
261 orig_hash_add_if(batman_if, bat_priv->num_ifaces);
5beef3c9 262
208e13e4 263 atomic_set(&batman_if->seqno, 1);
c1641862 264 bat_info(soft_device, "Adding interface: %s\n", batman_if->dev);
5beef3c9 265
208e13e4 266 if (hardif_is_iface_up(batman_if))
c1641862 267 hardif_activate_interface(soft_device, bat_priv, batman_if);
208e13e4 268 else
c1641862
SE
269 bat_err(soft_device, "Not using interface %s "
270 "(retrying later): interface not active\n",
271 batman_if->dev);
5beef3c9 272
208e13e4
ML
273 /* begin scheduling originator messages on that interface */
274 schedule_own_packet(batman_if);
5beef3c9 275
208e13e4
ML
276out:
277 return 0;
5beef3c9 278
208e13e4
ML
279err:
280 return -ENOMEM;
5beef3c9
AL
281}
282
208e13e4 283void hardif_disable_interface(struct batman_if *batman_if)
5beef3c9 284{
208e13e4
ML
285 /* FIXME: each batman_if will be attached to a softif */
286 struct bat_priv *bat_priv = netdev_priv(soft_device);
5beef3c9 287
208e13e4 288 if (batman_if->if_status == IF_ACTIVE)
c1641862 289 hardif_deactivate_interface(soft_device, batman_if);
5beef3c9 290
208e13e4
ML
291 if (batman_if->if_status != IF_INACTIVE)
292 return;
5beef3c9 293
c1641862 294 bat_info(soft_device, "Removing interface: %s\n", batman_if->dev);
208e13e4
ML
295 bat_priv->num_ifaces--;
296 orig_hash_del_if(batman_if, bat_priv->num_ifaces);
5beef3c9 297
208e13e4
ML
298 if (batman_if == bat_priv->primary_if)
299 set_primary_if(bat_priv, get_active_batman_if());
5beef3c9 300
208e13e4
ML
301 kfree(batman_if->packet_buff);
302 batman_if->packet_buff = NULL;
303 batman_if->if_status = IF_NOT_IN_USE;
5beef3c9 304
208e13e4
ML
305 if ((atomic_read(&module_state) == MODULE_ACTIVE) &&
306 (bat_priv->num_ifaces == 0))
307 deactivate_module();
5beef3c9
AL
308}
309
208e13e4 310static struct batman_if *hardif_add_interface(struct net_device *net_dev)
5beef3c9
AL
311{
312 struct batman_if *batman_if;
208e13e4 313 int ret;
5beef3c9 314
208e13e4
ML
315 ret = is_valid_iface(net_dev);
316 if (ret != 1)
317 goto out;
5beef3c9 318
51a00eaf
SE
319 dev_hold(net_dev);
320
208e13e4 321 batman_if = kmalloc(sizeof(struct batman_if), GFP_ATOMIC);
5beef3c9 322 if (!batman_if) {
c1641862 323 pr_err("Can't add interface (%s): out of memory\n",
208e13e4 324 net_dev->name);
51a00eaf 325 goto release_dev;
5beef3c9
AL
326 }
327
208e13e4
ML
328 batman_if->dev = kstrdup(net_dev->name, GFP_ATOMIC);
329 if (!batman_if->dev)
330 goto free_if;
5beef3c9 331
208e13e4
ML
332 ret = sysfs_add_hardif(&batman_if->hardif_obj, net_dev);
333 if (ret)
334 goto free_dev;
5beef3c9 335
208e13e4
ML
336 batman_if->if_num = -1;
337 batman_if->net_dev = net_dev;
338 batman_if->if_status = IF_NOT_IN_USE;
f86b9984 339 batman_if->packet_buff = NULL;
5beef3c9
AL
340 INIT_LIST_HEAD(&batman_if->list);
341
208e13e4
ML
342 check_known_mac_addr(batman_if->net_dev->dev_addr);
343 list_add_tail_rcu(&batman_if->list, &if_list);
344 return batman_if;
5beef3c9 345
208e13e4
ML
346free_dev:
347 kfree(batman_if->dev);
348free_if:
349 kfree(batman_if);
51a00eaf
SE
350release_dev:
351 dev_put(net_dev);
208e13e4
ML
352out:
353 return NULL;
354}
5beef3c9 355
208e13e4
ML
356static void hardif_free_interface(struct rcu_head *rcu)
357{
358 struct batman_if *batman_if = container_of(rcu, struct batman_if, rcu);
5beef3c9 359
208e13e4
ML
360 /* delete all references to this batman_if */
361 purge_orig(NULL);
362 purge_outstanding_packets(batman_if);
5beef3c9 363
208e13e4
ML
364 kfree(batman_if->dev);
365 kfree(batman_if);
366}
5beef3c9 367
208e13e4
ML
368static void hardif_remove_interface(struct batman_if *batman_if)
369{
370 /* first deactivate interface */
371 if (batman_if->if_status != IF_NOT_IN_USE)
372 hardif_disable_interface(batman_if);
5beef3c9 373
208e13e4
ML
374 if (batman_if->if_status != IF_NOT_IN_USE)
375 return;
5beef3c9 376
208e13e4
ML
377 batman_if->if_status = IF_TO_BE_REMOVED;
378 list_del_rcu(&batman_if->list);
379 sysfs_del_hardif(&batman_if->hardif_obj);
51a00eaf 380 dev_put(batman_if->net_dev);
208e13e4 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
1189f130
SE
400 if (!batman_if && event == NETDEV_REGISTER)
401 batman_if = hardif_add_interface(net_dev);
5beef3c9
AL
402
403 if (!batman_if)
404 goto out;
405
406 switch (event) {
208e13e4 407 case NETDEV_UP:
c1641862 408 hardif_activate_interface(soft_device, bat_priv, batman_if);
208e13e4 409 break;
5beef3c9
AL
410 case NETDEV_GOING_DOWN:
411 case NETDEV_DOWN:
c1641862 412 hardif_deactivate_interface(soft_device, batman_if);
5beef3c9 413 break;
208e13e4
ML
414 case NETDEV_UNREGISTER:
415 hardif_remove_interface(batman_if);
416 break;
417 case NETDEV_CHANGENAME:
418 break;
419 case NETDEV_CHANGEADDR:
420 check_known_mac_addr(batman_if->net_dev->dev_addr);
421 update_mac_addresses(batman_if);
422 if (batman_if == bat_priv->primary_if)
423 set_primary_if(bat_priv, batman_if);
5beef3c9 424 break;
5beef3c9 425 default:
5beef3c9
AL
426 break;
427 };
428
5beef3c9
AL
429out:
430 return NOTIFY_DONE;
431}
432
e7017195
SW
433/* receive a packet with the batman ethertype coming on a hard
434 * interface */
435int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
436 struct packet_type *ptype, struct net_device *orig_dev)
437{
84ec0864
ML
438 /* FIXME: each orig_node->batman_if will be attached to a softif */
439 struct bat_priv *bat_priv = netdev_priv(soft_device);
e7017195
SW
440 struct batman_packet *batman_packet;
441 struct batman_if *batman_if;
e7017195
SW
442 int ret;
443
da6d6c7a 444 skb = skb_share_check(skb, GFP_ATOMIC);
e7017195 445
da6d6c7a
ML
446 /* skb was released by skb_share_check() */
447 if (!skb)
448 goto err_out;
e7017195 449
af71b816
ML
450 if (atomic_read(&module_state) != MODULE_ACTIVE)
451 goto err_free;
452
e7017195
SW
453 /* packet should hold at least type and version */
454 if (unlikely(skb_headlen(skb) < 2))
455 goto err_free;
456
457 /* expect a valid ethernet header here. */
458 if (unlikely(skb->mac_len != sizeof(struct ethhdr)
459 || !skb_mac_header(skb)))
460 goto err_free;
461
208e13e4 462 batman_if = get_batman_if_by_netdev(skb->dev);
e7017195
SW
463 if (!batman_if)
464 goto err_free;
465
af71b816 466 /* discard frames on not active interfaces */
208e13e4 467 if (batman_if->if_status != IF_ACTIVE)
af71b816
ML
468 goto err_free;
469
e7017195
SW
470 batman_packet = (struct batman_packet *)skb->data;
471
472 if (batman_packet->version != COMPAT_VERSION) {
84ec0864 473 bat_dbg(DBG_BATMAN, bat_priv,
e7017195
SW
474 "Drop packet: incompatible batman version (%i)\n",
475 batman_packet->version);
476 goto err_free;
477 }
478
479 /* all receive handlers return whether they received or reused
480 * the supplied skb. if not, we have to free the skb. */
481
482 switch (batman_packet->packet_type) {
483 /* batman originator packet */
484 case BAT_PACKET:
485 ret = recv_bat_packet(skb, batman_if);
486 break;
487
488 /* batman icmp packet */
489 case BAT_ICMP:
490 ret = recv_icmp_packet(skb);
491 break;
492
493 /* unicast packet */
494 case BAT_UNICAST:
11f79dec 495 ret = recv_unicast_packet(skb, batman_if);
e7017195
SW
496 break;
497
498 /* broadcast packet */
499 case BAT_BCAST:
500 ret = recv_bcast_packet(skb);
501 break;
502
503 /* vis packet */
504 case BAT_VIS:
505 ret = recv_vis_packet(skb);
506 break;
507 default:
508 ret = NET_RX_DROP;
509 }
da6d6c7a 510
e7017195
SW
511 if (ret == NET_RX_DROP)
512 kfree_skb(skb);
513
514 /* return NET_RX_SUCCESS in any case as we
515 * most probably dropped the packet for
516 * routing-logical reasons. */
517
518 return NET_RX_SUCCESS;
519
520err_free:
da6d6c7a
ML
521 kfree_skb(skb);
522err_out:
523 return NET_RX_DROP;
e7017195
SW
524}
525
5beef3c9 526struct notifier_block hard_if_notifier = {
bad2239e 527 .notifier_call = hard_if_event,
5beef3c9 528};