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