Staging: batman-adv: Use forw_bcast_list_lock always with disabled interrupts
[firefly-linux-kernel-4.4.55.git] / drivers / staging / batman-adv / send.c
1 /*
2  * Copyright (C) 2007-2009 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 "hard-interface.h"
27 #include "types.h"
28 #include "vis.h"
29 #include "aggregation.h"
30
31 #include "compat.h"
32
33 /* apply hop penalty for a normal link */
34 static uint8_t hop_penalty(const uint8_t tq)
35 {
36         return (tq * (TQ_MAX_VALUE - TQ_HOP_PENALTY)) / (TQ_MAX_VALUE);
37 }
38
39 /* when do we schedule our own packet to be sent */
40 static unsigned long own_send_time(void)
41 {
42         return jiffies +
43                 (((atomic_read(&originator_interval) - JITTER +
44                    (random32() % 2*JITTER)) * HZ) / 1000);
45 }
46
47 /* when do we schedule a forwarded packet to be sent */
48 static unsigned long forward_send_time(void)
49 {
50         unsigned long send_time = jiffies; /* Starting now plus... */
51
52         if (atomic_read(&aggregation_enabled))
53                 send_time += (((MAX_AGGREGATION_MS - (JITTER/2) +
54                                 (random32() % JITTER)) * HZ) / 1000);
55         else
56                 send_time += (((random32() % (JITTER/2)) * HZ) / 1000);
57
58         return send_time;
59 }
60
61 /* sends a raw packet. */
62 void send_raw_packet(unsigned char *pack_buff, int pack_buff_len,
63                      struct batman_if *batman_if, uint8_t *dst_addr)
64 {
65         struct ethhdr *ethhdr;
66         struct sk_buff *skb;
67         int retval;
68         char *data;
69
70         if (batman_if->if_active != IF_ACTIVE)
71                 return;
72
73         if (!(batman_if->net_dev->flags & IFF_UP)) {
74                 printk(KERN_WARNING
75                        "batman-adv:Interface %s is not up - can't send packet via that interface!\n",
76                        batman_if->dev);
77                 return;
78         }
79
80         skb = dev_alloc_skb(pack_buff_len + sizeof(struct ethhdr));
81         if (!skb)
82                 return;
83         data = skb_put(skb, pack_buff_len + sizeof(struct ethhdr));
84
85         memcpy(data + sizeof(struct ethhdr), pack_buff, pack_buff_len);
86
87         ethhdr = (struct ethhdr *) data;
88         memcpy(ethhdr->h_source, batman_if->net_dev->dev_addr, ETH_ALEN);
89         memcpy(ethhdr->h_dest, dst_addr, ETH_ALEN);
90         ethhdr->h_proto = __constant_htons(ETH_P_BATMAN);
91
92         skb_reset_mac_header(skb);
93         skb_set_network_header(skb, ETH_HLEN);
94         skb->priority = TC_PRIO_CONTROL;
95         skb->protocol = __constant_htons(ETH_P_BATMAN);
96         skb->dev = batman_if->net_dev;
97
98         /* dev_queue_xmit() returns a negative result on error.  However on
99          * congestion and traffic shaping, it drops and returns NET_XMIT_DROP
100          * (which is > 0). This will not be treated as an error. */
101         retval = dev_queue_xmit(skb);
102         if (retval < 0)
103                 printk(KERN_WARNING
104                        "batman-adv:Can't write to raw socket: %i\n",
105                        retval);
106 }
107
108 /* Send a packet to a given interface */
109 static void send_packet_to_if(struct forw_packet *forw_packet,
110                               struct batman_if *batman_if)
111 {
112         char *fwd_str;
113         uint8_t packet_num;
114         int16_t buff_pos;
115         struct batman_packet *batman_packet;
116         char orig_str[ETH_STR_LEN];
117
118         if (batman_if->if_active != IF_ACTIVE)
119                 return;
120
121         packet_num = buff_pos = 0;
122         batman_packet = (struct batman_packet *)
123                 (forw_packet->packet_buff);
124
125         /* adjust all flags and log packets */
126         while (aggregated_packet(buff_pos,
127                                  forw_packet->packet_len,
128                                  batman_packet->num_hna)) {
129
130                 /* we might have aggregated direct link packets with an
131                  * ordinary base packet */
132                 if ((forw_packet->direct_link_flags & (1 << packet_num)) &&
133                     (forw_packet->if_incoming == batman_if))
134                         batman_packet->flags |= DIRECTLINK;
135                 else
136                         batman_packet->flags &= ~DIRECTLINK;
137
138                 addr_to_string(orig_str, batman_packet->orig);
139                 fwd_str = (packet_num > 0 ? "Forwarding" : (forw_packet->own ?
140                                                             "Sending own" :
141                                                             "Forwarding"));
142                 bat_dbg(DBG_BATMAN,
143                         "%s %spacket (originator %s, seqno %d, TQ %d, TTL %d, IDF %s) on interface %s [%s]\n",
144                         fwd_str,
145                         (packet_num > 0 ? "aggregated " : ""),
146                         orig_str, ntohs(batman_packet->seqno),
147                         batman_packet->tq, batman_packet->ttl,
148                         (batman_packet->flags & DIRECTLINK ?
149                          "on" : "off"),
150                         batman_if->dev, batman_if->addr_str);
151
152                 buff_pos += sizeof(struct batman_packet) +
153                         (batman_packet->num_hna * ETH_ALEN);
154                 packet_num++;
155                 batman_packet = (struct batman_packet *)
156                         (forw_packet->packet_buff + buff_pos);
157         }
158
159         send_raw_packet(forw_packet->packet_buff,
160                         forw_packet->packet_len,
161                         batman_if, broadcastAddr);
162 }
163
164 /* send a batman packet */
165 static void send_packet(struct forw_packet *forw_packet)
166 {
167         struct batman_if *batman_if;
168         struct batman_packet *batman_packet =
169                 (struct batman_packet *)(forw_packet->packet_buff);
170         char orig_str[ETH_STR_LEN];
171         unsigned char directlink = (batman_packet->flags & DIRECTLINK ? 1 : 0);
172
173         if (!forw_packet->if_incoming) {
174                 printk(KERN_ERR "batman-adv: Error - can't forward packet: incoming iface not specified\n");
175                 return;
176         }
177
178         if (forw_packet->if_incoming->if_active != IF_ACTIVE)
179                 return;
180
181         addr_to_string(orig_str, batman_packet->orig);
182
183         /* multihomed peer assumed */
184         /* non-primary OGMs are only broadcasted on their interface */
185         if ((directlink && (batman_packet->ttl == 1)) ||
186             (forw_packet->own && (forw_packet->if_incoming->if_num > 0))) {
187
188                 /* FIXME: what about aggregated packets ? */
189                 bat_dbg(DBG_BATMAN,
190                         "%s packet (originator %s, seqno %d, TTL %d) on interface %s [%s]\n",
191                         (forw_packet->own ? "Sending own" : "Forwarding"),
192                         orig_str, ntohs(batman_packet->seqno),
193                         batman_packet->ttl, forw_packet->if_incoming->dev,
194                         forw_packet->if_incoming->addr_str);
195
196                 send_raw_packet(forw_packet->packet_buff,
197                                 forw_packet->packet_len,
198                                 forw_packet->if_incoming,
199                                 broadcastAddr);
200                 return;
201         }
202
203         /* broadcast on every interface */
204         rcu_read_lock();
205         list_for_each_entry_rcu(batman_if, &if_list, list)
206                 send_packet_to_if(forw_packet, batman_if);
207         rcu_read_unlock();
208 }
209
210 static void rebuild_batman_packet(struct batman_if *batman_if)
211 {
212         int new_len;
213         unsigned char *new_buff;
214         struct batman_packet *batman_packet;
215
216         new_len = sizeof(struct batman_packet) + (num_hna * ETH_ALEN);
217         new_buff = kmalloc(new_len, GFP_ATOMIC);
218
219         /* keep old buffer if kmalloc should fail */
220         if (new_buff) {
221                 memcpy(new_buff, batman_if->packet_buff,
222                        sizeof(struct batman_packet));
223                 batman_packet = (struct batman_packet *)new_buff;
224
225                 batman_packet->num_hna = hna_local_fill_buffer(
226                         new_buff + sizeof(struct batman_packet),
227                         new_len - sizeof(struct batman_packet));
228
229                 kfree(batman_if->packet_buff);
230                 batman_if->packet_buff = new_buff;
231                 batman_if->packet_len = new_len;
232         }
233 }
234
235 void schedule_own_packet(struct batman_if *batman_if)
236 {
237         unsigned long send_time;
238         struct batman_packet *batman_packet;
239
240         /**
241          * the interface gets activated here to avoid race conditions between
242          * the moment of activating the interface in
243          * hardif_activate_interface() where the originator mac is set and
244          * outdated packets (especially uninitialized mac addresses) in the
245          * packet queue
246          */
247         if (batman_if->if_active == IF_TO_BE_ACTIVATED)
248                 batman_if->if_active = IF_ACTIVE;
249
250         /* if local hna has changed and interface is a primary interface */
251         if ((atomic_read(&hna_local_changed)) && (batman_if->if_num == 0))
252                 rebuild_batman_packet(batman_if);
253
254         /**
255          * NOTE: packet_buff might just have been re-allocated in
256          * rebuild_batman_packet()
257          */
258         batman_packet = (struct batman_packet *)batman_if->packet_buff;
259
260         /* change sequence number to network order */
261         batman_packet->seqno = htons((uint16_t)atomic_read(&batman_if->seqno));
262
263         if (is_vis_server())
264                 batman_packet->flags = VIS_SERVER;
265         else
266                 batman_packet->flags = 0;
267
268         /* could be read by receive_bat_packet() */
269         atomic_inc(&batman_if->seqno);
270
271         slide_own_bcast_window(batman_if);
272         send_time = own_send_time();
273         add_bat_packet_to_list(batman_if->packet_buff,
274                                batman_if->packet_len, batman_if, 1, send_time);
275 }
276
277 void schedule_forward_packet(struct orig_node *orig_node,
278                              struct ethhdr *ethhdr,
279                              struct batman_packet *batman_packet,
280                              uint8_t directlink, int hna_buff_len,
281                              struct batman_if *if_incoming)
282 {
283         unsigned char in_tq, in_ttl, tq_avg = 0;
284         unsigned long send_time;
285
286         if (batman_packet->ttl <= 1) {
287                 bat_dbg(DBG_BATMAN, "ttl exceeded \n");
288                 return;
289         }
290
291         in_tq = batman_packet->tq;
292         in_ttl = batman_packet->ttl;
293
294         batman_packet->ttl--;
295         memcpy(batman_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
296
297         /* rebroadcast tq of our best ranking neighbor to ensure the rebroadcast
298          * of our best tq value */
299         if ((orig_node->router) && (orig_node->router->tq_avg != 0)) {
300
301                 /* rebroadcast ogm of best ranking neighbor as is */
302                 if (!compare_orig(orig_node->router->addr, ethhdr->h_source)) {
303                         batman_packet->tq = orig_node->router->tq_avg;
304
305                         if (orig_node->router->last_ttl)
306                                 batman_packet->ttl = orig_node->router->last_ttl - 1;
307                 }
308
309                 tq_avg = orig_node->router->tq_avg;
310         }
311
312         /* apply hop penalty */
313         batman_packet->tq = hop_penalty(batman_packet->tq);
314
315         bat_dbg(DBG_BATMAN, "Forwarding packet: tq_orig: %i, tq_avg: %i, tq_forw: %i, ttl_orig: %i, ttl_forw: %i \n",
316                 in_tq, tq_avg, batman_packet->tq, in_ttl - 1,
317                 batman_packet->ttl);
318
319         batman_packet->seqno = htons(batman_packet->seqno);
320
321         if (directlink)
322                 batman_packet->flags |= DIRECTLINK;
323         else
324                 batman_packet->flags &= ~DIRECTLINK;
325
326         send_time = forward_send_time();
327         add_bat_packet_to_list((unsigned char *)batman_packet,
328                                sizeof(struct batman_packet) + hna_buff_len,
329                                if_incoming, 0, send_time);
330 }
331
332 static void forw_packet_free(struct forw_packet *forw_packet)
333 {
334         kfree(forw_packet->packet_buff);
335         kfree(forw_packet);
336 }
337
338 static void _add_bcast_packet_to_list(struct forw_packet *forw_packet,
339                                       unsigned long send_time)
340 {
341         unsigned long flags;
342         INIT_HLIST_NODE(&forw_packet->list);
343
344         /* add new packet to packet list */
345         spin_lock_irqsave(&forw_bcast_list_lock, flags);
346         hlist_add_head(&forw_packet->list, &forw_bcast_list);
347         spin_unlock_irqrestore(&forw_bcast_list_lock, flags);
348
349         /* start timer for this packet */
350         INIT_DELAYED_WORK(&forw_packet->delayed_work,
351                           send_outstanding_bcast_packet);
352         queue_delayed_work(bat_event_workqueue, &forw_packet->delayed_work,
353                            send_time);
354 }
355
356 void add_bcast_packet_to_list(unsigned char *packet_buff, int packet_len)
357 {
358         struct forw_packet *forw_packet;
359
360         forw_packet = kmalloc(sizeof(struct forw_packet), GFP_ATOMIC);
361         if (!forw_packet)
362                 return;
363
364         forw_packet->packet_buff = kmalloc(packet_len, GFP_ATOMIC);
365         if (!forw_packet->packet_buff) {
366                 kfree(forw_packet);
367                 return;
368         }
369
370         forw_packet->packet_len = packet_len;
371         memcpy(forw_packet->packet_buff, packet_buff, forw_packet->packet_len);
372
373         /* how often did we send the bcast packet ? */
374         forw_packet->num_packets = 0;
375
376         _add_bcast_packet_to_list(forw_packet, 1);
377 }
378
379 void send_outstanding_bcast_packet(struct work_struct *work)
380 {
381         struct batman_if *batman_if;
382         struct delayed_work *delayed_work =
383                 container_of(work, struct delayed_work, work);
384         struct forw_packet *forw_packet =
385                 container_of(delayed_work, struct forw_packet, delayed_work);
386         unsigned long flags;
387
388         spin_lock_irqsave(&forw_bcast_list_lock, flags);
389         hlist_del(&forw_packet->list);
390         spin_unlock_irqrestore(&forw_bcast_list_lock, flags);
391
392         /* rebroadcast packet */
393         rcu_read_lock();
394         list_for_each_entry_rcu(batman_if, &if_list, list) {
395                 send_raw_packet(forw_packet->packet_buff,
396                                 forw_packet->packet_len,
397                                 batman_if, broadcastAddr);
398         }
399         rcu_read_unlock();
400
401         forw_packet->num_packets++;
402
403         /* if we still have some more bcasts to send and we are not shutting
404          * down */
405         if ((forw_packet->num_packets < 3) &&
406             (atomic_read(&module_state) != MODULE_DEACTIVATING))
407                 _add_bcast_packet_to_list(forw_packet, ((5 * HZ) / 1000));
408         else
409                 forw_packet_free(forw_packet);
410 }
411
412 void send_outstanding_bat_packet(struct work_struct *work)
413 {
414         struct delayed_work *delayed_work =
415                 container_of(work, struct delayed_work, work);
416         struct forw_packet *forw_packet =
417                 container_of(delayed_work, struct forw_packet, delayed_work);
418
419         spin_lock(&forw_bat_list_lock);
420         hlist_del(&forw_packet->list);
421         spin_unlock(&forw_bat_list_lock);
422
423         send_packet(forw_packet);
424
425         /**
426          * we have to have at least one packet in the queue
427          * to determine the queues wake up time unless we are
428          * shutting down
429          */
430         if ((forw_packet->own) &&
431             (atomic_read(&module_state) != MODULE_DEACTIVATING))
432                 schedule_own_packet(forw_packet->if_incoming);
433
434         forw_packet_free(forw_packet);
435 }
436
437 void purge_outstanding_packets(void)
438 {
439         struct forw_packet *forw_packet;
440         struct hlist_node *tmp_node, *safe_tmp_node;
441         unsigned long flags;
442
443         bat_dbg(DBG_BATMAN, "purge_outstanding_packets()\n");
444
445         /* free bcast list */
446         spin_lock_irqsave(&forw_bcast_list_lock, flags);
447         hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
448                                   &forw_bcast_list, list) {
449
450                 spin_unlock_irqrestore(&forw_bcast_list_lock, flags);
451
452                 /**
453                  * send_outstanding_bcast_packet() will lock the list to
454                  * delete the item from the list
455                  */
456                 cancel_delayed_work_sync(&forw_packet->delayed_work);
457                 spin_lock_irqsave(&forw_bcast_list_lock, flags);
458         }
459         spin_unlock_irqrestore(&forw_bcast_list_lock, flags);
460
461         /* free batman packet list */
462         spin_lock(&forw_bat_list_lock);
463         hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
464                                   &forw_bat_list, list) {
465
466                 spin_unlock(&forw_bat_list_lock);
467
468                 /**
469                  * send_outstanding_bat_packet() will lock the list to
470                  * delete the item from the list
471                  */
472                 cancel_delayed_work_sync(&forw_packet->delayed_work);
473                 spin_lock(&forw_bat_list_lock);
474         }
475         spin_unlock(&forw_bat_list_lock);
476 }