Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[GitHub/mt8127/android_kernel_alcatel_ttab.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 "icmp_socket.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 static 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 /* FIXME: each orig_node->batman_if will be attached to a softif */
81 struct bat_priv *bat_priv = netdev_priv(soft_device);
82
83 /* route deleted */
84 if ((orig_node->router != NULL) && (neigh_node == NULL)) {
85
86 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
87 orig_node->orig);
88 hna_global_del_orig(orig_node, "originator timed out");
89
90 /* route added */
91 } else if ((orig_node->router == NULL) && (neigh_node != NULL)) {
92
93 bat_dbg(DBG_ROUTES, bat_priv,
94 "Adding route towards: %pM (via %pM)\n",
95 orig_node->orig, neigh_node->addr);
96 hna_global_add_orig(orig_node, hna_buff, hna_buff_len);
97
98 /* route changed */
99 } else {
100 bat_dbg(DBG_ROUTES, bat_priv,
101 "Changing route towards: %pM "
102 "(now via %pM - was via %pM)\n",
103 orig_node->orig, neigh_node->addr,
104 orig_node->router->addr);
105 }
106
107 orig_node->router = neigh_node;
108 }
109
110
111 void update_routes(struct orig_node *orig_node,
112 struct neigh_node *neigh_node,
113 unsigned char *hna_buff, int hna_buff_len)
114 {
115
116 if (orig_node == NULL)
117 return;
118
119 if (orig_node->router != neigh_node)
120 update_route(orig_node, neigh_node, hna_buff, hna_buff_len);
121 /* may be just HNA changed */
122 else
123 update_HNA(orig_node, hna_buff, hna_buff_len);
124 }
125
126 static int is_bidirectional_neigh(struct orig_node *orig_node,
127 struct orig_node *orig_neigh_node,
128 struct batman_packet *batman_packet,
129 struct batman_if *if_incoming)
130 {
131 /* FIXME: each orig_node->batman_if will be attached to a softif */
132 struct bat_priv *bat_priv = netdev_priv(soft_device);
133 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
134 unsigned char total_count;
135
136 if (orig_node == orig_neigh_node) {
137 list_for_each_entry(tmp_neigh_node,
138 &orig_node->neigh_list,
139 list) {
140
141 if (compare_orig(tmp_neigh_node->addr,
142 orig_neigh_node->orig) &&
143 (tmp_neigh_node->if_incoming == if_incoming))
144 neigh_node = tmp_neigh_node;
145 }
146
147 if (!neigh_node)
148 neigh_node = create_neighbor(orig_node,
149 orig_neigh_node,
150 orig_neigh_node->orig,
151 if_incoming);
152 /* create_neighbor failed, return 0 */
153 if (!neigh_node)
154 return 0;
155
156 neigh_node->last_valid = jiffies;
157 } else {
158 /* find packet count of corresponding one hop neighbor */
159 list_for_each_entry(tmp_neigh_node,
160 &orig_neigh_node->neigh_list, list) {
161
162 if (compare_orig(tmp_neigh_node->addr,
163 orig_neigh_node->orig) &&
164 (tmp_neigh_node->if_incoming == if_incoming))
165 neigh_node = tmp_neigh_node;
166 }
167
168 if (!neigh_node)
169 neigh_node = create_neighbor(orig_neigh_node,
170 orig_neigh_node,
171 orig_neigh_node->orig,
172 if_incoming);
173 /* create_neighbor failed, return 0 */
174 if (!neigh_node)
175 return 0;
176 }
177
178 orig_node->last_valid = jiffies;
179
180 /* pay attention to not get a value bigger than 100 % */
181 total_count = (orig_neigh_node->bcast_own_sum[if_incoming->if_num] >
182 neigh_node->real_packet_count ?
183 neigh_node->real_packet_count :
184 orig_neigh_node->bcast_own_sum[if_incoming->if_num]);
185
186 /* if we have too few packets (too less data) we set tq_own to zero */
187 /* if we receive too few packets it is not considered bidirectional */
188 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
189 (neigh_node->real_packet_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
190 orig_neigh_node->tq_own = 0;
191 else
192 /* neigh_node->real_packet_count is never zero as we
193 * only purge old information when getting new
194 * information */
195 orig_neigh_node->tq_own = (TQ_MAX_VALUE * total_count) /
196 neigh_node->real_packet_count;
197
198 /*
199 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
200 * affect the nearly-symmetric links only a little, but
201 * punishes asymmetric links more. This will give a value
202 * between 0 and TQ_MAX_VALUE
203 */
204 orig_neigh_node->tq_asym_penalty =
205 TQ_MAX_VALUE -
206 (TQ_MAX_VALUE *
207 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
208 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
209 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count)) /
210 (TQ_LOCAL_WINDOW_SIZE *
211 TQ_LOCAL_WINDOW_SIZE *
212 TQ_LOCAL_WINDOW_SIZE);
213
214 batman_packet->tq = ((batman_packet->tq *
215 orig_neigh_node->tq_own *
216 orig_neigh_node->tq_asym_penalty) /
217 (TQ_MAX_VALUE * TQ_MAX_VALUE));
218
219 bat_dbg(DBG_BATMAN, bat_priv,
220 "bidirectional: "
221 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
222 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
223 "total tq: %3i\n",
224 orig_node->orig, orig_neigh_node->orig, 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 /* FIXME: get bat_priv */
243 struct bat_priv *bat_priv = netdev_priv(soft_device);
244 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
245 int tmp_hna_buff_len;
246
247 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
248 "Searching and updating originator entry of received packet\n");
249
250 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
251 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
252 (tmp_neigh_node->if_incoming == if_incoming)) {
253 neigh_node = tmp_neigh_node;
254 continue;
255 }
256
257 if (is_duplicate)
258 continue;
259
260 ring_buffer_set(tmp_neigh_node->tq_recv,
261 &tmp_neigh_node->tq_index, 0);
262 tmp_neigh_node->tq_avg =
263 ring_buffer_avg(tmp_neigh_node->tq_recv);
264 }
265
266 if (!neigh_node) {
267 struct orig_node *orig_tmp;
268
269 orig_tmp = get_orig_node(ethhdr->h_source);
270 if (!orig_tmp)
271 return;
272
273 neigh_node = create_neighbor(orig_node,
274 orig_tmp,
275 ethhdr->h_source, if_incoming);
276 if (!neigh_node)
277 return;
278 } else
279 bat_dbg(DBG_BATMAN, bat_priv,
280 "Updating existing last-hop neighbor of originator\n");
281
282 orig_node->flags = batman_packet->flags;
283 neigh_node->last_valid = jiffies;
284
285 ring_buffer_set(neigh_node->tq_recv,
286 &neigh_node->tq_index,
287 batman_packet->tq);
288 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
289
290 if (!is_duplicate) {
291 orig_node->last_ttl = batman_packet->ttl;
292 neigh_node->last_ttl = batman_packet->ttl;
293 }
294
295 tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
296 batman_packet->num_hna * ETH_ALEN : hna_buff_len);
297
298 /* if this neighbor already is our next hop there is nothing
299 * to change */
300 if (orig_node->router == neigh_node)
301 goto update_hna;
302
303 /* if this neighbor does not offer a better TQ we won't consider it */
304 if ((orig_node->router) &&
305 (orig_node->router->tq_avg > neigh_node->tq_avg))
306 goto update_hna;
307
308 /* if the TQ is the same and the link not more symetric we
309 * won't consider it either */
310 if ((orig_node->router) &&
311 ((neigh_node->tq_avg == orig_node->router->tq_avg) &&
312 (orig_node->router->orig_node->bcast_own_sum[if_incoming->if_num]
313 >= neigh_node->orig_node->bcast_own_sum[if_incoming->if_num])))
314 goto update_hna;
315
316 update_routes(orig_node, neigh_node, hna_buff, tmp_hna_buff_len);
317 return;
318
319 update_hna:
320 update_routes(orig_node, orig_node->router, hna_buff, tmp_hna_buff_len);
321 }
322
323 /* checks whether the host restarted and is in the protection time.
324 * returns:
325 * 0 if the packet is to be accepted
326 * 1 if the packet is to be ignored.
327 */
328 static int window_protected(int32_t seq_num_diff,
329 unsigned long *last_reset)
330 {
331 /* FIXME: each orig_node->batman_if will be attached to a softif */
332 struct bat_priv *bat_priv = netdev_priv(soft_device);
333
334 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
335 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
336 if (time_after(jiffies, *last_reset +
337 msecs_to_jiffies(RESET_PROTECTION_MS))) {
338
339 *last_reset = jiffies;
340 bat_dbg(DBG_BATMAN, bat_priv,
341 "old packet received, start protection\n");
342
343 return 0;
344 } else
345 return 1;
346 }
347 return 0;
348 }
349
350 /* processes a batman packet for all interfaces, adjusts the sequence number and
351 * finds out whether it is a duplicate.
352 * returns:
353 * 1 the packet is a duplicate
354 * 0 the packet has not yet been received
355 * -1 the packet is old and has been received while the seqno window
356 * was protected. Caller should drop it.
357 */
358 static char count_real_packets(struct ethhdr *ethhdr,
359 struct batman_packet *batman_packet,
360 struct batman_if *if_incoming)
361 {
362 /* FIXME: each orig_node->batman_if will be attached to a softif */
363 struct bat_priv *bat_priv = netdev_priv(soft_device);
364 struct orig_node *orig_node;
365 struct neigh_node *tmp_neigh_node;
366 char is_duplicate = 0;
367 int32_t seq_diff;
368 int need_update = 0;
369 int set_mark;
370
371 orig_node = get_orig_node(batman_packet->orig);
372 if (orig_node == NULL)
373 return 0;
374
375 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
376
377 /* signalize caller that the packet is to be dropped. */
378 if (window_protected(seq_diff, &orig_node->batman_seqno_reset))
379 return -1;
380
381 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
382
383 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
384 orig_node->last_real_seqno,
385 batman_packet->seqno);
386
387 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
388 (tmp_neigh_node->if_incoming == if_incoming))
389 set_mark = 1;
390 else
391 set_mark = 0;
392
393 /* if the window moved, set the update flag. */
394 need_update |= bit_get_packet(tmp_neigh_node->real_bits,
395 seq_diff, set_mark);
396
397 tmp_neigh_node->real_packet_count =
398 bit_packet_count(tmp_neigh_node->real_bits);
399 }
400
401 if (need_update) {
402 bat_dbg(DBG_BATMAN, bat_priv,
403 "updating last_seqno: old %d, new %d\n",
404 orig_node->last_real_seqno, batman_packet->seqno);
405 orig_node->last_real_seqno = batman_packet->seqno;
406 }
407
408 return is_duplicate;
409 }
410
411 /* copy primary address for bonding */
412 static void mark_bonding_address(struct bat_priv *bat_priv,
413 struct orig_node *orig_node,
414 struct orig_node *orig_neigh_node,
415 struct batman_packet *batman_packet)
416
417 {
418 if (batman_packet->flags & PRIMARIES_FIRST_HOP)
419 memcpy(orig_neigh_node->primary_addr,
420 orig_node->orig, ETH_ALEN);
421
422 return;
423 }
424
425 /* mark possible bond.candidates in the neighbor list */
426 void update_bonding_candidates(struct bat_priv *bat_priv,
427 struct orig_node *orig_node)
428 {
429 int candidates;
430 int interference_candidate;
431 int best_tq;
432 struct neigh_node *tmp_neigh_node, *tmp_neigh_node2;
433 struct neigh_node *first_candidate, *last_candidate;
434
435 /* update the candidates for this originator */
436 if (!orig_node->router) {
437 orig_node->bond.candidates = 0;
438 return;
439 }
440
441 best_tq = orig_node->router->tq_avg;
442
443 /* update bond.candidates */
444
445 candidates = 0;
446
447 /* mark other nodes which also received "PRIMARIES FIRST HOP" packets
448 * as "bonding partner" */
449
450 /* first, zero the list */
451 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
452 tmp_neigh_node->next_bond_candidate = NULL;
453 }
454
455 first_candidate = NULL;
456 last_candidate = NULL;
457 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
458
459 /* only consider if it has the same primary address ... */
460 if (memcmp(orig_node->orig,
461 tmp_neigh_node->orig_node->primary_addr,
462 ETH_ALEN) != 0)
463 continue;
464
465 /* ... and is good enough to be considered */
466 if (tmp_neigh_node->tq_avg < best_tq - BONDING_TQ_THRESHOLD)
467 continue;
468
469 /* check if we have another candidate with the same
470 * mac address or interface. If we do, we won't
471 * select this candidate because of possible interference. */
472
473 interference_candidate = 0;
474 list_for_each_entry(tmp_neigh_node2,
475 &orig_node->neigh_list, list) {
476
477 if (tmp_neigh_node2 == tmp_neigh_node)
478 continue;
479
480 /* we only care if the other candidate is even
481 * considered as candidate. */
482 if (tmp_neigh_node2->next_bond_candidate == NULL)
483 continue;
484
485
486 if ((tmp_neigh_node->if_incoming ==
487 tmp_neigh_node2->if_incoming)
488 || (memcmp(tmp_neigh_node->addr,
489 tmp_neigh_node2->addr, ETH_ALEN) == 0)) {
490
491 interference_candidate = 1;
492 break;
493 }
494 }
495 /* don't care further if it is an interference candidate */
496 if (interference_candidate)
497 continue;
498
499 if (first_candidate == NULL) {
500 first_candidate = tmp_neigh_node;
501 tmp_neigh_node->next_bond_candidate = first_candidate;
502 } else
503 tmp_neigh_node->next_bond_candidate = last_candidate;
504
505 last_candidate = tmp_neigh_node;
506
507 candidates++;
508 }
509
510 if (candidates > 0) {
511 first_candidate->next_bond_candidate = last_candidate;
512 orig_node->bond.selected = first_candidate;
513 }
514
515 orig_node->bond.candidates = candidates;
516 }
517
518 void receive_bat_packet(struct ethhdr *ethhdr,
519 struct batman_packet *batman_packet,
520 unsigned char *hna_buff, int hna_buff_len,
521 struct batman_if *if_incoming)
522 {
523 /* FIXME: each orig_node->batman_if will be attached to a softif */
524 struct bat_priv *bat_priv = netdev_priv(soft_device);
525 struct batman_if *batman_if;
526 struct orig_node *orig_neigh_node, *orig_node;
527 char has_directlink_flag;
528 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
529 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
530 char is_duplicate;
531 uint32_t if_incoming_seqno;
532
533 /* Silently drop when the batman packet is actually not a
534 * correct packet.
535 *
536 * This might happen if a packet is padded (e.g. Ethernet has a
537 * minimum frame length of 64 byte) and the aggregation interprets
538 * it as an additional length.
539 *
540 * TODO: A more sane solution would be to have a bit in the
541 * batman_packet to detect whether the packet is the last
542 * packet in an aggregation. Here we expect that the padding
543 * is always zero (or not 0x01)
544 */
545 if (batman_packet->packet_type != BAT_PACKET)
546 return;
547
548 /* could be changed by schedule_own_packet() */
549 if_incoming_seqno = atomic_read(&if_incoming->seqno);
550
551 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
552
553 is_single_hop_neigh = (compare_orig(ethhdr->h_source,
554 batman_packet->orig) ? 1 : 0);
555
556 bat_dbg(DBG_BATMAN, bat_priv,
557 "Received BATMAN packet via NB: %pM, IF: %s [%s] "
558 "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
559 "TTL %d, V %d, IDF %d)\n",
560 ethhdr->h_source, if_incoming->dev, if_incoming->addr_str,
561 batman_packet->orig, batman_packet->prev_sender,
562 batman_packet->seqno, batman_packet->tq, batman_packet->ttl,
563 batman_packet->version, has_directlink_flag);
564
565 list_for_each_entry_rcu(batman_if, &if_list, list) {
566 if (batman_if->if_status != IF_ACTIVE)
567 continue;
568
569 if (compare_orig(ethhdr->h_source,
570 batman_if->net_dev->dev_addr))
571 is_my_addr = 1;
572
573 if (compare_orig(batman_packet->orig,
574 batman_if->net_dev->dev_addr))
575 is_my_orig = 1;
576
577 if (compare_orig(batman_packet->prev_sender,
578 batman_if->net_dev->dev_addr))
579 is_my_oldorig = 1;
580
581 if (compare_orig(ethhdr->h_source, broadcast_addr))
582 is_broadcast = 1;
583 }
584
585 if (batman_packet->version != COMPAT_VERSION) {
586 bat_dbg(DBG_BATMAN, bat_priv,
587 "Drop packet: incompatible batman version (%i)\n",
588 batman_packet->version);
589 return;
590 }
591
592 if (is_my_addr) {
593 bat_dbg(DBG_BATMAN, bat_priv,
594 "Drop packet: received my own broadcast (sender: %pM"
595 ")\n",
596 ethhdr->h_source);
597 return;
598 }
599
600 if (is_broadcast) {
601 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
602 "ignoring all packets with broadcast source addr (sender: %pM"
603 ")\n", ethhdr->h_source);
604 return;
605 }
606
607 if (is_my_orig) {
608 TYPE_OF_WORD *word;
609 int offset;
610
611 orig_neigh_node = get_orig_node(ethhdr->h_source);
612
613 if (!orig_neigh_node)
614 return;
615
616 /* neighbor has to indicate direct link and it has to
617 * come via the corresponding interface */
618 /* if received seqno equals last send seqno save new
619 * seqno for bidirectional check */
620 if (has_directlink_flag &&
621 compare_orig(if_incoming->net_dev->dev_addr,
622 batman_packet->orig) &&
623 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
624 offset = if_incoming->if_num * NUM_WORDS;
625 word = &(orig_neigh_node->bcast_own[offset]);
626 bit_mark(word, 0);
627 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
628 bit_packet_count(word);
629 }
630
631 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
632 "originator packet from myself (via neighbor)\n");
633 return;
634 }
635
636 if (is_my_oldorig) {
637 bat_dbg(DBG_BATMAN, bat_priv,
638 "Drop packet: ignoring all rebroadcast echos (sender: "
639 "%pM)\n", ethhdr->h_source);
640 return;
641 }
642
643 orig_node = get_orig_node(batman_packet->orig);
644 if (orig_node == NULL)
645 return;
646
647 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
648
649 if (is_duplicate == -1) {
650 bat_dbg(DBG_BATMAN, bat_priv,
651 "Drop packet: packet within seqno protection time "
652 "(sender: %pM)\n", ethhdr->h_source);
653 return;
654 }
655
656 if (batman_packet->tq == 0) {
657 bat_dbg(DBG_BATMAN, bat_priv,
658 "Drop packet: originator packet with tq equal 0\n");
659 return;
660 }
661
662 /* avoid temporary routing loops */
663 if ((orig_node->router) &&
664 (orig_node->router->orig_node->router) &&
665 (compare_orig(orig_node->router->addr,
666 batman_packet->prev_sender)) &&
667 !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
668 (compare_orig(orig_node->router->addr,
669 orig_node->router->orig_node->router->addr))) {
670 bat_dbg(DBG_BATMAN, bat_priv,
671 "Drop packet: ignoring all rebroadcast packets that "
672 "may make me loop (sender: %pM)\n", ethhdr->h_source);
673 return;
674 }
675
676 /* if sender is a direct neighbor the sender mac equals
677 * originator mac */
678 orig_neigh_node = (is_single_hop_neigh ?
679 orig_node : get_orig_node(ethhdr->h_source));
680 if (orig_neigh_node == NULL)
681 return;
682
683 /* drop packet if sender is not a direct neighbor and if we
684 * don't route towards it */
685 if (!is_single_hop_neigh &&
686 (orig_neigh_node->router == NULL)) {
687 bat_dbg(DBG_BATMAN, bat_priv,
688 "Drop packet: OGM via unknown neighbor!\n");
689 return;
690 }
691
692 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
693 batman_packet, if_incoming);
694
695 /* update ranking if it is not a duplicate or has the same
696 * seqno and similar ttl as the non-duplicate */
697 if (is_bidirectional &&
698 (!is_duplicate ||
699 ((orig_node->last_real_seqno == batman_packet->seqno) &&
700 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
701 update_orig(orig_node, ethhdr, batman_packet,
702 if_incoming, hna_buff, hna_buff_len, is_duplicate);
703
704 mark_bonding_address(bat_priv, orig_node,
705 orig_neigh_node, batman_packet);
706 update_bonding_candidates(bat_priv, orig_node);
707
708 /* is single hop (direct) neighbor */
709 if (is_single_hop_neigh) {
710
711 /* mark direct link on incoming interface */
712 schedule_forward_packet(orig_node, ethhdr, batman_packet,
713 1, hna_buff_len, if_incoming);
714
715 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
716 "rebroadcast neighbor packet with direct link flag\n");
717 return;
718 }
719
720 /* multihop originator */
721 if (!is_bidirectional) {
722 bat_dbg(DBG_BATMAN, bat_priv,
723 "Drop packet: not received via bidirectional link\n");
724 return;
725 }
726
727 if (is_duplicate) {
728 bat_dbg(DBG_BATMAN, bat_priv,
729 "Drop packet: duplicate packet received\n");
730 return;
731 }
732
733 bat_dbg(DBG_BATMAN, bat_priv,
734 "Forwarding packet: rebroadcast originator packet\n");
735 schedule_forward_packet(orig_node, ethhdr, batman_packet,
736 0, hna_buff_len, if_incoming);
737 }
738
739 int recv_bat_packet(struct sk_buff *skb,
740 struct batman_if *batman_if)
741 {
742 struct ethhdr *ethhdr;
743 unsigned long flags;
744 struct sk_buff *skb_old;
745
746 /* drop packet if it has not necessary minimum size */
747 if (skb_headlen(skb) < sizeof(struct batman_packet))
748 return NET_RX_DROP;
749
750 ethhdr = (struct ethhdr *)skb_mac_header(skb);
751
752 /* packet with broadcast indication but unicast recipient */
753 if (!is_bcast(ethhdr->h_dest))
754 return NET_RX_DROP;
755
756 /* packet with broadcast sender address */
757 if (is_bcast(ethhdr->h_source))
758 return NET_RX_DROP;
759
760 /* TODO: we use headlen instead of "length", because
761 * only this data is paged in. */
762
763 /* create a copy of the skb, if needed, to modify it. */
764 if (!skb_clone_writable(skb, skb_headlen(skb))) {
765 skb_old = skb;
766 skb = skb_copy(skb, GFP_ATOMIC);
767 if (!skb)
768 return NET_RX_DROP;
769 ethhdr = (struct ethhdr *)skb_mac_header(skb);
770 kfree_skb(skb_old);
771 }
772
773 spin_lock_irqsave(&orig_hash_lock, flags);
774 receive_aggr_bat_packet(ethhdr,
775 skb->data,
776 skb_headlen(skb),
777 batman_if);
778 spin_unlock_irqrestore(&orig_hash_lock, flags);
779
780 kfree_skb(skb);
781 return NET_RX_SUCCESS;
782 }
783
784 static int recv_my_icmp_packet(struct sk_buff *skb, size_t icmp_len)
785 {
786 struct orig_node *orig_node;
787 struct icmp_packet_rr *icmp_packet;
788 struct ethhdr *ethhdr;
789 struct sk_buff *skb_old;
790 struct batman_if *batman_if;
791 int ret;
792 unsigned long flags;
793 uint8_t dstaddr[ETH_ALEN];
794
795 icmp_packet = (struct icmp_packet_rr *)skb->data;
796 ethhdr = (struct ethhdr *)skb_mac_header(skb);
797
798 /* add data to device queue */
799 if (icmp_packet->msg_type != ECHO_REQUEST) {
800 bat_socket_receive_packet(icmp_packet, icmp_len);
801 return NET_RX_DROP;
802 }
803
804 /* answer echo request (ping) */
805 /* get routing information */
806 spin_lock_irqsave(&orig_hash_lock, flags);
807 orig_node = ((struct orig_node *)hash_find(orig_hash,
808 icmp_packet->orig));
809 ret = NET_RX_DROP;
810
811 if ((orig_node != NULL) &&
812 (orig_node->router != NULL)) {
813
814 /* don't lock while sending the packets ... we therefore
815 * copy the required data before sending */
816 batman_if = orig_node->router->if_incoming;
817 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
818 spin_unlock_irqrestore(&orig_hash_lock, flags);
819
820 /* create a copy of the skb, if needed, to modify it. */
821 skb_old = NULL;
822 if (!skb_clone_writable(skb, icmp_len)) {
823 skb_old = skb;
824 skb = skb_copy(skb, GFP_ATOMIC);
825 if (!skb)
826 return NET_RX_DROP;
827 icmp_packet = (struct icmp_packet_rr *)skb->data;
828 ethhdr = (struct ethhdr *)skb_mac_header(skb);
829 kfree_skb(skb_old);
830 }
831
832 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
833 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
834 icmp_packet->msg_type = ECHO_REPLY;
835 icmp_packet->ttl = TTL;
836
837 send_skb_packet(skb, batman_if, dstaddr);
838 ret = NET_RX_SUCCESS;
839
840 } else
841 spin_unlock_irqrestore(&orig_hash_lock, flags);
842
843 return ret;
844 }
845
846 static int recv_icmp_ttl_exceeded(struct sk_buff *skb, size_t icmp_len)
847 {
848 struct orig_node *orig_node;
849 struct icmp_packet *icmp_packet;
850 struct ethhdr *ethhdr;
851 struct sk_buff *skb_old;
852 struct batman_if *batman_if;
853 int ret;
854 unsigned long flags;
855 uint8_t dstaddr[ETH_ALEN];
856
857 icmp_packet = (struct icmp_packet *)skb->data;
858 ethhdr = (struct ethhdr *)skb_mac_header(skb);
859
860 /* send TTL exceeded if packet is an echo request (traceroute) */
861 if (icmp_packet->msg_type != ECHO_REQUEST) {
862 pr_warning("Warning - can't forward icmp packet from %pM to "
863 "%pM: ttl exceeded\n", icmp_packet->orig,
864 icmp_packet->dst);
865 return NET_RX_DROP;
866 }
867
868 /* get routing information */
869 spin_lock_irqsave(&orig_hash_lock, flags);
870 orig_node = ((struct orig_node *)
871 hash_find(orig_hash, icmp_packet->orig));
872 ret = NET_RX_DROP;
873
874 if ((orig_node != NULL) &&
875 (orig_node->router != NULL)) {
876
877 /* don't lock while sending the packets ... we therefore
878 * copy the required data before sending */
879 batman_if = orig_node->router->if_incoming;
880 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
881 spin_unlock_irqrestore(&orig_hash_lock, flags);
882
883 /* create a copy of the skb, if needed, to modify it. */
884 if (!skb_clone_writable(skb, icmp_len)) {
885 skb_old = skb;
886 skb = skb_copy(skb, GFP_ATOMIC);
887 if (!skb)
888 return NET_RX_DROP;
889 icmp_packet = (struct icmp_packet *) skb->data;
890 ethhdr = (struct ethhdr *)skb_mac_header(skb);
891 kfree_skb(skb_old);
892 }
893
894 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
895 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
896 icmp_packet->msg_type = TTL_EXCEEDED;
897 icmp_packet->ttl = TTL;
898
899 send_skb_packet(skb, batman_if, dstaddr);
900 ret = NET_RX_SUCCESS;
901
902 } else
903 spin_unlock_irqrestore(&orig_hash_lock, flags);
904
905 return ret;
906 }
907
908
909 int recv_icmp_packet(struct sk_buff *skb)
910 {
911 struct icmp_packet_rr *icmp_packet;
912 struct ethhdr *ethhdr;
913 struct orig_node *orig_node;
914 struct sk_buff *skb_old;
915 struct batman_if *batman_if;
916 int hdr_size = sizeof(struct icmp_packet);
917 int ret;
918 unsigned long flags;
919 uint8_t dstaddr[ETH_ALEN];
920
921 /**
922 * we truncate all incoming icmp packets if they don't match our size
923 */
924 if (skb_headlen(skb) >= sizeof(struct icmp_packet_rr))
925 hdr_size = sizeof(struct icmp_packet_rr);
926
927 /* drop packet if it has not necessary minimum size */
928 if (skb_headlen(skb) < hdr_size)
929 return NET_RX_DROP;
930
931 ethhdr = (struct ethhdr *)skb_mac_header(skb);
932
933 /* packet with unicast indication but broadcast recipient */
934 if (is_bcast(ethhdr->h_dest))
935 return NET_RX_DROP;
936
937 /* packet with broadcast sender address */
938 if (is_bcast(ethhdr->h_source))
939 return NET_RX_DROP;
940
941 /* not for me */
942 if (!is_my_mac(ethhdr->h_dest))
943 return NET_RX_DROP;
944
945 icmp_packet = (struct icmp_packet_rr *)skb->data;
946
947 /* add record route information if not full */
948 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
949 (icmp_packet->rr_cur < BAT_RR_LEN)) {
950 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
951 ethhdr->h_dest, ETH_ALEN);
952 icmp_packet->rr_cur++;
953 }
954
955 /* packet for me */
956 if (is_my_mac(icmp_packet->dst))
957 return recv_my_icmp_packet(skb, hdr_size);
958
959 /* TTL exceeded */
960 if (icmp_packet->ttl < 2)
961 return recv_icmp_ttl_exceeded(skb, hdr_size);
962
963 ret = NET_RX_DROP;
964
965 /* get routing information */
966 spin_lock_irqsave(&orig_hash_lock, flags);
967 orig_node = ((struct orig_node *)
968 hash_find(orig_hash, icmp_packet->dst));
969
970 if ((orig_node != NULL) &&
971 (orig_node->router != NULL)) {
972
973 /* don't lock while sending the packets ... we therefore
974 * copy the required data before sending */
975 batman_if = orig_node->router->if_incoming;
976 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
977 spin_unlock_irqrestore(&orig_hash_lock, flags);
978
979 /* create a copy of the skb, if needed, to modify it. */
980 if (!skb_clone_writable(skb, hdr_size)) {
981 skb_old = skb;
982 skb = skb_copy(skb, GFP_ATOMIC);
983 if (!skb)
984 return NET_RX_DROP;
985 icmp_packet = (struct icmp_packet_rr *)skb->data;
986 ethhdr = (struct ethhdr *)skb_mac_header(skb);
987 kfree_skb(skb_old);
988 }
989
990 /* decrement ttl */
991 icmp_packet->ttl--;
992
993 /* route it */
994 send_skb_packet(skb, batman_if, dstaddr);
995 ret = NET_RX_SUCCESS;
996
997 } else
998 spin_unlock_irqrestore(&orig_hash_lock, flags);
999
1000 return ret;
1001 }
1002
1003 /* find a suitable router for this originator, and use
1004 * bonding if possible. */
1005 struct neigh_node *find_router(struct orig_node *orig_node,
1006 struct batman_if *recv_if)
1007 {
1008 /* FIXME: each orig_node->batman_if will be attached to a softif */
1009 struct bat_priv *bat_priv = netdev_priv(soft_device);
1010 struct orig_node *primary_orig_node;
1011 struct orig_node *router_orig;
1012 struct neigh_node *router, *first_candidate, *best_router;
1013 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1014 int bonding_enabled;
1015
1016 if (!orig_node)
1017 return NULL;
1018
1019 if (!orig_node->router)
1020 return NULL;
1021
1022 /* without bonding, the first node should
1023 * always choose the default router. */
1024
1025 bonding_enabled = atomic_read(&bat_priv->bonding_enabled);
1026 if (!bonding_enabled && (recv_if == NULL))
1027 return orig_node->router;
1028
1029 router_orig = orig_node->router->orig_node;
1030
1031 /* if we have something in the primary_addr, we can search
1032 * for a potential bonding candidate. */
1033 if (memcmp(router_orig->primary_addr, zero_mac, ETH_ALEN) == 0)
1034 return orig_node->router;
1035
1036 /* find the orig_node which has the primary interface. might
1037 * even be the same as our router_orig in many cases */
1038
1039 if (memcmp(router_orig->primary_addr,
1040 router_orig->orig, ETH_ALEN) == 0) {
1041 primary_orig_node = router_orig;
1042 } else {
1043 primary_orig_node = hash_find(orig_hash,
1044 router_orig->primary_addr);
1045 if (!primary_orig_node)
1046 return orig_node->router;
1047 }
1048
1049 /* with less than 2 candidates, we can't do any
1050 * bonding and prefer the original router. */
1051
1052 if (primary_orig_node->bond.candidates < 2)
1053 return orig_node->router;
1054
1055
1056 /* all nodes between should choose a candidate which
1057 * is is not on the interface where the packet came
1058 * in. */
1059 first_candidate = primary_orig_node->bond.selected;
1060 router = first_candidate;
1061
1062 if (bonding_enabled) {
1063 /* in the bonding case, send the packets in a round
1064 * robin fashion over the remaining interfaces. */
1065 do {
1066 /* recv_if == NULL on the first node. */
1067 if (router->if_incoming != recv_if)
1068 break;
1069
1070 router = router->next_bond_candidate;
1071 } while (router != first_candidate);
1072
1073 primary_orig_node->bond.selected = router->next_bond_candidate;
1074
1075 } else {
1076 /* if bonding is disabled, use the best of the
1077 * remaining candidates which are not using
1078 * this interface. */
1079 best_router = first_candidate;
1080
1081 do {
1082 /* recv_if == NULL on the first node. */
1083 if ((router->if_incoming != recv_if) &&
1084 (router->tq_avg > best_router->tq_avg))
1085 best_router = router;
1086
1087 router = router->next_bond_candidate;
1088 } while (router != first_candidate);
1089
1090 router = best_router;
1091 }
1092
1093 return router;
1094 }
1095
1096 int recv_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1097 {
1098 struct unicast_packet *unicast_packet;
1099 struct orig_node *orig_node;
1100 struct neigh_node *router;
1101 struct ethhdr *ethhdr;
1102 struct batman_if *batman_if;
1103 struct sk_buff *skb_old;
1104 uint8_t dstaddr[ETH_ALEN];
1105 int hdr_size = sizeof(struct unicast_packet);
1106 unsigned long flags;
1107
1108 /* drop packet if it has not necessary minimum size */
1109 if (skb_headlen(skb) < hdr_size)
1110 return NET_RX_DROP;
1111
1112 ethhdr = (struct ethhdr *) skb_mac_header(skb);
1113
1114 /* packet with unicast indication but broadcast recipient */
1115 if (is_bcast(ethhdr->h_dest))
1116 return NET_RX_DROP;
1117
1118 /* packet with broadcast sender address */
1119 if (is_bcast(ethhdr->h_source))
1120 return NET_RX_DROP;
1121
1122 /* not for me */
1123 if (!is_my_mac(ethhdr->h_dest))
1124 return NET_RX_DROP;
1125
1126 unicast_packet = (struct unicast_packet *) skb->data;
1127
1128 /* packet for me */
1129 if (is_my_mac(unicast_packet->dest)) {
1130 interface_rx(skb, hdr_size);
1131 return NET_RX_SUCCESS;
1132 }
1133
1134 /* TTL exceeded */
1135 if (unicast_packet->ttl < 2) {
1136 pr_warning("Warning - can't forward unicast packet from %pM to "
1137 "%pM: ttl exceeded\n", ethhdr->h_source,
1138 unicast_packet->dest);
1139 return NET_RX_DROP;
1140 }
1141
1142 /* get routing information */
1143 spin_lock_irqsave(&orig_hash_lock, flags);
1144 orig_node = ((struct orig_node *)
1145 hash_find(orig_hash, unicast_packet->dest));
1146
1147 router = find_router(orig_node, recv_if);
1148
1149 if (!router) {
1150 spin_unlock_irqrestore(&orig_hash_lock, flags);
1151 return NET_RX_DROP;
1152 }
1153
1154 /* don't lock while sending the packets ... we therefore
1155 * copy the required data before sending */
1156
1157 batman_if = router->if_incoming;
1158 memcpy(dstaddr, router->addr, ETH_ALEN);
1159
1160 spin_unlock_irqrestore(&orig_hash_lock, flags);
1161
1162 /* create a copy of the skb, if needed, to modify it. */
1163 if (!skb_clone_writable(skb, sizeof(struct unicast_packet))) {
1164 skb_old = skb;
1165 skb = skb_copy(skb, GFP_ATOMIC);
1166 if (!skb)
1167 return NET_RX_DROP;
1168 unicast_packet = (struct unicast_packet *) skb->data;
1169 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1170 kfree_skb(skb_old);
1171 }
1172
1173 /* decrement ttl */
1174 unicast_packet->ttl--;
1175
1176 /* route it */
1177 send_skb_packet(skb, batman_if, dstaddr);
1178
1179 return NET_RX_SUCCESS;
1180 }
1181
1182 int recv_bcast_packet(struct sk_buff *skb)
1183 {
1184 struct orig_node *orig_node;
1185 struct bcast_packet *bcast_packet;
1186 struct ethhdr *ethhdr;
1187 int hdr_size = sizeof(struct bcast_packet);
1188 int32_t seq_diff;
1189 unsigned long flags;
1190
1191 /* drop packet if it has not necessary minimum size */
1192 if (skb_headlen(skb) < hdr_size)
1193 return NET_RX_DROP;
1194
1195 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1196
1197 /* packet with broadcast indication but unicast recipient */
1198 if (!is_bcast(ethhdr->h_dest))
1199 return NET_RX_DROP;
1200
1201 /* packet with broadcast sender address */
1202 if (is_bcast(ethhdr->h_source))
1203 return NET_RX_DROP;
1204
1205 /* ignore broadcasts sent by myself */
1206 if (is_my_mac(ethhdr->h_source))
1207 return NET_RX_DROP;
1208
1209 bcast_packet = (struct bcast_packet *)skb->data;
1210
1211 /* ignore broadcasts originated by myself */
1212 if (is_my_mac(bcast_packet->orig))
1213 return NET_RX_DROP;
1214
1215 if (bcast_packet->ttl < 2)
1216 return NET_RX_DROP;
1217
1218 spin_lock_irqsave(&orig_hash_lock, flags);
1219 orig_node = ((struct orig_node *)
1220 hash_find(orig_hash, bcast_packet->orig));
1221
1222 if (orig_node == NULL) {
1223 spin_unlock_irqrestore(&orig_hash_lock, flags);
1224 return NET_RX_DROP;
1225 }
1226
1227 /* check whether the packet is a duplicate */
1228 if (get_bit_status(orig_node->bcast_bits,
1229 orig_node->last_bcast_seqno,
1230 ntohl(bcast_packet->seqno))) {
1231 spin_unlock_irqrestore(&orig_hash_lock, flags);
1232 return NET_RX_DROP;
1233 }
1234
1235 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1236
1237 /* check whether the packet is old and the host just restarted. */
1238 if (window_protected(seq_diff, &orig_node->bcast_seqno_reset)) {
1239 spin_unlock_irqrestore(&orig_hash_lock, flags);
1240 return NET_RX_DROP;
1241 }
1242
1243 /* mark broadcast in flood history, update window position
1244 * if required. */
1245 if (bit_get_packet(orig_node->bcast_bits, seq_diff, 1))
1246 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1247
1248 spin_unlock_irqrestore(&orig_hash_lock, flags);
1249 /* rebroadcast packet */
1250 add_bcast_packet_to_list(skb);
1251
1252 /* broadcast for me */
1253 interface_rx(skb, hdr_size);
1254
1255 return NET_RX_SUCCESS;
1256 }
1257
1258 int recv_vis_packet(struct sk_buff *skb)
1259 {
1260 struct vis_packet *vis_packet;
1261 struct ethhdr *ethhdr;
1262 struct bat_priv *bat_priv;
1263 int hdr_size = sizeof(struct vis_packet);
1264
1265 if (skb_headlen(skb) < hdr_size)
1266 return NET_RX_DROP;
1267
1268 vis_packet = (struct vis_packet *) skb->data;
1269 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1270
1271 /* not for me */
1272 if (!is_my_mac(ethhdr->h_dest))
1273 return NET_RX_DROP;
1274
1275 /* ignore own packets */
1276 if (is_my_mac(vis_packet->vis_orig))
1277 return NET_RX_DROP;
1278
1279 if (is_my_mac(vis_packet->sender_orig))
1280 return NET_RX_DROP;
1281
1282 /* FIXME: each batman_if will be attached to a softif */
1283 bat_priv = netdev_priv(soft_device);
1284
1285 switch (vis_packet->vis_type) {
1286 case VIS_TYPE_SERVER_SYNC:
1287 /* TODO: handle fragmented skbs properly */
1288 receive_server_sync_packet(bat_priv, vis_packet,
1289 skb_headlen(skb));
1290 break;
1291
1292 case VIS_TYPE_CLIENT_UPDATE:
1293 /* TODO: handle fragmented skbs properly */
1294 receive_client_update_packet(bat_priv, vis_packet,
1295 skb_headlen(skb));
1296 break;
1297
1298 default: /* ignore unknown packet */
1299 break;
1300 }
1301
1302 /* We take a copy of the data in the packet, so we should
1303 always free the skbuf. */
1304 return NET_RX_DROP;
1305 }