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