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