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