mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / net / batman-adv / routing.c
blobcf91099c4ecad11f6c8e9441993c5b95cb93b285
1 /* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich
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.
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.
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
20 #include "main.h"
21 #include "routing.h"
22 #include "send.h"
23 #include "soft-interface.h"
24 #include "hard-interface.h"
25 #include "icmp_socket.h"
26 #include "translation-table.h"
27 #include "originator.h"
28 #include "vis.h"
29 #include "unicast.h"
30 #include "bridge_loop_avoidance.h"
31 #include "distributed-arp-table.h"
32 #include "network-coding.h"
34 static int batadv_route_unicast_packet(struct sk_buff *skb,
35 struct batadv_hard_iface *recv_if);
37 static void _batadv_update_route(struct batadv_priv *bat_priv,
38 struct batadv_orig_node *orig_node,
39 struct batadv_neigh_node *neigh_node)
41 struct batadv_neigh_node *curr_router;
43 curr_router = batadv_orig_node_get_router(orig_node);
45 /* route deleted */
46 if ((curr_router) && (!neigh_node)) {
47 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
48 "Deleting route towards: %pM\n", orig_node->orig);
49 batadv_tt_global_del_orig(bat_priv, orig_node,
50 "Deleted route towards originator");
52 /* route added */
53 } else if ((!curr_router) && (neigh_node)) {
54 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
55 "Adding route towards: %pM (via %pM)\n",
56 orig_node->orig, neigh_node->addr);
57 /* route changed */
58 } else if (neigh_node && curr_router) {
59 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
60 "Changing route towards: %pM (now via %pM - was via %pM)\n",
61 orig_node->orig, neigh_node->addr,
62 curr_router->addr);
65 if (curr_router)
66 batadv_neigh_node_free_ref(curr_router);
68 /* increase refcount of new best neighbor */
69 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
70 neigh_node = NULL;
72 spin_lock_bh(&orig_node->neigh_list_lock);
73 /* curr_router used earlier may not be the current orig_node->router
74 * anymore because it was dereferenced outside of the neigh_list_lock
75 * protected region. After the new best neighbor has replace the current
76 * best neighbor the reference counter needs to decrease. Consequently,
77 * the code needs to ensure the curr_router variable contains a pointer
78 * to the replaced best neighbor.
80 curr_router = rcu_dereference_protected(orig_node->router, true);
82 rcu_assign_pointer(orig_node->router, neigh_node);
83 spin_unlock_bh(&orig_node->neigh_list_lock);
85 /* decrease refcount of previous best neighbor */
86 if (curr_router)
87 batadv_neigh_node_free_ref(curr_router);
90 void batadv_update_route(struct batadv_priv *bat_priv,
91 struct batadv_orig_node *orig_node,
92 struct batadv_neigh_node *neigh_node)
94 struct batadv_neigh_node *router = NULL;
96 if (!orig_node)
97 goto out;
99 router = batadv_orig_node_get_router(orig_node);
101 if (router != neigh_node)
102 _batadv_update_route(bat_priv, orig_node, neigh_node);
104 out:
105 if (router)
106 batadv_neigh_node_free_ref(router);
109 /* caller must hold the neigh_list_lock */
110 void batadv_bonding_candidate_del(struct batadv_orig_node *orig_node,
111 struct batadv_neigh_node *neigh_node)
113 /* this neighbor is not part of our candidate list */
114 if (list_empty(&neigh_node->bonding_list))
115 goto out;
117 list_del_rcu(&neigh_node->bonding_list);
118 INIT_LIST_HEAD(&neigh_node->bonding_list);
119 batadv_neigh_node_free_ref(neigh_node);
120 atomic_dec(&orig_node->bond_candidates);
122 out:
123 return;
126 void batadv_bonding_candidate_add(struct batadv_orig_node *orig_node,
127 struct batadv_neigh_node *neigh_node)
129 struct batadv_neigh_node *tmp_neigh_node, *router = NULL;
130 uint8_t interference_candidate = 0;
132 spin_lock_bh(&orig_node->neigh_list_lock);
134 /* only consider if it has the same primary address ... */
135 if (!batadv_compare_eth(orig_node->orig,
136 neigh_node->orig_node->primary_addr))
137 goto candidate_del;
139 router = batadv_orig_node_get_router(orig_node);
140 if (!router)
141 goto candidate_del;
143 /* ... and is good enough to be considered */
144 if (neigh_node->tq_avg < router->tq_avg - BATADV_BONDING_TQ_THRESHOLD)
145 goto candidate_del;
147 /* check if we have another candidate with the same mac address or
148 * interface. If we do, we won't select this candidate because of
149 * possible interference.
151 hlist_for_each_entry_rcu(tmp_neigh_node,
152 &orig_node->neigh_list, list) {
153 if (tmp_neigh_node == neigh_node)
154 continue;
156 /* we only care if the other candidate is even
157 * considered as candidate.
159 if (list_empty(&tmp_neigh_node->bonding_list))
160 continue;
162 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
163 (batadv_compare_eth(neigh_node->addr,
164 tmp_neigh_node->addr))) {
165 interference_candidate = 1;
166 break;
170 /* don't care further if it is an interference candidate */
171 if (interference_candidate)
172 goto candidate_del;
174 /* this neighbor already is part of our candidate list */
175 if (!list_empty(&neigh_node->bonding_list))
176 goto out;
178 if (!atomic_inc_not_zero(&neigh_node->refcount))
179 goto out;
181 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
182 atomic_inc(&orig_node->bond_candidates);
183 goto out;
185 candidate_del:
186 batadv_bonding_candidate_del(orig_node, neigh_node);
188 out:
189 spin_unlock_bh(&orig_node->neigh_list_lock);
191 if (router)
192 batadv_neigh_node_free_ref(router);
195 /* copy primary address for bonding */
196 void
197 batadv_bonding_save_primary(const struct batadv_orig_node *orig_node,
198 struct batadv_orig_node *orig_neigh_node,
199 const struct batadv_ogm_packet *batman_ogm_packet)
201 if (!(batman_ogm_packet->flags & BATADV_PRIMARIES_FIRST_HOP))
202 return;
204 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
207 /* checks whether the host restarted and is in the protection time.
208 * returns:
209 * 0 if the packet is to be accepted
210 * 1 if the packet is to be ignored.
212 int batadv_window_protected(struct batadv_priv *bat_priv, int32_t seq_num_diff,
213 unsigned long *last_reset)
215 if (seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE ||
216 seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
217 if (!batadv_has_timed_out(*last_reset,
218 BATADV_RESET_PROTECTION_MS))
219 return 1;
221 *last_reset = jiffies;
222 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
223 "old packet received, start protection\n");
226 return 0;
229 bool batadv_check_management_packet(struct sk_buff *skb,
230 struct batadv_hard_iface *hard_iface,
231 int header_len)
233 struct ethhdr *ethhdr;
235 /* drop packet if it has not necessary minimum size */
236 if (unlikely(!pskb_may_pull(skb, header_len)))
237 return false;
239 ethhdr = eth_hdr(skb);
241 /* packet with broadcast indication but unicast recipient */
242 if (!is_broadcast_ether_addr(ethhdr->h_dest))
243 return false;
245 /* packet with broadcast sender address */
246 if (is_broadcast_ether_addr(ethhdr->h_source))
247 return false;
249 /* create a copy of the skb, if needed, to modify it. */
250 if (skb_cow(skb, 0) < 0)
251 return false;
253 /* keep skb linear */
254 if (skb_linearize(skb) < 0)
255 return false;
257 return true;
260 static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
261 struct sk_buff *skb, size_t icmp_len)
263 struct batadv_hard_iface *primary_if = NULL;
264 struct batadv_orig_node *orig_node = NULL;
265 struct batadv_icmp_packet_rr *icmp_packet;
266 int ret = NET_RX_DROP;
268 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
270 /* add data to device queue */
271 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
272 batadv_socket_receive_packet(icmp_packet, icmp_len);
273 goto out;
276 primary_if = batadv_primary_if_get_selected(bat_priv);
277 if (!primary_if)
278 goto out;
280 /* answer echo request (ping) */
281 /* get routing information */
282 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
283 if (!orig_node)
284 goto out;
286 /* create a copy of the skb, if needed, to modify it. */
287 if (skb_cow(skb, ETH_HLEN) < 0)
288 goto out;
290 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
292 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
293 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
294 icmp_packet->msg_type = BATADV_ECHO_REPLY;
295 icmp_packet->header.ttl = BATADV_TTL;
297 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
298 ret = NET_RX_SUCCESS;
300 out:
301 if (primary_if)
302 batadv_hardif_free_ref(primary_if);
303 if (orig_node)
304 batadv_orig_node_free_ref(orig_node);
305 return ret;
308 static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
309 struct sk_buff *skb)
311 struct batadv_hard_iface *primary_if = NULL;
312 struct batadv_orig_node *orig_node = NULL;
313 struct batadv_icmp_packet *icmp_packet;
314 int ret = NET_RX_DROP;
316 icmp_packet = (struct batadv_icmp_packet *)skb->data;
318 /* send TTL exceeded if packet is an echo request (traceroute) */
319 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
320 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
321 icmp_packet->orig, icmp_packet->dst);
322 goto out;
325 primary_if = batadv_primary_if_get_selected(bat_priv);
326 if (!primary_if)
327 goto out;
329 /* get routing information */
330 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
331 if (!orig_node)
332 goto out;
334 /* create a copy of the skb, if needed, to modify it. */
335 if (skb_cow(skb, ETH_HLEN) < 0)
336 goto out;
338 icmp_packet = (struct batadv_icmp_packet *)skb->data;
340 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
341 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
342 icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
343 icmp_packet->header.ttl = BATADV_TTL;
345 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
346 ret = NET_RX_SUCCESS;
348 out:
349 if (primary_if)
350 batadv_hardif_free_ref(primary_if);
351 if (orig_node)
352 batadv_orig_node_free_ref(orig_node);
353 return ret;
357 int batadv_recv_icmp_packet(struct sk_buff *skb,
358 struct batadv_hard_iface *recv_if)
360 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
361 struct batadv_icmp_packet_rr *icmp_packet;
362 struct ethhdr *ethhdr;
363 struct batadv_orig_node *orig_node = NULL;
364 int hdr_size = sizeof(struct batadv_icmp_packet);
365 int ret = NET_RX_DROP;
367 /* we truncate all incoming icmp packets if they don't match our size */
368 if (skb->len >= sizeof(struct batadv_icmp_packet_rr))
369 hdr_size = sizeof(struct batadv_icmp_packet_rr);
371 /* drop packet if it has not necessary minimum size */
372 if (unlikely(!pskb_may_pull(skb, hdr_size)))
373 goto out;
375 ethhdr = eth_hdr(skb);
377 /* packet with unicast indication but broadcast recipient */
378 if (is_broadcast_ether_addr(ethhdr->h_dest))
379 goto out;
381 /* packet with broadcast sender address */
382 if (is_broadcast_ether_addr(ethhdr->h_source))
383 goto out;
385 /* not for me */
386 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
387 goto out;
389 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
391 /* add record route information if not full */
392 if ((hdr_size == sizeof(struct batadv_icmp_packet_rr)) &&
393 (icmp_packet->rr_cur < BATADV_RR_LEN)) {
394 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
395 ethhdr->h_dest, ETH_ALEN);
396 icmp_packet->rr_cur++;
399 /* packet for me */
400 if (batadv_is_my_mac(bat_priv, icmp_packet->dst))
401 return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size);
403 /* TTL exceeded */
404 if (icmp_packet->header.ttl < 2)
405 return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
407 /* get routing information */
408 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
409 if (!orig_node)
410 goto out;
412 /* create a copy of the skb, if needed, to modify it. */
413 if (skb_cow(skb, ETH_HLEN) < 0)
414 goto out;
416 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
418 /* decrement ttl */
419 icmp_packet->header.ttl--;
421 /* route it */
422 if (batadv_send_skb_to_orig(skb, orig_node, recv_if) != NET_XMIT_DROP)
423 ret = NET_RX_SUCCESS;
425 out:
426 if (orig_node)
427 batadv_orig_node_free_ref(orig_node);
428 return ret;
431 /* In the bonding case, send the packets in a round
432 * robin fashion over the remaining interfaces.
434 * This method rotates the bonding list and increases the
435 * returned router's refcount.
437 static struct batadv_neigh_node *
438 batadv_find_bond_router(struct batadv_orig_node *primary_orig,
439 const struct batadv_hard_iface *recv_if)
441 struct batadv_neigh_node *tmp_neigh_node;
442 struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
444 rcu_read_lock();
445 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
446 bonding_list) {
447 if (!first_candidate)
448 first_candidate = tmp_neigh_node;
450 /* recv_if == NULL on the first node. */
451 if (tmp_neigh_node->if_incoming == recv_if)
452 continue;
454 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
455 continue;
457 router = tmp_neigh_node;
458 break;
461 /* use the first candidate if nothing was found. */
462 if (!router && first_candidate &&
463 atomic_inc_not_zero(&first_candidate->refcount))
464 router = first_candidate;
466 if (!router)
467 goto out;
469 /* selected should point to the next element
470 * after the current router
472 spin_lock_bh(&primary_orig->neigh_list_lock);
473 /* this is a list_move(), which unfortunately
474 * does not exist as rcu version
476 list_del_rcu(&primary_orig->bond_list);
477 list_add_rcu(&primary_orig->bond_list,
478 &router->bonding_list);
479 spin_unlock_bh(&primary_orig->neigh_list_lock);
481 out:
482 rcu_read_unlock();
483 return router;
486 /* Interface Alternating: Use the best of the
487 * remaining candidates which are not using
488 * this interface.
490 * Increases the returned router's refcount
492 static struct batadv_neigh_node *
493 batadv_find_ifalter_router(struct batadv_orig_node *primary_orig,
494 const struct batadv_hard_iface *recv_if)
496 struct batadv_neigh_node *tmp_neigh_node;
497 struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
499 rcu_read_lock();
500 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
501 bonding_list) {
502 if (!first_candidate)
503 first_candidate = tmp_neigh_node;
505 /* recv_if == NULL on the first node. */
506 if (tmp_neigh_node->if_incoming == recv_if)
507 continue;
509 if (router && tmp_neigh_node->tq_avg <= router->tq_avg)
510 continue;
512 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
513 continue;
515 /* decrement refcount of previously selected router */
516 if (router)
517 batadv_neigh_node_free_ref(router);
519 /* we found a better router (or at least one valid router) */
520 router = tmp_neigh_node;
523 /* use the first candidate if nothing was found. */
524 if (!router && first_candidate &&
525 atomic_inc_not_zero(&first_candidate->refcount))
526 router = first_candidate;
528 rcu_read_unlock();
529 return router;
533 * batadv_check_unicast_packet - Check for malformed unicast packets
534 * @bat_priv: the bat priv with all the soft interface information
535 * @skb: packet to check
536 * @hdr_size: size of header to pull
538 * Check for short header and bad addresses in given packet. Returns negative
539 * value when check fails and 0 otherwise. The negative value depends on the
540 * reason: -ENODATA for bad header, -EBADR for broadcast destination or source,
541 * and -EREMOTE for non-local (other host) destination.
543 static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
544 struct sk_buff *skb, int hdr_size)
546 struct ethhdr *ethhdr;
548 /* drop packet if it has not necessary minimum size */
549 if (unlikely(!pskb_may_pull(skb, hdr_size)))
550 return -ENODATA;
552 ethhdr = eth_hdr(skb);
554 /* packet with unicast indication but broadcast recipient */
555 if (is_broadcast_ether_addr(ethhdr->h_dest))
556 return -EBADR;
558 /* packet with broadcast sender address */
559 if (is_broadcast_ether_addr(ethhdr->h_source))
560 return -EBADR;
562 /* not for me */
563 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
564 return -EREMOTE;
566 return 0;
569 int batadv_recv_tt_query(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
571 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
572 struct batadv_tt_query_packet *tt_query;
573 uint16_t tt_size;
574 int hdr_size = sizeof(*tt_query);
575 char tt_flag;
576 size_t packet_size;
578 if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
579 return NET_RX_DROP;
581 /* I could need to modify it */
582 if (skb_cow(skb, sizeof(struct batadv_tt_query_packet)) < 0)
583 goto out;
585 tt_query = (struct batadv_tt_query_packet *)skb->data;
587 switch (tt_query->flags & BATADV_TT_QUERY_TYPE_MASK) {
588 case BATADV_TT_REQUEST:
589 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
591 /* If we cannot provide an answer the tt_request is
592 * forwarded
594 if (!batadv_send_tt_response(bat_priv, tt_query)) {
595 if (tt_query->flags & BATADV_TT_FULL_TABLE)
596 tt_flag = 'F';
597 else
598 tt_flag = '.';
600 batadv_dbg(BATADV_DBG_TT, bat_priv,
601 "Routing TT_REQUEST to %pM [%c]\n",
602 tt_query->dst,
603 tt_flag);
604 return batadv_route_unicast_packet(skb, recv_if);
606 break;
607 case BATADV_TT_RESPONSE:
608 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
610 if (batadv_is_my_mac(bat_priv, tt_query->dst)) {
611 /* packet needs to be linearized to access the TT
612 * changes
614 if (skb_linearize(skb) < 0)
615 goto out;
616 /* skb_linearize() possibly changed skb->data */
617 tt_query = (struct batadv_tt_query_packet *)skb->data;
619 tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
621 /* Ensure we have all the claimed data */
622 packet_size = sizeof(struct batadv_tt_query_packet);
623 packet_size += tt_size;
624 if (unlikely(skb_headlen(skb) < packet_size))
625 goto out;
627 batadv_handle_tt_response(bat_priv, tt_query);
628 } else {
629 if (tt_query->flags & BATADV_TT_FULL_TABLE)
630 tt_flag = 'F';
631 else
632 tt_flag = '.';
633 batadv_dbg(BATADV_DBG_TT, bat_priv,
634 "Routing TT_RESPONSE to %pM [%c]\n",
635 tt_query->dst,
636 tt_flag);
637 return batadv_route_unicast_packet(skb, recv_if);
639 break;
642 out:
643 /* returning NET_RX_DROP will make the caller function kfree the skb */
644 return NET_RX_DROP;
647 int batadv_recv_roam_adv(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
649 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
650 struct batadv_roam_adv_packet *roam_adv_packet;
651 struct batadv_orig_node *orig_node;
653 if (batadv_check_unicast_packet(bat_priv, skb,
654 sizeof(*roam_adv_packet)) < 0)
655 goto out;
657 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
659 roam_adv_packet = (struct batadv_roam_adv_packet *)skb->data;
661 if (!batadv_is_my_mac(bat_priv, roam_adv_packet->dst))
662 return batadv_route_unicast_packet(skb, recv_if);
664 /* check if it is a backbone gateway. we don't accept
665 * roaming advertisement from it, as it has the same
666 * entries as we have.
668 if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
669 goto out;
671 orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
672 if (!orig_node)
673 goto out;
675 batadv_dbg(BATADV_DBG_TT, bat_priv,
676 "Received ROAMING_ADV from %pM (client %pM)\n",
677 roam_adv_packet->src, roam_adv_packet->client);
679 batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
680 BATADV_TT_CLIENT_ROAM,
681 atomic_read(&orig_node->last_ttvn) + 1);
683 batadv_orig_node_free_ref(orig_node);
684 out:
685 /* returning NET_RX_DROP will make the caller function kfree the skb */
686 return NET_RX_DROP;
689 /* find a suitable router for this originator, and use
690 * bonding if possible. increases the found neighbors
691 * refcount.
693 struct batadv_neigh_node *
694 batadv_find_router(struct batadv_priv *bat_priv,
695 struct batadv_orig_node *orig_node,
696 const struct batadv_hard_iface *recv_if)
698 struct batadv_orig_node *primary_orig_node;
699 struct batadv_orig_node *router_orig;
700 struct batadv_neigh_node *router;
701 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
702 int bonding_enabled;
703 uint8_t *primary_addr;
705 if (!orig_node)
706 return NULL;
708 router = batadv_orig_node_get_router(orig_node);
709 if (!router)
710 goto err;
712 /* without bonding, the first node should
713 * always choose the default router.
715 bonding_enabled = atomic_read(&bat_priv->bonding);
717 rcu_read_lock();
718 /* select default router to output */
719 router_orig = router->orig_node;
720 if (!router_orig)
721 goto err_unlock;
723 if ((!recv_if) && (!bonding_enabled))
724 goto return_router;
726 primary_addr = router_orig->primary_addr;
728 /* if we have something in the primary_addr, we can search
729 * for a potential bonding candidate.
731 if (batadv_compare_eth(primary_addr, zero_mac))
732 goto return_router;
734 /* find the orig_node which has the primary interface. might
735 * even be the same as our router_orig in many cases
737 if (batadv_compare_eth(primary_addr, router_orig->orig)) {
738 primary_orig_node = router_orig;
739 } else {
740 primary_orig_node = batadv_orig_hash_find(bat_priv,
741 primary_addr);
742 if (!primary_orig_node)
743 goto return_router;
745 batadv_orig_node_free_ref(primary_orig_node);
748 /* with less than 2 candidates, we can't do any
749 * bonding and prefer the original router.
751 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
752 goto return_router;
754 /* all nodes between should choose a candidate which
755 * is is not on the interface where the packet came
756 * in.
758 batadv_neigh_node_free_ref(router);
760 if (bonding_enabled)
761 router = batadv_find_bond_router(primary_orig_node, recv_if);
762 else
763 router = batadv_find_ifalter_router(primary_orig_node, recv_if);
765 return_router:
766 if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
767 goto err_unlock;
769 rcu_read_unlock();
770 return router;
771 err_unlock:
772 rcu_read_unlock();
773 err:
774 if (router)
775 batadv_neigh_node_free_ref(router);
776 return NULL;
779 static int batadv_route_unicast_packet(struct sk_buff *skb,
780 struct batadv_hard_iface *recv_if)
782 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
783 struct batadv_orig_node *orig_node = NULL;
784 struct batadv_neigh_node *neigh_node = NULL;
785 struct batadv_unicast_packet *unicast_packet;
786 struct ethhdr *ethhdr = eth_hdr(skb);
787 int res, hdr_len, ret = NET_RX_DROP;
788 struct sk_buff *new_skb;
790 unicast_packet = (struct batadv_unicast_packet *)skb->data;
792 /* TTL exceeded */
793 if (unicast_packet->header.ttl < 2) {
794 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
795 ethhdr->h_source, unicast_packet->dest);
796 goto out;
799 /* get routing information */
800 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
802 if (!orig_node)
803 goto out;
805 /* find_router() increases neigh_nodes refcount if found. */
806 neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
808 if (!neigh_node)
809 goto out;
811 /* create a copy of the skb, if needed, to modify it. */
812 if (skb_cow(skb, ETH_HLEN) < 0)
813 goto out;
815 unicast_packet = (struct batadv_unicast_packet *)skb->data;
817 if (unicast_packet->header.packet_type == BATADV_UNICAST &&
818 atomic_read(&bat_priv->fragmentation) &&
819 skb->len > neigh_node->if_incoming->net_dev->mtu) {
820 ret = batadv_frag_send_skb(skb, bat_priv,
821 neigh_node->if_incoming,
822 neigh_node->addr);
823 goto out;
826 if (unicast_packet->header.packet_type == BATADV_UNICAST_FRAG &&
827 batadv_frag_can_reassemble(skb,
828 neigh_node->if_incoming->net_dev->mtu)) {
829 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
831 if (ret == NET_RX_DROP)
832 goto out;
834 /* packet was buffered for late merge */
835 if (!new_skb) {
836 ret = NET_RX_SUCCESS;
837 goto out;
840 skb = new_skb;
841 unicast_packet = (struct batadv_unicast_packet *)skb->data;
844 /* decrement ttl */
845 unicast_packet->header.ttl--;
847 switch (unicast_packet->header.packet_type) {
848 case BATADV_UNICAST_4ADDR:
849 hdr_len = sizeof(struct batadv_unicast_4addr_packet);
850 break;
851 case BATADV_UNICAST:
852 hdr_len = sizeof(struct batadv_unicast_packet);
853 break;
854 default:
855 /* other packet types not supported - yet */
856 hdr_len = -1;
857 break;
860 if (hdr_len > 0)
861 batadv_skb_set_priority(skb, hdr_len);
863 res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
865 /* translate transmit result into receive result */
866 if (res == NET_XMIT_SUCCESS) {
867 /* skb was transmitted and consumed */
868 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
869 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
870 skb->len + ETH_HLEN);
872 ret = NET_RX_SUCCESS;
873 } else if (res == NET_XMIT_POLICED) {
874 /* skb was buffered and consumed */
875 ret = NET_RX_SUCCESS;
878 out:
879 if (neigh_node)
880 batadv_neigh_node_free_ref(neigh_node);
881 if (orig_node)
882 batadv_orig_node_free_ref(orig_node);
883 return ret;
887 * batadv_reroute_unicast_packet - update the unicast header for re-routing
888 * @bat_priv: the bat priv with all the soft interface information
889 * @unicast_packet: the unicast header to be updated
890 * @dst_addr: the payload destination
892 * Search the translation table for dst_addr and update the unicast header with
893 * the new corresponding information (originator address where the destination
894 * client currently is and its known TTVN)
896 * Returns true if the packet header has been updated, false otherwise
898 static bool
899 batadv_reroute_unicast_packet(struct batadv_priv *bat_priv,
900 struct batadv_unicast_packet *unicast_packet,
901 uint8_t *dst_addr)
903 struct batadv_orig_node *orig_node = NULL;
904 struct batadv_hard_iface *primary_if = NULL;
905 bool ret = false;
906 uint8_t *orig_addr, orig_ttvn;
908 if (batadv_is_my_client(bat_priv, dst_addr)) {
909 primary_if = batadv_primary_if_get_selected(bat_priv);
910 if (!primary_if)
911 goto out;
912 orig_addr = primary_if->net_dev->dev_addr;
913 orig_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
914 } else {
915 orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr);
916 if (!orig_node)
917 goto out;
919 if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
920 goto out;
922 orig_addr = orig_node->orig;
923 orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
926 /* update the packet header */
927 memcpy(unicast_packet->dest, orig_addr, ETH_ALEN);
928 unicast_packet->ttvn = orig_ttvn;
930 ret = true;
931 out:
932 if (primary_if)
933 batadv_hardif_free_ref(primary_if);
934 if (orig_node)
935 batadv_orig_node_free_ref(orig_node);
937 return ret;
940 static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
941 struct sk_buff *skb, int hdr_len) {
942 uint8_t curr_ttvn, old_ttvn;
943 struct batadv_orig_node *orig_node;
944 struct ethhdr *ethhdr;
945 struct batadv_hard_iface *primary_if;
946 struct batadv_unicast_packet *unicast_packet;
947 int is_old_ttvn;
949 /* check if there is enough data before accessing it */
950 if (pskb_may_pull(skb, hdr_len + ETH_HLEN) < 0)
951 return 0;
953 /* create a copy of the skb (in case of for re-routing) to modify it. */
954 if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
955 return 0;
957 unicast_packet = (struct batadv_unicast_packet *)skb->data;
958 ethhdr = (struct ethhdr *)(skb->data + hdr_len);
960 /* check if the destination client was served by this node and it is now
961 * roaming. In this case, it means that the node has got a ROAM_ADV
962 * message and that it knows the new destination in the mesh to re-route
963 * the packet to
965 if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest)) {
966 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
967 ethhdr->h_dest))
968 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT,
969 bat_priv,
970 "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
971 unicast_packet->dest,
972 ethhdr->h_dest);
973 /* at this point the mesh destination should have been
974 * substituted with the originator address found in the global
975 * table. If not, let the packet go untouched anyway because
976 * there is nothing the node can do
978 return 1;
981 /* retrieve the TTVN known by this node for the packet destination. This
982 * value is used later to check if the node which sent (or re-routed
983 * last time) the packet had an updated information or not
985 curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
986 if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
987 orig_node = batadv_orig_hash_find(bat_priv,
988 unicast_packet->dest);
989 /* if it is not possible to find the orig_node representing the
990 * destination, the packet can immediately be dropped as it will
991 * not be possible to deliver it
993 if (!orig_node)
994 return 0;
996 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
997 batadv_orig_node_free_ref(orig_node);
1000 /* check if the TTVN contained in the packet is fresher than what the
1001 * node knows
1003 is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
1004 if (!is_old_ttvn)
1005 return 1;
1007 old_ttvn = unicast_packet->ttvn;
1008 /* the packet was forged based on outdated network information. Its
1009 * destination can possibly be updated and forwarded towards the new
1010 * target host
1012 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
1013 ethhdr->h_dest)) {
1014 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv,
1015 "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
1016 unicast_packet->dest, ethhdr->h_dest,
1017 old_ttvn, curr_ttvn);
1018 return 1;
1021 /* the packet has not been re-routed: either the destination is
1022 * currently served by this node or there is no destination at all and
1023 * it is possible to drop the packet
1025 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
1026 return 0;
1028 /* update the header in order to let the packet be delivered to this
1029 * node's soft interface
1031 primary_if = batadv_primary_if_get_selected(bat_priv);
1032 if (!primary_if)
1033 return 0;
1035 memcpy(unicast_packet->dest, primary_if->net_dev->dev_addr, ETH_ALEN);
1037 batadv_hardif_free_ref(primary_if);
1039 unicast_packet->ttvn = curr_ttvn;
1041 return 1;
1044 int batadv_recv_unicast_packet(struct sk_buff *skb,
1045 struct batadv_hard_iface *recv_if)
1047 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1048 struct batadv_unicast_packet *unicast_packet;
1049 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
1050 uint8_t *orig_addr;
1051 struct batadv_orig_node *orig_node = NULL;
1052 int check, hdr_size = sizeof(*unicast_packet);
1053 bool is4addr;
1055 unicast_packet = (struct batadv_unicast_packet *)skb->data;
1056 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
1058 is4addr = unicast_packet->header.packet_type == BATADV_UNICAST_4ADDR;
1059 /* the caller function should have already pulled 2 bytes */
1060 if (is4addr)
1061 hdr_size = sizeof(*unicast_4addr_packet);
1063 /* function returns -EREMOTE for promiscuous packets */
1064 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
1066 /* Even though the packet is not for us, we might save it to use for
1067 * decoding a later received coded packet
1069 if (check == -EREMOTE)
1070 batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
1072 if (check < 0)
1073 return NET_RX_DROP;
1074 if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
1075 return NET_RX_DROP;
1077 /* packet for me */
1078 if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
1079 if (is4addr) {
1080 batadv_dat_inc_counter(bat_priv,
1081 unicast_4addr_packet->subtype);
1082 orig_addr = unicast_4addr_packet->src;
1083 orig_node = batadv_orig_hash_find(bat_priv, orig_addr);
1086 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
1087 hdr_size))
1088 goto rx_success;
1089 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
1090 hdr_size))
1091 goto rx_success;
1093 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
1094 orig_node);
1096 rx_success:
1097 if (orig_node)
1098 batadv_orig_node_free_ref(orig_node);
1100 return NET_RX_SUCCESS;
1103 return batadv_route_unicast_packet(skb, recv_if);
1106 int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
1107 struct batadv_hard_iface *recv_if)
1109 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1110 struct batadv_unicast_frag_packet *unicast_packet;
1111 int hdr_size = sizeof(*unicast_packet);
1112 struct sk_buff *new_skb = NULL;
1113 int ret;
1115 if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
1116 return NET_RX_DROP;
1118 if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
1119 return NET_RX_DROP;
1121 unicast_packet = (struct batadv_unicast_frag_packet *)skb->data;
1123 /* packet for me */
1124 if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
1125 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
1127 if (ret == NET_RX_DROP)
1128 return NET_RX_DROP;
1130 /* packet was buffered for late merge */
1131 if (!new_skb)
1132 return NET_RX_SUCCESS;
1134 if (batadv_dat_snoop_incoming_arp_request(bat_priv, new_skb,
1135 hdr_size))
1136 goto rx_success;
1137 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, new_skb,
1138 hdr_size))
1139 goto rx_success;
1141 batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
1142 sizeof(struct batadv_unicast_packet), NULL);
1144 rx_success:
1145 return NET_RX_SUCCESS;
1148 return batadv_route_unicast_packet(skb, recv_if);
1152 int batadv_recv_bcast_packet(struct sk_buff *skb,
1153 struct batadv_hard_iface *recv_if)
1155 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1156 struct batadv_orig_node *orig_node = NULL;
1157 struct batadv_bcast_packet *bcast_packet;
1158 struct ethhdr *ethhdr;
1159 int hdr_size = sizeof(*bcast_packet);
1160 int ret = NET_RX_DROP;
1161 int32_t seq_diff;
1163 /* drop packet if it has not necessary minimum size */
1164 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1165 goto out;
1167 ethhdr = eth_hdr(skb);
1169 /* packet with broadcast indication but unicast recipient */
1170 if (!is_broadcast_ether_addr(ethhdr->h_dest))
1171 goto out;
1173 /* packet with broadcast sender address */
1174 if (is_broadcast_ether_addr(ethhdr->h_source))
1175 goto out;
1177 /* ignore broadcasts sent by myself */
1178 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
1179 goto out;
1181 bcast_packet = (struct batadv_bcast_packet *)skb->data;
1183 /* ignore broadcasts originated by myself */
1184 if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
1185 goto out;
1187 if (bcast_packet->header.ttl < 2)
1188 goto out;
1190 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
1192 if (!orig_node)
1193 goto out;
1195 spin_lock_bh(&orig_node->bcast_seqno_lock);
1197 /* check whether the packet is a duplicate */
1198 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1199 ntohl(bcast_packet->seqno)))
1200 goto spin_unlock;
1202 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1204 /* check whether the packet is old and the host just restarted. */
1205 if (batadv_window_protected(bat_priv, seq_diff,
1206 &orig_node->bcast_seqno_reset))
1207 goto spin_unlock;
1209 /* mark broadcast in flood history, update window position
1210 * if required.
1212 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1213 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1215 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1217 /* check whether this has been sent by another originator before */
1218 if (batadv_bla_check_bcast_duplist(bat_priv, skb))
1219 goto out;
1221 batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
1223 /* rebroadcast packet */
1224 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
1226 /* don't hand the broadcast up if it is from an originator
1227 * from the same backbone.
1229 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
1230 goto out;
1232 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
1233 goto rx_success;
1234 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
1235 goto rx_success;
1237 /* broadcast for me */
1238 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
1239 orig_node);
1241 rx_success:
1242 ret = NET_RX_SUCCESS;
1243 goto out;
1245 spin_unlock:
1246 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1247 out:
1248 if (orig_node)
1249 batadv_orig_node_free_ref(orig_node);
1250 return ret;
1253 int batadv_recv_vis_packet(struct sk_buff *skb,
1254 struct batadv_hard_iface *recv_if)
1256 struct batadv_vis_packet *vis_packet;
1257 struct ethhdr *ethhdr;
1258 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1259 int hdr_size = sizeof(*vis_packet);
1261 /* keep skb linear */
1262 if (skb_linearize(skb) < 0)
1263 return NET_RX_DROP;
1265 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1266 return NET_RX_DROP;
1268 vis_packet = (struct batadv_vis_packet *)skb->data;
1269 ethhdr = eth_hdr(skb);
1271 /* not for me */
1272 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
1273 return NET_RX_DROP;
1275 /* ignore own packets */
1276 if (batadv_is_my_mac(bat_priv, vis_packet->vis_orig))
1277 return NET_RX_DROP;
1279 if (batadv_is_my_mac(bat_priv, vis_packet->sender_orig))
1280 return NET_RX_DROP;
1282 switch (vis_packet->vis_type) {
1283 case BATADV_VIS_TYPE_SERVER_SYNC:
1284 batadv_receive_server_sync_packet(bat_priv, vis_packet,
1285 skb_headlen(skb));
1286 break;
1288 case BATADV_VIS_TYPE_CLIENT_UPDATE:
1289 batadv_receive_client_update_packet(bat_priv, vis_packet,
1290 skb_headlen(skb));
1291 break;
1293 default: /* ignore unknown packet */
1294 break;
1297 /* We take a copy of the data in the packet, so we should
1298 * always free the skbuf.
1300 return NET_RX_DROP;