Staging: batman-adv: Track references of batman_if in set_primary_if
[pandora-kernel.git] / drivers / staging / batman-adv / hard-interface.c
1 /*
2  * Copyright (C) 2007-2010 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"
24 #include "soft-interface.h"
25 #include "send.h"
26 #include "translation-table.h"
27 #include "routing.h"
28 #include "bat_sysfs.h"
29 #include "originator.h"
30 #include "hash.h"
31
32 #include <linux/if_arp.h>
33
34 #define MIN(x, y) ((x) < (y) ? (x) : (y))
35
36 /* protect update critical side of if_list - but not the content */
37 static DEFINE_SPINLOCK(if_list_lock);
38
39 struct batman_if *get_batman_if_by_netdev(struct net_device *net_dev)
40 {
41         struct batman_if *batman_if;
42
43         rcu_read_lock();
44         list_for_each_entry_rcu(batman_if, &if_list, list) {
45                 if (batman_if->net_dev == net_dev)
46                         goto out;
47         }
48
49         batman_if = NULL;
50
51 out:
52         if (batman_if)
53                 hardif_hold(batman_if);
54
55         rcu_read_unlock();
56         return batman_if;
57 }
58
59 static int is_valid_iface(struct net_device *net_dev)
60 {
61         if (net_dev->flags & IFF_LOOPBACK)
62                 return 0;
63
64         if (net_dev->type != ARPHRD_ETHER)
65                 return 0;
66
67         if (net_dev->addr_len != ETH_ALEN)
68                 return 0;
69
70         /* no batman over batman */
71 #ifdef HAVE_NET_DEVICE_OPS
72         if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
73                 return 0;
74 #else
75         if (net_dev->hard_start_xmit == interface_tx)
76                 return 0;
77 #endif
78
79         /* Device is being bridged */
80         /* if (net_dev->priv_flags & IFF_BRIDGE_PORT)
81                 return 0; */
82
83         return 1;
84 }
85
86 static struct batman_if *get_active_batman_if(struct net_device *soft_iface)
87 {
88         struct batman_if *batman_if;
89
90         rcu_read_lock();
91         list_for_each_entry_rcu(batman_if, &if_list, list) {
92                 if (batman_if->soft_iface != soft_iface)
93                         continue;
94
95                 if (batman_if->if_status == IF_ACTIVE)
96                         goto out;
97         }
98
99         batman_if = NULL;
100
101 out:
102         if (batman_if)
103                 hardif_hold(batman_if);
104
105         rcu_read_unlock();
106         return batman_if;
107 }
108
109 static void set_primary_if(struct bat_priv *bat_priv,
110                            struct batman_if *batman_if)
111 {
112         struct batman_packet *batman_packet;
113         struct vis_packet *vis_packet;
114         struct batman_if *old_if;
115
116         if (batman_if)
117                 hardif_hold(batman_if);
118
119         old_if = bat_priv->primary_if;
120         bat_priv->primary_if = batman_if;
121
122         if (old_if)
123                 hardif_put(old_if);
124
125         if (!bat_priv->primary_if)
126                 return;
127
128         batman_packet = (struct batman_packet *)(batman_if->packet_buff);
129         batman_packet->flags = PRIMARIES_FIRST_HOP;
130         batman_packet->ttl = TTL;
131
132         vis_packet = (struct vis_packet *)
133                                 bat_priv->my_vis_info->skb_packet->data;
134         memcpy(vis_packet->vis_orig,
135                bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
136         memcpy(vis_packet->sender_orig,
137                bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
138
139         /***
140          * hacky trick to make sure that we send the HNA information via
141          * our new primary interface
142          */
143         atomic_set(&bat_priv->hna_local_changed, 1);
144 }
145
146 static bool hardif_is_iface_up(struct batman_if *batman_if)
147 {
148         if (batman_if->net_dev->flags & IFF_UP)
149                 return true;
150
151         return false;
152 }
153
154 static void update_mac_addresses(struct batman_if *batman_if)
155 {
156         addr_to_string(batman_if->addr_str, batman_if->net_dev->dev_addr);
157
158         memcpy(((struct batman_packet *)(batman_if->packet_buff))->orig,
159                batman_if->net_dev->dev_addr, ETH_ALEN);
160         memcpy(((struct batman_packet *)(batman_if->packet_buff))->prev_sender,
161                batman_if->net_dev->dev_addr, ETH_ALEN);
162 }
163
164 static void check_known_mac_addr(uint8_t *addr)
165 {
166         struct batman_if *batman_if;
167
168         rcu_read_lock();
169         list_for_each_entry_rcu(batman_if, &if_list, list) {
170                 if ((batman_if->if_status != IF_ACTIVE) &&
171                     (batman_if->if_status != IF_TO_BE_ACTIVATED))
172                         continue;
173
174                 if (!compare_orig(batman_if->net_dev->dev_addr, addr))
175                         continue;
176
177                 pr_warning("The newly added mac address (%pM) already exists "
178                            "on: %s\n", addr, batman_if->net_dev->name);
179                 pr_warning("It is strongly recommended to keep mac addresses "
180                            "unique to avoid problems!\n");
181         }
182         rcu_read_unlock();
183 }
184
185 int hardif_min_mtu(struct net_device *soft_iface)
186 {
187         struct bat_priv *bat_priv = netdev_priv(soft_iface);
188         struct batman_if *batman_if;
189         /* allow big frames if all devices are capable to do so
190          * (have MTU > 1500 + BAT_HEADER_LEN) */
191         int min_mtu = ETH_DATA_LEN;
192
193         if (atomic_read(&bat_priv->frag_enabled))
194                 goto out;
195
196         rcu_read_lock();
197         list_for_each_entry_rcu(batman_if, &if_list, list) {
198                 if ((batman_if->if_status != IF_ACTIVE) &&
199                     (batman_if->if_status != IF_TO_BE_ACTIVATED))
200                         continue;
201
202                 if (batman_if->soft_iface != soft_iface)
203                         continue;
204
205                 min_mtu = MIN(batman_if->net_dev->mtu - BAT_HEADER_LEN,
206                               min_mtu);
207         }
208         rcu_read_unlock();
209 out:
210         return min_mtu;
211 }
212
213 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
214 void update_min_mtu(struct net_device *soft_iface)
215 {
216         int min_mtu;
217
218         min_mtu = hardif_min_mtu(soft_iface);
219         if (soft_iface->mtu != min_mtu)
220                 soft_iface->mtu = min_mtu;
221 }
222
223 static void hardif_activate_interface(struct batman_if *batman_if)
224 {
225         struct bat_priv *bat_priv;
226
227         if (batman_if->if_status != IF_INACTIVE)
228                 return;
229
230         bat_priv = netdev_priv(batman_if->soft_iface);
231
232         update_mac_addresses(batman_if);
233         batman_if->if_status = IF_TO_BE_ACTIVATED;
234
235         /**
236          * the first active interface becomes our primary interface or
237          * the next active interface after the old primay interface was removed
238          */
239         if (!bat_priv->primary_if)
240                 set_primary_if(bat_priv, batman_if);
241
242         bat_info(batman_if->soft_iface, "Interface activated: %s\n",
243                  batman_if->net_dev->name);
244
245         update_min_mtu(batman_if->soft_iface);
246         return;
247 }
248
249 static void hardif_deactivate_interface(struct batman_if *batman_if)
250 {
251         if ((batman_if->if_status != IF_ACTIVE) &&
252            (batman_if->if_status != IF_TO_BE_ACTIVATED))
253                 return;
254
255         batman_if->if_status = IF_INACTIVE;
256
257         bat_info(batman_if->soft_iface, "Interface deactivated: %s\n",
258                  batman_if->net_dev->name);
259
260         update_min_mtu(batman_if->soft_iface);
261 }
262
263 int hardif_enable_interface(struct batman_if *batman_if, char *iface_name)
264 {
265         struct bat_priv *bat_priv;
266         struct batman_packet *batman_packet;
267
268         if (batman_if->if_status != IF_NOT_IN_USE)
269                 goto out;
270
271         batman_if->soft_iface = dev_get_by_name(&init_net, iface_name);
272
273         if (!batman_if->soft_iface) {
274                 batman_if->soft_iface = softif_create(iface_name);
275
276                 if (!batman_if->soft_iface)
277                         goto err;
278
279                 /* dev_get_by_name() increases the reference counter for us */
280                 dev_hold(batman_if->soft_iface);
281         }
282
283         bat_priv = netdev_priv(batman_if->soft_iface);
284         batman_if->packet_len = BAT_PACKET_LEN;
285         batman_if->packet_buff = kmalloc(batman_if->packet_len, GFP_ATOMIC);
286
287         if (!batman_if->packet_buff) {
288                 bat_err(batman_if->soft_iface, "Can't add interface packet "
289                         "(%s): out of memory\n", batman_if->net_dev->name);
290                 goto err;
291         }
292
293         batman_packet = (struct batman_packet *)(batman_if->packet_buff);
294         batman_packet->packet_type = BAT_PACKET;
295         batman_packet->version = COMPAT_VERSION;
296         batman_packet->flags = 0;
297         batman_packet->ttl = 2;
298         batman_packet->tq = TQ_MAX_VALUE;
299         batman_packet->num_hna = 0;
300
301         batman_if->if_num = bat_priv->num_ifaces;
302         bat_priv->num_ifaces++;
303         batman_if->if_status = IF_INACTIVE;
304         orig_hash_add_if(batman_if, bat_priv->num_ifaces);
305
306         batman_if->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
307         batman_if->batman_adv_ptype.func = batman_skb_recv;
308         batman_if->batman_adv_ptype.dev = batman_if->net_dev;
309         hardif_hold(batman_if);
310         dev_add_pack(&batman_if->batman_adv_ptype);
311
312         atomic_set(&batman_if->seqno, 1);
313         atomic_set(&batman_if->frag_seqno, 1);
314         bat_info(batman_if->soft_iface, "Adding interface: %s\n",
315                  batman_if->net_dev->name);
316
317         if (atomic_read(&bat_priv->frag_enabled) && batman_if->net_dev->mtu <
318                 ETH_DATA_LEN + BAT_HEADER_LEN)
319                 bat_info(batman_if->soft_iface,
320                         "The MTU of interface %s is too small (%i) to handle "
321                         "the transport of batman-adv packets. Packets going "
322                         "over this interface will be fragmented on layer2 "
323                         "which could impact the performance. Setting the MTU "
324                         "to %zi would solve the problem.\n",
325                         batman_if->net_dev->name, batman_if->net_dev->mtu,
326                         ETH_DATA_LEN + BAT_HEADER_LEN);
327
328         if (!atomic_read(&bat_priv->frag_enabled) && batman_if->net_dev->mtu <
329                 ETH_DATA_LEN + BAT_HEADER_LEN)
330                 bat_info(batman_if->soft_iface,
331                         "The MTU of interface %s is too small (%i) to handle "
332                         "the transport of batman-adv packets. If you experience"
333                         " problems getting traffic through try increasing the "
334                         "MTU to %zi.\n",
335                         batman_if->net_dev->name, batman_if->net_dev->mtu,
336                         ETH_DATA_LEN + BAT_HEADER_LEN);
337
338         if (hardif_is_iface_up(batman_if))
339                 hardif_activate_interface(batman_if);
340         else
341                 bat_err(batman_if->soft_iface, "Not using interface %s "
342                         "(retrying later): interface not active\n",
343                         batman_if->net_dev->name);
344
345         /* begin scheduling originator messages on that interface */
346         schedule_own_packet(batman_if);
347
348 out:
349         return 0;
350
351 err:
352         return -ENOMEM;
353 }
354
355 void hardif_disable_interface(struct batman_if *batman_if)
356 {
357         struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
358
359         if (batman_if->if_status == IF_ACTIVE)
360                 hardif_deactivate_interface(batman_if);
361
362         if (batman_if->if_status != IF_INACTIVE)
363                 return;
364
365         bat_info(batman_if->soft_iface, "Removing interface: %s\n",
366                  batman_if->net_dev->name);
367         dev_remove_pack(&batman_if->batman_adv_ptype);
368         hardif_put(batman_if);
369
370         bat_priv->num_ifaces--;
371         orig_hash_del_if(batman_if, bat_priv->num_ifaces);
372
373         if (batman_if == bat_priv->primary_if) {
374                 struct batman_if *new_if;
375
376                 new_if = get_active_batman_if(batman_if->soft_iface);
377                 set_primary_if(bat_priv, new_if);
378
379                 if (new_if)
380                         hardif_put(new_if);
381         }
382
383         kfree(batman_if->packet_buff);
384         batman_if->packet_buff = NULL;
385         batman_if->if_status = IF_NOT_IN_USE;
386
387         /* delete all references to this batman_if */
388         purge_orig_ref(bat_priv);
389         purge_outstanding_packets(bat_priv, batman_if);
390         dev_put(batman_if->soft_iface);
391
392         /* nobody uses this interface anymore */
393         if (!bat_priv->num_ifaces)
394                 softif_destroy(batman_if->soft_iface);
395
396         batman_if->soft_iface = NULL;
397 }
398
399 static struct batman_if *hardif_add_interface(struct net_device *net_dev)
400 {
401         struct batman_if *batman_if;
402         int ret;
403
404         ret = is_valid_iface(net_dev);
405         if (ret != 1)
406                 goto out;
407
408         dev_hold(net_dev);
409
410         batman_if = kmalloc(sizeof(struct batman_if), GFP_ATOMIC);
411         if (!batman_if) {
412                 pr_err("Can't add interface (%s): out of memory\n",
413                        net_dev->name);
414                 goto release_dev;
415         }
416
417         ret = sysfs_add_hardif(&batman_if->hardif_obj, net_dev);
418         if (ret)
419                 goto free_if;
420
421         batman_if->if_num = -1;
422         batman_if->net_dev = net_dev;
423         batman_if->soft_iface = NULL;
424         batman_if->if_status = IF_NOT_IN_USE;
425         INIT_LIST_HEAD(&batman_if->list);
426         atomic_set(&batman_if->refcnt, 0);
427         hardif_hold(batman_if);
428
429         check_known_mac_addr(batman_if->net_dev->dev_addr);
430
431         spin_lock(&if_list_lock);
432         list_add_tail_rcu(&batman_if->list, &if_list);
433         spin_unlock(&if_list_lock);
434
435         /* extra reference for return */
436         hardif_hold(batman_if);
437         return batman_if;
438
439 free_if:
440         kfree(batman_if);
441 release_dev:
442         dev_put(net_dev);
443 out:
444         return NULL;
445 }
446
447 static void hardif_remove_interface(struct batman_if *batman_if)
448 {
449         /* first deactivate interface */
450         if (batman_if->if_status != IF_NOT_IN_USE)
451                 hardif_disable_interface(batman_if);
452
453         if (batman_if->if_status != IF_NOT_IN_USE)
454                 return;
455
456         batman_if->if_status = IF_TO_BE_REMOVED;
457
458         /* caller must take if_list_lock */
459         list_del_rcu(&batman_if->list);
460         synchronize_rcu();
461         sysfs_del_hardif(&batman_if->hardif_obj);
462         hardif_put(batman_if);
463 }
464
465 void hardif_remove_interfaces(void)
466 {
467         struct batman_if *batman_if, *batman_if_tmp;
468
469         rtnl_lock();
470         spin_lock(&if_list_lock);
471         list_for_each_entry_safe(batman_if, batman_if_tmp, &if_list, list) {
472                 hardif_remove_interface(batman_if);
473         }
474         spin_unlock(&if_list_lock);
475         rtnl_unlock();
476 }
477
478 static int hard_if_event(struct notifier_block *this,
479                          unsigned long event, void *ptr)
480 {
481         struct net_device *net_dev = (struct net_device *)ptr;
482         struct batman_if *batman_if = get_batman_if_by_netdev(net_dev);
483         struct bat_priv *bat_priv;
484
485         if (!batman_if && event == NETDEV_REGISTER)
486                 batman_if = hardif_add_interface(net_dev);
487
488         if (!batman_if)
489                 goto out;
490
491         switch (event) {
492         case NETDEV_UP:
493                 hardif_activate_interface(batman_if);
494                 break;
495         case NETDEV_GOING_DOWN:
496         case NETDEV_DOWN:
497                 hardif_deactivate_interface(batman_if);
498                 break;
499         case NETDEV_UNREGISTER:
500                 spin_lock(&if_list_lock);
501                 hardif_remove_interface(batman_if);
502                 spin_unlock(&if_list_lock);
503                 break;
504         case NETDEV_CHANGEMTU:
505                 if (batman_if->soft_iface)
506                         update_min_mtu(batman_if->soft_iface);
507                 break;
508         case NETDEV_CHANGEADDR:
509                 if (batman_if->if_status == IF_NOT_IN_USE) {
510                         hardif_put(batman_if);
511                         goto out;
512                 }
513
514                 check_known_mac_addr(batman_if->net_dev->dev_addr);
515                 update_mac_addresses(batman_if);
516
517                 bat_priv = netdev_priv(batman_if->soft_iface);
518                 if (batman_if == bat_priv->primary_if)
519                         set_primary_if(bat_priv, batman_if);
520                 break;
521         default:
522                 break;
523         };
524         hardif_put(batman_if);
525
526 out:
527         return NOTIFY_DONE;
528 }
529
530 /* receive a packet with the batman ethertype coming on a hard
531  * interface */
532 int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
533         struct packet_type *ptype, struct net_device *orig_dev)
534 {
535         struct bat_priv *bat_priv;
536         struct batman_packet *batman_packet;
537         struct batman_if *batman_if;
538         int ret;
539
540         batman_if = container_of(ptype, struct batman_if, batman_adv_ptype);
541         skb = skb_share_check(skb, GFP_ATOMIC);
542
543         /* skb was released by skb_share_check() */
544         if (!skb)
545                 goto err_out;
546
547         /* packet should hold at least type and version */
548         if (unlikely(!pskb_may_pull(skb, 2)))
549                 goto err_free;
550
551         /* expect a valid ethernet header here. */
552         if (unlikely(skb->mac_len != sizeof(struct ethhdr)
553                                 || !skb_mac_header(skb)))
554                 goto err_free;
555
556         if (!batman_if->soft_iface)
557                 goto err_free;
558
559         bat_priv = netdev_priv(batman_if->soft_iface);
560
561         if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
562                 goto err_free;
563
564         /* discard frames on not active interfaces */
565         if (batman_if->if_status != IF_ACTIVE)
566                 goto err_free;
567
568         batman_packet = (struct batman_packet *)skb->data;
569
570         if (batman_packet->version != COMPAT_VERSION) {
571                 bat_dbg(DBG_BATMAN, bat_priv,
572                         "Drop packet: incompatible batman version (%i)\n",
573                         batman_packet->version);
574                 goto err_free;
575         }
576
577         /* all receive handlers return whether they received or reused
578          * the supplied skb. if not, we have to free the skb. */
579
580         switch (batman_packet->packet_type) {
581                 /* batman originator packet */
582         case BAT_PACKET:
583                 ret = recv_bat_packet(skb, batman_if);
584                 break;
585
586                 /* batman icmp packet */
587         case BAT_ICMP:
588                 ret = recv_icmp_packet(skb, batman_if);
589                 break;
590
591                 /* unicast packet */
592         case BAT_UNICAST:
593                 ret = recv_unicast_packet(skb, batman_if);
594                 break;
595
596                 /* fragmented unicast packet */
597         case BAT_UNICAST_FRAG:
598                 ret = recv_ucast_frag_packet(skb, batman_if);
599                 break;
600
601                 /* broadcast packet */
602         case BAT_BCAST:
603                 ret = recv_bcast_packet(skb, batman_if);
604                 break;
605
606                 /* vis packet */
607         case BAT_VIS:
608                 ret = recv_vis_packet(skb, batman_if);
609                 break;
610         default:
611                 ret = NET_RX_DROP;
612         }
613
614         if (ret == NET_RX_DROP)
615                 kfree_skb(skb);
616
617         /* return NET_RX_SUCCESS in any case as we
618          * most probably dropped the packet for
619          * routing-logical reasons. */
620
621         return NET_RX_SUCCESS;
622
623 err_free:
624         kfree_skb(skb);
625 err_out:
626         return NET_RX_DROP;
627 }
628
629 struct notifier_block hard_if_notifier = {
630         .notifier_call = hard_if_event,
631 };