2 * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "soft-interface.h"
26 #include "hard-interface.h"
27 #include "icmp_socket.h"
28 #include "translation-table.h"
29 #include "originator.h"
34 void slide_own_bcast_window(struct hard_iface *hard_iface)
36 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
37 struct hashtable_t *hash = bat_priv->orig_hash;
38 struct hlist_node *node;
39 struct hlist_head *head;
40 struct orig_node *orig_node;
45 for (i = 0; i < hash->size; i++) {
46 head = &hash->table[i];
49 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
50 spin_lock_bh(&orig_node->ogm_cnt_lock);
51 word_index = hard_iface->if_num * NUM_WORDS;
52 word = &(orig_node->bcast_own[word_index]);
54 bit_get_packet(bat_priv, word, 1, 0);
55 orig_node->bcast_own_sum[hard_iface->if_num] =
56 bit_packet_count(word);
57 spin_unlock_bh(&orig_node->ogm_cnt_lock);
63 static void _update_route(struct bat_priv *bat_priv,
64 struct orig_node *orig_node,
65 struct neigh_node *neigh_node)
67 struct neigh_node *curr_router;
69 curr_router = orig_node_get_router(orig_node);
72 if ((curr_router) && (!neigh_node)) {
73 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
75 tt_global_del_orig(bat_priv, orig_node,
76 "Deleted route towards originator");
79 } else if ((!curr_router) && (neigh_node)) {
81 bat_dbg(DBG_ROUTES, bat_priv,
82 "Adding route towards: %pM (via %pM)\n",
83 orig_node->orig, neigh_node->addr);
85 } else if (neigh_node && curr_router) {
86 bat_dbg(DBG_ROUTES, bat_priv,
87 "Changing route towards: %pM "
88 "(now via %pM - was via %pM)\n",
89 orig_node->orig, neigh_node->addr,
94 neigh_node_free_ref(curr_router);
96 /* increase refcount of new best neighbor */
97 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
100 spin_lock_bh(&orig_node->neigh_list_lock);
101 /* curr_router used earlier may not be the current orig_node->router
102 * anymore because it was dereferenced outside of the neigh_list_lock
103 * protected region. After the new best neighbor has replace the current
104 * best neighbor the reference counter needs to decrease. Consequently,
105 * the code needs to ensure the curr_router variable contains a pointer
106 * to the replaced best neighbor.
108 curr_router = rcu_dereference_protected(orig_node->router, true);
110 rcu_assign_pointer(orig_node->router, neigh_node);
111 spin_unlock_bh(&orig_node->neigh_list_lock);
113 /* decrease refcount of previous best neighbor */
115 neigh_node_free_ref(curr_router);
118 void update_route(struct bat_priv *bat_priv, struct orig_node *orig_node,
119 struct neigh_node *neigh_node)
121 struct neigh_node *router = NULL;
126 router = orig_node_get_router(orig_node);
128 if (router != neigh_node)
129 _update_route(bat_priv, orig_node, neigh_node);
133 neigh_node_free_ref(router);
136 /* caller must hold the neigh_list_lock */
137 void bonding_candidate_del(struct orig_node *orig_node,
138 struct neigh_node *neigh_node)
140 /* this neighbor is not part of our candidate list */
141 if (list_empty(&neigh_node->bonding_list))
144 list_del_rcu(&neigh_node->bonding_list);
145 INIT_LIST_HEAD(&neigh_node->bonding_list);
146 neigh_node_free_ref(neigh_node);
147 atomic_dec(&orig_node->bond_candidates);
153 void bonding_candidate_add(struct orig_node *orig_node,
154 struct neigh_node *neigh_node)
156 struct hlist_node *node;
157 struct neigh_node *tmp_neigh_node, *router = NULL;
158 uint8_t interference_candidate = 0;
160 spin_lock_bh(&orig_node->neigh_list_lock);
162 /* only consider if it has the same primary address ... */
163 if (!compare_eth(orig_node->orig,
164 neigh_node->orig_node->primary_addr))
167 router = orig_node_get_router(orig_node);
171 /* ... and is good enough to be considered */
172 if (neigh_node->tq_avg < router->tq_avg - BONDING_TQ_THRESHOLD)
176 * check if we have another candidate with the same mac address or
177 * interface. If we do, we won't select this candidate because of
178 * possible interference.
180 hlist_for_each_entry_rcu(tmp_neigh_node, node,
181 &orig_node->neigh_list, list) {
183 if (tmp_neigh_node == neigh_node)
186 /* we only care if the other candidate is even
187 * considered as candidate. */
188 if (list_empty(&tmp_neigh_node->bonding_list))
191 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
192 (compare_eth(neigh_node->addr, tmp_neigh_node->addr))) {
193 interference_candidate = 1;
198 /* don't care further if it is an interference candidate */
199 if (interference_candidate)
202 /* this neighbor already is part of our candidate list */
203 if (!list_empty(&neigh_node->bonding_list))
206 if (!atomic_inc_not_zero(&neigh_node->refcount))
209 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
210 atomic_inc(&orig_node->bond_candidates);
214 bonding_candidate_del(orig_node, neigh_node);
217 spin_unlock_bh(&orig_node->neigh_list_lock);
220 neigh_node_free_ref(router);
223 /* copy primary address for bonding */
224 void bonding_save_primary(const struct orig_node *orig_node,
225 struct orig_node *orig_neigh_node,
226 const struct batman_ogm_packet *batman_ogm_packet)
228 if (!(batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
231 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
234 /* checks whether the host restarted and is in the protection time.
236 * 0 if the packet is to be accepted
237 * 1 if the packet is to be ignored.
239 int window_protected(struct bat_priv *bat_priv, int32_t seq_num_diff,
240 unsigned long *last_reset)
242 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
243 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
244 if (time_after(jiffies, *last_reset +
245 msecs_to_jiffies(RESET_PROTECTION_MS))) {
247 *last_reset = jiffies;
248 bat_dbg(DBG_BATMAN, bat_priv,
249 "old packet received, start protection\n");
258 int recv_bat_ogm_packet(struct sk_buff *skb, struct hard_iface *hard_iface)
260 struct ethhdr *ethhdr;
262 /* drop packet if it has not necessary minimum size */
263 if (unlikely(!pskb_may_pull(skb, BATMAN_OGM_LEN)))
266 ethhdr = (struct ethhdr *)skb_mac_header(skb);
268 /* packet with broadcast indication but unicast recipient */
269 if (!is_broadcast_ether_addr(ethhdr->h_dest))
272 /* packet with broadcast sender address */
273 if (is_broadcast_ether_addr(ethhdr->h_source))
276 /* create a copy of the skb, if needed, to modify it. */
277 if (skb_cow(skb, 0) < 0)
280 /* keep skb linear */
281 if (skb_linearize(skb) < 0)
284 ethhdr = (struct ethhdr *)skb_mac_header(skb);
286 bat_ogm_receive(ethhdr, skb->data, skb_headlen(skb), hard_iface);
289 return NET_RX_SUCCESS;
292 static int recv_my_icmp_packet(struct bat_priv *bat_priv,
293 struct sk_buff *skb, size_t icmp_len)
295 struct hard_iface *primary_if = NULL;
296 struct orig_node *orig_node = NULL;
297 struct neigh_node *router = NULL;
298 struct icmp_packet_rr *icmp_packet;
299 int ret = NET_RX_DROP;
301 icmp_packet = (struct icmp_packet_rr *)skb->data;
303 /* add data to device queue */
304 if (icmp_packet->msg_type != ECHO_REQUEST) {
305 bat_socket_receive_packet(icmp_packet, icmp_len);
309 primary_if = primary_if_get_selected(bat_priv);
313 /* answer echo request (ping) */
314 /* get routing information */
315 orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
319 router = orig_node_get_router(orig_node);
323 /* create a copy of the skb, if needed, to modify it. */
324 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
327 icmp_packet = (struct icmp_packet_rr *)skb->data;
329 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
330 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
331 icmp_packet->msg_type = ECHO_REPLY;
332 icmp_packet->ttl = TTL;
334 send_skb_packet(skb, router->if_incoming, router->addr);
335 ret = NET_RX_SUCCESS;
339 hardif_free_ref(primary_if);
341 neigh_node_free_ref(router);
343 orig_node_free_ref(orig_node);
347 static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
350 struct hard_iface *primary_if = NULL;
351 struct orig_node *orig_node = NULL;
352 struct neigh_node *router = NULL;
353 struct icmp_packet *icmp_packet;
354 int ret = NET_RX_DROP;
356 icmp_packet = (struct icmp_packet *)skb->data;
358 /* send TTL exceeded if packet is an echo request (traceroute) */
359 if (icmp_packet->msg_type != ECHO_REQUEST) {
360 pr_debug("Warning - can't forward icmp packet from %pM to "
361 "%pM: ttl exceeded\n", icmp_packet->orig,
366 primary_if = primary_if_get_selected(bat_priv);
370 /* get routing information */
371 orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
375 router = orig_node_get_router(orig_node);
379 /* create a copy of the skb, if needed, to modify it. */
380 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
383 icmp_packet = (struct icmp_packet *)skb->data;
385 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
386 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
387 icmp_packet->msg_type = TTL_EXCEEDED;
388 icmp_packet->ttl = TTL;
390 send_skb_packet(skb, router->if_incoming, router->addr);
391 ret = NET_RX_SUCCESS;
395 hardif_free_ref(primary_if);
397 neigh_node_free_ref(router);
399 orig_node_free_ref(orig_node);
404 int recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if)
406 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
407 struct icmp_packet_rr *icmp_packet;
408 struct ethhdr *ethhdr;
409 struct orig_node *orig_node = NULL;
410 struct neigh_node *router = NULL;
411 int hdr_size = sizeof(struct icmp_packet);
412 int ret = NET_RX_DROP;
415 * we truncate all incoming icmp packets if they don't match our size
417 if (skb->len >= sizeof(struct icmp_packet_rr))
418 hdr_size = sizeof(struct icmp_packet_rr);
420 /* drop packet if it has not necessary minimum size */
421 if (unlikely(!pskb_may_pull(skb, hdr_size)))
424 ethhdr = (struct ethhdr *)skb_mac_header(skb);
426 /* packet with unicast indication but broadcast recipient */
427 if (is_broadcast_ether_addr(ethhdr->h_dest))
430 /* packet with broadcast sender address */
431 if (is_broadcast_ether_addr(ethhdr->h_source))
435 if (!is_my_mac(ethhdr->h_dest))
438 icmp_packet = (struct icmp_packet_rr *)skb->data;
440 /* add record route information if not full */
441 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
442 (icmp_packet->rr_cur < BAT_RR_LEN)) {
443 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
444 ethhdr->h_dest, ETH_ALEN);
445 icmp_packet->rr_cur++;
449 if (is_my_mac(icmp_packet->dst))
450 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
453 if (icmp_packet->ttl < 2)
454 return recv_icmp_ttl_exceeded(bat_priv, skb);
456 /* get routing information */
457 orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
461 router = orig_node_get_router(orig_node);
465 /* create a copy of the skb, if needed, to modify it. */
466 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
469 icmp_packet = (struct icmp_packet_rr *)skb->data;
475 send_skb_packet(skb, router->if_incoming, router->addr);
476 ret = NET_RX_SUCCESS;
480 neigh_node_free_ref(router);
482 orig_node_free_ref(orig_node);
486 /* In the bonding case, send the packets in a round
487 * robin fashion over the remaining interfaces.
489 * This method rotates the bonding list and increases the
490 * returned router's refcount. */
491 static struct neigh_node *find_bond_router(struct orig_node *primary_orig,
492 const struct hard_iface *recv_if)
494 struct neigh_node *tmp_neigh_node;
495 struct neigh_node *router = NULL, *first_candidate = NULL;
498 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
500 if (!first_candidate)
501 first_candidate = tmp_neigh_node;
503 /* recv_if == NULL on the first node. */
504 if (tmp_neigh_node->if_incoming == recv_if)
507 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
510 router = tmp_neigh_node;
514 /* use the first candidate if nothing was found. */
515 if (!router && first_candidate &&
516 atomic_inc_not_zero(&first_candidate->refcount))
517 router = first_candidate;
522 /* selected should point to the next element
523 * after the current router */
524 spin_lock_bh(&primary_orig->neigh_list_lock);
525 /* this is a list_move(), which unfortunately
526 * does not exist as rcu version */
527 list_del_rcu(&primary_orig->bond_list);
528 list_add_rcu(&primary_orig->bond_list,
529 &router->bonding_list);
530 spin_unlock_bh(&primary_orig->neigh_list_lock);
537 /* Interface Alternating: Use the best of the
538 * remaining candidates which are not using
541 * Increases the returned router's refcount */
542 static struct neigh_node *find_ifalter_router(struct orig_node *primary_orig,
543 const struct hard_iface *recv_if)
545 struct neigh_node *tmp_neigh_node;
546 struct neigh_node *router = NULL, *first_candidate = NULL;
549 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
551 if (!first_candidate)
552 first_candidate = tmp_neigh_node;
554 /* recv_if == NULL on the first node. */
555 if (tmp_neigh_node->if_incoming == recv_if)
558 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
561 /* if we don't have a router yet
562 * or this one is better, choose it. */
564 (tmp_neigh_node->tq_avg > router->tq_avg)) {
565 /* decrement refcount of
566 * previously selected router */
568 neigh_node_free_ref(router);
570 router = tmp_neigh_node;
571 atomic_inc_not_zero(&router->refcount);
574 neigh_node_free_ref(tmp_neigh_node);
577 /* use the first candidate if nothing was found. */
578 if (!router && first_candidate &&
579 atomic_inc_not_zero(&first_candidate->refcount))
580 router = first_candidate;
586 int recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if)
588 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
589 struct tt_query_packet *tt_query;
590 struct ethhdr *ethhdr;
592 /* drop packet if it has not necessary minimum size */
593 if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet))))
596 /* I could need to modify it */
597 if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0)
600 ethhdr = (struct ethhdr *)skb_mac_header(skb);
602 /* packet with unicast indication but broadcast recipient */
603 if (is_broadcast_ether_addr(ethhdr->h_dest))
606 /* packet with broadcast sender address */
607 if (is_broadcast_ether_addr(ethhdr->h_source))
610 tt_query = (struct tt_query_packet *)skb->data;
612 tt_query->tt_data = ntohs(tt_query->tt_data);
614 switch (tt_query->flags & TT_QUERY_TYPE_MASK) {
616 /* If we cannot provide an answer the tt_request is
618 if (!send_tt_response(bat_priv, tt_query)) {
619 bat_dbg(DBG_TT, bat_priv,
620 "Routing TT_REQUEST to %pM [%c]\n",
622 (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
623 tt_query->tt_data = htons(tt_query->tt_data);
624 return route_unicast_packet(skb, recv_if);
628 /* packet needs to be linearized to access the TT changes */
629 if (skb_linearize(skb) < 0)
631 /* skb_linearize() possibly changed skb->data */
632 tt_query = (struct tt_query_packet *)skb->data;
634 if (is_my_mac(tt_query->dst))
635 handle_tt_response(bat_priv, tt_query);
637 bat_dbg(DBG_TT, bat_priv,
638 "Routing TT_RESPONSE to %pM [%c]\n",
640 (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
641 tt_query->tt_data = htons(tt_query->tt_data);
642 return route_unicast_packet(skb, recv_if);
648 /* returning NET_RX_DROP will make the caller function kfree the skb */
652 int recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
654 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
655 struct roam_adv_packet *roam_adv_packet;
656 struct orig_node *orig_node;
657 struct ethhdr *ethhdr;
659 /* drop packet if it has not necessary minimum size */
660 if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
663 ethhdr = (struct ethhdr *)skb_mac_header(skb);
665 /* packet with unicast indication but broadcast recipient */
666 if (is_broadcast_ether_addr(ethhdr->h_dest))
669 /* packet with broadcast sender address */
670 if (is_broadcast_ether_addr(ethhdr->h_source))
673 roam_adv_packet = (struct roam_adv_packet *)skb->data;
675 if (!is_my_mac(roam_adv_packet->dst))
676 return route_unicast_packet(skb, recv_if);
678 orig_node = orig_hash_find(bat_priv, roam_adv_packet->src);
682 bat_dbg(DBG_TT, bat_priv, "Received ROAMING_ADV from %pM "
683 "(client %pM)\n", roam_adv_packet->src,
684 roam_adv_packet->client);
686 tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
687 atomic_read(&orig_node->last_ttvn) + 1, true, false);
689 /* Roaming phase starts: I have new information but the ttvn has not
690 * been incremented yet. This flag will make me check all the incoming
691 * packets for the correct destination. */
692 bat_priv->tt_poss_change = true;
694 orig_node_free_ref(orig_node);
696 /* returning NET_RX_DROP will make the caller function kfree the skb */
700 /* find a suitable router for this originator, and use
701 * bonding if possible. increases the found neighbors
703 struct neigh_node *find_router(struct bat_priv *bat_priv,
704 struct orig_node *orig_node,
705 const struct hard_iface *recv_if)
707 struct orig_node *primary_orig_node;
708 struct orig_node *router_orig;
709 struct neigh_node *router;
710 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
716 router = orig_node_get_router(orig_node);
720 /* without bonding, the first node should
721 * always choose the default router. */
722 bonding_enabled = atomic_read(&bat_priv->bonding);
725 /* select default router to output */
726 router_orig = router->orig_node;
730 if ((!recv_if) && (!bonding_enabled))
733 /* if we have something in the primary_addr, we can search
734 * for a potential bonding candidate. */
735 if (compare_eth(router_orig->primary_addr, zero_mac))
738 /* find the orig_node which has the primary interface. might
739 * even be the same as our router_orig in many cases */
741 if (compare_eth(router_orig->primary_addr, router_orig->orig)) {
742 primary_orig_node = router_orig;
744 primary_orig_node = orig_hash_find(bat_priv,
745 router_orig->primary_addr);
746 if (!primary_orig_node)
749 orig_node_free_ref(primary_orig_node);
752 /* with less than 2 candidates, we can't do any
753 * bonding and prefer the original router. */
754 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
757 /* all nodes between should choose a candidate which
758 * is is not on the interface where the packet came
761 neigh_node_free_ref(router);
764 router = find_bond_router(primary_orig_node, recv_if);
766 router = find_ifalter_router(primary_orig_node, recv_if);
769 if (router && router->if_incoming->if_status != IF_ACTIVE)
778 neigh_node_free_ref(router);
782 static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
784 struct ethhdr *ethhdr;
786 /* drop packet if it has not necessary minimum size */
787 if (unlikely(!pskb_may_pull(skb, hdr_size)))
790 ethhdr = (struct ethhdr *)skb_mac_header(skb);
792 /* packet with unicast indication but broadcast recipient */
793 if (is_broadcast_ether_addr(ethhdr->h_dest))
796 /* packet with broadcast sender address */
797 if (is_broadcast_ether_addr(ethhdr->h_source))
801 if (!is_my_mac(ethhdr->h_dest))
807 int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
809 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
810 struct orig_node *orig_node = NULL;
811 struct neigh_node *neigh_node = NULL;
812 struct unicast_packet *unicast_packet;
813 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
814 int ret = NET_RX_DROP;
815 struct sk_buff *new_skb;
817 unicast_packet = (struct unicast_packet *)skb->data;
820 if (unicast_packet->ttl < 2) {
821 pr_debug("Warning - can't forward unicast packet from %pM to "
822 "%pM: ttl exceeded\n", ethhdr->h_source,
823 unicast_packet->dest);
827 /* get routing information */
828 orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
833 /* find_router() increases neigh_nodes refcount if found. */
834 neigh_node = find_router(bat_priv, orig_node, recv_if);
839 /* create a copy of the skb, if needed, to modify it. */
840 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
843 unicast_packet = (struct unicast_packet *)skb->data;
845 if (unicast_packet->packet_type == BAT_UNICAST &&
846 atomic_read(&bat_priv->fragmentation) &&
847 skb->len > neigh_node->if_incoming->net_dev->mtu) {
848 ret = frag_send_skb(skb, bat_priv,
849 neigh_node->if_incoming, neigh_node->addr);
853 if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
854 frag_can_reassemble(skb, neigh_node->if_incoming->net_dev->mtu)) {
856 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
858 if (ret == NET_RX_DROP)
861 /* packet was buffered for late merge */
863 ret = NET_RX_SUCCESS;
868 unicast_packet = (struct unicast_packet *)skb->data;
872 unicast_packet->ttl--;
875 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
876 ret = NET_RX_SUCCESS;
880 neigh_node_free_ref(neigh_node);
882 orig_node_free_ref(orig_node);
886 static int check_unicast_ttvn(struct bat_priv *bat_priv,
887 struct sk_buff *skb) {
889 struct orig_node *orig_node;
890 struct ethhdr *ethhdr;
891 struct hard_iface *primary_if;
892 struct unicast_packet *unicast_packet;
895 /* I could need to modify it */
896 if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
899 unicast_packet = (struct unicast_packet *)skb->data;
901 if (is_my_mac(unicast_packet->dest)) {
902 tt_poss_change = bat_priv->tt_poss_change;
903 curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
905 orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
910 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
911 tt_poss_change = orig_node->tt_poss_change;
912 orig_node_free_ref(orig_node);
915 /* Check whether I have to reroute the packet */
916 if (seq_before(unicast_packet->ttvn, curr_ttvn) || tt_poss_change) {
917 /* Linearize the skb before accessing it */
918 if (skb_linearize(skb) < 0)
921 ethhdr = (struct ethhdr *)(skb->data +
922 sizeof(struct unicast_packet));
923 orig_node = transtable_search(bat_priv, NULL, ethhdr->h_dest);
926 if (!is_my_client(bat_priv, ethhdr->h_dest))
928 primary_if = primary_if_get_selected(bat_priv);
931 memcpy(unicast_packet->dest,
932 primary_if->net_dev->dev_addr, ETH_ALEN);
933 hardif_free_ref(primary_if);
935 memcpy(unicast_packet->dest, orig_node->orig,
937 curr_ttvn = (uint8_t)
938 atomic_read(&orig_node->last_ttvn);
939 orig_node_free_ref(orig_node);
942 bat_dbg(DBG_ROUTES, bat_priv, "TTVN mismatch (old_ttvn %u "
943 "new_ttvn %u)! Rerouting unicast packet (for %pM) to "
944 "%pM\n", unicast_packet->ttvn, curr_ttvn,
945 ethhdr->h_dest, unicast_packet->dest);
947 unicast_packet->ttvn = curr_ttvn;
952 int recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
954 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
955 struct unicast_packet *unicast_packet;
956 int hdr_size = sizeof(*unicast_packet);
958 if (check_unicast_packet(skb, hdr_size) < 0)
961 if (!check_unicast_ttvn(bat_priv, skb))
964 unicast_packet = (struct unicast_packet *)skb->data;
967 if (is_my_mac(unicast_packet->dest)) {
968 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
969 return NET_RX_SUCCESS;
972 return route_unicast_packet(skb, recv_if);
975 int recv_ucast_frag_packet(struct sk_buff *skb, struct hard_iface *recv_if)
977 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
978 struct unicast_frag_packet *unicast_packet;
979 int hdr_size = sizeof(*unicast_packet);
980 struct sk_buff *new_skb = NULL;
983 if (check_unicast_packet(skb, hdr_size) < 0)
986 if (!check_unicast_ttvn(bat_priv, skb))
989 unicast_packet = (struct unicast_frag_packet *)skb->data;
992 if (is_my_mac(unicast_packet->dest)) {
994 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
996 if (ret == NET_RX_DROP)
999 /* packet was buffered for late merge */
1001 return NET_RX_SUCCESS;
1003 interface_rx(recv_if->soft_iface, new_skb, recv_if,
1004 sizeof(struct unicast_packet));
1005 return NET_RX_SUCCESS;
1008 return route_unicast_packet(skb, recv_if);
1012 int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1014 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1015 struct orig_node *orig_node = NULL;
1016 struct bcast_packet *bcast_packet;
1017 struct ethhdr *ethhdr;
1018 int hdr_size = sizeof(*bcast_packet);
1019 int ret = NET_RX_DROP;
1022 /* drop packet if it has not necessary minimum size */
1023 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1026 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1028 /* packet with broadcast indication but unicast recipient */
1029 if (!is_broadcast_ether_addr(ethhdr->h_dest))
1032 /* packet with broadcast sender address */
1033 if (is_broadcast_ether_addr(ethhdr->h_source))
1036 /* ignore broadcasts sent by myself */
1037 if (is_my_mac(ethhdr->h_source))
1040 bcast_packet = (struct bcast_packet *)skb->data;
1042 /* ignore broadcasts originated by myself */
1043 if (is_my_mac(bcast_packet->orig))
1046 if (bcast_packet->ttl < 2)
1049 orig_node = orig_hash_find(bat_priv, bcast_packet->orig);
1054 spin_lock_bh(&orig_node->bcast_seqno_lock);
1056 /* check whether the packet is a duplicate */
1057 if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1058 ntohl(bcast_packet->seqno)))
1061 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1063 /* check whether the packet is old and the host just restarted. */
1064 if (window_protected(bat_priv, seq_diff,
1065 &orig_node->bcast_seqno_reset))
1068 /* mark broadcast in flood history, update window position
1070 if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1071 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1073 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1075 /* rebroadcast packet */
1076 add_bcast_packet_to_list(bat_priv, skb, 1);
1078 /* broadcast for me */
1079 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1080 ret = NET_RX_SUCCESS;
1084 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1087 orig_node_free_ref(orig_node);
1091 int recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1093 struct vis_packet *vis_packet;
1094 struct ethhdr *ethhdr;
1095 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1096 int hdr_size = sizeof(*vis_packet);
1098 /* keep skb linear */
1099 if (skb_linearize(skb) < 0)
1102 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1105 vis_packet = (struct vis_packet *)skb->data;
1106 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1109 if (!is_my_mac(ethhdr->h_dest))
1112 /* ignore own packets */
1113 if (is_my_mac(vis_packet->vis_orig))
1116 if (is_my_mac(vis_packet->sender_orig))
1119 switch (vis_packet->vis_type) {
1120 case VIS_TYPE_SERVER_SYNC:
1121 receive_server_sync_packet(bat_priv, vis_packet,
1125 case VIS_TYPE_CLIENT_UPDATE:
1126 receive_client_update_packet(bat_priv, vis_packet,
1130 default: /* ignore unknown packet */
1134 /* We take a copy of the data in the packet, so we should
1135 always free the skbuf. */