batman-adv: add build check macros for packet member offset
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / net / batman-adv / routing.c
CommitLineData
0b873931 1/* Copyright (C) 2007-2013 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 "unicast.h"
23721387 29#include "bridge_loop_avoidance.h"
c384ea3e 30#include "distributed-arp-table.h"
95332477 31#include "network-coding.h"
c6c8fea2 32
63b01037 33static int batadv_route_unicast_packet(struct sk_buff *skb,
56303d34 34 struct batadv_hard_iface *recv_if);
a7f6ee94 35
56303d34
SE
36static void _batadv_update_route(struct batadv_priv *bat_priv,
37 struct batadv_orig_node *orig_node,
38 struct batadv_neigh_node *neigh_node)
c6c8fea2 39{
56303d34 40 struct batadv_neigh_node *curr_router;
e1a5382f 41
7d211efc 42 curr_router = batadv_orig_node_get_router(orig_node);
a8e7f4bc 43
c6c8fea2 44 /* route deleted */
e1a5382f 45 if ((curr_router) && (!neigh_node)) {
39c75a51
SE
46 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
47 "Deleting route towards: %pM\n", orig_node->orig);
08c36d3e
SE
48 batadv_tt_global_del_orig(bat_priv, orig_node,
49 "Deleted route towards originator");
c6c8fea2 50
e1a5382f
LL
51 /* route added */
52 } else if ((!curr_router) && (neigh_node)) {
39c75a51 53 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
1eda58bf
SE
54 "Adding route towards: %pM (via %pM)\n",
55 orig_node->orig, neigh_node->addr);
e1a5382f 56 /* route changed */
bb899b89 57 } else if (neigh_node && curr_router) {
39c75a51 58 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
1eda58bf
SE
59 "Changing route towards: %pM (now via %pM - was via %pM)\n",
60 orig_node->orig, neigh_node->addr,
61 curr_router->addr);
c6c8fea2
SE
62 }
63
e1a5382f 64 if (curr_router)
7d211efc 65 batadv_neigh_node_free_ref(curr_router);
e1a5382f
LL
66
67 /* increase refcount of new best neighbor */
44524fcd
ML
68 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
69 neigh_node = NULL;
e1a5382f
LL
70
71 spin_lock_bh(&orig_node->neigh_list_lock);
72 rcu_assign_pointer(orig_node->router, neigh_node);
73 spin_unlock_bh(&orig_node->neigh_list_lock);
74
75 /* decrease refcount of previous best neighbor */
76 if (curr_router)
7d211efc 77 batadv_neigh_node_free_ref(curr_router);
c6c8fea2
SE
78}
79
56303d34
SE
80void batadv_update_route(struct batadv_priv *bat_priv,
81 struct batadv_orig_node *orig_node,
82 struct batadv_neigh_node *neigh_node)
c6c8fea2 83{
56303d34 84 struct batadv_neigh_node *router = NULL;
c6c8fea2
SE
85
86 if (!orig_node)
e1a5382f
LL
87 goto out;
88
7d211efc 89 router = batadv_orig_node_get_router(orig_node);
c6c8fea2 90
e1a5382f 91 if (router != neigh_node)
63b01037 92 _batadv_update_route(bat_priv, orig_node, neigh_node);
e1a5382f
LL
93
94out:
95 if (router)
7d211efc 96 batadv_neigh_node_free_ref(router);
c6c8fea2
SE
97}
98
a4c135c5 99/* caller must hold the neigh_list_lock */
56303d34
SE
100void batadv_bonding_candidate_del(struct batadv_orig_node *orig_node,
101 struct batadv_neigh_node *neigh_node)
a4c135c5
SW
102{
103 /* this neighbor is not part of our candidate list */
104 if (list_empty(&neigh_node->bonding_list))
105 goto out;
106
107 list_del_rcu(&neigh_node->bonding_list);
a4c135c5 108 INIT_LIST_HEAD(&neigh_node->bonding_list);
7d211efc 109 batadv_neigh_node_free_ref(neigh_node);
a4c135c5
SW
110 atomic_dec(&orig_node->bond_candidates);
111
112out:
113 return;
114}
115
56303d34
SE
116void batadv_bonding_candidate_add(struct batadv_orig_node *orig_node,
117 struct batadv_neigh_node *neigh_node)
a4c135c5 118{
56303d34 119 struct batadv_neigh_node *tmp_neigh_node, *router = NULL;
e1a5382f 120 uint8_t interference_candidate = 0;
a4c135c5
SW
121
122 spin_lock_bh(&orig_node->neigh_list_lock);
123
124 /* only consider if it has the same primary address ... */
1eda58bf
SE
125 if (!batadv_compare_eth(orig_node->orig,
126 neigh_node->orig_node->primary_addr))
a4c135c5
SW
127 goto candidate_del;
128
7d211efc 129 router = batadv_orig_node_get_router(orig_node);
e1a5382f 130 if (!router)
a4c135c5
SW
131 goto candidate_del;
132
a4c135c5 133 /* ... and is good enough to be considered */
42d0b044 134 if (neigh_node->tq_avg < router->tq_avg - BATADV_BONDING_TQ_THRESHOLD)
a4c135c5
SW
135 goto candidate_del;
136
9cfc7bd6 137 /* check if we have another candidate with the same mac address or
a4c135c5
SW
138 * interface. If we do, we won't select this candidate because of
139 * possible interference.
140 */
b67bfe0d 141 hlist_for_each_entry_rcu(tmp_neigh_node,
a4c135c5 142 &orig_node->neigh_list, list) {
a4c135c5
SW
143 if (tmp_neigh_node == neigh_node)
144 continue;
145
146 /* we only care if the other candidate is even
9cfc7bd6
SE
147 * considered as candidate.
148 */
a4c135c5
SW
149 if (list_empty(&tmp_neigh_node->bonding_list))
150 continue;
151
152 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
1eda58bf
SE
153 (batadv_compare_eth(neigh_node->addr,
154 tmp_neigh_node->addr))) {
a4c135c5
SW
155 interference_candidate = 1;
156 break;
157 }
158 }
159
160 /* don't care further if it is an interference candidate */
161 if (interference_candidate)
162 goto candidate_del;
163
164 /* this neighbor already is part of our candidate list */
165 if (!list_empty(&neigh_node->bonding_list))
166 goto out;
167
44524fcd
ML
168 if (!atomic_inc_not_zero(&neigh_node->refcount))
169 goto out;
170
a4c135c5 171 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
a4c135c5
SW
172 atomic_inc(&orig_node->bond_candidates);
173 goto out;
174
175candidate_del:
30d3c511 176 batadv_bonding_candidate_del(orig_node, neigh_node);
a4c135c5
SW
177
178out:
179 spin_unlock_bh(&orig_node->neigh_list_lock);
e1a5382f
LL
180
181 if (router)
7d211efc 182 batadv_neigh_node_free_ref(router);
a4c135c5
SW
183}
184
185/* copy primary address for bonding */
30d3c511 186void
56303d34
SE
187batadv_bonding_save_primary(const struct batadv_orig_node *orig_node,
188 struct batadv_orig_node *orig_neigh_node,
96412690 189 const struct batadv_ogm_packet *batman_ogm_packet)
a4c135c5 190{
acd34afa 191 if (!(batman_ogm_packet->flags & BATADV_PRIMARIES_FIRST_HOP))
a4c135c5
SW
192 return;
193
194 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
195}
196
c6c8fea2
SE
197/* checks whether the host restarted and is in the protection time.
198 * returns:
199 * 0 if the packet is to be accepted
200 * 1 if the packet is to be ignored.
201 */
56303d34 202int batadv_window_protected(struct batadv_priv *bat_priv, int32_t seq_num_diff,
30d3c511 203 unsigned long *last_reset)
c6c8fea2 204{
42d0b044
SE
205 if (seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE ||
206 seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
207 if (!batadv_has_timed_out(*last_reset,
208 BATADV_RESET_PROTECTION_MS))
c6c8fea2 209 return 1;
8c7bf248
ML
210
211 *last_reset = jiffies;
39c75a51 212 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 213 "old packet received, start protection\n");
c6c8fea2 214 }
8c7bf248 215
c6c8fea2
SE
216 return 0;
217}
218
30d3c511 219bool batadv_check_management_packet(struct sk_buff *skb,
56303d34 220 struct batadv_hard_iface *hard_iface,
30d3c511 221 int header_len)
c6c8fea2 222{
c6c8fea2
SE
223 struct ethhdr *ethhdr;
224
225 /* drop packet if it has not necessary minimum size */
c3e29312
ML
226 if (unlikely(!pskb_may_pull(skb, header_len)))
227 return false;
c6c8fea2 228
7ed4be95 229 ethhdr = eth_hdr(skb);
c6c8fea2
SE
230
231 /* packet with broadcast indication but unicast recipient */
232 if (!is_broadcast_ether_addr(ethhdr->h_dest))
c3e29312 233 return false;
c6c8fea2
SE
234
235 /* packet with broadcast sender address */
236 if (is_broadcast_ether_addr(ethhdr->h_source))
c3e29312 237 return false;
c6c8fea2
SE
238
239 /* create a copy of the skb, if needed, to modify it. */
240 if (skb_cow(skb, 0) < 0)
c3e29312 241 return false;
c6c8fea2
SE
242
243 /* keep skb linear */
244 if (skb_linearize(skb) < 0)
c3e29312 245 return false;
c6c8fea2 246
c3e29312 247 return true;
c6c8fea2
SE
248}
249
56303d34 250static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
63b01037 251 struct sk_buff *skb, size_t icmp_len)
c6c8fea2 252{
56303d34
SE
253 struct batadv_hard_iface *primary_if = NULL;
254 struct batadv_orig_node *orig_node = NULL;
96412690 255 struct batadv_icmp_packet_rr *icmp_packet;
44524fcd 256 int ret = NET_RX_DROP;
c6c8fea2 257
96412690 258 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
c6c8fea2
SE
259
260 /* add data to device queue */
acd34afa 261 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
9039dc7e 262 batadv_socket_receive_packet(icmp_packet, icmp_len);
44524fcd 263 goto out;
c6c8fea2
SE
264 }
265
e5d89254 266 primary_if = batadv_primary_if_get_selected(bat_priv);
32ae9b22 267 if (!primary_if)
44524fcd 268 goto out;
c6c8fea2
SE
269
270 /* answer echo request (ping) */
271 /* get routing information */
da641193 272 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
44524fcd 273 if (!orig_node)
e1a5382f 274 goto out;
c6c8fea2 275
44524fcd 276 /* create a copy of the skb, if needed, to modify it. */
0d125074 277 if (skb_cow(skb, ETH_HLEN) < 0)
44524fcd
ML
278 goto out;
279
96412690 280 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
44524fcd
ML
281
282 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
32ae9b22 283 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
acd34afa 284 icmp_packet->msg_type = BATADV_ECHO_REPLY;
42d0b044 285 icmp_packet->header.ttl = BATADV_TTL;
44524fcd 286
e91ecfc6 287 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
bb351ba0 288 ret = NET_RX_SUCCESS;
c6c8fea2 289
44524fcd 290out:
32ae9b22 291 if (primary_if)
e5d89254 292 batadv_hardif_free_ref(primary_if);
44524fcd 293 if (orig_node)
7d211efc 294 batadv_orig_node_free_ref(orig_node);
c6c8fea2
SE
295 return ret;
296}
297
56303d34 298static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
63b01037 299 struct sk_buff *skb)
c6c8fea2 300{
56303d34
SE
301 struct batadv_hard_iface *primary_if = NULL;
302 struct batadv_orig_node *orig_node = NULL;
96412690 303 struct batadv_icmp_packet *icmp_packet;
44524fcd 304 int ret = NET_RX_DROP;
c6c8fea2 305
96412690 306 icmp_packet = (struct batadv_icmp_packet *)skb->data;
c6c8fea2
SE
307
308 /* send TTL exceeded if packet is an echo request (traceroute) */
acd34afa 309 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
86ceb360
SE
310 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
311 icmp_packet->orig, icmp_packet->dst);
44524fcd 312 goto out;
c6c8fea2
SE
313 }
314
e5d89254 315 primary_if = batadv_primary_if_get_selected(bat_priv);
32ae9b22 316 if (!primary_if)
44524fcd 317 goto out;
c6c8fea2
SE
318
319 /* get routing information */
da641193 320 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
44524fcd 321 if (!orig_node)
e1a5382f 322 goto out;
c6c8fea2 323
44524fcd 324 /* create a copy of the skb, if needed, to modify it. */
0d125074 325 if (skb_cow(skb, ETH_HLEN) < 0)
44524fcd 326 goto out;
c6c8fea2 327
96412690 328 icmp_packet = (struct batadv_icmp_packet *)skb->data;
c6c8fea2 329
44524fcd 330 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
32ae9b22 331 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
acd34afa 332 icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
42d0b044 333 icmp_packet->header.ttl = BATADV_TTL;
44524fcd 334
e91ecfc6 335 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
bb351ba0 336 ret = NET_RX_SUCCESS;
c6c8fea2 337
44524fcd 338out:
32ae9b22 339 if (primary_if)
e5d89254 340 batadv_hardif_free_ref(primary_if);
44524fcd 341 if (orig_node)
7d211efc 342 batadv_orig_node_free_ref(orig_node);
c6c8fea2
SE
343 return ret;
344}
345
346
56303d34
SE
347int batadv_recv_icmp_packet(struct sk_buff *skb,
348 struct batadv_hard_iface *recv_if)
c6c8fea2 349{
56303d34 350 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
96412690 351 struct batadv_icmp_packet_rr *icmp_packet;
c6c8fea2 352 struct ethhdr *ethhdr;
56303d34 353 struct batadv_orig_node *orig_node = NULL;
96412690 354 int hdr_size = sizeof(struct batadv_icmp_packet);
44524fcd 355 int ret = NET_RX_DROP;
c6c8fea2 356
9cfc7bd6 357 /* we truncate all incoming icmp packets if they don't match our size */
96412690
SE
358 if (skb->len >= sizeof(struct batadv_icmp_packet_rr))
359 hdr_size = sizeof(struct batadv_icmp_packet_rr);
c6c8fea2
SE
360
361 /* drop packet if it has not necessary minimum size */
362 if (unlikely(!pskb_may_pull(skb, hdr_size)))
44524fcd 363 goto out;
c6c8fea2 364
7ed4be95 365 ethhdr = eth_hdr(skb);
c6c8fea2
SE
366
367 /* packet with unicast indication but broadcast recipient */
368 if (is_broadcast_ether_addr(ethhdr->h_dest))
44524fcd 369 goto out;
c6c8fea2
SE
370
371 /* packet with broadcast sender address */
372 if (is_broadcast_ether_addr(ethhdr->h_source))
44524fcd 373 goto out;
c6c8fea2
SE
374
375 /* not for me */
fe8a93b9 376 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
44524fcd 377 goto out;
c6c8fea2 378
96412690 379 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
c6c8fea2
SE
380
381 /* add record route information if not full */
96412690 382 if ((hdr_size == sizeof(struct batadv_icmp_packet_rr)) &&
7e071c79 383 (icmp_packet->rr_cur < BATADV_RR_LEN)) {
c6c8fea2 384 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
7c64fd98 385 ethhdr->h_dest, ETH_ALEN);
c6c8fea2
SE
386 icmp_packet->rr_cur++;
387 }
388
389 /* packet for me */
fe8a93b9 390 if (batadv_is_my_mac(bat_priv, icmp_packet->dst))
63b01037 391 return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size);
c6c8fea2
SE
392
393 /* TTL exceeded */
76543d14 394 if (icmp_packet->header.ttl < 2)
63b01037 395 return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
c6c8fea2 396
c6c8fea2 397 /* get routing information */
da641193 398 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
44524fcd 399 if (!orig_node)
e1a5382f 400 goto out;
c6c8fea2 401
44524fcd 402 /* create a copy of the skb, if needed, to modify it. */
0d125074 403 if (skb_cow(skb, ETH_HLEN) < 0)
44524fcd 404 goto out;
c6c8fea2 405
96412690 406 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
c6c8fea2 407
44524fcd 408 /* decrement ttl */
76543d14 409 icmp_packet->header.ttl--;
44524fcd
ML
410
411 /* route it */
e91ecfc6 412 if (batadv_send_skb_to_orig(skb, orig_node, recv_if) != NET_XMIT_DROP)
bb351ba0 413 ret = NET_RX_SUCCESS;
c6c8fea2 414
44524fcd 415out:
44524fcd 416 if (orig_node)
7d211efc 417 batadv_orig_node_free_ref(orig_node);
c6c8fea2
SE
418 return ret;
419}
420
55158629
LL
421/* In the bonding case, send the packets in a round
422 * robin fashion over the remaining interfaces.
423 *
424 * This method rotates the bonding list and increases the
9cfc7bd6
SE
425 * returned router's refcount.
426 */
56303d34
SE
427static struct batadv_neigh_node *
428batadv_find_bond_router(struct batadv_orig_node *primary_orig,
429 const struct batadv_hard_iface *recv_if)
55158629 430{
56303d34
SE
431 struct batadv_neigh_node *tmp_neigh_node;
432 struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
55158629
LL
433
434 rcu_read_lock();
435 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
436 bonding_list) {
437 if (!first_candidate)
438 first_candidate = tmp_neigh_node;
439
440 /* recv_if == NULL on the first node. */
441 if (tmp_neigh_node->if_incoming == recv_if)
442 continue;
443
444 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
445 continue;
446
447 router = tmp_neigh_node;
448 break;
449 }
450
451 /* use the first candidate if nothing was found. */
452 if (!router && first_candidate &&
453 atomic_inc_not_zero(&first_candidate->refcount))
454 router = first_candidate;
455
456 if (!router)
457 goto out;
458
459 /* selected should point to the next element
9cfc7bd6
SE
460 * after the current router
461 */
55158629
LL
462 spin_lock_bh(&primary_orig->neigh_list_lock);
463 /* this is a list_move(), which unfortunately
9cfc7bd6
SE
464 * does not exist as rcu version
465 */
55158629
LL
466 list_del_rcu(&primary_orig->bond_list);
467 list_add_rcu(&primary_orig->bond_list,
468 &router->bonding_list);
469 spin_unlock_bh(&primary_orig->neigh_list_lock);
470
471out:
472 rcu_read_unlock();
473 return router;
474}
475
476/* Interface Alternating: Use the best of the
477 * remaining candidates which are not using
478 * this interface.
479 *
9cfc7bd6
SE
480 * Increases the returned router's refcount
481 */
56303d34
SE
482static struct batadv_neigh_node *
483batadv_find_ifalter_router(struct batadv_orig_node *primary_orig,
484 const struct batadv_hard_iface *recv_if)
55158629 485{
56303d34
SE
486 struct batadv_neigh_node *tmp_neigh_node;
487 struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
55158629
LL
488
489 rcu_read_lock();
490 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
491 bonding_list) {
492 if (!first_candidate)
493 first_candidate = tmp_neigh_node;
494
495 /* recv_if == NULL on the first node. */
496 if (tmp_neigh_node->if_incoming == recv_if)
497 continue;
498
fe3f4cfe
SE
499 if (router && tmp_neigh_node->tq_avg <= router->tq_avg)
500 continue;
501
55158629
LL
502 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
503 continue;
504
fe3f4cfe
SE
505 /* decrement refcount of previously selected router */
506 if (router)
507 batadv_neigh_node_free_ref(router);
55158629 508
fe3f4cfe
SE
509 /* we found a better router (or at least one valid router) */
510 router = tmp_neigh_node;
55158629
LL
511 }
512
513 /* use the first candidate if nothing was found. */
514 if (!router && first_candidate &&
515 atomic_inc_not_zero(&first_candidate->refcount))
516 router = first_candidate;
517
518 rcu_read_unlock();
519 return router;
520}
521
f86ce0ad
MH
522/**
523 * batadv_check_unicast_packet - Check for malformed unicast packets
6e0895c2 524 * @bat_priv: the bat priv with all the soft interface information
f86ce0ad
MH
525 * @skb: packet to check
526 * @hdr_size: size of header to pull
527 *
528 * Check for short header and bad addresses in given packet. Returns negative
529 * value when check fails and 0 otherwise. The negative value depends on the
530 * reason: -ENODATA for bad header, -EBADR for broadcast destination or source,
531 * and -EREMOTE for non-local (other host) destination.
532 */
fe8a93b9
AQ
533static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
534 struct sk_buff *skb, int hdr_size)
ff51fd70
MH
535{
536 struct ethhdr *ethhdr;
537
538 /* drop packet if it has not necessary minimum size */
539 if (unlikely(!pskb_may_pull(skb, hdr_size)))
f86ce0ad 540 return -ENODATA;
ff51fd70 541
7ed4be95 542 ethhdr = eth_hdr(skb);
ff51fd70
MH
543
544 /* packet with unicast indication but broadcast recipient */
545 if (is_broadcast_ether_addr(ethhdr->h_dest))
f86ce0ad 546 return -EBADR;
ff51fd70
MH
547
548 /* packet with broadcast sender address */
549 if (is_broadcast_ether_addr(ethhdr->h_source))
f86ce0ad 550 return -EBADR;
ff51fd70
MH
551
552 /* not for me */
fe8a93b9 553 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
f86ce0ad 554 return -EREMOTE;
ff51fd70
MH
555
556 return 0;
557}
558
c6c8fea2 559/* find a suitable router for this originator, and use
a4c135c5 560 * bonding if possible. increases the found neighbors
9cfc7bd6
SE
561 * refcount.
562 */
56303d34
SE
563struct batadv_neigh_node *
564batadv_find_router(struct batadv_priv *bat_priv,
565 struct batadv_orig_node *orig_node,
566 const struct batadv_hard_iface *recv_if)
c6c8fea2 567{
56303d34
SE
568 struct batadv_orig_node *primary_orig_node;
569 struct batadv_orig_node *router_orig;
570 struct batadv_neigh_node *router;
c6c8fea2
SE
571 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
572 int bonding_enabled;
da641193 573 uint8_t *primary_addr;
c6c8fea2
SE
574
575 if (!orig_node)
576 return NULL;
577
7d211efc 578 router = batadv_orig_node_get_router(orig_node);
e1a5382f 579 if (!router)
01df2b65 580 goto err;
c6c8fea2
SE
581
582 /* without bonding, the first node should
9cfc7bd6
SE
583 * always choose the default router.
584 */
c6c8fea2
SE
585 bonding_enabled = atomic_read(&bat_priv->bonding);
586
a4c135c5
SW
587 rcu_read_lock();
588 /* select default router to output */
e1a5382f 589 router_orig = router->orig_node;
01df2b65
ML
590 if (!router_orig)
591 goto err_unlock;
a4c135c5 592
a4c135c5
SW
593 if ((!recv_if) && (!bonding_enabled))
594 goto return_router;
c6c8fea2 595
da641193
SE
596 primary_addr = router_orig->primary_addr;
597
c6c8fea2 598 /* if we have something in the primary_addr, we can search
9cfc7bd6
SE
599 * for a potential bonding candidate.
600 */
1eda58bf 601 if (batadv_compare_eth(primary_addr, zero_mac))
a4c135c5 602 goto return_router;
c6c8fea2
SE
603
604 /* find the orig_node which has the primary interface. might
9cfc7bd6
SE
605 * even be the same as our router_orig in many cases
606 */
1eda58bf 607 if (batadv_compare_eth(primary_addr, router_orig->orig)) {
c6c8fea2
SE
608 primary_orig_node = router_orig;
609 } else {
da641193
SE
610 primary_orig_node = batadv_orig_hash_find(bat_priv,
611 primary_addr);
c6c8fea2 612 if (!primary_orig_node)
a4c135c5 613 goto return_router;
7aadf889 614
7d211efc 615 batadv_orig_node_free_ref(primary_orig_node);
c6c8fea2
SE
616 }
617
618 /* with less than 2 candidates, we can't do any
9cfc7bd6
SE
619 * bonding and prefer the original router.
620 */
a4c135c5
SW
621 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
622 goto return_router;
c6c8fea2 623
c6c8fea2
SE
624 /* all nodes between should choose a candidate which
625 * is is not on the interface where the packet came
9cfc7bd6
SE
626 * in.
627 */
7d211efc 628 batadv_neigh_node_free_ref(router);
c6c8fea2 629
55158629 630 if (bonding_enabled)
63b01037 631 router = batadv_find_bond_router(primary_orig_node, recv_if);
55158629 632 else
63b01037 633 router = batadv_find_ifalter_router(primary_orig_node, recv_if);
c6c8fea2 634
a4c135c5 635return_router:
e9a4f295 636 if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
e2cbc11c
AQ
637 goto err_unlock;
638
a4c135c5 639 rcu_read_unlock();
c6c8fea2 640 return router;
01df2b65
ML
641err_unlock:
642 rcu_read_unlock();
643err:
644 if (router)
7d211efc 645 batadv_neigh_node_free_ref(router);
01df2b65 646 return NULL;
c6c8fea2
SE
647}
648
63b01037 649static int batadv_route_unicast_packet(struct sk_buff *skb,
56303d34 650 struct batadv_hard_iface *recv_if)
c6c8fea2 651{
56303d34
SE
652 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
653 struct batadv_orig_node *orig_node = NULL;
654 struct batadv_neigh_node *neigh_node = NULL;
96412690 655 struct batadv_unicast_packet *unicast_packet;
7ed4be95 656 struct ethhdr *ethhdr = eth_hdr(skb);
c54f38c9 657 int res, hdr_len, ret = NET_RX_DROP;
c6c8fea2
SE
658 struct sk_buff *new_skb;
659
96412690 660 unicast_packet = (struct batadv_unicast_packet *)skb->data;
c6c8fea2
SE
661
662 /* TTL exceeded */
76543d14 663 if (unicast_packet->header.ttl < 2) {
86ceb360
SE
664 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
665 ethhdr->h_source, unicast_packet->dest);
44524fcd 666 goto out;
c6c8fea2
SE
667 }
668
669 /* get routing information */
da641193 670 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
7aadf889 671
44524fcd 672 if (!orig_node)
b5a6f69c 673 goto out;
c6c8fea2 674
a4c135c5 675 /* find_router() increases neigh_nodes refcount if found. */
30d3c511 676 neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
c6c8fea2 677
d0072609 678 if (!neigh_node)
44524fcd 679 goto out;
c6c8fea2
SE
680
681 /* create a copy of the skb, if needed, to modify it. */
0d125074 682 if (skb_cow(skb, ETH_HLEN) < 0)
44524fcd 683 goto out;
c6c8fea2 684
96412690 685 unicast_packet = (struct batadv_unicast_packet *)skb->data;
c6c8fea2 686
acd34afa 687 if (unicast_packet->header.packet_type == BATADV_UNICAST &&
c6c8fea2 688 atomic_read(&bat_priv->fragmentation) &&
d0072609 689 skb->len > neigh_node->if_incoming->net_dev->mtu) {
88ed1e77
SE
690 ret = batadv_frag_send_skb(skb, bat_priv,
691 neigh_node->if_incoming,
692 neigh_node->addr);
d0072609
ML
693 goto out;
694 }
c6c8fea2 695
acd34afa 696 if (unicast_packet->header.packet_type == BATADV_UNICAST_FRAG &&
f0530ee5
SE
697 batadv_frag_can_reassemble(skb,
698 neigh_node->if_incoming->net_dev->mtu)) {
88ed1e77 699 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
c6c8fea2
SE
700
701 if (ret == NET_RX_DROP)
44524fcd 702 goto out;
c6c8fea2
SE
703
704 /* packet was buffered for late merge */
44524fcd
ML
705 if (!new_skb) {
706 ret = NET_RX_SUCCESS;
707 goto out;
708 }
c6c8fea2
SE
709
710 skb = new_skb;
96412690 711 unicast_packet = (struct batadv_unicast_packet *)skb->data;
c6c8fea2
SE
712 }
713
714 /* decrement ttl */
76543d14 715 unicast_packet->header.ttl--;
c6c8fea2 716
c54f38c9
SW
717 switch (unicast_packet->header.packet_type) {
718 case BATADV_UNICAST_4ADDR:
719 hdr_len = sizeof(struct batadv_unicast_4addr_packet);
720 break;
721 case BATADV_UNICAST:
722 hdr_len = sizeof(struct batadv_unicast_packet);
723 break;
724 default:
725 /* other packet types not supported - yet */
726 hdr_len = -1;
727 break;
728 }
729
730 if (hdr_len > 0)
731 batadv_skb_set_priority(skb, hdr_len);
732
e91ecfc6 733 res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
95332477 734
e91ecfc6
MH
735 /* translate transmit result into receive result */
736 if (res == NET_XMIT_SUCCESS) {
737 /* skb was transmitted and consumed */
95332477
MH
738 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
739 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
740 skb->len + ETH_HLEN);
e91ecfc6
MH
741
742 ret = NET_RX_SUCCESS;
743 } else if (res == NET_XMIT_POLICED) {
744 /* skb was buffered and consumed */
745 ret = NET_RX_SUCCESS;
95332477 746 }
c6c8fea2 747
44524fcd
ML
748out:
749 if (neigh_node)
7d211efc 750 batadv_neigh_node_free_ref(neigh_node);
44524fcd 751 if (orig_node)
7d211efc 752 batadv_orig_node_free_ref(orig_node);
44524fcd 753 return ret;
c6c8fea2
SE
754}
755
7c1fd91d
AQ
756/**
757 * batadv_reroute_unicast_packet - update the unicast header for re-routing
758 * @bat_priv: the bat priv with all the soft interface information
759 * @unicast_packet: the unicast header to be updated
760 * @dst_addr: the payload destination
761 *
762 * Search the translation table for dst_addr and update the unicast header with
763 * the new corresponding information (originator address where the destination
764 * client currently is and its known TTVN)
765 *
766 * Returns true if the packet header has been updated, false otherwise
767 */
768static bool
769batadv_reroute_unicast_packet(struct batadv_priv *bat_priv,
770 struct batadv_unicast_packet *unicast_packet,
771 uint8_t *dst_addr)
772{
773 struct batadv_orig_node *orig_node = NULL;
774 struct batadv_hard_iface *primary_if = NULL;
775 bool ret = false;
776 uint8_t *orig_addr, orig_ttvn;
777
778 if (batadv_is_my_client(bat_priv, dst_addr)) {
779 primary_if = batadv_primary_if_get_selected(bat_priv);
780 if (!primary_if)
781 goto out;
782 orig_addr = primary_if->net_dev->dev_addr;
783 orig_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
784 } else {
785 orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr);
786 if (!orig_node)
787 goto out;
788
789 if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
790 goto out;
791
792 orig_addr = orig_node->orig;
793 orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
794 }
795
796 /* update the packet header */
797 memcpy(unicast_packet->dest, orig_addr, ETH_ALEN);
798 unicast_packet->ttvn = orig_ttvn;
799
800 ret = true;
801out:
802 if (primary_if)
803 batadv_hardif_free_ref(primary_if);
804 if (orig_node)
805 batadv_orig_node_free_ref(orig_node);
806
807 return ret;
808}
809
56303d34 810static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
dd981ab0 811 struct sk_buff *skb, int hdr_len) {
7c1fd91d 812 uint8_t curr_ttvn, old_ttvn;
56303d34 813 struct batadv_orig_node *orig_node;
a73105b8 814 struct ethhdr *ethhdr;
56303d34 815 struct batadv_hard_iface *primary_if;
96412690 816 struct batadv_unicast_packet *unicast_packet;
3e34819e 817 int is_old_ttvn;
a73105b8 818
b8fcfa42 819 /* check if there is enough data before accessing it */
dd981ab0 820 if (pskb_may_pull(skb, hdr_len + ETH_HLEN) < 0)
b8fcfa42
AQ
821 return 0;
822
823 /* create a copy of the skb (in case of for re-routing) to modify it. */
824 if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
a73105b8
AQ
825 return 0;
826
96412690 827 unicast_packet = (struct batadv_unicast_packet *)skb->data;
dd981ab0 828 ethhdr = (struct ethhdr *)(skb->data + hdr_len);
a73105b8 829
7c1fd91d
AQ
830 /* check if the destination client was served by this node and it is now
831 * roaming. In this case, it means that the node has got a ROAM_ADV
832 * message and that it knows the new destination in the mesh to re-route
833 * the packet to
834 */
835 if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest)) {
836 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
837 ethhdr->h_dest))
838 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT,
839 bat_priv,
840 "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
841 unicast_packet->dest,
842 ethhdr->h_dest);
843 /* at this point the mesh destination should have been
844 * substituted with the originator address found in the global
845 * table. If not, let the packet go untouched anyway because
846 * there is nothing the node can do
847 */
848 return 1;
849 }
850
851 /* retrieve the TTVN known by this node for the packet destination. This
852 * value is used later to check if the node which sent (or re-routed
853 * last time) the packet had an updated information or not
854 */
855 curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
fe8a93b9 856 if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
da641193
SE
857 orig_node = batadv_orig_hash_find(bat_priv,
858 unicast_packet->dest);
7c1fd91d
AQ
859 /* if it is not possible to find the orig_node representing the
860 * destination, the packet can immediately be dropped as it will
861 * not be possible to deliver it
862 */
a73105b8
AQ
863 if (!orig_node)
864 return 0;
865
866 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
7d211efc 867 batadv_orig_node_free_ref(orig_node);
a73105b8
AQ
868 }
869
7c1fd91d
AQ
870 /* check if the TTVN contained in the packet is fresher than what the
871 * node knows
872 */
3e34819e 873 is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
7c1fd91d
AQ
874 if (!is_old_ttvn)
875 return 1;
a73105b8 876
7c1fd91d
AQ
877 old_ttvn = unicast_packet->ttvn;
878 /* the packet was forged based on outdated network information. Its
879 * destination can possibly be updated and forwarded towards the new
880 * target host
881 */
882 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
883 ethhdr->h_dest)) {
884 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv,
885 "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
886 unicast_packet->dest, ethhdr->h_dest,
887 old_ttvn, curr_ttvn);
888 return 1;
889 }
3275e7cc 890
7c1fd91d
AQ
891 /* the packet has not been re-routed: either the destination is
892 * currently served by this node or there is no destination at all and
893 * it is possible to drop the packet
894 */
895 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
896 return 0;
3275e7cc 897
7c1fd91d
AQ
898 /* update the header in order to let the packet be delivered to this
899 * node's soft interface
900 */
901 primary_if = batadv_primary_if_get_selected(bat_priv);
902 if (!primary_if)
903 return 0;
a73105b8 904
7c1fd91d
AQ
905 memcpy(unicast_packet->dest, primary_if->net_dev->dev_addr, ETH_ALEN);
906
907 batadv_hardif_free_ref(primary_if);
908
909 unicast_packet->ttvn = curr_ttvn;
a73105b8 910
a73105b8
AQ
911 return 1;
912}
913
56303d34
SE
914int batadv_recv_unicast_packet(struct sk_buff *skb,
915 struct batadv_hard_iface *recv_if)
c6c8fea2 916{
56303d34 917 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
96412690 918 struct batadv_unicast_packet *unicast_packet;
4046b24a 919 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
9affec6b
AQ
920 uint8_t *orig_addr;
921 struct batadv_orig_node *orig_node = NULL;
612d2b4f 922 int check, hdr_size = sizeof(*unicast_packet);
c384ea3e 923 bool is4addr;
c6c8fea2 924
7cdcf6dd 925 unicast_packet = (struct batadv_unicast_packet *)skb->data;
4046b24a 926 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
7cdcf6dd 927
c384ea3e 928 is4addr = unicast_packet->header.packet_type == BATADV_UNICAST_4ADDR;
7cdcf6dd 929 /* the caller function should have already pulled 2 bytes */
c384ea3e 930 if (is4addr)
4046b24a 931 hdr_size = sizeof(*unicast_4addr_packet);
7cdcf6dd 932
612d2b4f 933 /* function returns -EREMOTE for promiscuous packets */
6e0895c2 934 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
612d2b4f
MH
935
936 /* Even though the packet is not for us, we might save it to use for
937 * decoding a later received coded packet
938 */
939 if (check == -EREMOTE)
940 batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
941
942 if (check < 0)
c6c8fea2 943 return NET_RX_DROP;
dd981ab0 944 if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
a73105b8
AQ
945 return NET_RX_DROP;
946
c6c8fea2 947 /* packet for me */
fe8a93b9 948 if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
9affec6b 949 if (is4addr) {
4046b24a
MH
950 batadv_dat_inc_counter(bat_priv,
951 unicast_4addr_packet->subtype);
9affec6b
AQ
952 orig_addr = unicast_4addr_packet->src;
953 orig_node = batadv_orig_hash_find(bat_priv, orig_addr);
954 }
4046b24a 955
c384ea3e
AQ
956 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
957 hdr_size))
958 goto rx_success;
959 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
960 hdr_size))
961 goto rx_success;
962
37135173 963 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
9affec6b 964 orig_node);
37135173 965
c384ea3e 966rx_success:
9affec6b
AQ
967 if (orig_node)
968 batadv_orig_node_free_ref(orig_node);
969
c6c8fea2
SE
970 return NET_RX_SUCCESS;
971 }
972
63b01037 973 return batadv_route_unicast_packet(skb, recv_if);
c6c8fea2
SE
974}
975
30d3c511 976int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
56303d34 977 struct batadv_hard_iface *recv_if)
c6c8fea2 978{
56303d34 979 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
96412690 980 struct batadv_unicast_frag_packet *unicast_packet;
704509b8 981 int hdr_size = sizeof(*unicast_packet);
c6c8fea2
SE
982 struct sk_buff *new_skb = NULL;
983 int ret;
984
fe8a93b9 985 if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
c6c8fea2
SE
986 return NET_RX_DROP;
987
dd981ab0 988 if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
a73105b8
AQ
989 return NET_RX_DROP;
990
96412690 991 unicast_packet = (struct batadv_unicast_frag_packet *)skb->data;
c6c8fea2
SE
992
993 /* packet for me */
fe8a93b9 994 if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
88ed1e77 995 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
c6c8fea2
SE
996
997 if (ret == NET_RX_DROP)
998 return NET_RX_DROP;
999
1000 /* packet was buffered for late merge */
1001 if (!new_skb)
1002 return NET_RX_SUCCESS;
1003
c384ea3e
AQ
1004 if (batadv_dat_snoop_incoming_arp_request(bat_priv, new_skb,
1005 hdr_size))
1006 goto rx_success;
1007 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, new_skb,
1008 hdr_size))
1009 goto rx_success;
1010
04b482a2 1011 batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
37135173 1012 sizeof(struct batadv_unicast_packet), NULL);
c384ea3e
AQ
1013
1014rx_success:
c6c8fea2
SE
1015 return NET_RX_SUCCESS;
1016 }
1017
63b01037 1018 return batadv_route_unicast_packet(skb, recv_if);
c6c8fea2
SE
1019}
1020
ef261577
ML
1021/**
1022 * batadv_recv_unicast_tvlv - receive and process unicast tvlv packets
1023 * @skb: unicast tvlv packet to process
1024 * @recv_if: pointer to interface this packet was received on
1025 * @dst_addr: the payload destination
1026 *
1027 * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
1028 * otherwise.
1029 */
1030int batadv_recv_unicast_tvlv(struct sk_buff *skb,
1031 struct batadv_hard_iface *recv_if)
1032{
1033 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1034 struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
1035 unsigned char *tvlv_buff;
1036 uint16_t tvlv_buff_len;
1037 int hdr_size = sizeof(*unicast_tvlv_packet);
1038 int ret = NET_RX_DROP;
1039
1040 if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
1041 return NET_RX_DROP;
1042
1043 /* the header is likely to be modified while forwarding */
1044 if (skb_cow(skb, hdr_size) < 0)
1045 return NET_RX_DROP;
1046
1047 /* packet needs to be linearized to access the tvlv content */
1048 if (skb_linearize(skb) < 0)
1049 return NET_RX_DROP;
1050
1051 unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data;
1052
1053 tvlv_buff = (unsigned char *)(skb->data + hdr_size);
1054 tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len);
1055
1056 if (tvlv_buff_len > skb->len - hdr_size)
1057 return NET_RX_DROP;
1058
1059 ret = batadv_tvlv_containers_process(bat_priv, false, NULL,
1060 unicast_tvlv_packet->src,
1061 unicast_tvlv_packet->dst,
1062 tvlv_buff, tvlv_buff_len);
1063
1064 if (ret != NET_RX_SUCCESS)
1065 ret = batadv_route_unicast_packet(skb, recv_if);
1066
1067 return ret;
1068}
c6c8fea2 1069
56303d34
SE
1070int batadv_recv_bcast_packet(struct sk_buff *skb,
1071 struct batadv_hard_iface *recv_if)
c6c8fea2 1072{
56303d34
SE
1073 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1074 struct batadv_orig_node *orig_node = NULL;
96412690 1075 struct batadv_bcast_packet *bcast_packet;
c6c8fea2 1076 struct ethhdr *ethhdr;
704509b8 1077 int hdr_size = sizeof(*bcast_packet);
f3e0008f 1078 int ret = NET_RX_DROP;
c6c8fea2
SE
1079 int32_t seq_diff;
1080
1081 /* drop packet if it has not necessary minimum size */
1082 if (unlikely(!pskb_may_pull(skb, hdr_size)))
f3e0008f 1083 goto out;
c6c8fea2 1084
7ed4be95 1085 ethhdr = eth_hdr(skb);
c6c8fea2
SE
1086
1087 /* packet with broadcast indication but unicast recipient */
1088 if (!is_broadcast_ether_addr(ethhdr->h_dest))
f3e0008f 1089 goto out;
c6c8fea2
SE
1090
1091 /* packet with broadcast sender address */
1092 if (is_broadcast_ether_addr(ethhdr->h_source))
f3e0008f 1093 goto out;
c6c8fea2
SE
1094
1095 /* ignore broadcasts sent by myself */
fe8a93b9 1096 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
f3e0008f 1097 goto out;
c6c8fea2 1098
96412690 1099 bcast_packet = (struct batadv_bcast_packet *)skb->data;
c6c8fea2
SE
1100
1101 /* ignore broadcasts originated by myself */
fe8a93b9 1102 if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
f3e0008f 1103 goto out;
c6c8fea2 1104
76543d14 1105 if (bcast_packet->header.ttl < 2)
f3e0008f 1106 goto out;
c6c8fea2 1107
da641193 1108 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
f3e0008f
ML
1109
1110 if (!orig_node)
b5a6f69c 1111 goto out;
c6c8fea2 1112
f3e0008f 1113 spin_lock_bh(&orig_node->bcast_seqno_lock);
c6c8fea2
SE
1114
1115 /* check whether the packet is a duplicate */
9b4a1159
SE
1116 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1117 ntohl(bcast_packet->seqno)))
f3e0008f 1118 goto spin_unlock;
c6c8fea2
SE
1119
1120 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1121
1122 /* check whether the packet is old and the host just restarted. */
30d3c511
SE
1123 if (batadv_window_protected(bat_priv, seq_diff,
1124 &orig_node->bcast_seqno_reset))
f3e0008f 1125 goto spin_unlock;
c6c8fea2
SE
1126
1127 /* mark broadcast in flood history, update window position
9cfc7bd6
SE
1128 * if required.
1129 */
0f5f9322 1130 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
c6c8fea2
SE
1131 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1132
f3e0008f 1133 spin_unlock_bh(&orig_node->bcast_seqno_lock);
f3e0008f 1134
fe2da6ff 1135 /* check whether this has been sent by another originator before */
004e86fc 1136 if (batadv_bla_check_bcast_duplist(bat_priv, skb))
fe2da6ff
SW
1137 goto out;
1138
c54f38c9
SW
1139 batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
1140
c6c8fea2 1141 /* rebroadcast packet */
9455e34c 1142 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
c6c8fea2 1143
23721387
SW
1144 /* don't hand the broadcast up if it is from an originator
1145 * from the same backbone.
1146 */
08adf151 1147 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
23721387
SW
1148 goto out;
1149
c384ea3e
AQ
1150 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
1151 goto rx_success;
1152 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
1153 goto rx_success;
1154
c6c8fea2 1155 /* broadcast for me */
37135173
AQ
1156 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
1157 orig_node);
c384ea3e
AQ
1158
1159rx_success:
f3e0008f
ML
1160 ret = NET_RX_SUCCESS;
1161 goto out;
c6c8fea2 1162
f3e0008f
ML
1163spin_unlock:
1164 spin_unlock_bh(&orig_node->bcast_seqno_lock);
f3e0008f
ML
1165out:
1166 if (orig_node)
7d211efc 1167 batadv_orig_node_free_ref(orig_node);
f3e0008f 1168 return ret;
c6c8fea2 1169}