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