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