Staging: batman-adv: Split originator handling parts out of routing.c
[cascardo/linux.git] / drivers / staging / batman-adv / routing.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 "routing.h"
24 #include "send.h"
25 #include "hash.h"
26 #include "soft-interface.h"
27 #include "hard-interface.h"
28 #include "device.h"
29 #include "translation-table.h"
30 #include "originator.h"
31 #include "types.h"
32 #include "ring_buffer.h"
33 #include "vis.h"
34 #include "aggregation.h"
35 #include "compat.h"
36
37 DECLARE_WAIT_QUEUE_HEAD(thread_wait);
38
39 static atomic_t data_ready_cond;
40 atomic_t exit_cond;
41 void slide_own_bcast_window(struct batman_if *batman_if)
42 {
43         struct hash_it_t *hashit = NULL;
44         struct orig_node *orig_node;
45         TYPE_OF_WORD *word;
46
47         spin_lock(&orig_hash_lock);
48
49         while (NULL != (hashit = hash_iterate(orig_hash, hashit))) {
50                 orig_node = hashit->bucket->data;
51                 word = &(orig_node->bcast_own[batman_if->if_num * NUM_WORDS]);
52
53                 bit_get_packet(word, 1, 0);
54                 orig_node->bcast_own_sum[batman_if->if_num] =
55                         bit_packet_count(word);
56         }
57
58         spin_unlock(&orig_hash_lock);
59 }
60
61 static void update_HNA(struct orig_node *orig_node,
62                        unsigned char *hna_buff, int hna_buff_len)
63 {
64         if ((hna_buff_len != orig_node->hna_buff_len) ||
65             ((hna_buff_len > 0) &&
66              (orig_node->hna_buff_len > 0) &&
67              (memcmp(orig_node->hna_buff, hna_buff, hna_buff_len) != 0))) {
68
69                 if (orig_node->hna_buff_len > 0)
70                         hna_global_del_orig(orig_node,
71                                             "originator changed hna");
72
73                 if ((hna_buff_len > 0) && (hna_buff != NULL))
74                         hna_global_add_orig(orig_node, hna_buff, hna_buff_len);
75         }
76 }
77
78 static void update_route(struct orig_node *orig_node,
79                          struct neigh_node *neigh_node,
80                          unsigned char *hna_buff, int hna_buff_len)
81 {
82         char orig_str[ETH_STR_LEN], neigh_str[ETH_STR_LEN];
83         char router_str[ETH_STR_LEN];
84
85         addr_to_string(orig_str, orig_node->orig);
86
87         /* route deleted */
88         if ((orig_node->router != NULL) && (neigh_node == NULL)) {
89
90                 bat_dbg(DBG_ROUTES, "Deleting route towards: %s\n",
91                         orig_str);
92                 hna_global_del_orig(orig_node, "originator timed out");
93
94                 /* route added */
95         } else if ((orig_node->router == NULL) && (neigh_node != NULL)) {
96
97                 addr_to_string(neigh_str, neigh_node->addr);
98                 bat_dbg(DBG_ROUTES,
99                         "Adding route towards: %s (via %s)\n",
100                         orig_str, neigh_str);
101                 hna_global_add_orig(orig_node, hna_buff, hna_buff_len);
102
103                 /* route changed */
104         } else {
105                 addr_to_string(neigh_str, neigh_node->addr);
106                 addr_to_string(router_str, orig_node->router->addr);
107                 bat_dbg(DBG_ROUTES, "Changing route towards: %s (now via %s - was via %s)\n", orig_str, neigh_str, router_str);
108         }
109
110         if (neigh_node != NULL)
111                 orig_node->batman_if = neigh_node->if_incoming;
112         else
113                 orig_node->batman_if = NULL;
114
115         orig_node->router = neigh_node;
116 }
117
118
119 void update_routes(struct orig_node *orig_node,
120                           struct neigh_node *neigh_node,
121                           unsigned char *hna_buff, int hna_buff_len)
122 {
123
124         if (orig_node == NULL)
125                 return;
126
127         if (orig_node->router != neigh_node)
128                 update_route(orig_node, neigh_node, hna_buff, hna_buff_len);
129         /* may be just HNA changed */
130         else
131                 update_HNA(orig_node, hna_buff, hna_buff_len);
132 }
133
134 static int isBidirectionalNeigh(struct orig_node *orig_node,
135                                 struct orig_node *orig_neigh_node,
136                                 struct batman_packet *batman_packet,
137                                 struct batman_if *if_incoming)
138 {
139         struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
140         char orig_str[ETH_STR_LEN], neigh_str[ETH_STR_LEN];
141         unsigned char total_count;
142
143         addr_to_string(orig_str, orig_node->orig);
144         addr_to_string(neigh_str, orig_neigh_node->orig);
145
146         if (orig_node == orig_neigh_node) {
147                 list_for_each_entry(tmp_neigh_node,
148                                     &orig_node->neigh_list,
149                                     list) {
150
151                         if (compare_orig(tmp_neigh_node->addr,
152                                          orig_neigh_node->orig) &&
153                             (tmp_neigh_node->if_incoming == if_incoming))
154                                 neigh_node = tmp_neigh_node;
155                 }
156
157                 if (neigh_node == NULL)
158                         neigh_node = create_neighbor(orig_node,
159                                                      orig_neigh_node,
160                                                      orig_neigh_node->orig,
161                                                      if_incoming);
162
163                 neigh_node->last_valid = jiffies;
164         } else {
165                 /* find packet count of corresponding one hop neighbor */
166                 list_for_each_entry(tmp_neigh_node,
167                                     &orig_neigh_node->neigh_list, list) {
168
169                         if (compare_orig(tmp_neigh_node->addr,
170                                          orig_neigh_node->orig) &&
171                             (tmp_neigh_node->if_incoming == if_incoming))
172                                 neigh_node = tmp_neigh_node;
173                 }
174
175                 if (neigh_node == NULL)
176                         neigh_node = create_neighbor(orig_neigh_node,
177                                                      orig_neigh_node,
178                                                      orig_neigh_node->orig,
179                                                      if_incoming);
180         }
181
182         orig_node->last_valid = jiffies;
183
184         /* pay attention to not get a value bigger than 100 % */
185         total_count = (orig_neigh_node->bcast_own_sum[if_incoming->if_num] >
186                        neigh_node->real_packet_count ?
187                        neigh_node->real_packet_count :
188                        orig_neigh_node->bcast_own_sum[if_incoming->if_num]);
189
190         /* if we have too few packets (too less data) we set tq_own to zero */
191         /* if we receive too few packets it is not considered bidirectional */
192         if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
193             (neigh_node->real_packet_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
194                 orig_neigh_node->tq_own = 0;
195         else
196                 /* neigh_node->real_packet_count is never zero as we
197                  * only purge old information when getting new
198                  * information */
199                 orig_neigh_node->tq_own = (TQ_MAX_VALUE * total_count) /
200                         neigh_node->real_packet_count;
201
202         /*
203          * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
204          * affect the nearly-symmetric links only a little, but
205          * punishes asymmetric links more.  This will give a value
206          * between 0 and TQ_MAX_VALUE
207          */
208         orig_neigh_node->tq_asym_penalty =
209                 TQ_MAX_VALUE -
210                 (TQ_MAX_VALUE *
211                  (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
212                  (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
213                  (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count)) /
214                 (TQ_LOCAL_WINDOW_SIZE *
215                  TQ_LOCAL_WINDOW_SIZE *
216                  TQ_LOCAL_WINDOW_SIZE);
217
218         batman_packet->tq = ((batman_packet->tq *
219                               orig_neigh_node->tq_own *
220                               orig_neigh_node->tq_asym_penalty) /
221                              (TQ_MAX_VALUE *     TQ_MAX_VALUE));
222
223         bat_dbg(DBG_BATMAN, "bidirectional: orig = %-15s neigh = %-15s => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i \n",
224                 orig_str, neigh_str, total_count,
225                 neigh_node->real_packet_count, orig_neigh_node->tq_own,
226                 orig_neigh_node->tq_asym_penalty, batman_packet->tq);
227
228         /* if link has the minimum required transmission quality
229          * consider it bidirectional */
230         if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
231                 return 1;
232
233         return 0;
234 }
235
236 static void update_orig(struct orig_node *orig_node, struct ethhdr *ethhdr,
237                         struct batman_packet *batman_packet,
238                         struct batman_if *if_incoming,
239                         unsigned char *hna_buff, int hna_buff_len,
240                         char is_duplicate)
241 {
242         struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
243         int tmp_hna_buff_len;
244
245         bat_dbg(DBG_BATMAN, "update_originator(): Searching and updating originator entry of received packet \n");
246
247         list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
248                 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
249                     (tmp_neigh_node->if_incoming == if_incoming)) {
250                         neigh_node = tmp_neigh_node;
251                         continue;
252                 }
253
254                 if (is_duplicate)
255                         continue;
256
257                 ring_buffer_set(tmp_neigh_node->tq_recv,
258                                 &tmp_neigh_node->tq_index, 0);
259                 tmp_neigh_node->tq_avg =
260                         ring_buffer_avg(tmp_neigh_node->tq_recv);
261         }
262
263         if (neigh_node == NULL)
264                 neigh_node = create_neighbor(orig_node,
265                                              get_orig_node(ethhdr->h_source),
266                                              ethhdr->h_source, if_incoming);
267         else
268                 bat_dbg(DBG_BATMAN,
269                         "Updating existing last-hop neighbour of originator\n");
270
271         orig_node->flags = batman_packet->flags;
272         neigh_node->last_valid = jiffies;
273
274         ring_buffer_set(neigh_node->tq_recv,
275                         &neigh_node->tq_index,
276                         batman_packet->tq);
277         neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
278
279         if (!is_duplicate) {
280                 orig_node->last_ttl = batman_packet->ttl;
281                 neigh_node->last_ttl = batman_packet->ttl;
282         }
283
284         tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
285                             batman_packet->num_hna * ETH_ALEN : hna_buff_len);
286
287         /* if this neighbor already is our next hop there is nothing
288          * to change */
289         if (orig_node->router == neigh_node)
290                 goto update_hna;
291
292         /* if this neighbor does not offer a better TQ we won't consider it */
293         if ((orig_node->router) &&
294             (orig_node->router->tq_avg > neigh_node->tq_avg))
295                 goto update_hna;
296
297         /* if the TQ is the same and the link not more symetric we
298          * won't consider it either */
299         if ((orig_node->router) &&
300              ((neigh_node->tq_avg == orig_node->router->tq_avg) &&
301              (orig_node->router->orig_node->bcast_own_sum[if_incoming->if_num]
302               >= neigh_node->orig_node->bcast_own_sum[if_incoming->if_num])))
303                 goto update_hna;
304
305         update_routes(orig_node, neigh_node, hna_buff, tmp_hna_buff_len);
306         return;
307
308 update_hna:
309         update_routes(orig_node, orig_node->router, hna_buff, tmp_hna_buff_len);
310         return;
311 }
312
313 static char count_real_packets(struct ethhdr *ethhdr,
314                                struct batman_packet *batman_packet,
315                                struct batman_if *if_incoming)
316 {
317         struct orig_node *orig_node;
318         struct neigh_node *tmp_neigh_node;
319         char is_duplicate = 0;
320         uint16_t seq_diff;
321
322         orig_node = get_orig_node(batman_packet->orig);
323         if (orig_node == NULL)
324                 return 0;
325
326         list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
327
328                 if (!is_duplicate)
329                         is_duplicate =
330                                 get_bit_status(tmp_neigh_node->real_bits,
331                                                orig_node->last_real_seqno,
332                                                batman_packet->seqno);
333                 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
334                 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
335                     (tmp_neigh_node->if_incoming == if_incoming))
336                         bit_get_packet(tmp_neigh_node->real_bits, seq_diff, 1);
337                 else
338                         bit_get_packet(tmp_neigh_node->real_bits, seq_diff, 0);
339
340                 tmp_neigh_node->real_packet_count =
341                         bit_packet_count(tmp_neigh_node->real_bits);
342         }
343
344         if (!is_duplicate) {
345                 bat_dbg(DBG_BATMAN, "updating last_seqno: old %d, new %d \n",
346                         orig_node->last_real_seqno, batman_packet->seqno);
347                 orig_node->last_real_seqno = batman_packet->seqno;
348         }
349
350         return is_duplicate;
351 }
352
353 void receive_bat_packet(struct ethhdr *ethhdr,
354                         struct batman_packet *batman_packet,
355                         unsigned char *hna_buff,
356                         int hna_buff_len,
357                         struct batman_if *if_incoming)
358 {
359         struct batman_if *batman_if;
360         struct orig_node *orig_neigh_node, *orig_node;
361         char orig_str[ETH_STR_LEN], prev_sender_str[ETH_STR_LEN];
362         char neigh_str[ETH_STR_LEN];
363         char has_directlink_flag;
364         char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
365         char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
366         char is_duplicate;
367         unsigned short if_incoming_seqno;
368
369         /* Silently drop when the batman packet is actually not a
370          * correct packet.
371          *
372          * This might happen if a packet is padded (e.g. Ethernet has a
373          * minimum frame length of 64 byte) and the aggregation interprets
374          * it as an additional length.
375          *
376          * TODO: A more sane solution would be to have a bit in the
377          * batman_packet to detect whether the packet is the last
378          * packet in an aggregation.  Here we expect that the padding
379          * is always zero (or not 0x01)
380          */
381         if (batman_packet->packet_type != BAT_PACKET)
382                 return;
383
384         /* could be changed by schedule_own_packet() */
385         if_incoming_seqno = atomic_read(&if_incoming->seqno);
386
387         addr_to_string(orig_str, batman_packet->orig);
388         addr_to_string(prev_sender_str, batman_packet->prev_sender);
389         addr_to_string(neigh_str, ethhdr->h_source);
390
391         has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
392
393         is_single_hop_neigh = (compare_orig(ethhdr->h_source,
394                                             batman_packet->orig) ? 1 : 0);
395
396         bat_dbg(DBG_BATMAN, "Received BATMAN packet via NB: %s, IF: %s [%s] (from OG: %s, via prev OG: %s, seqno %d, tq %d, TTL %d, V %d, IDF %d) \n",
397                 neigh_str, if_incoming->dev, if_incoming->addr_str,
398                 orig_str, prev_sender_str, batman_packet->seqno,
399                 batman_packet->tq, batman_packet->ttl, batman_packet->version,
400                 has_directlink_flag);
401
402         list_for_each_entry_rcu(batman_if, &if_list, list) {
403                 if (batman_if->if_active != IF_ACTIVE)
404                         continue;
405
406                 if (compare_orig(ethhdr->h_source,
407                                  batman_if->net_dev->dev_addr))
408                         is_my_addr = 1;
409
410                 if (compare_orig(batman_packet->orig,
411                                  batman_if->net_dev->dev_addr))
412                         is_my_orig = 1;
413
414                 if (compare_orig(batman_packet->prev_sender,
415                                  batman_if->net_dev->dev_addr))
416                         is_my_oldorig = 1;
417
418                 if (compare_orig(ethhdr->h_source, broadcastAddr))
419                         is_broadcast = 1;
420         }
421
422         if (batman_packet->version != COMPAT_VERSION) {
423                 bat_dbg(DBG_BATMAN,
424                         "Drop packet: incompatible batman version (%i)\n",
425                         batman_packet->version);
426                 return;
427         }
428
429         if (is_my_addr) {
430                 bat_dbg(DBG_BATMAN,
431                         "Drop packet: received my own broadcast (sender: %s)\n",
432                         neigh_str);
433                 return;
434         }
435
436         if (is_broadcast) {
437                 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all packets with broadcast source addr (sender: %s) \n", neigh_str);
438                 return;
439         }
440
441         if (is_my_orig) {
442                 TYPE_OF_WORD *word;
443                 int offset;
444
445                 orig_neigh_node = get_orig_node(ethhdr->h_source);
446
447                 /* neighbour has to indicate direct link and it has to
448                  * come via the corresponding interface */
449                 /* if received seqno equals last send seqno save new
450                  * seqno for bidirectional check */
451                 if (has_directlink_flag &&
452                     compare_orig(if_incoming->net_dev->dev_addr,
453                                  batman_packet->orig) &&
454                     (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
455                         offset = if_incoming->if_num * NUM_WORDS;
456                         word = &(orig_neigh_node->bcast_own[offset]);
457                         bit_mark(word, 0);
458                         orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
459                                 bit_packet_count(word);
460                 }
461
462                 bat_dbg(DBG_BATMAN, "Drop packet: originator packet from myself (via neighbour) \n");
463                 return;
464         }
465
466         if (batman_packet->tq == 0) {
467                 count_real_packets(ethhdr, batman_packet, if_incoming);
468
469                 bat_dbg(DBG_BATMAN, "Drop packet: originator packet with tq equal 0 \n");
470                 return;
471         }
472
473         if (is_my_oldorig) {
474                 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all rebroadcast echos (sender: %s) \n", neigh_str);
475                 return;
476         }
477
478         is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
479
480         orig_node = get_orig_node(batman_packet->orig);
481         if (orig_node == NULL)
482                 return;
483
484         /* avoid temporary routing loops */
485         if ((orig_node->router) &&
486             (orig_node->router->orig_node->router) &&
487             (compare_orig(orig_node->router->addr,
488                           batman_packet->prev_sender)) &&
489             !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
490             (compare_orig(orig_node->router->addr,
491                           orig_node->router->orig_node->router->addr))) {
492                 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %s) \n", neigh_str);
493                 return;
494         }
495
496         /* if sender is a direct neighbor the sender mac equals
497          * originator mac */
498         orig_neigh_node = (is_single_hop_neigh ?
499                            orig_node : get_orig_node(ethhdr->h_source));
500         if (orig_neigh_node == NULL)
501                 return;
502
503         /* drop packet if sender is not a direct neighbor and if we
504          * don't route towards it */
505         if (!is_single_hop_neigh &&
506             (orig_neigh_node->router == NULL)) {
507                 bat_dbg(DBG_BATMAN, "Drop packet: OGM via unknown neighbor!\n");
508                 return;
509         }
510
511         is_bidirectional = isBidirectionalNeigh(orig_node, orig_neigh_node,
512                                                 batman_packet, if_incoming);
513
514         /* update ranking if it is not a duplicate or has the same
515          * seqno and similar ttl as the non-duplicate */
516         if (is_bidirectional &&
517             (!is_duplicate ||
518              ((orig_node->last_real_seqno == batman_packet->seqno) &&
519               (orig_node->last_ttl - 3 <= batman_packet->ttl))))
520                 update_orig(orig_node, ethhdr, batman_packet,
521                             if_incoming, hna_buff, hna_buff_len, is_duplicate);
522
523         /* is single hop (direct) neighbour */
524         if (is_single_hop_neigh) {
525
526                 /* mark direct link on incoming interface */
527                 schedule_forward_packet(orig_node, ethhdr, batman_packet,
528                                         1, hna_buff_len, if_incoming);
529
530                 bat_dbg(DBG_BATMAN, "Forwarding packet: rebroadcast neighbour packet with direct link flag\n");
531                 return;
532         }
533
534         /* multihop originator */
535         if (!is_bidirectional) {
536                 bat_dbg(DBG_BATMAN,
537                         "Drop packet: not received via bidirectional link\n");
538                 return;
539         }
540
541         if (is_duplicate) {
542                 bat_dbg(DBG_BATMAN, "Drop packet: duplicate packet received\n");
543                 return;
544         }
545
546         bat_dbg(DBG_BATMAN,
547                 "Forwarding packet: rebroadcast originator packet\n");
548         schedule_forward_packet(orig_node, ethhdr, batman_packet,
549                                 0, hna_buff_len, if_incoming);
550 }
551
552
553 static int receive_raw_packet(struct socket *raw_sock,
554                               unsigned char *packet_buff, int packet_buff_len)
555 {
556         struct kvec iov;
557         struct msghdr msg;
558
559         iov.iov_base = packet_buff;
560         iov.iov_len = packet_buff_len;
561
562         msg.msg_flags = MSG_DONTWAIT;   /* non-blocking */
563         msg.msg_name = NULL;
564         msg.msg_namelen = 0;
565         msg.msg_control = NULL;
566
567         return kernel_recvmsg(raw_sock, &msg, &iov, 1, packet_buff_len,
568                               MSG_DONTWAIT);
569 }
570
571 static void recv_bat_packet(struct ethhdr *ethhdr,
572                             unsigned char *packet_buff,
573                             int result,
574                             struct batman_if *batman_if)
575 {
576         /* packet with broadcast indication but unicast recipient */
577         if (!is_bcast(ethhdr->h_dest))
578                 return;
579
580         /* packet with broadcast sender address */
581         if (is_bcast(ethhdr->h_source))
582                 return;
583
584         /* drop packet if it has not at least one batman packet as payload */
585         if (result < sizeof(struct ethhdr) + sizeof(struct batman_packet))
586                 return;
587
588         spin_lock(&orig_hash_lock);
589         receive_aggr_bat_packet(ethhdr,
590                                 packet_buff + sizeof(struct ethhdr),
591                                 result - sizeof(struct ethhdr),
592                                 batman_if);
593         spin_unlock(&orig_hash_lock);
594 }
595
596 static void recv_my_icmp_packet(struct ethhdr *ethhdr,
597                                 struct icmp_packet *icmp_packet,
598                                 unsigned char *packet_buff,
599                                 int result)
600 {
601         struct orig_node *orig_node;
602
603         /* add data to device queue */
604         if (icmp_packet->msg_type != ECHO_REQUEST) {
605                 bat_device_receive_packet(icmp_packet);
606                 return;
607         }
608
609         /* answer echo request (ping) */
610         /* get routing information */
611         spin_lock(&orig_hash_lock);
612         orig_node = ((struct orig_node *)hash_find(orig_hash,
613                                                    icmp_packet->orig));
614
615         if ((orig_node != NULL) &&
616             (orig_node->batman_if != NULL) &&
617             (orig_node->router != NULL)) {
618                 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
619                 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
620                 icmp_packet->msg_type = ECHO_REPLY;
621                 icmp_packet->ttl = TTL;
622
623                 send_raw_packet(packet_buff + sizeof(struct ethhdr),
624                                 result - sizeof(struct ethhdr),
625                                 orig_node->batman_if,
626                                 orig_node->router->addr);
627         }
628
629         spin_unlock(&orig_hash_lock);
630         return;
631 }
632
633 static void recv_icmp_ttl_exceeded(struct icmp_packet *icmp_packet,
634                                    struct ethhdr *ethhdr,
635                                    unsigned char *packet_buff,
636                                    int result,
637                                    struct batman_if *batman_if)
638 {
639         unsigned char src_str[ETH_STR_LEN], dst_str[ETH_STR_LEN];
640         struct orig_node *orig_node;
641
642         addr_to_string(src_str, icmp_packet->orig);
643         addr_to_string(dst_str, icmp_packet->dst);
644
645         printk(KERN_WARNING "batman-adv:Warning - can't send packet from %s to %s: ttl exceeded\n", src_str, dst_str);
646
647         /* send TTL exceeded if packet is an echo request (traceroute) */
648         if (icmp_packet->msg_type != ECHO_REQUEST)
649                 return;
650
651         /* get routing information */
652         spin_lock(&orig_hash_lock);
653         orig_node = ((struct orig_node *)
654                      hash_find(orig_hash, icmp_packet->orig));
655
656         if ((orig_node != NULL) &&
657             (orig_node->batman_if != NULL) &&
658             (orig_node->router != NULL)) {
659                 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
660                 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
661                 icmp_packet->msg_type = TTL_EXCEEDED;
662                 icmp_packet->ttl = TTL;
663
664                 send_raw_packet(packet_buff + sizeof(struct ethhdr),
665                                 result - sizeof(struct ethhdr),
666                                 orig_node->batman_if,
667                                 orig_node->router->addr);
668
669         }
670
671         spin_unlock(&orig_hash_lock);
672 }
673
674
675
676 static void recv_icmp_packet(struct ethhdr *ethhdr,
677                              unsigned char *packet_buff,
678                              int result,
679                              struct batman_if *batman_if)
680 {
681         struct icmp_packet *icmp_packet;
682         struct orig_node *orig_node;
683
684         /* packet with unicast indication but broadcast recipient */
685         if (is_bcast(ethhdr->h_dest))
686                 return;
687
688         /* packet with broadcast sender address */
689         if (is_bcast(ethhdr->h_source))
690                 return;
691
692         /* not for me */
693         if (!is_my_mac(ethhdr->h_dest))
694                 return;
695
696         /* drop packet if it has not necessary minimum size */
697         if (result < sizeof(struct ethhdr) + sizeof(struct icmp_packet))
698                 return;
699
700         icmp_packet = (struct icmp_packet *)
701                 (packet_buff + sizeof(struct ethhdr));
702
703         /* packet for me */
704         if (is_my_mac(icmp_packet->dst))
705                 recv_my_icmp_packet(ethhdr, icmp_packet, packet_buff, result);
706
707         /* TTL exceeded */
708         if (icmp_packet->ttl < 2) {
709                 recv_icmp_ttl_exceeded(icmp_packet, ethhdr, packet_buff, result,
710                                        batman_if);
711                 return;
712
713         }
714
715         /* get routing information */
716         spin_lock(&orig_hash_lock);
717         orig_node = ((struct orig_node *)
718                      hash_find(orig_hash, icmp_packet->dst));
719
720         if ((orig_node != NULL) &&
721             (orig_node->batman_if != NULL) &&
722             (orig_node->router != NULL)) {
723
724                 /* decrement ttl */
725                 icmp_packet->ttl--;
726
727                 /* route it */
728                 send_raw_packet(packet_buff + sizeof(struct ethhdr),
729                                 result - sizeof(struct ethhdr),
730                                 orig_node->batman_if,
731                                 orig_node->router->addr);
732         }
733         spin_unlock(&orig_hash_lock);
734 }
735
736 static void recv_unicast_packet(struct ethhdr *ethhdr,
737                                 unsigned char *packet_buff,
738                                 int result,
739                                 struct batman_if *batman_if)
740 {
741         struct unicast_packet *unicast_packet;
742         unsigned char src_str[ETH_STR_LEN], dst_str[ETH_STR_LEN];
743         struct orig_node *orig_node;
744         int hdr_size = sizeof(struct ethhdr) + sizeof(struct unicast_packet);
745
746         /* packet with unicast indication but broadcast recipient */
747         if (is_bcast(ethhdr->h_dest))
748                 return;
749
750         /* packet with broadcast sender address */
751         if (is_bcast(ethhdr->h_source))
752                 return;
753
754         /* not for me */
755         if (!is_my_mac(ethhdr->h_dest))
756                 return;
757
758         /* drop packet if it has not necessary minimum size */
759         if (result < hdr_size)
760                 return;
761
762         unicast_packet = (struct unicast_packet *)
763                 (packet_buff + sizeof(struct ethhdr));
764
765         /* packet for me */
766         if (is_my_mac(unicast_packet->dest)) {
767                 interface_rx(soft_device, packet_buff + hdr_size,
768                              result - hdr_size);
769                 return;
770
771         }
772
773         /* TTL exceeded */
774         if (unicast_packet->ttl < 2) {
775                 addr_to_string(src_str, ((struct ethhdr *)
776                                          (unicast_packet + 1))->h_source);
777                 addr_to_string(dst_str, unicast_packet->dest);
778
779                 printk(KERN_WARNING "batman-adv:Warning - can't send packet from %s to %s: ttl exceeded\n", src_str, dst_str);
780                 return;
781         }
782
783         /* get routing information */
784         spin_lock(&orig_hash_lock);
785         orig_node = ((struct orig_node *)
786                      hash_find(orig_hash, unicast_packet->dest));
787
788         if ((orig_node != NULL) &&
789             (orig_node->batman_if != NULL) &&
790             (orig_node->router != NULL)) {
791                 /* decrement ttl */
792                 unicast_packet->ttl--;
793
794                 /* route it */
795                 send_raw_packet(packet_buff + sizeof(struct ethhdr),
796                                 result - sizeof(struct ethhdr),
797                                 orig_node->batman_if,
798                                 orig_node->router->addr);
799         }
800         spin_unlock(&orig_hash_lock);
801 }
802
803
804 static void recv_bcast_packet(struct ethhdr *ethhdr,
805                               unsigned char *packet_buff,
806                               int result,
807                               struct batman_if *batman_if)
808 {
809         struct orig_node *orig_node;
810         struct bcast_packet *bcast_packet;
811         int hdr_size = sizeof(struct ethhdr) + sizeof(struct bcast_packet);
812
813         /* packet with broadcast indication but unicast recipient */
814         if (!is_bcast(ethhdr->h_dest))
815                 return;
816
817         /* packet with broadcast sender address */
818         if (is_bcast(ethhdr->h_source))
819                 return;
820
821         /* drop packet if it has not necessary minimum size */
822         if (result < hdr_size)
823                 return;
824
825         /* ignore broadcasts sent by myself */
826         if (is_my_mac(ethhdr->h_source))
827                 return;
828
829         bcast_packet = (struct bcast_packet *)
830                 (packet_buff + sizeof(struct ethhdr));
831
832         /* ignore broadcasts originated by myself */
833         if (is_my_mac(bcast_packet->orig))
834                 return;
835
836         spin_lock(&orig_hash_lock);
837         orig_node = ((struct orig_node *)
838                      hash_find(orig_hash, bcast_packet->orig));
839
840         if (orig_node == NULL) {
841                 spin_unlock(&orig_hash_lock);
842                 return;
843         }
844
845         /* check flood history */
846         if (get_bit_status(orig_node->bcast_bits,
847                            orig_node->last_bcast_seqno,
848                            ntohs(bcast_packet->seqno))) {
849                 spin_unlock(&orig_hash_lock);
850                 return;
851         }
852
853         /* mark broadcast in flood history */
854         if (bit_get_packet(orig_node->bcast_bits,
855                            ntohs(bcast_packet->seqno) -
856                            orig_node->last_bcast_seqno, 1))
857                 orig_node->last_bcast_seqno = ntohs(bcast_packet->seqno);
858
859         spin_unlock(&orig_hash_lock);
860
861         /* broadcast for me */
862         interface_rx(soft_device, packet_buff + hdr_size, result - hdr_size);
863
864         /* rebroadcast packet */
865         add_bcast_packet_to_list(packet_buff + sizeof(struct ethhdr),
866                                  result - sizeof(struct ethhdr));
867 }
868
869 static void recv_vis_packet(struct ethhdr *ethhdr,
870                             unsigned char *packet_buff,
871                             int result)
872 {
873         struct vis_packet *vis_packet;
874         int hdr_size = sizeof(struct ethhdr) + sizeof(struct vis_packet);
875         int vis_info_len;
876
877         /* drop if too short. */
878         if (result < hdr_size)
879                 return;
880
881         /* not for me */
882         if (!is_my_mac(ethhdr->h_dest))
883                 return;
884
885         vis_packet = (struct vis_packet *)(packet_buff + sizeof(struct ethhdr));
886         vis_info_len = result  - hdr_size;
887
888         /* ignore own packets */
889         if (is_my_mac(vis_packet->vis_orig))
890                 return;
891
892         if (is_my_mac(vis_packet->sender_orig))
893                 return;
894
895         switch (vis_packet->vis_type) {
896         case VIS_TYPE_SERVER_SYNC:
897                 receive_server_sync_packet(vis_packet, vis_info_len);
898                 break;
899
900         case VIS_TYPE_CLIENT_UPDATE:
901                 receive_client_update_packet(vis_packet, vis_info_len);
902                 break;
903
904         default:        /* ignore unknown packet */
905                 break;
906         }
907 }
908
909 static int recv_one_packet(struct batman_if *batman_if,
910                            unsigned char *packet_buff)
911 {
912         int result;
913         struct ethhdr *ethhdr;
914         struct batman_packet *batman_packet;
915
916         result = receive_raw_packet(batman_if->raw_sock, packet_buff,
917                                     PACKBUFF_SIZE);
918         if (result <= 0)
919                 return result;
920
921         if (result < sizeof(struct ethhdr) + 2)
922                 return 0;
923
924         ethhdr = (struct ethhdr *)packet_buff;
925         batman_packet = (struct batman_packet *)
926                 (packet_buff + sizeof(struct ethhdr));
927
928         if (batman_packet->version != COMPAT_VERSION) {
929                 bat_dbg(DBG_BATMAN,
930                         "Drop packet: incompatible batman version (%i)\n",
931                         batman_packet->version);
932                 return 0;
933         }
934
935         switch (batman_packet->packet_type) {
936                 /* batman originator packet */
937         case BAT_PACKET:
938                 recv_bat_packet(ethhdr, packet_buff, result, batman_if);
939                 break;
940
941                 /* batman icmp packet */
942         case BAT_ICMP:
943                 recv_icmp_packet(ethhdr, packet_buff, result, batman_if);
944                 break;
945
946                 /* unicast packet */
947         case BAT_UNICAST:
948                 recv_unicast_packet(ethhdr, packet_buff, result, batman_if);
949                 break;
950
951                 /* broadcast packet */
952         case BAT_BCAST:
953                 recv_bcast_packet(ethhdr,
954                                   packet_buff, result, batman_if);
955                 break;
956
957                 /* vis packet */
958         case BAT_VIS:
959                 recv_vis_packet(ethhdr, packet_buff, result);
960                 break;
961         }
962         return 0;
963 }
964
965
966 static int discard_one_packet(struct batman_if *batman_if,
967                               unsigned char *packet_buff)
968 {
969         int result = -EAGAIN;
970
971         if (batman_if->raw_sock) {
972                 result = receive_raw_packet(batman_if->raw_sock,
973                                             packet_buff,
974                                             PACKBUFF_SIZE);
975         }
976         return result;
977 }
978
979
980 static bool is_interface_active(struct batman_if *batman_if)
981 {
982         if (batman_if->if_active != IF_ACTIVE)
983                 return false;
984
985         return true;
986 }
987
988 static void service_interface(struct batman_if *batman_if,
989                               unsigned char *packet_buff)
990
991 {
992         int result;
993
994         do {
995                 if (is_interface_active(batman_if))
996                         result = recv_one_packet(batman_if, packet_buff);
997                 else
998                         result = discard_one_packet(batman_if, packet_buff);
999         } while (result >= 0);
1000
1001         /* we perform none blocking reads, so EAGAIN indicates there
1002            are no more packets to read. Anything else is a real
1003            error.*/
1004
1005         if ((result < 0) && (result != -EAGAIN))
1006                 printk(KERN_ERR "batman-adv:Could not receive packet from interface %s: %i\n", batman_if->dev, result);
1007 }
1008
1009 static void service_interfaces(unsigned char *packet_buffer)
1010 {
1011         struct batman_if *batman_if;
1012         rcu_read_lock();
1013         list_for_each_entry_rcu(batman_if, &if_list, list) {
1014                 rcu_read_unlock();
1015                 service_interface(batman_if, packet_buffer);
1016                 rcu_read_lock();
1017         }
1018         rcu_read_unlock();
1019 }
1020
1021
1022 int packet_recv_thread(void *data)
1023 {
1024         unsigned char *packet_buff;
1025
1026         atomic_set(&data_ready_cond, 0);
1027         atomic_set(&exit_cond, 0);
1028         packet_buff = kmalloc(PACKBUFF_SIZE, GFP_KERNEL);
1029         if (!packet_buff) {
1030                 printk(KERN_ERR"batman-adv:Could allocate memory for the packet buffer. :(\n");
1031                 return -1;
1032         }
1033
1034         while ((!kthread_should_stop()) && (!atomic_read(&exit_cond))) {
1035
1036                 wait_event_interruptible(thread_wait,
1037                                          (atomic_read(&data_ready_cond) ||
1038                                           atomic_read(&exit_cond)));
1039
1040                 atomic_set(&data_ready_cond, 0);
1041
1042                 if (kthread_should_stop() || atomic_read(&exit_cond))
1043                         break;
1044
1045                 service_interfaces(packet_buff);
1046         }
1047         kfree(packet_buff);
1048
1049         /* do not exit until kthread_stop() is actually called,
1050          * otherwise it will wait for us forever. */
1051         while (!kthread_should_stop())
1052                 schedule();
1053
1054         return 0;
1055 }
1056
1057 void batman_data_ready(struct sock *sk, int len)
1058 {
1059         void (*data_ready)(struct sock *, int) = sk->sk_user_data;
1060
1061         data_ready(sk, len);
1062
1063         atomic_set(&data_ready_cond, 1);
1064         wake_up_interruptible(&thread_wait);
1065 }
1066