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