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