Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1 | /* Copyright (C) 2007-2012 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" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 28 | #include "vis.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 29 | #include "unicast.h" |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 30 | #include "bridge_loop_avoidance.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, |
| 33 | struct hard_iface *recv_if); |
Simon Wunderlich | a7f6ee9 | 2012-01-22 20:00:18 +0100 | [diff] [blame] | 34 | |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 35 | void batadv_slide_own_bcast_window(struct hard_iface *hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 36 | { |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 37 | struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 38 | struct hashtable_t *hash = bat_priv->orig_hash; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 39 | struct hlist_node *node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 40 | struct hlist_head *head; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 41 | struct orig_node *orig_node; |
| 42 | unsigned long *word; |
Antonio Quartulli | c90681b | 2011-10-05 17:05:25 +0200 | [diff] [blame] | 43 | uint32_t i; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 44 | size_t word_index; |
| 45 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 46 | for (i = 0; i < hash->size; i++) { |
| 47 | head = &hash->table[i]; |
| 48 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 49 | rcu_read_lock(); |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 50 | hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) { |
Marek Lindner | 2ae2daf | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 51 | spin_lock_bh(&orig_node->ogm_cnt_lock); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 52 | word_index = hard_iface->if_num * NUM_WORDS; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 53 | word = &(orig_node->bcast_own[word_index]); |
| 54 | |
Sven Eckelmann | 0f5f9322 | 2012-05-12 02:09:25 +0200 | [diff] [blame] | 55 | batadv_bit_get_packet(bat_priv, word, 1, 0); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 56 | orig_node->bcast_own_sum[hard_iface->if_num] = |
Sven Eckelmann | 0079d2c | 2012-02-04 17:34:52 +0100 | [diff] [blame] | 57 | bitmap_weight(word, TQ_LOCAL_WINDOW_SIZE); |
Marek Lindner | 2ae2daf | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 58 | spin_unlock_bh(&orig_node->ogm_cnt_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 59 | } |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 60 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 61 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 64 | static void _batadv_update_route(struct bat_priv *bat_priv, |
| 65 | struct orig_node *orig_node, |
| 66 | struct neigh_node *neigh_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 67 | { |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 68 | struct neigh_node *curr_router; |
| 69 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 70 | curr_router = batadv_orig_node_get_router(orig_node); |
Marek Lindner | a8e7f4b | 2010-12-12 21:57:10 +0000 | [diff] [blame] | 71 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 72 | /* route deleted */ |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 73 | if ((curr_router) && (!neigh_node)) { |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 74 | batadv_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n", |
| 75 | orig_node->orig); |
Sven Eckelmann | 08c36d3 | 2012-05-12 02:09:39 +0200 | [diff] [blame] | 76 | batadv_tt_global_del_orig(bat_priv, orig_node, |
| 77 | "Deleted route towards originator"); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 78 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 79 | /* route added */ |
| 80 | } else if ((!curr_router) && (neigh_node)) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 81 | |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 82 | batadv_dbg(DBG_ROUTES, bat_priv, |
| 83 | "Adding route towards: %pM (via %pM)\n", |
| 84 | orig_node->orig, neigh_node->addr); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 85 | /* route changed */ |
Sven Eckelmann | bb899b8 | 2011-05-10 11:22:37 +0200 | [diff] [blame] | 86 | } else if (neigh_node && curr_router) { |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 87 | batadv_dbg(DBG_ROUTES, bat_priv, |
| 88 | "Changing route towards: %pM (now via %pM - was via %pM)\n", |
| 89 | orig_node->orig, neigh_node->addr, |
| 90 | curr_router->addr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 93 | if (curr_router) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 94 | batadv_neigh_node_free_ref(curr_router); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 95 | |
| 96 | /* increase refcount of new best neighbor */ |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 97 | if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount)) |
| 98 | neigh_node = NULL; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 99 | |
| 100 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 101 | rcu_assign_pointer(orig_node->router, neigh_node); |
| 102 | spin_unlock_bh(&orig_node->neigh_list_lock); |
| 103 | |
| 104 | /* decrease refcount of previous best neighbor */ |
| 105 | if (curr_router) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 106 | batadv_neigh_node_free_ref(curr_router); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 109 | void batadv_update_route(struct bat_priv *bat_priv, struct orig_node *orig_node, |
| 110 | struct neigh_node *neigh_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 111 | { |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 112 | struct neigh_node *router = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 113 | |
| 114 | if (!orig_node) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 115 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 116 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 117 | router = batadv_orig_node_get_router(orig_node); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 118 | |
| 119 | if (router != neigh_node) |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 120 | _batadv_update_route(bat_priv, orig_node, neigh_node); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 121 | |
| 122 | out: |
| 123 | if (router) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 124 | batadv_neigh_node_free_ref(router); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 127 | /* caller must hold the neigh_list_lock */ |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 128 | void batadv_bonding_candidate_del(struct orig_node *orig_node, |
| 129 | struct neigh_node *neigh_node) |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 130 | { |
| 131 | /* this neighbor is not part of our candidate list */ |
| 132 | if (list_empty(&neigh_node->bonding_list)) |
| 133 | goto out; |
| 134 | |
| 135 | list_del_rcu(&neigh_node->bonding_list); |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 136 | INIT_LIST_HEAD(&neigh_node->bonding_list); |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 137 | batadv_neigh_node_free_ref(neigh_node); |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 138 | atomic_dec(&orig_node->bond_candidates); |
| 139 | |
| 140 | out: |
| 141 | return; |
| 142 | } |
| 143 | |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 144 | void batadv_bonding_candidate_add(struct orig_node *orig_node, |
| 145 | struct neigh_node *neigh_node) |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 146 | { |
| 147 | struct hlist_node *node; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 148 | struct neigh_node *tmp_neigh_node, *router = NULL; |
| 149 | uint8_t interference_candidate = 0; |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 150 | |
| 151 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 152 | |
| 153 | /* only consider if it has the same primary address ... */ |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 154 | if (!batadv_compare_eth(orig_node->orig, |
| 155 | neigh_node->orig_node->primary_addr)) |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 156 | goto candidate_del; |
| 157 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 158 | router = batadv_orig_node_get_router(orig_node); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 159 | if (!router) |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 160 | goto candidate_del; |
| 161 | |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 162 | /* ... and is good enough to be considered */ |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 163 | if (neigh_node->tq_avg < router->tq_avg - BONDING_TQ_THRESHOLD) |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 164 | goto candidate_del; |
| 165 | |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 166 | /* check if we have another candidate with the same mac address or |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 167 | * interface. If we do, we won't select this candidate because of |
| 168 | * possible interference. |
| 169 | */ |
| 170 | hlist_for_each_entry_rcu(tmp_neigh_node, node, |
| 171 | &orig_node->neigh_list, list) { |
| 172 | |
| 173 | if (tmp_neigh_node == neigh_node) |
| 174 | continue; |
| 175 | |
| 176 | /* we only care if the other candidate is even |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 177 | * considered as candidate. |
| 178 | */ |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 179 | if (list_empty(&tmp_neigh_node->bonding_list)) |
| 180 | continue; |
| 181 | |
| 182 | if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) || |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 183 | (batadv_compare_eth(neigh_node->addr, |
| 184 | tmp_neigh_node->addr))) { |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 185 | interference_candidate = 1; |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /* don't care further if it is an interference candidate */ |
| 191 | if (interference_candidate) |
| 192 | goto candidate_del; |
| 193 | |
| 194 | /* this neighbor already is part of our candidate list */ |
| 195 | if (!list_empty(&neigh_node->bonding_list)) |
| 196 | goto out; |
| 197 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 198 | if (!atomic_inc_not_zero(&neigh_node->refcount)) |
| 199 | goto out; |
| 200 | |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 201 | list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list); |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 202 | atomic_inc(&orig_node->bond_candidates); |
| 203 | goto out; |
| 204 | |
| 205 | candidate_del: |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 206 | batadv_bonding_candidate_del(orig_node, neigh_node); |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 207 | |
| 208 | out: |
| 209 | spin_unlock_bh(&orig_node->neigh_list_lock); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 210 | |
| 211 | if (router) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 212 | batadv_neigh_node_free_ref(router); |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* copy primary address for bonding */ |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 216 | void |
| 217 | batadv_bonding_save_primary(const struct orig_node *orig_node, |
| 218 | struct orig_node *orig_neigh_node, |
| 219 | const struct batman_ogm_packet *batman_ogm_packet) |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 220 | { |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame] | 221 | if (!(batman_ogm_packet->flags & PRIMARIES_FIRST_HOP)) |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 222 | return; |
| 223 | |
| 224 | memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN); |
| 225 | } |
| 226 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 227 | /* checks whether the host restarted and is in the protection time. |
| 228 | * returns: |
| 229 | * 0 if the packet is to be accepted |
| 230 | * 1 if the packet is to be ignored. |
| 231 | */ |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 232 | int batadv_window_protected(struct bat_priv *bat_priv, int32_t seq_num_diff, |
| 233 | unsigned long *last_reset) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 234 | { |
Sven Eckelmann | 7c64fd9 | 2012-02-28 10:55:36 +0100 | [diff] [blame] | 235 | if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE) || |
| 236 | (seq_num_diff >= EXPECTED_SEQNO_RANGE)) { |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 237 | if (!batadv_has_timed_out(*last_reset, RESET_PROTECTION_MS)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 238 | return 1; |
Marek Lindner | 8c7bf24 | 2012-03-17 15:28:33 +0800 | [diff] [blame] | 239 | |
| 240 | *last_reset = jiffies; |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 241 | batadv_dbg(DBG_BATMAN, bat_priv, |
| 242 | "old packet received, start protection\n"); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 243 | } |
Marek Lindner | 8c7bf24 | 2012-03-17 15:28:33 +0800 | [diff] [blame] | 244 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 248 | bool batadv_check_management_packet(struct sk_buff *skb, |
| 249 | struct hard_iface *hard_iface, |
| 250 | int header_len) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 251 | { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 252 | struct ethhdr *ethhdr; |
| 253 | |
| 254 | /* drop packet if it has not necessary minimum size */ |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 255 | if (unlikely(!pskb_may_pull(skb, header_len))) |
| 256 | return false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 257 | |
| 258 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 259 | |
| 260 | /* packet with broadcast indication but unicast recipient */ |
| 261 | if (!is_broadcast_ether_addr(ethhdr->h_dest)) |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 262 | return false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 263 | |
| 264 | /* packet with broadcast sender address */ |
| 265 | if (is_broadcast_ether_addr(ethhdr->h_source)) |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 266 | return false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 267 | |
| 268 | /* create a copy of the skb, if needed, to modify it. */ |
| 269 | if (skb_cow(skb, 0) < 0) |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 270 | return false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 271 | |
| 272 | /* keep skb linear */ |
| 273 | if (skb_linearize(skb) < 0) |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 274 | return false; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 275 | |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 276 | return true; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 279 | static int batadv_recv_my_icmp_packet(struct bat_priv *bat_priv, |
| 280 | struct sk_buff *skb, size_t icmp_len) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 281 | { |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 282 | struct hard_iface *primary_if = NULL; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 283 | struct orig_node *orig_node = NULL; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 284 | struct neigh_node *router = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 285 | struct icmp_packet_rr *icmp_packet; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 286 | int ret = NET_RX_DROP; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 287 | |
| 288 | icmp_packet = (struct icmp_packet_rr *)skb->data; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 289 | |
| 290 | /* add data to device queue */ |
| 291 | if (icmp_packet->msg_type != ECHO_REQUEST) { |
Sven Eckelmann | 9039dc7 | 2012-05-12 02:09:33 +0200 | [diff] [blame] | 292 | batadv_socket_receive_packet(icmp_packet, icmp_len); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 293 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 296 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 297 | if (!primary_if) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 298 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 299 | |
| 300 | /* answer echo request (ping) */ |
| 301 | /* get routing information */ |
Sven Eckelmann | da64119 | 2012-05-12 13:48:56 +0200 | [diff] [blame] | 302 | orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 303 | if (!orig_node) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 304 | goto out; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 305 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 306 | router = batadv_orig_node_get_router(orig_node); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 307 | if (!router) |
| 308 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 309 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 310 | /* create a copy of the skb, if needed, to modify it. */ |
Antonio Quartulli | 0d12507 | 2012-02-18 11:27:34 +0100 | [diff] [blame] | 311 | if (skb_cow(skb, ETH_HLEN) < 0) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 312 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 313 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 314 | icmp_packet = (struct icmp_packet_rr *)skb->data; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 315 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 316 | memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 317 | memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 318 | icmp_packet->msg_type = ECHO_REPLY; |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 319 | icmp_packet->header.ttl = TTL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 320 | |
Sven Eckelmann | 9455e34 | 2012-05-12 02:09:37 +0200 | [diff] [blame] | 321 | batadv_send_skb_packet(skb, router->if_incoming, router->addr); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 322 | ret = NET_RX_SUCCESS; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 323 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 324 | out: |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 325 | if (primary_if) |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 326 | batadv_hardif_free_ref(primary_if); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 327 | if (router) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 328 | batadv_neigh_node_free_ref(router); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 329 | if (orig_node) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 330 | batadv_orig_node_free_ref(orig_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 331 | return ret; |
| 332 | } |
| 333 | |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 334 | static int batadv_recv_icmp_ttl_exceeded(struct bat_priv *bat_priv, |
| 335 | struct sk_buff *skb) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 336 | { |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 337 | struct hard_iface *primary_if = NULL; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 338 | struct orig_node *orig_node = NULL; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 339 | struct neigh_node *router = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 340 | struct icmp_packet *icmp_packet; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 341 | int ret = NET_RX_DROP; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 342 | |
| 343 | icmp_packet = (struct icmp_packet *)skb->data; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 344 | |
| 345 | /* send TTL exceeded if packet is an echo request (traceroute) */ |
| 346 | if (icmp_packet->msg_type != ECHO_REQUEST) { |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 347 | pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n", |
| 348 | icmp_packet->orig, icmp_packet->dst); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 349 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 352 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 353 | if (!primary_if) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 354 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 355 | |
| 356 | /* get routing information */ |
Sven Eckelmann | da64119 | 2012-05-12 13:48:56 +0200 | [diff] [blame] | 357 | orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 358 | if (!orig_node) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 359 | goto out; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 360 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 361 | router = batadv_orig_node_get_router(orig_node); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 362 | if (!router) |
| 363 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 364 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 365 | /* create a copy of the skb, if needed, to modify it. */ |
Antonio Quartulli | 0d12507 | 2012-02-18 11:27:34 +0100 | [diff] [blame] | 366 | if (skb_cow(skb, ETH_HLEN) < 0) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 367 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 368 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 369 | icmp_packet = (struct icmp_packet *)skb->data; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 370 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 371 | memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 372 | memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 373 | icmp_packet->msg_type = TTL_EXCEEDED; |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 374 | icmp_packet->header.ttl = TTL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 375 | |
Sven Eckelmann | 9455e34 | 2012-05-12 02:09:37 +0200 | [diff] [blame] | 376 | batadv_send_skb_packet(skb, router->if_incoming, router->addr); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 377 | ret = NET_RX_SUCCESS; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 378 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 379 | out: |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 380 | if (primary_if) |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 381 | batadv_hardif_free_ref(primary_if); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 382 | if (router) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 383 | batadv_neigh_node_free_ref(router); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 384 | if (orig_node) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 385 | batadv_orig_node_free_ref(orig_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 386 | return ret; |
| 387 | } |
| 388 | |
| 389 | |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 390 | int batadv_recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 391 | { |
| 392 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
| 393 | struct icmp_packet_rr *icmp_packet; |
| 394 | struct ethhdr *ethhdr; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 395 | struct orig_node *orig_node = NULL; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 396 | struct neigh_node *router = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 397 | int hdr_size = sizeof(struct icmp_packet); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 398 | int ret = NET_RX_DROP; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 399 | |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 400 | /* we truncate all incoming icmp packets if they don't match our size */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 401 | if (skb->len >= sizeof(struct icmp_packet_rr)) |
| 402 | hdr_size = sizeof(struct icmp_packet_rr); |
| 403 | |
| 404 | /* drop packet if it has not necessary minimum size */ |
| 405 | if (unlikely(!pskb_may_pull(skb, hdr_size))) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 406 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 407 | |
| 408 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 409 | |
| 410 | /* packet with unicast indication but broadcast recipient */ |
| 411 | if (is_broadcast_ether_addr(ethhdr->h_dest)) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 412 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 413 | |
| 414 | /* packet with broadcast sender address */ |
| 415 | if (is_broadcast_ether_addr(ethhdr->h_source)) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 416 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 417 | |
| 418 | /* not for me */ |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 419 | if (!batadv_is_my_mac(ethhdr->h_dest)) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 420 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 421 | |
| 422 | icmp_packet = (struct icmp_packet_rr *)skb->data; |
| 423 | |
| 424 | /* add record route information if not full */ |
| 425 | if ((hdr_size == sizeof(struct icmp_packet_rr)) && |
| 426 | (icmp_packet->rr_cur < BAT_RR_LEN)) { |
| 427 | memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]), |
Sven Eckelmann | 7c64fd9 | 2012-02-28 10:55:36 +0100 | [diff] [blame] | 428 | ethhdr->h_dest, ETH_ALEN); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 429 | icmp_packet->rr_cur++; |
| 430 | } |
| 431 | |
| 432 | /* packet for me */ |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 433 | if (batadv_is_my_mac(icmp_packet->dst)) |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 434 | return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 435 | |
| 436 | /* TTL exceeded */ |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 437 | if (icmp_packet->header.ttl < 2) |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 438 | return batadv_recv_icmp_ttl_exceeded(bat_priv, skb); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 439 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 440 | /* get routing information */ |
Sven Eckelmann | da64119 | 2012-05-12 13:48:56 +0200 | [diff] [blame] | 441 | orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 442 | if (!orig_node) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 443 | goto out; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 444 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 445 | router = batadv_orig_node_get_router(orig_node); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 446 | if (!router) |
| 447 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 448 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 449 | /* create a copy of the skb, if needed, to modify it. */ |
Antonio Quartulli | 0d12507 | 2012-02-18 11:27:34 +0100 | [diff] [blame] | 450 | if (skb_cow(skb, ETH_HLEN) < 0) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 451 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 452 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 453 | icmp_packet = (struct icmp_packet_rr *)skb->data; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 454 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 455 | /* decrement ttl */ |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 456 | icmp_packet->header.ttl--; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 457 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 458 | /* route it */ |
Sven Eckelmann | 9455e34 | 2012-05-12 02:09:37 +0200 | [diff] [blame] | 459 | batadv_send_skb_packet(skb, router->if_incoming, router->addr); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 460 | ret = NET_RX_SUCCESS; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 461 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 462 | out: |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 463 | if (router) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 464 | batadv_neigh_node_free_ref(router); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 465 | if (orig_node) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 466 | batadv_orig_node_free_ref(orig_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 467 | return ret; |
| 468 | } |
| 469 | |
Linus Lüssing | 5515862 | 2011-03-14 22:43:27 +0000 | [diff] [blame] | 470 | /* In the bonding case, send the packets in a round |
| 471 | * robin fashion over the remaining interfaces. |
| 472 | * |
| 473 | * This method rotates the bonding list and increases the |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 474 | * returned router's refcount. |
| 475 | */ |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 476 | static struct neigh_node * |
| 477 | batadv_find_bond_router(struct orig_node *primary_orig, |
| 478 | const struct hard_iface *recv_if) |
Linus Lüssing | 5515862 | 2011-03-14 22:43:27 +0000 | [diff] [blame] | 479 | { |
| 480 | struct neigh_node *tmp_neigh_node; |
| 481 | struct neigh_node *router = NULL, *first_candidate = NULL; |
| 482 | |
| 483 | rcu_read_lock(); |
| 484 | list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list, |
| 485 | bonding_list) { |
| 486 | if (!first_candidate) |
| 487 | first_candidate = tmp_neigh_node; |
| 488 | |
| 489 | /* recv_if == NULL on the first node. */ |
| 490 | if (tmp_neigh_node->if_incoming == recv_if) |
| 491 | continue; |
| 492 | |
| 493 | if (!atomic_inc_not_zero(&tmp_neigh_node->refcount)) |
| 494 | continue; |
| 495 | |
| 496 | router = tmp_neigh_node; |
| 497 | break; |
| 498 | } |
| 499 | |
| 500 | /* use the first candidate if nothing was found. */ |
| 501 | if (!router && first_candidate && |
| 502 | atomic_inc_not_zero(&first_candidate->refcount)) |
| 503 | router = first_candidate; |
| 504 | |
| 505 | if (!router) |
| 506 | goto out; |
| 507 | |
| 508 | /* selected should point to the next element |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 509 | * after the current router |
| 510 | */ |
Linus Lüssing | 5515862 | 2011-03-14 22:43:27 +0000 | [diff] [blame] | 511 | spin_lock_bh(&primary_orig->neigh_list_lock); |
| 512 | /* this is a list_move(), which unfortunately |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 513 | * does not exist as rcu version |
| 514 | */ |
Linus Lüssing | 5515862 | 2011-03-14 22:43:27 +0000 | [diff] [blame] | 515 | list_del_rcu(&primary_orig->bond_list); |
| 516 | list_add_rcu(&primary_orig->bond_list, |
| 517 | &router->bonding_list); |
| 518 | spin_unlock_bh(&primary_orig->neigh_list_lock); |
| 519 | |
| 520 | out: |
| 521 | rcu_read_unlock(); |
| 522 | return router; |
| 523 | } |
| 524 | |
| 525 | /* Interface Alternating: Use the best of the |
| 526 | * remaining candidates which are not using |
| 527 | * this interface. |
| 528 | * |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 529 | * Increases the returned router's refcount |
| 530 | */ |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 531 | static struct neigh_node * |
| 532 | batadv_find_ifalter_router(struct orig_node *primary_orig, |
| 533 | const struct hard_iface *recv_if) |
Linus Lüssing | 5515862 | 2011-03-14 22:43:27 +0000 | [diff] [blame] | 534 | { |
| 535 | struct neigh_node *tmp_neigh_node; |
| 536 | struct neigh_node *router = NULL, *first_candidate = NULL; |
| 537 | |
| 538 | rcu_read_lock(); |
| 539 | list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list, |
| 540 | bonding_list) { |
| 541 | if (!first_candidate) |
| 542 | first_candidate = tmp_neigh_node; |
| 543 | |
| 544 | /* recv_if == NULL on the first node. */ |
| 545 | if (tmp_neigh_node->if_incoming == recv_if) |
| 546 | continue; |
| 547 | |
| 548 | if (!atomic_inc_not_zero(&tmp_neigh_node->refcount)) |
| 549 | continue; |
| 550 | |
| 551 | /* if we don't have a router yet |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 552 | * or this one is better, choose it. |
| 553 | */ |
Linus Lüssing | 5515862 | 2011-03-14 22:43:27 +0000 | [diff] [blame] | 554 | if ((!router) || |
| 555 | (tmp_neigh_node->tq_avg > router->tq_avg)) { |
| 556 | /* decrement refcount of |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 557 | * previously selected router |
| 558 | */ |
Linus Lüssing | 5515862 | 2011-03-14 22:43:27 +0000 | [diff] [blame] | 559 | if (router) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 560 | batadv_neigh_node_free_ref(router); |
Linus Lüssing | 5515862 | 2011-03-14 22:43:27 +0000 | [diff] [blame] | 561 | |
| 562 | router = tmp_neigh_node; |
| 563 | atomic_inc_not_zero(&router->refcount); |
| 564 | } |
| 565 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 566 | batadv_neigh_node_free_ref(tmp_neigh_node); |
Linus Lüssing | 5515862 | 2011-03-14 22:43:27 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | /* use the first candidate if nothing was found. */ |
| 570 | if (!router && first_candidate && |
| 571 | atomic_inc_not_zero(&first_candidate->refcount)) |
| 572 | router = first_candidate; |
| 573 | |
| 574 | rcu_read_unlock(); |
| 575 | return router; |
| 576 | } |
| 577 | |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 578 | int batadv_recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 579 | { |
| 580 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
| 581 | struct tt_query_packet *tt_query; |
Al Viro | f25bd58 | 2012-04-22 07:44:27 +0100 | [diff] [blame] | 582 | uint16_t tt_size; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 583 | struct ethhdr *ethhdr; |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 584 | char tt_flag; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 585 | |
| 586 | /* drop packet if it has not necessary minimum size */ |
| 587 | if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet)))) |
| 588 | goto out; |
| 589 | |
| 590 | /* I could need to modify it */ |
| 591 | if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0) |
| 592 | goto out; |
| 593 | |
| 594 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 595 | |
| 596 | /* packet with unicast indication but broadcast recipient */ |
| 597 | if (is_broadcast_ether_addr(ethhdr->h_dest)) |
| 598 | goto out; |
| 599 | |
| 600 | /* packet with broadcast sender address */ |
| 601 | if (is_broadcast_ether_addr(ethhdr->h_source)) |
| 602 | goto out; |
| 603 | |
| 604 | tt_query = (struct tt_query_packet *)skb->data; |
| 605 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 606 | switch (tt_query->flags & TT_QUERY_TYPE_MASK) { |
| 607 | case TT_REQUEST: |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 608 | batadv_inc_counter(bat_priv, BAT_CNT_TT_REQUEST_RX); |
| 609 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 610 | /* If we cannot provide an answer the tt_request is |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 611 | * forwarded |
| 612 | */ |
Sven Eckelmann | 08c36d3 | 2012-05-12 02:09:39 +0200 | [diff] [blame] | 613 | if (!batadv_send_tt_response(bat_priv, tt_query)) { |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 614 | tt_flag = tt_query->flags & TT_FULL_TABLE ? 'F' : '.'; |
| 615 | batadv_dbg(DBG_TT, bat_priv, |
| 616 | "Routing TT_REQUEST to %pM [%c]\n", |
| 617 | tt_query->dst, |
| 618 | tt_flag); |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 619 | return batadv_route_unicast_packet(skb, recv_if); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 620 | } |
| 621 | break; |
| 622 | case TT_RESPONSE: |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 623 | batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_RX); |
| 624 | |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 625 | if (batadv_is_my_mac(tt_query->dst)) { |
Antonio Quartulli | dc58fe3 | 2011-10-16 20:32:02 +0200 | [diff] [blame] | 626 | /* packet needs to be linearized to access the TT |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 627 | * changes |
| 628 | */ |
Antonio Quartulli | dc58fe3 | 2011-10-16 20:32:02 +0200 | [diff] [blame] | 629 | if (skb_linearize(skb) < 0) |
| 630 | goto out; |
Antonio Quartulli | d2b6cc8 | 2012-06-14 22:21:28 +0200 | [diff] [blame] | 631 | /* skb_linearize() possibly changed skb->data */ |
| 632 | tt_query = (struct tt_query_packet *)skb->data; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 633 | |
Sven Eckelmann | 08c36d3 | 2012-05-12 02:09:39 +0200 | [diff] [blame] | 634 | tt_size = batadv_tt_len(ntohs(tt_query->tt_data)); |
Antonio Quartulli | 8b7342d | 2011-10-16 20:32:03 +0200 | [diff] [blame] | 635 | |
| 636 | /* Ensure we have all the claimed data */ |
| 637 | if (unlikely(skb_headlen(skb) < |
Al Viro | f25bd58 | 2012-04-22 07:44:27 +0100 | [diff] [blame] | 638 | sizeof(struct tt_query_packet) + tt_size)) |
Antonio Quartulli | 8b7342d | 2011-10-16 20:32:03 +0200 | [diff] [blame] | 639 | goto out; |
| 640 | |
Sven Eckelmann | 08c36d3 | 2012-05-12 02:09:39 +0200 | [diff] [blame] | 641 | batadv_handle_tt_response(bat_priv, tt_query); |
Antonio Quartulli | dc58fe3 | 2011-10-16 20:32:02 +0200 | [diff] [blame] | 642 | } else { |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 643 | tt_flag = tt_query->flags & TT_FULL_TABLE ? 'F' : '.'; |
| 644 | batadv_dbg(DBG_TT, bat_priv, |
| 645 | "Routing TT_RESPONSE to %pM [%c]\n", |
| 646 | tt_query->dst, |
| 647 | tt_flag); |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 648 | return batadv_route_unicast_packet(skb, recv_if); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 649 | } |
| 650 | break; |
| 651 | } |
| 652 | |
| 653 | out: |
| 654 | /* returning NET_RX_DROP will make the caller function kfree the skb */ |
| 655 | return NET_RX_DROP; |
| 656 | } |
| 657 | |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 658 | int batadv_recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if) |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 659 | { |
| 660 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
| 661 | struct roam_adv_packet *roam_adv_packet; |
| 662 | struct orig_node *orig_node; |
| 663 | struct ethhdr *ethhdr; |
| 664 | |
| 665 | /* drop packet if it has not necessary minimum size */ |
| 666 | if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet)))) |
| 667 | goto out; |
| 668 | |
| 669 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 670 | |
| 671 | /* packet with unicast indication but broadcast recipient */ |
| 672 | if (is_broadcast_ether_addr(ethhdr->h_dest)) |
| 673 | goto out; |
| 674 | |
| 675 | /* packet with broadcast sender address */ |
| 676 | if (is_broadcast_ether_addr(ethhdr->h_source)) |
| 677 | goto out; |
| 678 | |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 679 | batadv_inc_counter(bat_priv, BAT_CNT_TT_ROAM_ADV_RX); |
| 680 | |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 681 | roam_adv_packet = (struct roam_adv_packet *)skb->data; |
| 682 | |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 683 | if (!batadv_is_my_mac(roam_adv_packet->dst)) |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 684 | return batadv_route_unicast_packet(skb, recv_if); |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 685 | |
Simon Wunderlich | 20ff9d5 | 2012-01-22 20:00:23 +0100 | [diff] [blame] | 686 | /* check if it is a backbone gateway. we don't accept |
| 687 | * roaming advertisement from it, as it has the same |
| 688 | * entries as we have. |
| 689 | */ |
Sven Eckelmann | 08adf15 | 2012-05-12 13:38:47 +0200 | [diff] [blame] | 690 | if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src)) |
Simon Wunderlich | 20ff9d5 | 2012-01-22 20:00:23 +0100 | [diff] [blame] | 691 | goto out; |
| 692 | |
Sven Eckelmann | da64119 | 2012-05-12 13:48:56 +0200 | [diff] [blame] | 693 | orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src); |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 694 | if (!orig_node) |
| 695 | goto out; |
| 696 | |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 697 | batadv_dbg(DBG_TT, bat_priv, |
| 698 | "Received ROAMING_ADV from %pM (client %pM)\n", |
| 699 | roam_adv_packet->src, roam_adv_packet->client); |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 700 | |
Sven Eckelmann | 08c36d3 | 2012-05-12 02:09:39 +0200 | [diff] [blame] | 701 | batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client, |
Antonio Quartulli | d4f4469 | 2012-05-25 00:00:54 +0200 | [diff] [blame] | 702 | TT_CLIENT_ROAM, |
| 703 | atomic_read(&orig_node->last_ttvn) + 1); |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 704 | |
| 705 | /* Roaming phase starts: I have new information but the ttvn has not |
| 706 | * been incremented yet. This flag will make me check all the incoming |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 707 | * packets for the correct destination. |
| 708 | */ |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 709 | bat_priv->tt_poss_change = true; |
| 710 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 711 | batadv_orig_node_free_ref(orig_node); |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 712 | out: |
| 713 | /* returning NET_RX_DROP will make the caller function kfree the skb */ |
| 714 | return NET_RX_DROP; |
| 715 | } |
| 716 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 717 | /* find a suitable router for this originator, and use |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 718 | * bonding if possible. increases the found neighbors |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 719 | * refcount. |
| 720 | */ |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 721 | struct neigh_node *batadv_find_router(struct bat_priv *bat_priv, |
| 722 | struct orig_node *orig_node, |
| 723 | const struct hard_iface *recv_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 724 | { |
| 725 | struct orig_node *primary_orig_node; |
| 726 | struct orig_node *router_orig; |
Linus Lüssing | 5515862 | 2011-03-14 22:43:27 +0000 | [diff] [blame] | 727 | struct neigh_node *router; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 728 | static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0}; |
| 729 | int bonding_enabled; |
Sven Eckelmann | da64119 | 2012-05-12 13:48:56 +0200 | [diff] [blame] | 730 | uint8_t *primary_addr; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 731 | |
| 732 | if (!orig_node) |
| 733 | return NULL; |
| 734 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 735 | router = batadv_orig_node_get_router(orig_node); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 736 | if (!router) |
Marek Lindner | 01df2b6 | 2011-05-05 14:14:46 +0200 | [diff] [blame] | 737 | goto err; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 738 | |
| 739 | /* without bonding, the first node should |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 740 | * always choose the default router. |
| 741 | */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 742 | bonding_enabled = atomic_read(&bat_priv->bonding); |
| 743 | |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 744 | rcu_read_lock(); |
| 745 | /* select default router to output */ |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 746 | router_orig = router->orig_node; |
Marek Lindner | 01df2b6 | 2011-05-05 14:14:46 +0200 | [diff] [blame] | 747 | if (!router_orig) |
| 748 | goto err_unlock; |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 749 | |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 750 | if ((!recv_if) && (!bonding_enabled)) |
| 751 | goto return_router; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 752 | |
Sven Eckelmann | da64119 | 2012-05-12 13:48:56 +0200 | [diff] [blame] | 753 | primary_addr = router_orig->primary_addr; |
| 754 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 755 | /* if we have something in the primary_addr, we can search |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 756 | * for a potential bonding candidate. |
| 757 | */ |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 758 | if (batadv_compare_eth(primary_addr, zero_mac)) |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 759 | goto return_router; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 760 | |
| 761 | /* find the orig_node which has the primary interface. might |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 762 | * even be the same as our router_orig in many cases |
| 763 | */ |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 764 | if (batadv_compare_eth(primary_addr, router_orig->orig)) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 765 | primary_orig_node = router_orig; |
| 766 | } else { |
Sven Eckelmann | da64119 | 2012-05-12 13:48:56 +0200 | [diff] [blame] | 767 | primary_orig_node = batadv_orig_hash_find(bat_priv, |
| 768 | primary_addr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 769 | if (!primary_orig_node) |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 770 | goto return_router; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 771 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 772 | batadv_orig_node_free_ref(primary_orig_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | /* with less than 2 candidates, we can't do any |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 776 | * bonding and prefer the original router. |
| 777 | */ |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 778 | if (atomic_read(&primary_orig_node->bond_candidates) < 2) |
| 779 | goto return_router; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 780 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 781 | /* all nodes between should choose a candidate which |
| 782 | * is is not on the interface where the packet came |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 783 | * in. |
| 784 | */ |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 785 | batadv_neigh_node_free_ref(router); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 786 | |
Linus Lüssing | 5515862 | 2011-03-14 22:43:27 +0000 | [diff] [blame] | 787 | if (bonding_enabled) |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 788 | router = batadv_find_bond_router(primary_orig_node, recv_if); |
Linus Lüssing | 5515862 | 2011-03-14 22:43:27 +0000 | [diff] [blame] | 789 | else |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 790 | router = batadv_find_ifalter_router(primary_orig_node, recv_if); |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 791 | |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 792 | return_router: |
Antonio Quartulli | e2cbc11 | 2011-05-08 20:52:57 +0200 | [diff] [blame] | 793 | if (router && router->if_incoming->if_status != IF_ACTIVE) |
| 794 | goto err_unlock; |
| 795 | |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 796 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 797 | return router; |
Marek Lindner | 01df2b6 | 2011-05-05 14:14:46 +0200 | [diff] [blame] | 798 | err_unlock: |
| 799 | rcu_read_unlock(); |
| 800 | err: |
| 801 | if (router) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 802 | batadv_neigh_node_free_ref(router); |
Marek Lindner | 01df2b6 | 2011-05-05 14:14:46 +0200 | [diff] [blame] | 803 | return NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 806 | static int batadv_check_unicast_packet(struct sk_buff *skb, int hdr_size) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 807 | { |
| 808 | struct ethhdr *ethhdr; |
| 809 | |
| 810 | /* drop packet if it has not necessary minimum size */ |
| 811 | if (unlikely(!pskb_may_pull(skb, hdr_size))) |
| 812 | return -1; |
| 813 | |
| 814 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 815 | |
| 816 | /* packet with unicast indication but broadcast recipient */ |
| 817 | if (is_broadcast_ether_addr(ethhdr->h_dest)) |
| 818 | return -1; |
| 819 | |
| 820 | /* packet with broadcast sender address */ |
| 821 | if (is_broadcast_ether_addr(ethhdr->h_source)) |
| 822 | return -1; |
| 823 | |
| 824 | /* not for me */ |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 825 | if (!batadv_is_my_mac(ethhdr->h_dest)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 826 | return -1; |
| 827 | |
| 828 | return 0; |
| 829 | } |
| 830 | |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 831 | static int batadv_route_unicast_packet(struct sk_buff *skb, |
| 832 | struct hard_iface *recv_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 833 | { |
| 834 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 835 | struct orig_node *orig_node = NULL; |
| 836 | struct neigh_node *neigh_node = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 837 | struct unicast_packet *unicast_packet; |
| 838 | struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 839 | int ret = NET_RX_DROP; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 840 | struct sk_buff *new_skb; |
| 841 | |
| 842 | unicast_packet = (struct unicast_packet *)skb->data; |
| 843 | |
| 844 | /* TTL exceeded */ |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 845 | if (unicast_packet->header.ttl < 2) { |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 846 | pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n", |
| 847 | ethhdr->h_source, unicast_packet->dest); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 848 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | /* get routing information */ |
Sven Eckelmann | da64119 | 2012-05-12 13:48:56 +0200 | [diff] [blame] | 852 | orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest); |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 853 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 854 | if (!orig_node) |
Antonio Quartulli | b5a6f69 | 2011-04-16 11:30:57 +0200 | [diff] [blame] | 855 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 856 | |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 857 | /* find_router() increases neigh_nodes refcount if found. */ |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 858 | neigh_node = batadv_find_router(bat_priv, orig_node, recv_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 859 | |
Marek Lindner | d007260 | 2011-01-19 20:01:44 +0000 | [diff] [blame] | 860 | if (!neigh_node) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 861 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 862 | |
| 863 | /* create a copy of the skb, if needed, to modify it. */ |
Antonio Quartulli | 0d12507 | 2012-02-18 11:27:34 +0100 | [diff] [blame] | 864 | if (skb_cow(skb, ETH_HLEN) < 0) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 865 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 866 | |
| 867 | unicast_packet = (struct unicast_packet *)skb->data; |
| 868 | |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 869 | if (unicast_packet->header.packet_type == BAT_UNICAST && |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 870 | atomic_read(&bat_priv->fragmentation) && |
Marek Lindner | d007260 | 2011-01-19 20:01:44 +0000 | [diff] [blame] | 871 | skb->len > neigh_node->if_incoming->net_dev->mtu) { |
Sven Eckelmann | 88ed1e77 | 2012-05-12 02:09:40 +0200 | [diff] [blame] | 872 | ret = batadv_frag_send_skb(skb, bat_priv, |
| 873 | neigh_node->if_incoming, |
| 874 | neigh_node->addr); |
Marek Lindner | d007260 | 2011-01-19 20:01:44 +0000 | [diff] [blame] | 875 | goto out; |
| 876 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 877 | |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 878 | if (unicast_packet->header.packet_type == BAT_UNICAST_FRAG && |
Sven Eckelmann | f0530ee | 2012-05-12 13:48:57 +0200 | [diff] [blame] | 879 | batadv_frag_can_reassemble(skb, |
| 880 | neigh_node->if_incoming->net_dev->mtu)) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 881 | |
Sven Eckelmann | 88ed1e77 | 2012-05-12 02:09:40 +0200 | [diff] [blame] | 882 | ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 883 | |
| 884 | if (ret == NET_RX_DROP) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 885 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 886 | |
| 887 | /* packet was buffered for late merge */ |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 888 | if (!new_skb) { |
| 889 | ret = NET_RX_SUCCESS; |
| 890 | goto out; |
| 891 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 892 | |
| 893 | skb = new_skb; |
| 894 | unicast_packet = (struct unicast_packet *)skb->data; |
| 895 | } |
| 896 | |
| 897 | /* decrement ttl */ |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 898 | unicast_packet->header.ttl--; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 899 | |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 900 | /* Update stats counter */ |
| 901 | batadv_inc_counter(bat_priv, BAT_CNT_FORWARD); |
| 902 | batadv_add_counter(bat_priv, BAT_CNT_FORWARD_BYTES, |
| 903 | skb->len + ETH_HLEN); |
| 904 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 905 | /* route it */ |
Sven Eckelmann | 9455e34 | 2012-05-12 02:09:37 +0200 | [diff] [blame] | 906 | batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 907 | ret = NET_RX_SUCCESS; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 908 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 909 | out: |
| 910 | if (neigh_node) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 911 | batadv_neigh_node_free_ref(neigh_node); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 912 | if (orig_node) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 913 | batadv_orig_node_free_ref(orig_node); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 914 | return ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 917 | static int batadv_check_unicast_ttvn(struct bat_priv *bat_priv, |
| 918 | struct sk_buff *skb) { |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 919 | uint8_t curr_ttvn; |
| 920 | struct orig_node *orig_node; |
| 921 | struct ethhdr *ethhdr; |
| 922 | struct hard_iface *primary_if; |
| 923 | struct unicast_packet *unicast_packet; |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 924 | bool tt_poss_change; |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 925 | int is_old_ttvn; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 926 | |
| 927 | /* I could need to modify it */ |
| 928 | if (skb_cow(skb, sizeof(struct unicast_packet)) < 0) |
| 929 | return 0; |
| 930 | |
| 931 | unicast_packet = (struct unicast_packet *)skb->data; |
| 932 | |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 933 | if (batadv_is_my_mac(unicast_packet->dest)) { |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 934 | tt_poss_change = bat_priv->tt_poss_change; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 935 | curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn); |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 936 | } else { |
Sven Eckelmann | da64119 | 2012-05-12 13:48:56 +0200 | [diff] [blame] | 937 | orig_node = batadv_orig_hash_find(bat_priv, |
| 938 | unicast_packet->dest); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 939 | |
| 940 | if (!orig_node) |
| 941 | return 0; |
| 942 | |
| 943 | curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn); |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 944 | tt_poss_change = orig_node->tt_poss_change; |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 945 | batadv_orig_node_free_ref(orig_node); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | /* Check whether I have to reroute the packet */ |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 949 | is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn); |
| 950 | if (is_old_ttvn || tt_poss_change) { |
Antonio Quartulli | 8710e26 | 2012-03-16 11:52:31 +0100 | [diff] [blame] | 951 | /* check if there is enough data before accessing it */ |
| 952 | if (pskb_may_pull(skb, sizeof(struct unicast_packet) + |
| 953 | ETH_HLEN) < 0) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 954 | return 0; |
| 955 | |
| 956 | ethhdr = (struct ethhdr *)(skb->data + |
| 957 | sizeof(struct unicast_packet)); |
Antonio Quartulli | 3275e7c | 2012-03-16 18:03:28 +0100 | [diff] [blame] | 958 | |
| 959 | /* we don't have an updated route for this client, so we should |
| 960 | * not try to reroute the packet!! |
| 961 | */ |
Sven Eckelmann | 08c36d3 | 2012-05-12 02:09:39 +0200 | [diff] [blame] | 962 | if (batadv_tt_global_client_is_roaming(bat_priv, |
| 963 | ethhdr->h_dest)) |
Antonio Quartulli | 3275e7c | 2012-03-16 18:03:28 +0100 | [diff] [blame] | 964 | return 1; |
| 965 | |
Sven Eckelmann | 08c36d3 | 2012-05-12 02:09:39 +0200 | [diff] [blame] | 966 | orig_node = batadv_transtable_search(bat_priv, NULL, |
| 967 | ethhdr->h_dest); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 968 | |
| 969 | if (!orig_node) { |
Sven Eckelmann | 08c36d3 | 2012-05-12 02:09:39 +0200 | [diff] [blame] | 970 | if (!batadv_is_my_client(bat_priv, ethhdr->h_dest)) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 971 | return 0; |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 972 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 973 | if (!primary_if) |
| 974 | return 0; |
| 975 | memcpy(unicast_packet->dest, |
| 976 | primary_if->net_dev->dev_addr, ETH_ALEN); |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 977 | batadv_hardif_free_ref(primary_if); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 978 | } else { |
| 979 | memcpy(unicast_packet->dest, orig_node->orig, |
| 980 | ETH_ALEN); |
| 981 | curr_ttvn = (uint8_t) |
| 982 | atomic_read(&orig_node->last_ttvn); |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 983 | batadv_orig_node_free_ref(orig_node); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 984 | } |
| 985 | |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 986 | batadv_dbg(DBG_ROUTES, bat_priv, |
| 987 | "TTVN mismatch (old_ttvn %u new_ttvn %u)! Rerouting unicast packet (for %pM) to %pM\n", |
| 988 | unicast_packet->ttvn, curr_ttvn, ethhdr->h_dest, |
| 989 | unicast_packet->dest); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 990 | |
| 991 | unicast_packet->ttvn = curr_ttvn; |
| 992 | } |
| 993 | return 1; |
| 994 | } |
| 995 | |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 996 | int batadv_recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 997 | { |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 998 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 999 | struct unicast_packet *unicast_packet; |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 1000 | int hdr_size = sizeof(*unicast_packet); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1001 | |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 1002 | if (batadv_check_unicast_packet(skb, hdr_size) < 0) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1003 | return NET_RX_DROP; |
| 1004 | |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 1005 | if (!batadv_check_unicast_ttvn(bat_priv, skb)) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1006 | return NET_RX_DROP; |
| 1007 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1008 | unicast_packet = (struct unicast_packet *)skb->data; |
| 1009 | |
| 1010 | /* packet for me */ |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 1011 | if (batadv_is_my_mac(unicast_packet->dest)) { |
Sven Eckelmann | 04b482a | 2012-05-12 02:09:38 +0200 | [diff] [blame] | 1012 | batadv_interface_rx(recv_if->soft_iface, skb, recv_if, |
| 1013 | hdr_size); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1014 | return NET_RX_SUCCESS; |
| 1015 | } |
| 1016 | |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 1017 | return batadv_route_unicast_packet(skb, recv_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 1020 | int batadv_recv_ucast_frag_packet(struct sk_buff *skb, |
| 1021 | struct hard_iface *recv_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1022 | { |
| 1023 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
| 1024 | struct unicast_frag_packet *unicast_packet; |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 1025 | int hdr_size = sizeof(*unicast_packet); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1026 | struct sk_buff *new_skb = NULL; |
| 1027 | int ret; |
| 1028 | |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 1029 | if (batadv_check_unicast_packet(skb, hdr_size) < 0) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1030 | return NET_RX_DROP; |
| 1031 | |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 1032 | if (!batadv_check_unicast_ttvn(bat_priv, skb)) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1033 | return NET_RX_DROP; |
| 1034 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1035 | unicast_packet = (struct unicast_frag_packet *)skb->data; |
| 1036 | |
| 1037 | /* packet for me */ |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 1038 | if (batadv_is_my_mac(unicast_packet->dest)) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1039 | |
Sven Eckelmann | 88ed1e77 | 2012-05-12 02:09:40 +0200 | [diff] [blame] | 1040 | ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1041 | |
| 1042 | if (ret == NET_RX_DROP) |
| 1043 | return NET_RX_DROP; |
| 1044 | |
| 1045 | /* packet was buffered for late merge */ |
| 1046 | if (!new_skb) |
| 1047 | return NET_RX_SUCCESS; |
| 1048 | |
Sven Eckelmann | 04b482a | 2012-05-12 02:09:38 +0200 | [diff] [blame] | 1049 | batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if, |
| 1050 | sizeof(struct unicast_packet)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1051 | return NET_RX_SUCCESS; |
| 1052 | } |
| 1053 | |
Sven Eckelmann | 63b0103 | 2012-05-16 20:23:13 +0200 | [diff] [blame] | 1054 | return batadv_route_unicast_packet(skb, recv_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 1058 | int batadv_recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1059 | { |
| 1060 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1061 | struct orig_node *orig_node = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1062 | struct bcast_packet *bcast_packet; |
| 1063 | struct ethhdr *ethhdr; |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 1064 | int hdr_size = sizeof(*bcast_packet); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1065 | int ret = NET_RX_DROP; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1066 | int32_t seq_diff; |
| 1067 | |
| 1068 | /* drop packet if it has not necessary minimum size */ |
| 1069 | if (unlikely(!pskb_may_pull(skb, hdr_size))) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1070 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1071 | |
| 1072 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 1073 | |
| 1074 | /* packet with broadcast indication but unicast recipient */ |
| 1075 | if (!is_broadcast_ether_addr(ethhdr->h_dest)) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1076 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1077 | |
| 1078 | /* packet with broadcast sender address */ |
| 1079 | if (is_broadcast_ether_addr(ethhdr->h_source)) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1080 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1081 | |
| 1082 | /* ignore broadcasts sent by myself */ |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 1083 | if (batadv_is_my_mac(ethhdr->h_source)) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1084 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1085 | |
| 1086 | bcast_packet = (struct bcast_packet *)skb->data; |
| 1087 | |
| 1088 | /* ignore broadcasts originated by myself */ |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 1089 | if (batadv_is_my_mac(bcast_packet->orig)) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1090 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1091 | |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 1092 | if (bcast_packet->header.ttl < 2) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1093 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1094 | |
Sven Eckelmann | da64119 | 2012-05-12 13:48:56 +0200 | [diff] [blame] | 1095 | orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1096 | |
| 1097 | if (!orig_node) |
Antonio Quartulli | b5a6f69 | 2011-04-16 11:30:57 +0200 | [diff] [blame] | 1098 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1099 | |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1100 | spin_lock_bh(&orig_node->bcast_seqno_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1101 | |
| 1102 | /* check whether the packet is a duplicate */ |
Sven Eckelmann | 9b4a115 | 2012-05-12 13:48:53 +0200 | [diff] [blame] | 1103 | if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno, |
| 1104 | ntohl(bcast_packet->seqno))) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1105 | goto spin_unlock; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1106 | |
| 1107 | seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno; |
| 1108 | |
| 1109 | /* check whether the packet is old and the host just restarted. */ |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 1110 | if (batadv_window_protected(bat_priv, seq_diff, |
| 1111 | &orig_node->bcast_seqno_reset)) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1112 | goto spin_unlock; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1113 | |
| 1114 | /* mark broadcast in flood history, update window position |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1115 | * if required. |
| 1116 | */ |
Sven Eckelmann | 0f5f9322 | 2012-05-12 02:09:25 +0200 | [diff] [blame] | 1117 | 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] | 1118 | orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno); |
| 1119 | |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1120 | spin_unlock_bh(&orig_node->bcast_seqno_lock); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1121 | |
Simon Wunderlich | fe2da6f | 2012-01-22 20:00:24 +0100 | [diff] [blame] | 1122 | /* check whether this has been sent by another originator before */ |
Sven Eckelmann | 08adf15 | 2012-05-12 13:38:47 +0200 | [diff] [blame] | 1123 | if (batadv_bla_check_bcast_duplist(bat_priv, bcast_packet, hdr_size)) |
Simon Wunderlich | fe2da6f | 2012-01-22 20:00:24 +0100 | [diff] [blame] | 1124 | goto out; |
| 1125 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1126 | /* rebroadcast packet */ |
Sven Eckelmann | 9455e34 | 2012-05-12 02:09:37 +0200 | [diff] [blame] | 1127 | batadv_add_bcast_packet_to_list(bat_priv, skb, 1); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1128 | |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1129 | /* don't hand the broadcast up if it is from an originator |
| 1130 | * from the same backbone. |
| 1131 | */ |
Sven Eckelmann | 08adf15 | 2012-05-12 13:38:47 +0200 | [diff] [blame] | 1132 | if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size)) |
Simon Wunderlich | 2372138 | 2012-01-22 20:00:19 +0100 | [diff] [blame] | 1133 | goto out; |
| 1134 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1135 | /* broadcast for me */ |
Sven Eckelmann | 04b482a | 2012-05-12 02:09:38 +0200 | [diff] [blame] | 1136 | batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1137 | ret = NET_RX_SUCCESS; |
| 1138 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1139 | |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1140 | spin_unlock: |
| 1141 | spin_unlock_bh(&orig_node->bcast_seqno_lock); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1142 | out: |
| 1143 | if (orig_node) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 1144 | batadv_orig_node_free_ref(orig_node); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1145 | return ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 1148 | int batadv_recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1149 | { |
| 1150 | struct vis_packet *vis_packet; |
| 1151 | struct ethhdr *ethhdr; |
| 1152 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 1153 | int hdr_size = sizeof(*vis_packet); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1154 | |
| 1155 | /* keep skb linear */ |
| 1156 | if (skb_linearize(skb) < 0) |
| 1157 | return NET_RX_DROP; |
| 1158 | |
| 1159 | if (unlikely(!pskb_may_pull(skb, hdr_size))) |
| 1160 | return NET_RX_DROP; |
| 1161 | |
| 1162 | vis_packet = (struct vis_packet *)skb->data; |
| 1163 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 1164 | |
| 1165 | /* not for me */ |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 1166 | if (!batadv_is_my_mac(ethhdr->h_dest)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1167 | return NET_RX_DROP; |
| 1168 | |
| 1169 | /* ignore own packets */ |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 1170 | if (batadv_is_my_mac(vis_packet->vis_orig)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1171 | return NET_RX_DROP; |
| 1172 | |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 1173 | if (batadv_is_my_mac(vis_packet->sender_orig)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1174 | return NET_RX_DROP; |
| 1175 | |
| 1176 | switch (vis_packet->vis_type) { |
| 1177 | case VIS_TYPE_SERVER_SYNC: |
Sven Eckelmann | d0f714f | 2012-05-12 02:09:41 +0200 | [diff] [blame] | 1178 | batadv_receive_server_sync_packet(bat_priv, vis_packet, |
| 1179 | skb_headlen(skb)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1180 | break; |
| 1181 | |
| 1182 | case VIS_TYPE_CLIENT_UPDATE: |
Sven Eckelmann | d0f714f | 2012-05-12 02:09:41 +0200 | [diff] [blame] | 1183 | batadv_receive_client_update_packet(bat_priv, vis_packet, |
| 1184 | skb_headlen(skb)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1185 | break; |
| 1186 | |
| 1187 | default: /* ignore unknown packet */ |
| 1188 | break; |
| 1189 | } |
| 1190 | |
| 1191 | /* We take a copy of the data in the packet, so we should |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1192 | * always free the skbuf. |
| 1193 | */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1194 | return NET_RX_DROP; |
| 1195 | } |