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