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