Antonio Quartulli | 0b87393 | 2013-01-04 03:05:31 +0100 | [diff] [blame] | 1 | /* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors: |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 2 | * |
| 3 | * Marek Lindner, Simon Wunderlich |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of version 2 of the GNU General Public |
| 7 | * License as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 | * 02110-1301, USA |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "main.h" |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 21 | #include "translation-table.h" |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 22 | #include "originator.h" |
| 23 | #include "routing.h" |
| 24 | #include "gateway_common.h" |
| 25 | #include "gateway_client.h" |
| 26 | #include "hard-interface.h" |
| 27 | #include "send.h" |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 28 | #include "bat_algo.h" |
Martin Hundebøll | d56b170 | 2013-01-25 11:12:39 +0100 | [diff] [blame] | 29 | #include "network-coding.h" |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 30 | |
Antonio Quartulli | 791c2a2 | 2013-08-17 12:44:44 +0200 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * batadv_dup_status - duplicate status |
| 34 | * @BATADV_NO_DUP: the packet is a duplicate |
| 35 | * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the |
| 36 | * neighbor) |
| 37 | * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor |
| 38 | * @BATADV_PROTECTED: originator is currently protected (after reboot) |
| 39 | */ |
| 40 | enum batadv_dup_status { |
| 41 | BATADV_NO_DUP = 0, |
| 42 | BATADV_ORIG_DUP, |
| 43 | BATADV_NEIGH_DUP, |
| 44 | BATADV_PROTECTED, |
| 45 | }; |
| 46 | |
Antonio Quartulli | 24a5dee | 2013-04-08 09:38:12 +0200 | [diff] [blame] | 47 | /** |
| 48 | * batadv_ring_buffer_set - update the ring buffer with the given value |
| 49 | * @lq_recv: pointer to the ring buffer |
| 50 | * @lq_index: index to store the value at |
| 51 | * @value: value to store in the ring buffer |
| 52 | */ |
| 53 | static void batadv_ring_buffer_set(uint8_t lq_recv[], uint8_t *lq_index, |
| 54 | uint8_t value) |
| 55 | { |
| 56 | lq_recv[*lq_index] = value; |
| 57 | *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * batadv_ring_buffer_set - compute the average of all non-zero values stored |
| 62 | * in the given ring buffer |
| 63 | * @lq_recv: pointer to the ring buffer |
| 64 | * |
| 65 | * Returns computed average value. |
| 66 | */ |
| 67 | static uint8_t batadv_ring_buffer_avg(const uint8_t lq_recv[]) |
| 68 | { |
| 69 | const uint8_t *ptr; |
| 70 | uint16_t count = 0, i = 0, sum = 0; |
| 71 | |
| 72 | ptr = lq_recv; |
| 73 | |
| 74 | while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) { |
| 75 | if (*ptr != 0) { |
| 76 | count++; |
| 77 | sum += *ptr; |
| 78 | } |
| 79 | |
| 80 | i++; |
| 81 | ptr++; |
| 82 | } |
| 83 | |
| 84 | if (count == 0) |
| 85 | return 0; |
| 86 | |
| 87 | return (uint8_t)(sum / count); |
| 88 | } |
David S. Miller | d98cae64e | 2013-06-19 16:49:39 -0700 | [diff] [blame] | 89 | |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 90 | /** |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 91 | * batadv_iv_ogm_orig_free - free the private resources allocated for this |
| 92 | * orig_node |
| 93 | * @orig_node: the orig_node for which the resources have to be free'd |
| 94 | */ |
| 95 | static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node) |
| 96 | { |
| 97 | kfree(orig_node->bat_iv.bcast_own); |
| 98 | kfree(orig_node->bat_iv.bcast_own_sum); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * batadv_iv_ogm_orig_add_if - change the private structures of the orig_node to |
| 103 | * include the new hard-interface |
| 104 | * @orig_node: the orig_node that has to be changed |
| 105 | * @max_if_num: the current amount of interfaces |
| 106 | * |
| 107 | * Returns 0 on success, a negative error code otherwise. |
| 108 | */ |
| 109 | static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node, |
| 110 | int max_if_num) |
| 111 | { |
| 112 | void *data_ptr; |
| 113 | size_t data_size, old_size; |
| 114 | int ret = -ENOMEM; |
| 115 | |
| 116 | spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
| 117 | |
| 118 | data_size = max_if_num * sizeof(unsigned long) * BATADV_NUM_WORDS; |
| 119 | old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS; |
| 120 | data_ptr = kmalloc(data_size, GFP_ATOMIC); |
| 121 | if (!data_ptr) |
| 122 | goto unlock; |
| 123 | |
| 124 | memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size); |
| 125 | kfree(orig_node->bat_iv.bcast_own); |
| 126 | orig_node->bat_iv.bcast_own = data_ptr; |
| 127 | |
| 128 | data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC); |
| 129 | if (!data_ptr) { |
| 130 | kfree(orig_node->bat_iv.bcast_own); |
| 131 | goto unlock; |
| 132 | } |
| 133 | |
| 134 | memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum, |
| 135 | (max_if_num - 1) * sizeof(uint8_t)); |
| 136 | kfree(orig_node->bat_iv.bcast_own_sum); |
| 137 | orig_node->bat_iv.bcast_own_sum = data_ptr; |
| 138 | |
| 139 | ret = 0; |
| 140 | |
| 141 | unlock: |
| 142 | spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
| 143 | |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to |
| 149 | * exclude the removed interface |
| 150 | * @orig_node: the orig_node that has to be changed |
| 151 | * @max_if_num: the current amount of interfaces |
| 152 | * @del_if_num: the index of the interface being removed |
| 153 | * |
| 154 | * Returns 0 on success, a negative error code otherwise. |
| 155 | */ |
| 156 | static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node, |
| 157 | int max_if_num, int del_if_num) |
| 158 | { |
| 159 | int chunk_size, ret = -ENOMEM, if_offset; |
| 160 | void *data_ptr = NULL; |
| 161 | |
| 162 | spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
| 163 | |
| 164 | /* last interface was removed */ |
| 165 | if (max_if_num == 0) |
| 166 | goto free_bcast_own; |
| 167 | |
| 168 | chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS; |
| 169 | data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC); |
| 170 | if (!data_ptr) |
| 171 | goto unlock; |
| 172 | |
| 173 | /* copy first part */ |
| 174 | memcpy(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size); |
| 175 | |
| 176 | /* copy second part */ |
| 177 | memcpy((char *)data_ptr + del_if_num * chunk_size, |
| 178 | orig_node->bat_iv.bcast_own + ((del_if_num + 1) * chunk_size), |
| 179 | (max_if_num - del_if_num) * chunk_size); |
| 180 | |
| 181 | free_bcast_own: |
| 182 | kfree(orig_node->bat_iv.bcast_own); |
| 183 | orig_node->bat_iv.bcast_own = data_ptr; |
| 184 | |
| 185 | if (max_if_num == 0) |
| 186 | goto free_own_sum; |
| 187 | |
| 188 | data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC); |
| 189 | if (!data_ptr) { |
| 190 | kfree(orig_node->bat_iv.bcast_own); |
| 191 | goto unlock; |
| 192 | } |
| 193 | |
| 194 | memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum, |
| 195 | del_if_num * sizeof(uint8_t)); |
| 196 | |
| 197 | if_offset = (del_if_num + 1) * sizeof(uint8_t); |
| 198 | memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t), |
| 199 | orig_node->bat_iv.bcast_own_sum + if_offset, |
| 200 | (max_if_num - del_if_num) * sizeof(uint8_t)); |
| 201 | |
| 202 | free_own_sum: |
| 203 | kfree(orig_node->bat_iv.bcast_own_sum); |
| 204 | orig_node->bat_iv.bcast_own_sum = data_ptr; |
| 205 | |
| 206 | ret = 0; |
| 207 | unlock: |
| 208 | spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
| 209 | |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | /** |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 214 | * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator |
| 215 | * @bat_priv: the bat priv with all the soft interface information |
| 216 | * @addr: mac address of the originator |
| 217 | * |
| 218 | * Returns the originator object corresponding to the passed mac address or NULL |
| 219 | * on failure. |
| 220 | * If the object does not exists it is created an initialised. |
| 221 | */ |
| 222 | static struct batadv_orig_node * |
| 223 | batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const uint8_t *addr) |
| 224 | { |
| 225 | struct batadv_orig_node *orig_node; |
| 226 | int size, hash_added; |
| 227 | |
| 228 | orig_node = batadv_orig_hash_find(bat_priv, addr); |
| 229 | if (orig_node) |
| 230 | return orig_node; |
| 231 | |
| 232 | orig_node = batadv_orig_node_new(bat_priv, addr); |
| 233 | if (!orig_node) |
| 234 | return NULL; |
| 235 | |
| 236 | spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock); |
| 237 | |
| 238 | size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS; |
| 239 | orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC); |
| 240 | if (!orig_node->bat_iv.bcast_own) |
| 241 | goto free_orig_node; |
| 242 | |
| 243 | size = bat_priv->num_ifaces * sizeof(uint8_t); |
| 244 | orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC); |
| 245 | if (!orig_node->bat_iv.bcast_own_sum) |
| 246 | goto free_bcast_own; |
| 247 | |
| 248 | hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig, |
| 249 | batadv_choose_orig, orig_node, |
| 250 | &orig_node->hash_entry); |
| 251 | if (hash_added != 0) |
| 252 | goto free_bcast_own; |
| 253 | |
| 254 | return orig_node; |
| 255 | |
| 256 | free_bcast_own: |
| 257 | kfree(orig_node->bat_iv.bcast_own); |
| 258 | free_orig_node: |
| 259 | batadv_orig_node_free_ref(orig_node); |
| 260 | |
| 261 | return NULL; |
| 262 | } |
| 263 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 264 | static struct batadv_neigh_node * |
| 265 | batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface, |
| 266 | const uint8_t *neigh_addr, |
| 267 | struct batadv_orig_node *orig_node, |
Antonio Quartulli | 863dd7a | 2013-03-25 13:49:46 +0100 | [diff] [blame] | 268 | struct batadv_orig_node *orig_neigh) |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 269 | { |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 270 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 271 | struct batadv_neigh_node *neigh_node; |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 272 | |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 273 | neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr, orig_node); |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 274 | if (!neigh_node) |
| 275 | goto out; |
| 276 | |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 277 | spin_lock_init(&neigh_node->bat_iv.lq_update_lock); |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 278 | |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 279 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 280 | "Creating new neighbor %pM for orig_node %pM on interface %s\n", |
| 281 | neigh_addr, orig_node->orig, hard_iface->net_dev->name); |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 282 | |
| 283 | spin_lock_bh(&orig_node->neigh_list_lock); |
| 284 | hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list); |
| 285 | spin_unlock_bh(&orig_node->neigh_list_lock); |
| 286 | |
| 287 | out: |
| 288 | return neigh_node; |
| 289 | } |
| 290 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 291 | static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface) |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 292 | { |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 293 | struct batadv_ogm_packet *batadv_ogm_packet; |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 294 | unsigned char *ogm_buff; |
Marek Lindner | d7d32ec | 2012-02-07 17:20:46 +0800 | [diff] [blame] | 295 | uint32_t random_seqno; |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 296 | int res = -ENOMEM; |
Marek Lindner | d7d32ec | 2012-02-07 17:20:46 +0800 | [diff] [blame] | 297 | |
| 298 | /* randomize initial seqno to avoid collision */ |
| 299 | get_random_bytes(&random_seqno, sizeof(random_seqno)); |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 300 | atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno); |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 301 | |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 302 | hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN; |
| 303 | ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC); |
| 304 | if (!ogm_buff) |
Marek Lindner | 77af757 | 2012-02-07 17:20:48 +0800 | [diff] [blame] | 305 | goto out; |
| 306 | |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 307 | hard_iface->bat_iv.ogm_buff = ogm_buff; |
| 308 | |
| 309 | batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 310 | batadv_ogm_packet->header.packet_type = BATADV_IV_OGM; |
| 311 | batadv_ogm_packet->header.version = BATADV_COMPAT_VERSION; |
| 312 | batadv_ogm_packet->header.ttl = 2; |
| 313 | batadv_ogm_packet->flags = BATADV_NO_FLAGS; |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 314 | batadv_ogm_packet->reserved = 0; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 315 | batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE; |
Marek Lindner | 77af757 | 2012-02-07 17:20:48 +0800 | [diff] [blame] | 316 | |
| 317 | res = 0; |
| 318 | |
| 319 | out: |
| 320 | return res; |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 323 | static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface) |
Marek Lindner | 00a5007 | 2012-02-07 17:20:47 +0800 | [diff] [blame] | 324 | { |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 325 | kfree(hard_iface->bat_iv.ogm_buff); |
| 326 | hard_iface->bat_iv.ogm_buff = NULL; |
Marek Lindner | 00a5007 | 2012-02-07 17:20:47 +0800 | [diff] [blame] | 327 | } |
| 328 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 329 | static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface) |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 330 | { |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 331 | struct batadv_ogm_packet *batadv_ogm_packet; |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 332 | unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff; |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 333 | |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 334 | batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 335 | memcpy(batadv_ogm_packet->orig, |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 336 | hard_iface->net_dev->dev_addr, ETH_ALEN); |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 337 | memcpy(batadv_ogm_packet->prev_sender, |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 338 | hard_iface->net_dev->dev_addr, ETH_ALEN); |
| 339 | } |
| 340 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 341 | static void |
| 342 | batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface) |
Marek Lindner | c322939 | 2012-03-11 06:17:50 +0800 | [diff] [blame] | 343 | { |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 344 | struct batadv_ogm_packet *batadv_ogm_packet; |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 345 | unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff; |
Marek Lindner | c322939 | 2012-03-11 06:17:50 +0800 | [diff] [blame] | 346 | |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 347 | batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 348 | batadv_ogm_packet->flags = BATADV_PRIMARIES_FIRST_HOP; |
| 349 | batadv_ogm_packet->header.ttl = BATADV_TTL; |
Marek Lindner | c322939 | 2012-03-11 06:17:50 +0800 | [diff] [blame] | 350 | } |
| 351 | |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 352 | /* when do we schedule our own ogm to be sent */ |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 353 | static unsigned long |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 354 | batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 355 | { |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 356 | unsigned int msecs; |
| 357 | |
| 358 | msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER; |
Akinobu Mita | e76e432 | 2012-12-24 11:14:07 +0900 | [diff] [blame] | 359 | msecs += prandom_u32() % (2 * BATADV_JITTER); |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 360 | |
| 361 | return jiffies + msecs_to_jiffies(msecs); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /* when do we schedule a ogm packet to be sent */ |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 365 | static unsigned long batadv_iv_ogm_fwd_send_time(void) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 366 | { |
Akinobu Mita | e76e432 | 2012-12-24 11:14:07 +0900 | [diff] [blame] | 367 | return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2)); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /* apply hop penalty for a normal link */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 371 | static uint8_t batadv_hop_penalty(uint8_t tq, |
| 372 | const struct batadv_priv *bat_priv) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 373 | { |
| 374 | int hop_penalty = atomic_read(&bat_priv->hop_penalty); |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 375 | int new_tq; |
| 376 | |
| 377 | new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty); |
| 378 | new_tq /= BATADV_TQ_MAX_VALUE; |
| 379 | |
| 380 | return new_tq; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 381 | } |
| 382 | |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 383 | /* is there another aggregated packet here? */ |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 384 | static int batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len, |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 385 | __be16 tvlv_len) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 386 | { |
Sven Eckelmann | 08c36d3 | 2012-05-12 02:09:39 +0200 | [diff] [blame] | 387 | int next_buff_pos = 0; |
| 388 | |
Sven Eckelmann | 7e071c7 | 2012-06-03 22:19:13 +0200 | [diff] [blame] | 389 | next_buff_pos += buff_pos + BATADV_OGM_HLEN; |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 390 | next_buff_pos += ntohs(tvlv_len); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 391 | |
| 392 | return (next_buff_pos <= packet_len) && |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 393 | (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 394 | } |
| 395 | |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 396 | /* send a batman ogm to a given interface */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 397 | static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet, |
| 398 | struct batadv_hard_iface *hard_iface) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 399 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 400 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 401 | char *fwd_str; |
| 402 | uint8_t packet_num; |
| 403 | int16_t buff_pos; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 404 | struct batadv_ogm_packet *batadv_ogm_packet; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 405 | struct sk_buff *skb; |
Sven Eckelmann | c67893d | 2012-07-08 18:33:51 +0200 | [diff] [blame] | 406 | uint8_t *packet_pos; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 407 | |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 408 | if (hard_iface->if_status != BATADV_IF_ACTIVE) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 409 | return; |
| 410 | |
| 411 | packet_num = 0; |
| 412 | buff_pos = 0; |
Sven Eckelmann | c67893d | 2012-07-08 18:33:51 +0200 | [diff] [blame] | 413 | packet_pos = forw_packet->skb->data; |
| 414 | batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 415 | |
| 416 | /* adjust all flags and log packets */ |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 417 | while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len, |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 418 | batadv_ogm_packet->tvlv_len)) { |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 419 | /* we might have aggregated direct link packets with an |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 420 | * ordinary base packet |
| 421 | */ |
Sven Eckelmann | 8de47de | 2012-07-08 16:32:09 +0200 | [diff] [blame] | 422 | if (forw_packet->direct_link_flags & BIT(packet_num) && |
| 423 | forw_packet->if_incoming == hard_iface) |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 424 | batadv_ogm_packet->flags |= BATADV_DIRECTLINK; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 425 | else |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 426 | batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 427 | |
Sven Eckelmann | c67893d | 2012-07-08 18:33:51 +0200 | [diff] [blame] | 428 | if (packet_num > 0 || !forw_packet->own) |
| 429 | fwd_str = "Forwarding"; |
| 430 | else |
| 431 | fwd_str = "Sending own"; |
| 432 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 433 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Marek Lindner | e1bf0c1 | 2013-04-23 21:40:01 +0800 | [diff] [blame] | 434 | "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n", |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 435 | fwd_str, (packet_num > 0 ? "aggregated " : ""), |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 436 | batadv_ogm_packet->orig, |
| 437 | ntohl(batadv_ogm_packet->seqno), |
| 438 | batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl, |
| 439 | (batadv_ogm_packet->flags & BATADV_DIRECTLINK ? |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 440 | "on" : "off"), |
Marek Lindner | e1bf0c1 | 2013-04-23 21:40:01 +0800 | [diff] [blame] | 441 | hard_iface->net_dev->name, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 442 | hard_iface->net_dev->dev_addr); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 443 | |
Sven Eckelmann | 7e071c7 | 2012-06-03 22:19:13 +0200 | [diff] [blame] | 444 | buff_pos += BATADV_OGM_HLEN; |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 445 | buff_pos += ntohs(batadv_ogm_packet->tvlv_len); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 446 | packet_num++; |
Sven Eckelmann | c67893d | 2012-07-08 18:33:51 +0200 | [diff] [blame] | 447 | packet_pos = forw_packet->skb->data + buff_pos; |
| 448 | batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /* create clone because function is called more than once */ |
| 452 | skb = skb_clone(forw_packet->skb, GFP_ATOMIC); |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 453 | if (skb) { |
Sven Eckelmann | d69909d | 2012-06-03 22:19:20 +0200 | [diff] [blame] | 454 | batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX); |
| 455 | batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES, |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 456 | skb->len + ETH_HLEN); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 457 | batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr); |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 458 | } |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* send a batman ogm packet */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 462 | static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 463 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 464 | struct batadv_hard_iface *hard_iface; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 465 | struct net_device *soft_iface; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 466 | struct batadv_priv *bat_priv; |
| 467 | struct batadv_hard_iface *primary_if = NULL; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 468 | struct batadv_ogm_packet *batadv_ogm_packet; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 469 | unsigned char directlink; |
Sven Eckelmann | c67893d | 2012-07-08 18:33:51 +0200 | [diff] [blame] | 470 | uint8_t *packet_pos; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 471 | |
Sven Eckelmann | c67893d | 2012-07-08 18:33:51 +0200 | [diff] [blame] | 472 | packet_pos = forw_packet->skb->data; |
| 473 | batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 474 | directlink = (batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 475 | |
| 476 | if (!forw_packet->if_incoming) { |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 477 | pr_err("Error - can't forward packet: incoming iface not specified\n"); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 478 | goto out; |
| 479 | } |
| 480 | |
| 481 | soft_iface = forw_packet->if_incoming->soft_iface; |
| 482 | bat_priv = netdev_priv(soft_iface); |
| 483 | |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 484 | if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 485 | goto out; |
| 486 | |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 487 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 488 | if (!primary_if) |
| 489 | goto out; |
| 490 | |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 491 | /* multihomed peer assumed |
| 492 | * non-primary OGMs are only broadcasted on their interface |
| 493 | */ |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 494 | if ((directlink && (batadv_ogm_packet->header.ttl == 1)) || |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 495 | (forw_packet->own && (forw_packet->if_incoming != primary_if))) { |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 496 | /* FIXME: what about aggregated packets ? */ |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 497 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 498 | "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n", |
| 499 | (forw_packet->own ? "Sending own" : "Forwarding"), |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 500 | batadv_ogm_packet->orig, |
| 501 | ntohl(batadv_ogm_packet->seqno), |
| 502 | batadv_ogm_packet->header.ttl, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 503 | forw_packet->if_incoming->net_dev->name, |
| 504 | forw_packet->if_incoming->net_dev->dev_addr); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 505 | |
| 506 | /* skb is only used once and than forw_packet is free'd */ |
Sven Eckelmann | 9455e34 | 2012-05-12 02:09:37 +0200 | [diff] [blame] | 507 | batadv_send_skb_packet(forw_packet->skb, |
| 508 | forw_packet->if_incoming, |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 509 | batadv_broadcast_addr); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 510 | forw_packet->skb = NULL; |
| 511 | |
| 512 | goto out; |
| 513 | } |
| 514 | |
| 515 | /* broadcast on every interface */ |
| 516 | rcu_read_lock(); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 517 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 518 | if (hard_iface->soft_iface != soft_iface) |
| 519 | continue; |
| 520 | |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 521 | batadv_iv_ogm_send_to_if(forw_packet, hard_iface); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 522 | } |
| 523 | rcu_read_unlock(); |
| 524 | |
| 525 | out: |
| 526 | if (primary_if) |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 527 | batadv_hardif_free_ref(primary_if); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /* return true if new_packet can be aggregated with forw_packet */ |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 531 | static bool |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 532 | batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 533 | struct batadv_priv *bat_priv, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 534 | int packet_len, unsigned long send_time, |
| 535 | bool directlink, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 536 | const struct batadv_hard_iface *if_incoming, |
| 537 | const struct batadv_forw_packet *forw_packet) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 538 | { |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 539 | struct batadv_ogm_packet *batadv_ogm_packet; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 540 | int aggregated_bytes = forw_packet->packet_len + packet_len; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 541 | struct batadv_hard_iface *primary_if = NULL; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 542 | bool res = false; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 543 | unsigned long aggregation_end_time; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 544 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 545 | batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 546 | aggregation_end_time = send_time; |
| 547 | aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 548 | |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 549 | /* we can aggregate the current packet to this aggregated packet |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 550 | * if: |
| 551 | * |
| 552 | * - the send time is within our MAX_AGGREGATION_MS time |
| 553 | * - the resulting packet wont be bigger than |
| 554 | * MAX_AGGREGATION_BYTES |
| 555 | */ |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 556 | if (time_before(send_time, forw_packet->send_time) && |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 557 | time_after_eq(aggregation_end_time, forw_packet->send_time) && |
| 558 | (aggregated_bytes <= BATADV_MAX_AGGREGATION_BYTES)) { |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 559 | /* check aggregation compatibility |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 560 | * -> direct link packets are broadcasted on |
| 561 | * their interface only |
| 562 | * -> aggregate packet if the current packet is |
| 563 | * a "global" packet as well as the base |
| 564 | * packet |
| 565 | */ |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 566 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 567 | if (!primary_if) |
| 568 | goto out; |
| 569 | |
| 570 | /* packets without direct link flag and high TTL |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 571 | * are flooded through the net |
| 572 | */ |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 573 | if ((!directlink) && |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 574 | (!(batadv_ogm_packet->flags & BATADV_DIRECTLINK)) && |
| 575 | (batadv_ogm_packet->header.ttl != 1) && |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 576 | |
| 577 | /* own packets originating non-primary |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 578 | * interfaces leave only that interface |
| 579 | */ |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 580 | ((!forw_packet->own) || |
| 581 | (forw_packet->if_incoming == primary_if))) { |
| 582 | res = true; |
| 583 | goto out; |
| 584 | } |
| 585 | |
| 586 | /* if the incoming packet is sent via this one |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 587 | * interface only - we still can aggregate |
| 588 | */ |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 589 | if ((directlink) && |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 590 | (new_bat_ogm_packet->header.ttl == 1) && |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 591 | (forw_packet->if_incoming == if_incoming) && |
| 592 | |
| 593 | /* packets from direct neighbors or |
| 594 | * own secondary interface packets |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 595 | * (= secondary interface packets in general) |
| 596 | */ |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 597 | (batadv_ogm_packet->flags & BATADV_DIRECTLINK || |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 598 | (forw_packet->own && |
| 599 | forw_packet->if_incoming != primary_if))) { |
| 600 | res = true; |
| 601 | goto out; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | out: |
| 606 | if (primary_if) |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 607 | batadv_hardif_free_ref(primary_if); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 608 | return res; |
| 609 | } |
| 610 | |
| 611 | /* create a new aggregated packet and add this packet to it */ |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 612 | static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff, |
| 613 | int packet_len, unsigned long send_time, |
| 614 | bool direct_link, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 615 | struct batadv_hard_iface *if_incoming, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 616 | int own_packet) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 617 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 618 | struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
| 619 | struct batadv_forw_packet *forw_packet_aggr; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 620 | unsigned char *skb_buff; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 621 | unsigned int skb_size; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 622 | |
| 623 | if (!atomic_inc_not_zero(&if_incoming->refcount)) |
| 624 | return; |
| 625 | |
| 626 | /* own packet should always be scheduled */ |
| 627 | if (!own_packet) { |
Sven Eckelmann | 3e34819 | 2012-05-16 20:23:22 +0200 | [diff] [blame] | 628 | if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 629 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 630 | "batman packet queue full\n"); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 631 | goto out; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC); |
| 636 | if (!forw_packet_aggr) { |
| 637 | if (!own_packet) |
| 638 | atomic_inc(&bat_priv->batman_queue_left); |
| 639 | goto out; |
| 640 | } |
| 641 | |
| 642 | if ((atomic_read(&bat_priv->aggregated_ogms)) && |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 643 | (packet_len < BATADV_MAX_AGGREGATION_BYTES)) |
Sven Eckelmann | 5b24657 | 2012-11-04 17:11:45 +0100 | [diff] [blame] | 644 | skb_size = BATADV_MAX_AGGREGATION_BYTES; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 645 | else |
Sven Eckelmann | 5b24657 | 2012-11-04 17:11:45 +0100 | [diff] [blame] | 646 | skb_size = packet_len; |
| 647 | |
Antonio Quartulli | 41ab6c4 | 2013-04-02 22:28:44 +0200 | [diff] [blame] | 648 | skb_size += ETH_HLEN; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 649 | |
Antonio Quartulli | 41ab6c4 | 2013-04-02 22:28:44 +0200 | [diff] [blame] | 650 | forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 651 | if (!forw_packet_aggr->skb) { |
| 652 | if (!own_packet) |
| 653 | atomic_inc(&bat_priv->batman_queue_left); |
| 654 | kfree(forw_packet_aggr); |
| 655 | goto out; |
| 656 | } |
Simon Wunderlich | c54f38c | 2013-07-29 17:56:44 +0200 | [diff] [blame] | 657 | forw_packet_aggr->skb->priority = TC_PRIO_CONTROL; |
Antonio Quartulli | 41ab6c4 | 2013-04-02 22:28:44 +0200 | [diff] [blame] | 658 | skb_reserve(forw_packet_aggr->skb, ETH_HLEN); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 659 | |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 660 | skb_buff = skb_put(forw_packet_aggr->skb, packet_len); |
| 661 | forw_packet_aggr->packet_len = packet_len; |
| 662 | memcpy(skb_buff, packet_buff, packet_len); |
| 663 | |
| 664 | forw_packet_aggr->own = own_packet; |
| 665 | forw_packet_aggr->if_incoming = if_incoming; |
| 666 | forw_packet_aggr->num_packets = 0; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 667 | forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 668 | forw_packet_aggr->send_time = send_time; |
| 669 | |
| 670 | /* save packet direct link flag status */ |
| 671 | if (direct_link) |
| 672 | forw_packet_aggr->direct_link_flags |= 1; |
| 673 | |
| 674 | /* add new packet to packet list */ |
| 675 | spin_lock_bh(&bat_priv->forw_bat_list_lock); |
| 676 | hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list); |
| 677 | spin_unlock_bh(&bat_priv->forw_bat_list_lock); |
| 678 | |
| 679 | /* start timer for this packet */ |
| 680 | INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work, |
Sven Eckelmann | 9455e34 | 2012-05-12 02:09:37 +0200 | [diff] [blame] | 681 | batadv_send_outstanding_bat_ogm_packet); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 682 | queue_delayed_work(batadv_event_workqueue, |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 683 | &forw_packet_aggr->delayed_work, |
| 684 | send_time - jiffies); |
| 685 | |
| 686 | return; |
| 687 | out: |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 688 | batadv_hardif_free_ref(if_incoming); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /* aggregate a new packet into the existing ogm packet */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 692 | static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 693 | const unsigned char *packet_buff, |
| 694 | int packet_len, bool direct_link) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 695 | { |
| 696 | unsigned char *skb_buff; |
Sven Eckelmann | 8de47de | 2012-07-08 16:32:09 +0200 | [diff] [blame] | 697 | unsigned long new_direct_link_flag; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 698 | |
| 699 | skb_buff = skb_put(forw_packet_aggr->skb, packet_len); |
| 700 | memcpy(skb_buff, packet_buff, packet_len); |
| 701 | forw_packet_aggr->packet_len += packet_len; |
| 702 | forw_packet_aggr->num_packets++; |
| 703 | |
| 704 | /* save packet direct link flag status */ |
Sven Eckelmann | 8de47de | 2012-07-08 16:32:09 +0200 | [diff] [blame] | 705 | if (direct_link) { |
| 706 | new_direct_link_flag = BIT(forw_packet_aggr->num_packets); |
| 707 | forw_packet_aggr->direct_link_flags |= new_direct_link_flag; |
| 708 | } |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 709 | } |
| 710 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 711 | static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 712 | unsigned char *packet_buff, |
| 713 | int packet_len, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 714 | struct batadv_hard_iface *if_incoming, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 715 | int own_packet, unsigned long send_time) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 716 | { |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 717 | /* _aggr -> pointer to the packet we want to aggregate with |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 718 | * _pos -> pointer to the position in the queue |
| 719 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 720 | struct batadv_forw_packet *forw_packet_aggr = NULL; |
| 721 | struct batadv_forw_packet *forw_packet_pos = NULL; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 722 | struct batadv_ogm_packet *batadv_ogm_packet; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 723 | bool direct_link; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 724 | unsigned long max_aggregation_jiffies; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 725 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 726 | batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff; |
| 727 | direct_link = batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 728 | max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 729 | |
| 730 | /* find position for the packet in the forward queue */ |
| 731 | spin_lock_bh(&bat_priv->forw_bat_list_lock); |
| 732 | /* own packets are not to be aggregated */ |
| 733 | if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 734 | hlist_for_each_entry(forw_packet_pos, |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 735 | &bat_priv->forw_bat_list, list) { |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 736 | if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 737 | bat_priv, packet_len, |
| 738 | send_time, direct_link, |
| 739 | if_incoming, |
| 740 | forw_packet_pos)) { |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 741 | forw_packet_aggr = forw_packet_pos; |
| 742 | break; |
| 743 | } |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | /* nothing to aggregate with - either aggregation disabled or no |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 748 | * suitable aggregation packet found |
| 749 | */ |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 750 | if (!forw_packet_aggr) { |
| 751 | /* the following section can run without the lock */ |
| 752 | spin_unlock_bh(&bat_priv->forw_bat_list_lock); |
| 753 | |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 754 | /* if we could not aggregate this packet with one of the others |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 755 | * we hold it back for a while, so that it might be aggregated |
| 756 | * later on |
| 757 | */ |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 758 | if (!own_packet && atomic_read(&bat_priv->aggregated_ogms)) |
| 759 | send_time += max_aggregation_jiffies; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 760 | |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 761 | batadv_iv_ogm_aggregate_new(packet_buff, packet_len, |
| 762 | send_time, direct_link, |
| 763 | if_incoming, own_packet); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 764 | } else { |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 765 | batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff, |
| 766 | packet_len, direct_link); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 767 | spin_unlock_bh(&bat_priv->forw_bat_list_lock); |
| 768 | } |
| 769 | } |
| 770 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 771 | static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 772 | const struct ethhdr *ethhdr, |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 773 | struct batadv_ogm_packet *batadv_ogm_packet, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 774 | bool is_single_hop_neigh, |
| 775 | bool is_from_best_next_hop, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 776 | struct batadv_hard_iface *if_incoming) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 777 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 778 | struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 779 | uint16_t tvlv_len; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 780 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 781 | if (batadv_ogm_packet->header.ttl <= 1) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 782 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n"); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 783 | return; |
| 784 | } |
| 785 | |
Marek Lindner | 13b2541 | 2012-03-11 06:17:53 +0800 | [diff] [blame] | 786 | if (!is_from_best_next_hop) { |
| 787 | /* Mark the forwarded packet when it is not coming from our |
| 788 | * best next hop. We still need to forward the packet for our |
| 789 | * neighbor link quality detection to work in case the packet |
| 790 | * originated from a single hop neighbor. Otherwise we can |
| 791 | * simply drop the ogm. |
| 792 | */ |
| 793 | if (is_single_hop_neigh) |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 794 | batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP; |
Marek Lindner | 13b2541 | 2012-03-11 06:17:53 +0800 | [diff] [blame] | 795 | else |
| 796 | return; |
| 797 | } |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 798 | |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 799 | tvlv_len = ntohs(batadv_ogm_packet->tvlv_len); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 800 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 801 | batadv_ogm_packet->header.ttl--; |
| 802 | memcpy(batadv_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 803 | |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 804 | /* apply hop penalty */ |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 805 | batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 806 | bat_priv); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 807 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 808 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 809 | "Forwarding packet: tq: %i, ttl: %i\n", |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 810 | batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 811 | |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 812 | /* switch of primaries first hop flag when forwarding */ |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 813 | batadv_ogm_packet->flags &= ~BATADV_PRIMARIES_FIRST_HOP; |
Marek Lindner | 75cd33f | 2012-03-01 15:35:16 +0800 | [diff] [blame] | 814 | if (is_single_hop_neigh) |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 815 | batadv_ogm_packet->flags |= BATADV_DIRECTLINK; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 816 | else |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 817 | batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 818 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 819 | batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet, |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 820 | BATADV_OGM_HLEN + tvlv_len, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 821 | if_incoming, 0, batadv_iv_ogm_fwd_send_time()); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 822 | } |
| 823 | |
Antonio Quartulli | d989661 | 2013-04-17 17:44:43 +0200 | [diff] [blame] | 824 | /** |
| 825 | * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for |
| 826 | * the given interface |
| 827 | * @hard_iface: the interface for which the windows have to be shifted |
| 828 | */ |
| 829 | static void |
| 830 | batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface) |
| 831 | { |
| 832 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
| 833 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
| 834 | struct hlist_head *head; |
| 835 | struct batadv_orig_node *orig_node; |
| 836 | unsigned long *word; |
| 837 | uint32_t i; |
| 838 | size_t word_index; |
| 839 | uint8_t *w; |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 840 | int if_num; |
Antonio Quartulli | d989661 | 2013-04-17 17:44:43 +0200 | [diff] [blame] | 841 | |
| 842 | for (i = 0; i < hash->size; i++) { |
| 843 | head = &hash->table[i]; |
| 844 | |
| 845 | rcu_read_lock(); |
| 846 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 847 | spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
Antonio Quartulli | d989661 | 2013-04-17 17:44:43 +0200 | [diff] [blame] | 848 | word_index = hard_iface->if_num * BATADV_NUM_WORDS; |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 849 | word = &(orig_node->bat_iv.bcast_own[word_index]); |
Antonio Quartulli | d989661 | 2013-04-17 17:44:43 +0200 | [diff] [blame] | 850 | |
| 851 | batadv_bit_get_packet(bat_priv, word, 1, 0); |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 852 | if_num = hard_iface->if_num; |
| 853 | w = &orig_node->bat_iv.bcast_own_sum[if_num]; |
Antonio Quartulli | d989661 | 2013-04-17 17:44:43 +0200 | [diff] [blame] | 854 | *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE); |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 855 | spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
Antonio Quartulli | d989661 | 2013-04-17 17:44:43 +0200 | [diff] [blame] | 856 | } |
| 857 | rcu_read_unlock(); |
| 858 | } |
| 859 | } |
| 860 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 861 | static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 862 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 863 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 864 | unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 865 | struct batadv_ogm_packet *batadv_ogm_packet; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 866 | struct batadv_hard_iface *primary_if; |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 867 | int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len; |
Sven Eckelmann | bbb1f90 | 2012-07-08 17:13:15 +0200 | [diff] [blame] | 868 | uint32_t seqno; |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 869 | uint16_t tvlv_len = 0; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 870 | |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 871 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 872 | |
Marek Lindner | e1bf0c1 | 2013-04-23 21:40:01 +0800 | [diff] [blame] | 873 | if (hard_iface == primary_if) { |
| 874 | /* tt changes have to be committed before the tvlv data is |
| 875 | * appended as it may alter the tt tvlv container |
| 876 | */ |
| 877 | batadv_tt_local_commit_changes(bat_priv); |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 878 | tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff, |
| 879 | ogm_buff_len, |
| 880 | BATADV_OGM_HLEN); |
Marek Lindner | e1bf0c1 | 2013-04-23 21:40:01 +0800 | [diff] [blame] | 881 | } |
Marek Lindner | be9aa4c | 2012-05-07 04:22:05 +0800 | [diff] [blame] | 882 | |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 883 | batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff); |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 884 | batadv_ogm_packet->tvlv_len = htons(tvlv_len); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 885 | |
| 886 | /* change sequence number to network order */ |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 887 | seqno = (uint32_t)atomic_read(&hard_iface->bat_iv.ogm_seqno); |
Sven Eckelmann | bbb1f90 | 2012-07-08 17:13:15 +0200 | [diff] [blame] | 888 | batadv_ogm_packet->seqno = htonl(seqno); |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 889 | atomic_inc(&hard_iface->bat_iv.ogm_seqno); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 890 | |
Antonio Quartulli | d989661 | 2013-04-17 17:44:43 +0200 | [diff] [blame] | 891 | batadv_iv_ogm_slide_own_bcast_window(hard_iface); |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 892 | batadv_iv_ogm_queue_add(bat_priv, hard_iface->bat_iv.ogm_buff, |
| 893 | hard_iface->bat_iv.ogm_buff_len, hard_iface, 1, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 894 | batadv_iv_ogm_emit_send_time(bat_priv)); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 895 | |
| 896 | if (primary_if) |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 897 | batadv_hardif_free_ref(primary_if); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 898 | } |
| 899 | |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 900 | static void |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 901 | batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv, |
| 902 | struct batadv_orig_node *orig_node, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 903 | const struct ethhdr *ethhdr, |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 904 | const struct batadv_ogm_packet *batadv_ogm_packet, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 905 | struct batadv_hard_iface *if_incoming, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 906 | const unsigned char *tt_buff, |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 907 | enum batadv_dup_status dup_status) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 908 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 909 | struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL; |
| 910 | struct batadv_neigh_node *router = NULL; |
| 911 | struct batadv_orig_node *orig_node_tmp; |
Linus LĂĽssing | 7caf69f | 2012-09-18 03:01:08 +0200 | [diff] [blame] | 912 | int if_num; |
Sven Eckelmann | bbb1f90 | 2012-07-08 17:13:15 +0200 | [diff] [blame] | 913 | uint8_t sum_orig, sum_neigh; |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 914 | uint8_t *neigh_addr; |
Sven Eckelmann | bbb1f90 | 2012-07-08 17:13:15 +0200 | [diff] [blame] | 915 | uint8_t tq_avg; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 916 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 917 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 918 | "update_originator(): Searching and updating originator entry of received packet\n"); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 919 | |
| 920 | rcu_read_lock(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 921 | hlist_for_each_entry_rcu(tmp_neigh_node, |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 922 | &orig_node->neigh_list, list) { |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 923 | neigh_addr = tmp_neigh_node->addr; |
| 924 | if (batadv_compare_eth(neigh_addr, ethhdr->h_source) && |
| 925 | tmp_neigh_node->if_incoming == if_incoming && |
| 926 | atomic_inc_not_zero(&tmp_neigh_node->refcount)) { |
Antonio Quartulli | 38dc40e | 2013-03-30 17:22:00 +0100 | [diff] [blame] | 927 | if (WARN(neigh_node, "too many matching neigh_nodes")) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 928 | batadv_neigh_node_free_ref(neigh_node); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 929 | neigh_node = tmp_neigh_node; |
| 930 | continue; |
| 931 | } |
| 932 | |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 933 | if (dup_status != BATADV_NO_DUP) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 934 | continue; |
| 935 | |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 936 | spin_lock_bh(&tmp_neigh_node->bat_iv.lq_update_lock); |
| 937 | batadv_ring_buffer_set(tmp_neigh_node->bat_iv.tq_recv, |
| 938 | &tmp_neigh_node->bat_iv.tq_index, 0); |
| 939 | tq_avg = batadv_ring_buffer_avg(tmp_neigh_node->bat_iv.tq_recv); |
| 940 | tmp_neigh_node->bat_iv.tq_avg = tq_avg; |
| 941 | spin_unlock_bh(&tmp_neigh_node->bat_iv.lq_update_lock); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | if (!neigh_node) { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 945 | struct batadv_orig_node *orig_tmp; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 946 | |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 947 | orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 948 | if (!orig_tmp) |
| 949 | goto unlock; |
| 950 | |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 951 | neigh_node = batadv_iv_ogm_neigh_new(if_incoming, |
| 952 | ethhdr->h_source, |
Antonio Quartulli | 863dd7a | 2013-03-25 13:49:46 +0100 | [diff] [blame] | 953 | orig_node, orig_tmp); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 954 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 955 | batadv_orig_node_free_ref(orig_tmp); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 956 | if (!neigh_node) |
| 957 | goto unlock; |
| 958 | } else |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 959 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 960 | "Updating existing last-hop neighbor of originator\n"); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 961 | |
| 962 | rcu_read_unlock(); |
| 963 | |
Marek Lindner | d7b2a97 | 2012-03-01 15:35:19 +0800 | [diff] [blame] | 964 | neigh_node->last_seen = jiffies; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 965 | |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 966 | spin_lock_bh(&neigh_node->bat_iv.lq_update_lock); |
| 967 | batadv_ring_buffer_set(neigh_node->bat_iv.tq_recv, |
| 968 | &neigh_node->bat_iv.tq_index, |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 969 | batadv_ogm_packet->tq); |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 970 | tq_avg = batadv_ring_buffer_avg(neigh_node->bat_iv.tq_recv); |
| 971 | neigh_node->bat_iv.tq_avg = tq_avg; |
| 972 | spin_unlock_bh(&neigh_node->bat_iv.lq_update_lock); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 973 | |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 974 | if (dup_status == BATADV_NO_DUP) { |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 975 | orig_node->last_ttl = batadv_ogm_packet->header.ttl; |
| 976 | neigh_node->last_ttl = batadv_ogm_packet->header.ttl; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 977 | } |
| 978 | |
Antonio Quartulli | 6680a12 | 2013-09-02 12:15:06 +0200 | [diff] [blame] | 979 | batadv_bonding_candidate_add(bat_priv, orig_node, neigh_node); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 980 | |
| 981 | /* if this neighbor already is our next hop there is nothing |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 982 | * to change |
| 983 | */ |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 984 | router = batadv_orig_node_get_router(orig_node); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 985 | if (router == neigh_node) |
Marek Lindner | e1bf0c1 | 2013-04-23 21:40:01 +0800 | [diff] [blame] | 986 | goto out; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 987 | |
| 988 | /* if this neighbor does not offer a better TQ we won't consider it */ |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 989 | if (router && (router->bat_iv.tq_avg > neigh_node->bat_iv.tq_avg)) |
Marek Lindner | e1bf0c1 | 2013-04-23 21:40:01 +0800 | [diff] [blame] | 990 | goto out; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 991 | |
| 992 | /* if the TQ is the same and the link not more symmetric we |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 993 | * won't consider it either |
| 994 | */ |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 995 | if (router && (neigh_node->bat_iv.tq_avg == router->bat_iv.tq_avg)) { |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 996 | orig_node_tmp = router->orig_node; |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 997 | spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock); |
Linus LĂĽssing | 7caf69f | 2012-09-18 03:01:08 +0200 | [diff] [blame] | 998 | if_num = router->if_incoming->if_num; |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 999 | sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num]; |
| 1000 | spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1001 | |
| 1002 | orig_node_tmp = neigh_node->orig_node; |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1003 | spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock); |
Linus LĂĽssing | 7caf69f | 2012-09-18 03:01:08 +0200 | [diff] [blame] | 1004 | if_num = neigh_node->if_incoming->if_num; |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1005 | sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num]; |
| 1006 | spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1007 | |
Sven Eckelmann | bbb1f90 | 2012-07-08 17:13:15 +0200 | [diff] [blame] | 1008 | if (sum_orig >= sum_neigh) |
Marek Lindner | e1bf0c1 | 2013-04-23 21:40:01 +0800 | [diff] [blame] | 1009 | goto out; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1010 | } |
| 1011 | |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 1012 | batadv_update_route(bat_priv, orig_node, neigh_node); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1013 | goto out; |
| 1014 | |
| 1015 | unlock: |
| 1016 | rcu_read_unlock(); |
| 1017 | out: |
| 1018 | if (neigh_node) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 1019 | batadv_neigh_node_free_ref(neigh_node); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1020 | if (router) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 1021 | batadv_neigh_node_free_ref(router); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1022 | } |
| 1023 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1024 | static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node, |
| 1025 | struct batadv_orig_node *orig_neigh_node, |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1026 | struct batadv_ogm_packet *batadv_ogm_packet, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1027 | struct batadv_hard_iface *if_incoming) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1028 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1029 | struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
| 1030 | struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1031 | uint8_t total_count; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1032 | uint8_t orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own; |
| 1033 | unsigned int neigh_rq_inv_cube, neigh_rq_max_cube; |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1034 | int tq_asym_penalty, inv_asym_penalty, if_num, ret = 0; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1035 | unsigned int combined_tq; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1036 | |
| 1037 | /* find corresponding one hop neighbor */ |
| 1038 | rcu_read_lock(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1039 | hlist_for_each_entry_rcu(tmp_neigh_node, |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1040 | &orig_neigh_node->neigh_list, list) { |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1041 | if (!batadv_compare_eth(tmp_neigh_node->addr, |
| 1042 | orig_neigh_node->orig)) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1043 | continue; |
| 1044 | |
| 1045 | if (tmp_neigh_node->if_incoming != if_incoming) |
| 1046 | continue; |
| 1047 | |
| 1048 | if (!atomic_inc_not_zero(&tmp_neigh_node->refcount)) |
| 1049 | continue; |
| 1050 | |
| 1051 | neigh_node = tmp_neigh_node; |
| 1052 | break; |
| 1053 | } |
| 1054 | rcu_read_unlock(); |
| 1055 | |
| 1056 | if (!neigh_node) |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 1057 | neigh_node = batadv_iv_ogm_neigh_new(if_incoming, |
| 1058 | orig_neigh_node->orig, |
| 1059 | orig_neigh_node, |
Antonio Quartulli | 863dd7a | 2013-03-25 13:49:46 +0100 | [diff] [blame] | 1060 | orig_neigh_node); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1061 | |
| 1062 | if (!neigh_node) |
| 1063 | goto out; |
| 1064 | |
Marek Lindner | d7b2a97 | 2012-03-01 15:35:19 +0800 | [diff] [blame] | 1065 | /* if orig_node is direct neighbor update neigh_node last_seen */ |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1066 | if (orig_node == orig_neigh_node) |
Marek Lindner | d7b2a97 | 2012-03-01 15:35:19 +0800 | [diff] [blame] | 1067 | neigh_node->last_seen = jiffies; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1068 | |
Marek Lindner | d7b2a97 | 2012-03-01 15:35:19 +0800 | [diff] [blame] | 1069 | orig_node->last_seen = jiffies; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1070 | |
| 1071 | /* find packet count of corresponding one hop neighbor */ |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1072 | spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
| 1073 | if_num = if_incoming->if_num; |
| 1074 | orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num]; |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 1075 | neigh_rq_count = neigh_node->bat_iv.real_packet_count; |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1076 | spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1077 | |
| 1078 | /* pay attention to not get a value bigger than 100 % */ |
Sven Eckelmann | c67893d | 2012-07-08 18:33:51 +0200 | [diff] [blame] | 1079 | if (orig_eq_count > neigh_rq_count) |
| 1080 | total_count = neigh_rq_count; |
| 1081 | else |
| 1082 | total_count = orig_eq_count; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1083 | |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1084 | /* if we have too few packets (too less data) we set tq_own to zero |
| 1085 | * if we receive too few packets it is not considered bidirectional |
| 1086 | */ |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1087 | if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM || |
| 1088 | neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1089 | tq_own = 0; |
| 1090 | else |
| 1091 | /* neigh_node->real_packet_count is never zero as we |
| 1092 | * only purge old information when getting new |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1093 | * information |
| 1094 | */ |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1095 | tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1096 | |
Sven Eckelmann | 21a1236 | 2012-03-07 09:07:46 +0100 | [diff] [blame] | 1097 | /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1098 | * affect the nearly-symmetric links only a little, but |
| 1099 | * punishes asymmetric links more. This will give a value |
| 1100 | * between 0 and TQ_MAX_VALUE |
| 1101 | */ |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1102 | neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count; |
| 1103 | neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv; |
| 1104 | neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE * |
| 1105 | BATADV_TQ_LOCAL_WINDOW_SIZE * |
| 1106 | BATADV_TQ_LOCAL_WINDOW_SIZE; |
| 1107 | inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube; |
| 1108 | inv_asym_penalty /= neigh_rq_max_cube; |
| 1109 | tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1110 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1111 | combined_tq = batadv_ogm_packet->tq * tq_own * tq_asym_penalty; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1112 | combined_tq /= BATADV_TQ_MAX_VALUE * BATADV_TQ_MAX_VALUE; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1113 | batadv_ogm_packet->tq = combined_tq; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1114 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1115 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1116 | "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n", |
| 1117 | orig_node->orig, orig_neigh_node->orig, total_count, |
| 1118 | neigh_rq_count, tq_own, |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1119 | tq_asym_penalty, batadv_ogm_packet->tq); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1120 | |
| 1121 | /* if link has the minimum required transmission quality |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1122 | * consider it bidirectional |
| 1123 | */ |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1124 | if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1125 | ret = 1; |
| 1126 | |
| 1127 | out: |
| 1128 | if (neigh_node) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 1129 | batadv_neigh_node_free_ref(neigh_node); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1130 | return ret; |
| 1131 | } |
| 1132 | |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1133 | /** |
| 1134 | * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces, |
| 1135 | * adjust the sequence number and find out whether it is a duplicate |
| 1136 | * @ethhdr: ethernet header of the packet |
| 1137 | * @batadv_ogm_packet: OGM packet to be considered |
| 1138 | * @if_incoming: interface on which the OGM packet was received |
| 1139 | * |
| 1140 | * Returns duplicate status as enum batadv_dup_status |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1141 | */ |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1142 | static enum batadv_dup_status |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 1143 | batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr, |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1144 | const struct batadv_ogm_packet *batadv_ogm_packet, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1145 | const struct batadv_hard_iface *if_incoming) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1146 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1147 | struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
| 1148 | struct batadv_orig_node *orig_node; |
| 1149 | struct batadv_neigh_node *tmp_neigh_node; |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1150 | int is_dup; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1151 | int32_t seq_diff; |
| 1152 | int need_update = 0; |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1153 | int set_mark; |
| 1154 | enum batadv_dup_status ret = BATADV_NO_DUP; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1155 | uint32_t seqno = ntohl(batadv_ogm_packet->seqno); |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1156 | uint8_t *neigh_addr; |
Sven Eckelmann | bbb1f90 | 2012-07-08 17:13:15 +0200 | [diff] [blame] | 1157 | uint8_t packet_count; |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 1158 | unsigned long *bitmap; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1159 | |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1160 | orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1161 | if (!orig_node) |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1162 | return BATADV_NO_DUP; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1163 | |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1164 | spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
Al Viro | e0f5211 | 2012-04-22 07:46:29 +0100 | [diff] [blame] | 1165 | seq_diff = seqno - orig_node->last_real_seqno; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1166 | |
| 1167 | /* signalize caller that the packet is to be dropped. */ |
Antonio Quartulli | 1e5cc26 | 2012-02-26 15:39:42 +0100 | [diff] [blame] | 1168 | if (!hlist_empty(&orig_node->neigh_list) && |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 1169 | batadv_window_protected(bat_priv, seq_diff, |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1170 | &orig_node->batman_seqno_reset)) { |
| 1171 | ret = BATADV_PROTECTED; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1172 | goto out; |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1173 | } |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1174 | |
| 1175 | rcu_read_lock(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1176 | hlist_for_each_entry_rcu(tmp_neigh_node, |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1177 | &orig_node->neigh_list, list) { |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1178 | neigh_addr = tmp_neigh_node->addr; |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 1179 | is_dup = batadv_test_bit(tmp_neigh_node->bat_iv.real_bits, |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1180 | orig_node->last_real_seqno, |
| 1181 | seqno); |
| 1182 | |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1183 | if (batadv_compare_eth(neigh_addr, ethhdr->h_source) && |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1184 | tmp_neigh_node->if_incoming == if_incoming) { |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1185 | set_mark = 1; |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1186 | if (is_dup) |
| 1187 | ret = BATADV_NEIGH_DUP; |
| 1188 | } else { |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1189 | set_mark = 0; |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1190 | if (is_dup && (ret != BATADV_NEIGH_DUP)) |
| 1191 | ret = BATADV_ORIG_DUP; |
| 1192 | } |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1193 | |
| 1194 | /* if the window moved, set the update flag. */ |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 1195 | bitmap = tmp_neigh_node->bat_iv.real_bits; |
| 1196 | need_update |= batadv_bit_get_packet(bat_priv, bitmap, |
Sven Eckelmann | 0f5f932 | 2012-05-12 02:09:25 +0200 | [diff] [blame] | 1197 | seq_diff, set_mark); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1198 | |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 1199 | packet_count = bitmap_weight(tmp_neigh_node->bat_iv.real_bits, |
Sven Eckelmann | bbb1f90 | 2012-07-08 17:13:15 +0200 | [diff] [blame] | 1200 | BATADV_TQ_LOCAL_WINDOW_SIZE); |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 1201 | tmp_neigh_node->bat_iv.real_packet_count = packet_count; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1202 | } |
| 1203 | rcu_read_unlock(); |
| 1204 | |
| 1205 | if (need_update) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1206 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1207 | "updating last_seqno: old %u, new %u\n", |
| 1208 | orig_node->last_real_seqno, seqno); |
Al Viro | e0f5211 | 2012-04-22 07:46:29 +0100 | [diff] [blame] | 1209 | orig_node->last_real_seqno = seqno; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1210 | } |
| 1211 | |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1212 | out: |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1213 | spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 1214 | batadv_orig_node_free_ref(orig_node); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1215 | return ret; |
| 1216 | } |
| 1217 | |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 1218 | static void batadv_iv_ogm_process(const struct ethhdr *ethhdr, |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1219 | struct batadv_ogm_packet *batadv_ogm_packet, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 1220 | const unsigned char *tt_buff, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1221 | struct batadv_hard_iface *if_incoming) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1222 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1223 | struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
| 1224 | struct batadv_hard_iface *hard_iface; |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 1225 | struct batadv_orig_node *orig_neigh_node, *orig_node, *orig_node_tmp; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1226 | struct batadv_neigh_node *router = NULL, *router_router = NULL; |
| 1227 | struct batadv_neigh_node *orig_neigh_router = NULL; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1228 | int has_directlink_flag; |
| 1229 | int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0; |
Antonio Quartulli | 281581d | 2013-03-25 22:27:00 +0100 | [diff] [blame] | 1230 | int is_bidirect; |
Marek Lindner | 75cd33f | 2012-03-01 15:35:16 +0800 | [diff] [blame] | 1231 | bool is_single_hop_neigh = false; |
Marek Lindner | 13b2541 | 2012-03-11 06:17:53 +0800 | [diff] [blame] | 1232 | bool is_from_best_next_hop = false; |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1233 | int sameseq, similar_ttl; |
| 1234 | enum batadv_dup_status dup_status; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1235 | uint32_t if_incoming_seqno; |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1236 | uint8_t *prev_sender; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1237 | |
| 1238 | /* Silently drop when the batman packet is actually not a |
| 1239 | * correct packet. |
| 1240 | * |
| 1241 | * This might happen if a packet is padded (e.g. Ethernet has a |
| 1242 | * minimum frame length of 64 byte) and the aggregation interprets |
| 1243 | * it as an additional length. |
| 1244 | * |
| 1245 | * TODO: A more sane solution would be to have a bit in the |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1246 | * batadv_ogm_packet to detect whether the packet is the last |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1247 | * packet in an aggregation. Here we expect that the padding |
| 1248 | * is always zero (or not 0x01) |
| 1249 | */ |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1250 | if (batadv_ogm_packet->header.packet_type != BATADV_IV_OGM) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1251 | return; |
| 1252 | |
| 1253 | /* could be changed by schedule_own_packet() */ |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 1254 | if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1255 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1256 | if (batadv_ogm_packet->flags & BATADV_DIRECTLINK) |
Sven Eckelmann | acd34af | 2012-06-03 22:19:21 +0200 | [diff] [blame] | 1257 | has_directlink_flag = 1; |
| 1258 | else |
| 1259 | has_directlink_flag = 0; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1260 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1261 | if (batadv_compare_eth(ethhdr->h_source, batadv_ogm_packet->orig)) |
Marek Lindner | 75cd33f | 2012-03-01 15:35:16 +0800 | [diff] [blame] | 1262 | is_single_hop_neigh = true; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1263 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1264 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Marek Lindner | e1bf0c1 | 2013-04-23 21:40:01 +0800 | [diff] [blame] | 1265 | "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, tq %d, TTL %d, V %d, IDF %d)\n", |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1266 | ethhdr->h_source, if_incoming->net_dev->name, |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1267 | if_incoming->net_dev->dev_addr, batadv_ogm_packet->orig, |
| 1268 | batadv_ogm_packet->prev_sender, |
Marek Lindner | e1bf0c1 | 2013-04-23 21:40:01 +0800 | [diff] [blame] | 1269 | ntohl(batadv_ogm_packet->seqno), batadv_ogm_packet->tq, |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1270 | batadv_ogm_packet->header.ttl, |
| 1271 | batadv_ogm_packet->header.version, has_directlink_flag); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1272 | |
| 1273 | rcu_read_lock(); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 1274 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 1275 | if (hard_iface->if_status != BATADV_IF_ACTIVE) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1276 | continue; |
| 1277 | |
| 1278 | if (hard_iface->soft_iface != if_incoming->soft_iface) |
| 1279 | continue; |
| 1280 | |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1281 | if (batadv_compare_eth(ethhdr->h_source, |
| 1282 | hard_iface->net_dev->dev_addr)) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1283 | is_my_addr = 1; |
| 1284 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1285 | if (batadv_compare_eth(batadv_ogm_packet->orig, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1286 | hard_iface->net_dev->dev_addr)) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1287 | is_my_orig = 1; |
| 1288 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1289 | if (batadv_compare_eth(batadv_ogm_packet->prev_sender, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1290 | hard_iface->net_dev->dev_addr)) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1291 | is_my_oldorig = 1; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1292 | } |
| 1293 | rcu_read_unlock(); |
| 1294 | |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1295 | if (is_my_addr) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1296 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1297 | "Drop packet: received my own broadcast (sender: %pM)\n", |
| 1298 | ethhdr->h_source); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1299 | return; |
| 1300 | } |
| 1301 | |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1302 | if (is_my_orig) { |
| 1303 | unsigned long *word; |
| 1304 | int offset; |
Sven Eckelmann | 9b4a115 | 2012-05-12 13:48:53 +0200 | [diff] [blame] | 1305 | int32_t bit_pos; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1306 | int16_t if_num; |
| 1307 | uint8_t *weight; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1308 | |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1309 | orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv, |
| 1310 | ethhdr->h_source); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1311 | if (!orig_neigh_node) |
| 1312 | return; |
| 1313 | |
| 1314 | /* neighbor has to indicate direct link and it has to |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1315 | * come via the corresponding interface |
| 1316 | * save packet seqno for bidirectional check |
| 1317 | */ |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1318 | if (has_directlink_flag && |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1319 | batadv_compare_eth(if_incoming->net_dev->dev_addr, |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1320 | batadv_ogm_packet->orig)) { |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1321 | if_num = if_incoming->if_num; |
| 1322 | offset = if_num * BATADV_NUM_WORDS; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1323 | |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1324 | spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock); |
| 1325 | word = &(orig_neigh_node->bat_iv.bcast_own[offset]); |
Sven Eckelmann | 9b4a115 | 2012-05-12 13:48:53 +0200 | [diff] [blame] | 1326 | bit_pos = if_incoming_seqno - 2; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1327 | bit_pos -= ntohl(batadv_ogm_packet->seqno); |
Sven Eckelmann | 9b4a115 | 2012-05-12 13:48:53 +0200 | [diff] [blame] | 1328 | batadv_set_bit(word, bit_pos); |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1329 | weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num]; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1330 | *weight = bitmap_weight(word, |
| 1331 | BATADV_TQ_LOCAL_WINDOW_SIZE); |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1332 | spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1333 | } |
| 1334 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1335 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1336 | "Drop packet: originator packet from myself (via neighbor)\n"); |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 1337 | batadv_orig_node_free_ref(orig_neigh_node); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1338 | return; |
| 1339 | } |
| 1340 | |
| 1341 | if (is_my_oldorig) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1342 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1343 | "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n", |
| 1344 | ethhdr->h_source); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1345 | return; |
| 1346 | } |
| 1347 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1348 | if (batadv_ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1349 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1350 | "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n", |
| 1351 | ethhdr->h_source); |
Marek Lindner | 13b2541 | 2012-03-11 06:17:53 +0800 | [diff] [blame] | 1352 | return; |
| 1353 | } |
| 1354 | |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1355 | orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1356 | if (!orig_node) |
| 1357 | return; |
| 1358 | |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1359 | dup_status = batadv_iv_ogm_update_seqnos(ethhdr, batadv_ogm_packet, |
| 1360 | if_incoming); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1361 | |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1362 | if (dup_status == BATADV_PROTECTED) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1363 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1364 | "Drop packet: packet within seqno protection time (sender: %pM)\n", |
| 1365 | ethhdr->h_source); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1366 | goto out; |
| 1367 | } |
| 1368 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1369 | if (batadv_ogm_packet->tq == 0) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1370 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1371 | "Drop packet: originator packet with tq equal 0\n"); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1372 | goto out; |
| 1373 | } |
| 1374 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 1375 | router = batadv_orig_node_get_router(orig_node); |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 1376 | if (router) { |
| 1377 | orig_node_tmp = router->orig_node; |
| 1378 | router_router = batadv_orig_node_get_router(orig_node_tmp); |
| 1379 | } |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1380 | |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 1381 | if ((router && router->bat_iv.tq_avg != 0) && |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1382 | (batadv_compare_eth(router->addr, ethhdr->h_source))) |
Marek Lindner | 13b2541 | 2012-03-11 06:17:53 +0800 | [diff] [blame] | 1383 | is_from_best_next_hop = true; |
| 1384 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1385 | prev_sender = batadv_ogm_packet->prev_sender; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1386 | /* avoid temporary routing loops */ |
| 1387 | if (router && router_router && |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1388 | (batadv_compare_eth(router->addr, prev_sender)) && |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1389 | !(batadv_compare_eth(batadv_ogm_packet->orig, prev_sender)) && |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1390 | (batadv_compare_eth(router->addr, router_router->addr))) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1391 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1392 | "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n", |
| 1393 | ethhdr->h_source); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1394 | goto out; |
| 1395 | } |
| 1396 | |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 1397 | batadv_tvlv_ogm_receive(bat_priv, batadv_ogm_packet, orig_node); |
| 1398 | |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1399 | /* if sender is a direct neighbor the sender mac equals |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1400 | * originator mac |
| 1401 | */ |
Sven Eckelmann | c67893d | 2012-07-08 18:33:51 +0200 | [diff] [blame] | 1402 | if (is_single_hop_neigh) |
| 1403 | orig_neigh_node = orig_node; |
| 1404 | else |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1405 | orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv, |
| 1406 | ethhdr->h_source); |
Sven Eckelmann | c67893d | 2012-07-08 18:33:51 +0200 | [diff] [blame] | 1407 | |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1408 | if (!orig_neigh_node) |
| 1409 | goto out; |
| 1410 | |
Martin Hundebøll | d56b170 | 2013-01-25 11:12:39 +0100 | [diff] [blame] | 1411 | /* Update nc_nodes of the originator */ |
| 1412 | batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node, |
| 1413 | batadv_ogm_packet, is_single_hop_neigh); |
| 1414 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 1415 | orig_neigh_router = batadv_orig_node_get_router(orig_neigh_node); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1416 | |
| 1417 | /* drop packet if sender is not a direct neighbor and if we |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1418 | * don't route towards it |
| 1419 | */ |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1420 | if (!is_single_hop_neigh && (!orig_neigh_router)) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1421 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1422 | "Drop packet: OGM via unknown neighbor!\n"); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1423 | goto out_neigh; |
| 1424 | } |
| 1425 | |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 1426 | is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node, |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1427 | batadv_ogm_packet, if_incoming); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1428 | |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 1429 | batadv_bonding_save_primary(orig_node, orig_neigh_node, |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1430 | batadv_ogm_packet); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1431 | |
| 1432 | /* update ranking if it is not a duplicate or has the same |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1433 | * seqno and similar ttl as the non-duplicate |
| 1434 | */ |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1435 | sameseq = orig_node->last_real_seqno == ntohl(batadv_ogm_packet->seqno); |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1436 | similar_ttl = orig_node->last_ttl - 3 <= batadv_ogm_packet->header.ttl; |
| 1437 | if (is_bidirect && ((dup_status == BATADV_NO_DUP) || |
| 1438 | (sameseq && similar_ttl))) |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 1439 | batadv_iv_ogm_orig_update(bat_priv, orig_node, ethhdr, |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1440 | batadv_ogm_packet, if_incoming, |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1441 | tt_buff, dup_status); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1442 | |
| 1443 | /* is single hop (direct) neighbor */ |
| 1444 | if (is_single_hop_neigh) { |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1445 | /* mark direct link on incoming interface */ |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1446 | batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 1447 | is_single_hop_neigh, |
| 1448 | is_from_best_next_hop, if_incoming); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1449 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1450 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1451 | "Forwarding packet: rebroadcast neighbor packet with direct link flag\n"); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1452 | goto out_neigh; |
| 1453 | } |
| 1454 | |
| 1455 | /* multihop originator */ |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 1456 | if (!is_bidirect) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1457 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1458 | "Drop packet: not received via bidirectional link\n"); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1459 | goto out_neigh; |
| 1460 | } |
| 1461 | |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1462 | if (dup_status == BATADV_NEIGH_DUP) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1463 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1464 | "Drop packet: duplicate packet received\n"); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1465 | goto out_neigh; |
| 1466 | } |
| 1467 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1468 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1469 | "Forwarding packet: rebroadcast originator packet\n"); |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1470 | batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 1471 | is_single_hop_neigh, is_from_best_next_hop, |
| 1472 | if_incoming); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1473 | |
| 1474 | out_neigh: |
| 1475 | if ((orig_neigh_node) && (!is_single_hop_neigh)) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 1476 | batadv_orig_node_free_ref(orig_neigh_node); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1477 | out: |
| 1478 | if (router) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 1479 | batadv_neigh_node_free_ref(router); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1480 | if (router_router) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 1481 | batadv_neigh_node_free_ref(router_router); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1482 | if (orig_neigh_router) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 1483 | batadv_neigh_node_free_ref(orig_neigh_router); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1484 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 1485 | batadv_orig_node_free_ref(orig_node); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1486 | } |
| 1487 | |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 1488 | static int batadv_iv_ogm_receive(struct sk_buff *skb, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1489 | struct batadv_hard_iface *if_incoming) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1490 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1491 | struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1492 | struct batadv_ogm_packet *batadv_ogm_packet; |
Marek Lindner | 8780dad | 2011-12-05 04:01:51 +0800 | [diff] [blame] | 1493 | struct ethhdr *ethhdr; |
| 1494 | int buff_pos = 0, packet_len; |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 1495 | unsigned char *tvlv_buff, *packet_buff; |
Sven Eckelmann | c67893d | 2012-07-08 18:33:51 +0200 | [diff] [blame] | 1496 | uint8_t *packet_pos; |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 1497 | bool ret; |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 1498 | |
Sven Eckelmann | 7e071c7 | 2012-06-03 22:19:13 +0200 | [diff] [blame] | 1499 | ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN); |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 1500 | if (!ret) |
| 1501 | return NET_RX_DROP; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1502 | |
Marek Lindner | edbf12b | 2012-03-11 06:17:49 +0800 | [diff] [blame] | 1503 | /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface |
| 1504 | * that does not have B.A.T.M.A.N. IV enabled ? |
| 1505 | */ |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 1506 | if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit) |
Marek Lindner | edbf12b | 2012-03-11 06:17:49 +0800 | [diff] [blame] | 1507 | return NET_RX_DROP; |
| 1508 | |
Sven Eckelmann | d69909d | 2012-06-03 22:19:20 +0200 | [diff] [blame] | 1509 | batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX); |
| 1510 | batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES, |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 1511 | skb->len + ETH_HLEN); |
| 1512 | |
Marek Lindner | 8780dad | 2011-12-05 04:01:51 +0800 | [diff] [blame] | 1513 | packet_len = skb_headlen(skb); |
Antonio Quartulli | 7ed4be9 | 2013-04-08 15:08:18 +0200 | [diff] [blame] | 1514 | ethhdr = eth_hdr(skb); |
Marek Lindner | 8780dad | 2011-12-05 04:01:51 +0800 | [diff] [blame] | 1515 | packet_buff = skb->data; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1516 | batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1517 | |
| 1518 | /* unpack the aggregated packets and process them one by one */ |
Marek Lindner | b47506d | 2013-03-04 10:39:49 +0800 | [diff] [blame] | 1519 | while (batadv_iv_ogm_aggr_packet(buff_pos, packet_len, |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 1520 | batadv_ogm_packet->tvlv_len)) { |
| 1521 | tvlv_buff = packet_buff + buff_pos + BATADV_OGM_HLEN; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1522 | |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 1523 | batadv_iv_ogm_process(ethhdr, batadv_ogm_packet, |
| 1524 | tvlv_buff, if_incoming); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1525 | |
Sven Eckelmann | 7e071c7 | 2012-06-03 22:19:13 +0200 | [diff] [blame] | 1526 | buff_pos += BATADV_OGM_HLEN; |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 1527 | buff_pos += ntohs(batadv_ogm_packet->tvlv_len); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1528 | |
Sven Eckelmann | c67893d | 2012-07-08 18:33:51 +0200 | [diff] [blame] | 1529 | packet_pos = packet_buff + buff_pos; |
| 1530 | batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos; |
Marek Lindner | b47506d | 2013-03-04 10:39:49 +0800 | [diff] [blame] | 1531 | } |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 1532 | |
| 1533 | kfree_skb(skb); |
| 1534 | return NET_RX_SUCCESS; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1535 | } |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 1536 | |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1537 | /** |
| 1538 | * batadv_iv_ogm_orig_print - print the originator table |
| 1539 | * @bat_priv: the bat priv with all the soft interface information |
| 1540 | * @seq: debugfs table seq_file struct |
| 1541 | */ |
| 1542 | static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv, |
| 1543 | struct seq_file *seq) |
| 1544 | { |
| 1545 | struct batadv_neigh_node *neigh_node, *neigh_node_tmp; |
| 1546 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
| 1547 | int last_seen_msecs, last_seen_secs; |
| 1548 | struct batadv_orig_node *orig_node; |
| 1549 | unsigned long last_seen_jiffies; |
| 1550 | struct hlist_head *head; |
| 1551 | int batman_count = 0; |
| 1552 | uint32_t i; |
| 1553 | |
| 1554 | seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n", |
| 1555 | "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE, |
| 1556 | "Nexthop", "outgoingIF", "Potential nexthops"); |
| 1557 | |
| 1558 | for (i = 0; i < hash->size; i++) { |
| 1559 | head = &hash->table[i]; |
| 1560 | |
| 1561 | rcu_read_lock(); |
| 1562 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { |
| 1563 | neigh_node = batadv_orig_node_get_router(orig_node); |
| 1564 | if (!neigh_node) |
| 1565 | continue; |
| 1566 | |
| 1567 | if (neigh_node->bat_iv.tq_avg == 0) |
| 1568 | goto next; |
| 1569 | |
| 1570 | last_seen_jiffies = jiffies - orig_node->last_seen; |
| 1571 | last_seen_msecs = jiffies_to_msecs(last_seen_jiffies); |
| 1572 | last_seen_secs = last_seen_msecs / 1000; |
| 1573 | last_seen_msecs = last_seen_msecs % 1000; |
| 1574 | |
| 1575 | seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:", |
| 1576 | orig_node->orig, last_seen_secs, |
| 1577 | last_seen_msecs, neigh_node->bat_iv.tq_avg, |
| 1578 | neigh_node->addr, |
| 1579 | neigh_node->if_incoming->net_dev->name); |
| 1580 | |
| 1581 | hlist_for_each_entry_rcu(neigh_node_tmp, |
| 1582 | &orig_node->neigh_list, list) { |
| 1583 | seq_printf(seq, " %pM (%3i)", |
| 1584 | neigh_node_tmp->addr, |
| 1585 | neigh_node_tmp->bat_iv.tq_avg); |
| 1586 | } |
| 1587 | |
| 1588 | seq_puts(seq, "\n"); |
| 1589 | batman_count++; |
| 1590 | |
| 1591 | next: |
| 1592 | batadv_neigh_node_free_ref(neigh_node); |
| 1593 | } |
| 1594 | rcu_read_unlock(); |
| 1595 | } |
| 1596 | |
| 1597 | if (batman_count == 0) |
| 1598 | seq_puts(seq, "No batman nodes in range ...\n"); |
| 1599 | } |
| 1600 | |
Antonio Quartulli | a3285a8 | 2013-09-02 12:15:04 +0200 | [diff] [blame] | 1601 | /** |
| 1602 | * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors |
| 1603 | * @neigh1: the first neighbor object of the comparison |
| 1604 | * @neigh2: the second neighbor object of the comparison |
| 1605 | * |
| 1606 | * Returns a value less, equal to or greater than 0 if the metric via neigh1 is |
| 1607 | * lower, the same as or higher than the metric via neigh2 |
| 1608 | */ |
| 1609 | static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1, |
| 1610 | struct batadv_neigh_node *neigh2) |
| 1611 | { |
| 1612 | uint8_t tq1, tq2; |
| 1613 | |
| 1614 | tq1 = neigh1->bat_iv.tq_avg; |
| 1615 | tq2 = neigh2->bat_iv.tq_avg; |
| 1616 | |
| 1617 | return tq1 - tq2; |
| 1618 | } |
| 1619 | |
Antonio Quartulli | c43c981 | 2013-09-02 12:15:05 +0200 | [diff] [blame] | 1620 | /** |
| 1621 | * batadv_iv_ogm_neigh_is_eob - check if neigh1 is equally good or better than |
| 1622 | * neigh2 from the metric prospective |
| 1623 | * @neigh1: the first neighbor object of the comparison |
| 1624 | * @neigh2: the second neighbor object of the comparison |
| 1625 | * |
| 1626 | * Returns true if the metric via neigh1 is equally good or better than the |
| 1627 | * metric via neigh2, false otherwise. |
| 1628 | */ |
| 1629 | static bool batadv_iv_ogm_neigh_is_eob(struct batadv_neigh_node *neigh1, |
| 1630 | struct batadv_neigh_node *neigh2) |
| 1631 | { |
| 1632 | int diff = batadv_iv_ogm_neigh_cmp(neigh1, neigh2); |
| 1633 | |
| 1634 | return diff > -BATADV_TQ_SIMILARITY_THRESHOLD; |
| 1635 | } |
| 1636 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1637 | static struct batadv_algo_ops batadv_batman_iv __read_mostly = { |
Marek Lindner | 519d349 | 2012-04-18 17:15:57 +0800 | [diff] [blame] | 1638 | .name = "BATMAN_IV", |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 1639 | .bat_iface_enable = batadv_iv_ogm_iface_enable, |
| 1640 | .bat_iface_disable = batadv_iv_ogm_iface_disable, |
| 1641 | .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac, |
| 1642 | .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set, |
| 1643 | .bat_ogm_schedule = batadv_iv_ogm_schedule, |
| 1644 | .bat_ogm_emit = batadv_iv_ogm_emit, |
Antonio Quartulli | a3285a8 | 2013-09-02 12:15:04 +0200 | [diff] [blame] | 1645 | .bat_neigh_cmp = batadv_iv_ogm_neigh_cmp, |
Antonio Quartulli | c43c981 | 2013-09-02 12:15:05 +0200 | [diff] [blame] | 1646 | .bat_neigh_is_equiv_or_better = batadv_iv_ogm_neigh_is_eob, |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1647 | .bat_orig_print = batadv_iv_ogm_orig_print, |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 1648 | .bat_orig_free = batadv_iv_ogm_orig_free, |
| 1649 | .bat_orig_add_if = batadv_iv_ogm_orig_add_if, |
| 1650 | .bat_orig_del_if = batadv_iv_ogm_orig_del_if, |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 1651 | }; |
| 1652 | |
Sven Eckelmann | 81c524f | 2012-05-12 02:09:22 +0200 | [diff] [blame] | 1653 | int __init batadv_iv_init(void) |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 1654 | { |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 1655 | int ret; |
| 1656 | |
| 1657 | /* batman originator packet */ |
Sven Eckelmann | acd34af | 2012-06-03 22:19:21 +0200 | [diff] [blame] | 1658 | ret = batadv_recv_handler_register(BATADV_IV_OGM, |
| 1659 | batadv_iv_ogm_receive); |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 1660 | if (ret < 0) |
| 1661 | goto out; |
| 1662 | |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 1663 | ret = batadv_algo_register(&batadv_batman_iv); |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 1664 | if (ret < 0) |
| 1665 | goto handler_unregister; |
| 1666 | |
| 1667 | goto out; |
| 1668 | |
| 1669 | handler_unregister: |
Sven Eckelmann | acd34af | 2012-06-03 22:19:21 +0200 | [diff] [blame] | 1670 | batadv_recv_handler_unregister(BATADV_IV_OGM); |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 1671 | out: |
| 1672 | return ret; |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 1673 | } |