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