Merge branch 'fix/misc' into topic/misc
[pandora-kernel.git] / drivers / staging / batman-adv / send.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 "send.h"
24 #include "routing.h"
25 #include "translation-table.h"
26 #include "soft-interface.h"
27 #include "hard-interface.h"
28 #include "types.h"
29 #include "vis.h"
30 #include "aggregation.h"
31
32
33 static void send_outstanding_bcast_packet(struct work_struct *work);
34
35 /* apply hop penalty for a normal link */
36 static uint8_t hop_penalty(const uint8_t tq)
37 {
38         return (tq * (TQ_MAX_VALUE - TQ_HOP_PENALTY)) / (TQ_MAX_VALUE);
39 }
40
41 /* when do we schedule our own packet to be sent */
42 static unsigned long own_send_time(struct bat_priv *bat_priv)
43 {
44         return jiffies + msecs_to_jiffies(
45                    atomic_read(&bat_priv->orig_interval) -
46                    JITTER + (random32() % 2*JITTER));
47 }
48
49 /* when do we schedule a forwarded packet to be sent */
50 static unsigned long forward_send_time(struct bat_priv *bat_priv)
51 {
52         return jiffies + msecs_to_jiffies(random32() % (JITTER/2));
53 }
54
55 /* send out an already prepared packet to the given address via the
56  * specified batman interface */
57 int send_skb_packet(struct sk_buff *skb,
58                                 struct batman_if *batman_if,
59                                 uint8_t *dst_addr)
60 {
61         struct ethhdr *ethhdr;
62
63         if (batman_if->if_status != IF_ACTIVE)
64                 goto send_skb_err;
65
66         if (unlikely(!batman_if->net_dev))
67                 goto send_skb_err;
68
69         if (!(batman_if->net_dev->flags & IFF_UP)) {
70                 pr_warning("Interface %s is not up - can't send packet via "
71                            "that interface!\n", batman_if->dev);
72                 goto send_skb_err;
73         }
74
75         /* push to the ethernet header. */
76         if (my_skb_push(skb, sizeof(struct ethhdr)) < 0)
77                 goto send_skb_err;
78
79         skb_reset_mac_header(skb);
80
81         ethhdr = (struct ethhdr *) skb_mac_header(skb);
82         memcpy(ethhdr->h_source, batman_if->net_dev->dev_addr, ETH_ALEN);
83         memcpy(ethhdr->h_dest, dst_addr, ETH_ALEN);
84         ethhdr->h_proto = __constant_htons(ETH_P_BATMAN);
85
86         skb_set_network_header(skb, ETH_HLEN);
87         skb->priority = TC_PRIO_CONTROL;
88         skb->protocol = __constant_htons(ETH_P_BATMAN);
89
90         skb->dev = batman_if->net_dev;
91
92         /* dev_queue_xmit() returns a negative result on error.  However on
93          * congestion and traffic shaping, it drops and returns NET_XMIT_DROP
94          * (which is > 0). This will not be treated as an error. */
95
96         return dev_queue_xmit(skb);
97 send_skb_err:
98         kfree_skb(skb);
99         return NET_XMIT_DROP;
100 }
101
102 /* sends a raw packet. */
103 void send_raw_packet(unsigned char *pack_buff, int pack_buff_len,
104                      struct batman_if *batman_if, uint8_t *dst_addr)
105 {
106         struct sk_buff *skb;
107         char *data;
108
109         skb = dev_alloc_skb(pack_buff_len + sizeof(struct ethhdr));
110         if (!skb)
111                 return;
112         data = skb_put(skb, pack_buff_len + sizeof(struct ethhdr));
113         memcpy(data + sizeof(struct ethhdr), pack_buff, pack_buff_len);
114         /* pull back to the batman "network header" */
115         skb_pull(skb, sizeof(struct ethhdr));
116         send_skb_packet(skb, batman_if, dst_addr);
117 }
118
119 /* Send a packet to a given interface */
120 static void send_packet_to_if(struct forw_packet *forw_packet,
121                               struct batman_if *batman_if)
122 {
123         /* FIXME: each batman_if will be attached to a softif */
124         struct bat_priv *bat_priv = netdev_priv(soft_device);
125         char *fwd_str;
126         uint8_t packet_num;
127         int16_t buff_pos;
128         struct batman_packet *batman_packet;
129
130         if (batman_if->if_status != IF_ACTIVE)
131                 return;
132
133         packet_num = 0;
134         buff_pos = 0;
135         batman_packet = (struct batman_packet *)
136                 (forw_packet->packet_buff);
137
138         /* adjust all flags and log packets */
139         while (aggregated_packet(buff_pos,
140                                  forw_packet->packet_len,
141                                  batman_packet->num_hna)) {
142
143                 /* we might have aggregated direct link packets with an
144                  * ordinary base packet */
145                 if ((forw_packet->direct_link_flags & (1 << packet_num)) &&
146                     (forw_packet->if_incoming == batman_if))
147                         batman_packet->flags |= DIRECTLINK;
148                 else
149                         batman_packet->flags &= ~DIRECTLINK;
150
151                 fwd_str = (packet_num > 0 ? "Forwarding" : (forw_packet->own ?
152                                                             "Sending own" :
153                                                             "Forwarding"));
154                 bat_dbg(DBG_BATMAN, bat_priv,
155                         "%s %spacket (originator %pM, seqno %d, TQ %d, TTL %d,"
156                         " IDF %s) on interface %s [%s]\n",
157                         fwd_str, (packet_num > 0 ? "aggregated " : ""),
158                         batman_packet->orig, ntohl(batman_packet->seqno),
159                         batman_packet->tq, batman_packet->ttl,
160                         (batman_packet->flags & DIRECTLINK ?
161                          "on" : "off"),
162                         batman_if->dev, batman_if->addr_str);
163
164                 buff_pos += sizeof(struct batman_packet) +
165                         (batman_packet->num_hna * ETH_ALEN);
166                 packet_num++;
167                 batman_packet = (struct batman_packet *)
168                         (forw_packet->packet_buff + buff_pos);
169         }
170
171         send_raw_packet(forw_packet->packet_buff,
172                         forw_packet->packet_len,
173                         batman_if, broadcast_addr);
174 }
175
176 /* send a batman packet */
177 static void send_packet(struct forw_packet *forw_packet)
178 {
179         /* FIXME: each batman_if will be attached to a softif */
180         struct bat_priv *bat_priv = netdev_priv(soft_device);
181         struct batman_if *batman_if;
182         struct batman_packet *batman_packet =
183                 (struct batman_packet *)(forw_packet->packet_buff);
184         unsigned char directlink = (batman_packet->flags & DIRECTLINK ? 1 : 0);
185
186         if (!forw_packet->if_incoming) {
187                 pr_err("Error - can't forward packet: incoming iface not "
188                        "specified\n");
189                 return;
190         }
191
192         if (forw_packet->if_incoming->if_status != IF_ACTIVE)
193                 return;
194
195         /* multihomed peer assumed */
196         /* non-primary OGMs are only broadcasted on their interface */
197         if ((directlink && (batman_packet->ttl == 1)) ||
198             (forw_packet->own && (forw_packet->if_incoming->if_num > 0))) {
199
200                 /* FIXME: what about aggregated packets ? */
201                 bat_dbg(DBG_BATMAN, bat_priv,
202                         "%s packet (originator %pM, seqno %d, TTL %d) "
203                         "on interface %s [%s]\n",
204                         (forw_packet->own ? "Sending own" : "Forwarding"),
205                         batman_packet->orig, ntohl(batman_packet->seqno),
206                         batman_packet->ttl, forw_packet->if_incoming->dev,
207                         forw_packet->if_incoming->addr_str);
208
209                 send_raw_packet(forw_packet->packet_buff,
210                                 forw_packet->packet_len,
211                                 forw_packet->if_incoming,
212                                 broadcast_addr);
213                 return;
214         }
215
216         /* broadcast on every interface */
217         rcu_read_lock();
218         list_for_each_entry_rcu(batman_if, &if_list, list)
219                 send_packet_to_if(forw_packet, batman_if);
220         rcu_read_unlock();
221 }
222
223 static void rebuild_batman_packet(struct batman_if *batman_if)
224 {
225         int new_len;
226         unsigned char *new_buff;
227         struct batman_packet *batman_packet;
228
229         new_len = sizeof(struct batman_packet) + (num_hna * ETH_ALEN);
230         new_buff = kmalloc(new_len, GFP_ATOMIC);
231
232         /* keep old buffer if kmalloc should fail */
233         if (new_buff) {
234                 memcpy(new_buff, batman_if->packet_buff,
235                        sizeof(struct batman_packet));
236                 batman_packet = (struct batman_packet *)new_buff;
237
238                 batman_packet->num_hna = hna_local_fill_buffer(
239                         new_buff + sizeof(struct batman_packet),
240                         new_len - sizeof(struct batman_packet));
241
242                 kfree(batman_if->packet_buff);
243                 batman_if->packet_buff = new_buff;
244                 batman_if->packet_len = new_len;
245         }
246 }
247
248 void schedule_own_packet(struct batman_if *batman_if)
249 {
250         /* FIXME: each batman_if will be attached to a softif */
251         struct bat_priv *bat_priv = netdev_priv(soft_device);
252         unsigned long send_time;
253         struct batman_packet *batman_packet;
254         int vis_server;
255
256         if ((batman_if->if_status == IF_NOT_IN_USE) ||
257             (batman_if->if_status == IF_TO_BE_REMOVED))
258                 return;
259
260         vis_server = atomic_read(&bat_priv->vis_mode);
261
262         /**
263          * the interface gets activated here to avoid race conditions between
264          * the moment of activating the interface in
265          * hardif_activate_interface() where the originator mac is set and
266          * outdated packets (especially uninitialized mac addresses) in the
267          * packet queue
268          */
269         if (batman_if->if_status == IF_TO_BE_ACTIVATED)
270                 batman_if->if_status = IF_ACTIVE;
271
272         /* if local hna has changed and interface is a primary interface */
273         if ((atomic_read(&hna_local_changed)) &&
274             (batman_if == bat_priv->primary_if))
275                 rebuild_batman_packet(batman_if);
276
277         /**
278          * NOTE: packet_buff might just have been re-allocated in
279          * rebuild_batman_packet()
280          */
281         batman_packet = (struct batman_packet *)batman_if->packet_buff;
282
283         /* change sequence number to network order */
284         batman_packet->seqno =
285                 htonl((uint32_t)atomic_read(&batman_if->seqno));
286
287         if (vis_server == VIS_TYPE_SERVER_SYNC)
288                 batman_packet->flags |= VIS_SERVER;
289         else
290                 batman_packet->flags &= ~VIS_SERVER;
291
292         atomic_inc(&batman_if->seqno);
293
294         slide_own_bcast_window(batman_if);
295         send_time = own_send_time(bat_priv);
296         add_bat_packet_to_list(bat_priv,
297                                batman_if->packet_buff,
298                                batman_if->packet_len,
299                                batman_if, 1, send_time);
300 }
301
302 void schedule_forward_packet(struct orig_node *orig_node,
303                              struct ethhdr *ethhdr,
304                              struct batman_packet *batman_packet,
305                              uint8_t directlink, int hna_buff_len,
306                              struct batman_if *if_incoming)
307 {
308         /* FIXME: each batman_if will be attached to a softif */
309         struct bat_priv *bat_priv = netdev_priv(soft_device);
310         unsigned char in_tq, in_ttl, tq_avg = 0;
311         unsigned long send_time;
312
313         if (batman_packet->ttl <= 1) {
314                 bat_dbg(DBG_BATMAN, bat_priv, "ttl exceeded\n");
315                 return;
316         }
317
318         in_tq = batman_packet->tq;
319         in_ttl = batman_packet->ttl;
320
321         batman_packet->ttl--;
322         memcpy(batman_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
323
324         /* rebroadcast tq of our best ranking neighbor to ensure the rebroadcast
325          * of our best tq value */
326         if ((orig_node->router) && (orig_node->router->tq_avg != 0)) {
327
328                 /* rebroadcast ogm of best ranking neighbor as is */
329                 if (!compare_orig(orig_node->router->addr, ethhdr->h_source)) {
330                         batman_packet->tq = orig_node->router->tq_avg;
331
332                         if (orig_node->router->last_ttl)
333                                 batman_packet->ttl = orig_node->router->last_ttl
334                                                         - 1;
335                 }
336
337                 tq_avg = orig_node->router->tq_avg;
338         }
339
340         /* apply hop penalty */
341         batman_packet->tq = hop_penalty(batman_packet->tq);
342
343         bat_dbg(DBG_BATMAN, bat_priv,
344                 "Forwarding packet: tq_orig: %i, tq_avg: %i, "
345                 "tq_forw: %i, ttl_orig: %i, ttl_forw: %i\n",
346                 in_tq, tq_avg, batman_packet->tq, in_ttl - 1,
347                 batman_packet->ttl);
348
349         batman_packet->seqno = htonl(batman_packet->seqno);
350
351         /* switch of primaries first hop flag when forwarding */
352         batman_packet->flags &= ~PRIMARIES_FIRST_HOP;
353         if (directlink)
354                 batman_packet->flags |= DIRECTLINK;
355         else
356                 batman_packet->flags &= ~DIRECTLINK;
357
358         send_time = forward_send_time(bat_priv);
359         add_bat_packet_to_list(bat_priv,
360                                (unsigned char *)batman_packet,
361                                sizeof(struct batman_packet) + hna_buff_len,
362                                if_incoming, 0, send_time);
363 }
364
365 static void forw_packet_free(struct forw_packet *forw_packet)
366 {
367         if (forw_packet->skb)
368                 kfree_skb(forw_packet->skb);
369         kfree(forw_packet->packet_buff);
370         kfree(forw_packet);
371 }
372
373 static void _add_bcast_packet_to_list(struct forw_packet *forw_packet,
374                                       unsigned long send_time)
375 {
376         unsigned long flags;
377         INIT_HLIST_NODE(&forw_packet->list);
378
379         /* add new packet to packet list */
380         spin_lock_irqsave(&forw_bcast_list_lock, flags);
381         hlist_add_head(&forw_packet->list, &forw_bcast_list);
382         spin_unlock_irqrestore(&forw_bcast_list_lock, flags);
383
384         /* start timer for this packet */
385         INIT_DELAYED_WORK(&forw_packet->delayed_work,
386                           send_outstanding_bcast_packet);
387         queue_delayed_work(bat_event_workqueue, &forw_packet->delayed_work,
388                            send_time);
389 }
390
391 #define atomic_dec_not_zero(v)          atomic_add_unless((v), -1, 0)
392 /* add a broadcast packet to the queue and setup timers. broadcast packets
393  * are sent multiple times to increase probability for beeing received.
394  *
395  * This function returns NETDEV_TX_OK on success and NETDEV_TX_BUSY on
396  * errors.
397  *
398  * The skb is not consumed, so the caller should make sure that the
399  * skb is freed. */
400 int add_bcast_packet_to_list(struct sk_buff *skb)
401 {
402         struct forw_packet *forw_packet;
403         struct bcast_packet *bcast_packet;
404         /* FIXME: each batman_if will be attached to a softif */
405         struct bat_priv *bat_priv = netdev_priv(soft_device);
406
407         if (!atomic_dec_not_zero(&bcast_queue_left)) {
408                 bat_dbg(DBG_BATMAN, bat_priv, "bcast packet queue full\n");
409                 goto out;
410         }
411
412         forw_packet = kmalloc(sizeof(struct forw_packet), GFP_ATOMIC);
413
414         if (!forw_packet)
415                 goto out_and_inc;
416
417         skb = skb_copy(skb, GFP_ATOMIC);
418         if (!skb)
419                 goto packet_free;
420
421         /* as we have a copy now, it is safe to decrease the TTL */
422         bcast_packet = (struct bcast_packet *)skb->data;
423         bcast_packet->ttl--;
424
425         skb_reset_mac_header(skb);
426
427         forw_packet->skb = skb;
428         forw_packet->packet_buff = NULL;
429
430         /* how often did we send the bcast packet ? */
431         forw_packet->num_packets = 0;
432
433         _add_bcast_packet_to_list(forw_packet, 1);
434         return NETDEV_TX_OK;
435
436 packet_free:
437         kfree(forw_packet);
438 out_and_inc:
439         atomic_inc(&bcast_queue_left);
440 out:
441         return NETDEV_TX_BUSY;
442 }
443
444 static void send_outstanding_bcast_packet(struct work_struct *work)
445 {
446         struct batman_if *batman_if;
447         struct delayed_work *delayed_work =
448                 container_of(work, struct delayed_work, work);
449         struct forw_packet *forw_packet =
450                 container_of(delayed_work, struct forw_packet, delayed_work);
451         unsigned long flags;
452         struct sk_buff *skb1;
453
454         spin_lock_irqsave(&forw_bcast_list_lock, flags);
455         hlist_del(&forw_packet->list);
456         spin_unlock_irqrestore(&forw_bcast_list_lock, flags);
457
458         if (atomic_read(&module_state) == MODULE_DEACTIVATING)
459                 goto out;
460
461         /* rebroadcast packet */
462         rcu_read_lock();
463         list_for_each_entry_rcu(batman_if, &if_list, list) {
464                 /* send a copy of the saved skb */
465                 skb1 = skb_copy(forw_packet->skb, GFP_ATOMIC);
466                 if (skb1)
467                         send_skb_packet(skb1,
468                                 batman_if, broadcast_addr);
469         }
470         rcu_read_unlock();
471
472         forw_packet->num_packets++;
473
474         /* if we still have some more bcasts to send */
475         if (forw_packet->num_packets < 3) {
476                 _add_bcast_packet_to_list(forw_packet, ((5 * HZ) / 1000));
477                 return;
478         }
479
480 out:
481         forw_packet_free(forw_packet);
482         atomic_inc(&bcast_queue_left);
483 }
484
485 void send_outstanding_bat_packet(struct work_struct *work)
486 {
487         struct delayed_work *delayed_work =
488                 container_of(work, struct delayed_work, work);
489         struct forw_packet *forw_packet =
490                 container_of(delayed_work, struct forw_packet, delayed_work);
491         unsigned long flags;
492
493         spin_lock_irqsave(&forw_bat_list_lock, flags);
494         hlist_del(&forw_packet->list);
495         spin_unlock_irqrestore(&forw_bat_list_lock, flags);
496
497         if (atomic_read(&module_state) == MODULE_DEACTIVATING)
498                 goto out;
499
500         send_packet(forw_packet);
501
502         /**
503          * we have to have at least one packet in the queue
504          * to determine the queues wake up time unless we are
505          * shutting down
506          */
507         if (forw_packet->own)
508                 schedule_own_packet(forw_packet->if_incoming);
509
510 out:
511         /* don't count own packet */
512         if (!forw_packet->own)
513                 atomic_inc(&batman_queue_left);
514
515         forw_packet_free(forw_packet);
516 }
517
518 void purge_outstanding_packets(struct batman_if *batman_if)
519 {
520         /* FIXME: each batman_if will be attached to a softif */
521         struct bat_priv *bat_priv = netdev_priv(soft_device);
522         struct forw_packet *forw_packet;
523         struct hlist_node *tmp_node, *safe_tmp_node;
524         unsigned long flags;
525
526         if (batman_if)
527                 bat_dbg(DBG_BATMAN, bat_priv,
528                         "purge_outstanding_packets(): %s\n",
529                         batman_if->dev);
530         else
531                 bat_dbg(DBG_BATMAN, bat_priv,
532                         "purge_outstanding_packets()\n");
533
534         /* free bcast list */
535         spin_lock_irqsave(&forw_bcast_list_lock, flags);
536         hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
537                                   &forw_bcast_list, list) {
538
539                 /**
540                  * if purge_outstanding_packets() was called with an argmument
541                  * we delete only packets belonging to the given interface
542                  */
543                 if ((batman_if) &&
544                     (forw_packet->if_incoming != batman_if))
545                         continue;
546
547                 spin_unlock_irqrestore(&forw_bcast_list_lock, flags);
548
549                 /**
550                  * send_outstanding_bcast_packet() will lock the list to
551                  * delete the item from the list
552                  */
553                 cancel_delayed_work_sync(&forw_packet->delayed_work);
554                 spin_lock_irqsave(&forw_bcast_list_lock, flags);
555         }
556         spin_unlock_irqrestore(&forw_bcast_list_lock, flags);
557
558         /* free batman packet list */
559         spin_lock_irqsave(&forw_bat_list_lock, flags);
560         hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
561                                   &forw_bat_list, list) {
562
563                 /**
564                  * if purge_outstanding_packets() was called with an argmument
565                  * we delete only packets belonging to the given interface
566                  */
567                 if ((batman_if) &&
568                     (forw_packet->if_incoming != batman_if))
569                         continue;
570
571                 spin_unlock_irqrestore(&forw_bat_list_lock, flags);
572
573                 /**
574                  * send_outstanding_bat_packet() will lock the list to
575                  * delete the item from the list
576                  */
577                 cancel_delayed_work_sync(&forw_packet->delayed_work);
578                 spin_lock_irqsave(&forw_bat_list_lock, flags);
579         }
580         spin_unlock_irqrestore(&forw_bat_list_lock, flags);
581 }