Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1 | /* |
Sven Eckelmann | 64afe35 | 2011-01-27 10:38:15 +0100 | [diff] [blame] | 2 | * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 3 | * |
| 4 | * Marek Lindner, Simon Wunderlich |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of version 2 of the GNU General Public |
| 8 | * License as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 | * 02110-1301, USA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include "main.h" |
| 23 | #include "routing.h" |
| 24 | #include "send.h" |
| 25 | #include "hash.h" |
| 26 | #include "soft-interface.h" |
| 27 | #include "hard-interface.h" |
| 28 | #include "icmp_socket.h" |
| 29 | #include "translation-table.h" |
| 30 | #include "originator.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 31 | #include "ring_buffer.h" |
| 32 | #include "vis.h" |
| 33 | #include "aggregation.h" |
| 34 | #include "gateway_common.h" |
| 35 | #include "gateway_client.h" |
| 36 | #include "unicast.h" |
| 37 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 38 | void slide_own_bcast_window(struct hard_iface *hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 39 | { |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 40 | struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 41 | struct hashtable_t *hash = bat_priv->orig_hash; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 42 | struct hlist_node *node; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 43 | struct hlist_head *head; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 44 | struct orig_node *orig_node; |
| 45 | unsigned long *word; |
| 46 | int i; |
| 47 | size_t word_index; |
| 48 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 49 | for (i = 0; i < hash->size; i++) { |
| 50 | head = &hash->table[i]; |
| 51 | |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 52 | rcu_read_lock(); |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 53 | hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) { |
Marek Lindner | 2ae2daf | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 54 | spin_lock_bh(&orig_node->ogm_cnt_lock); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 55 | word_index = hard_iface->if_num * NUM_WORDS; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 56 | word = &(orig_node->bcast_own[word_index]); |
| 57 | |
| 58 | bit_get_packet(bat_priv, word, 1, 0); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 59 | orig_node->bcast_own_sum[hard_iface->if_num] = |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 60 | bit_packet_count(word); |
Marek Lindner | 2ae2daf | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 61 | spin_unlock_bh(&orig_node->ogm_cnt_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 62 | } |
Marek Lindner | fb778ea | 2011-01-19 20:01:40 +0000 | [diff] [blame] | 63 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 64 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 67 | static void update_route(struct bat_priv *bat_priv, |
| 68 | struct orig_node *orig_node, |
| 69 | struct neigh_node *neigh_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 70 | { |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 71 | struct neigh_node *curr_router; |
| 72 | |
| 73 | curr_router = orig_node_get_router(orig_node); |
Marek Lindner | a8e7f4b | 2010-12-12 21:57:10 +0000 | [diff] [blame] | 74 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 75 | /* route deleted */ |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 76 | if ((curr_router) && (!neigh_node)) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 77 | bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n", |
| 78 | orig_node->orig); |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 79 | tt_global_del_orig(bat_priv, orig_node, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 80 | "Deleted route towards originator"); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 81 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 82 | /* route added */ |
| 83 | } else if ((!curr_router) && (neigh_node)) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 84 | |
| 85 | bat_dbg(DBG_ROUTES, bat_priv, |
| 86 | "Adding route towards: %pM (via %pM)\n", |
| 87 | orig_node->orig, neigh_node->addr); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 88 | /* route changed */ |
Sven Eckelmann | bb899b8 | 2011-05-10 11:22:37 +0200 | [diff] [blame] | 89 | } else if (neigh_node && curr_router) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 90 | bat_dbg(DBG_ROUTES, bat_priv, |
| 91 | "Changing route towards: %pM " |
| 92 | "(now via %pM - was via %pM)\n", |
| 93 | orig_node->orig, neigh_node->addr, |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 94 | curr_router->addr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 97 | if (curr_router) |
| 98 | neigh_node_free_ref(curr_router); |
| 99 | |
| 100 | /* increase refcount of new best neighbor */ |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 101 | if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount)) |
| 102 | neigh_node = NULL; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 103 | |
| 104 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 105 | rcu_assign_pointer(orig_node->router, neigh_node); |
| 106 | spin_unlock_bh(&orig_node->neigh_list_lock); |
| 107 | |
| 108 | /* decrease refcount of previous best neighbor */ |
| 109 | if (curr_router) |
| 110 | neigh_node_free_ref(curr_router); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 113 | void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 114 | struct neigh_node *neigh_node) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 115 | { |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 116 | struct neigh_node *router = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 117 | |
| 118 | if (!orig_node) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 119 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 120 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 121 | router = orig_node_get_router(orig_node); |
| 122 | |
| 123 | if (router != neigh_node) |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 124 | update_route(bat_priv, orig_node, neigh_node); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 125 | |
| 126 | out: |
| 127 | if (router) |
| 128 | neigh_node_free_ref(router); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static int is_bidirectional_neigh(struct orig_node *orig_node, |
| 132 | struct orig_node *orig_neigh_node, |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 133 | struct batman_ogm_packet *batman_ogm_packet, |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 134 | struct hard_iface *if_incoming) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 135 | { |
| 136 | struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
Marek Lindner | 1605d0d | 2011-02-18 12:28:11 +0000 | [diff] [blame] | 137 | struct neigh_node *neigh_node = NULL, *tmp_neigh_node; |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 138 | struct hlist_node *node; |
Sven Eckelmann | b4e1705 | 2011-06-15 09:41:37 +0200 | [diff] [blame] | 139 | uint8_t total_count; |
Marek Lindner | 0ede9f4 | 2011-01-25 21:52:10 +0000 | [diff] [blame] | 140 | uint8_t orig_eq_count, neigh_rq_count, tq_own; |
| 141 | int tq_asym_penalty, ret = 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 142 | |
Daniele Furlan | 27aea21 | 2011-05-07 22:45:19 +0200 | [diff] [blame] | 143 | /* find corresponding one hop neighbor */ |
| 144 | rcu_read_lock(); |
| 145 | hlist_for_each_entry_rcu(tmp_neigh_node, node, |
| 146 | &orig_neigh_node->neigh_list, list) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 147 | |
Daniele Furlan | 27aea21 | 2011-05-07 22:45:19 +0200 | [diff] [blame] | 148 | if (!compare_eth(tmp_neigh_node->addr, orig_neigh_node->orig)) |
| 149 | continue; |
Marek Lindner | 1605d0d | 2011-02-18 12:28:11 +0000 | [diff] [blame] | 150 | |
Daniele Furlan | 27aea21 | 2011-05-07 22:45:19 +0200 | [diff] [blame] | 151 | if (tmp_neigh_node->if_incoming != if_incoming) |
| 152 | continue; |
Marek Lindner | 1605d0d | 2011-02-18 12:28:11 +0000 | [diff] [blame] | 153 | |
Daniele Furlan | 27aea21 | 2011-05-07 22:45:19 +0200 | [diff] [blame] | 154 | if (!atomic_inc_not_zero(&tmp_neigh_node->refcount)) |
| 155 | continue; |
Marek Lindner | 1605d0d | 2011-02-18 12:28:11 +0000 | [diff] [blame] | 156 | |
Daniele Furlan | 27aea21 | 2011-05-07 22:45:19 +0200 | [diff] [blame] | 157 | neigh_node = tmp_neigh_node; |
| 158 | break; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 159 | } |
Daniele Furlan | 27aea21 | 2011-05-07 22:45:19 +0200 | [diff] [blame] | 160 | rcu_read_unlock(); |
| 161 | |
| 162 | if (!neigh_node) |
| 163 | neigh_node = create_neighbor(orig_neigh_node, |
| 164 | orig_neigh_node, |
| 165 | orig_neigh_node->orig, |
| 166 | if_incoming); |
| 167 | |
| 168 | if (!neigh_node) |
| 169 | goto out; |
| 170 | |
Antonio Quartulli | 015758d | 2011-07-09 17:52:13 +0200 | [diff] [blame] | 171 | /* if orig_node is direct neighbor update neigh_node last_valid */ |
Daniele Furlan | 27aea21 | 2011-05-07 22:45:19 +0200 | [diff] [blame] | 172 | if (orig_node == orig_neigh_node) |
| 173 | neigh_node->last_valid = jiffies; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 174 | |
| 175 | orig_node->last_valid = jiffies; |
| 176 | |
Daniele Furlan | 27aea21 | 2011-05-07 22:45:19 +0200 | [diff] [blame] | 177 | /* find packet count of corresponding one hop neighbor */ |
Marek Lindner | 0ede9f4 | 2011-01-25 21:52:10 +0000 | [diff] [blame] | 178 | spin_lock_bh(&orig_node->ogm_cnt_lock); |
| 179 | orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num]; |
| 180 | neigh_rq_count = neigh_node->real_packet_count; |
| 181 | spin_unlock_bh(&orig_node->ogm_cnt_lock); |
| 182 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 183 | /* pay attention to not get a value bigger than 100 % */ |
Marek Lindner | 0ede9f4 | 2011-01-25 21:52:10 +0000 | [diff] [blame] | 184 | total_count = (orig_eq_count > neigh_rq_count ? |
| 185 | neigh_rq_count : orig_eq_count); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 186 | |
| 187 | /* if we have too few packets (too less data) we set tq_own to zero */ |
| 188 | /* if we receive too few packets it is not considered bidirectional */ |
| 189 | if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) || |
Marek Lindner | 0ede9f4 | 2011-01-25 21:52:10 +0000 | [diff] [blame] | 190 | (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM)) |
| 191 | tq_own = 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 192 | else |
| 193 | /* neigh_node->real_packet_count is never zero as we |
| 194 | * only purge old information when getting new |
| 195 | * information */ |
Marek Lindner | 0ede9f4 | 2011-01-25 21:52:10 +0000 | [diff] [blame] | 196 | tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 197 | |
| 198 | /* |
| 199 | * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does |
| 200 | * affect the nearly-symmetric links only a little, but |
| 201 | * punishes asymmetric links more. This will give a value |
| 202 | * between 0 and TQ_MAX_VALUE |
| 203 | */ |
Marek Lindner | 0ede9f4 | 2011-01-25 21:52:10 +0000 | [diff] [blame] | 204 | tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE * |
| 205 | (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) * |
| 206 | (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) * |
| 207 | (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) / |
| 208 | (TQ_LOCAL_WINDOW_SIZE * |
| 209 | TQ_LOCAL_WINDOW_SIZE * |
| 210 | TQ_LOCAL_WINDOW_SIZE); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 211 | |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 212 | batman_ogm_packet->tq = ((batman_ogm_packet->tq * tq_own |
| 213 | * tq_asym_penalty) / |
Marek Lindner | 0ede9f4 | 2011-01-25 21:52:10 +0000 | [diff] [blame] | 214 | (TQ_MAX_VALUE * TQ_MAX_VALUE)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 215 | |
| 216 | bat_dbg(DBG_BATMAN, bat_priv, |
| 217 | "bidirectional: " |
| 218 | "orig = %-15pM neigh = %-15pM => own_bcast = %2i, " |
| 219 | "real recv = %2i, local tq: %3i, asym_penalty: %3i, " |
| 220 | "total tq: %3i\n", |
| 221 | orig_node->orig, orig_neigh_node->orig, total_count, |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 222 | neigh_rq_count, tq_own, tq_asym_penalty, batman_ogm_packet->tq); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 223 | |
| 224 | /* if link has the minimum required transmission quality |
| 225 | * consider it bidirectional */ |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 226 | if (batman_ogm_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT) |
Marek Lindner | a775eb8 | 2011-01-19 20:01:39 +0000 | [diff] [blame] | 227 | ret = 1; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 228 | |
Marek Lindner | a775eb8 | 2011-01-19 20:01:39 +0000 | [diff] [blame] | 229 | out: |
| 230 | if (neigh_node) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 231 | neigh_node_free_ref(neigh_node); |
Marek Lindner | a775eb8 | 2011-01-19 20:01:39 +0000 | [diff] [blame] | 232 | return ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 235 | /* caller must hold the neigh_list_lock */ |
| 236 | void bonding_candidate_del(struct orig_node *orig_node, |
| 237 | struct neigh_node *neigh_node) |
| 238 | { |
| 239 | /* this neighbor is not part of our candidate list */ |
| 240 | if (list_empty(&neigh_node->bonding_list)) |
| 241 | goto out; |
| 242 | |
| 243 | list_del_rcu(&neigh_node->bonding_list); |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 244 | INIT_LIST_HEAD(&neigh_node->bonding_list); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 245 | neigh_node_free_ref(neigh_node); |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 246 | atomic_dec(&orig_node->bond_candidates); |
| 247 | |
| 248 | out: |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | static void bonding_candidate_add(struct orig_node *orig_node, |
| 253 | struct neigh_node *neigh_node) |
| 254 | { |
| 255 | struct hlist_node *node; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 256 | struct neigh_node *tmp_neigh_node, *router = NULL; |
| 257 | uint8_t interference_candidate = 0; |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 258 | |
| 259 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 260 | |
| 261 | /* only consider if it has the same primary address ... */ |
Marek Lindner | 39901e7 | 2011-02-18 12:28:08 +0000 | [diff] [blame] | 262 | if (!compare_eth(orig_node->orig, |
| 263 | neigh_node->orig_node->primary_addr)) |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 264 | goto candidate_del; |
| 265 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 266 | router = orig_node_get_router(orig_node); |
| 267 | if (!router) |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 268 | goto candidate_del; |
| 269 | |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 270 | /* ... and is good enough to be considered */ |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 271 | if (neigh_node->tq_avg < router->tq_avg - BONDING_TQ_THRESHOLD) |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 272 | goto candidate_del; |
| 273 | |
| 274 | /** |
| 275 | * check if we have another candidate with the same mac address or |
| 276 | * interface. If we do, we won't select this candidate because of |
| 277 | * possible interference. |
| 278 | */ |
| 279 | hlist_for_each_entry_rcu(tmp_neigh_node, node, |
| 280 | &orig_node->neigh_list, list) { |
| 281 | |
| 282 | if (tmp_neigh_node == neigh_node) |
| 283 | continue; |
| 284 | |
| 285 | /* we only care if the other candidate is even |
| 286 | * considered as candidate. */ |
| 287 | if (list_empty(&tmp_neigh_node->bonding_list)) |
| 288 | continue; |
| 289 | |
| 290 | if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) || |
Marek Lindner | 39901e7 | 2011-02-18 12:28:08 +0000 | [diff] [blame] | 291 | (compare_eth(neigh_node->addr, tmp_neigh_node->addr))) { |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 292 | interference_candidate = 1; |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /* don't care further if it is an interference candidate */ |
| 298 | if (interference_candidate) |
| 299 | goto candidate_del; |
| 300 | |
| 301 | /* this neighbor already is part of our candidate list */ |
| 302 | if (!list_empty(&neigh_node->bonding_list)) |
| 303 | goto out; |
| 304 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 305 | if (!atomic_inc_not_zero(&neigh_node->refcount)) |
| 306 | goto out; |
| 307 | |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 308 | list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list); |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 309 | atomic_inc(&orig_node->bond_candidates); |
| 310 | goto out; |
| 311 | |
| 312 | candidate_del: |
| 313 | bonding_candidate_del(orig_node, neigh_node); |
| 314 | |
| 315 | out: |
| 316 | spin_unlock_bh(&orig_node->neigh_list_lock); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 317 | |
| 318 | if (router) |
| 319 | neigh_node_free_ref(router); |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /* copy primary address for bonding */ |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 323 | static void bonding_save_primary(const struct orig_node *orig_node, |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 324 | struct orig_node *orig_neigh_node, |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 325 | const struct batman_ogm_packet |
| 326 | *batman_ogm_packet) |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 327 | { |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 328 | if (!(batman_ogm_packet->flags & PRIMARIES_FIRST_HOP)) |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 329 | return; |
| 330 | |
| 331 | memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN); |
| 332 | } |
| 333 | |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 334 | static void update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node, |
| 335 | const struct ethhdr *ethhdr, |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 336 | const struct batman_ogm_packet *batman_ogm_packet, |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 337 | struct hard_iface *if_incoming, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 338 | const unsigned char *tt_buff, int is_duplicate) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 339 | { |
| 340 | struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 341 | struct neigh_node *router = NULL; |
Marek Lindner | 2ae2daf | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 342 | struct orig_node *orig_node_tmp; |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 343 | struct hlist_node *node; |
Marek Lindner | 2ae2daf | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 344 | uint8_t bcast_own_sum_orig, bcast_own_sum_neigh; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 345 | |
| 346 | bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): " |
| 347 | "Searching and updating originator entry of received packet\n"); |
| 348 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 349 | rcu_read_lock(); |
| 350 | hlist_for_each_entry_rcu(tmp_neigh_node, node, |
| 351 | &orig_node->neigh_list, list) { |
Marek Lindner | 39901e7 | 2011-02-18 12:28:08 +0000 | [diff] [blame] | 352 | if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) && |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 353 | (tmp_neigh_node->if_incoming == if_incoming) && |
| 354 | atomic_inc_not_zero(&tmp_neigh_node->refcount)) { |
| 355 | if (neigh_node) |
| 356 | neigh_node_free_ref(neigh_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 357 | neigh_node = tmp_neigh_node; |
| 358 | continue; |
| 359 | } |
| 360 | |
| 361 | if (is_duplicate) |
| 362 | continue; |
| 363 | |
Linus Lüssing | 6800390 | 2011-03-14 22:43:40 +0000 | [diff] [blame] | 364 | spin_lock_bh(&tmp_neigh_node->tq_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 365 | ring_buffer_set(tmp_neigh_node->tq_recv, |
| 366 | &tmp_neigh_node->tq_index, 0); |
| 367 | tmp_neigh_node->tq_avg = |
| 368 | ring_buffer_avg(tmp_neigh_node->tq_recv); |
Linus Lüssing | 6800390 | 2011-03-14 22:43:40 +0000 | [diff] [blame] | 369 | spin_unlock_bh(&tmp_neigh_node->tq_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | if (!neigh_node) { |
| 373 | struct orig_node *orig_tmp; |
| 374 | |
| 375 | orig_tmp = get_orig_node(bat_priv, ethhdr->h_source); |
| 376 | if (!orig_tmp) |
Marek Lindner | a775eb8 | 2011-01-19 20:01:39 +0000 | [diff] [blame] | 377 | goto unlock; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 378 | |
| 379 | neigh_node = create_neighbor(orig_node, orig_tmp, |
| 380 | ethhdr->h_source, if_incoming); |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 381 | |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 382 | orig_node_free_ref(orig_tmp); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 383 | if (!neigh_node) |
Marek Lindner | a775eb8 | 2011-01-19 20:01:39 +0000 | [diff] [blame] | 384 | goto unlock; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 385 | } else |
| 386 | bat_dbg(DBG_BATMAN, bat_priv, |
| 387 | "Updating existing last-hop neighbor of originator\n"); |
| 388 | |
Marek Lindner | a775eb8 | 2011-01-19 20:01:39 +0000 | [diff] [blame] | 389 | rcu_read_unlock(); |
| 390 | |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 391 | orig_node->flags = batman_ogm_packet->flags; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 392 | neigh_node->last_valid = jiffies; |
| 393 | |
Linus Lüssing | 6800390 | 2011-03-14 22:43:40 +0000 | [diff] [blame] | 394 | spin_lock_bh(&neigh_node->tq_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 395 | ring_buffer_set(neigh_node->tq_recv, |
| 396 | &neigh_node->tq_index, |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 397 | batman_ogm_packet->tq); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 398 | neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv); |
Linus Lüssing | 6800390 | 2011-03-14 22:43:40 +0000 | [diff] [blame] | 399 | spin_unlock_bh(&neigh_node->tq_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 400 | |
| 401 | if (!is_duplicate) { |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 402 | orig_node->last_ttl = batman_ogm_packet->ttl; |
| 403 | neigh_node->last_ttl = batman_ogm_packet->ttl; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 406 | bonding_candidate_add(orig_node, neigh_node); |
| 407 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 408 | /* if this neighbor already is our next hop there is nothing |
| 409 | * to change */ |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 410 | router = orig_node_get_router(orig_node); |
| 411 | if (router == neigh_node) |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 412 | goto update_tt; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 413 | |
| 414 | /* if this neighbor does not offer a better TQ we won't consider it */ |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 415 | if (router && (router->tq_avg > neigh_node->tq_avg)) |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 416 | goto update_tt; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 417 | |
Antonio Quartulli | 015758d | 2011-07-09 17:52:13 +0200 | [diff] [blame] | 418 | /* if the TQ is the same and the link not more symmetric we |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 419 | * won't consider it either */ |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 420 | if (router && (neigh_node->tq_avg == router->tq_avg)) { |
| 421 | orig_node_tmp = router->orig_node; |
Marek Lindner | 2ae2daf | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 422 | spin_lock_bh(&orig_node_tmp->ogm_cnt_lock); |
| 423 | bcast_own_sum_orig = |
| 424 | orig_node_tmp->bcast_own_sum[if_incoming->if_num]; |
| 425 | spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock); |
| 426 | |
| 427 | orig_node_tmp = neigh_node->orig_node; |
| 428 | spin_lock_bh(&orig_node_tmp->ogm_cnt_lock); |
| 429 | bcast_own_sum_neigh = |
| 430 | orig_node_tmp->bcast_own_sum[if_incoming->if_num]; |
| 431 | spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock); |
| 432 | |
| 433 | if (bcast_own_sum_orig >= bcast_own_sum_neigh) |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 434 | goto update_tt; |
Marek Lindner | 2ae2daf | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 435 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 436 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 437 | update_routes(bat_priv, orig_node, neigh_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 438 | |
Antonio Quartulli | 2dafb49 | 2011-05-05 08:42:45 +0200 | [diff] [blame] | 439 | update_tt: |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 440 | /* I have to check for transtable changes only if the OGM has been |
| 441 | * sent through a primary interface */ |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 442 | if (((batman_ogm_packet->orig != ethhdr->h_source) && |
| 443 | (batman_ogm_packet->ttl > 2)) || |
| 444 | (batman_ogm_packet->flags & PRIMARIES_FIRST_HOP)) |
Marek Lindner | a943cac | 2011-07-30 13:10:18 +0200 | [diff] [blame] | 445 | tt_update_orig(bat_priv, orig_node, tt_buff, |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 446 | batman_ogm_packet->tt_num_changes, |
| 447 | batman_ogm_packet->ttvn, |
| 448 | batman_ogm_packet->tt_crc); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 449 | |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 450 | if (orig_node->gw_flags != batman_ogm_packet->gw_flags) |
| 451 | gw_node_update(bat_priv, orig_node, |
| 452 | batman_ogm_packet->gw_flags); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 453 | |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 454 | orig_node->gw_flags = batman_ogm_packet->gw_flags; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 455 | |
| 456 | /* restart gateway selection if fast or late switching was enabled */ |
| 457 | if ((orig_node->gw_flags) && |
| 458 | (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) && |
| 459 | (atomic_read(&bat_priv->gw_sel_class) > 2)) |
| 460 | gw_check_election(bat_priv, orig_node); |
Marek Lindner | a775eb8 | 2011-01-19 20:01:39 +0000 | [diff] [blame] | 461 | |
| 462 | goto out; |
| 463 | |
| 464 | unlock: |
| 465 | rcu_read_unlock(); |
| 466 | out: |
| 467 | if (neigh_node) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 468 | neigh_node_free_ref(neigh_node); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 469 | if (router) |
| 470 | neigh_node_free_ref(router); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | /* checks whether the host restarted and is in the protection time. |
| 474 | * returns: |
| 475 | * 0 if the packet is to be accepted |
| 476 | * 1 if the packet is to be ignored. |
| 477 | */ |
| 478 | static int window_protected(struct bat_priv *bat_priv, |
| 479 | int32_t seq_num_diff, |
| 480 | unsigned long *last_reset) |
| 481 | { |
| 482 | if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE) |
| 483 | || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) { |
| 484 | if (time_after(jiffies, *last_reset + |
| 485 | msecs_to_jiffies(RESET_PROTECTION_MS))) { |
| 486 | |
| 487 | *last_reset = jiffies; |
| 488 | bat_dbg(DBG_BATMAN, bat_priv, |
| 489 | "old packet received, start protection\n"); |
| 490 | |
| 491 | return 0; |
| 492 | } else |
| 493 | return 1; |
| 494 | } |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | /* processes a batman packet for all interfaces, adjusts the sequence number and |
| 499 | * finds out whether it is a duplicate. |
| 500 | * returns: |
| 501 | * 1 the packet is a duplicate |
| 502 | * 0 the packet has not yet been received |
| 503 | * -1 the packet is old and has been received while the seqno window |
| 504 | * was protected. Caller should drop it. |
| 505 | */ |
David Howells | b2c44a5 | 2011-06-15 09:41:36 +0200 | [diff] [blame] | 506 | static int count_real_packets(const struct ethhdr *ethhdr, |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 507 | const struct batman_ogm_packet *batman_ogm_packet, |
| 508 | const struct hard_iface *if_incoming) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 509 | { |
| 510 | struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
| 511 | struct orig_node *orig_node; |
| 512 | struct neigh_node *tmp_neigh_node; |
Marek Lindner | 9591a79 | 2010-12-12 21:57:11 +0000 | [diff] [blame] | 513 | struct hlist_node *node; |
David Howells | b2c44a5 | 2011-06-15 09:41:36 +0200 | [diff] [blame] | 514 | int is_duplicate = 0; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 515 | int32_t seq_diff; |
| 516 | int need_update = 0; |
Marek Lindner | 0ede9f4 | 2011-01-25 21:52:10 +0000 | [diff] [blame] | 517 | int set_mark, ret = -1; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 518 | |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 519 | orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 520 | if (!orig_node) |
| 521 | return 0; |
| 522 | |
Marek Lindner | 0ede9f4 | 2011-01-25 21:52:10 +0000 | [diff] [blame] | 523 | spin_lock_bh(&orig_node->ogm_cnt_lock); |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 524 | seq_diff = batman_ogm_packet->seqno - orig_node->last_real_seqno; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 525 | |
| 526 | /* signalize caller that the packet is to be dropped. */ |
| 527 | if (window_protected(bat_priv, seq_diff, |
| 528 | &orig_node->batman_seqno_reset)) |
Marek Lindner | 0ede9f4 | 2011-01-25 21:52:10 +0000 | [diff] [blame] | 529 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 530 | |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 531 | rcu_read_lock(); |
| 532 | hlist_for_each_entry_rcu(tmp_neigh_node, node, |
| 533 | &orig_node->neigh_list, list) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 534 | |
| 535 | is_duplicate |= get_bit_status(tmp_neigh_node->real_bits, |
| 536 | orig_node->last_real_seqno, |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 537 | batman_ogm_packet->seqno); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 538 | |
Marek Lindner | 39901e7 | 2011-02-18 12:28:08 +0000 | [diff] [blame] | 539 | if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) && |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 540 | (tmp_neigh_node->if_incoming == if_incoming)) |
| 541 | set_mark = 1; |
| 542 | else |
| 543 | set_mark = 0; |
| 544 | |
| 545 | /* if the window moved, set the update flag. */ |
| 546 | need_update |= bit_get_packet(bat_priv, |
| 547 | tmp_neigh_node->real_bits, |
| 548 | seq_diff, set_mark); |
| 549 | |
| 550 | tmp_neigh_node->real_packet_count = |
| 551 | bit_packet_count(tmp_neigh_node->real_bits); |
| 552 | } |
Marek Lindner | f987ed6 | 2010-12-12 21:57:12 +0000 | [diff] [blame] | 553 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 554 | |
| 555 | if (need_update) { |
| 556 | bat_dbg(DBG_BATMAN, bat_priv, |
| 557 | "updating last_seqno: old %d, new %d\n", |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 558 | orig_node->last_real_seqno, batman_ogm_packet->seqno); |
| 559 | orig_node->last_real_seqno = batman_ogm_packet->seqno; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Marek Lindner | 0ede9f4 | 2011-01-25 21:52:10 +0000 | [diff] [blame] | 562 | ret = is_duplicate; |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 563 | |
Marek Lindner | 0ede9f4 | 2011-01-25 21:52:10 +0000 | [diff] [blame] | 564 | out: |
| 565 | spin_unlock_bh(&orig_node->ogm_cnt_lock); |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 566 | orig_node_free_ref(orig_node); |
Marek Lindner | 0ede9f4 | 2011-01-25 21:52:10 +0000 | [diff] [blame] | 567 | return ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 570 | void receive_bat_packet(const struct ethhdr *ethhdr, |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 571 | struct batman_ogm_packet *batman_ogm_packet, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 572 | const unsigned char *tt_buff, |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 573 | struct hard_iface *if_incoming) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 574 | { |
| 575 | struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 576 | struct hard_iface *hard_iface; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 577 | struct orig_node *orig_neigh_node, *orig_node; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 578 | struct neigh_node *router = NULL, *router_router = NULL; |
| 579 | struct neigh_node *orig_neigh_router = NULL; |
Sven Eckelmann | b4e1705 | 2011-06-15 09:41:37 +0200 | [diff] [blame] | 580 | int has_directlink_flag; |
| 581 | int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0; |
| 582 | int is_broadcast = 0, is_bidirectional, is_single_hop_neigh; |
David Howells | b2c44a5 | 2011-06-15 09:41:36 +0200 | [diff] [blame] | 583 | int is_duplicate; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 584 | uint32_t if_incoming_seqno; |
| 585 | |
| 586 | /* Silently drop when the batman packet is actually not a |
| 587 | * correct packet. |
| 588 | * |
| 589 | * This might happen if a packet is padded (e.g. Ethernet has a |
| 590 | * minimum frame length of 64 byte) and the aggregation interprets |
| 591 | * it as an additional length. |
| 592 | * |
| 593 | * TODO: A more sane solution would be to have a bit in the |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 594 | * batman_ogm_packet to detect whether the packet is the last |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 595 | * packet in an aggregation. Here we expect that the padding |
| 596 | * is always zero (or not 0x01) |
| 597 | */ |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 598 | if (batman_ogm_packet->packet_type != BAT_OGM) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 599 | return; |
| 600 | |
| 601 | /* could be changed by schedule_own_packet() */ |
| 602 | if_incoming_seqno = atomic_read(&if_incoming->seqno); |
| 603 | |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 604 | has_directlink_flag = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 605 | |
Marek Lindner | 39901e7 | 2011-02-18 12:28:08 +0000 | [diff] [blame] | 606 | is_single_hop_neigh = (compare_eth(ethhdr->h_source, |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 607 | batman_ogm_packet->orig) ? 1 : 0); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 608 | |
| 609 | bat_dbg(DBG_BATMAN, bat_priv, |
| 610 | "Received BATMAN packet via NB: %pM, IF: %s [%pM] " |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 611 | "(from OG: %pM, via prev OG: %pM, seqno %d, ttvn %u, " |
| 612 | "crc %u, changes %u, td %d, TTL %d, V %d, IDF %d)\n", |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 613 | ethhdr->h_source, if_incoming->net_dev->name, |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 614 | if_incoming->net_dev->dev_addr, batman_ogm_packet->orig, |
| 615 | batman_ogm_packet->prev_sender, batman_ogm_packet->seqno, |
| 616 | batman_ogm_packet->ttvn, batman_ogm_packet->tt_crc, |
| 617 | batman_ogm_packet->tt_num_changes, batman_ogm_packet->tq, |
| 618 | batman_ogm_packet->ttl, batman_ogm_packet->version, |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 619 | has_directlink_flag); |
| 620 | |
| 621 | rcu_read_lock(); |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 622 | list_for_each_entry_rcu(hard_iface, &hardif_list, list) { |
| 623 | if (hard_iface->if_status != IF_ACTIVE) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 624 | continue; |
| 625 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 626 | if (hard_iface->soft_iface != if_incoming->soft_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 627 | continue; |
| 628 | |
Marek Lindner | 39901e7 | 2011-02-18 12:28:08 +0000 | [diff] [blame] | 629 | if (compare_eth(ethhdr->h_source, |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 630 | hard_iface->net_dev->dev_addr)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 631 | is_my_addr = 1; |
| 632 | |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 633 | if (compare_eth(batman_ogm_packet->orig, |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 634 | hard_iface->net_dev->dev_addr)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 635 | is_my_orig = 1; |
| 636 | |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 637 | if (compare_eth(batman_ogm_packet->prev_sender, |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 638 | hard_iface->net_dev->dev_addr)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 639 | is_my_oldorig = 1; |
| 640 | |
Antonio Quartulli | 44e92bc | 2011-05-21 01:33:07 +0200 | [diff] [blame] | 641 | if (is_broadcast_ether_addr(ethhdr->h_source)) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 642 | is_broadcast = 1; |
| 643 | } |
| 644 | rcu_read_unlock(); |
| 645 | |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 646 | if (batman_ogm_packet->version != COMPAT_VERSION) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 647 | bat_dbg(DBG_BATMAN, bat_priv, |
| 648 | "Drop packet: incompatible batman version (%i)\n", |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 649 | batman_ogm_packet->version); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 650 | return; |
| 651 | } |
| 652 | |
| 653 | if (is_my_addr) { |
| 654 | bat_dbg(DBG_BATMAN, bat_priv, |
| 655 | "Drop packet: received my own broadcast (sender: %pM" |
| 656 | ")\n", |
| 657 | ethhdr->h_source); |
| 658 | return; |
| 659 | } |
| 660 | |
| 661 | if (is_broadcast) { |
| 662 | bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: " |
| 663 | "ignoring all packets with broadcast source addr (sender: %pM" |
| 664 | ")\n", ethhdr->h_source); |
| 665 | return; |
| 666 | } |
| 667 | |
| 668 | if (is_my_orig) { |
| 669 | unsigned long *word; |
| 670 | int offset; |
| 671 | |
| 672 | orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 673 | if (!orig_neigh_node) |
| 674 | return; |
| 675 | |
| 676 | /* neighbor has to indicate direct link and it has to |
| 677 | * come via the corresponding interface */ |
Daniele Furlan | d1829fa | 2011-06-07 00:45:55 +0200 | [diff] [blame] | 678 | /* save packet seqno for bidirectional check */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 679 | if (has_directlink_flag && |
Marek Lindner | 39901e7 | 2011-02-18 12:28:08 +0000 | [diff] [blame] | 680 | compare_eth(if_incoming->net_dev->dev_addr, |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 681 | batman_ogm_packet->orig)) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 682 | offset = if_incoming->if_num * NUM_WORDS; |
Marek Lindner | 2ae2daf | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 683 | |
| 684 | spin_lock_bh(&orig_neigh_node->ogm_cnt_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 685 | word = &(orig_neigh_node->bcast_own[offset]); |
Daniele Furlan | d1829fa | 2011-06-07 00:45:55 +0200 | [diff] [blame] | 686 | bit_mark(word, |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 687 | if_incoming_seqno - |
| 688 | batman_ogm_packet->seqno - 2); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 689 | orig_neigh_node->bcast_own_sum[if_incoming->if_num] = |
| 690 | bit_packet_count(word); |
Marek Lindner | 2ae2daf | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 691 | spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: " |
| 695 | "originator packet from myself (via neighbor)\n"); |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 696 | orig_node_free_ref(orig_neigh_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 697 | return; |
| 698 | } |
| 699 | |
| 700 | if (is_my_oldorig) { |
| 701 | bat_dbg(DBG_BATMAN, bat_priv, |
| 702 | "Drop packet: ignoring all rebroadcast echos (sender: " |
| 703 | "%pM)\n", ethhdr->h_source); |
| 704 | return; |
| 705 | } |
| 706 | |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 707 | orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 708 | if (!orig_node) |
| 709 | return; |
| 710 | |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 711 | is_duplicate = count_real_packets(ethhdr, batman_ogm_packet, |
| 712 | if_incoming); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 713 | |
| 714 | if (is_duplicate == -1) { |
| 715 | bat_dbg(DBG_BATMAN, bat_priv, |
| 716 | "Drop packet: packet within seqno protection time " |
| 717 | "(sender: %pM)\n", ethhdr->h_source); |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 718 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 721 | if (batman_ogm_packet->tq == 0) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 722 | bat_dbg(DBG_BATMAN, bat_priv, |
| 723 | "Drop packet: originator packet with tq equal 0\n"); |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 724 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 727 | router = orig_node_get_router(orig_node); |
| 728 | if (router) |
| 729 | router_router = orig_node_get_router(router->orig_node); |
| 730 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 731 | /* avoid temporary routing loops */ |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 732 | if (router && router_router && |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 733 | (compare_eth(router->addr, batman_ogm_packet->prev_sender)) && |
| 734 | !(compare_eth(batman_ogm_packet->orig, |
| 735 | batman_ogm_packet->prev_sender)) && |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 736 | (compare_eth(router->addr, router_router->addr))) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 737 | bat_dbg(DBG_BATMAN, bat_priv, |
| 738 | "Drop packet: ignoring all rebroadcast packets that " |
| 739 | "may make me loop (sender: %pM)\n", ethhdr->h_source); |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 740 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | /* if sender is a direct neighbor the sender mac equals |
| 744 | * originator mac */ |
| 745 | orig_neigh_node = (is_single_hop_neigh ? |
| 746 | orig_node : |
| 747 | get_orig_node(bat_priv, ethhdr->h_source)); |
| 748 | if (!orig_neigh_node) |
Marek Lindner | d007260 | 2011-01-19 20:01:44 +0000 | [diff] [blame] | 749 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 750 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 751 | orig_neigh_router = orig_node_get_router(orig_neigh_node); |
| 752 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 753 | /* drop packet if sender is not a direct neighbor and if we |
| 754 | * don't route towards it */ |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 755 | if (!is_single_hop_neigh && (!orig_neigh_router)) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 756 | bat_dbg(DBG_BATMAN, bat_priv, |
| 757 | "Drop packet: OGM via unknown neighbor!\n"); |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 758 | goto out_neigh; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node, |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 762 | batman_ogm_packet, |
| 763 | if_incoming); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 764 | |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 765 | bonding_save_primary(orig_node, orig_neigh_node, batman_ogm_packet); |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 766 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 767 | /* update ranking if it is not a duplicate or has the same |
| 768 | * seqno and similar ttl as the non-duplicate */ |
| 769 | if (is_bidirectional && |
| 770 | (!is_duplicate || |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 771 | ((orig_node->last_real_seqno == batman_ogm_packet->seqno) && |
| 772 | (orig_node->last_ttl - 3 <= batman_ogm_packet->ttl)))) |
| 773 | update_orig(bat_priv, orig_node, ethhdr, batman_ogm_packet, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 774 | if_incoming, tt_buff, is_duplicate); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 775 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 776 | /* is single hop (direct) neighbor */ |
| 777 | if (is_single_hop_neigh) { |
| 778 | |
| 779 | /* mark direct link on incoming interface */ |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 780 | schedule_forward_packet(orig_node, ethhdr, batman_ogm_packet, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 781 | 1, if_incoming); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 782 | |
| 783 | bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: " |
| 784 | "rebroadcast neighbor packet with direct link flag\n"); |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 785 | goto out_neigh; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | /* multihop originator */ |
| 789 | if (!is_bidirectional) { |
| 790 | bat_dbg(DBG_BATMAN, bat_priv, |
| 791 | "Drop packet: not received via bidirectional link\n"); |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 792 | goto out_neigh; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | if (is_duplicate) { |
| 796 | bat_dbg(DBG_BATMAN, bat_priv, |
| 797 | "Drop packet: duplicate packet received\n"); |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 798 | goto out_neigh; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | bat_dbg(DBG_BATMAN, bat_priv, |
| 802 | "Forwarding packet: rebroadcast originator packet\n"); |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 803 | schedule_forward_packet(orig_node, ethhdr, batman_ogm_packet, |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 804 | 0, if_incoming); |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 805 | |
| 806 | out_neigh: |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 807 | if ((orig_neigh_node) && (!is_single_hop_neigh)) |
| 808 | orig_node_free_ref(orig_neigh_node); |
Marek Lindner | 16b1aba | 2011-01-19 20:01:42 +0000 | [diff] [blame] | 809 | out: |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 810 | if (router) |
| 811 | neigh_node_free_ref(router); |
| 812 | if (router_router) |
| 813 | neigh_node_free_ref(router_router); |
| 814 | if (orig_neigh_router) |
| 815 | neigh_node_free_ref(orig_neigh_router); |
| 816 | |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 817 | orig_node_free_ref(orig_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 820 | int recv_bat_packet(struct sk_buff *skb, struct hard_iface *hard_iface) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 821 | { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 822 | struct ethhdr *ethhdr; |
| 823 | |
| 824 | /* drop packet if it has not necessary minimum size */ |
Marek Lindner | b6da4bf | 2011-07-29 17:31:50 +0200 | [diff] [blame^] | 825 | if (unlikely(!pskb_may_pull(skb, BATMAN_OGM_LEN))) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 826 | return NET_RX_DROP; |
| 827 | |
| 828 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 829 | |
| 830 | /* packet with broadcast indication but unicast recipient */ |
| 831 | if (!is_broadcast_ether_addr(ethhdr->h_dest)) |
| 832 | return NET_RX_DROP; |
| 833 | |
| 834 | /* packet with broadcast sender address */ |
| 835 | if (is_broadcast_ether_addr(ethhdr->h_source)) |
| 836 | return NET_RX_DROP; |
| 837 | |
| 838 | /* create a copy of the skb, if needed, to modify it. */ |
| 839 | if (skb_cow(skb, 0) < 0) |
| 840 | return NET_RX_DROP; |
| 841 | |
| 842 | /* keep skb linear */ |
| 843 | if (skb_linearize(skb) < 0) |
| 844 | return NET_RX_DROP; |
| 845 | |
| 846 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 847 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 848 | receive_aggr_bat_packet(ethhdr, |
| 849 | skb->data, |
| 850 | skb_headlen(skb), |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 851 | hard_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 852 | |
| 853 | kfree_skb(skb); |
| 854 | return NET_RX_SUCCESS; |
| 855 | } |
| 856 | |
| 857 | static int recv_my_icmp_packet(struct bat_priv *bat_priv, |
| 858 | struct sk_buff *skb, size_t icmp_len) |
| 859 | { |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 860 | struct hard_iface *primary_if = NULL; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 861 | struct orig_node *orig_node = NULL; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 862 | struct neigh_node *router = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 863 | struct icmp_packet_rr *icmp_packet; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 864 | int ret = NET_RX_DROP; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 865 | |
| 866 | icmp_packet = (struct icmp_packet_rr *)skb->data; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 867 | |
| 868 | /* add data to device queue */ |
| 869 | if (icmp_packet->msg_type != ECHO_REQUEST) { |
| 870 | bat_socket_receive_packet(icmp_packet, icmp_len); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 871 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 874 | primary_if = primary_if_get_selected(bat_priv); |
| 875 | if (!primary_if) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 876 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 877 | |
| 878 | /* answer echo request (ping) */ |
| 879 | /* get routing information */ |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 880 | orig_node = orig_hash_find(bat_priv, icmp_packet->orig); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 881 | if (!orig_node) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 882 | goto out; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 883 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 884 | router = orig_node_get_router(orig_node); |
| 885 | if (!router) |
| 886 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 887 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 888 | /* create a copy of the skb, if needed, to modify it. */ |
| 889 | if (skb_cow(skb, sizeof(struct ethhdr)) < 0) |
| 890 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 891 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 892 | icmp_packet = (struct icmp_packet_rr *)skb->data; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 893 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 894 | memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 895 | memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 896 | icmp_packet->msg_type = ECHO_REPLY; |
| 897 | icmp_packet->ttl = TTL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 898 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 899 | send_skb_packet(skb, router->if_incoming, router->addr); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 900 | ret = NET_RX_SUCCESS; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 901 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 902 | out: |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 903 | if (primary_if) |
| 904 | hardif_free_ref(primary_if); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 905 | if (router) |
| 906 | neigh_node_free_ref(router); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 907 | if (orig_node) |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 908 | orig_node_free_ref(orig_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 909 | return ret; |
| 910 | } |
| 911 | |
| 912 | static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv, |
Simon Wunderlich | 74ef115 | 2010-12-29 16:15:19 +0000 | [diff] [blame] | 913 | struct sk_buff *skb) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 914 | { |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 915 | struct hard_iface *primary_if = NULL; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 916 | struct orig_node *orig_node = NULL; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 917 | struct neigh_node *router = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 918 | struct icmp_packet *icmp_packet; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 919 | int ret = NET_RX_DROP; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 920 | |
| 921 | icmp_packet = (struct icmp_packet *)skb->data; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 922 | |
| 923 | /* send TTL exceeded if packet is an echo request (traceroute) */ |
| 924 | if (icmp_packet->msg_type != ECHO_REQUEST) { |
| 925 | pr_debug("Warning - can't forward icmp packet from %pM to " |
| 926 | "%pM: ttl exceeded\n", icmp_packet->orig, |
| 927 | icmp_packet->dst); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 928 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 931 | primary_if = primary_if_get_selected(bat_priv); |
| 932 | if (!primary_if) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 933 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 934 | |
| 935 | /* get routing information */ |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 936 | orig_node = orig_hash_find(bat_priv, icmp_packet->orig); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 937 | if (!orig_node) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 938 | goto out; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 939 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 940 | router = orig_node_get_router(orig_node); |
| 941 | if (!router) |
| 942 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 943 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 944 | /* create a copy of the skb, if needed, to modify it. */ |
| 945 | if (skb_cow(skb, sizeof(struct ethhdr)) < 0) |
| 946 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 947 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 948 | icmp_packet = (struct icmp_packet *)skb->data; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 949 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 950 | memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 951 | memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 952 | icmp_packet->msg_type = TTL_EXCEEDED; |
| 953 | icmp_packet->ttl = TTL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 954 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 955 | send_skb_packet(skb, router->if_incoming, router->addr); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 956 | ret = NET_RX_SUCCESS; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 957 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 958 | out: |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 959 | if (primary_if) |
| 960 | hardif_free_ref(primary_if); |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 961 | if (router) |
| 962 | neigh_node_free_ref(router); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 963 | if (orig_node) |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 964 | orig_node_free_ref(orig_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 965 | return ret; |
| 966 | } |
| 967 | |
| 968 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 969 | int recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 970 | { |
| 971 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
| 972 | struct icmp_packet_rr *icmp_packet; |
| 973 | struct ethhdr *ethhdr; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 974 | struct orig_node *orig_node = NULL; |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 975 | struct neigh_node *router = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 976 | int hdr_size = sizeof(struct icmp_packet); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 977 | int ret = NET_RX_DROP; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 978 | |
| 979 | /** |
| 980 | * we truncate all incoming icmp packets if they don't match our size |
| 981 | */ |
| 982 | if (skb->len >= sizeof(struct icmp_packet_rr)) |
| 983 | hdr_size = sizeof(struct icmp_packet_rr); |
| 984 | |
| 985 | /* drop packet if it has not necessary minimum size */ |
| 986 | if (unlikely(!pskb_may_pull(skb, hdr_size))) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 987 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 988 | |
| 989 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 990 | |
| 991 | /* packet with unicast indication but broadcast recipient */ |
| 992 | if (is_broadcast_ether_addr(ethhdr->h_dest)) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 993 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 994 | |
| 995 | /* packet with broadcast sender address */ |
| 996 | if (is_broadcast_ether_addr(ethhdr->h_source)) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 997 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 998 | |
| 999 | /* not for me */ |
| 1000 | if (!is_my_mac(ethhdr->h_dest)) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1001 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1002 | |
| 1003 | icmp_packet = (struct icmp_packet_rr *)skb->data; |
| 1004 | |
| 1005 | /* add record route information if not full */ |
| 1006 | if ((hdr_size == sizeof(struct icmp_packet_rr)) && |
| 1007 | (icmp_packet->rr_cur < BAT_RR_LEN)) { |
| 1008 | memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]), |
| 1009 | ethhdr->h_dest, ETH_ALEN); |
| 1010 | icmp_packet->rr_cur++; |
| 1011 | } |
| 1012 | |
| 1013 | /* packet for me */ |
| 1014 | if (is_my_mac(icmp_packet->dst)) |
| 1015 | return recv_my_icmp_packet(bat_priv, skb, hdr_size); |
| 1016 | |
| 1017 | /* TTL exceeded */ |
| 1018 | if (icmp_packet->ttl < 2) |
Simon Wunderlich | 74ef115 | 2010-12-29 16:15:19 +0000 | [diff] [blame] | 1019 | return recv_icmp_ttl_exceeded(bat_priv, skb); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1020 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1021 | /* get routing information */ |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 1022 | orig_node = orig_hash_find(bat_priv, icmp_packet->dst); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1023 | if (!orig_node) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 1024 | goto out; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1025 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 1026 | router = orig_node_get_router(orig_node); |
| 1027 | if (!router) |
| 1028 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1029 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1030 | /* create a copy of the skb, if needed, to modify it. */ |
| 1031 | if (skb_cow(skb, sizeof(struct ethhdr)) < 0) |
| 1032 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1033 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1034 | icmp_packet = (struct icmp_packet_rr *)skb->data; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1035 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1036 | /* decrement ttl */ |
| 1037 | icmp_packet->ttl--; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1038 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1039 | /* route it */ |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 1040 | send_skb_packet(skb, router->if_incoming, router->addr); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1041 | ret = NET_RX_SUCCESS; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1042 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1043 | out: |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 1044 | if (router) |
| 1045 | neigh_node_free_ref(router); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1046 | if (orig_node) |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 1047 | orig_node_free_ref(orig_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1048 | return ret; |
| 1049 | } |
| 1050 | |
Linus Lüssing | 5515862 | 2011-03-14 22:43:27 +0000 | [diff] [blame] | 1051 | /* In the bonding case, send the packets in a round |
| 1052 | * robin fashion over the remaining interfaces. |
| 1053 | * |
| 1054 | * This method rotates the bonding list and increases the |
| 1055 | * returned router's refcount. */ |
| 1056 | static struct neigh_node *find_bond_router(struct orig_node *primary_orig, |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 1057 | const struct hard_iface *recv_if) |
Linus Lüssing | 5515862 | 2011-03-14 22:43:27 +0000 | [diff] [blame] | 1058 | { |
| 1059 | struct neigh_node *tmp_neigh_node; |
| 1060 | struct neigh_node *router = NULL, *first_candidate = NULL; |
| 1061 | |
| 1062 | rcu_read_lock(); |
| 1063 | list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list, |
| 1064 | bonding_list) { |
| 1065 | if (!first_candidate) |
| 1066 | first_candidate = tmp_neigh_node; |
| 1067 | |
| 1068 | /* recv_if == NULL on the first node. */ |
| 1069 | if (tmp_neigh_node->if_incoming == recv_if) |
| 1070 | continue; |
| 1071 | |
| 1072 | if (!atomic_inc_not_zero(&tmp_neigh_node->refcount)) |
| 1073 | continue; |
| 1074 | |
| 1075 | router = tmp_neigh_node; |
| 1076 | break; |
| 1077 | } |
| 1078 | |
| 1079 | /* use the first candidate if nothing was found. */ |
| 1080 | if (!router && first_candidate && |
| 1081 | atomic_inc_not_zero(&first_candidate->refcount)) |
| 1082 | router = first_candidate; |
| 1083 | |
| 1084 | if (!router) |
| 1085 | goto out; |
| 1086 | |
| 1087 | /* selected should point to the next element |
| 1088 | * after the current router */ |
| 1089 | spin_lock_bh(&primary_orig->neigh_list_lock); |
| 1090 | /* this is a list_move(), which unfortunately |
| 1091 | * does not exist as rcu version */ |
| 1092 | list_del_rcu(&primary_orig->bond_list); |
| 1093 | list_add_rcu(&primary_orig->bond_list, |
| 1094 | &router->bonding_list); |
| 1095 | spin_unlock_bh(&primary_orig->neigh_list_lock); |
| 1096 | |
| 1097 | out: |
| 1098 | rcu_read_unlock(); |
| 1099 | return router; |
| 1100 | } |
| 1101 | |
| 1102 | /* Interface Alternating: Use the best of the |
| 1103 | * remaining candidates which are not using |
| 1104 | * this interface. |
| 1105 | * |
| 1106 | * Increases the returned router's refcount */ |
| 1107 | static struct neigh_node *find_ifalter_router(struct orig_node *primary_orig, |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 1108 | const struct hard_iface *recv_if) |
Linus Lüssing | 5515862 | 2011-03-14 22:43:27 +0000 | [diff] [blame] | 1109 | { |
| 1110 | struct neigh_node *tmp_neigh_node; |
| 1111 | struct neigh_node *router = NULL, *first_candidate = NULL; |
| 1112 | |
| 1113 | rcu_read_lock(); |
| 1114 | list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list, |
| 1115 | bonding_list) { |
| 1116 | if (!first_candidate) |
| 1117 | first_candidate = tmp_neigh_node; |
| 1118 | |
| 1119 | /* recv_if == NULL on the first node. */ |
| 1120 | if (tmp_neigh_node->if_incoming == recv_if) |
| 1121 | continue; |
| 1122 | |
| 1123 | if (!atomic_inc_not_zero(&tmp_neigh_node->refcount)) |
| 1124 | continue; |
| 1125 | |
| 1126 | /* if we don't have a router yet |
| 1127 | * or this one is better, choose it. */ |
| 1128 | if ((!router) || |
| 1129 | (tmp_neigh_node->tq_avg > router->tq_avg)) { |
| 1130 | /* decrement refcount of |
| 1131 | * previously selected router */ |
| 1132 | if (router) |
| 1133 | neigh_node_free_ref(router); |
| 1134 | |
| 1135 | router = tmp_neigh_node; |
| 1136 | atomic_inc_not_zero(&router->refcount); |
| 1137 | } |
| 1138 | |
| 1139 | neigh_node_free_ref(tmp_neigh_node); |
| 1140 | } |
| 1141 | |
| 1142 | /* use the first candidate if nothing was found. */ |
| 1143 | if (!router && first_candidate && |
| 1144 | atomic_inc_not_zero(&first_candidate->refcount)) |
| 1145 | router = first_candidate; |
| 1146 | |
| 1147 | rcu_read_unlock(); |
| 1148 | return router; |
| 1149 | } |
| 1150 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1151 | int recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if) |
| 1152 | { |
| 1153 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
| 1154 | struct tt_query_packet *tt_query; |
| 1155 | struct ethhdr *ethhdr; |
| 1156 | |
| 1157 | /* drop packet if it has not necessary minimum size */ |
| 1158 | if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet)))) |
| 1159 | goto out; |
| 1160 | |
| 1161 | /* I could need to modify it */ |
| 1162 | if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0) |
| 1163 | goto out; |
| 1164 | |
| 1165 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 1166 | |
| 1167 | /* packet with unicast indication but broadcast recipient */ |
| 1168 | if (is_broadcast_ether_addr(ethhdr->h_dest)) |
| 1169 | goto out; |
| 1170 | |
| 1171 | /* packet with broadcast sender address */ |
| 1172 | if (is_broadcast_ether_addr(ethhdr->h_source)) |
| 1173 | goto out; |
| 1174 | |
| 1175 | tt_query = (struct tt_query_packet *)skb->data; |
| 1176 | |
| 1177 | tt_query->tt_data = ntohs(tt_query->tt_data); |
| 1178 | |
| 1179 | switch (tt_query->flags & TT_QUERY_TYPE_MASK) { |
| 1180 | case TT_REQUEST: |
| 1181 | /* If we cannot provide an answer the tt_request is |
| 1182 | * forwarded */ |
| 1183 | if (!send_tt_response(bat_priv, tt_query)) { |
| 1184 | bat_dbg(DBG_TT, bat_priv, |
| 1185 | "Routing TT_REQUEST to %pM [%c]\n", |
| 1186 | tt_query->dst, |
| 1187 | (tt_query->flags & TT_FULL_TABLE ? 'F' : '.')); |
| 1188 | tt_query->tt_data = htons(tt_query->tt_data); |
| 1189 | return route_unicast_packet(skb, recv_if); |
| 1190 | } |
| 1191 | break; |
| 1192 | case TT_RESPONSE: |
Antonio Quartulli | 015758d | 2011-07-09 17:52:13 +0200 | [diff] [blame] | 1193 | /* packet needs to be linearized to access the TT changes */ |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1194 | if (skb_linearize(skb) < 0) |
| 1195 | goto out; |
| 1196 | |
| 1197 | if (is_my_mac(tt_query->dst)) |
| 1198 | handle_tt_response(bat_priv, tt_query); |
| 1199 | else { |
| 1200 | bat_dbg(DBG_TT, bat_priv, |
| 1201 | "Routing TT_RESPONSE to %pM [%c]\n", |
| 1202 | tt_query->dst, |
| 1203 | (tt_query->flags & TT_FULL_TABLE ? 'F' : '.')); |
| 1204 | tt_query->tt_data = htons(tt_query->tt_data); |
| 1205 | return route_unicast_packet(skb, recv_if); |
| 1206 | } |
| 1207 | break; |
| 1208 | } |
| 1209 | |
| 1210 | out: |
| 1211 | /* returning NET_RX_DROP will make the caller function kfree the skb */ |
| 1212 | return NET_RX_DROP; |
| 1213 | } |
| 1214 | |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1215 | int recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if) |
| 1216 | { |
| 1217 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
| 1218 | struct roam_adv_packet *roam_adv_packet; |
| 1219 | struct orig_node *orig_node; |
| 1220 | struct ethhdr *ethhdr; |
| 1221 | |
| 1222 | /* drop packet if it has not necessary minimum size */ |
| 1223 | if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet)))) |
| 1224 | goto out; |
| 1225 | |
| 1226 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 1227 | |
| 1228 | /* packet with unicast indication but broadcast recipient */ |
| 1229 | if (is_broadcast_ether_addr(ethhdr->h_dest)) |
| 1230 | goto out; |
| 1231 | |
| 1232 | /* packet with broadcast sender address */ |
| 1233 | if (is_broadcast_ether_addr(ethhdr->h_source)) |
| 1234 | goto out; |
| 1235 | |
| 1236 | roam_adv_packet = (struct roam_adv_packet *)skb->data; |
| 1237 | |
| 1238 | if (!is_my_mac(roam_adv_packet->dst)) |
| 1239 | return route_unicast_packet(skb, recv_if); |
| 1240 | |
| 1241 | orig_node = orig_hash_find(bat_priv, roam_adv_packet->src); |
| 1242 | if (!orig_node) |
| 1243 | goto out; |
| 1244 | |
| 1245 | bat_dbg(DBG_TT, bat_priv, "Received ROAMING_ADV from %pM " |
| 1246 | "(client %pM)\n", roam_adv_packet->src, |
| 1247 | roam_adv_packet->client); |
| 1248 | |
| 1249 | tt_global_add(bat_priv, orig_node, roam_adv_packet->client, |
Antonio Quartulli | bc27908 | 2011-07-07 15:35:35 +0200 | [diff] [blame] | 1250 | atomic_read(&orig_node->last_ttvn) + 1, true, false); |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1251 | |
| 1252 | /* Roaming phase starts: I have new information but the ttvn has not |
| 1253 | * been incremented yet. This flag will make me check all the incoming |
| 1254 | * packets for the correct destination. */ |
| 1255 | bat_priv->tt_poss_change = true; |
| 1256 | |
| 1257 | orig_node_free_ref(orig_node); |
| 1258 | out: |
| 1259 | /* returning NET_RX_DROP will make the caller function kfree the skb */ |
| 1260 | return NET_RX_DROP; |
| 1261 | } |
| 1262 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1263 | /* find a suitable router for this originator, and use |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 1264 | * bonding if possible. increases the found neighbors |
| 1265 | * refcount.*/ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1266 | struct neigh_node *find_router(struct bat_priv *bat_priv, |
| 1267 | struct orig_node *orig_node, |
Sven Eckelmann | 747e422 | 2011-05-14 23:14:50 +0200 | [diff] [blame] | 1268 | const struct hard_iface *recv_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1269 | { |
| 1270 | struct orig_node *primary_orig_node; |
| 1271 | struct orig_node *router_orig; |
Linus Lüssing | 5515862 | 2011-03-14 22:43:27 +0000 | [diff] [blame] | 1272 | struct neigh_node *router; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1273 | static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0}; |
| 1274 | int bonding_enabled; |
| 1275 | |
| 1276 | if (!orig_node) |
| 1277 | return NULL; |
| 1278 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 1279 | router = orig_node_get_router(orig_node); |
| 1280 | if (!router) |
Marek Lindner | 01df2b6 | 2011-05-05 14:14:46 +0200 | [diff] [blame] | 1281 | goto err; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1282 | |
| 1283 | /* without bonding, the first node should |
| 1284 | * always choose the default router. */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1285 | bonding_enabled = atomic_read(&bat_priv->bonding); |
| 1286 | |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 1287 | rcu_read_lock(); |
| 1288 | /* select default router to output */ |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 1289 | router_orig = router->orig_node; |
Marek Lindner | 01df2b6 | 2011-05-05 14:14:46 +0200 | [diff] [blame] | 1290 | if (!router_orig) |
| 1291 | goto err_unlock; |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 1292 | |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 1293 | if ((!recv_if) && (!bonding_enabled)) |
| 1294 | goto return_router; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1295 | |
| 1296 | /* if we have something in the primary_addr, we can search |
| 1297 | * for a potential bonding candidate. */ |
Marek Lindner | 39901e7 | 2011-02-18 12:28:08 +0000 | [diff] [blame] | 1298 | if (compare_eth(router_orig->primary_addr, zero_mac)) |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 1299 | goto return_router; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1300 | |
| 1301 | /* find the orig_node which has the primary interface. might |
| 1302 | * even be the same as our router_orig in many cases */ |
| 1303 | |
Marek Lindner | 39901e7 | 2011-02-18 12:28:08 +0000 | [diff] [blame] | 1304 | if (compare_eth(router_orig->primary_addr, router_orig->orig)) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1305 | primary_orig_node = router_orig; |
| 1306 | } else { |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 1307 | primary_orig_node = orig_hash_find(bat_priv, |
| 1308 | router_orig->primary_addr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1309 | if (!primary_orig_node) |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 1310 | goto return_router; |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 1311 | |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 1312 | orig_node_free_ref(primary_orig_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | /* with less than 2 candidates, we can't do any |
| 1316 | * bonding and prefer the original router. */ |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 1317 | if (atomic_read(&primary_orig_node->bond_candidates) < 2) |
| 1318 | goto return_router; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1319 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1320 | /* all nodes between should choose a candidate which |
| 1321 | * is is not on the interface where the packet came |
| 1322 | * in. */ |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 1323 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1324 | neigh_node_free_ref(router); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1325 | |
Linus Lüssing | 5515862 | 2011-03-14 22:43:27 +0000 | [diff] [blame] | 1326 | if (bonding_enabled) |
| 1327 | router = find_bond_router(primary_orig_node, recv_if); |
| 1328 | else |
| 1329 | router = find_ifalter_router(primary_orig_node, recv_if); |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 1330 | |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 1331 | return_router: |
Antonio Quartulli | e2cbc11 | 2011-05-08 20:52:57 +0200 | [diff] [blame] | 1332 | if (router && router->if_incoming->if_status != IF_ACTIVE) |
| 1333 | goto err_unlock; |
| 1334 | |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 1335 | rcu_read_unlock(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1336 | return router; |
Marek Lindner | 01df2b6 | 2011-05-05 14:14:46 +0200 | [diff] [blame] | 1337 | err_unlock: |
| 1338 | rcu_read_unlock(); |
| 1339 | err: |
| 1340 | if (router) |
| 1341 | neigh_node_free_ref(router); |
| 1342 | return NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | static int check_unicast_packet(struct sk_buff *skb, int hdr_size) |
| 1346 | { |
| 1347 | struct ethhdr *ethhdr; |
| 1348 | |
| 1349 | /* drop packet if it has not necessary minimum size */ |
| 1350 | if (unlikely(!pskb_may_pull(skb, hdr_size))) |
| 1351 | return -1; |
| 1352 | |
| 1353 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 1354 | |
| 1355 | /* packet with unicast indication but broadcast recipient */ |
| 1356 | if (is_broadcast_ether_addr(ethhdr->h_dest)) |
| 1357 | return -1; |
| 1358 | |
| 1359 | /* packet with broadcast sender address */ |
| 1360 | if (is_broadcast_ether_addr(ethhdr->h_source)) |
| 1361 | return -1; |
| 1362 | |
| 1363 | /* not for me */ |
| 1364 | if (!is_my_mac(ethhdr->h_dest)) |
| 1365 | return -1; |
| 1366 | |
| 1367 | return 0; |
| 1368 | } |
| 1369 | |
Linus Lüssing | 7cefb14 | 2011-03-02 17:39:31 +0000 | [diff] [blame] | 1370 | 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] | 1371 | { |
| 1372 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1373 | struct orig_node *orig_node = NULL; |
| 1374 | struct neigh_node *neigh_node = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1375 | struct unicast_packet *unicast_packet; |
| 1376 | struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1377 | int ret = NET_RX_DROP; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1378 | struct sk_buff *new_skb; |
| 1379 | |
| 1380 | unicast_packet = (struct unicast_packet *)skb->data; |
| 1381 | |
| 1382 | /* TTL exceeded */ |
| 1383 | if (unicast_packet->ttl < 2) { |
| 1384 | pr_debug("Warning - can't forward unicast packet from %pM to " |
| 1385 | "%pM: ttl exceeded\n", ethhdr->h_source, |
| 1386 | unicast_packet->dest); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1387 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | /* get routing information */ |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 1391 | orig_node = orig_hash_find(bat_priv, unicast_packet->dest); |
| 1392 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1393 | if (!orig_node) |
Antonio Quartulli | b5a6f69 | 2011-04-16 11:30:57 +0200 | [diff] [blame] | 1394 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1395 | |
Simon Wunderlich | a4c135c | 2011-01-19 20:01:43 +0000 | [diff] [blame] | 1396 | /* find_router() increases neigh_nodes refcount if found. */ |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1397 | neigh_node = find_router(bat_priv, orig_node, recv_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1398 | |
Marek Lindner | d007260 | 2011-01-19 20:01:44 +0000 | [diff] [blame] | 1399 | if (!neigh_node) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1400 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1401 | |
| 1402 | /* create a copy of the skb, if needed, to modify it. */ |
| 1403 | if (skb_cow(skb, sizeof(struct ethhdr)) < 0) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1404 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1405 | |
| 1406 | unicast_packet = (struct unicast_packet *)skb->data; |
| 1407 | |
| 1408 | if (unicast_packet->packet_type == BAT_UNICAST && |
| 1409 | atomic_read(&bat_priv->fragmentation) && |
Marek Lindner | d007260 | 2011-01-19 20:01:44 +0000 | [diff] [blame] | 1410 | skb->len > neigh_node->if_incoming->net_dev->mtu) { |
| 1411 | ret = frag_send_skb(skb, bat_priv, |
| 1412 | neigh_node->if_incoming, neigh_node->addr); |
| 1413 | goto out; |
| 1414 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1415 | |
| 1416 | if (unicast_packet->packet_type == BAT_UNICAST_FRAG && |
Marek Lindner | d007260 | 2011-01-19 20:01:44 +0000 | [diff] [blame] | 1417 | frag_can_reassemble(skb, neigh_node->if_incoming->net_dev->mtu)) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1418 | |
| 1419 | ret = frag_reassemble_skb(skb, bat_priv, &new_skb); |
| 1420 | |
| 1421 | if (ret == NET_RX_DROP) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1422 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1423 | |
| 1424 | /* packet was buffered for late merge */ |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1425 | if (!new_skb) { |
| 1426 | ret = NET_RX_SUCCESS; |
| 1427 | goto out; |
| 1428 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1429 | |
| 1430 | skb = new_skb; |
| 1431 | unicast_packet = (struct unicast_packet *)skb->data; |
| 1432 | } |
| 1433 | |
| 1434 | /* decrement ttl */ |
| 1435 | unicast_packet->ttl--; |
| 1436 | |
| 1437 | /* route it */ |
Marek Lindner | d007260 | 2011-01-19 20:01:44 +0000 | [diff] [blame] | 1438 | send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1439 | ret = NET_RX_SUCCESS; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1440 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1441 | out: |
| 1442 | if (neigh_node) |
| 1443 | neigh_node_free_ref(neigh_node); |
| 1444 | if (orig_node) |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 1445 | orig_node_free_ref(orig_node); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 1446 | return ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1449 | static int check_unicast_ttvn(struct bat_priv *bat_priv, |
| 1450 | struct sk_buff *skb) { |
| 1451 | uint8_t curr_ttvn; |
| 1452 | struct orig_node *orig_node; |
| 1453 | struct ethhdr *ethhdr; |
| 1454 | struct hard_iface *primary_if; |
| 1455 | struct unicast_packet *unicast_packet; |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1456 | bool tt_poss_change; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1457 | |
| 1458 | /* I could need to modify it */ |
| 1459 | if (skb_cow(skb, sizeof(struct unicast_packet)) < 0) |
| 1460 | return 0; |
| 1461 | |
| 1462 | unicast_packet = (struct unicast_packet *)skb->data; |
| 1463 | |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1464 | if (is_my_mac(unicast_packet->dest)) { |
| 1465 | tt_poss_change = bat_priv->tt_poss_change; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1466 | curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn); |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1467 | } else { |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1468 | orig_node = orig_hash_find(bat_priv, unicast_packet->dest); |
| 1469 | |
| 1470 | if (!orig_node) |
| 1471 | return 0; |
| 1472 | |
| 1473 | curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn); |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1474 | tt_poss_change = orig_node->tt_poss_change; |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1475 | orig_node_free_ref(orig_node); |
| 1476 | } |
| 1477 | |
| 1478 | /* Check whether I have to reroute the packet */ |
Antonio Quartulli | cc47f66 | 2011-04-27 14:27:57 +0200 | [diff] [blame] | 1479 | if (seq_before(unicast_packet->ttvn, curr_ttvn) || tt_poss_change) { |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1480 | /* Linearize the skb before accessing it */ |
| 1481 | if (skb_linearize(skb) < 0) |
| 1482 | return 0; |
| 1483 | |
| 1484 | ethhdr = (struct ethhdr *)(skb->data + |
| 1485 | sizeof(struct unicast_packet)); |
Antonio Quartulli | 3d393e4 | 2011-07-07 15:35:37 +0200 | [diff] [blame] | 1486 | orig_node = transtable_search(bat_priv, NULL, ethhdr->h_dest); |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1487 | |
| 1488 | if (!orig_node) { |
| 1489 | if (!is_my_client(bat_priv, ethhdr->h_dest)) |
| 1490 | return 0; |
| 1491 | primary_if = primary_if_get_selected(bat_priv); |
| 1492 | if (!primary_if) |
| 1493 | return 0; |
| 1494 | memcpy(unicast_packet->dest, |
| 1495 | primary_if->net_dev->dev_addr, ETH_ALEN); |
| 1496 | hardif_free_ref(primary_if); |
| 1497 | } else { |
| 1498 | memcpy(unicast_packet->dest, orig_node->orig, |
| 1499 | ETH_ALEN); |
| 1500 | curr_ttvn = (uint8_t) |
| 1501 | atomic_read(&orig_node->last_ttvn); |
| 1502 | orig_node_free_ref(orig_node); |
| 1503 | } |
| 1504 | |
| 1505 | bat_dbg(DBG_ROUTES, bat_priv, "TTVN mismatch (old_ttvn %u " |
| 1506 | "new_ttvn %u)! Rerouting unicast packet (for %pM) to " |
| 1507 | "%pM\n", unicast_packet->ttvn, curr_ttvn, |
| 1508 | ethhdr->h_dest, unicast_packet->dest); |
| 1509 | |
| 1510 | unicast_packet->ttvn = curr_ttvn; |
| 1511 | } |
| 1512 | return 1; |
| 1513 | } |
| 1514 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 1515 | int recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1516 | { |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1517 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1518 | struct unicast_packet *unicast_packet; |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 1519 | int hdr_size = sizeof(*unicast_packet); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1520 | |
| 1521 | if (check_unicast_packet(skb, hdr_size) < 0) |
| 1522 | return NET_RX_DROP; |
| 1523 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1524 | if (!check_unicast_ttvn(bat_priv, skb)) |
| 1525 | return NET_RX_DROP; |
| 1526 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1527 | unicast_packet = (struct unicast_packet *)skb->data; |
| 1528 | |
| 1529 | /* packet for me */ |
| 1530 | if (is_my_mac(unicast_packet->dest)) { |
| 1531 | interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size); |
| 1532 | return NET_RX_SUCCESS; |
| 1533 | } |
| 1534 | |
Linus Lüssing | 7cefb14 | 2011-03-02 17:39:31 +0000 | [diff] [blame] | 1535 | return route_unicast_packet(skb, recv_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 1538 | int recv_ucast_frag_packet(struct sk_buff *skb, struct hard_iface *recv_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1539 | { |
| 1540 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
| 1541 | struct unicast_frag_packet *unicast_packet; |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 1542 | int hdr_size = sizeof(*unicast_packet); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1543 | struct sk_buff *new_skb = NULL; |
| 1544 | int ret; |
| 1545 | |
| 1546 | if (check_unicast_packet(skb, hdr_size) < 0) |
| 1547 | return NET_RX_DROP; |
| 1548 | |
Antonio Quartulli | a73105b | 2011-04-27 14:27:44 +0200 | [diff] [blame] | 1549 | if (!check_unicast_ttvn(bat_priv, skb)) |
| 1550 | return NET_RX_DROP; |
| 1551 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1552 | unicast_packet = (struct unicast_frag_packet *)skb->data; |
| 1553 | |
| 1554 | /* packet for me */ |
| 1555 | if (is_my_mac(unicast_packet->dest)) { |
| 1556 | |
| 1557 | ret = frag_reassemble_skb(skb, bat_priv, &new_skb); |
| 1558 | |
| 1559 | if (ret == NET_RX_DROP) |
| 1560 | return NET_RX_DROP; |
| 1561 | |
| 1562 | /* packet was buffered for late merge */ |
| 1563 | if (!new_skb) |
| 1564 | return NET_RX_SUCCESS; |
| 1565 | |
| 1566 | interface_rx(recv_if->soft_iface, new_skb, recv_if, |
| 1567 | sizeof(struct unicast_packet)); |
| 1568 | return NET_RX_SUCCESS; |
| 1569 | } |
| 1570 | |
Linus Lüssing | 7cefb14 | 2011-03-02 17:39:31 +0000 | [diff] [blame] | 1571 | return route_unicast_packet(skb, recv_if); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 1575 | int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1576 | { |
| 1577 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1578 | struct orig_node *orig_node = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1579 | struct bcast_packet *bcast_packet; |
| 1580 | struct ethhdr *ethhdr; |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 1581 | int hdr_size = sizeof(*bcast_packet); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1582 | int ret = NET_RX_DROP; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1583 | int32_t seq_diff; |
| 1584 | |
| 1585 | /* drop packet if it has not necessary minimum size */ |
| 1586 | if (unlikely(!pskb_may_pull(skb, hdr_size))) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1587 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1588 | |
| 1589 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 1590 | |
| 1591 | /* packet with broadcast indication but unicast recipient */ |
| 1592 | if (!is_broadcast_ether_addr(ethhdr->h_dest)) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1593 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1594 | |
| 1595 | /* packet with broadcast sender address */ |
| 1596 | if (is_broadcast_ether_addr(ethhdr->h_source)) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1597 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1598 | |
| 1599 | /* ignore broadcasts sent by myself */ |
| 1600 | if (is_my_mac(ethhdr->h_source)) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1601 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1602 | |
| 1603 | bcast_packet = (struct bcast_packet *)skb->data; |
| 1604 | |
| 1605 | /* ignore broadcasts originated by myself */ |
| 1606 | if (is_my_mac(bcast_packet->orig)) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1607 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1608 | |
| 1609 | if (bcast_packet->ttl < 2) |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1610 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1611 | |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 1612 | orig_node = orig_hash_find(bat_priv, bcast_packet->orig); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1613 | |
| 1614 | if (!orig_node) |
Antonio Quartulli | b5a6f69 | 2011-04-16 11:30:57 +0200 | [diff] [blame] | 1615 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1616 | |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1617 | spin_lock_bh(&orig_node->bcast_seqno_lock); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1618 | |
| 1619 | /* check whether the packet is a duplicate */ |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1620 | if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno, |
| 1621 | ntohl(bcast_packet->seqno))) |
| 1622 | goto spin_unlock; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1623 | |
| 1624 | seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno; |
| 1625 | |
| 1626 | /* check whether the packet is old and the host just restarted. */ |
| 1627 | if (window_protected(bat_priv, seq_diff, |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1628 | &orig_node->bcast_seqno_reset)) |
| 1629 | goto spin_unlock; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1630 | |
| 1631 | /* mark broadcast in flood history, update window position |
| 1632 | * if required. */ |
| 1633 | if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1)) |
| 1634 | orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno); |
| 1635 | |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1636 | spin_unlock_bh(&orig_node->bcast_seqno_lock); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1637 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1638 | /* rebroadcast packet */ |
Antonio Quartulli | 8698529 | 2011-06-25 19:09:12 +0200 | [diff] [blame] | 1639 | add_bcast_packet_to_list(bat_priv, skb, 1); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1640 | |
| 1641 | /* broadcast for me */ |
| 1642 | interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1643 | ret = NET_RX_SUCCESS; |
| 1644 | goto out; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1645 | |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1646 | spin_unlock: |
| 1647 | spin_unlock_bh(&orig_node->bcast_seqno_lock); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1648 | out: |
| 1649 | if (orig_node) |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 1650 | orig_node_free_ref(orig_node); |
Marek Lindner | f3e0008 | 2011-01-25 21:52:11 +0000 | [diff] [blame] | 1651 | return ret; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Marek Lindner | e6c10f4 | 2011-02-18 12:33:20 +0000 | [diff] [blame] | 1654 | int recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1655 | { |
| 1656 | struct vis_packet *vis_packet; |
| 1657 | struct ethhdr *ethhdr; |
| 1658 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 1659 | int hdr_size = sizeof(*vis_packet); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1660 | |
| 1661 | /* keep skb linear */ |
| 1662 | if (skb_linearize(skb) < 0) |
| 1663 | return NET_RX_DROP; |
| 1664 | |
| 1665 | if (unlikely(!pskb_may_pull(skb, hdr_size))) |
| 1666 | return NET_RX_DROP; |
| 1667 | |
| 1668 | vis_packet = (struct vis_packet *)skb->data; |
| 1669 | ethhdr = (struct ethhdr *)skb_mac_header(skb); |
| 1670 | |
| 1671 | /* not for me */ |
| 1672 | if (!is_my_mac(ethhdr->h_dest)) |
| 1673 | return NET_RX_DROP; |
| 1674 | |
| 1675 | /* ignore own packets */ |
| 1676 | if (is_my_mac(vis_packet->vis_orig)) |
| 1677 | return NET_RX_DROP; |
| 1678 | |
| 1679 | if (is_my_mac(vis_packet->sender_orig)) |
| 1680 | return NET_RX_DROP; |
| 1681 | |
| 1682 | switch (vis_packet->vis_type) { |
| 1683 | case VIS_TYPE_SERVER_SYNC: |
| 1684 | receive_server_sync_packet(bat_priv, vis_packet, |
| 1685 | skb_headlen(skb)); |
| 1686 | break; |
| 1687 | |
| 1688 | case VIS_TYPE_CLIENT_UPDATE: |
| 1689 | receive_client_update_packet(bat_priv, vis_packet, |
| 1690 | skb_headlen(skb)); |
| 1691 | break; |
| 1692 | |
| 1693 | default: /* ignore unknown packet */ |
| 1694 | break; |
| 1695 | } |
| 1696 | |
| 1697 | /* We take a copy of the data in the packet, so we should |
| 1698 | always free the skbuf. */ |
| 1699 | return NET_RX_DROP; |
| 1700 | } |