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