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