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