batman-adv: Prefix originator 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
a7f6ee94
SW
32static int route_unicast_packet(struct sk_buff *skb,
33 struct hard_iface *recv_if);
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
fc957275
ML
64static void _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)
fc957275 120 _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
279static int recv_my_icmp_packet(struct bat_priv *bat_priv,
280 struct sk_buff *skb, size_t icmp_len)
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
334static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
74ef1153 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))
c6c8fea2
SE
434 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
435
436 /* TTL exceeded */
76543d14 437 if (icmp_packet->header.ttl < 2)
74ef1153 438 return 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 */
55158629 476static struct neigh_node *find_bond_router(struct orig_node *primary_orig,
747e4221 477 const struct hard_iface *recv_if)
55158629
LL
478{
479 struct neigh_node *tmp_neigh_node;
480 struct neigh_node *router = NULL, *first_candidate = NULL;
481
482 rcu_read_lock();
483 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
484 bonding_list) {
485 if (!first_candidate)
486 first_candidate = tmp_neigh_node;
487
488 /* recv_if == NULL on the first node. */
489 if (tmp_neigh_node->if_incoming == recv_if)
490 continue;
491
492 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
493 continue;
494
495 router = tmp_neigh_node;
496 break;
497 }
498
499 /* use the first candidate if nothing was found. */
500 if (!router && first_candidate &&
501 atomic_inc_not_zero(&first_candidate->refcount))
502 router = first_candidate;
503
504 if (!router)
505 goto out;
506
507 /* selected should point to the next element
9cfc7bd6
SE
508 * after the current router
509 */
55158629
LL
510 spin_lock_bh(&primary_orig->neigh_list_lock);
511 /* this is a list_move(), which unfortunately
9cfc7bd6
SE
512 * does not exist as rcu version
513 */
55158629
LL
514 list_del_rcu(&primary_orig->bond_list);
515 list_add_rcu(&primary_orig->bond_list,
516 &router->bonding_list);
517 spin_unlock_bh(&primary_orig->neigh_list_lock);
518
519out:
520 rcu_read_unlock();
521 return router;
522}
523
524/* Interface Alternating: Use the best of the
525 * remaining candidates which are not using
526 * this interface.
527 *
9cfc7bd6
SE
528 * Increases the returned router's refcount
529 */
55158629 530static struct neigh_node *find_ifalter_router(struct orig_node *primary_orig,
747e4221 531 const struct hard_iface *recv_if)
55158629
LL
532{
533 struct neigh_node *tmp_neigh_node;
534 struct neigh_node *router = NULL, *first_candidate = NULL;
535
536 rcu_read_lock();
537 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
538 bonding_list) {
539 if (!first_candidate)
540 first_candidate = tmp_neigh_node;
541
542 /* recv_if == NULL on the first node. */
543 if (tmp_neigh_node->if_incoming == recv_if)
544 continue;
545
546 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
547 continue;
548
549 /* if we don't have a router yet
9cfc7bd6
SE
550 * or this one is better, choose it.
551 */
55158629
LL
552 if ((!router) ||
553 (tmp_neigh_node->tq_avg > router->tq_avg)) {
554 /* decrement refcount of
9cfc7bd6
SE
555 * previously selected router
556 */
55158629 557 if (router)
7d211efc 558 batadv_neigh_node_free_ref(router);
55158629
LL
559
560 router = tmp_neigh_node;
561 atomic_inc_not_zero(&router->refcount);
562 }
563
7d211efc 564 batadv_neigh_node_free_ref(tmp_neigh_node);
55158629
LL
565 }
566
567 /* use the first candidate if nothing was found. */
568 if (!router && first_candidate &&
569 atomic_inc_not_zero(&first_candidate->refcount))
570 router = first_candidate;
571
572 rcu_read_unlock();
573 return router;
574}
575
30d3c511 576int batadv_recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if)
a73105b8
AQ
577{
578 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
579 struct tt_query_packet *tt_query;
f25bd58a 580 uint16_t tt_size;
a73105b8 581 struct ethhdr *ethhdr;
1eda58bf 582 char tt_flag;
a73105b8
AQ
583
584 /* drop packet if it has not necessary minimum size */
585 if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet))))
586 goto out;
587
588 /* I could need to modify it */
589 if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0)
590 goto out;
591
592 ethhdr = (struct ethhdr *)skb_mac_header(skb);
593
594 /* packet with unicast indication but broadcast recipient */
595 if (is_broadcast_ether_addr(ethhdr->h_dest))
596 goto out;
597
598 /* packet with broadcast sender address */
599 if (is_broadcast_ether_addr(ethhdr->h_source))
600 goto out;
601
602 tt_query = (struct tt_query_packet *)skb->data;
603
a73105b8
AQ
604 switch (tt_query->flags & TT_QUERY_TYPE_MASK) {
605 case TT_REQUEST:
f8214865
MH
606 batadv_inc_counter(bat_priv, BAT_CNT_TT_REQUEST_RX);
607
a73105b8 608 /* If we cannot provide an answer the tt_request is
9cfc7bd6
SE
609 * forwarded
610 */
08c36d3e 611 if (!batadv_send_tt_response(bat_priv, tt_query)) {
1eda58bf
SE
612 tt_flag = tt_query->flags & TT_FULL_TABLE ? 'F' : '.';
613 batadv_dbg(DBG_TT, bat_priv,
614 "Routing TT_REQUEST to %pM [%c]\n",
615 tt_query->dst,
616 tt_flag);
a73105b8
AQ
617 return route_unicast_packet(skb, recv_if);
618 }
619 break;
620 case TT_RESPONSE:
f8214865
MH
621 batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_RX);
622
3193e8fd 623 if (batadv_is_my_mac(tt_query->dst)) {
dc58fe32 624 /* packet needs to be linearized to access the TT
9cfc7bd6
SE
625 * changes
626 */
dc58fe32
AQ
627 if (skb_linearize(skb) < 0)
628 goto out;
d2b6cc8e
AQ
629 /* skb_linearize() possibly changed skb->data */
630 tt_query = (struct tt_query_packet *)skb->data;
a73105b8 631
08c36d3e 632 tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
8b7342d6
AQ
633
634 /* Ensure we have all the claimed data */
635 if (unlikely(skb_headlen(skb) <
f25bd58a 636 sizeof(struct tt_query_packet) + tt_size))
8b7342d6
AQ
637 goto out;
638
08c36d3e 639 batadv_handle_tt_response(bat_priv, tt_query);
dc58fe32 640 } else {
1eda58bf
SE
641 tt_flag = tt_query->flags & TT_FULL_TABLE ? 'F' : '.';
642 batadv_dbg(DBG_TT, bat_priv,
643 "Routing TT_RESPONSE to %pM [%c]\n",
644 tt_query->dst,
645 tt_flag);
a73105b8
AQ
646 return route_unicast_packet(skb, recv_if);
647 }
648 break;
649 }
650
651out:
652 /* returning NET_RX_DROP will make the caller function kfree the skb */
653 return NET_RX_DROP;
654}
655
30d3c511 656int batadv_recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
cc47f66e
AQ
657{
658 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
659 struct roam_adv_packet *roam_adv_packet;
660 struct orig_node *orig_node;
661 struct ethhdr *ethhdr;
662
663 /* drop packet if it has not necessary minimum size */
664 if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
665 goto out;
666
667 ethhdr = (struct ethhdr *)skb_mac_header(skb);
668
669 /* packet with unicast indication but broadcast recipient */
670 if (is_broadcast_ether_addr(ethhdr->h_dest))
671 goto out;
672
673 /* packet with broadcast sender address */
674 if (is_broadcast_ether_addr(ethhdr->h_source))
675 goto out;
676
f8214865
MH
677 batadv_inc_counter(bat_priv, BAT_CNT_TT_ROAM_ADV_RX);
678
cc47f66e
AQ
679 roam_adv_packet = (struct roam_adv_packet *)skb->data;
680
3193e8fd 681 if (!batadv_is_my_mac(roam_adv_packet->dst))
cc47f66e
AQ
682 return route_unicast_packet(skb, recv_if);
683
20ff9d59
SW
684 /* check if it is a backbone gateway. we don't accept
685 * roaming advertisement from it, as it has the same
686 * entries as we have.
687 */
08adf151 688 if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
20ff9d59
SW
689 goto out;
690
da641193 691 orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
cc47f66e
AQ
692 if (!orig_node)
693 goto out;
694
1eda58bf
SE
695 batadv_dbg(DBG_TT, bat_priv,
696 "Received ROAMING_ADV from %pM (client %pM)\n",
697 roam_adv_packet->src, roam_adv_packet->client);
cc47f66e 698
08c36d3e
SE
699 batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
700 atomic_read(&orig_node->last_ttvn) + 1, true,
701 false);
cc47f66e
AQ
702
703 /* Roaming phase starts: I have new information but the ttvn has not
704 * been incremented yet. This flag will make me check all the incoming
9cfc7bd6
SE
705 * packets for the correct destination.
706 */
cc47f66e
AQ
707 bat_priv->tt_poss_change = true;
708
7d211efc 709 batadv_orig_node_free_ref(orig_node);
cc47f66e
AQ
710out:
711 /* returning NET_RX_DROP will make the caller function kfree the skb */
712 return NET_RX_DROP;
713}
714
c6c8fea2 715/* find a suitable router for this originator, and use
a4c135c5 716 * bonding if possible. increases the found neighbors
9cfc7bd6
SE
717 * refcount.
718 */
30d3c511
SE
719struct neigh_node *batadv_find_router(struct bat_priv *bat_priv,
720 struct orig_node *orig_node,
721 const struct hard_iface *recv_if)
c6c8fea2
SE
722{
723 struct orig_node *primary_orig_node;
724 struct orig_node *router_orig;
55158629 725 struct neigh_node *router;
c6c8fea2
SE
726 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
727 int bonding_enabled;
da641193 728 uint8_t *primary_addr;
c6c8fea2
SE
729
730 if (!orig_node)
731 return NULL;
732
7d211efc 733 router = batadv_orig_node_get_router(orig_node);
e1a5382f 734 if (!router)
01df2b65 735 goto err;
c6c8fea2
SE
736
737 /* without bonding, the first node should
9cfc7bd6
SE
738 * always choose the default router.
739 */
c6c8fea2
SE
740 bonding_enabled = atomic_read(&bat_priv->bonding);
741
a4c135c5
SW
742 rcu_read_lock();
743 /* select default router to output */
e1a5382f 744 router_orig = router->orig_node;
01df2b65
ML
745 if (!router_orig)
746 goto err_unlock;
a4c135c5 747
a4c135c5
SW
748 if ((!recv_if) && (!bonding_enabled))
749 goto return_router;
c6c8fea2 750
da641193
SE
751 primary_addr = router_orig->primary_addr;
752
c6c8fea2 753 /* if we have something in the primary_addr, we can search
9cfc7bd6
SE
754 * for a potential bonding candidate.
755 */
1eda58bf 756 if (batadv_compare_eth(primary_addr, zero_mac))
a4c135c5 757 goto return_router;
c6c8fea2
SE
758
759 /* find the orig_node which has the primary interface. might
9cfc7bd6
SE
760 * even be the same as our router_orig in many cases
761 */
1eda58bf 762 if (batadv_compare_eth(primary_addr, router_orig->orig)) {
c6c8fea2
SE
763 primary_orig_node = router_orig;
764 } else {
da641193
SE
765 primary_orig_node = batadv_orig_hash_find(bat_priv,
766 primary_addr);
c6c8fea2 767 if (!primary_orig_node)
a4c135c5 768 goto return_router;
7aadf889 769
7d211efc 770 batadv_orig_node_free_ref(primary_orig_node);
c6c8fea2
SE
771 }
772
773 /* with less than 2 candidates, we can't do any
9cfc7bd6
SE
774 * bonding and prefer the original router.
775 */
a4c135c5
SW
776 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
777 goto return_router;
c6c8fea2 778
c6c8fea2
SE
779 /* all nodes between should choose a candidate which
780 * is is not on the interface where the packet came
9cfc7bd6
SE
781 * in.
782 */
7d211efc 783 batadv_neigh_node_free_ref(router);
c6c8fea2 784
55158629
LL
785 if (bonding_enabled)
786 router = find_bond_router(primary_orig_node, recv_if);
787 else
788 router = find_ifalter_router(primary_orig_node, recv_if);
c6c8fea2 789
a4c135c5 790return_router:
e2cbc11c
AQ
791 if (router && router->if_incoming->if_status != IF_ACTIVE)
792 goto err_unlock;
793
a4c135c5 794 rcu_read_unlock();
c6c8fea2 795 return router;
01df2b65
ML
796err_unlock:
797 rcu_read_unlock();
798err:
799 if (router)
7d211efc 800 batadv_neigh_node_free_ref(router);
01df2b65 801 return NULL;
c6c8fea2
SE
802}
803
804static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
805{
806 struct ethhdr *ethhdr;
807
808 /* drop packet if it has not necessary minimum size */
809 if (unlikely(!pskb_may_pull(skb, hdr_size)))
810 return -1;
811
812 ethhdr = (struct ethhdr *)skb_mac_header(skb);
813
814 /* packet with unicast indication but broadcast recipient */
815 if (is_broadcast_ether_addr(ethhdr->h_dest))
816 return -1;
817
818 /* packet with broadcast sender address */
819 if (is_broadcast_ether_addr(ethhdr->h_source))
820 return -1;
821
822 /* not for me */
3193e8fd 823 if (!batadv_is_my_mac(ethhdr->h_dest))
c6c8fea2
SE
824 return -1;
825
826 return 0;
827}
828
a7f6ee94 829static int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
c6c8fea2
SE
830{
831 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
44524fcd
ML
832 struct orig_node *orig_node = NULL;
833 struct neigh_node *neigh_node = NULL;
c6c8fea2
SE
834 struct unicast_packet *unicast_packet;
835 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
44524fcd 836 int ret = NET_RX_DROP;
c6c8fea2
SE
837 struct sk_buff *new_skb;
838
839 unicast_packet = (struct unicast_packet *)skb->data;
840
841 /* TTL exceeded */
76543d14 842 if (unicast_packet->header.ttl < 2) {
86ceb360
SE
843 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
844 ethhdr->h_source, unicast_packet->dest);
44524fcd 845 goto out;
c6c8fea2
SE
846 }
847
848 /* get routing information */
da641193 849 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
7aadf889 850
44524fcd 851 if (!orig_node)
b5a6f69c 852 goto out;
c6c8fea2 853
a4c135c5 854 /* find_router() increases neigh_nodes refcount if found. */
30d3c511 855 neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
c6c8fea2 856
d0072609 857 if (!neigh_node)
44524fcd 858 goto out;
c6c8fea2
SE
859
860 /* create a copy of the skb, if needed, to modify it. */
0d125074 861 if (skb_cow(skb, ETH_HLEN) < 0)
44524fcd 862 goto out;
c6c8fea2
SE
863
864 unicast_packet = (struct unicast_packet *)skb->data;
865
76543d14 866 if (unicast_packet->header.packet_type == BAT_UNICAST &&
c6c8fea2 867 atomic_read(&bat_priv->fragmentation) &&
d0072609 868 skb->len > neigh_node->if_incoming->net_dev->mtu) {
88ed1e77
SE
869 ret = batadv_frag_send_skb(skb, bat_priv,
870 neigh_node->if_incoming,
871 neigh_node->addr);
d0072609
ML
872 goto out;
873 }
c6c8fea2 874
76543d14 875 if (unicast_packet->header.packet_type == BAT_UNICAST_FRAG &&
f0530ee5
SE
876 batadv_frag_can_reassemble(skb,
877 neigh_node->if_incoming->net_dev->mtu)) {
c6c8fea2 878
88ed1e77 879 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
c6c8fea2
SE
880
881 if (ret == NET_RX_DROP)
44524fcd 882 goto out;
c6c8fea2
SE
883
884 /* packet was buffered for late merge */
44524fcd
ML
885 if (!new_skb) {
886 ret = NET_RX_SUCCESS;
887 goto out;
888 }
c6c8fea2
SE
889
890 skb = new_skb;
891 unicast_packet = (struct unicast_packet *)skb->data;
892 }
893
894 /* decrement ttl */
76543d14 895 unicast_packet->header.ttl--;
c6c8fea2 896
f8214865
MH
897 /* Update stats counter */
898 batadv_inc_counter(bat_priv, BAT_CNT_FORWARD);
899 batadv_add_counter(bat_priv, BAT_CNT_FORWARD_BYTES,
900 skb->len + ETH_HLEN);
901
c6c8fea2 902 /* route it */
9455e34c 903 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
44524fcd 904 ret = NET_RX_SUCCESS;
c6c8fea2 905
44524fcd
ML
906out:
907 if (neigh_node)
7d211efc 908 batadv_neigh_node_free_ref(neigh_node);
44524fcd 909 if (orig_node)
7d211efc 910 batadv_orig_node_free_ref(orig_node);
44524fcd 911 return ret;
c6c8fea2
SE
912}
913
a73105b8
AQ
914static int check_unicast_ttvn(struct bat_priv *bat_priv,
915 struct sk_buff *skb) {
916 uint8_t curr_ttvn;
917 struct orig_node *orig_node;
918 struct ethhdr *ethhdr;
919 struct hard_iface *primary_if;
920 struct unicast_packet *unicast_packet;
cc47f66e 921 bool tt_poss_change;
a73105b8
AQ
922
923 /* I could need to modify it */
924 if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
925 return 0;
926
927 unicast_packet = (struct unicast_packet *)skb->data;
928
3193e8fd 929 if (batadv_is_my_mac(unicast_packet->dest)) {
cc47f66e 930 tt_poss_change = bat_priv->tt_poss_change;
a73105b8 931 curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
cc47f66e 932 } else {
da641193
SE
933 orig_node = batadv_orig_hash_find(bat_priv,
934 unicast_packet->dest);
a73105b8
AQ
935
936 if (!orig_node)
937 return 0;
938
939 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
cc47f66e 940 tt_poss_change = orig_node->tt_poss_change;
7d211efc 941 batadv_orig_node_free_ref(orig_node);
a73105b8
AQ
942 }
943
944 /* Check whether I have to reroute the packet */
cc47f66e 945 if (seq_before(unicast_packet->ttvn, curr_ttvn) || tt_poss_change) {
8710e261
AQ
946 /* check if there is enough data before accessing it */
947 if (pskb_may_pull(skb, sizeof(struct unicast_packet) +
948 ETH_HLEN) < 0)
a73105b8
AQ
949 return 0;
950
951 ethhdr = (struct ethhdr *)(skb->data +
952 sizeof(struct unicast_packet));
3275e7cc
AQ
953
954 /* we don't have an updated route for this client, so we should
955 * not try to reroute the packet!!
956 */
08c36d3e
SE
957 if (batadv_tt_global_client_is_roaming(bat_priv,
958 ethhdr->h_dest))
3275e7cc
AQ
959 return 1;
960
08c36d3e
SE
961 orig_node = batadv_transtable_search(bat_priv, NULL,
962 ethhdr->h_dest);
a73105b8
AQ
963
964 if (!orig_node) {
08c36d3e 965 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
a73105b8 966 return 0;
e5d89254 967 primary_if = batadv_primary_if_get_selected(bat_priv);
a73105b8
AQ
968 if (!primary_if)
969 return 0;
970 memcpy(unicast_packet->dest,
971 primary_if->net_dev->dev_addr, ETH_ALEN);
e5d89254 972 batadv_hardif_free_ref(primary_if);
a73105b8
AQ
973 } else {
974 memcpy(unicast_packet->dest, orig_node->orig,
975 ETH_ALEN);
976 curr_ttvn = (uint8_t)
977 atomic_read(&orig_node->last_ttvn);
7d211efc 978 batadv_orig_node_free_ref(orig_node);
a73105b8
AQ
979 }
980
1eda58bf
SE
981 batadv_dbg(DBG_ROUTES, bat_priv,
982 "TTVN mismatch (old_ttvn %u new_ttvn %u)! Rerouting unicast packet (for %pM) to %pM\n",
983 unicast_packet->ttvn, curr_ttvn, ethhdr->h_dest,
984 unicast_packet->dest);
a73105b8
AQ
985
986 unicast_packet->ttvn = curr_ttvn;
987 }
988 return 1;
989}
990
30d3c511 991int batadv_recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
c6c8fea2 992{
a73105b8 993 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
c6c8fea2 994 struct unicast_packet *unicast_packet;
704509b8 995 int hdr_size = sizeof(*unicast_packet);
c6c8fea2
SE
996
997 if (check_unicast_packet(skb, hdr_size) < 0)
998 return NET_RX_DROP;
999
a73105b8
AQ
1000 if (!check_unicast_ttvn(bat_priv, skb))
1001 return NET_RX_DROP;
1002
c6c8fea2
SE
1003 unicast_packet = (struct unicast_packet *)skb->data;
1004
1005 /* packet for me */
3193e8fd 1006 if (batadv_is_my_mac(unicast_packet->dest)) {
04b482a2
SE
1007 batadv_interface_rx(recv_if->soft_iface, skb, recv_if,
1008 hdr_size);
c6c8fea2
SE
1009 return NET_RX_SUCCESS;
1010 }
1011
7cefb149 1012 return route_unicast_packet(skb, recv_if);
c6c8fea2
SE
1013}
1014
30d3c511
SE
1015int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
1016 struct hard_iface *recv_if)
c6c8fea2
SE
1017{
1018 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1019 struct unicast_frag_packet *unicast_packet;
704509b8 1020 int hdr_size = sizeof(*unicast_packet);
c6c8fea2
SE
1021 struct sk_buff *new_skb = NULL;
1022 int ret;
1023
1024 if (check_unicast_packet(skb, hdr_size) < 0)
1025 return NET_RX_DROP;
1026
a73105b8
AQ
1027 if (!check_unicast_ttvn(bat_priv, skb))
1028 return NET_RX_DROP;
1029
c6c8fea2
SE
1030 unicast_packet = (struct unicast_frag_packet *)skb->data;
1031
1032 /* packet for me */
3193e8fd 1033 if (batadv_is_my_mac(unicast_packet->dest)) {
c6c8fea2 1034
88ed1e77 1035 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
c6c8fea2
SE
1036
1037 if (ret == NET_RX_DROP)
1038 return NET_RX_DROP;
1039
1040 /* packet was buffered for late merge */
1041 if (!new_skb)
1042 return NET_RX_SUCCESS;
1043
04b482a2
SE
1044 batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
1045 sizeof(struct unicast_packet));
c6c8fea2
SE
1046 return NET_RX_SUCCESS;
1047 }
1048
7cefb149 1049 return route_unicast_packet(skb, recv_if);
c6c8fea2
SE
1050}
1051
1052
30d3c511 1053int batadv_recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
c6c8fea2
SE
1054{
1055 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
f3e0008f 1056 struct orig_node *orig_node = NULL;
c6c8fea2
SE
1057 struct bcast_packet *bcast_packet;
1058 struct ethhdr *ethhdr;
704509b8 1059 int hdr_size = sizeof(*bcast_packet);
f3e0008f 1060 int ret = NET_RX_DROP;
c6c8fea2
SE
1061 int32_t seq_diff;
1062
1063 /* drop packet if it has not necessary minimum size */
1064 if (unlikely(!pskb_may_pull(skb, hdr_size)))
f3e0008f 1065 goto out;
c6c8fea2
SE
1066
1067 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1068
1069 /* packet with broadcast indication but unicast recipient */
1070 if (!is_broadcast_ether_addr(ethhdr->h_dest))
f3e0008f 1071 goto out;
c6c8fea2
SE
1072
1073 /* packet with broadcast sender address */
1074 if (is_broadcast_ether_addr(ethhdr->h_source))
f3e0008f 1075 goto out;
c6c8fea2
SE
1076
1077 /* ignore broadcasts sent by myself */
3193e8fd 1078 if (batadv_is_my_mac(ethhdr->h_source))
f3e0008f 1079 goto out;
c6c8fea2
SE
1080
1081 bcast_packet = (struct bcast_packet *)skb->data;
1082
1083 /* ignore broadcasts originated by myself */
3193e8fd 1084 if (batadv_is_my_mac(bcast_packet->orig))
f3e0008f 1085 goto out;
c6c8fea2 1086
76543d14 1087 if (bcast_packet->header.ttl < 2)
f3e0008f 1088 goto out;
c6c8fea2 1089
da641193 1090 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
f3e0008f
ML
1091
1092 if (!orig_node)
b5a6f69c 1093 goto out;
c6c8fea2 1094
f3e0008f 1095 spin_lock_bh(&orig_node->bcast_seqno_lock);
c6c8fea2
SE
1096
1097 /* check whether the packet is a duplicate */
9b4a1159
SE
1098 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1099 ntohl(bcast_packet->seqno)))
f3e0008f 1100 goto spin_unlock;
c6c8fea2
SE
1101
1102 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1103
1104 /* check whether the packet is old and the host just restarted. */
30d3c511
SE
1105 if (batadv_window_protected(bat_priv, seq_diff,
1106 &orig_node->bcast_seqno_reset))
f3e0008f 1107 goto spin_unlock;
c6c8fea2
SE
1108
1109 /* mark broadcast in flood history, update window position
9cfc7bd6
SE
1110 * if required.
1111 */
0f5f9322 1112 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
c6c8fea2
SE
1113 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1114
f3e0008f 1115 spin_unlock_bh(&orig_node->bcast_seqno_lock);
f3e0008f 1116
fe2da6ff 1117 /* check whether this has been sent by another originator before */
08adf151 1118 if (batadv_bla_check_bcast_duplist(bat_priv, bcast_packet, hdr_size))
fe2da6ff
SW
1119 goto out;
1120
c6c8fea2 1121 /* rebroadcast packet */
9455e34c 1122 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
c6c8fea2 1123
23721387
SW
1124 /* don't hand the broadcast up if it is from an originator
1125 * from the same backbone.
1126 */
08adf151 1127 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
23721387
SW
1128 goto out;
1129
c6c8fea2 1130 /* broadcast for me */
04b482a2 1131 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
f3e0008f
ML
1132 ret = NET_RX_SUCCESS;
1133 goto out;
c6c8fea2 1134
f3e0008f
ML
1135spin_unlock:
1136 spin_unlock_bh(&orig_node->bcast_seqno_lock);
f3e0008f
ML
1137out:
1138 if (orig_node)
7d211efc 1139 batadv_orig_node_free_ref(orig_node);
f3e0008f 1140 return ret;
c6c8fea2
SE
1141}
1142
30d3c511 1143int batadv_recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
c6c8fea2
SE
1144{
1145 struct vis_packet *vis_packet;
1146 struct ethhdr *ethhdr;
1147 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
704509b8 1148 int hdr_size = sizeof(*vis_packet);
c6c8fea2
SE
1149
1150 /* keep skb linear */
1151 if (skb_linearize(skb) < 0)
1152 return NET_RX_DROP;
1153
1154 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1155 return NET_RX_DROP;
1156
1157 vis_packet = (struct vis_packet *)skb->data;
1158 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1159
1160 /* not for me */
3193e8fd 1161 if (!batadv_is_my_mac(ethhdr->h_dest))
c6c8fea2
SE
1162 return NET_RX_DROP;
1163
1164 /* ignore own packets */
3193e8fd 1165 if (batadv_is_my_mac(vis_packet->vis_orig))
c6c8fea2
SE
1166 return NET_RX_DROP;
1167
3193e8fd 1168 if (batadv_is_my_mac(vis_packet->sender_orig))
c6c8fea2
SE
1169 return NET_RX_DROP;
1170
1171 switch (vis_packet->vis_type) {
1172 case VIS_TYPE_SERVER_SYNC:
d0f714f4
SE
1173 batadv_receive_server_sync_packet(bat_priv, vis_packet,
1174 skb_headlen(skb));
c6c8fea2
SE
1175 break;
1176
1177 case VIS_TYPE_CLIENT_UPDATE:
d0f714f4
SE
1178 batadv_receive_client_update_packet(bat_priv, vis_packet,
1179 skb_headlen(skb));
c6c8fea2
SE
1180 break;
1181
1182 default: /* ignore unknown packet */
1183 break;
1184 }
1185
1186 /* We take a copy of the data in the packet, so we should
9cfc7bd6
SE
1187 * always free the skbuf.
1188 */
c6c8fea2
SE
1189 return NET_RX_DROP;
1190}
This page took 0.167452 seconds and 5 git commands to generate.