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