batman-adv: Prefix main local static functions with batadv_
[deliverable/linux.git] / net / batman-adv / routing.c
CommitLineData
9cfc7bd6 1/* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
c6c8fea2
SE
2 *
3 * Marek Lindner, Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
c6c8fea2
SE
18 */
19
20#include "main.h"
21#include "routing.h"
22#include "send.h"
c6c8fea2
SE
23#include "soft-interface.h"
24#include "hard-interface.h"
25#include "icmp_socket.h"
26#include "translation-table.h"
27#include "originator.h"
c6c8fea2 28#include "vis.h"
c6c8fea2 29#include "unicast.h"
23721387 30#include "bridge_loop_avoidance.h"
c6c8fea2 31
63b01037
SE
32static int batadv_route_unicast_packet(struct sk_buff *skb,
33 struct hard_iface *recv_if);
a7f6ee94 34
30d3c511 35void batadv_slide_own_bcast_window(struct hard_iface *hard_iface)
c6c8fea2 36{
e6c10f43 37 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
c6c8fea2 38 struct hashtable_t *hash = bat_priv->orig_hash;
7aadf889 39 struct hlist_node *node;
c6c8fea2 40 struct hlist_head *head;
c6c8fea2
SE
41 struct orig_node *orig_node;
42 unsigned long *word;
c90681b8 43 uint32_t i;
c6c8fea2
SE
44 size_t word_index;
45
c6c8fea2
SE
46 for (i = 0; i < hash->size; i++) {
47 head = &hash->table[i];
48
fb778ea1 49 rcu_read_lock();
7aadf889 50 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
2ae2daf6 51 spin_lock_bh(&orig_node->ogm_cnt_lock);
e6c10f43 52 word_index = hard_iface->if_num * NUM_WORDS;
c6c8fea2
SE
53 word = &(orig_node->bcast_own[word_index]);
54
0f5f9322 55 batadv_bit_get_packet(bat_priv, word, 1, 0);
e6c10f43 56 orig_node->bcast_own_sum[hard_iface->if_num] =
0079d2ce 57 bitmap_weight(word, TQ_LOCAL_WINDOW_SIZE);
2ae2daf6 58 spin_unlock_bh(&orig_node->ogm_cnt_lock);
c6c8fea2 59 }
fb778ea1 60 rcu_read_unlock();
c6c8fea2 61 }
c6c8fea2
SE
62}
63
63b01037
SE
64static void _batadv_update_route(struct bat_priv *bat_priv,
65 struct orig_node *orig_node,
66 struct neigh_node *neigh_node)
c6c8fea2 67{
e1a5382f
LL
68 struct neigh_node *curr_router;
69
7d211efc 70 curr_router = batadv_orig_node_get_router(orig_node);
a8e7f4bc 71
c6c8fea2 72 /* route deleted */
e1a5382f 73 if ((curr_router) && (!neigh_node)) {
1eda58bf
SE
74 batadv_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
75 orig_node->orig);
08c36d3e
SE
76 batadv_tt_global_del_orig(bat_priv, orig_node,
77 "Deleted route towards originator");
c6c8fea2 78
e1a5382f
LL
79 /* route added */
80 } else if ((!curr_router) && (neigh_node)) {
c6c8fea2 81
1eda58bf
SE
82 batadv_dbg(DBG_ROUTES, bat_priv,
83 "Adding route towards: %pM (via %pM)\n",
84 orig_node->orig, neigh_node->addr);
e1a5382f 85 /* route changed */
bb899b89 86 } else if (neigh_node && curr_router) {
1eda58bf
SE
87 batadv_dbg(DBG_ROUTES, bat_priv,
88 "Changing route towards: %pM (now via %pM - was via %pM)\n",
89 orig_node->orig, neigh_node->addr,
90 curr_router->addr);
c6c8fea2
SE
91 }
92
e1a5382f 93 if (curr_router)
7d211efc 94 batadv_neigh_node_free_ref(curr_router);
e1a5382f
LL
95
96 /* increase refcount of new best neighbor */
44524fcd
ML
97 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
98 neigh_node = NULL;
e1a5382f
LL
99
100 spin_lock_bh(&orig_node->neigh_list_lock);
101 rcu_assign_pointer(orig_node->router, neigh_node);
102 spin_unlock_bh(&orig_node->neigh_list_lock);
103
104 /* decrease refcount of previous best neighbor */
105 if (curr_router)
7d211efc 106 batadv_neigh_node_free_ref(curr_router);
c6c8fea2
SE
107}
108
30d3c511
SE
109void batadv_update_route(struct bat_priv *bat_priv, struct orig_node *orig_node,
110 struct neigh_node *neigh_node)
c6c8fea2 111{
e1a5382f 112 struct neigh_node *router = NULL;
c6c8fea2
SE
113
114 if (!orig_node)
e1a5382f
LL
115 goto out;
116
7d211efc 117 router = batadv_orig_node_get_router(orig_node);
c6c8fea2 118
e1a5382f 119 if (router != neigh_node)
63b01037 120 _batadv_update_route(bat_priv, orig_node, neigh_node);
e1a5382f
LL
121
122out:
123 if (router)
7d211efc 124 batadv_neigh_node_free_ref(router);
c6c8fea2
SE
125}
126
a4c135c5 127/* caller must hold the neigh_list_lock */
30d3c511
SE
128void batadv_bonding_candidate_del(struct orig_node *orig_node,
129 struct neigh_node *neigh_node)
a4c135c5
SW
130{
131 /* this neighbor is not part of our candidate list */
132 if (list_empty(&neigh_node->bonding_list))
133 goto out;
134
135 list_del_rcu(&neigh_node->bonding_list);
a4c135c5 136 INIT_LIST_HEAD(&neigh_node->bonding_list);
7d211efc 137 batadv_neigh_node_free_ref(neigh_node);
a4c135c5
SW
138 atomic_dec(&orig_node->bond_candidates);
139
140out:
141 return;
142}
143
30d3c511
SE
144void batadv_bonding_candidate_add(struct orig_node *orig_node,
145 struct neigh_node *neigh_node)
a4c135c5
SW
146{
147 struct hlist_node *node;
e1a5382f
LL
148 struct neigh_node *tmp_neigh_node, *router = NULL;
149 uint8_t interference_candidate = 0;
a4c135c5
SW
150
151 spin_lock_bh(&orig_node->neigh_list_lock);
152
153 /* only consider if it has the same primary address ... */
1eda58bf
SE
154 if (!batadv_compare_eth(orig_node->orig,
155 neigh_node->orig_node->primary_addr))
a4c135c5
SW
156 goto candidate_del;
157
7d211efc 158 router = batadv_orig_node_get_router(orig_node);
e1a5382f 159 if (!router)
a4c135c5
SW
160 goto candidate_del;
161
a4c135c5 162 /* ... and is good enough to be considered */
e1a5382f 163 if (neigh_node->tq_avg < router->tq_avg - BONDING_TQ_THRESHOLD)
a4c135c5
SW
164 goto candidate_del;
165
9cfc7bd6 166 /* check if we have another candidate with the same mac address or
a4c135c5
SW
167 * interface. If we do, we won't select this candidate because of
168 * possible interference.
169 */
170 hlist_for_each_entry_rcu(tmp_neigh_node, node,
171 &orig_node->neigh_list, list) {
172
173 if (tmp_neigh_node == neigh_node)
174 continue;
175
176 /* we only care if the other candidate is even
9cfc7bd6
SE
177 * considered as candidate.
178 */
a4c135c5
SW
179 if (list_empty(&tmp_neigh_node->bonding_list))
180 continue;
181
182 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
1eda58bf
SE
183 (batadv_compare_eth(neigh_node->addr,
184 tmp_neigh_node->addr))) {
a4c135c5
SW
185 interference_candidate = 1;
186 break;
187 }
188 }
189
190 /* don't care further if it is an interference candidate */
191 if (interference_candidate)
192 goto candidate_del;
193
194 /* this neighbor already is part of our candidate list */
195 if (!list_empty(&neigh_node->bonding_list))
196 goto out;
197
44524fcd
ML
198 if (!atomic_inc_not_zero(&neigh_node->refcount))
199 goto out;
200
a4c135c5 201 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
a4c135c5
SW
202 atomic_inc(&orig_node->bond_candidates);
203 goto out;
204
205candidate_del:
30d3c511 206 batadv_bonding_candidate_del(orig_node, neigh_node);
a4c135c5
SW
207
208out:
209 spin_unlock_bh(&orig_node->neigh_list_lock);
e1a5382f
LL
210
211 if (router)
7d211efc 212 batadv_neigh_node_free_ref(router);
a4c135c5
SW
213}
214
215/* copy primary address for bonding */
30d3c511
SE
216void
217batadv_bonding_save_primary(const struct orig_node *orig_node,
218 struct orig_node *orig_neigh_node,
219 const struct batman_ogm_packet *batman_ogm_packet)
a4c135c5 220{
b6da4bf5 221 if (!(batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
a4c135c5
SW
222 return;
223
224 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
225}
226
c6c8fea2
SE
227/* checks whether the host restarted and is in the protection time.
228 * returns:
229 * 0 if the packet is to be accepted
230 * 1 if the packet is to be ignored.
231 */
30d3c511
SE
232int batadv_window_protected(struct bat_priv *bat_priv, int32_t seq_num_diff,
233 unsigned long *last_reset)
c6c8fea2 234{
7c64fd98
SE
235 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE) ||
236 (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
1eda58bf 237 if (!batadv_has_timed_out(*last_reset, RESET_PROTECTION_MS))
c6c8fea2 238 return 1;
8c7bf248
ML
239
240 *last_reset = jiffies;
1eda58bf
SE
241 batadv_dbg(DBG_BATMAN, bat_priv,
242 "old packet received, start protection\n");
c6c8fea2 243 }
8c7bf248 244
c6c8fea2
SE
245 return 0;
246}
247
30d3c511
SE
248bool batadv_check_management_packet(struct sk_buff *skb,
249 struct hard_iface *hard_iface,
250 int header_len)
c6c8fea2 251{
c6c8fea2
SE
252 struct ethhdr *ethhdr;
253
254 /* drop packet if it has not necessary minimum size */
c3e29312
ML
255 if (unlikely(!pskb_may_pull(skb, header_len)))
256 return false;
c6c8fea2
SE
257
258 ethhdr = (struct ethhdr *)skb_mac_header(skb);
259
260 /* packet with broadcast indication but unicast recipient */
261 if (!is_broadcast_ether_addr(ethhdr->h_dest))
c3e29312 262 return false;
c6c8fea2
SE
263
264 /* packet with broadcast sender address */
265 if (is_broadcast_ether_addr(ethhdr->h_source))
c3e29312 266 return false;
c6c8fea2
SE
267
268 /* create a copy of the skb, if needed, to modify it. */
269 if (skb_cow(skb, 0) < 0)
c3e29312 270 return false;
c6c8fea2
SE
271
272 /* keep skb linear */
273 if (skb_linearize(skb) < 0)
c3e29312 274 return false;
c6c8fea2 275
c3e29312 276 return true;
c6c8fea2
SE
277}
278
63b01037
SE
279static int batadv_recv_my_icmp_packet(struct bat_priv *bat_priv,
280 struct sk_buff *skb, size_t icmp_len)
c6c8fea2 281{
32ae9b22 282 struct hard_iface *primary_if = NULL;
44524fcd 283 struct orig_node *orig_node = NULL;
e1a5382f 284 struct neigh_node *router = NULL;
c6c8fea2 285 struct icmp_packet_rr *icmp_packet;
44524fcd 286 int ret = NET_RX_DROP;
c6c8fea2
SE
287
288 icmp_packet = (struct icmp_packet_rr *)skb->data;
c6c8fea2
SE
289
290 /* add data to device queue */
291 if (icmp_packet->msg_type != ECHO_REQUEST) {
9039dc7e 292 batadv_socket_receive_packet(icmp_packet, icmp_len);
44524fcd 293 goto out;
c6c8fea2
SE
294 }
295
e5d89254 296 primary_if = batadv_primary_if_get_selected(bat_priv);
32ae9b22 297 if (!primary_if)
44524fcd 298 goto out;
c6c8fea2
SE
299
300 /* answer echo request (ping) */
301 /* get routing information */
da641193 302 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
44524fcd 303 if (!orig_node)
e1a5382f 304 goto out;
c6c8fea2 305
7d211efc 306 router = batadv_orig_node_get_router(orig_node);
e1a5382f
LL
307 if (!router)
308 goto out;
c6c8fea2 309
44524fcd 310 /* create a copy of the skb, if needed, to modify it. */
0d125074 311 if (skb_cow(skb, ETH_HLEN) < 0)
44524fcd
ML
312 goto out;
313
314 icmp_packet = (struct icmp_packet_rr *)skb->data;
315
316 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
32ae9b22 317 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
44524fcd 318 icmp_packet->msg_type = ECHO_REPLY;
76543d14 319 icmp_packet->header.ttl = TTL;
44524fcd 320
9455e34c 321 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
44524fcd 322 ret = NET_RX_SUCCESS;
c6c8fea2 323
44524fcd 324out:
32ae9b22 325 if (primary_if)
e5d89254 326 batadv_hardif_free_ref(primary_if);
e1a5382f 327 if (router)
7d211efc 328 batadv_neigh_node_free_ref(router);
44524fcd 329 if (orig_node)
7d211efc 330 batadv_orig_node_free_ref(orig_node);
c6c8fea2
SE
331 return ret;
332}
333
63b01037
SE
334static int batadv_recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
335 struct sk_buff *skb)
c6c8fea2 336{
32ae9b22 337 struct hard_iface *primary_if = NULL;
44524fcd 338 struct orig_node *orig_node = NULL;
e1a5382f 339 struct neigh_node *router = NULL;
c6c8fea2 340 struct icmp_packet *icmp_packet;
44524fcd 341 int ret = NET_RX_DROP;
c6c8fea2
SE
342
343 icmp_packet = (struct icmp_packet *)skb->data;
c6c8fea2
SE
344
345 /* send TTL exceeded if packet is an echo request (traceroute) */
346 if (icmp_packet->msg_type != ECHO_REQUEST) {
86ceb360
SE
347 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
348 icmp_packet->orig, icmp_packet->dst);
44524fcd 349 goto out;
c6c8fea2
SE
350 }
351
e5d89254 352 primary_if = batadv_primary_if_get_selected(bat_priv);
32ae9b22 353 if (!primary_if)
44524fcd 354 goto out;
c6c8fea2
SE
355
356 /* get routing information */
da641193 357 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
44524fcd 358 if (!orig_node)
e1a5382f 359 goto out;
c6c8fea2 360
7d211efc 361 router = batadv_orig_node_get_router(orig_node);
e1a5382f
LL
362 if (!router)
363 goto out;
c6c8fea2 364
44524fcd 365 /* create a copy of the skb, if needed, to modify it. */
0d125074 366 if (skb_cow(skb, ETH_HLEN) < 0)
44524fcd 367 goto out;
c6c8fea2 368
44524fcd 369 icmp_packet = (struct icmp_packet *)skb->data;
c6c8fea2 370
44524fcd 371 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
32ae9b22 372 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
44524fcd 373 icmp_packet->msg_type = TTL_EXCEEDED;
76543d14 374 icmp_packet->header.ttl = TTL;
44524fcd 375
9455e34c 376 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
44524fcd 377 ret = NET_RX_SUCCESS;
c6c8fea2 378
44524fcd 379out:
32ae9b22 380 if (primary_if)
e5d89254 381 batadv_hardif_free_ref(primary_if);
e1a5382f 382 if (router)
7d211efc 383 batadv_neigh_node_free_ref(router);
44524fcd 384 if (orig_node)
7d211efc 385 batadv_orig_node_free_ref(orig_node);
c6c8fea2
SE
386 return ret;
387}
388
389
30d3c511 390int batadv_recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if)
c6c8fea2
SE
391{
392 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
393 struct icmp_packet_rr *icmp_packet;
394 struct ethhdr *ethhdr;
44524fcd 395 struct orig_node *orig_node = NULL;
e1a5382f 396 struct neigh_node *router = NULL;
c6c8fea2 397 int hdr_size = sizeof(struct icmp_packet);
44524fcd 398 int ret = NET_RX_DROP;
c6c8fea2 399
9cfc7bd6 400 /* we truncate all incoming icmp packets if they don't match our size */
c6c8fea2
SE
401 if (skb->len >= sizeof(struct icmp_packet_rr))
402 hdr_size = sizeof(struct icmp_packet_rr);
403
404 /* drop packet if it has not necessary minimum size */
405 if (unlikely(!pskb_may_pull(skb, hdr_size)))
44524fcd 406 goto out;
c6c8fea2
SE
407
408 ethhdr = (struct ethhdr *)skb_mac_header(skb);
409
410 /* packet with unicast indication but broadcast recipient */
411 if (is_broadcast_ether_addr(ethhdr->h_dest))
44524fcd 412 goto out;
c6c8fea2
SE
413
414 /* packet with broadcast sender address */
415 if (is_broadcast_ether_addr(ethhdr->h_source))
44524fcd 416 goto out;
c6c8fea2
SE
417
418 /* not for me */
3193e8fd 419 if (!batadv_is_my_mac(ethhdr->h_dest))
44524fcd 420 goto out;
c6c8fea2
SE
421
422 icmp_packet = (struct icmp_packet_rr *)skb->data;
423
424 /* add record route information if not full */
425 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
426 (icmp_packet->rr_cur < BAT_RR_LEN)) {
427 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
7c64fd98 428 ethhdr->h_dest, ETH_ALEN);
c6c8fea2
SE
429 icmp_packet->rr_cur++;
430 }
431
432 /* packet for me */
3193e8fd 433 if (batadv_is_my_mac(icmp_packet->dst))
63b01037 434 return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size);
c6c8fea2
SE
435
436 /* TTL exceeded */
76543d14 437 if (icmp_packet->header.ttl < 2)
63b01037 438 return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
c6c8fea2 439
c6c8fea2 440 /* get routing information */
da641193 441 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
44524fcd 442 if (!orig_node)
e1a5382f 443 goto out;
c6c8fea2 444
7d211efc 445 router = batadv_orig_node_get_router(orig_node);
e1a5382f
LL
446 if (!router)
447 goto out;
c6c8fea2 448
44524fcd 449 /* create a copy of the skb, if needed, to modify it. */
0d125074 450 if (skb_cow(skb, ETH_HLEN) < 0)
44524fcd 451 goto out;
c6c8fea2 452
44524fcd 453 icmp_packet = (struct icmp_packet_rr *)skb->data;
c6c8fea2 454
44524fcd 455 /* decrement ttl */
76543d14 456 icmp_packet->header.ttl--;
44524fcd
ML
457
458 /* route it */
9455e34c 459 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
44524fcd 460 ret = NET_RX_SUCCESS;
c6c8fea2 461
44524fcd 462out:
e1a5382f 463 if (router)
7d211efc 464 batadv_neigh_node_free_ref(router);
44524fcd 465 if (orig_node)
7d211efc 466 batadv_orig_node_free_ref(orig_node);
c6c8fea2
SE
467 return ret;
468}
469
55158629
LL
470/* In the bonding case, send the packets in a round
471 * robin fashion over the remaining interfaces.
472 *
473 * This method rotates the bonding list and increases the
9cfc7bd6
SE
474 * returned router's refcount.
475 */
63b01037
SE
476static struct neigh_node *
477batadv_find_bond_router(struct orig_node *primary_orig,
478 const struct hard_iface *recv_if)
55158629
LL
479{
480 struct neigh_node *tmp_neigh_node;
481 struct neigh_node *router = NULL, *first_candidate = NULL;
482
483 rcu_read_lock();
484 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
485 bonding_list) {
486 if (!first_candidate)
487 first_candidate = tmp_neigh_node;
488
489 /* recv_if == NULL on the first node. */
490 if (tmp_neigh_node->if_incoming == recv_if)
491 continue;
492
493 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
494 continue;
495
496 router = tmp_neigh_node;
497 break;
498 }
499
500 /* use the first candidate if nothing was found. */
501 if (!router && first_candidate &&
502 atomic_inc_not_zero(&first_candidate->refcount))
503 router = first_candidate;
504
505 if (!router)
506 goto out;
507
508 /* selected should point to the next element
9cfc7bd6
SE
509 * after the current router
510 */
55158629
LL
511 spin_lock_bh(&primary_orig->neigh_list_lock);
512 /* this is a list_move(), which unfortunately
9cfc7bd6
SE
513 * does not exist as rcu version
514 */
55158629
LL
515 list_del_rcu(&primary_orig->bond_list);
516 list_add_rcu(&primary_orig->bond_list,
517 &router->bonding_list);
518 spin_unlock_bh(&primary_orig->neigh_list_lock);
519
520out:
521 rcu_read_unlock();
522 return router;
523}
524
525/* Interface Alternating: Use the best of the
526 * remaining candidates which are not using
527 * this interface.
528 *
9cfc7bd6
SE
529 * Increases the returned router's refcount
530 */
63b01037
SE
531static struct neigh_node *
532batadv_find_ifalter_router(struct orig_node *primary_orig,
533 const struct hard_iface *recv_if)
55158629
LL
534{
535 struct neigh_node *tmp_neigh_node;
536 struct neigh_node *router = NULL, *first_candidate = NULL;
537
538 rcu_read_lock();
539 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
540 bonding_list) {
541 if (!first_candidate)
542 first_candidate = tmp_neigh_node;
543
544 /* recv_if == NULL on the first node. */
545 if (tmp_neigh_node->if_incoming == recv_if)
546 continue;
547
548 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
549 continue;
550
551 /* if we don't have a router yet
9cfc7bd6
SE
552 * or this one is better, choose it.
553 */
55158629
LL
554 if ((!router) ||
555 (tmp_neigh_node->tq_avg > router->tq_avg)) {
556 /* decrement refcount of
9cfc7bd6
SE
557 * previously selected router
558 */
55158629 559 if (router)
7d211efc 560 batadv_neigh_node_free_ref(router);
55158629
LL
561
562 router = tmp_neigh_node;
563 atomic_inc_not_zero(&router->refcount);
564 }
565
7d211efc 566 batadv_neigh_node_free_ref(tmp_neigh_node);
55158629
LL
567 }
568
569 /* use the first candidate if nothing was found. */
570 if (!router && first_candidate &&
571 atomic_inc_not_zero(&first_candidate->refcount))
572 router = first_candidate;
573
574 rcu_read_unlock();
575 return router;
576}
577
30d3c511 578int batadv_recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if)
a73105b8
AQ
579{
580 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
581 struct tt_query_packet *tt_query;
f25bd58a 582 uint16_t tt_size;
a73105b8 583 struct ethhdr *ethhdr;
1eda58bf 584 char tt_flag;
a73105b8
AQ
585
586 /* drop packet if it has not necessary minimum size */
587 if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet))))
588 goto out;
589
590 /* I could need to modify it */
591 if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0)
592 goto out;
593
594 ethhdr = (struct ethhdr *)skb_mac_header(skb);
595
596 /* packet with unicast indication but broadcast recipient */
597 if (is_broadcast_ether_addr(ethhdr->h_dest))
598 goto out;
599
600 /* packet with broadcast sender address */
601 if (is_broadcast_ether_addr(ethhdr->h_source))
602 goto out;
603
604 tt_query = (struct tt_query_packet *)skb->data;
605
a73105b8
AQ
606 switch (tt_query->flags & TT_QUERY_TYPE_MASK) {
607 case TT_REQUEST:
f8214865
MH
608 batadv_inc_counter(bat_priv, BAT_CNT_TT_REQUEST_RX);
609
a73105b8 610 /* If we cannot provide an answer the tt_request is
9cfc7bd6
SE
611 * forwarded
612 */
08c36d3e 613 if (!batadv_send_tt_response(bat_priv, tt_query)) {
1eda58bf
SE
614 tt_flag = tt_query->flags & TT_FULL_TABLE ? 'F' : '.';
615 batadv_dbg(DBG_TT, bat_priv,
616 "Routing TT_REQUEST to %pM [%c]\n",
617 tt_query->dst,
618 tt_flag);
63b01037 619 return batadv_route_unicast_packet(skb, recv_if);
a73105b8
AQ
620 }
621 break;
622 case TT_RESPONSE:
f8214865
MH
623 batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_RX);
624
3193e8fd 625 if (batadv_is_my_mac(tt_query->dst)) {
dc58fe32 626 /* packet needs to be linearized to access the TT
9cfc7bd6
SE
627 * changes
628 */
dc58fe32
AQ
629 if (skb_linearize(skb) < 0)
630 goto out;
d2b6cc8e
AQ
631 /* skb_linearize() possibly changed skb->data */
632 tt_query = (struct tt_query_packet *)skb->data;
a73105b8 633
08c36d3e 634 tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
8b7342d6
AQ
635
636 /* Ensure we have all the claimed data */
637 if (unlikely(skb_headlen(skb) <
f25bd58a 638 sizeof(struct tt_query_packet) + tt_size))
8b7342d6
AQ
639 goto out;
640
08c36d3e 641 batadv_handle_tt_response(bat_priv, tt_query);
dc58fe32 642 } else {
1eda58bf
SE
643 tt_flag = tt_query->flags & TT_FULL_TABLE ? 'F' : '.';
644 batadv_dbg(DBG_TT, bat_priv,
645 "Routing TT_RESPONSE to %pM [%c]\n",
646 tt_query->dst,
647 tt_flag);
63b01037 648 return batadv_route_unicast_packet(skb, recv_if);
a73105b8
AQ
649 }
650 break;
651 }
652
653out:
654 /* returning NET_RX_DROP will make the caller function kfree the skb */
655 return NET_RX_DROP;
656}
657
30d3c511 658int batadv_recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
cc47f66e
AQ
659{
660 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
661 struct roam_adv_packet *roam_adv_packet;
662 struct orig_node *orig_node;
663 struct ethhdr *ethhdr;
664
665 /* drop packet if it has not necessary minimum size */
666 if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
667 goto out;
668
669 ethhdr = (struct ethhdr *)skb_mac_header(skb);
670
671 /* packet with unicast indication but broadcast recipient */
672 if (is_broadcast_ether_addr(ethhdr->h_dest))
673 goto out;
674
675 /* packet with broadcast sender address */
676 if (is_broadcast_ether_addr(ethhdr->h_source))
677 goto out;
678
f8214865
MH
679 batadv_inc_counter(bat_priv, BAT_CNT_TT_ROAM_ADV_RX);
680
cc47f66e
AQ
681 roam_adv_packet = (struct roam_adv_packet *)skb->data;
682
3193e8fd 683 if (!batadv_is_my_mac(roam_adv_packet->dst))
63b01037 684 return batadv_route_unicast_packet(skb, recv_if);
cc47f66e 685
20ff9d59
SW
686 /* check if it is a backbone gateway. we don't accept
687 * roaming advertisement from it, as it has the same
688 * entries as we have.
689 */
08adf151 690 if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
20ff9d59
SW
691 goto out;
692
da641193 693 orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
cc47f66e
AQ
694 if (!orig_node)
695 goto out;
696
1eda58bf
SE
697 batadv_dbg(DBG_TT, bat_priv,
698 "Received ROAMING_ADV from %pM (client %pM)\n",
699 roam_adv_packet->src, roam_adv_packet->client);
cc47f66e 700
08c36d3e
SE
701 batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
702 atomic_read(&orig_node->last_ttvn) + 1, true,
703 false);
cc47f66e
AQ
704
705 /* Roaming phase starts: I have new information but the ttvn has not
706 * been incremented yet. This flag will make me check all the incoming
9cfc7bd6
SE
707 * packets for the correct destination.
708 */
cc47f66e
AQ
709 bat_priv->tt_poss_change = true;
710
7d211efc 711 batadv_orig_node_free_ref(orig_node);
cc47f66e
AQ
712out:
713 /* returning NET_RX_DROP will make the caller function kfree the skb */
714 return NET_RX_DROP;
715}
716
c6c8fea2 717/* find a suitable router for this originator, and use
a4c135c5 718 * bonding if possible. increases the found neighbors
9cfc7bd6
SE
719 * refcount.
720 */
30d3c511
SE
721struct neigh_node *batadv_find_router(struct bat_priv *bat_priv,
722 struct orig_node *orig_node,
723 const struct hard_iface *recv_if)
c6c8fea2
SE
724{
725 struct orig_node *primary_orig_node;
726 struct orig_node *router_orig;
55158629 727 struct neigh_node *router;
c6c8fea2
SE
728 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
729 int bonding_enabled;
da641193 730 uint8_t *primary_addr;
c6c8fea2
SE
731
732 if (!orig_node)
733 return NULL;
734
7d211efc 735 router = batadv_orig_node_get_router(orig_node);
e1a5382f 736 if (!router)
01df2b65 737 goto err;
c6c8fea2
SE
738
739 /* without bonding, the first node should
9cfc7bd6
SE
740 * always choose the default router.
741 */
c6c8fea2
SE
742 bonding_enabled = atomic_read(&bat_priv->bonding);
743
a4c135c5
SW
744 rcu_read_lock();
745 /* select default router to output */
e1a5382f 746 router_orig = router->orig_node;
01df2b65
ML
747 if (!router_orig)
748 goto err_unlock;
a4c135c5 749
a4c135c5
SW
750 if ((!recv_if) && (!bonding_enabled))
751 goto return_router;
c6c8fea2 752
da641193
SE
753 primary_addr = router_orig->primary_addr;
754
c6c8fea2 755 /* if we have something in the primary_addr, we can search
9cfc7bd6
SE
756 * for a potential bonding candidate.
757 */
1eda58bf 758 if (batadv_compare_eth(primary_addr, zero_mac))
a4c135c5 759 goto return_router;
c6c8fea2
SE
760
761 /* find the orig_node which has the primary interface. might
9cfc7bd6
SE
762 * even be the same as our router_orig in many cases
763 */
1eda58bf 764 if (batadv_compare_eth(primary_addr, router_orig->orig)) {
c6c8fea2
SE
765 primary_orig_node = router_orig;
766 } else {
da641193
SE
767 primary_orig_node = batadv_orig_hash_find(bat_priv,
768 primary_addr);
c6c8fea2 769 if (!primary_orig_node)
a4c135c5 770 goto return_router;
7aadf889 771
7d211efc 772 batadv_orig_node_free_ref(primary_orig_node);
c6c8fea2
SE
773 }
774
775 /* with less than 2 candidates, we can't do any
9cfc7bd6
SE
776 * bonding and prefer the original router.
777 */
a4c135c5
SW
778 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
779 goto return_router;
c6c8fea2 780
c6c8fea2
SE
781 /* all nodes between should choose a candidate which
782 * is is not on the interface where the packet came
9cfc7bd6
SE
783 * in.
784 */
7d211efc 785 batadv_neigh_node_free_ref(router);
c6c8fea2 786
55158629 787 if (bonding_enabled)
63b01037 788 router = batadv_find_bond_router(primary_orig_node, recv_if);
55158629 789 else
63b01037 790 router = batadv_find_ifalter_router(primary_orig_node, recv_if);
c6c8fea2 791
a4c135c5 792return_router:
e2cbc11c
AQ
793 if (router && router->if_incoming->if_status != IF_ACTIVE)
794 goto err_unlock;
795
a4c135c5 796 rcu_read_unlock();
c6c8fea2 797 return router;
01df2b65
ML
798err_unlock:
799 rcu_read_unlock();
800err:
801 if (router)
7d211efc 802 batadv_neigh_node_free_ref(router);
01df2b65 803 return NULL;
c6c8fea2
SE
804}
805
63b01037 806static int batadv_check_unicast_packet(struct sk_buff *skb, int hdr_size)
c6c8fea2
SE
807{
808 struct ethhdr *ethhdr;
809
810 /* drop packet if it has not necessary minimum size */
811 if (unlikely(!pskb_may_pull(skb, hdr_size)))
812 return -1;
813
814 ethhdr = (struct ethhdr *)skb_mac_header(skb);
815
816 /* packet with unicast indication but broadcast recipient */
817 if (is_broadcast_ether_addr(ethhdr->h_dest))
818 return -1;
819
820 /* packet with broadcast sender address */
821 if (is_broadcast_ether_addr(ethhdr->h_source))
822 return -1;
823
824 /* not for me */
3193e8fd 825 if (!batadv_is_my_mac(ethhdr->h_dest))
c6c8fea2
SE
826 return -1;
827
828 return 0;
829}
830
63b01037
SE
831static int batadv_route_unicast_packet(struct sk_buff *skb,
832 struct hard_iface *recv_if)
c6c8fea2
SE
833{
834 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
44524fcd
ML
835 struct orig_node *orig_node = NULL;
836 struct neigh_node *neigh_node = NULL;
c6c8fea2
SE
837 struct unicast_packet *unicast_packet;
838 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
44524fcd 839 int ret = NET_RX_DROP;
c6c8fea2
SE
840 struct sk_buff *new_skb;
841
842 unicast_packet = (struct unicast_packet *)skb->data;
843
844 /* TTL exceeded */
76543d14 845 if (unicast_packet->header.ttl < 2) {
86ceb360
SE
846 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
847 ethhdr->h_source, unicast_packet->dest);
44524fcd 848 goto out;
c6c8fea2
SE
849 }
850
851 /* get routing information */
da641193 852 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
7aadf889 853
44524fcd 854 if (!orig_node)
b5a6f69c 855 goto out;
c6c8fea2 856
a4c135c5 857 /* find_router() increases neigh_nodes refcount if found. */
30d3c511 858 neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
c6c8fea2 859
d0072609 860 if (!neigh_node)
44524fcd 861 goto out;
c6c8fea2
SE
862
863 /* create a copy of the skb, if needed, to modify it. */
0d125074 864 if (skb_cow(skb, ETH_HLEN) < 0)
44524fcd 865 goto out;
c6c8fea2
SE
866
867 unicast_packet = (struct unicast_packet *)skb->data;
868
76543d14 869 if (unicast_packet->header.packet_type == BAT_UNICAST &&
c6c8fea2 870 atomic_read(&bat_priv->fragmentation) &&
d0072609 871 skb->len > neigh_node->if_incoming->net_dev->mtu) {
88ed1e77
SE
872 ret = batadv_frag_send_skb(skb, bat_priv,
873 neigh_node->if_incoming,
874 neigh_node->addr);
d0072609
ML
875 goto out;
876 }
c6c8fea2 877
76543d14 878 if (unicast_packet->header.packet_type == BAT_UNICAST_FRAG &&
f0530ee5
SE
879 batadv_frag_can_reassemble(skb,
880 neigh_node->if_incoming->net_dev->mtu)) {
c6c8fea2 881
88ed1e77 882 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
c6c8fea2
SE
883
884 if (ret == NET_RX_DROP)
44524fcd 885 goto out;
c6c8fea2
SE
886
887 /* packet was buffered for late merge */
44524fcd
ML
888 if (!new_skb) {
889 ret = NET_RX_SUCCESS;
890 goto out;
891 }
c6c8fea2
SE
892
893 skb = new_skb;
894 unicast_packet = (struct unicast_packet *)skb->data;
895 }
896
897 /* decrement ttl */
76543d14 898 unicast_packet->header.ttl--;
c6c8fea2 899
f8214865
MH
900 /* Update stats counter */
901 batadv_inc_counter(bat_priv, BAT_CNT_FORWARD);
902 batadv_add_counter(bat_priv, BAT_CNT_FORWARD_BYTES,
903 skb->len + ETH_HLEN);
904
c6c8fea2 905 /* route it */
9455e34c 906 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
44524fcd 907 ret = NET_RX_SUCCESS;
c6c8fea2 908
44524fcd
ML
909out:
910 if (neigh_node)
7d211efc 911 batadv_neigh_node_free_ref(neigh_node);
44524fcd 912 if (orig_node)
7d211efc 913 batadv_orig_node_free_ref(orig_node);
44524fcd 914 return ret;
c6c8fea2
SE
915}
916
63b01037
SE
917static int batadv_check_unicast_ttvn(struct bat_priv *bat_priv,
918 struct sk_buff *skb) {
a73105b8
AQ
919 uint8_t curr_ttvn;
920 struct orig_node *orig_node;
921 struct ethhdr *ethhdr;
922 struct hard_iface *primary_if;
923 struct unicast_packet *unicast_packet;
cc47f66e 924 bool tt_poss_change;
a73105b8
AQ
925
926 /* I could need to modify it */
927 if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
928 return 0;
929
930 unicast_packet = (struct unicast_packet *)skb->data;
931
3193e8fd 932 if (batadv_is_my_mac(unicast_packet->dest)) {
cc47f66e 933 tt_poss_change = bat_priv->tt_poss_change;
a73105b8 934 curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
cc47f66e 935 } else {
da641193
SE
936 orig_node = batadv_orig_hash_find(bat_priv,
937 unicast_packet->dest);
a73105b8
AQ
938
939 if (!orig_node)
940 return 0;
941
942 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
cc47f66e 943 tt_poss_change = orig_node->tt_poss_change;
7d211efc 944 batadv_orig_node_free_ref(orig_node);
a73105b8
AQ
945 }
946
947 /* Check whether I have to reroute the packet */
cc47f66e 948 if (seq_before(unicast_packet->ttvn, curr_ttvn) || tt_poss_change) {
8710e261
AQ
949 /* check if there is enough data before accessing it */
950 if (pskb_may_pull(skb, sizeof(struct unicast_packet) +
951 ETH_HLEN) < 0)
a73105b8
AQ
952 return 0;
953
954 ethhdr = (struct ethhdr *)(skb->data +
955 sizeof(struct unicast_packet));
3275e7cc
AQ
956
957 /* we don't have an updated route for this client, so we should
958 * not try to reroute the packet!!
959 */
08c36d3e
SE
960 if (batadv_tt_global_client_is_roaming(bat_priv,
961 ethhdr->h_dest))
3275e7cc
AQ
962 return 1;
963
08c36d3e
SE
964 orig_node = batadv_transtable_search(bat_priv, NULL,
965 ethhdr->h_dest);
a73105b8
AQ
966
967 if (!orig_node) {
08c36d3e 968 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
a73105b8 969 return 0;
e5d89254 970 primary_if = batadv_primary_if_get_selected(bat_priv);
a73105b8
AQ
971 if (!primary_if)
972 return 0;
973 memcpy(unicast_packet->dest,
974 primary_if->net_dev->dev_addr, ETH_ALEN);
e5d89254 975 batadv_hardif_free_ref(primary_if);
a73105b8
AQ
976 } else {
977 memcpy(unicast_packet->dest, orig_node->orig,
978 ETH_ALEN);
979 curr_ttvn = (uint8_t)
980 atomic_read(&orig_node->last_ttvn);
7d211efc 981 batadv_orig_node_free_ref(orig_node);
a73105b8
AQ
982 }
983
1eda58bf
SE
984 batadv_dbg(DBG_ROUTES, bat_priv,
985 "TTVN mismatch (old_ttvn %u new_ttvn %u)! Rerouting unicast packet (for %pM) to %pM\n",
986 unicast_packet->ttvn, curr_ttvn, ethhdr->h_dest,
987 unicast_packet->dest);
a73105b8
AQ
988
989 unicast_packet->ttvn = curr_ttvn;
990 }
991 return 1;
992}
993
30d3c511 994int batadv_recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
c6c8fea2 995{
a73105b8 996 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
c6c8fea2 997 struct unicast_packet *unicast_packet;
704509b8 998 int hdr_size = sizeof(*unicast_packet);
c6c8fea2 999
63b01037 1000 if (batadv_check_unicast_packet(skb, hdr_size) < 0)
c6c8fea2
SE
1001 return NET_RX_DROP;
1002
63b01037 1003 if (!batadv_check_unicast_ttvn(bat_priv, skb))
a73105b8
AQ
1004 return NET_RX_DROP;
1005
c6c8fea2
SE
1006 unicast_packet = (struct unicast_packet *)skb->data;
1007
1008 /* packet for me */
3193e8fd 1009 if (batadv_is_my_mac(unicast_packet->dest)) {
04b482a2
SE
1010 batadv_interface_rx(recv_if->soft_iface, skb, recv_if,
1011 hdr_size);
c6c8fea2
SE
1012 return NET_RX_SUCCESS;
1013 }
1014
63b01037 1015 return batadv_route_unicast_packet(skb, recv_if);
c6c8fea2
SE
1016}
1017
30d3c511
SE
1018int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
1019 struct hard_iface *recv_if)
c6c8fea2
SE
1020{
1021 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1022 struct unicast_frag_packet *unicast_packet;
704509b8 1023 int hdr_size = sizeof(*unicast_packet);
c6c8fea2
SE
1024 struct sk_buff *new_skb = NULL;
1025 int ret;
1026
63b01037 1027 if (batadv_check_unicast_packet(skb, hdr_size) < 0)
c6c8fea2
SE
1028 return NET_RX_DROP;
1029
63b01037 1030 if (!batadv_check_unicast_ttvn(bat_priv, skb))
a73105b8
AQ
1031 return NET_RX_DROP;
1032
c6c8fea2
SE
1033 unicast_packet = (struct unicast_frag_packet *)skb->data;
1034
1035 /* packet for me */
3193e8fd 1036 if (batadv_is_my_mac(unicast_packet->dest)) {
c6c8fea2 1037
88ed1e77 1038 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
c6c8fea2
SE
1039
1040 if (ret == NET_RX_DROP)
1041 return NET_RX_DROP;
1042
1043 /* packet was buffered for late merge */
1044 if (!new_skb)
1045 return NET_RX_SUCCESS;
1046
04b482a2
SE
1047 batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
1048 sizeof(struct unicast_packet));
c6c8fea2
SE
1049 return NET_RX_SUCCESS;
1050 }
1051
63b01037 1052 return batadv_route_unicast_packet(skb, recv_if);
c6c8fea2
SE
1053}
1054
1055
30d3c511 1056int batadv_recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
c6c8fea2
SE
1057{
1058 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
f3e0008f 1059 struct orig_node *orig_node = NULL;
c6c8fea2
SE
1060 struct bcast_packet *bcast_packet;
1061 struct ethhdr *ethhdr;
704509b8 1062 int hdr_size = sizeof(*bcast_packet);
f3e0008f 1063 int ret = NET_RX_DROP;
c6c8fea2
SE
1064 int32_t seq_diff;
1065
1066 /* drop packet if it has not necessary minimum size */
1067 if (unlikely(!pskb_may_pull(skb, hdr_size)))
f3e0008f 1068 goto out;
c6c8fea2
SE
1069
1070 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1071
1072 /* packet with broadcast indication but unicast recipient */
1073 if (!is_broadcast_ether_addr(ethhdr->h_dest))
f3e0008f 1074 goto out;
c6c8fea2
SE
1075
1076 /* packet with broadcast sender address */
1077 if (is_broadcast_ether_addr(ethhdr->h_source))
f3e0008f 1078 goto out;
c6c8fea2
SE
1079
1080 /* ignore broadcasts sent by myself */
3193e8fd 1081 if (batadv_is_my_mac(ethhdr->h_source))
f3e0008f 1082 goto out;
c6c8fea2
SE
1083
1084 bcast_packet = (struct bcast_packet *)skb->data;
1085
1086 /* ignore broadcasts originated by myself */
3193e8fd 1087 if (batadv_is_my_mac(bcast_packet->orig))
f3e0008f 1088 goto out;
c6c8fea2 1089
76543d14 1090 if (bcast_packet->header.ttl < 2)
f3e0008f 1091 goto out;
c6c8fea2 1092
da641193 1093 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
f3e0008f
ML
1094
1095 if (!orig_node)
b5a6f69c 1096 goto out;
c6c8fea2 1097
f3e0008f 1098 spin_lock_bh(&orig_node->bcast_seqno_lock);
c6c8fea2
SE
1099
1100 /* check whether the packet is a duplicate */
9b4a1159
SE
1101 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1102 ntohl(bcast_packet->seqno)))
f3e0008f 1103 goto spin_unlock;
c6c8fea2
SE
1104
1105 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1106
1107 /* check whether the packet is old and the host just restarted. */
30d3c511
SE
1108 if (batadv_window_protected(bat_priv, seq_diff,
1109 &orig_node->bcast_seqno_reset))
f3e0008f 1110 goto spin_unlock;
c6c8fea2
SE
1111
1112 /* mark broadcast in flood history, update window position
9cfc7bd6
SE
1113 * if required.
1114 */
0f5f9322 1115 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
c6c8fea2
SE
1116 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1117
f3e0008f 1118 spin_unlock_bh(&orig_node->bcast_seqno_lock);
f3e0008f 1119
fe2da6ff 1120 /* check whether this has been sent by another originator before */
08adf151 1121 if (batadv_bla_check_bcast_duplist(bat_priv, bcast_packet, hdr_size))
fe2da6ff
SW
1122 goto out;
1123
c6c8fea2 1124 /* rebroadcast packet */
9455e34c 1125 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
c6c8fea2 1126
23721387
SW
1127 /* don't hand the broadcast up if it is from an originator
1128 * from the same backbone.
1129 */
08adf151 1130 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
23721387
SW
1131 goto out;
1132
c6c8fea2 1133 /* broadcast for me */
04b482a2 1134 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
f3e0008f
ML
1135 ret = NET_RX_SUCCESS;
1136 goto out;
c6c8fea2 1137
f3e0008f
ML
1138spin_unlock:
1139 spin_unlock_bh(&orig_node->bcast_seqno_lock);
f3e0008f
ML
1140out:
1141 if (orig_node)
7d211efc 1142 batadv_orig_node_free_ref(orig_node);
f3e0008f 1143 return ret;
c6c8fea2
SE
1144}
1145
30d3c511 1146int batadv_recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
c6c8fea2
SE
1147{
1148 struct vis_packet *vis_packet;
1149 struct ethhdr *ethhdr;
1150 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
704509b8 1151 int hdr_size = sizeof(*vis_packet);
c6c8fea2
SE
1152
1153 /* keep skb linear */
1154 if (skb_linearize(skb) < 0)
1155 return NET_RX_DROP;
1156
1157 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1158 return NET_RX_DROP;
1159
1160 vis_packet = (struct vis_packet *)skb->data;
1161 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1162
1163 /* not for me */
3193e8fd 1164 if (!batadv_is_my_mac(ethhdr->h_dest))
c6c8fea2
SE
1165 return NET_RX_DROP;
1166
1167 /* ignore own packets */
3193e8fd 1168 if (batadv_is_my_mac(vis_packet->vis_orig))
c6c8fea2
SE
1169 return NET_RX_DROP;
1170
3193e8fd 1171 if (batadv_is_my_mac(vis_packet->sender_orig))
c6c8fea2
SE
1172 return NET_RX_DROP;
1173
1174 switch (vis_packet->vis_type) {
1175 case VIS_TYPE_SERVER_SYNC:
d0f714f4
SE
1176 batadv_receive_server_sync_packet(bat_priv, vis_packet,
1177 skb_headlen(skb));
c6c8fea2
SE
1178 break;
1179
1180 case VIS_TYPE_CLIENT_UPDATE:
d0f714f4
SE
1181 batadv_receive_client_update_packet(bat_priv, vis_packet,
1182 skb_headlen(skb));
c6c8fea2
SE
1183 break;
1184
1185 default: /* ignore unknown packet */
1186 break;
1187 }
1188
1189 /* We take a copy of the data in the packet, so we should
9cfc7bd6
SE
1190 * always free the skbuf.
1191 */
c6c8fea2
SE
1192 return NET_RX_DROP;
1193}
This page took 0.212018 seconds and 5 git commands to generate.