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