Sven Eckelmann | 0046b04 | 2016-01-01 00:01:03 +0100 | [diff] [blame] | 1 | /* Copyright (C) 2007-2016 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 | |
Sven Eckelmann | a2d0816 | 2016-05-15 11:07:46 +0200 | [diff] [blame] | 18 | #include "bat_iv_ogm.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 19 | #include "main.h" |
| 20 | |
| 21 | #include <linux/atomic.h> |
| 22 | #include <linux/bitmap.h> |
| 23 | #include <linux/bitops.h> |
| 24 | #include <linux/bug.h> |
| 25 | #include <linux/byteorder/generic.h> |
| 26 | #include <linux/cache.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/etherdevice.h> |
| 29 | #include <linux/fs.h> |
| 30 | #include <linux/if_ether.h> |
| 31 | #include <linux/init.h> |
| 32 | #include <linux/jiffies.h> |
Antonio Quartulli | f0d9725 | 2016-05-03 01:45:34 +0800 | [diff] [blame] | 33 | #include <linux/kernel.h> |
Sven Eckelmann | 77ae32e | 2016-01-16 10:29:53 +0100 | [diff] [blame] | 34 | #include <linux/kref.h> |
Sven Eckelmann | fcafa5e | 2016-05-15 11:07:42 +0200 | [diff] [blame] | 35 | #include <linux/list.h> |
Sven Eckelmann | 64ae7445 | 2016-02-22 22:56:34 +0100 | [diff] [blame] | 36 | #include <linux/lockdep.h> |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 37 | #include <linux/netdevice.h> |
| 38 | #include <linux/pkt_sched.h> |
| 39 | #include <linux/printk.h> |
| 40 | #include <linux/random.h> |
| 41 | #include <linux/rculist.h> |
| 42 | #include <linux/rcupdate.h> |
| 43 | #include <linux/seq_file.h> |
| 44 | #include <linux/skbuff.h> |
| 45 | #include <linux/slab.h> |
| 46 | #include <linux/spinlock.h> |
| 47 | #include <linux/stddef.h> |
| 48 | #include <linux/string.h> |
| 49 | #include <linux/types.h> |
| 50 | #include <linux/workqueue.h> |
| 51 | |
Sven Eckelmann | a2d0816 | 2016-05-15 11:07:46 +0200 | [diff] [blame] | 52 | #include "bat_algo.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 53 | #include "bitarray.h" |
Antonio Quartulli | 34d99cf | 2016-07-03 12:46:33 +0200 | [diff] [blame] | 54 | #include "gateway_client.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 55 | #include "hard-interface.h" |
| 56 | #include "hash.h" |
Sven Eckelmann | ba41208 | 2016-05-15 23:48:31 +0200 | [diff] [blame] | 57 | #include "log.h" |
Martin Hundebøll | d56b170 | 2013-01-25 11:12:39 +0100 | [diff] [blame] | 58 | #include "network-coding.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 59 | #include "originator.h" |
| 60 | #include "packet.h" |
| 61 | #include "routing.h" |
| 62 | #include "send.h" |
| 63 | #include "translation-table.h" |
Markus Pargmann | 1f8dce4 | 2016-05-15 11:07:43 +0200 | [diff] [blame] | 64 | #include "tvlv.h" |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 65 | |
Antonio Quartulli | f0d9725 | 2016-05-03 01:45:34 +0800 | [diff] [blame] | 66 | static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work); |
| 67 | |
Antonio Quartulli | 791c2a2 | 2013-08-17 12:44:44 +0200 | [diff] [blame] | 68 | /** |
Martin Hundebøll | 95298a9 | 2014-07-15 09:41:05 +0200 | [diff] [blame] | 69 | * enum batadv_dup_status - duplicate status |
Markus Pargmann | 9f52ee1 | 2014-12-26 12:41:34 +0100 | [diff] [blame] | 70 | * @BATADV_NO_DUP: the packet is no duplicate |
Antonio Quartulli | 791c2a2 | 2013-08-17 12:44:44 +0200 | [diff] [blame] | 71 | * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the |
| 72 | * neighbor) |
| 73 | * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor |
| 74 | * @BATADV_PROTECTED: originator is currently protected (after reboot) |
| 75 | */ |
| 76 | enum batadv_dup_status { |
| 77 | BATADV_NO_DUP = 0, |
| 78 | BATADV_ORIG_DUP, |
| 79 | BATADV_NEIGH_DUP, |
| 80 | BATADV_PROTECTED, |
| 81 | }; |
| 82 | |
Antonio Quartulli | 24a5dee | 2013-04-08 09:38:12 +0200 | [diff] [blame] | 83 | /** |
| 84 | * batadv_ring_buffer_set - update the ring buffer with the given value |
| 85 | * @lq_recv: pointer to the ring buffer |
| 86 | * @lq_index: index to store the value at |
| 87 | * @value: value to store in the ring buffer |
| 88 | */ |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 89 | static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value) |
Antonio Quartulli | 24a5dee | 2013-04-08 09:38:12 +0200 | [diff] [blame] | 90 | { |
| 91 | lq_recv[*lq_index] = value; |
| 92 | *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE; |
| 93 | } |
| 94 | |
| 95 | /** |
Markus Pargmann | d491dbb | 2014-12-26 12:41:36 +0100 | [diff] [blame] | 96 | * batadv_ring_buffer_avg - compute the average of all non-zero values stored |
Antonio Quartulli | 24a5dee | 2013-04-08 09:38:12 +0200 | [diff] [blame] | 97 | * in the given ring buffer |
| 98 | * @lq_recv: pointer to the ring buffer |
| 99 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 100 | * Return: computed average value. |
Antonio Quartulli | 24a5dee | 2013-04-08 09:38:12 +0200 | [diff] [blame] | 101 | */ |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 102 | static u8 batadv_ring_buffer_avg(const u8 lq_recv[]) |
Antonio Quartulli | 24a5dee | 2013-04-08 09:38:12 +0200 | [diff] [blame] | 103 | { |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 104 | const u8 *ptr; |
| 105 | u16 count = 0; |
| 106 | u16 i = 0; |
| 107 | u16 sum = 0; |
Antonio Quartulli | 24a5dee | 2013-04-08 09:38:12 +0200 | [diff] [blame] | 108 | |
| 109 | ptr = lq_recv; |
| 110 | |
| 111 | while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) { |
| 112 | if (*ptr != 0) { |
| 113 | count++; |
| 114 | sum += *ptr; |
| 115 | } |
| 116 | |
| 117 | i++; |
| 118 | ptr++; |
| 119 | } |
| 120 | |
| 121 | if (count == 0) |
| 122 | return 0; |
| 123 | |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 124 | return (u8)(sum / count); |
Antonio Quartulli | 24a5dee | 2013-04-08 09:38:12 +0200 | [diff] [blame] | 125 | } |
David S. Miller | d98cae64e | 2013-06-19 16:49:39 -0700 | [diff] [blame] | 126 | |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 127 | /** |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 128 | * batadv_iv_ogm_orig_free - free the private resources allocated for this |
| 129 | * orig_node |
| 130 | * @orig_node: the orig_node for which the resources have to be free'd |
| 131 | */ |
| 132 | static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node) |
| 133 | { |
| 134 | kfree(orig_node->bat_iv.bcast_own); |
| 135 | kfree(orig_node->bat_iv.bcast_own_sum); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * batadv_iv_ogm_orig_add_if - change the private structures of the orig_node to |
| 140 | * include the new hard-interface |
| 141 | * @orig_node: the orig_node that has to be changed |
| 142 | * @max_if_num: the current amount of interfaces |
| 143 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 144 | * Return: 0 on success, a negative error code otherwise. |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 145 | */ |
| 146 | static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node, |
| 147 | int max_if_num) |
| 148 | { |
| 149 | void *data_ptr; |
Antonio Quartulli | 0185dda | 2014-05-24 06:43:47 +0200 | [diff] [blame] | 150 | size_t old_size; |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 151 | int ret = -ENOMEM; |
| 152 | |
| 153 | spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
| 154 | |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 155 | old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS; |
Antonio Quartulli | 0185dda | 2014-05-24 06:43:47 +0200 | [diff] [blame] | 156 | data_ptr = kmalloc_array(max_if_num, |
| 157 | BATADV_NUM_WORDS * sizeof(unsigned long), |
| 158 | GFP_ATOMIC); |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 159 | if (!data_ptr) |
| 160 | goto unlock; |
| 161 | |
| 162 | memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size); |
| 163 | kfree(orig_node->bat_iv.bcast_own); |
| 164 | orig_node->bat_iv.bcast_own = data_ptr; |
| 165 | |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 166 | data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC); |
Sven Eckelmann | f7dcdf5 | 2016-02-22 22:56:33 +0100 | [diff] [blame] | 167 | if (!data_ptr) |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 168 | goto unlock; |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 169 | |
| 170 | memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum, |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 171 | (max_if_num - 1) * sizeof(u8)); |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 172 | kfree(orig_node->bat_iv.bcast_own_sum); |
| 173 | orig_node->bat_iv.bcast_own_sum = data_ptr; |
| 174 | |
| 175 | ret = 0; |
| 176 | |
| 177 | unlock: |
| 178 | spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
| 179 | |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | /** |
Sven Eckelmann | 64ae7445 | 2016-02-22 22:56:34 +0100 | [diff] [blame] | 184 | * batadv_iv_ogm_drop_bcast_own_entry - drop section of bcast_own |
| 185 | * @orig_node: the orig_node that has to be changed |
| 186 | * @max_if_num: the current amount of interfaces |
| 187 | * @del_if_num: the index of the interface being removed |
| 188 | */ |
| 189 | static void |
| 190 | batadv_iv_ogm_drop_bcast_own_entry(struct batadv_orig_node *orig_node, |
| 191 | int max_if_num, int del_if_num) |
| 192 | { |
| 193 | size_t chunk_size; |
| 194 | size_t if_offset; |
| 195 | void *data_ptr; |
| 196 | |
| 197 | lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock); |
| 198 | |
| 199 | chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS; |
| 200 | data_ptr = kmalloc_array(max_if_num, chunk_size, GFP_ATOMIC); |
| 201 | if (!data_ptr) |
| 202 | /* use old buffer when new one could not be allocated */ |
| 203 | data_ptr = orig_node->bat_iv.bcast_own; |
| 204 | |
| 205 | /* copy first part */ |
| 206 | memmove(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size); |
| 207 | |
| 208 | /* copy second part */ |
| 209 | if_offset = (del_if_num + 1) * chunk_size; |
| 210 | memmove((char *)data_ptr + del_if_num * chunk_size, |
| 211 | (uint8_t *)orig_node->bat_iv.bcast_own + if_offset, |
| 212 | (max_if_num - del_if_num) * chunk_size); |
| 213 | |
| 214 | /* bcast_own was shrunk down in new buffer; free old one */ |
| 215 | if (orig_node->bat_iv.bcast_own != data_ptr) { |
| 216 | kfree(orig_node->bat_iv.bcast_own); |
| 217 | orig_node->bat_iv.bcast_own = data_ptr; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * batadv_iv_ogm_drop_bcast_own_sum_entry - drop section of bcast_own_sum |
| 223 | * @orig_node: the orig_node that has to be changed |
| 224 | * @max_if_num: the current amount of interfaces |
| 225 | * @del_if_num: the index of the interface being removed |
| 226 | */ |
| 227 | static void |
| 228 | batadv_iv_ogm_drop_bcast_own_sum_entry(struct batadv_orig_node *orig_node, |
| 229 | int max_if_num, int del_if_num) |
| 230 | { |
| 231 | size_t if_offset; |
| 232 | void *data_ptr; |
| 233 | |
| 234 | lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock); |
| 235 | |
| 236 | data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC); |
| 237 | if (!data_ptr) |
| 238 | /* use old buffer when new one could not be allocated */ |
| 239 | data_ptr = orig_node->bat_iv.bcast_own_sum; |
| 240 | |
| 241 | memmove(data_ptr, orig_node->bat_iv.bcast_own_sum, |
| 242 | del_if_num * sizeof(u8)); |
| 243 | |
| 244 | if_offset = (del_if_num + 1) * sizeof(u8); |
| 245 | memmove((char *)data_ptr + del_if_num * sizeof(u8), |
| 246 | orig_node->bat_iv.bcast_own_sum + if_offset, |
| 247 | (max_if_num - del_if_num) * sizeof(u8)); |
| 248 | |
| 249 | /* bcast_own_sum was shrunk down in new buffer; free old one */ |
| 250 | if (orig_node->bat_iv.bcast_own_sum != data_ptr) { |
| 251 | kfree(orig_node->bat_iv.bcast_own_sum); |
| 252 | orig_node->bat_iv.bcast_own_sum = data_ptr; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /** |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 257 | * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to |
| 258 | * exclude the removed interface |
| 259 | * @orig_node: the orig_node that has to be changed |
| 260 | * @max_if_num: the current amount of interfaces |
| 261 | * @del_if_num: the index of the interface being removed |
| 262 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 263 | * Return: 0 on success, a negative error code otherwise. |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 264 | */ |
| 265 | static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node, |
| 266 | int max_if_num, int del_if_num) |
| 267 | { |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 268 | spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
| 269 | |
Sven Eckelmann | 64ae7445 | 2016-02-22 22:56:34 +0100 | [diff] [blame] | 270 | if (max_if_num == 0) { |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 271 | kfree(orig_node->bat_iv.bcast_own); |
Sven Eckelmann | 64ae7445 | 2016-02-22 22:56:34 +0100 | [diff] [blame] | 272 | kfree(orig_node->bat_iv.bcast_own_sum); |
| 273 | orig_node->bat_iv.bcast_own = NULL; |
| 274 | orig_node->bat_iv.bcast_own_sum = NULL; |
| 275 | } else { |
| 276 | batadv_iv_ogm_drop_bcast_own_entry(orig_node, max_if_num, |
| 277 | del_if_num); |
| 278 | batadv_iv_ogm_drop_bcast_own_sum_entry(orig_node, max_if_num, |
| 279 | del_if_num); |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 280 | } |
| 281 | |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 282 | spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
| 283 | |
Sven Eckelmann | 64ae7445 | 2016-02-22 22:56:34 +0100 | [diff] [blame] | 284 | return 0; |
Antonio Quartulli | d0015fd | 2013-09-03 11:10:23 +0200 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /** |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 288 | * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator |
| 289 | * @bat_priv: the bat priv with all the soft interface information |
| 290 | * @addr: mac address of the originator |
| 291 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 292 | * Return: the originator object corresponding to the passed mac address or NULL |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 293 | * on failure. |
| 294 | * If the object does not exists it is created an initialised. |
| 295 | */ |
| 296 | static struct batadv_orig_node * |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 297 | batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr) |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 298 | { |
| 299 | struct batadv_orig_node *orig_node; |
| 300 | int size, hash_added; |
| 301 | |
| 302 | orig_node = batadv_orig_hash_find(bat_priv, addr); |
| 303 | if (orig_node) |
| 304 | return orig_node; |
| 305 | |
| 306 | orig_node = batadv_orig_node_new(bat_priv, addr); |
| 307 | if (!orig_node) |
| 308 | return NULL; |
| 309 | |
| 310 | spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock); |
| 311 | |
| 312 | size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS; |
| 313 | orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC); |
| 314 | if (!orig_node->bat_iv.bcast_own) |
| 315 | goto free_orig_node; |
| 316 | |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 317 | size = bat_priv->num_ifaces * sizeof(u8); |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 318 | orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC); |
| 319 | if (!orig_node->bat_iv.bcast_own_sum) |
Antonio Quartulli | a5a5cb8 | 2014-02-15 02:17:20 +0100 | [diff] [blame] | 320 | goto free_orig_node; |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 321 | |
| 322 | hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig, |
| 323 | batadv_choose_orig, orig_node, |
| 324 | &orig_node->hash_entry); |
| 325 | if (hash_added != 0) |
Antonio Quartulli | a5a5cb8 | 2014-02-15 02:17:20 +0100 | [diff] [blame] | 326 | goto free_orig_node; |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 327 | |
| 328 | return orig_node; |
| 329 | |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 330 | free_orig_node: |
Simon Wunderlich | b2262df | 2014-02-08 16:45:06 +0100 | [diff] [blame] | 331 | /* free twice, as batadv_orig_node_new sets refcount to 2 */ |
Sven Eckelmann | 5d96731 | 2016-01-17 11:01:09 +0100 | [diff] [blame] | 332 | batadv_orig_node_put(orig_node); |
| 333 | batadv_orig_node_put(orig_node); |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 334 | |
| 335 | return NULL; |
| 336 | } |
| 337 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 338 | static struct batadv_neigh_node * |
| 339 | batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface, |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 340 | const u8 *neigh_addr, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 341 | struct batadv_orig_node *orig_node, |
Antonio Quartulli | 863dd7a | 2013-03-25 13:49:46 +0100 | [diff] [blame] | 342 | struct batadv_orig_node *orig_neigh) |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 343 | { |
Marek Lindner | 741aa06 | 2015-07-26 04:57:43 +0800 | [diff] [blame] | 344 | struct batadv_neigh_node *neigh_node; |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 345 | |
Marek Lindner | 6f0a6b5 | 2016-05-03 01:52:08 +0800 | [diff] [blame] | 346 | neigh_node = batadv_neigh_node_get_or_create(orig_node, |
| 347 | hard_iface, neigh_addr); |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 348 | if (!neigh_node) |
| 349 | goto out; |
| 350 | |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 351 | neigh_node->orig_node = orig_neigh; |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 352 | |
Marek Lindner | 7ae8b28 | 2012-03-01 15:35:21 +0800 | [diff] [blame] | 353 | out: |
| 354 | return neigh_node; |
| 355 | } |
| 356 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 357 | 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] | 358 | { |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 359 | struct batadv_ogm_packet *batadv_ogm_packet; |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 360 | unsigned char *ogm_buff; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 361 | u32 random_seqno; |
Marek Lindner | d7d32ec | 2012-02-07 17:20:46 +0800 | [diff] [blame] | 362 | |
| 363 | /* randomize initial seqno to avoid collision */ |
| 364 | get_random_bytes(&random_seqno, sizeof(random_seqno)); |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 365 | atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno); |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 366 | |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 367 | hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN; |
| 368 | ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC); |
| 369 | if (!ogm_buff) |
Markus Pargmann | 42d9f2c | 2014-12-26 12:41:26 +0100 | [diff] [blame] | 370 | return -ENOMEM; |
Marek Lindner | 77af757 | 2012-02-07 17:20:48 +0800 | [diff] [blame] | 371 | |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 372 | hard_iface->bat_iv.ogm_buff = ogm_buff; |
| 373 | |
| 374 | batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff; |
Simon Wunderlich | a40d9b0 | 2013-12-02 20:38:31 +0100 | [diff] [blame] | 375 | batadv_ogm_packet->packet_type = BATADV_IV_OGM; |
| 376 | batadv_ogm_packet->version = BATADV_COMPAT_VERSION; |
| 377 | batadv_ogm_packet->ttl = 2; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 378 | batadv_ogm_packet->flags = BATADV_NO_FLAGS; |
Marek Lindner | 414254e | 2013-04-23 21:39:58 +0800 | [diff] [blame] | 379 | batadv_ogm_packet->reserved = 0; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 380 | batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE; |
Marek Lindner | 77af757 | 2012-02-07 17:20:48 +0800 | [diff] [blame] | 381 | |
Markus Pargmann | 42d9f2c | 2014-12-26 12:41:26 +0100 | [diff] [blame] | 382 | return 0; |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 385 | 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] | 386 | { |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 387 | kfree(hard_iface->bat_iv.ogm_buff); |
| 388 | hard_iface->bat_iv.ogm_buff = NULL; |
Marek Lindner | 00a5007 | 2012-02-07 17:20:47 +0800 | [diff] [blame] | 389 | } |
| 390 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 391 | 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] | 392 | { |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 393 | struct batadv_ogm_packet *batadv_ogm_packet; |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 394 | unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff; |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 395 | |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 396 | batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff; |
Antonio Quartulli | 8fdd015 | 2014-01-22 00:42:11 +0100 | [diff] [blame] | 397 | ether_addr_copy(batadv_ogm_packet->orig, |
| 398 | hard_iface->net_dev->dev_addr); |
| 399 | ether_addr_copy(batadv_ogm_packet->prev_sender, |
| 400 | hard_iface->net_dev->dev_addr); |
Marek Lindner | d0b9fd8 | 2011-07-30 12:33:33 +0200 | [diff] [blame] | 401 | } |
| 402 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 403 | static void |
| 404 | batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface) |
Marek Lindner | c322939 | 2012-03-11 06:17:50 +0800 | [diff] [blame] | 405 | { |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 406 | struct batadv_ogm_packet *batadv_ogm_packet; |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 407 | unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff; |
Marek Lindner | c322939 | 2012-03-11 06:17:50 +0800 | [diff] [blame] | 408 | |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 409 | batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff; |
Simon Wunderlich | a40d9b0 | 2013-12-02 20:38:31 +0100 | [diff] [blame] | 410 | batadv_ogm_packet->ttl = BATADV_TTL; |
Marek Lindner | c322939 | 2012-03-11 06:17:50 +0800 | [diff] [blame] | 411 | } |
| 412 | |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 413 | /* when do we schedule our own ogm to be sent */ |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 414 | static unsigned long |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 415 | batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 416 | { |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 417 | unsigned int msecs; |
| 418 | |
| 419 | msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER; |
Akinobu Mita | e76e432 | 2012-12-24 11:14:07 +0900 | [diff] [blame] | 420 | msecs += prandom_u32() % (2 * BATADV_JITTER); |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 421 | |
| 422 | return jiffies + msecs_to_jiffies(msecs); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /* when do we schedule a ogm packet to be sent */ |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 426 | static unsigned long batadv_iv_ogm_fwd_send_time(void) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 427 | { |
Akinobu Mita | e76e432 | 2012-12-24 11:14:07 +0900 | [diff] [blame] | 428 | return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2)); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /* apply hop penalty for a normal link */ |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 432 | static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 433 | { |
| 434 | int hop_penalty = atomic_read(&bat_priv->hop_penalty); |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 435 | int new_tq; |
| 436 | |
| 437 | new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty); |
| 438 | new_tq /= BATADV_TQ_MAX_VALUE; |
| 439 | |
| 440 | return new_tq; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Simon Wunderlich | 600184b | 2015-11-23 19:57:21 +0100 | [diff] [blame] | 443 | /** |
| 444 | * batadv_iv_ogm_aggr_packet - checks if there is another OGM attached |
| 445 | * @buff_pos: current position in the skb |
| 446 | * @packet_len: total length of the skb |
| 447 | * @tvlv_len: tvlv length of the previously considered OGM |
| 448 | * |
| 449 | * Return: true if there is enough space for another OGM, false otherwise. |
| 450 | */ |
Markus Pargmann | 9fd9b19 | 2014-12-26 12:41:27 +0100 | [diff] [blame] | 451 | static bool batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len, |
| 452 | __be16 tvlv_len) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 453 | { |
Sven Eckelmann | 08c36d3 | 2012-05-12 02:09:39 +0200 | [diff] [blame] | 454 | int next_buff_pos = 0; |
| 455 | |
Sven Eckelmann | 7e071c7 | 2012-06-03 22:19:13 +0200 | [diff] [blame] | 456 | next_buff_pos += buff_pos + BATADV_OGM_HLEN; |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 457 | next_buff_pos += ntohs(tvlv_len); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 458 | |
| 459 | return (next_buff_pos <= packet_len) && |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 460 | (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 461 | } |
| 462 | |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 463 | /* send a batman ogm to a given interface */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 464 | static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet, |
| 465 | struct batadv_hard_iface *hard_iface) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 466 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 467 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
Markus Pargmann | de12bae | 2014-12-26 12:41:28 +0100 | [diff] [blame] | 468 | const char *fwd_str; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 469 | u8 packet_num; |
| 470 | s16 buff_pos; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 471 | struct batadv_ogm_packet *batadv_ogm_packet; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 472 | struct sk_buff *skb; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 473 | u8 *packet_pos; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 474 | |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 475 | if (hard_iface->if_status != BATADV_IF_ACTIVE) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 476 | return; |
| 477 | |
| 478 | packet_num = 0; |
| 479 | buff_pos = 0; |
Sven Eckelmann | c67893d | 2012-07-08 18:33:51 +0200 | [diff] [blame] | 480 | packet_pos = forw_packet->skb->data; |
| 481 | batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 482 | |
| 483 | /* adjust all flags and log packets */ |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 484 | while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len, |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 485 | batadv_ogm_packet->tvlv_len)) { |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 486 | /* we might have aggregated direct link packets with an |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 487 | * ordinary base packet |
| 488 | */ |
Sven Eckelmann | 8de47de | 2012-07-08 16:32:09 +0200 | [diff] [blame] | 489 | if (forw_packet->direct_link_flags & BIT(packet_num) && |
| 490 | forw_packet->if_incoming == hard_iface) |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 491 | batadv_ogm_packet->flags |= BATADV_DIRECTLINK; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 492 | else |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 493 | batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 494 | |
Sven Eckelmann | c67893d | 2012-07-08 18:33:51 +0200 | [diff] [blame] | 495 | if (packet_num > 0 || !forw_packet->own) |
| 496 | fwd_str = "Forwarding"; |
| 497 | else |
| 498 | fwd_str = "Sending own"; |
| 499 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 500 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Marek Lindner | e1bf0c1 | 2013-04-23 21:40:01 +0800 | [diff] [blame] | 501 | "%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] | 502 | fwd_str, (packet_num > 0 ? "aggregated " : ""), |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 503 | batadv_ogm_packet->orig, |
| 504 | ntohl(batadv_ogm_packet->seqno), |
Simon Wunderlich | a40d9b0 | 2013-12-02 20:38:31 +0100 | [diff] [blame] | 505 | batadv_ogm_packet->tq, batadv_ogm_packet->ttl, |
Sven Eckelmann | a2f2b6c | 2015-04-23 18:22:24 +0200 | [diff] [blame] | 506 | ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ? |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 507 | "on" : "off"), |
Marek Lindner | e1bf0c1 | 2013-04-23 21:40:01 +0800 | [diff] [blame] | 508 | hard_iface->net_dev->name, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 509 | hard_iface->net_dev->dev_addr); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 510 | |
Sven Eckelmann | 7e071c7 | 2012-06-03 22:19:13 +0200 | [diff] [blame] | 511 | buff_pos += BATADV_OGM_HLEN; |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 512 | buff_pos += ntohs(batadv_ogm_packet->tvlv_len); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 513 | packet_num++; |
Sven Eckelmann | c67893d | 2012-07-08 18:33:51 +0200 | [diff] [blame] | 514 | packet_pos = forw_packet->skb->data + buff_pos; |
| 515 | batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* create clone because function is called more than once */ |
| 519 | skb = skb_clone(forw_packet->skb, GFP_ATOMIC); |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 520 | if (skb) { |
Sven Eckelmann | d69909d | 2012-06-03 22:19:20 +0200 | [diff] [blame] | 521 | batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX); |
| 522 | batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES, |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 523 | skb->len + ETH_HLEN); |
Antonio Quartulli | 95d3927 | 2016-01-16 16:40:15 +0800 | [diff] [blame] | 524 | batadv_send_broadcast_skb(skb, hard_iface); |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 525 | } |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /* send a batman ogm packet */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 529 | static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 530 | { |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 531 | struct net_device *soft_iface; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 532 | |
| 533 | if (!forw_packet->if_incoming) { |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 534 | pr_err("Error - can't forward packet: incoming iface not specified\n"); |
Linus LĂĽssing | f55a2e8 | 2016-06-14 22:56:50 +0200 | [diff] [blame] | 535 | return; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | soft_iface = forw_packet->if_incoming->soft_iface; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 539 | |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 540 | if (WARN_ON(!forw_packet->if_outgoing)) |
Linus LĂĽssing | f55a2e8 | 2016-06-14 22:56:50 +0200 | [diff] [blame] | 541 | return; |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 542 | |
| 543 | if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface)) |
Linus LĂĽssing | f55a2e8 | 2016-06-14 22:56:50 +0200 | [diff] [blame] | 544 | return; |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 545 | |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 546 | if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE) |
Linus LĂĽssing | f55a2e8 | 2016-06-14 22:56:50 +0200 | [diff] [blame] | 547 | return; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 548 | |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 549 | /* only for one specific outgoing interface */ |
| 550 | batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 551 | } |
| 552 | |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 553 | /** |
| 554 | * batadv_iv_ogm_can_aggregate - find out if an OGM can be aggregated on an |
| 555 | * existing forward packet |
| 556 | * @new_bat_ogm_packet: OGM packet to be aggregated |
| 557 | * @bat_priv: the bat priv with all the soft interface information |
| 558 | * @packet_len: (total) length of the OGM |
| 559 | * @send_time: timestamp (jiffies) when the packet is to be sent |
Martin Hundebøll | 95298a9 | 2014-07-15 09:41:05 +0200 | [diff] [blame] | 560 | * @directlink: true if this is a direct link packet |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 561 | * @if_incoming: interface where the packet was received |
| 562 | * @if_outgoing: interface for which the retransmission should be considered |
| 563 | * @forw_packet: the forwarded packet which should be checked |
| 564 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 565 | * Return: true if new_packet can be aggregated with forw_packet |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 566 | */ |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 567 | static bool |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 568 | 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] | 569 | struct batadv_priv *bat_priv, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 570 | int packet_len, unsigned long send_time, |
| 571 | bool directlink, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 572 | const struct batadv_hard_iface *if_incoming, |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 573 | const struct batadv_hard_iface *if_outgoing, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 574 | const struct batadv_forw_packet *forw_packet) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 575 | { |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 576 | struct batadv_ogm_packet *batadv_ogm_packet; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 577 | int aggregated_bytes = forw_packet->packet_len + packet_len; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 578 | struct batadv_hard_iface *primary_if = NULL; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 579 | bool res = false; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 580 | unsigned long aggregation_end_time; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 581 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 582 | batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 583 | aggregation_end_time = send_time; |
| 584 | aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 585 | |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 586 | /* we can aggregate the current packet to this aggregated packet |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 587 | * if: |
| 588 | * |
| 589 | * - the send time is within our MAX_AGGREGATION_MS time |
| 590 | * - the resulting packet wont be bigger than |
| 591 | * MAX_AGGREGATION_BYTES |
Markus Pargmann | 8f34b38 | 2014-12-26 12:41:29 +0100 | [diff] [blame] | 592 | * otherwise aggregation is not possible |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 593 | */ |
Markus Pargmann | 8f34b38 | 2014-12-26 12:41:29 +0100 | [diff] [blame] | 594 | if (!time_before(send_time, forw_packet->send_time) || |
| 595 | !time_after_eq(aggregation_end_time, forw_packet->send_time)) |
| 596 | return false; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 597 | |
Markus Pargmann | 8f34b38 | 2014-12-26 12:41:29 +0100 | [diff] [blame] | 598 | if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES) |
| 599 | return false; |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 600 | |
Markus Pargmann | 8f34b38 | 2014-12-26 12:41:29 +0100 | [diff] [blame] | 601 | /* packet is not leaving on the same interface. */ |
| 602 | if (forw_packet->if_outgoing != if_outgoing) |
| 603 | return false; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 604 | |
Markus Pargmann | 8f34b38 | 2014-12-26 12:41:29 +0100 | [diff] [blame] | 605 | /* check aggregation compatibility |
| 606 | * -> direct link packets are broadcasted on |
| 607 | * their interface only |
| 608 | * -> aggregate packet if the current packet is |
| 609 | * a "global" packet as well as the base |
| 610 | * packet |
| 611 | */ |
| 612 | primary_if = batadv_primary_if_get_selected(bat_priv); |
| 613 | if (!primary_if) |
| 614 | return false; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 615 | |
Markus Pargmann | 8f34b38 | 2014-12-26 12:41:29 +0100 | [diff] [blame] | 616 | /* packets without direct link flag and high TTL |
| 617 | * are flooded through the net |
| 618 | */ |
| 619 | if (!directlink && |
| 620 | !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) && |
| 621 | batadv_ogm_packet->ttl != 1 && |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 622 | |
Markus Pargmann | 8f34b38 | 2014-12-26 12:41:29 +0100 | [diff] [blame] | 623 | /* own packets originating non-primary |
| 624 | * interfaces leave only that interface |
| 625 | */ |
| 626 | (!forw_packet->own || |
| 627 | forw_packet->if_incoming == primary_if)) { |
| 628 | res = true; |
| 629 | goto out; |
| 630 | } |
| 631 | |
| 632 | /* if the incoming packet is sent via this one |
| 633 | * interface only - we still can aggregate |
| 634 | */ |
| 635 | if (directlink && |
| 636 | new_bat_ogm_packet->ttl == 1 && |
| 637 | forw_packet->if_incoming == if_incoming && |
| 638 | |
| 639 | /* packets from direct neighbors or |
| 640 | * own secondary interface packets |
| 641 | * (= secondary interface packets in general) |
| 642 | */ |
| 643 | (batadv_ogm_packet->flags & BATADV_DIRECTLINK || |
| 644 | (forw_packet->own && |
| 645 | forw_packet->if_incoming != primary_if))) { |
| 646 | res = true; |
| 647 | goto out; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | out: |
| 651 | if (primary_if) |
Sven Eckelmann | 82047ad | 2016-01-17 11:01:10 +0100 | [diff] [blame] | 652 | batadv_hardif_put(primary_if); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 653 | return res; |
| 654 | } |
| 655 | |
Simon Wunderlich | 1b371d1 | 2014-01-15 21:17:54 +0100 | [diff] [blame] | 656 | /** |
| 657 | * batadv_iv_ogm_aggregate_new - create a new aggregated packet and add this |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 658 | * packet to it. |
| 659 | * @packet_buff: pointer to the OGM |
| 660 | * @packet_len: (total) length of the OGM |
| 661 | * @send_time: timestamp (jiffies) when the packet is to be sent |
| 662 | * @direct_link: whether this OGM has direct link status |
| 663 | * @if_incoming: interface where the packet was received |
| 664 | * @if_outgoing: interface for which the retransmission should be considered |
| 665 | * @own_packet: true if it is a self-generated ogm |
| 666 | */ |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 667 | static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff, |
| 668 | int packet_len, unsigned long send_time, |
| 669 | bool direct_link, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 670 | struct batadv_hard_iface *if_incoming, |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 671 | struct batadv_hard_iface *if_outgoing, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 672 | int own_packet) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 673 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 674 | struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
| 675 | struct batadv_forw_packet *forw_packet_aggr; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 676 | unsigned char *skb_buff; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 677 | unsigned int skb_size; |
Linus LĂĽssing | a65e548 | 2016-06-20 21:39:54 +0200 | [diff] [blame^] | 678 | atomic_t *queue_left = own_packet ? NULL : &bat_priv->batman_queue_left; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 679 | |
Linus LĂĽssing | a65e548 | 2016-06-20 21:39:54 +0200 | [diff] [blame^] | 680 | forw_packet_aggr = batadv_forw_packet_alloc(if_incoming, if_outgoing, |
| 681 | queue_left, bat_priv); |
Markus Pargmann | 940d156 | 2014-12-26 12:41:31 +0100 | [diff] [blame] | 682 | if (!forw_packet_aggr) |
Linus LĂĽssing | a65e548 | 2016-06-20 21:39:54 +0200 | [diff] [blame^] | 683 | return; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 684 | |
Markus Pargmann | 940d156 | 2014-12-26 12:41:31 +0100 | [diff] [blame] | 685 | if (atomic_read(&bat_priv->aggregated_ogms) && |
| 686 | packet_len < BATADV_MAX_AGGREGATION_BYTES) |
Sven Eckelmann | 5b24657 | 2012-11-04 17:11:45 +0100 | [diff] [blame] | 687 | skb_size = BATADV_MAX_AGGREGATION_BYTES; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 688 | else |
Sven Eckelmann | 5b24657 | 2012-11-04 17:11:45 +0100 | [diff] [blame] | 689 | skb_size = packet_len; |
| 690 | |
Antonio Quartulli | 41ab6c4 | 2013-04-02 22:28:44 +0200 | [diff] [blame] | 691 | skb_size += ETH_HLEN; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 692 | |
Antonio Quartulli | 41ab6c4 | 2013-04-02 22:28:44 +0200 | [diff] [blame] | 693 | forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size); |
Linus LĂĽssing | a65e548 | 2016-06-20 21:39:54 +0200 | [diff] [blame^] | 694 | if (!forw_packet_aggr->skb) { |
| 695 | batadv_forw_packet_free(forw_packet_aggr); |
| 696 | return; |
| 697 | } |
| 698 | |
Simon Wunderlich | c54f38c | 2013-07-29 17:56:44 +0200 | [diff] [blame] | 699 | forw_packet_aggr->skb->priority = TC_PRIO_CONTROL; |
Antonio Quartulli | 41ab6c4 | 2013-04-02 22:28:44 +0200 | [diff] [blame] | 700 | skb_reserve(forw_packet_aggr->skb, ETH_HLEN); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 701 | |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 702 | skb_buff = skb_put(forw_packet_aggr->skb, packet_len); |
| 703 | forw_packet_aggr->packet_len = packet_len; |
| 704 | memcpy(skb_buff, packet_buff, packet_len); |
| 705 | |
| 706 | forw_packet_aggr->own = own_packet; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 707 | forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 708 | forw_packet_aggr->send_time = send_time; |
| 709 | |
| 710 | /* save packet direct link flag status */ |
| 711 | if (direct_link) |
| 712 | forw_packet_aggr->direct_link_flags |= 1; |
| 713 | |
| 714 | /* add new packet to packet list */ |
| 715 | spin_lock_bh(&bat_priv->forw_bat_list_lock); |
| 716 | hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list); |
| 717 | spin_unlock_bh(&bat_priv->forw_bat_list_lock); |
| 718 | |
| 719 | /* start timer for this packet */ |
| 720 | INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work, |
Antonio Quartulli | f0d9725 | 2016-05-03 01:45:34 +0800 | [diff] [blame] | 721 | batadv_iv_send_outstanding_bat_ogm_packet); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 722 | queue_delayed_work(batadv_event_workqueue, |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 723 | &forw_packet_aggr->delayed_work, |
| 724 | send_time - jiffies); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /* aggregate a new packet into the existing ogm packet */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 728 | 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] | 729 | const unsigned char *packet_buff, |
| 730 | int packet_len, bool direct_link) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 731 | { |
| 732 | unsigned char *skb_buff; |
Sven Eckelmann | 8de47de | 2012-07-08 16:32:09 +0200 | [diff] [blame] | 733 | unsigned long new_direct_link_flag; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 734 | |
| 735 | skb_buff = skb_put(forw_packet_aggr->skb, packet_len); |
| 736 | memcpy(skb_buff, packet_buff, packet_len); |
| 737 | forw_packet_aggr->packet_len += packet_len; |
| 738 | forw_packet_aggr->num_packets++; |
| 739 | |
| 740 | /* save packet direct link flag status */ |
Sven Eckelmann | 8de47de | 2012-07-08 16:32:09 +0200 | [diff] [blame] | 741 | if (direct_link) { |
| 742 | new_direct_link_flag = BIT(forw_packet_aggr->num_packets); |
| 743 | forw_packet_aggr->direct_link_flags |= new_direct_link_flag; |
| 744 | } |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 745 | } |
| 746 | |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 747 | /** |
| 748 | * batadv_iv_ogm_queue_add - queue up an OGM for transmission |
| 749 | * @bat_priv: the bat priv with all the soft interface information |
| 750 | * @packet_buff: pointer to the OGM |
| 751 | * @packet_len: (total) length of the OGM |
| 752 | * @if_incoming: interface where the packet was received |
| 753 | * @if_outgoing: interface for which the retransmission should be considered |
| 754 | * @own_packet: true if it is a self-generated ogm |
| 755 | * @send_time: timestamp (jiffies) when the packet is to be sent |
| 756 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 757 | static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 758 | unsigned char *packet_buff, |
| 759 | int packet_len, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 760 | struct batadv_hard_iface *if_incoming, |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 761 | struct batadv_hard_iface *if_outgoing, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 762 | int own_packet, unsigned long send_time) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 763 | { |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 764 | /* _aggr -> pointer to the packet we want to aggregate with |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 765 | * _pos -> pointer to the position in the queue |
| 766 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 767 | struct batadv_forw_packet *forw_packet_aggr = NULL; |
| 768 | struct batadv_forw_packet *forw_packet_pos = NULL; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 769 | struct batadv_ogm_packet *batadv_ogm_packet; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 770 | bool direct_link; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 771 | unsigned long max_aggregation_jiffies; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 772 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 773 | batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff; |
Markus Pargmann | 5648915 | 2014-12-26 12:41:32 +0100 | [diff] [blame] | 774 | direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK); |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 775 | max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 776 | |
| 777 | /* find position for the packet in the forward queue */ |
| 778 | spin_lock_bh(&bat_priv->forw_bat_list_lock); |
| 779 | /* own packets are not to be aggregated */ |
Markus Pargmann | 5648915 | 2014-12-26 12:41:32 +0100 | [diff] [blame] | 780 | if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 781 | hlist_for_each_entry(forw_packet_pos, |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 782 | &bat_priv->forw_bat_list, list) { |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 783 | if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 784 | bat_priv, packet_len, |
| 785 | send_time, direct_link, |
| 786 | if_incoming, |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 787 | if_outgoing, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 788 | forw_packet_pos)) { |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 789 | forw_packet_aggr = forw_packet_pos; |
| 790 | break; |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | /* nothing to aggregate with - either aggregation disabled or no |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 796 | * suitable aggregation packet found |
| 797 | */ |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 798 | if (!forw_packet_aggr) { |
| 799 | /* the following section can run without the lock */ |
| 800 | spin_unlock_bh(&bat_priv->forw_bat_list_lock); |
| 801 | |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 802 | /* if we could not aggregate this packet with one of the others |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 803 | * we hold it back for a while, so that it might be aggregated |
| 804 | * later on |
| 805 | */ |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 806 | if (!own_packet && atomic_read(&bat_priv->aggregated_ogms)) |
| 807 | send_time += max_aggregation_jiffies; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 808 | |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 809 | batadv_iv_ogm_aggregate_new(packet_buff, packet_len, |
| 810 | send_time, direct_link, |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 811 | if_incoming, if_outgoing, |
| 812 | own_packet); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 813 | } else { |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 814 | batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff, |
| 815 | packet_len, direct_link); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 816 | spin_unlock_bh(&bat_priv->forw_bat_list_lock); |
| 817 | } |
| 818 | } |
| 819 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 820 | static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 821 | const struct ethhdr *ethhdr, |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 822 | struct batadv_ogm_packet *batadv_ogm_packet, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 823 | bool is_single_hop_neigh, |
| 824 | bool is_from_best_next_hop, |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 825 | struct batadv_hard_iface *if_incoming, |
| 826 | struct batadv_hard_iface *if_outgoing) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 827 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 828 | struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 829 | u16 tvlv_len; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 830 | |
Simon Wunderlich | a40d9b0 | 2013-12-02 20:38:31 +0100 | [diff] [blame] | 831 | if (batadv_ogm_packet->ttl <= 1) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 832 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n"); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 833 | return; |
| 834 | } |
| 835 | |
Marek Lindner | 13b2541 | 2012-03-11 06:17:53 +0800 | [diff] [blame] | 836 | if (!is_from_best_next_hop) { |
| 837 | /* Mark the forwarded packet when it is not coming from our |
| 838 | * best next hop. We still need to forward the packet for our |
| 839 | * neighbor link quality detection to work in case the packet |
| 840 | * originated from a single hop neighbor. Otherwise we can |
| 841 | * simply drop the ogm. |
| 842 | */ |
| 843 | if (is_single_hop_neigh) |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 844 | batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP; |
Marek Lindner | 13b2541 | 2012-03-11 06:17:53 +0800 | [diff] [blame] | 845 | else |
| 846 | return; |
| 847 | } |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 848 | |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 849 | tvlv_len = ntohs(batadv_ogm_packet->tvlv_len); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 850 | |
Simon Wunderlich | a40d9b0 | 2013-12-02 20:38:31 +0100 | [diff] [blame] | 851 | batadv_ogm_packet->ttl--; |
Antonio Quartulli | 8fdd015 | 2014-01-22 00:42:11 +0100 | [diff] [blame] | 852 | ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 853 | |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 854 | /* apply hop penalty */ |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 855 | batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 856 | bat_priv); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 857 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 858 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 859 | "Forwarding packet: tq: %i, ttl: %i\n", |
Simon Wunderlich | a40d9b0 | 2013-12-02 20:38:31 +0100 | [diff] [blame] | 860 | batadv_ogm_packet->tq, batadv_ogm_packet->ttl); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 861 | |
Marek Lindner | 75cd33f | 2012-03-01 15:35:16 +0800 | [diff] [blame] | 862 | if (is_single_hop_neigh) |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 863 | batadv_ogm_packet->flags |= BATADV_DIRECTLINK; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 864 | else |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 865 | batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 866 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 867 | batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet, |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 868 | BATADV_OGM_HLEN + tvlv_len, |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 869 | if_incoming, if_outgoing, 0, |
| 870 | batadv_iv_ogm_fwd_send_time()); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 871 | } |
| 872 | |
Antonio Quartulli | d989661 | 2013-04-17 17:44:43 +0200 | [diff] [blame] | 873 | /** |
| 874 | * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for |
| 875 | * the given interface |
| 876 | * @hard_iface: the interface for which the windows have to be shifted |
| 877 | */ |
| 878 | static void |
| 879 | batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface) |
| 880 | { |
| 881 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
| 882 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
| 883 | struct hlist_head *head; |
| 884 | struct batadv_orig_node *orig_node; |
| 885 | unsigned long *word; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 886 | u32 i; |
Antonio Quartulli | d989661 | 2013-04-17 17:44:43 +0200 | [diff] [blame] | 887 | size_t word_index; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 888 | u8 *w; |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 889 | int if_num; |
Antonio Quartulli | d989661 | 2013-04-17 17:44:43 +0200 | [diff] [blame] | 890 | |
| 891 | for (i = 0; i < hash->size; i++) { |
| 892 | head = &hash->table[i]; |
| 893 | |
| 894 | rcu_read_lock(); |
| 895 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 896 | spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
Antonio Quartulli | d989661 | 2013-04-17 17:44:43 +0200 | [diff] [blame] | 897 | word_index = hard_iface->if_num * BATADV_NUM_WORDS; |
Antonio Quartulli | 32db6aa | 2014-09-01 14:37:29 +0200 | [diff] [blame] | 898 | word = &orig_node->bat_iv.bcast_own[word_index]; |
Antonio Quartulli | d989661 | 2013-04-17 17:44:43 +0200 | [diff] [blame] | 899 | |
| 900 | batadv_bit_get_packet(bat_priv, word, 1, 0); |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 901 | if_num = hard_iface->if_num; |
| 902 | w = &orig_node->bat_iv.bcast_own_sum[if_num]; |
Antonio Quartulli | d989661 | 2013-04-17 17:44:43 +0200 | [diff] [blame] | 903 | *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE); |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 904 | spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
Antonio Quartulli | d989661 | 2013-04-17 17:44:43 +0200 | [diff] [blame] | 905 | } |
| 906 | rcu_read_unlock(); |
| 907 | } |
| 908 | } |
| 909 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 910 | static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface) |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 911 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 912 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 913 | unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 914 | struct batadv_ogm_packet *batadv_ogm_packet; |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 915 | struct batadv_hard_iface *primary_if, *tmp_hard_iface; |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 916 | int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 917 | u32 seqno; |
| 918 | u16 tvlv_len = 0; |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 919 | unsigned long send_time; |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 920 | |
Antonio Quartulli | f0d9725 | 2016-05-03 01:45:34 +0800 | [diff] [blame] | 921 | if ((hard_iface->if_status == BATADV_IF_NOT_IN_USE) || |
| 922 | (hard_iface->if_status == BATADV_IF_TO_BE_REMOVED)) |
| 923 | return; |
| 924 | |
| 925 | /* the interface gets activated here to avoid race conditions between |
| 926 | * the moment of activating the interface in |
| 927 | * hardif_activate_interface() where the originator mac is set and |
| 928 | * outdated packets (especially uninitialized mac addresses) in the |
| 929 | * packet queue |
| 930 | */ |
| 931 | if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED) |
| 932 | hard_iface->if_status = BATADV_IF_ACTIVE; |
| 933 | |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 934 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 935 | |
Marek Lindner | e1bf0c1 | 2013-04-23 21:40:01 +0800 | [diff] [blame] | 936 | if (hard_iface == primary_if) { |
| 937 | /* tt changes have to be committed before the tvlv data is |
| 938 | * appended as it may alter the tt tvlv container |
| 939 | */ |
| 940 | batadv_tt_local_commit_changes(bat_priv); |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 941 | tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff, |
| 942 | ogm_buff_len, |
| 943 | BATADV_OGM_HLEN); |
Marek Lindner | e1bf0c1 | 2013-04-23 21:40:01 +0800 | [diff] [blame] | 944 | } |
Marek Lindner | be9aa4c | 2012-05-07 04:22:05 +0800 | [diff] [blame] | 945 | |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 946 | batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff); |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 947 | batadv_ogm_packet->tvlv_len = htons(tvlv_len); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 948 | |
| 949 | /* change sequence number to network order */ |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 950 | seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno); |
Sven Eckelmann | bbb1f90 | 2012-07-08 17:13:15 +0200 | [diff] [blame] | 951 | batadv_ogm_packet->seqno = htonl(seqno); |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 952 | atomic_inc(&hard_iface->bat_iv.ogm_seqno); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 953 | |
Antonio Quartulli | d989661 | 2013-04-17 17:44:43 +0200 | [diff] [blame] | 954 | batadv_iv_ogm_slide_own_bcast_window(hard_iface); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 955 | |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 956 | send_time = batadv_iv_ogm_emit_send_time(bat_priv); |
| 957 | |
| 958 | if (hard_iface != primary_if) { |
| 959 | /* OGMs from secondary interfaces are only scheduled on their |
| 960 | * respective interfaces. |
| 961 | */ |
| 962 | batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len, |
| 963 | hard_iface, hard_iface, 1, send_time); |
| 964 | goto out; |
| 965 | } |
| 966 | |
| 967 | /* OGMs from primary interfaces are scheduled on all |
| 968 | * interfaces. |
| 969 | */ |
| 970 | rcu_read_lock(); |
| 971 | list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) { |
| 972 | if (tmp_hard_iface->soft_iface != hard_iface->soft_iface) |
Sven Eckelmann | 87b40f5 | 2015-07-17 10:03:42 +0200 | [diff] [blame] | 973 | continue; |
Sven Eckelmann | 2735344 | 2016-03-05 16:09:16 +0100 | [diff] [blame] | 974 | |
| 975 | if (!kref_get_unless_zero(&tmp_hard_iface->refcount)) |
| 976 | continue; |
| 977 | |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 978 | batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, |
| 979 | *ogm_buff_len, hard_iface, |
| 980 | tmp_hard_iface, 1, send_time); |
Sven Eckelmann | 2735344 | 2016-03-05 16:09:16 +0100 | [diff] [blame] | 981 | |
| 982 | batadv_hardif_put(tmp_hard_iface); |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 983 | } |
| 984 | rcu_read_unlock(); |
| 985 | |
| 986 | out: |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 987 | if (primary_if) |
Sven Eckelmann | 82047ad | 2016-01-17 11:01:10 +0100 | [diff] [blame] | 988 | batadv_hardif_put(primary_if); |
Marek Lindner | b9dacc5 | 2011-08-03 09:09:30 +0200 | [diff] [blame] | 989 | } |
| 990 | |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 991 | /** |
| 992 | * batadv_iv_ogm_orig_update - use OGM to update corresponding data in an |
| 993 | * originator |
| 994 | * @bat_priv: the bat priv with all the soft interface information |
| 995 | * @orig_node: the orig node who originally emitted the ogm packet |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 996 | * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 997 | * @ethhdr: Ethernet header of the OGM |
| 998 | * @batadv_ogm_packet: the ogm packet |
| 999 | * @if_incoming: interface where the packet was received |
| 1000 | * @if_outgoing: interface for which the retransmission should be considered |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1001 | * @dup_status: the duplicate status of this ogm packet. |
| 1002 | */ |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 1003 | static void |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1004 | batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv, |
| 1005 | struct batadv_orig_node *orig_node, |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1006 | struct batadv_orig_ifinfo *orig_ifinfo, |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 1007 | const struct ethhdr *ethhdr, |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1008 | const struct batadv_ogm_packet *batadv_ogm_packet, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1009 | struct batadv_hard_iface *if_incoming, |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1010 | struct batadv_hard_iface *if_outgoing, |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1011 | enum batadv_dup_status dup_status) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1012 | { |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1013 | struct batadv_neigh_ifinfo *neigh_ifinfo = NULL; |
| 1014 | struct batadv_neigh_ifinfo *router_ifinfo = NULL; |
Sven Eckelmann | 4f248cf | 2015-06-09 20:50:49 +0200 | [diff] [blame] | 1015 | struct batadv_neigh_node *neigh_node = NULL; |
| 1016 | struct batadv_neigh_node *tmp_neigh_node = NULL; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1017 | struct batadv_neigh_node *router = NULL; |
| 1018 | struct batadv_orig_node *orig_node_tmp; |
Linus LĂĽssing | 7caf69f | 2012-09-18 03:01:08 +0200 | [diff] [blame] | 1019 | int if_num; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 1020 | u8 sum_orig, sum_neigh; |
| 1021 | u8 *neigh_addr; |
| 1022 | u8 tq_avg; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1023 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1024 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1025 | "update_originator(): Searching and updating originator entry of received packet\n"); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1026 | |
| 1027 | rcu_read_lock(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1028 | hlist_for_each_entry_rcu(tmp_neigh_node, |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1029 | &orig_node->neigh_list, list) { |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1030 | neigh_addr = tmp_neigh_node->addr; |
| 1031 | if (batadv_compare_eth(neigh_addr, ethhdr->h_source) && |
| 1032 | tmp_neigh_node->if_incoming == if_incoming && |
Sven Eckelmann | 77ae32e | 2016-01-16 10:29:53 +0100 | [diff] [blame] | 1033 | kref_get_unless_zero(&tmp_neigh_node->refcount)) { |
Antonio Quartulli | 38dc40e | 2013-03-30 17:22:00 +0100 | [diff] [blame] | 1034 | if (WARN(neigh_node, "too many matching neigh_nodes")) |
Sven Eckelmann | 25bb250 | 2016-01-17 11:01:11 +0100 | [diff] [blame] | 1035 | batadv_neigh_node_put(neigh_node); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1036 | neigh_node = tmp_neigh_node; |
| 1037 | continue; |
| 1038 | } |
| 1039 | |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1040 | if (dup_status != BATADV_NO_DUP) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1041 | continue; |
| 1042 | |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1043 | /* only update the entry for this outgoing interface */ |
| 1044 | neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node, |
| 1045 | if_outgoing); |
| 1046 | if (!neigh_ifinfo) |
| 1047 | continue; |
| 1048 | |
| 1049 | spin_lock_bh(&tmp_neigh_node->ifinfo_lock); |
| 1050 | batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv, |
| 1051 | &neigh_ifinfo->bat_iv.tq_index, 0); |
| 1052 | tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv); |
| 1053 | neigh_ifinfo->bat_iv.tq_avg = tq_avg; |
| 1054 | spin_unlock_bh(&tmp_neigh_node->ifinfo_lock); |
| 1055 | |
Sven Eckelmann | 044fa3a | 2016-01-17 11:01:12 +0100 | [diff] [blame] | 1056 | batadv_neigh_ifinfo_put(neigh_ifinfo); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1057 | neigh_ifinfo = NULL; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | if (!neigh_node) { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1061 | struct batadv_orig_node *orig_tmp; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1062 | |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1063 | orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1064 | if (!orig_tmp) |
| 1065 | goto unlock; |
| 1066 | |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 1067 | neigh_node = batadv_iv_ogm_neigh_new(if_incoming, |
| 1068 | ethhdr->h_source, |
Antonio Quartulli | 863dd7a | 2013-03-25 13:49:46 +0100 | [diff] [blame] | 1069 | orig_node, orig_tmp); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1070 | |
Sven Eckelmann | 5d96731 | 2016-01-17 11:01:09 +0100 | [diff] [blame] | 1071 | batadv_orig_node_put(orig_tmp); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1072 | if (!neigh_node) |
| 1073 | goto unlock; |
Markus Pargmann | 23badd6 | 2014-12-26 12:41:33 +0100 | [diff] [blame] | 1074 | } else { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1075 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1076 | "Updating existing last-hop neighbor of originator\n"); |
Markus Pargmann | 23badd6 | 2014-12-26 12:41:33 +0100 | [diff] [blame] | 1077 | } |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1078 | |
| 1079 | rcu_read_unlock(); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1080 | neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing); |
| 1081 | if (!neigh_ifinfo) |
| 1082 | goto out; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1083 | |
Marek Lindner | d7b2a97 | 2012-03-01 15:35:19 +0800 | [diff] [blame] | 1084 | neigh_node->last_seen = jiffies; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1085 | |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1086 | spin_lock_bh(&neigh_node->ifinfo_lock); |
| 1087 | batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv, |
| 1088 | &neigh_ifinfo->bat_iv.tq_index, |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1089 | batadv_ogm_packet->tq); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1090 | tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv); |
| 1091 | neigh_ifinfo->bat_iv.tq_avg = tq_avg; |
| 1092 | spin_unlock_bh(&neigh_node->ifinfo_lock); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1093 | |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1094 | if (dup_status == BATADV_NO_DUP) { |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1095 | orig_ifinfo->last_ttl = batadv_ogm_packet->ttl; |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1096 | neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1097 | } |
| 1098 | |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1099 | /* if this neighbor already is our next hop there is nothing |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1100 | * to change |
| 1101 | */ |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1102 | router = batadv_orig_router_get(orig_node, if_outgoing); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1103 | if (router == neigh_node) |
Marek Lindner | e1bf0c1 | 2013-04-23 21:40:01 +0800 | [diff] [blame] | 1104 | goto out; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1105 | |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1106 | if (router) { |
| 1107 | router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing); |
| 1108 | if (!router_ifinfo) |
| 1109 | goto out; |
| 1110 | |
| 1111 | /* if this neighbor does not offer a better TQ we won't |
| 1112 | * consider it |
| 1113 | */ |
| 1114 | if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg) |
| 1115 | goto out; |
| 1116 | } |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1117 | |
| 1118 | /* 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] | 1119 | * won't consider it either |
| 1120 | */ |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1121 | if (router_ifinfo && |
Markus Pargmann | 01b97a3 | 2014-12-26 12:41:30 +0100 | [diff] [blame] | 1122 | neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) { |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1123 | orig_node_tmp = router->orig_node; |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1124 | spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock); |
Linus LĂĽssing | 7caf69f | 2012-09-18 03:01:08 +0200 | [diff] [blame] | 1125 | if_num = router->if_incoming->if_num; |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1126 | sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num]; |
| 1127 | spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1128 | |
| 1129 | orig_node_tmp = neigh_node->orig_node; |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1130 | spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock); |
Linus LĂĽssing | 7caf69f | 2012-09-18 03:01:08 +0200 | [diff] [blame] | 1131 | if_num = neigh_node->if_incoming->if_num; |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1132 | sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num]; |
| 1133 | spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1134 | |
Sven Eckelmann | bbb1f90 | 2012-07-08 17:13:15 +0200 | [diff] [blame] | 1135 | if (sum_orig >= sum_neigh) |
Marek Lindner | e1bf0c1 | 2013-04-23 21:40:01 +0800 | [diff] [blame] | 1136 | goto out; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1137 | } |
| 1138 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1139 | batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1140 | goto out; |
| 1141 | |
| 1142 | unlock: |
| 1143 | rcu_read_unlock(); |
| 1144 | out: |
| 1145 | if (neigh_node) |
Sven Eckelmann | 25bb250 | 2016-01-17 11:01:11 +0100 | [diff] [blame] | 1146 | batadv_neigh_node_put(neigh_node); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1147 | if (router) |
Sven Eckelmann | 25bb250 | 2016-01-17 11:01:11 +0100 | [diff] [blame] | 1148 | batadv_neigh_node_put(router); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1149 | if (neigh_ifinfo) |
Sven Eckelmann | 044fa3a | 2016-01-17 11:01:12 +0100 | [diff] [blame] | 1150 | batadv_neigh_ifinfo_put(neigh_ifinfo); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1151 | if (router_ifinfo) |
Sven Eckelmann | 044fa3a | 2016-01-17 11:01:12 +0100 | [diff] [blame] | 1152 | batadv_neigh_ifinfo_put(router_ifinfo); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1153 | } |
| 1154 | |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1155 | /** |
| 1156 | * batadv_iv_ogm_calc_tq - calculate tq for current received ogm packet |
| 1157 | * @orig_node: the orig node who originally emitted the ogm packet |
| 1158 | * @orig_neigh_node: the orig node struct of the neighbor who sent the packet |
| 1159 | * @batadv_ogm_packet: the ogm packet |
| 1160 | * @if_incoming: interface where the packet was received |
| 1161 | * @if_outgoing: interface for which the retransmission should be considered |
| 1162 | * |
Sven Eckelmann | 4b426b1 | 2016-02-22 21:02:39 +0100 | [diff] [blame] | 1163 | * Return: true if the link can be considered bidirectional, false otherwise |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1164 | */ |
Sven Eckelmann | 4b426b1 | 2016-02-22 21:02:39 +0100 | [diff] [blame] | 1165 | static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node, |
| 1166 | struct batadv_orig_node *orig_neigh_node, |
| 1167 | struct batadv_ogm_packet *batadv_ogm_packet, |
| 1168 | struct batadv_hard_iface *if_incoming, |
| 1169 | struct batadv_hard_iface *if_outgoing) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1170 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1171 | struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
| 1172 | struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node; |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1173 | struct batadv_neigh_ifinfo *neigh_ifinfo; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 1174 | u8 total_count; |
| 1175 | u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1176 | unsigned int neigh_rq_inv_cube, neigh_rq_max_cube; |
Sven Eckelmann | d285f52 | 2016-02-16 10:47:07 +0100 | [diff] [blame] | 1177 | int if_num; |
| 1178 | unsigned int tq_asym_penalty, inv_asym_penalty; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1179 | unsigned int combined_tq; |
Sven Eckelmann | d285f52 | 2016-02-16 10:47:07 +0100 | [diff] [blame] | 1180 | unsigned int tq_iface_penalty; |
Sven Eckelmann | 4b426b1 | 2016-02-22 21:02:39 +0100 | [diff] [blame] | 1181 | bool ret = false; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1182 | |
| 1183 | /* find corresponding one hop neighbor */ |
| 1184 | rcu_read_lock(); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1185 | hlist_for_each_entry_rcu(tmp_neigh_node, |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1186 | &orig_neigh_node->neigh_list, list) { |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1187 | if (!batadv_compare_eth(tmp_neigh_node->addr, |
| 1188 | orig_neigh_node->orig)) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1189 | continue; |
| 1190 | |
| 1191 | if (tmp_neigh_node->if_incoming != if_incoming) |
| 1192 | continue; |
| 1193 | |
Sven Eckelmann | 77ae32e | 2016-01-16 10:29:53 +0100 | [diff] [blame] | 1194 | if (!kref_get_unless_zero(&tmp_neigh_node->refcount)) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1195 | continue; |
| 1196 | |
| 1197 | neigh_node = tmp_neigh_node; |
| 1198 | break; |
| 1199 | } |
| 1200 | rcu_read_unlock(); |
| 1201 | |
| 1202 | if (!neigh_node) |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 1203 | neigh_node = batadv_iv_ogm_neigh_new(if_incoming, |
| 1204 | orig_neigh_node->orig, |
| 1205 | orig_neigh_node, |
Antonio Quartulli | 863dd7a | 2013-03-25 13:49:46 +0100 | [diff] [blame] | 1206 | orig_neigh_node); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1207 | |
| 1208 | if (!neigh_node) |
| 1209 | goto out; |
| 1210 | |
Marek Lindner | d7b2a97 | 2012-03-01 15:35:19 +0800 | [diff] [blame] | 1211 | /* if orig_node is direct neighbor update neigh_node last_seen */ |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1212 | if (orig_node == orig_neigh_node) |
Marek Lindner | d7b2a97 | 2012-03-01 15:35:19 +0800 | [diff] [blame] | 1213 | neigh_node->last_seen = jiffies; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1214 | |
Marek Lindner | d7b2a97 | 2012-03-01 15:35:19 +0800 | [diff] [blame] | 1215 | orig_node->last_seen = jiffies; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1216 | |
| 1217 | /* find packet count of corresponding one hop neighbor */ |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1218 | spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
| 1219 | if_num = if_incoming->if_num; |
| 1220 | 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] | 1221 | neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing); |
| 1222 | if (neigh_ifinfo) { |
| 1223 | neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count; |
Sven Eckelmann | 044fa3a | 2016-01-17 11:01:12 +0100 | [diff] [blame] | 1224 | batadv_neigh_ifinfo_put(neigh_ifinfo); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1225 | } else { |
| 1226 | neigh_rq_count = 0; |
| 1227 | } |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1228 | spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1229 | |
| 1230 | /* pay attention to not get a value bigger than 100 % */ |
Sven Eckelmann | c67893d | 2012-07-08 18:33:51 +0200 | [diff] [blame] | 1231 | if (orig_eq_count > neigh_rq_count) |
| 1232 | total_count = neigh_rq_count; |
| 1233 | else |
| 1234 | total_count = orig_eq_count; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1235 | |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1236 | /* if we have too few packets (too less data) we set tq_own to zero |
| 1237 | * if we receive too few packets it is not considered bidirectional |
| 1238 | */ |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1239 | if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM || |
| 1240 | neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1241 | tq_own = 0; |
| 1242 | else |
| 1243 | /* neigh_node->real_packet_count is never zero as we |
| 1244 | * only purge old information when getting new |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1245 | * information |
| 1246 | */ |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1247 | tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1248 | |
Sven Eckelmann | 21a1236 | 2012-03-07 09:07:46 +0100 | [diff] [blame] | 1249 | /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1250 | * affect the nearly-symmetric links only a little, but |
| 1251 | * punishes asymmetric links more. This will give a value |
| 1252 | * between 0 and TQ_MAX_VALUE |
| 1253 | */ |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1254 | neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count; |
| 1255 | neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv; |
| 1256 | neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE * |
| 1257 | BATADV_TQ_LOCAL_WINDOW_SIZE * |
| 1258 | BATADV_TQ_LOCAL_WINDOW_SIZE; |
| 1259 | inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube; |
| 1260 | inv_asym_penalty /= neigh_rq_max_cube; |
| 1261 | tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1262 | |
Simon Wunderlich | c039876 | 2013-11-13 19:14:48 +0100 | [diff] [blame] | 1263 | /* penalize if the OGM is forwarded on the same interface. WiFi |
| 1264 | * interfaces and other half duplex devices suffer from throughput |
| 1265 | * drops as they can't send and receive at the same time. |
| 1266 | */ |
| 1267 | tq_iface_penalty = BATADV_TQ_MAX_VALUE; |
| 1268 | if (if_outgoing && (if_incoming == if_outgoing) && |
| 1269 | batadv_is_wifi_netdev(if_outgoing->net_dev)) |
| 1270 | tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE, |
| 1271 | bat_priv); |
| 1272 | |
| 1273 | combined_tq = batadv_ogm_packet->tq * |
| 1274 | tq_own * |
| 1275 | tq_asym_penalty * |
| 1276 | tq_iface_penalty; |
| 1277 | combined_tq /= BATADV_TQ_MAX_VALUE * |
| 1278 | BATADV_TQ_MAX_VALUE * |
| 1279 | BATADV_TQ_MAX_VALUE; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1280 | batadv_ogm_packet->tq = combined_tq; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1281 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1282 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Simon Wunderlich | c039876 | 2013-11-13 19:14:48 +0100 | [diff] [blame] | 1283 | "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] | 1284 | orig_node->orig, orig_neigh_node->orig, total_count, |
Simon Wunderlich | c039876 | 2013-11-13 19:14:48 +0100 | [diff] [blame] | 1285 | neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty, |
| 1286 | batadv_ogm_packet->tq, if_incoming->net_dev->name, |
| 1287 | if_outgoing ? if_outgoing->net_dev->name : "DEFAULT"); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1288 | |
| 1289 | /* if link has the minimum required transmission quality |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1290 | * consider it bidirectional |
| 1291 | */ |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1292 | if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT) |
Sven Eckelmann | 4b426b1 | 2016-02-22 21:02:39 +0100 | [diff] [blame] | 1293 | ret = true; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1294 | |
| 1295 | out: |
| 1296 | if (neigh_node) |
Sven Eckelmann | 25bb250 | 2016-01-17 11:01:11 +0100 | [diff] [blame] | 1297 | batadv_neigh_node_put(neigh_node); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1298 | return ret; |
| 1299 | } |
| 1300 | |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1301 | /** |
| 1302 | * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces, |
| 1303 | * adjust the sequence number and find out whether it is a duplicate |
| 1304 | * @ethhdr: ethernet header of the packet |
| 1305 | * @batadv_ogm_packet: OGM packet to be considered |
| 1306 | * @if_incoming: interface on which the OGM packet was received |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1307 | * @if_outgoing: interface for which the retransmission should be considered |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1308 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 1309 | * Return: duplicate status as enum batadv_dup_status |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1310 | */ |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1311 | static enum batadv_dup_status |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 1312 | batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr, |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 1313 | const struct batadv_ogm_packet *batadv_ogm_packet, |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1314 | const struct batadv_hard_iface *if_incoming, |
| 1315 | struct batadv_hard_iface *if_outgoing) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1316 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1317 | struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
| 1318 | struct batadv_orig_node *orig_node; |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1319 | struct batadv_orig_ifinfo *orig_ifinfo = NULL; |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1320 | struct batadv_neigh_node *neigh_node; |
| 1321 | struct batadv_neigh_ifinfo *neigh_ifinfo; |
Sven Eckelmann | 4b426b1 | 2016-02-22 21:02:39 +0100 | [diff] [blame] | 1322 | bool is_dup; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 1323 | s32 seq_diff; |
Sven Eckelmann | 4b426b1 | 2016-02-22 21:02:39 +0100 | [diff] [blame] | 1324 | bool need_update = false; |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1325 | int set_mark; |
| 1326 | enum batadv_dup_status ret = BATADV_NO_DUP; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 1327 | u32 seqno = ntohl(batadv_ogm_packet->seqno); |
| 1328 | u8 *neigh_addr; |
| 1329 | u8 packet_count; |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 1330 | unsigned long *bitmap; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1331 | |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1332 | 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] | 1333 | if (!orig_node) |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1334 | return BATADV_NO_DUP; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1335 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1336 | orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing); |
| 1337 | if (WARN_ON(!orig_ifinfo)) { |
Sven Eckelmann | 5d96731 | 2016-01-17 11:01:09 +0100 | [diff] [blame] | 1338 | batadv_orig_node_put(orig_node); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1339 | return 0; |
| 1340 | } |
| 1341 | |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1342 | spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1343 | seq_diff = seqno - orig_ifinfo->last_real_seqno; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1344 | |
| 1345 | /* signalize caller that the packet is to be dropped. */ |
Antonio Quartulli | 1e5cc26 | 2012-02-26 15:39:42 +0100 | [diff] [blame] | 1346 | if (!hlist_empty(&orig_node->neigh_list) && |
Sven Eckelmann | 30d3c51 | 2012-05-12 02:09:36 +0200 | [diff] [blame] | 1347 | batadv_window_protected(bat_priv, seq_diff, |
Simon Wunderlich | 81f0268 | 2015-11-23 19:57:22 +0100 | [diff] [blame] | 1348 | BATADV_TQ_LOCAL_WINDOW_SIZE, |
| 1349 | &orig_ifinfo->batman_seqno_reset, NULL)) { |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1350 | ret = BATADV_PROTECTED; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1351 | goto out; |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1352 | } |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1353 | |
| 1354 | rcu_read_lock(); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1355 | hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) { |
| 1356 | neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, |
| 1357 | if_outgoing); |
| 1358 | if (!neigh_ifinfo) |
| 1359 | continue; |
| 1360 | |
| 1361 | neigh_addr = neigh_node->addr; |
| 1362 | is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits, |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1363 | orig_ifinfo->last_real_seqno, |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1364 | seqno); |
| 1365 | |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1366 | if (batadv_compare_eth(neigh_addr, ethhdr->h_source) && |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1367 | neigh_node->if_incoming == if_incoming) { |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1368 | set_mark = 1; |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1369 | if (is_dup) |
| 1370 | ret = BATADV_NEIGH_DUP; |
| 1371 | } else { |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1372 | set_mark = 0; |
Simon Wunderlich | 7c24bbb | 2013-05-23 13:07:42 +0200 | [diff] [blame] | 1373 | if (is_dup && (ret != BATADV_NEIGH_DUP)) |
| 1374 | ret = BATADV_ORIG_DUP; |
| 1375 | } |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1376 | |
| 1377 | /* if the window moved, set the update flag. */ |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1378 | bitmap = neigh_ifinfo->bat_iv.real_bits; |
Antonio Quartulli | 0538f75 | 2013-09-02 12:15:01 +0200 | [diff] [blame] | 1379 | need_update |= batadv_bit_get_packet(bat_priv, bitmap, |
Sven Eckelmann | 0f5f932 | 2012-05-12 02:09:25 +0200 | [diff] [blame] | 1380 | seq_diff, set_mark); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1381 | |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1382 | packet_count = bitmap_weight(bitmap, |
Sven Eckelmann | bbb1f90 | 2012-07-08 17:13:15 +0200 | [diff] [blame] | 1383 | BATADV_TQ_LOCAL_WINDOW_SIZE); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1384 | neigh_ifinfo->bat_iv.real_packet_count = packet_count; |
Sven Eckelmann | 044fa3a | 2016-01-17 11:01:12 +0100 | [diff] [blame] | 1385 | batadv_neigh_ifinfo_put(neigh_ifinfo); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1386 | } |
| 1387 | rcu_read_unlock(); |
| 1388 | |
| 1389 | if (need_update) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1390 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1391 | "%s updating last_seqno: old %u, new %u\n", |
| 1392 | if_outgoing ? if_outgoing->net_dev->name : "DEFAULT", |
| 1393 | orig_ifinfo->last_real_seqno, seqno); |
| 1394 | orig_ifinfo->last_real_seqno = seqno; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1395 | } |
| 1396 | |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1397 | out: |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1398 | spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
Sven Eckelmann | 5d96731 | 2016-01-17 11:01:09 +0100 | [diff] [blame] | 1399 | batadv_orig_node_put(orig_node); |
Sven Eckelmann | 35f9477 | 2016-01-17 11:01:13 +0100 | [diff] [blame] | 1400 | batadv_orig_ifinfo_put(orig_ifinfo); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1401 | return ret; |
| 1402 | } |
| 1403 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1404 | /** |
| 1405 | * batadv_iv_ogm_process_per_outif - process a batman iv OGM for an outgoing if |
| 1406 | * @skb: the skb containing the OGM |
Martin Hundebøll | 95298a9 | 2014-07-15 09:41:05 +0200 | [diff] [blame] | 1407 | * @ogm_offset: offset from skb->data to start of ogm header |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1408 | * @orig_node: the (cached) orig node for the originator of this OGM |
| 1409 | * @if_incoming: the interface where this packet was received |
| 1410 | * @if_outgoing: the interface for which the packet should be considered |
| 1411 | */ |
| 1412 | static void |
| 1413 | batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset, |
| 1414 | struct batadv_orig_node *orig_node, |
| 1415 | struct batadv_hard_iface *if_incoming, |
| 1416 | struct batadv_hard_iface *if_outgoing) |
| 1417 | { |
| 1418 | struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
Marek Lindner | 4ff1e2a | 2015-08-04 21:09:58 +0800 | [diff] [blame] | 1419 | struct batadv_hardif_neigh_node *hardif_neigh = NULL; |
Sven Eckelmann | 4f248cf | 2015-06-09 20:50:49 +0200 | [diff] [blame] | 1420 | struct batadv_neigh_node *router = NULL; |
| 1421 | struct batadv_neigh_node *router_router = NULL; |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1422 | struct batadv_orig_node *orig_neigh_node; |
| 1423 | struct batadv_orig_ifinfo *orig_ifinfo; |
| 1424 | struct batadv_neigh_node *orig_neigh_router = NULL; |
| 1425 | struct batadv_neigh_ifinfo *router_ifinfo = NULL; |
| 1426 | struct batadv_ogm_packet *ogm_packet; |
| 1427 | enum batadv_dup_status dup_status; |
| 1428 | bool is_from_best_next_hop = false; |
| 1429 | bool is_single_hop_neigh = false; |
| 1430 | bool sameseq, similar_ttl; |
| 1431 | struct sk_buff *skb_priv; |
| 1432 | struct ethhdr *ethhdr; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 1433 | u8 *prev_sender; |
Sven Eckelmann | 4b426b1 | 2016-02-22 21:02:39 +0100 | [diff] [blame] | 1434 | bool is_bidirect; |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1435 | |
| 1436 | /* create a private copy of the skb, as some functions change tq value |
| 1437 | * and/or flags. |
| 1438 | */ |
| 1439 | skb_priv = skb_copy(skb, GFP_ATOMIC); |
| 1440 | if (!skb_priv) |
| 1441 | return; |
| 1442 | |
| 1443 | ethhdr = eth_hdr(skb_priv); |
| 1444 | ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset); |
| 1445 | |
| 1446 | dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet, |
| 1447 | if_incoming, if_outgoing); |
| 1448 | if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig)) |
| 1449 | is_single_hop_neigh = true; |
| 1450 | |
| 1451 | if (dup_status == BATADV_PROTECTED) { |
| 1452 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 1453 | "Drop packet: packet within seqno protection time (sender: %pM)\n", |
| 1454 | ethhdr->h_source); |
| 1455 | goto out; |
| 1456 | } |
| 1457 | |
| 1458 | if (ogm_packet->tq == 0) { |
| 1459 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 1460 | "Drop packet: originator packet with tq equal 0\n"); |
| 1461 | goto out; |
| 1462 | } |
| 1463 | |
Marek Lindner | 4ff1e2a | 2015-08-04 21:09:58 +0800 | [diff] [blame] | 1464 | if (is_single_hop_neigh) { |
| 1465 | hardif_neigh = batadv_hardif_neigh_get(if_incoming, |
| 1466 | ethhdr->h_source); |
| 1467 | if (hardif_neigh) |
| 1468 | hardif_neigh->last_seen = jiffies; |
| 1469 | } |
| 1470 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1471 | router = batadv_orig_router_get(orig_node, if_outgoing); |
| 1472 | if (router) { |
| 1473 | router_router = batadv_orig_router_get(router->orig_node, |
| 1474 | if_outgoing); |
| 1475 | router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing); |
| 1476 | } |
| 1477 | |
| 1478 | if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) && |
| 1479 | (batadv_compare_eth(router->addr, ethhdr->h_source))) |
| 1480 | is_from_best_next_hop = true; |
| 1481 | |
| 1482 | prev_sender = ogm_packet->prev_sender; |
| 1483 | /* avoid temporary routing loops */ |
| 1484 | if (router && router_router && |
| 1485 | (batadv_compare_eth(router->addr, prev_sender)) && |
| 1486 | !(batadv_compare_eth(ogm_packet->orig, prev_sender)) && |
| 1487 | (batadv_compare_eth(router->addr, router_router->addr))) { |
| 1488 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 1489 | "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n", |
| 1490 | ethhdr->h_source); |
| 1491 | goto out; |
| 1492 | } |
| 1493 | |
| 1494 | if (if_outgoing == BATADV_IF_DEFAULT) |
| 1495 | batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node); |
| 1496 | |
| 1497 | /* if sender is a direct neighbor the sender mac equals |
| 1498 | * originator mac |
| 1499 | */ |
| 1500 | if (is_single_hop_neigh) |
| 1501 | orig_neigh_node = orig_node; |
| 1502 | else |
| 1503 | orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv, |
| 1504 | ethhdr->h_source); |
| 1505 | |
| 1506 | if (!orig_neigh_node) |
| 1507 | goto out; |
| 1508 | |
| 1509 | /* Update nc_nodes of the originator */ |
| 1510 | batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node, |
| 1511 | ogm_packet, is_single_hop_neigh); |
| 1512 | |
| 1513 | orig_neigh_router = batadv_orig_router_get(orig_neigh_node, |
| 1514 | if_outgoing); |
| 1515 | |
| 1516 | /* drop packet if sender is not a direct neighbor and if we |
| 1517 | * don't route towards it |
| 1518 | */ |
| 1519 | if (!is_single_hop_neigh && (!orig_neigh_router)) { |
| 1520 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 1521 | "Drop packet: OGM via unknown neighbor!\n"); |
| 1522 | goto out_neigh; |
| 1523 | } |
| 1524 | |
| 1525 | is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node, |
| 1526 | ogm_packet, if_incoming, |
| 1527 | if_outgoing); |
| 1528 | |
| 1529 | /* update ranking if it is not a duplicate or has the same |
| 1530 | * seqno and similar ttl as the non-duplicate |
| 1531 | */ |
| 1532 | orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing); |
| 1533 | if (!orig_ifinfo) |
| 1534 | goto out_neigh; |
| 1535 | |
| 1536 | sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno); |
| 1537 | similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl; |
| 1538 | |
| 1539 | if (is_bidirect && ((dup_status == BATADV_NO_DUP) || |
| 1540 | (sameseq && similar_ttl))) { |
| 1541 | batadv_iv_ogm_orig_update(bat_priv, orig_node, |
| 1542 | orig_ifinfo, ethhdr, |
| 1543 | ogm_packet, if_incoming, |
| 1544 | if_outgoing, dup_status); |
| 1545 | } |
Sven Eckelmann | 35f9477 | 2016-01-17 11:01:13 +0100 | [diff] [blame] | 1546 | batadv_orig_ifinfo_put(orig_ifinfo); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1547 | |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 1548 | /* only forward for specific interface, not for the default one. */ |
| 1549 | if (if_outgoing == BATADV_IF_DEFAULT) |
| 1550 | goto out_neigh; |
| 1551 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1552 | /* is single hop (direct) neighbor */ |
| 1553 | if (is_single_hop_neigh) { |
| 1554 | /* OGMs from secondary interfaces should only scheduled once |
| 1555 | * per interface where it has been received, not multiple times |
| 1556 | */ |
| 1557 | if ((ogm_packet->ttl <= 2) && |
| 1558 | (if_incoming != if_outgoing)) { |
| 1559 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 1560 | "Drop packet: OGM from secondary interface and wrong outgoing interface\n"); |
| 1561 | goto out_neigh; |
| 1562 | } |
| 1563 | /* mark direct link on incoming interface */ |
| 1564 | batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet, |
| 1565 | is_single_hop_neigh, |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 1566 | is_from_best_next_hop, if_incoming, |
| 1567 | if_outgoing); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1568 | |
| 1569 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 1570 | "Forwarding packet: rebroadcast neighbor packet with direct link flag\n"); |
| 1571 | goto out_neigh; |
| 1572 | } |
| 1573 | |
| 1574 | /* multihop originator */ |
| 1575 | if (!is_bidirect) { |
| 1576 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 1577 | "Drop packet: not received via bidirectional link\n"); |
| 1578 | goto out_neigh; |
| 1579 | } |
| 1580 | |
| 1581 | if (dup_status == BATADV_NEIGH_DUP) { |
| 1582 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 1583 | "Drop packet: duplicate packet received\n"); |
| 1584 | goto out_neigh; |
| 1585 | } |
| 1586 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1587 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 1588 | "Forwarding packet: rebroadcast originator packet\n"); |
| 1589 | batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet, |
| 1590 | is_single_hop_neigh, is_from_best_next_hop, |
Simon Wunderlich | ef0a937 | 2013-11-13 19:14:49 +0100 | [diff] [blame] | 1591 | if_incoming, if_outgoing); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1592 | |
| 1593 | out_neigh: |
| 1594 | if ((orig_neigh_node) && (!is_single_hop_neigh)) |
Sven Eckelmann | 5d96731 | 2016-01-17 11:01:09 +0100 | [diff] [blame] | 1595 | batadv_orig_node_put(orig_neigh_node); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1596 | out: |
Simon Wunderlich | c1e517f | 2014-03-26 15:46:21 +0100 | [diff] [blame] | 1597 | if (router_ifinfo) |
Sven Eckelmann | 044fa3a | 2016-01-17 11:01:12 +0100 | [diff] [blame] | 1598 | batadv_neigh_ifinfo_put(router_ifinfo); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1599 | if (router) |
Sven Eckelmann | 25bb250 | 2016-01-17 11:01:11 +0100 | [diff] [blame] | 1600 | batadv_neigh_node_put(router); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1601 | if (router_router) |
Sven Eckelmann | 25bb250 | 2016-01-17 11:01:11 +0100 | [diff] [blame] | 1602 | batadv_neigh_node_put(router_router); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1603 | if (orig_neigh_router) |
Sven Eckelmann | 25bb250 | 2016-01-17 11:01:11 +0100 | [diff] [blame] | 1604 | batadv_neigh_node_put(orig_neigh_router); |
Marek Lindner | 4ff1e2a | 2015-08-04 21:09:58 +0800 | [diff] [blame] | 1605 | if (hardif_neigh) |
Sven Eckelmann | accadc3 | 2016-01-17 11:01:14 +0100 | [diff] [blame] | 1606 | batadv_hardif_neigh_put(hardif_neigh); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1607 | |
| 1608 | kfree_skb(skb_priv); |
| 1609 | } |
| 1610 | |
| 1611 | /** |
| 1612 | * batadv_iv_ogm_process - process an incoming batman iv OGM |
| 1613 | * @skb: the skb containing the OGM |
| 1614 | * @ogm_offset: offset to the OGM which should be processed (for aggregates) |
| 1615 | * @if_incoming: the interface where this packet was receved |
| 1616 | */ |
| 1617 | 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] | 1618 | struct batadv_hard_iface *if_incoming) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1619 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1620 | struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1621 | struct batadv_orig_node *orig_neigh_node, *orig_node; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1622 | struct batadv_hard_iface *hard_iface; |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1623 | struct batadv_ogm_packet *ogm_packet; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 1624 | u32 if_incoming_seqno; |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1625 | bool has_directlink_flag; |
| 1626 | struct ethhdr *ethhdr; |
| 1627 | bool is_my_oldorig = false; |
| 1628 | bool is_my_addr = false; |
| 1629 | bool is_my_orig = false; |
| 1630 | |
| 1631 | ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset); |
| 1632 | ethhdr = eth_hdr(skb); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1633 | |
| 1634 | /* Silently drop when the batman packet is actually not a |
| 1635 | * correct packet. |
| 1636 | * |
| 1637 | * This might happen if a packet is padded (e.g. Ethernet has a |
| 1638 | * minimum frame length of 64 byte) and the aggregation interprets |
| 1639 | * it as an additional length. |
| 1640 | * |
| 1641 | * 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] | 1642 | * batadv_ogm_packet to detect whether the packet is the last |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1643 | * packet in an aggregation. Here we expect that the padding |
| 1644 | * is always zero (or not 0x01) |
| 1645 | */ |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1646 | if (ogm_packet->packet_type != BATADV_IV_OGM) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1647 | return; |
| 1648 | |
| 1649 | /* could be changed by schedule_own_packet() */ |
Marek Lindner | 1451151 | 2012-08-02 17:20:26 +0200 | [diff] [blame] | 1650 | if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1651 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1652 | if (ogm_packet->flags & BATADV_DIRECTLINK) |
| 1653 | has_directlink_flag = true; |
Sven Eckelmann | acd34af | 2012-06-03 22:19:21 +0200 | [diff] [blame] | 1654 | else |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1655 | has_directlink_flag = false; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1656 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1657 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Marek Lindner | e1bf0c1 | 2013-04-23 21:40:01 +0800 | [diff] [blame] | 1658 | "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] | 1659 | ethhdr->h_source, if_incoming->net_dev->name, |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1660 | if_incoming->net_dev->dev_addr, ogm_packet->orig, |
| 1661 | ogm_packet->prev_sender, ntohl(ogm_packet->seqno), |
| 1662 | ogm_packet->tq, ogm_packet->ttl, |
| 1663 | ogm_packet->version, has_directlink_flag); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1664 | |
| 1665 | rcu_read_lock(); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 1666 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
Sven Eckelmann | e9a4f29 | 2012-06-03 22:19:19 +0200 | [diff] [blame] | 1667 | if (hard_iface->if_status != BATADV_IF_ACTIVE) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1668 | continue; |
| 1669 | |
| 1670 | if (hard_iface->soft_iface != if_incoming->soft_iface) |
| 1671 | continue; |
| 1672 | |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1673 | if (batadv_compare_eth(ethhdr->h_source, |
| 1674 | hard_iface->net_dev->dev_addr)) |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1675 | is_my_addr = true; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1676 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1677 | if (batadv_compare_eth(ogm_packet->orig, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1678 | hard_iface->net_dev->dev_addr)) |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1679 | is_my_orig = true; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1680 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1681 | if (batadv_compare_eth(ogm_packet->prev_sender, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1682 | hard_iface->net_dev->dev_addr)) |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1683 | is_my_oldorig = true; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1684 | } |
| 1685 | rcu_read_unlock(); |
| 1686 | |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1687 | if (is_my_addr) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1688 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1689 | "Drop packet: received my own broadcast (sender: %pM)\n", |
| 1690 | ethhdr->h_source); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1691 | return; |
| 1692 | } |
| 1693 | |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1694 | if (is_my_orig) { |
| 1695 | unsigned long *word; |
| 1696 | int offset; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 1697 | s32 bit_pos; |
| 1698 | s16 if_num; |
| 1699 | u8 *weight; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1700 | |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1701 | orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv, |
| 1702 | ethhdr->h_source); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1703 | if (!orig_neigh_node) |
| 1704 | return; |
| 1705 | |
| 1706 | /* neighbor has to indicate direct link and it has to |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1707 | * come via the corresponding interface |
| 1708 | * save packet seqno for bidirectional check |
| 1709 | */ |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1710 | if (has_directlink_flag && |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1711 | batadv_compare_eth(if_incoming->net_dev->dev_addr, |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1712 | ogm_packet->orig)) { |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1713 | if_num = if_incoming->if_num; |
| 1714 | offset = if_num * BATADV_NUM_WORDS; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1715 | |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1716 | spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock); |
Antonio Quartulli | 32db6aa | 2014-09-01 14:37:29 +0200 | [diff] [blame] | 1717 | word = &orig_neigh_node->bat_iv.bcast_own[offset]; |
Sven Eckelmann | 9b4a115 | 2012-05-12 13:48:53 +0200 | [diff] [blame] | 1718 | bit_pos = if_incoming_seqno - 2; |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1719 | bit_pos -= ntohl(ogm_packet->seqno); |
Sven Eckelmann | 9b4a115 | 2012-05-12 13:48:53 +0200 | [diff] [blame] | 1720 | batadv_set_bit(word, bit_pos); |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1721 | weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num]; |
Sven Eckelmann | 42d0b04 | 2012-06-03 22:19:17 +0200 | [diff] [blame] | 1722 | *weight = bitmap_weight(word, |
| 1723 | BATADV_TQ_LOCAL_WINDOW_SIZE); |
Antonio Quartulli | bbad0a5 | 2013-09-02 12:15:02 +0200 | [diff] [blame] | 1724 | spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1725 | } |
| 1726 | |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1727 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1728 | "Drop packet: originator packet from myself (via neighbor)\n"); |
Sven Eckelmann | 5d96731 | 2016-01-17 11:01:09 +0100 | [diff] [blame] | 1729 | batadv_orig_node_put(orig_neigh_node); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1730 | return; |
| 1731 | } |
| 1732 | |
| 1733 | if (is_my_oldorig) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1734 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1735 | "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n", |
| 1736 | ethhdr->h_source); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1737 | return; |
| 1738 | } |
| 1739 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1740 | if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 1741 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 1742 | "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n", |
| 1743 | ethhdr->h_source); |
Marek Lindner | 13b2541 | 2012-03-11 06:17:53 +0800 | [diff] [blame] | 1744 | return; |
| 1745 | } |
| 1746 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1747 | orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1748 | if (!orig_node) |
| 1749 | return; |
| 1750 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1751 | batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node, |
| 1752 | if_incoming, BATADV_IF_DEFAULT); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1753 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1754 | rcu_read_lock(); |
| 1755 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
| 1756 | if (hard_iface->if_status != BATADV_IF_ACTIVE) |
| 1757 | continue; |
| 1758 | |
| 1759 | if (hard_iface->soft_iface != bat_priv->soft_iface) |
| 1760 | continue; |
| 1761 | |
Sven Eckelmann | 2735344 | 2016-03-05 16:09:16 +0100 | [diff] [blame] | 1762 | if (!kref_get_unless_zero(&hard_iface->refcount)) |
| 1763 | continue; |
| 1764 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1765 | batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node, |
| 1766 | if_incoming, hard_iface); |
Sven Eckelmann | 2735344 | 2016-03-05 16:09:16 +0100 | [diff] [blame] | 1767 | |
| 1768 | batadv_hardif_put(hard_iface); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1769 | } |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1770 | rcu_read_unlock(); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1771 | |
Sven Eckelmann | 5d96731 | 2016-01-17 11:01:09 +0100 | [diff] [blame] | 1772 | batadv_orig_node_put(orig_node); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1773 | } |
| 1774 | |
Antonio Quartulli | f0d9725 | 2016-05-03 01:45:34 +0800 | [diff] [blame] | 1775 | static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work) |
| 1776 | { |
| 1777 | struct delayed_work *delayed_work; |
| 1778 | struct batadv_forw_packet *forw_packet; |
| 1779 | struct batadv_priv *bat_priv; |
| 1780 | |
| 1781 | delayed_work = to_delayed_work(work); |
| 1782 | forw_packet = container_of(delayed_work, struct batadv_forw_packet, |
| 1783 | delayed_work); |
| 1784 | bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface); |
| 1785 | spin_lock_bh(&bat_priv->forw_bat_list_lock); |
| 1786 | hlist_del(&forw_packet->list); |
| 1787 | spin_unlock_bh(&bat_priv->forw_bat_list_lock); |
| 1788 | |
| 1789 | if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) |
| 1790 | goto out; |
| 1791 | |
| 1792 | batadv_iv_ogm_emit(forw_packet); |
| 1793 | |
| 1794 | /* we have to have at least one packet in the queue to determine the |
| 1795 | * queues wake up time unless we are shutting down. |
| 1796 | * |
| 1797 | * only re-schedule if this is the "original" copy, e.g. the OGM of the |
| 1798 | * primary interface should only be rescheduled once per period, but |
| 1799 | * this function will be called for the forw_packet instances of the |
| 1800 | * other secondary interfaces as well. |
| 1801 | */ |
| 1802 | if (forw_packet->own && |
| 1803 | forw_packet->if_incoming == forw_packet->if_outgoing) |
| 1804 | batadv_iv_ogm_schedule(forw_packet->if_incoming); |
| 1805 | |
| 1806 | out: |
Antonio Quartulli | f0d9725 | 2016-05-03 01:45:34 +0800 | [diff] [blame] | 1807 | batadv_forw_packet_free(forw_packet); |
| 1808 | } |
| 1809 | |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 1810 | static int batadv_iv_ogm_receive(struct sk_buff *skb, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1811 | struct batadv_hard_iface *if_incoming) |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1812 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 1813 | struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1814 | struct batadv_ogm_packet *ogm_packet; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 1815 | u8 *packet_pos; |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1816 | int ogm_offset; |
Marek Lindner | ef26157 | 2013-04-23 21:39:57 +0800 | [diff] [blame] | 1817 | bool ret; |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 1818 | |
Sven Eckelmann | 7e071c7 | 2012-06-03 22:19:13 +0200 | [diff] [blame] | 1819 | ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN); |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 1820 | if (!ret) |
| 1821 | return NET_RX_DROP; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1822 | |
Marek Lindner | edbf12b | 2012-03-11 06:17:49 +0800 | [diff] [blame] | 1823 | /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface |
| 1824 | * that does not have B.A.T.M.A.N. IV enabled ? |
| 1825 | */ |
Antonio Quartulli | 29824a5 | 2016-05-25 23:27:31 +0800 | [diff] [blame] | 1826 | if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable) |
Marek Lindner | edbf12b | 2012-03-11 06:17:49 +0800 | [diff] [blame] | 1827 | return NET_RX_DROP; |
| 1828 | |
Sven Eckelmann | d69909d | 2012-06-03 22:19:20 +0200 | [diff] [blame] | 1829 | batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX); |
| 1830 | batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES, |
Martin Hundebøll | f821486 | 2012-04-20 17:02:45 +0200 | [diff] [blame] | 1831 | skb->len + ETH_HLEN); |
| 1832 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1833 | ogm_offset = 0; |
| 1834 | ogm_packet = (struct batadv_ogm_packet *)skb->data; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1835 | |
| 1836 | /* unpack the aggregated packets and process them one by one */ |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1837 | while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb), |
| 1838 | ogm_packet->tvlv_len)) { |
| 1839 | batadv_iv_ogm_process(skb, ogm_offset, if_incoming); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1840 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1841 | ogm_offset += BATADV_OGM_HLEN; |
| 1842 | ogm_offset += ntohs(ogm_packet->tvlv_len); |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1843 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1844 | packet_pos = skb->data + ogm_offset; |
| 1845 | ogm_packet = (struct batadv_ogm_packet *)packet_pos; |
Marek Lindner | b47506d | 2013-03-04 10:39:49 +0800 | [diff] [blame] | 1846 | } |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 1847 | |
| 1848 | kfree_skb(skb); |
| 1849 | return NET_RX_SUCCESS; |
Marek Lindner | fc95727 | 2011-07-30 12:04:12 +0200 | [diff] [blame] | 1850 | } |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 1851 | |
Simon Wunderlich | 1b371d1 | 2014-01-15 21:17:54 +0100 | [diff] [blame] | 1852 | /** |
| 1853 | * batadv_iv_ogm_orig_print_neigh - print neighbors for the originator table |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1854 | * @orig_node: the orig_node for which the neighbors are printed |
| 1855 | * @if_outgoing: outgoing interface for these entries |
| 1856 | * @seq: debugfs table seq_file struct |
| 1857 | * |
| 1858 | * Must be called while holding an rcu lock. |
| 1859 | */ |
| 1860 | static void |
| 1861 | batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node, |
| 1862 | struct batadv_hard_iface *if_outgoing, |
| 1863 | struct seq_file *seq) |
| 1864 | { |
| 1865 | struct batadv_neigh_node *neigh_node; |
| 1866 | struct batadv_neigh_ifinfo *n_ifinfo; |
| 1867 | |
| 1868 | hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) { |
| 1869 | n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing); |
| 1870 | if (!n_ifinfo) |
| 1871 | continue; |
| 1872 | |
| 1873 | seq_printf(seq, " %pM (%3i)", |
| 1874 | neigh_node->addr, |
| 1875 | n_ifinfo->bat_iv.tq_avg); |
| 1876 | |
Sven Eckelmann | 044fa3a | 2016-01-17 11:01:12 +0100 | [diff] [blame] | 1877 | batadv_neigh_ifinfo_put(n_ifinfo); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1878 | } |
| 1879 | } |
| 1880 | |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1881 | /** |
| 1882 | * batadv_iv_ogm_orig_print - print the originator table |
| 1883 | * @bat_priv: the bat priv with all the soft interface information |
| 1884 | * @seq: debugfs table seq_file struct |
Simon Wunderlich | cb1c92e | 2013-11-21 11:52:16 +0100 | [diff] [blame] | 1885 | * @if_outgoing: the outgoing interface for which this should be printed |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1886 | */ |
| 1887 | static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv, |
Simon Wunderlich | cb1c92e | 2013-11-21 11:52:16 +0100 | [diff] [blame] | 1888 | struct seq_file *seq, |
| 1889 | struct batadv_hard_iface *if_outgoing) |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1890 | { |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1891 | struct batadv_neigh_node *neigh_node; |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1892 | struct batadv_hashtable *hash = bat_priv->orig_hash; |
| 1893 | int last_seen_msecs, last_seen_secs; |
| 1894 | struct batadv_orig_node *orig_node; |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1895 | struct batadv_neigh_ifinfo *n_ifinfo; |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1896 | unsigned long last_seen_jiffies; |
| 1897 | struct hlist_head *head; |
| 1898 | int batman_count = 0; |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 1899 | u32 i; |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1900 | |
Antonio Quartulli | 925a6f3 | 2016-03-12 10:30:18 +0100 | [diff] [blame] | 1901 | seq_puts(seq, |
| 1902 | " Originator last-seen (#/255) Nexthop [outgoingIF]: Potential nexthops ...\n"); |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1903 | |
| 1904 | for (i = 0; i < hash->size; i++) { |
| 1905 | head = &hash->table[i]; |
| 1906 | |
| 1907 | rcu_read_lock(); |
| 1908 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 1909 | neigh_node = batadv_orig_router_get(orig_node, |
Simon Wunderlich | cb1c92e | 2013-11-21 11:52:16 +0100 | [diff] [blame] | 1910 | if_outgoing); |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1911 | if (!neigh_node) |
| 1912 | continue; |
| 1913 | |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1914 | n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, |
Simon Wunderlich | cb1c92e | 2013-11-21 11:52:16 +0100 | [diff] [blame] | 1915 | if_outgoing); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1916 | if (!n_ifinfo) |
| 1917 | goto next; |
| 1918 | |
| 1919 | if (n_ifinfo->bat_iv.tq_avg == 0) |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1920 | goto next; |
| 1921 | |
| 1922 | last_seen_jiffies = jiffies - orig_node->last_seen; |
| 1923 | last_seen_msecs = jiffies_to_msecs(last_seen_jiffies); |
| 1924 | last_seen_secs = last_seen_msecs / 1000; |
| 1925 | last_seen_msecs = last_seen_msecs % 1000; |
| 1926 | |
| 1927 | seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:", |
| 1928 | orig_node->orig, last_seen_secs, |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1929 | last_seen_msecs, n_ifinfo->bat_iv.tq_avg, |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1930 | neigh_node->addr, |
| 1931 | neigh_node->if_incoming->net_dev->name); |
| 1932 | |
Simon Wunderlich | cb1c92e | 2013-11-21 11:52:16 +0100 | [diff] [blame] | 1933 | batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing, |
| 1934 | seq); |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1935 | seq_puts(seq, "\n"); |
| 1936 | batman_count++; |
| 1937 | |
| 1938 | next: |
Sven Eckelmann | 25bb250 | 2016-01-17 11:01:11 +0100 | [diff] [blame] | 1939 | batadv_neigh_node_put(neigh_node); |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 1940 | if (n_ifinfo) |
Sven Eckelmann | 044fa3a | 2016-01-17 11:01:12 +0100 | [diff] [blame] | 1941 | batadv_neigh_ifinfo_put(n_ifinfo); |
Antonio Quartulli | 737a2a22 | 2013-09-02 12:15:03 +0200 | [diff] [blame] | 1942 | } |
| 1943 | rcu_read_unlock(); |
| 1944 | } |
| 1945 | |
| 1946 | if (batman_count == 0) |
| 1947 | seq_puts(seq, "No batman nodes in range ...\n"); |
| 1948 | } |
| 1949 | |
Antonio Quartulli | a3285a8 | 2013-09-02 12:15:04 +0200 | [diff] [blame] | 1950 | /** |
Marek Lindner | 7587405 | 2015-08-04 21:09:57 +0800 | [diff] [blame] | 1951 | * batadv_iv_hardif_neigh_print - print a single hop neighbour node |
| 1952 | * @seq: neighbour table seq_file struct |
| 1953 | * @hardif_neigh: hardif neighbour information |
| 1954 | */ |
| 1955 | static void |
| 1956 | batadv_iv_hardif_neigh_print(struct seq_file *seq, |
| 1957 | struct batadv_hardif_neigh_node *hardif_neigh) |
| 1958 | { |
| 1959 | int last_secs, last_msecs; |
| 1960 | |
| 1961 | last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000; |
| 1962 | last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000; |
| 1963 | |
| 1964 | seq_printf(seq, " %10s %pM %4i.%03is\n", |
| 1965 | hardif_neigh->if_incoming->net_dev->name, |
| 1966 | hardif_neigh->addr, last_secs, last_msecs); |
| 1967 | } |
| 1968 | |
| 1969 | /** |
| 1970 | * batadv_iv_ogm_neigh_print - print the single hop neighbour list |
| 1971 | * @bat_priv: the bat priv with all the soft interface information |
| 1972 | * @seq: neighbour table seq_file struct |
| 1973 | */ |
| 1974 | static void batadv_iv_neigh_print(struct batadv_priv *bat_priv, |
| 1975 | struct seq_file *seq) |
| 1976 | { |
| 1977 | struct net_device *net_dev = (struct net_device *)seq->private; |
| 1978 | struct batadv_hardif_neigh_node *hardif_neigh; |
| 1979 | struct batadv_hard_iface *hard_iface; |
| 1980 | int batman_count = 0; |
| 1981 | |
Antonio Quartulli | 925a6f3 | 2016-03-12 10:30:18 +0100 | [diff] [blame] | 1982 | seq_puts(seq, " IF Neighbor last-seen\n"); |
Marek Lindner | 7587405 | 2015-08-04 21:09:57 +0800 | [diff] [blame] | 1983 | |
| 1984 | rcu_read_lock(); |
| 1985 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
| 1986 | if (hard_iface->soft_iface != net_dev) |
| 1987 | continue; |
| 1988 | |
| 1989 | hlist_for_each_entry_rcu(hardif_neigh, |
| 1990 | &hard_iface->neigh_list, list) { |
| 1991 | batadv_iv_hardif_neigh_print(seq, hardif_neigh); |
| 1992 | batman_count++; |
| 1993 | } |
| 1994 | } |
| 1995 | rcu_read_unlock(); |
| 1996 | |
| 1997 | if (batman_count == 0) |
| 1998 | seq_puts(seq, "No batman nodes in range ...\n"); |
| 1999 | } |
| 2000 | |
| 2001 | /** |
Markus Pargmann | 57b1250 | 2016-07-03 11:07:14 +0200 | [diff] [blame] | 2002 | * batadv_iv_ogm_neigh_diff - calculate tq difference of two neighbors |
| 2003 | * @neigh1: the first neighbor object of the comparison |
| 2004 | * @if_outgoing1: outgoing interface for the first neighbor |
| 2005 | * @neigh2: the second neighbor object of the comparison |
| 2006 | * @if_outgoing2: outgoing interface for the second neighbor |
| 2007 | * @diff: pointer to integer receiving the calculated difference |
| 2008 | * |
| 2009 | * The content of *@diff is only valid when this function returns true. |
| 2010 | * It is less, equal to or greater than 0 if the metric via neigh1 is lower, |
| 2011 | * the same as or higher than the metric via neigh2 |
| 2012 | * |
| 2013 | * Return: true when the difference could be calculated, false otherwise |
| 2014 | */ |
| 2015 | static bool batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1, |
| 2016 | struct batadv_hard_iface *if_outgoing1, |
| 2017 | struct batadv_neigh_node *neigh2, |
| 2018 | struct batadv_hard_iface *if_outgoing2, |
| 2019 | int *diff) |
| 2020 | { |
| 2021 | struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo; |
| 2022 | u8 tq1, tq2; |
| 2023 | bool ret = true; |
| 2024 | |
| 2025 | neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1); |
| 2026 | neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2); |
| 2027 | |
| 2028 | if (!neigh1_ifinfo || !neigh2_ifinfo) { |
| 2029 | ret = false; |
| 2030 | goto out; |
| 2031 | } |
| 2032 | |
| 2033 | tq1 = neigh1_ifinfo->bat_iv.tq_avg; |
| 2034 | tq2 = neigh2_ifinfo->bat_iv.tq_avg; |
| 2035 | *diff = (int)tq1 - (int)tq2; |
| 2036 | |
| 2037 | out: |
| 2038 | if (neigh1_ifinfo) |
| 2039 | batadv_neigh_ifinfo_put(neigh1_ifinfo); |
| 2040 | if (neigh2_ifinfo) |
| 2041 | batadv_neigh_ifinfo_put(neigh2_ifinfo); |
| 2042 | |
| 2043 | return ret; |
| 2044 | } |
| 2045 | |
| 2046 | /** |
Antonio Quartulli | a3285a8 | 2013-09-02 12:15:04 +0200 | [diff] [blame] | 2047 | * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors |
| 2048 | * @neigh1: the first neighbor object of the comparison |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 2049 | * @if_outgoing1: outgoing interface for the first neighbor |
Antonio Quartulli | a3285a8 | 2013-09-02 12:15:04 +0200 | [diff] [blame] | 2050 | * @neigh2: the second neighbor object of the comparison |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 2051 | * @if_outgoing2: outgoing interface for the second neighbor |
Antonio Quartulli | a3285a8 | 2013-09-02 12:15:04 +0200 | [diff] [blame] | 2052 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 2053 | * Return: a value less, equal to or greater than 0 if the metric via neigh1 is |
Antonio Quartulli | a3285a8 | 2013-09-02 12:15:04 +0200 | [diff] [blame] | 2054 | * lower, the same as or higher than the metric via neigh2 |
| 2055 | */ |
| 2056 | static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1, |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 2057 | struct batadv_hard_iface *if_outgoing1, |
| 2058 | struct batadv_neigh_node *neigh2, |
| 2059 | struct batadv_hard_iface *if_outgoing2) |
Antonio Quartulli | a3285a8 | 2013-09-02 12:15:04 +0200 | [diff] [blame] | 2060 | { |
Markus Pargmann | 57b1250 | 2016-07-03 11:07:14 +0200 | [diff] [blame] | 2061 | bool ret; |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 2062 | int diff; |
Antonio Quartulli | a3285a8 | 2013-09-02 12:15:04 +0200 | [diff] [blame] | 2063 | |
Markus Pargmann | 57b1250 | 2016-07-03 11:07:14 +0200 | [diff] [blame] | 2064 | ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2, |
| 2065 | if_outgoing2, &diff); |
| 2066 | if (!ret) |
| 2067 | return 0; |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 2068 | |
| 2069 | return diff; |
Antonio Quartulli | a3285a8 | 2013-09-02 12:15:04 +0200 | [diff] [blame] | 2070 | } |
| 2071 | |
Antonio Quartulli | c43c981 | 2013-09-02 12:15:05 +0200 | [diff] [blame] | 2072 | /** |
Simon Wunderlich | 18165f6 | 2015-08-08 02:01:50 +0200 | [diff] [blame] | 2073 | * batadv_iv_ogm_neigh_is_sob - check if neigh1 is similarly good or better |
| 2074 | * than neigh2 from the metric prospective |
Antonio Quartulli | c43c981 | 2013-09-02 12:15:05 +0200 | [diff] [blame] | 2075 | * @neigh1: the first neighbor object of the comparison |
Martin Hundebøll | 95298a9 | 2014-07-15 09:41:05 +0200 | [diff] [blame] | 2076 | * @if_outgoing1: outgoing interface for the first neighbor |
Antonio Quartulli | c43c981 | 2013-09-02 12:15:05 +0200 | [diff] [blame] | 2077 | * @neigh2: the second neighbor object of the comparison |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 2078 | * @if_outgoing2: outgoing interface for the second neighbor |
Martin Hundebøll | 95298a9 | 2014-07-15 09:41:05 +0200 | [diff] [blame] | 2079 | * |
Sven Eckelmann | 62fe710 | 2015-09-15 19:00:48 +0200 | [diff] [blame] | 2080 | * Return: true if the metric via neigh1 is equally good or better than |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 2081 | * the metric via neigh2, false otherwise. |
| 2082 | */ |
| 2083 | static bool |
Simon Wunderlich | 18165f6 | 2015-08-08 02:01:50 +0200 | [diff] [blame] | 2084 | batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1, |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 2085 | struct batadv_hard_iface *if_outgoing1, |
| 2086 | struct batadv_neigh_node *neigh2, |
| 2087 | struct batadv_hard_iface *if_outgoing2) |
| 2088 | { |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 2089 | bool ret; |
Markus Pargmann | 57b1250 | 2016-07-03 11:07:14 +0200 | [diff] [blame] | 2090 | int diff; |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 2091 | |
Markus Pargmann | 57b1250 | 2016-07-03 11:07:14 +0200 | [diff] [blame] | 2092 | ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2, |
| 2093 | if_outgoing2, &diff); |
| 2094 | if (!ret) |
| 2095 | return false; |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 2096 | |
Markus Pargmann | 57b1250 | 2016-07-03 11:07:14 +0200 | [diff] [blame] | 2097 | ret = diff > -BATADV_TQ_SIMILARITY_THRESHOLD; |
Simon Wunderlich | 8965233 | 2013-11-13 19:14:46 +0100 | [diff] [blame] | 2098 | return ret; |
Antonio Quartulli | c43c981 | 2013-09-02 12:15:05 +0200 | [diff] [blame] | 2099 | } |
| 2100 | |
Antonio Quartulli | f0d9725 | 2016-05-03 01:45:34 +0800 | [diff] [blame] | 2101 | static void batadv_iv_iface_activate(struct batadv_hard_iface *hard_iface) |
| 2102 | { |
| 2103 | /* begin scheduling originator messages on that interface */ |
| 2104 | batadv_iv_ogm_schedule(hard_iface); |
| 2105 | } |
| 2106 | |
Antonio Quartulli | 34d99cf | 2016-07-03 12:46:33 +0200 | [diff] [blame] | 2107 | static struct batadv_gw_node * |
| 2108 | batadv_iv_gw_get_best_gw_node(struct batadv_priv *bat_priv) |
| 2109 | { |
| 2110 | struct batadv_neigh_node *router; |
| 2111 | struct batadv_neigh_ifinfo *router_ifinfo; |
| 2112 | struct batadv_gw_node *gw_node, *curr_gw = NULL; |
| 2113 | u64 max_gw_factor = 0; |
| 2114 | u64 tmp_gw_factor = 0; |
| 2115 | u8 max_tq = 0; |
| 2116 | u8 tq_avg; |
| 2117 | struct batadv_orig_node *orig_node; |
| 2118 | |
| 2119 | rcu_read_lock(); |
| 2120 | hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) { |
| 2121 | orig_node = gw_node->orig_node; |
| 2122 | router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT); |
| 2123 | if (!router) |
| 2124 | continue; |
| 2125 | |
| 2126 | router_ifinfo = batadv_neigh_ifinfo_get(router, |
| 2127 | BATADV_IF_DEFAULT); |
| 2128 | if (!router_ifinfo) |
| 2129 | goto next; |
| 2130 | |
| 2131 | if (!kref_get_unless_zero(&gw_node->refcount)) |
| 2132 | goto next; |
| 2133 | |
| 2134 | tq_avg = router_ifinfo->bat_iv.tq_avg; |
| 2135 | |
| 2136 | switch (atomic_read(&bat_priv->gw.sel_class)) { |
| 2137 | case 1: /* fast connection */ |
| 2138 | tmp_gw_factor = tq_avg * tq_avg; |
| 2139 | tmp_gw_factor *= gw_node->bandwidth_down; |
| 2140 | tmp_gw_factor *= 100 * 100; |
| 2141 | tmp_gw_factor >>= 18; |
| 2142 | |
| 2143 | if ((tmp_gw_factor > max_gw_factor) || |
| 2144 | ((tmp_gw_factor == max_gw_factor) && |
| 2145 | (tq_avg > max_tq))) { |
| 2146 | if (curr_gw) |
| 2147 | batadv_gw_node_put(curr_gw); |
| 2148 | curr_gw = gw_node; |
| 2149 | kref_get(&curr_gw->refcount); |
| 2150 | } |
| 2151 | break; |
| 2152 | |
| 2153 | default: /* 2: stable connection (use best statistic) |
| 2154 | * 3: fast-switch (use best statistic but change as |
| 2155 | * soon as a better gateway appears) |
| 2156 | * XX: late-switch (use best statistic but change as |
| 2157 | * soon as a better gateway appears which has |
| 2158 | * $routing_class more tq points) |
| 2159 | */ |
| 2160 | if (tq_avg > max_tq) { |
| 2161 | if (curr_gw) |
| 2162 | batadv_gw_node_put(curr_gw); |
| 2163 | curr_gw = gw_node; |
| 2164 | kref_get(&curr_gw->refcount); |
| 2165 | } |
| 2166 | break; |
| 2167 | } |
| 2168 | |
| 2169 | if (tq_avg > max_tq) |
| 2170 | max_tq = tq_avg; |
| 2171 | |
| 2172 | if (tmp_gw_factor > max_gw_factor) |
| 2173 | max_gw_factor = tmp_gw_factor; |
| 2174 | |
| 2175 | batadv_gw_node_put(gw_node); |
| 2176 | |
| 2177 | next: |
| 2178 | batadv_neigh_node_put(router); |
| 2179 | if (router_ifinfo) |
| 2180 | batadv_neigh_ifinfo_put(router_ifinfo); |
| 2181 | } |
| 2182 | rcu_read_unlock(); |
| 2183 | |
| 2184 | return curr_gw; |
| 2185 | } |
| 2186 | |
| 2187 | static bool batadv_iv_gw_is_eligible(struct batadv_priv *bat_priv, |
| 2188 | struct batadv_orig_node *curr_gw_orig, |
| 2189 | struct batadv_orig_node *orig_node) |
| 2190 | { |
| 2191 | struct batadv_neigh_ifinfo *router_orig_ifinfo = NULL; |
| 2192 | struct batadv_neigh_ifinfo *router_gw_ifinfo = NULL; |
| 2193 | struct batadv_neigh_node *router_gw = NULL; |
| 2194 | struct batadv_neigh_node *router_orig = NULL; |
| 2195 | u8 gw_tq_avg, orig_tq_avg; |
| 2196 | bool ret = false; |
| 2197 | |
| 2198 | /* dynamic re-election is performed only on fast or late switch */ |
| 2199 | if (atomic_read(&bat_priv->gw.sel_class) <= 2) |
| 2200 | return false; |
| 2201 | |
| 2202 | router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT); |
| 2203 | if (!router_gw) { |
| 2204 | ret = true; |
| 2205 | goto out; |
| 2206 | } |
| 2207 | |
| 2208 | router_gw_ifinfo = batadv_neigh_ifinfo_get(router_gw, |
| 2209 | BATADV_IF_DEFAULT); |
| 2210 | if (!router_gw_ifinfo) { |
| 2211 | ret = true; |
| 2212 | goto out; |
| 2213 | } |
| 2214 | |
| 2215 | router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT); |
| 2216 | if (!router_orig) |
| 2217 | goto out; |
| 2218 | |
| 2219 | router_orig_ifinfo = batadv_neigh_ifinfo_get(router_orig, |
| 2220 | BATADV_IF_DEFAULT); |
| 2221 | if (!router_orig_ifinfo) |
| 2222 | goto out; |
| 2223 | |
| 2224 | gw_tq_avg = router_gw_ifinfo->bat_iv.tq_avg; |
| 2225 | orig_tq_avg = router_orig_ifinfo->bat_iv.tq_avg; |
| 2226 | |
| 2227 | /* the TQ value has to be better */ |
| 2228 | if (orig_tq_avg < gw_tq_avg) |
| 2229 | goto out; |
| 2230 | |
| 2231 | /* if the routing class is greater than 3 the value tells us how much |
| 2232 | * greater the TQ value of the new gateway must be |
| 2233 | */ |
| 2234 | if ((atomic_read(&bat_priv->gw.sel_class) > 3) && |
| 2235 | (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw.sel_class))) |
| 2236 | goto out; |
| 2237 | |
| 2238 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 2239 | "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n", |
| 2240 | gw_tq_avg, orig_tq_avg); |
| 2241 | |
| 2242 | ret = true; |
| 2243 | out: |
| 2244 | if (router_gw_ifinfo) |
| 2245 | batadv_neigh_ifinfo_put(router_gw_ifinfo); |
| 2246 | if (router_orig_ifinfo) |
| 2247 | batadv_neigh_ifinfo_put(router_orig_ifinfo); |
| 2248 | if (router_gw) |
| 2249 | batadv_neigh_node_put(router_gw); |
| 2250 | if (router_orig) |
| 2251 | batadv_neigh_node_put(router_orig); |
| 2252 | |
| 2253 | return ret; |
| 2254 | } |
| 2255 | |
| 2256 | /* fails if orig_node has no router */ |
| 2257 | static int batadv_iv_gw_write_buffer_text(struct batadv_priv *bat_priv, |
| 2258 | struct seq_file *seq, |
| 2259 | const struct batadv_gw_node *gw_node) |
| 2260 | { |
| 2261 | struct batadv_gw_node *curr_gw; |
| 2262 | struct batadv_neigh_node *router; |
| 2263 | struct batadv_neigh_ifinfo *router_ifinfo = NULL; |
| 2264 | int ret = -1; |
| 2265 | |
| 2266 | router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT); |
| 2267 | if (!router) |
| 2268 | goto out; |
| 2269 | |
| 2270 | router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT); |
| 2271 | if (!router_ifinfo) |
| 2272 | goto out; |
| 2273 | |
| 2274 | curr_gw = batadv_gw_get_selected_gw_node(bat_priv); |
| 2275 | |
| 2276 | seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n", |
| 2277 | (curr_gw == gw_node ? "=>" : " "), |
| 2278 | gw_node->orig_node->orig, |
| 2279 | router_ifinfo->bat_iv.tq_avg, router->addr, |
| 2280 | router->if_incoming->net_dev->name, |
| 2281 | gw_node->bandwidth_down / 10, |
| 2282 | gw_node->bandwidth_down % 10, |
| 2283 | gw_node->bandwidth_up / 10, |
| 2284 | gw_node->bandwidth_up % 10); |
| 2285 | ret = seq_has_overflowed(seq) ? -1 : 0; |
| 2286 | |
| 2287 | if (curr_gw) |
| 2288 | batadv_gw_node_put(curr_gw); |
| 2289 | out: |
| 2290 | if (router_ifinfo) |
| 2291 | batadv_neigh_ifinfo_put(router_ifinfo); |
| 2292 | if (router) |
| 2293 | batadv_neigh_node_put(router); |
| 2294 | return ret; |
| 2295 | } |
| 2296 | |
| 2297 | static void batadv_iv_gw_print(struct batadv_priv *bat_priv, |
| 2298 | struct seq_file *seq) |
| 2299 | { |
| 2300 | struct batadv_gw_node *gw_node; |
| 2301 | int gw_count = 0; |
| 2302 | |
| 2303 | seq_puts(seq, |
| 2304 | " Gateway (#/255) Nexthop [outgoingIF]: advertised uplink bandwidth\n"); |
| 2305 | |
| 2306 | rcu_read_lock(); |
| 2307 | hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) { |
| 2308 | /* fails if orig_node has no router */ |
| 2309 | if (batadv_iv_gw_write_buffer_text(bat_priv, seq, gw_node) < 0) |
| 2310 | continue; |
| 2311 | |
| 2312 | gw_count++; |
| 2313 | } |
| 2314 | rcu_read_unlock(); |
| 2315 | |
| 2316 | if (gw_count == 0) |
| 2317 | seq_puts(seq, "No gateways in range ...\n"); |
| 2318 | } |
| 2319 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 2320 | static struct batadv_algo_ops batadv_batman_iv __read_mostly = { |
Marek Lindner | 519d349 | 2012-04-18 17:15:57 +0800 | [diff] [blame] | 2321 | .name = "BATMAN_IV", |
Antonio Quartulli | 29824a5 | 2016-05-25 23:27:31 +0800 | [diff] [blame] | 2322 | .iface = { |
| 2323 | .activate = batadv_iv_iface_activate, |
| 2324 | .enable = batadv_iv_ogm_iface_enable, |
| 2325 | .disable = batadv_iv_ogm_iface_disable, |
| 2326 | .update_mac = batadv_iv_ogm_iface_update_mac, |
| 2327 | .primary_set = batadv_iv_ogm_primary_iface_set, |
| 2328 | }, |
| 2329 | .neigh = { |
| 2330 | .cmp = batadv_iv_ogm_neigh_cmp, |
| 2331 | .is_similar_or_better = batadv_iv_ogm_neigh_is_sob, |
| 2332 | .print = batadv_iv_neigh_print, |
| 2333 | }, |
| 2334 | .orig = { |
| 2335 | .print = batadv_iv_ogm_orig_print, |
| 2336 | .free = batadv_iv_ogm_orig_free, |
| 2337 | .add_if = batadv_iv_ogm_orig_add_if, |
| 2338 | .del_if = batadv_iv_ogm_orig_del_if, |
| 2339 | }, |
Antonio Quartulli | 34d99cf | 2016-07-03 12:46:33 +0200 | [diff] [blame] | 2340 | .gw = { |
| 2341 | .get_best_gw_node = batadv_iv_gw_get_best_gw_node, |
| 2342 | .is_eligible = batadv_iv_gw_is_eligible, |
| 2343 | .print = batadv_iv_gw_print, |
| 2344 | }, |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 2345 | }; |
| 2346 | |
Sven Eckelmann | 81c524f | 2012-05-12 02:09:22 +0200 | [diff] [blame] | 2347 | int __init batadv_iv_init(void) |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 2348 | { |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 2349 | int ret; |
| 2350 | |
| 2351 | /* batman originator packet */ |
Sven Eckelmann | acd34af | 2012-06-03 22:19:21 +0200 | [diff] [blame] | 2352 | ret = batadv_recv_handler_register(BATADV_IV_OGM, |
| 2353 | batadv_iv_ogm_receive); |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 2354 | if (ret < 0) |
| 2355 | goto out; |
| 2356 | |
Sven Eckelmann | fe8bc39 | 2012-05-12 18:33:51 +0200 | [diff] [blame] | 2357 | ret = batadv_algo_register(&batadv_batman_iv); |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 2358 | if (ret < 0) |
| 2359 | goto handler_unregister; |
| 2360 | |
| 2361 | goto out; |
| 2362 | |
| 2363 | handler_unregister: |
Sven Eckelmann | acd34af | 2012-06-03 22:19:21 +0200 | [diff] [blame] | 2364 | batadv_recv_handler_unregister(BATADV_IV_OGM); |
Marek Lindner | c3e2931 | 2012-03-04 16:56:25 +0800 | [diff] [blame] | 2365 | out: |
| 2366 | return ret; |
Marek Lindner | 1c28047 | 2011-11-28 17:40:17 +0800 | [diff] [blame] | 2367 | } |