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