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