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