Merge tag 'wireless-drivers-next-for-davem-2016-01-09' of git://git.kernel.org/pub...
[deliverable/linux.git] / net / batman-adv / bat_iv_ogm.c
1 /* Copyright (C) 2007-2015 B.A.T.M.A.N. contributors:
2 *
3 * Marek Lindner, Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "bat_algo.h"
19 #include "main.h"
20
21 #include <linux/atomic.h>
22 #include <linux/bitmap.h>
23 #include <linux/bitops.h>
24 #include <linux/bug.h>
25 #include <linux/byteorder/generic.h>
26 #include <linux/cache.h>
27 #include <linux/errno.h>
28 #include <linux/etherdevice.h>
29 #include <linux/fs.h>
30 #include <linux/if_ether.h>
31 #include <linux/init.h>
32 #include <linux/jiffies.h>
33 #include <linux/list.h>
34 #include <linux/netdevice.h>
35 #include <linux/pkt_sched.h>
36 #include <linux/printk.h>
37 #include <linux/random.h>
38 #include <linux/rculist.h>
39 #include <linux/rcupdate.h>
40 #include <linux/seq_file.h>
41 #include <linux/skbuff.h>
42 #include <linux/slab.h>
43 #include <linux/spinlock.h>
44 #include <linux/stddef.h>
45 #include <linux/string.h>
46 #include <linux/types.h>
47 #include <linux/workqueue.h>
48
49 #include "bitarray.h"
50 #include "hard-interface.h"
51 #include "hash.h"
52 #include "network-coding.h"
53 #include "originator.h"
54 #include "packet.h"
55 #include "routing.h"
56 #include "send.h"
57 #include "translation-table.h"
58
59 /**
60 * enum batadv_dup_status - duplicate status
61 * @BATADV_NO_DUP: the packet is no duplicate
62 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
63 * neighbor)
64 * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
65 * @BATADV_PROTECTED: originator is currently protected (after reboot)
66 */
67 enum batadv_dup_status {
68 BATADV_NO_DUP = 0,
69 BATADV_ORIG_DUP,
70 BATADV_NEIGH_DUP,
71 BATADV_PROTECTED,
72 };
73
74 /**
75 * batadv_ring_buffer_set - update the ring buffer with the given value
76 * @lq_recv: pointer to the ring buffer
77 * @lq_index: index to store the value at
78 * @value: value to store in the ring buffer
79 */
80 static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value)
81 {
82 lq_recv[*lq_index] = value;
83 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
84 }
85
86 /**
87 * batadv_ring_buffer_avg - compute the average of all non-zero values stored
88 * in the given ring buffer
89 * @lq_recv: pointer to the ring buffer
90 *
91 * Returns computed average value.
92 */
93 static u8 batadv_ring_buffer_avg(const u8 lq_recv[])
94 {
95 const u8 *ptr;
96 u16 count = 0;
97 u16 i = 0;
98 u16 sum = 0;
99
100 ptr = lq_recv;
101
102 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
103 if (*ptr != 0) {
104 count++;
105 sum += *ptr;
106 }
107
108 i++;
109 ptr++;
110 }
111
112 if (count == 0)
113 return 0;
114
115 return (u8)(sum / count);
116 }
117
118 /**
119 * batadv_iv_ogm_orig_free - free the private resources allocated for this
120 * orig_node
121 * @orig_node: the orig_node for which the resources have to be free'd
122 */
123 static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node)
124 {
125 kfree(orig_node->bat_iv.bcast_own);
126 kfree(orig_node->bat_iv.bcast_own_sum);
127 }
128
129 /**
130 * batadv_iv_ogm_orig_add_if - change the private structures of the orig_node to
131 * include the new hard-interface
132 * @orig_node: the orig_node that has to be changed
133 * @max_if_num: the current amount of interfaces
134 *
135 * Returns 0 on success, a negative error code otherwise.
136 */
137 static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node,
138 int max_if_num)
139 {
140 void *data_ptr;
141 size_t old_size;
142 int ret = -ENOMEM;
143
144 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
145
146 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
147 data_ptr = kmalloc_array(max_if_num,
148 BATADV_NUM_WORDS * sizeof(unsigned long),
149 GFP_ATOMIC);
150 if (!data_ptr)
151 goto unlock;
152
153 memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size);
154 kfree(orig_node->bat_iv.bcast_own);
155 orig_node->bat_iv.bcast_own = data_ptr;
156
157 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
158 if (!data_ptr) {
159 kfree(orig_node->bat_iv.bcast_own);
160 goto unlock;
161 }
162
163 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
164 (max_if_num - 1) * sizeof(u8));
165 kfree(orig_node->bat_iv.bcast_own_sum);
166 orig_node->bat_iv.bcast_own_sum = data_ptr;
167
168 ret = 0;
169
170 unlock:
171 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
172
173 return ret;
174 }
175
176 /**
177 * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to
178 * exclude the removed interface
179 * @orig_node: the orig_node that has to be changed
180 * @max_if_num: the current amount of interfaces
181 * @del_if_num: the index of the interface being removed
182 *
183 * Returns 0 on success, a negative error code otherwise.
184 */
185 static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node,
186 int max_if_num, int del_if_num)
187 {
188 int chunk_size, ret = -ENOMEM, if_offset;
189 void *data_ptr = NULL;
190
191 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
192
193 /* last interface was removed */
194 if (max_if_num == 0)
195 goto free_bcast_own;
196
197 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
198 data_ptr = kmalloc_array(max_if_num, chunk_size, GFP_ATOMIC);
199 if (!data_ptr)
200 goto unlock;
201
202 /* copy first part */
203 memcpy(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size);
204
205 /* copy second part */
206 memcpy((char *)data_ptr + del_if_num * chunk_size,
207 orig_node->bat_iv.bcast_own + ((del_if_num + 1) * chunk_size),
208 (max_if_num - del_if_num) * chunk_size);
209
210 free_bcast_own:
211 kfree(orig_node->bat_iv.bcast_own);
212 orig_node->bat_iv.bcast_own = data_ptr;
213
214 if (max_if_num == 0)
215 goto free_own_sum;
216
217 data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
218 if (!data_ptr) {
219 kfree(orig_node->bat_iv.bcast_own);
220 goto unlock;
221 }
222
223 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
224 del_if_num * sizeof(u8));
225
226 if_offset = (del_if_num + 1) * sizeof(u8);
227 memcpy((char *)data_ptr + del_if_num * sizeof(u8),
228 orig_node->bat_iv.bcast_own_sum + if_offset,
229 (max_if_num - del_if_num) * sizeof(u8));
230
231 free_own_sum:
232 kfree(orig_node->bat_iv.bcast_own_sum);
233 orig_node->bat_iv.bcast_own_sum = data_ptr;
234
235 ret = 0;
236 unlock:
237 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
238
239 return ret;
240 }
241
242 /**
243 * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator
244 * @bat_priv: the bat priv with all the soft interface information
245 * @addr: mac address of the originator
246 *
247 * Returns the originator object corresponding to the passed mac address or NULL
248 * on failure.
249 * If the object does not exists it is created an initialised.
250 */
251 static struct batadv_orig_node *
252 batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
253 {
254 struct batadv_orig_node *orig_node;
255 int size, hash_added;
256
257 orig_node = batadv_orig_hash_find(bat_priv, addr);
258 if (orig_node)
259 return orig_node;
260
261 orig_node = batadv_orig_node_new(bat_priv, addr);
262 if (!orig_node)
263 return NULL;
264
265 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
266
267 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
268 orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC);
269 if (!orig_node->bat_iv.bcast_own)
270 goto free_orig_node;
271
272 size = bat_priv->num_ifaces * sizeof(u8);
273 orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
274 if (!orig_node->bat_iv.bcast_own_sum)
275 goto free_orig_node;
276
277 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
278 batadv_choose_orig, orig_node,
279 &orig_node->hash_entry);
280 if (hash_added != 0)
281 goto free_orig_node;
282
283 return orig_node;
284
285 free_orig_node:
286 /* free twice, as batadv_orig_node_new sets refcount to 2 */
287 batadv_orig_node_free_ref(orig_node);
288 batadv_orig_node_free_ref(orig_node);
289
290 return NULL;
291 }
292
293 static struct batadv_neigh_node *
294 batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
295 const u8 *neigh_addr,
296 struct batadv_orig_node *orig_node,
297 struct batadv_orig_node *orig_neigh)
298 {
299 struct batadv_neigh_node *neigh_node;
300
301 neigh_node = batadv_neigh_node_new(orig_node, hard_iface, neigh_addr);
302 if (!neigh_node)
303 goto out;
304
305 neigh_node->orig_node = orig_neigh;
306
307 out:
308 return neigh_node;
309 }
310
311 static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
312 {
313 struct batadv_ogm_packet *batadv_ogm_packet;
314 unsigned char *ogm_buff;
315 u32 random_seqno;
316
317 /* randomize initial seqno to avoid collision */
318 get_random_bytes(&random_seqno, sizeof(random_seqno));
319 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
320
321 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
322 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
323 if (!ogm_buff)
324 return -ENOMEM;
325
326 hard_iface->bat_iv.ogm_buff = ogm_buff;
327
328 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
329 batadv_ogm_packet->packet_type = BATADV_IV_OGM;
330 batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
331 batadv_ogm_packet->ttl = 2;
332 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
333 batadv_ogm_packet->reserved = 0;
334 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
335
336 return 0;
337 }
338
339 static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
340 {
341 kfree(hard_iface->bat_iv.ogm_buff);
342 hard_iface->bat_iv.ogm_buff = NULL;
343 }
344
345 static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
346 {
347 struct batadv_ogm_packet *batadv_ogm_packet;
348 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
349
350 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
351 ether_addr_copy(batadv_ogm_packet->orig,
352 hard_iface->net_dev->dev_addr);
353 ether_addr_copy(batadv_ogm_packet->prev_sender,
354 hard_iface->net_dev->dev_addr);
355 }
356
357 static void
358 batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
359 {
360 struct batadv_ogm_packet *batadv_ogm_packet;
361 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
362
363 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
364 batadv_ogm_packet->ttl = BATADV_TTL;
365 }
366
367 /* when do we schedule our own ogm to be sent */
368 static unsigned long
369 batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
370 {
371 unsigned int msecs;
372
373 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
374 msecs += prandom_u32() % (2 * BATADV_JITTER);
375
376 return jiffies + msecs_to_jiffies(msecs);
377 }
378
379 /* when do we schedule a ogm packet to be sent */
380 static unsigned long batadv_iv_ogm_fwd_send_time(void)
381 {
382 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
383 }
384
385 /* apply hop penalty for a normal link */
386 static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
387 {
388 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
389 int new_tq;
390
391 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
392 new_tq /= BATADV_TQ_MAX_VALUE;
393
394 return new_tq;
395 }
396
397 /* is there another aggregated packet here? */
398 static bool batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
399 __be16 tvlv_len)
400 {
401 int next_buff_pos = 0;
402
403 next_buff_pos += buff_pos + BATADV_OGM_HLEN;
404 next_buff_pos += ntohs(tvlv_len);
405
406 return (next_buff_pos <= packet_len) &&
407 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
408 }
409
410 /* send a batman ogm to a given interface */
411 static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
412 struct batadv_hard_iface *hard_iface)
413 {
414 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
415 const char *fwd_str;
416 u8 packet_num;
417 s16 buff_pos;
418 struct batadv_ogm_packet *batadv_ogm_packet;
419 struct sk_buff *skb;
420 u8 *packet_pos;
421
422 if (hard_iface->if_status != BATADV_IF_ACTIVE)
423 return;
424
425 packet_num = 0;
426 buff_pos = 0;
427 packet_pos = forw_packet->skb->data;
428 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
429
430 /* adjust all flags and log packets */
431 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
432 batadv_ogm_packet->tvlv_len)) {
433 /* we might have aggregated direct link packets with an
434 * ordinary base packet
435 */
436 if (forw_packet->direct_link_flags & BIT(packet_num) &&
437 forw_packet->if_incoming == hard_iface)
438 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
439 else
440 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
441
442 if (packet_num > 0 || !forw_packet->own)
443 fwd_str = "Forwarding";
444 else
445 fwd_str = "Sending own";
446
447 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
448 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
449 fwd_str, (packet_num > 0 ? "aggregated " : ""),
450 batadv_ogm_packet->orig,
451 ntohl(batadv_ogm_packet->seqno),
452 batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
453 ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ?
454 "on" : "off"),
455 hard_iface->net_dev->name,
456 hard_iface->net_dev->dev_addr);
457
458 buff_pos += BATADV_OGM_HLEN;
459 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
460 packet_num++;
461 packet_pos = forw_packet->skb->data + buff_pos;
462 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
463 }
464
465 /* create clone because function is called more than once */
466 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
467 if (skb) {
468 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
469 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
470 skb->len + ETH_HLEN);
471 batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr);
472 }
473 }
474
475 /* send a batman ogm packet */
476 static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
477 {
478 struct net_device *soft_iface;
479 struct batadv_priv *bat_priv;
480 struct batadv_hard_iface *primary_if = NULL;
481
482 if (!forw_packet->if_incoming) {
483 pr_err("Error - can't forward packet: incoming iface not specified\n");
484 goto out;
485 }
486
487 soft_iface = forw_packet->if_incoming->soft_iface;
488 bat_priv = netdev_priv(soft_iface);
489
490 if (WARN_ON(!forw_packet->if_outgoing))
491 goto out;
492
493 if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
494 goto out;
495
496 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
497 goto out;
498
499 primary_if = batadv_primary_if_get_selected(bat_priv);
500 if (!primary_if)
501 goto out;
502
503 /* only for one specific outgoing interface */
504 batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
505
506 out:
507 if (primary_if)
508 batadv_hardif_free_ref(primary_if);
509 }
510
511 /**
512 * batadv_iv_ogm_can_aggregate - find out if an OGM can be aggregated on an
513 * existing forward packet
514 * @new_bat_ogm_packet: OGM packet to be aggregated
515 * @bat_priv: the bat priv with all the soft interface information
516 * @packet_len: (total) length of the OGM
517 * @send_time: timestamp (jiffies) when the packet is to be sent
518 * @directlink: true if this is a direct link packet
519 * @if_incoming: interface where the packet was received
520 * @if_outgoing: interface for which the retransmission should be considered
521 * @forw_packet: the forwarded packet which should be checked
522 *
523 * Returns true if new_packet can be aggregated with forw_packet
524 */
525 static bool
526 batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
527 struct batadv_priv *bat_priv,
528 int packet_len, unsigned long send_time,
529 bool directlink,
530 const struct batadv_hard_iface *if_incoming,
531 const struct batadv_hard_iface *if_outgoing,
532 const struct batadv_forw_packet *forw_packet)
533 {
534 struct batadv_ogm_packet *batadv_ogm_packet;
535 int aggregated_bytes = forw_packet->packet_len + packet_len;
536 struct batadv_hard_iface *primary_if = NULL;
537 bool res = false;
538 unsigned long aggregation_end_time;
539
540 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
541 aggregation_end_time = send_time;
542 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
543
544 /* we can aggregate the current packet to this aggregated packet
545 * if:
546 *
547 * - the send time is within our MAX_AGGREGATION_MS time
548 * - the resulting packet wont be bigger than
549 * MAX_AGGREGATION_BYTES
550 * otherwise aggregation is not possible
551 */
552 if (!time_before(send_time, forw_packet->send_time) ||
553 !time_after_eq(aggregation_end_time, forw_packet->send_time))
554 return false;
555
556 if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES)
557 return false;
558
559 /* packet is not leaving on the same interface. */
560 if (forw_packet->if_outgoing != if_outgoing)
561 return false;
562
563 /* check aggregation compatibility
564 * -> direct link packets are broadcasted on
565 * their interface only
566 * -> aggregate packet if the current packet is
567 * a "global" packet as well as the base
568 * packet
569 */
570 primary_if = batadv_primary_if_get_selected(bat_priv);
571 if (!primary_if)
572 return false;
573
574 /* packets without direct link flag and high TTL
575 * are flooded through the net
576 */
577 if (!directlink &&
578 !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
579 batadv_ogm_packet->ttl != 1 &&
580
581 /* own packets originating non-primary
582 * interfaces leave only that interface
583 */
584 (!forw_packet->own ||
585 forw_packet->if_incoming == primary_if)) {
586 res = true;
587 goto out;
588 }
589
590 /* if the incoming packet is sent via this one
591 * interface only - we still can aggregate
592 */
593 if (directlink &&
594 new_bat_ogm_packet->ttl == 1 &&
595 forw_packet->if_incoming == if_incoming &&
596
597 /* packets from direct neighbors or
598 * own secondary interface packets
599 * (= secondary interface packets in general)
600 */
601 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
602 (forw_packet->own &&
603 forw_packet->if_incoming != primary_if))) {
604 res = true;
605 goto out;
606 }
607
608 out:
609 if (primary_if)
610 batadv_hardif_free_ref(primary_if);
611 return res;
612 }
613
614 /**
615 * batadv_iv_ogm_aggregate_new - create a new aggregated packet and add this
616 * packet to it.
617 * @packet_buff: pointer to the OGM
618 * @packet_len: (total) length of the OGM
619 * @send_time: timestamp (jiffies) when the packet is to be sent
620 * @direct_link: whether this OGM has direct link status
621 * @if_incoming: interface where the packet was received
622 * @if_outgoing: interface for which the retransmission should be considered
623 * @own_packet: true if it is a self-generated ogm
624 */
625 static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
626 int packet_len, unsigned long send_time,
627 bool direct_link,
628 struct batadv_hard_iface *if_incoming,
629 struct batadv_hard_iface *if_outgoing,
630 int own_packet)
631 {
632 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
633 struct batadv_forw_packet *forw_packet_aggr;
634 unsigned char *skb_buff;
635 unsigned int skb_size;
636
637 if (!atomic_inc_not_zero(&if_incoming->refcount))
638 return;
639
640 if (!atomic_inc_not_zero(&if_outgoing->refcount))
641 goto out_free_incoming;
642
643 /* own packet should always be scheduled */
644 if (!own_packet) {
645 if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
646 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
647 "batman packet queue full\n");
648 goto out_free_outgoing;
649 }
650 }
651
652 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
653 if (!forw_packet_aggr)
654 goto out_nomem;
655
656 if (atomic_read(&bat_priv->aggregated_ogms) &&
657 packet_len < BATADV_MAX_AGGREGATION_BYTES)
658 skb_size = BATADV_MAX_AGGREGATION_BYTES;
659 else
660 skb_size = packet_len;
661
662 skb_size += ETH_HLEN;
663
664 forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
665 if (!forw_packet_aggr->skb)
666 goto out_free_forw_packet;
667 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
668 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
669
670 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
671 forw_packet_aggr->packet_len = packet_len;
672 memcpy(skb_buff, packet_buff, packet_len);
673
674 forw_packet_aggr->own = own_packet;
675 forw_packet_aggr->if_incoming = if_incoming;
676 forw_packet_aggr->if_outgoing = if_outgoing;
677 forw_packet_aggr->num_packets = 0;
678 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
679 forw_packet_aggr->send_time = send_time;
680
681 /* save packet direct link flag status */
682 if (direct_link)
683 forw_packet_aggr->direct_link_flags |= 1;
684
685 /* add new packet to packet list */
686 spin_lock_bh(&bat_priv->forw_bat_list_lock);
687 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
688 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
689
690 /* start timer for this packet */
691 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
692 batadv_send_outstanding_bat_ogm_packet);
693 queue_delayed_work(batadv_event_workqueue,
694 &forw_packet_aggr->delayed_work,
695 send_time - jiffies);
696
697 return;
698 out_free_forw_packet:
699 kfree(forw_packet_aggr);
700 out_nomem:
701 if (!own_packet)
702 atomic_inc(&bat_priv->batman_queue_left);
703 out_free_outgoing:
704 batadv_hardif_free_ref(if_outgoing);
705 out_free_incoming:
706 batadv_hardif_free_ref(if_incoming);
707 }
708
709 /* aggregate a new packet into the existing ogm packet */
710 static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
711 const unsigned char *packet_buff,
712 int packet_len, bool direct_link)
713 {
714 unsigned char *skb_buff;
715 unsigned long new_direct_link_flag;
716
717 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
718 memcpy(skb_buff, packet_buff, packet_len);
719 forw_packet_aggr->packet_len += packet_len;
720 forw_packet_aggr->num_packets++;
721
722 /* save packet direct link flag status */
723 if (direct_link) {
724 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
725 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
726 }
727 }
728
729 /**
730 * batadv_iv_ogm_queue_add - queue up an OGM for transmission
731 * @bat_priv: the bat priv with all the soft interface information
732 * @packet_buff: pointer to the OGM
733 * @packet_len: (total) length of the OGM
734 * @if_incoming: interface where the packet was received
735 * @if_outgoing: interface for which the retransmission should be considered
736 * @own_packet: true if it is a self-generated ogm
737 * @send_time: timestamp (jiffies) when the packet is to be sent
738 */
739 static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
740 unsigned char *packet_buff,
741 int packet_len,
742 struct batadv_hard_iface *if_incoming,
743 struct batadv_hard_iface *if_outgoing,
744 int own_packet, unsigned long send_time)
745 {
746 /* _aggr -> pointer to the packet we want to aggregate with
747 * _pos -> pointer to the position in the queue
748 */
749 struct batadv_forw_packet *forw_packet_aggr = NULL;
750 struct batadv_forw_packet *forw_packet_pos = NULL;
751 struct batadv_ogm_packet *batadv_ogm_packet;
752 bool direct_link;
753 unsigned long max_aggregation_jiffies;
754
755 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
756 direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
757 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
758
759 /* find position for the packet in the forward queue */
760 spin_lock_bh(&bat_priv->forw_bat_list_lock);
761 /* own packets are not to be aggregated */
762 if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
763 hlist_for_each_entry(forw_packet_pos,
764 &bat_priv->forw_bat_list, list) {
765 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
766 bat_priv, packet_len,
767 send_time, direct_link,
768 if_incoming,
769 if_outgoing,
770 forw_packet_pos)) {
771 forw_packet_aggr = forw_packet_pos;
772 break;
773 }
774 }
775 }
776
777 /* nothing to aggregate with - either aggregation disabled or no
778 * suitable aggregation packet found
779 */
780 if (!forw_packet_aggr) {
781 /* the following section can run without the lock */
782 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
783
784 /* if we could not aggregate this packet with one of the others
785 * we hold it back for a while, so that it might be aggregated
786 * later on
787 */
788 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
789 send_time += max_aggregation_jiffies;
790
791 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
792 send_time, direct_link,
793 if_incoming, if_outgoing,
794 own_packet);
795 } else {
796 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
797 packet_len, direct_link);
798 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
799 }
800 }
801
802 static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
803 const struct ethhdr *ethhdr,
804 struct batadv_ogm_packet *batadv_ogm_packet,
805 bool is_single_hop_neigh,
806 bool is_from_best_next_hop,
807 struct batadv_hard_iface *if_incoming,
808 struct batadv_hard_iface *if_outgoing)
809 {
810 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
811 u16 tvlv_len;
812
813 if (batadv_ogm_packet->ttl <= 1) {
814 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
815 return;
816 }
817
818 if (!is_from_best_next_hop) {
819 /* Mark the forwarded packet when it is not coming from our
820 * best next hop. We still need to forward the packet for our
821 * neighbor link quality detection to work in case the packet
822 * originated from a single hop neighbor. Otherwise we can
823 * simply drop the ogm.
824 */
825 if (is_single_hop_neigh)
826 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
827 else
828 return;
829 }
830
831 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
832
833 batadv_ogm_packet->ttl--;
834 ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
835
836 /* apply hop penalty */
837 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
838 bat_priv);
839
840 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
841 "Forwarding packet: tq: %i, ttl: %i\n",
842 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
843
844 if (is_single_hop_neigh)
845 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
846 else
847 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
848
849 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
850 BATADV_OGM_HLEN + tvlv_len,
851 if_incoming, if_outgoing, 0,
852 batadv_iv_ogm_fwd_send_time());
853 }
854
855 /**
856 * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
857 * the given interface
858 * @hard_iface: the interface for which the windows have to be shifted
859 */
860 static void
861 batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
862 {
863 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
864 struct batadv_hashtable *hash = bat_priv->orig_hash;
865 struct hlist_head *head;
866 struct batadv_orig_node *orig_node;
867 unsigned long *word;
868 u32 i;
869 size_t word_index;
870 u8 *w;
871 int if_num;
872
873 for (i = 0; i < hash->size; i++) {
874 head = &hash->table[i];
875
876 rcu_read_lock();
877 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
878 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
879 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
880 word = &orig_node->bat_iv.bcast_own[word_index];
881
882 batadv_bit_get_packet(bat_priv, word, 1, 0);
883 if_num = hard_iface->if_num;
884 w = &orig_node->bat_iv.bcast_own_sum[if_num];
885 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
886 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
887 }
888 rcu_read_unlock();
889 }
890 }
891
892 static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
893 {
894 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
895 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
896 struct batadv_ogm_packet *batadv_ogm_packet;
897 struct batadv_hard_iface *primary_if, *tmp_hard_iface;
898 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
899 u32 seqno;
900 u16 tvlv_len = 0;
901 unsigned long send_time;
902
903 primary_if = batadv_primary_if_get_selected(bat_priv);
904
905 if (hard_iface == primary_if) {
906 /* tt changes have to be committed before the tvlv data is
907 * appended as it may alter the tt tvlv container
908 */
909 batadv_tt_local_commit_changes(bat_priv);
910 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
911 ogm_buff_len,
912 BATADV_OGM_HLEN);
913 }
914
915 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
916 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
917
918 /* change sequence number to network order */
919 seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
920 batadv_ogm_packet->seqno = htonl(seqno);
921 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
922
923 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
924
925 send_time = batadv_iv_ogm_emit_send_time(bat_priv);
926
927 if (hard_iface != primary_if) {
928 /* OGMs from secondary interfaces are only scheduled on their
929 * respective interfaces.
930 */
931 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
932 hard_iface, hard_iface, 1, send_time);
933 goto out;
934 }
935
936 /* OGMs from primary interfaces are scheduled on all
937 * interfaces.
938 */
939 rcu_read_lock();
940 list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
941 if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
942 continue;
943 batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
944 *ogm_buff_len, hard_iface,
945 tmp_hard_iface, 1, send_time);
946 }
947 rcu_read_unlock();
948
949 out:
950 if (primary_if)
951 batadv_hardif_free_ref(primary_if);
952 }
953
954 /**
955 * batadv_iv_ogm_orig_update - use OGM to update corresponding data in an
956 * originator
957 * @bat_priv: the bat priv with all the soft interface information
958 * @orig_node: the orig node who originally emitted the ogm packet
959 * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
960 * @ethhdr: Ethernet header of the OGM
961 * @batadv_ogm_packet: the ogm packet
962 * @if_incoming: interface where the packet was received
963 * @if_outgoing: interface for which the retransmission should be considered
964 * @dup_status: the duplicate status of this ogm packet.
965 */
966 static void
967 batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
968 struct batadv_orig_node *orig_node,
969 struct batadv_orig_ifinfo *orig_ifinfo,
970 const struct ethhdr *ethhdr,
971 const struct batadv_ogm_packet *batadv_ogm_packet,
972 struct batadv_hard_iface *if_incoming,
973 struct batadv_hard_iface *if_outgoing,
974 enum batadv_dup_status dup_status)
975 {
976 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
977 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
978 struct batadv_neigh_node *neigh_node = NULL;
979 struct batadv_neigh_node *tmp_neigh_node = NULL;
980 struct batadv_neigh_node *router = NULL;
981 struct batadv_orig_node *orig_node_tmp;
982 int if_num;
983 u8 sum_orig, sum_neigh;
984 u8 *neigh_addr;
985 u8 tq_avg;
986
987 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
988 "update_originator(): Searching and updating originator entry of received packet\n");
989
990 rcu_read_lock();
991 hlist_for_each_entry_rcu(tmp_neigh_node,
992 &orig_node->neigh_list, list) {
993 neigh_addr = tmp_neigh_node->addr;
994 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
995 tmp_neigh_node->if_incoming == if_incoming &&
996 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
997 if (WARN(neigh_node, "too many matching neigh_nodes"))
998 batadv_neigh_node_free_ref(neigh_node);
999 neigh_node = tmp_neigh_node;
1000 continue;
1001 }
1002
1003 if (dup_status != BATADV_NO_DUP)
1004 continue;
1005
1006 /* only update the entry for this outgoing interface */
1007 neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
1008 if_outgoing);
1009 if (!neigh_ifinfo)
1010 continue;
1011
1012 spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
1013 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1014 &neigh_ifinfo->bat_iv.tq_index, 0);
1015 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1016 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1017 spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
1018
1019 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
1020 neigh_ifinfo = NULL;
1021 }
1022
1023 if (!neigh_node) {
1024 struct batadv_orig_node *orig_tmp;
1025
1026 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
1027 if (!orig_tmp)
1028 goto unlock;
1029
1030 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1031 ethhdr->h_source,
1032 orig_node, orig_tmp);
1033
1034 batadv_orig_node_free_ref(orig_tmp);
1035 if (!neigh_node)
1036 goto unlock;
1037 } else {
1038 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1039 "Updating existing last-hop neighbor of originator\n");
1040 }
1041
1042 rcu_read_unlock();
1043 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1044 if (!neigh_ifinfo)
1045 goto out;
1046
1047 neigh_node->last_seen = jiffies;
1048
1049 spin_lock_bh(&neigh_node->ifinfo_lock);
1050 batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
1051 &neigh_ifinfo->bat_iv.tq_index,
1052 batadv_ogm_packet->tq);
1053 tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
1054 neigh_ifinfo->bat_iv.tq_avg = tq_avg;
1055 spin_unlock_bh(&neigh_node->ifinfo_lock);
1056
1057 if (dup_status == BATADV_NO_DUP) {
1058 orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
1059 neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
1060 }
1061
1062 /* if this neighbor already is our next hop there is nothing
1063 * to change
1064 */
1065 router = batadv_orig_router_get(orig_node, if_outgoing);
1066 if (router == neigh_node)
1067 goto out;
1068
1069 if (router) {
1070 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1071 if (!router_ifinfo)
1072 goto out;
1073
1074 /* if this neighbor does not offer a better TQ we won't
1075 * consider it
1076 */
1077 if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
1078 goto out;
1079 }
1080
1081 /* if the TQ is the same and the link not more symmetric we
1082 * won't consider it either
1083 */
1084 if (router_ifinfo &&
1085 neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
1086 orig_node_tmp = router->orig_node;
1087 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1088 if_num = router->if_incoming->if_num;
1089 sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1090 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1091
1092 orig_node_tmp = neigh_node->orig_node;
1093 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1094 if_num = neigh_node->if_incoming->if_num;
1095 sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
1096 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
1097
1098 if (sum_orig >= sum_neigh)
1099 goto out;
1100 }
1101
1102 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
1103 goto out;
1104
1105 unlock:
1106 rcu_read_unlock();
1107 out:
1108 if (neigh_node)
1109 batadv_neigh_node_free_ref(neigh_node);
1110 if (router)
1111 batadv_neigh_node_free_ref(router);
1112 if (neigh_ifinfo)
1113 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
1114 if (router_ifinfo)
1115 batadv_neigh_ifinfo_free_ref(router_ifinfo);
1116 }
1117
1118 /**
1119 * batadv_iv_ogm_calc_tq - calculate tq for current received ogm packet
1120 * @orig_node: the orig node who originally emitted the ogm packet
1121 * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
1122 * @batadv_ogm_packet: the ogm packet
1123 * @if_incoming: interface where the packet was received
1124 * @if_outgoing: interface for which the retransmission should be considered
1125 *
1126 * Returns 1 if the link can be considered bidirectional, 0 otherwise
1127 */
1128 static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
1129 struct batadv_orig_node *orig_neigh_node,
1130 struct batadv_ogm_packet *batadv_ogm_packet,
1131 struct batadv_hard_iface *if_incoming,
1132 struct batadv_hard_iface *if_outgoing)
1133 {
1134 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1135 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
1136 struct batadv_neigh_ifinfo *neigh_ifinfo;
1137 u8 total_count;
1138 u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
1139 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
1140 int tq_asym_penalty, inv_asym_penalty, if_num, ret = 0;
1141 unsigned int combined_tq;
1142 int tq_iface_penalty;
1143
1144 /* find corresponding one hop neighbor */
1145 rcu_read_lock();
1146 hlist_for_each_entry_rcu(tmp_neigh_node,
1147 &orig_neigh_node->neigh_list, list) {
1148 if (!batadv_compare_eth(tmp_neigh_node->addr,
1149 orig_neigh_node->orig))
1150 continue;
1151
1152 if (tmp_neigh_node->if_incoming != if_incoming)
1153 continue;
1154
1155 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
1156 continue;
1157
1158 neigh_node = tmp_neigh_node;
1159 break;
1160 }
1161 rcu_read_unlock();
1162
1163 if (!neigh_node)
1164 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
1165 orig_neigh_node->orig,
1166 orig_neigh_node,
1167 orig_neigh_node);
1168
1169 if (!neigh_node)
1170 goto out;
1171
1172 /* if orig_node is direct neighbor update neigh_node last_seen */
1173 if (orig_node == orig_neigh_node)
1174 neigh_node->last_seen = jiffies;
1175
1176 orig_node->last_seen = jiffies;
1177
1178 /* find packet count of corresponding one hop neighbor */
1179 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1180 if_num = if_incoming->if_num;
1181 orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
1182 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
1183 if (neigh_ifinfo) {
1184 neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
1185 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
1186 } else {
1187 neigh_rq_count = 0;
1188 }
1189 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1190
1191 /* pay attention to not get a value bigger than 100 % */
1192 if (orig_eq_count > neigh_rq_count)
1193 total_count = neigh_rq_count;
1194 else
1195 total_count = orig_eq_count;
1196
1197 /* if we have too few packets (too less data) we set tq_own to zero
1198 * if we receive too few packets it is not considered bidirectional
1199 */
1200 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
1201 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
1202 tq_own = 0;
1203 else
1204 /* neigh_node->real_packet_count is never zero as we
1205 * only purge old information when getting new
1206 * information
1207 */
1208 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
1209
1210 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
1211 * affect the nearly-symmetric links only a little, but
1212 * punishes asymmetric links more. This will give a value
1213 * between 0 and TQ_MAX_VALUE
1214 */
1215 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
1216 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
1217 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
1218 BATADV_TQ_LOCAL_WINDOW_SIZE *
1219 BATADV_TQ_LOCAL_WINDOW_SIZE;
1220 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
1221 inv_asym_penalty /= neigh_rq_max_cube;
1222 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
1223
1224 /* penalize if the OGM is forwarded on the same interface. WiFi
1225 * interfaces and other half duplex devices suffer from throughput
1226 * drops as they can't send and receive at the same time.
1227 */
1228 tq_iface_penalty = BATADV_TQ_MAX_VALUE;
1229 if (if_outgoing && (if_incoming == if_outgoing) &&
1230 batadv_is_wifi_netdev(if_outgoing->net_dev))
1231 tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
1232 bat_priv);
1233
1234 combined_tq = batadv_ogm_packet->tq *
1235 tq_own *
1236 tq_asym_penalty *
1237 tq_iface_penalty;
1238 combined_tq /= BATADV_TQ_MAX_VALUE *
1239 BATADV_TQ_MAX_VALUE *
1240 BATADV_TQ_MAX_VALUE;
1241 batadv_ogm_packet->tq = combined_tq;
1242
1243 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1244 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, iface_penalty: %3i, total tq: %3i, if_incoming = %s, if_outgoing = %s\n",
1245 orig_node->orig, orig_neigh_node->orig, total_count,
1246 neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
1247 batadv_ogm_packet->tq, if_incoming->net_dev->name,
1248 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
1249
1250 /* if link has the minimum required transmission quality
1251 * consider it bidirectional
1252 */
1253 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
1254 ret = 1;
1255
1256 out:
1257 if (neigh_node)
1258 batadv_neigh_node_free_ref(neigh_node);
1259 return ret;
1260 }
1261
1262 /**
1263 * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
1264 * adjust the sequence number and find out whether it is a duplicate
1265 * @ethhdr: ethernet header of the packet
1266 * @batadv_ogm_packet: OGM packet to be considered
1267 * @if_incoming: interface on which the OGM packet was received
1268 * @if_outgoing: interface for which the retransmission should be considered
1269 *
1270 * Returns duplicate status as enum batadv_dup_status
1271 */
1272 static enum batadv_dup_status
1273 batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
1274 const struct batadv_ogm_packet *batadv_ogm_packet,
1275 const struct batadv_hard_iface *if_incoming,
1276 struct batadv_hard_iface *if_outgoing)
1277 {
1278 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1279 struct batadv_orig_node *orig_node;
1280 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
1281 struct batadv_neigh_node *neigh_node;
1282 struct batadv_neigh_ifinfo *neigh_ifinfo;
1283 int is_dup;
1284 s32 seq_diff;
1285 int need_update = 0;
1286 int set_mark;
1287 enum batadv_dup_status ret = BATADV_NO_DUP;
1288 u32 seqno = ntohl(batadv_ogm_packet->seqno);
1289 u8 *neigh_addr;
1290 u8 packet_count;
1291 unsigned long *bitmap;
1292
1293 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1294 if (!orig_node)
1295 return BATADV_NO_DUP;
1296
1297 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1298 if (WARN_ON(!orig_ifinfo)) {
1299 batadv_orig_node_free_ref(orig_node);
1300 return 0;
1301 }
1302
1303 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1304 seq_diff = seqno - orig_ifinfo->last_real_seqno;
1305
1306 /* signalize caller that the packet is to be dropped. */
1307 if (!hlist_empty(&orig_node->neigh_list) &&
1308 batadv_window_protected(bat_priv, seq_diff,
1309 &orig_ifinfo->batman_seqno_reset)) {
1310 ret = BATADV_PROTECTED;
1311 goto out;
1312 }
1313
1314 rcu_read_lock();
1315 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1316 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
1317 if_outgoing);
1318 if (!neigh_ifinfo)
1319 continue;
1320
1321 neigh_addr = neigh_node->addr;
1322 is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
1323 orig_ifinfo->last_real_seqno,
1324 seqno);
1325
1326 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1327 neigh_node->if_incoming == if_incoming) {
1328 set_mark = 1;
1329 if (is_dup)
1330 ret = BATADV_NEIGH_DUP;
1331 } else {
1332 set_mark = 0;
1333 if (is_dup && (ret != BATADV_NEIGH_DUP))
1334 ret = BATADV_ORIG_DUP;
1335 }
1336
1337 /* if the window moved, set the update flag. */
1338 bitmap = neigh_ifinfo->bat_iv.real_bits;
1339 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
1340 seq_diff, set_mark);
1341
1342 packet_count = bitmap_weight(bitmap,
1343 BATADV_TQ_LOCAL_WINDOW_SIZE);
1344 neigh_ifinfo->bat_iv.real_packet_count = packet_count;
1345 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
1346 }
1347 rcu_read_unlock();
1348
1349 if (need_update) {
1350 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1351 "%s updating last_seqno: old %u, new %u\n",
1352 if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
1353 orig_ifinfo->last_real_seqno, seqno);
1354 orig_ifinfo->last_real_seqno = seqno;
1355 }
1356
1357 out:
1358 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1359 batadv_orig_node_free_ref(orig_node);
1360 batadv_orig_ifinfo_free_ref(orig_ifinfo);
1361 return ret;
1362 }
1363
1364 /**
1365 * batadv_iv_ogm_process_per_outif - process a batman iv OGM for an outgoing if
1366 * @skb: the skb containing the OGM
1367 * @ogm_offset: offset from skb->data to start of ogm header
1368 * @orig_node: the (cached) orig node for the originator of this OGM
1369 * @if_incoming: the interface where this packet was received
1370 * @if_outgoing: the interface for which the packet should be considered
1371 */
1372 static void
1373 batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
1374 struct batadv_orig_node *orig_node,
1375 struct batadv_hard_iface *if_incoming,
1376 struct batadv_hard_iface *if_outgoing)
1377 {
1378 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1379 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
1380 struct batadv_neigh_node *router = NULL;
1381 struct batadv_neigh_node *router_router = NULL;
1382 struct batadv_orig_node *orig_neigh_node;
1383 struct batadv_orig_ifinfo *orig_ifinfo;
1384 struct batadv_neigh_node *orig_neigh_router = NULL;
1385 struct batadv_neigh_ifinfo *router_ifinfo = NULL;
1386 struct batadv_ogm_packet *ogm_packet;
1387 enum batadv_dup_status dup_status;
1388 bool is_from_best_next_hop = false;
1389 bool is_single_hop_neigh = false;
1390 bool sameseq, similar_ttl;
1391 struct sk_buff *skb_priv;
1392 struct ethhdr *ethhdr;
1393 u8 *prev_sender;
1394 int is_bidirect;
1395
1396 /* create a private copy of the skb, as some functions change tq value
1397 * and/or flags.
1398 */
1399 skb_priv = skb_copy(skb, GFP_ATOMIC);
1400 if (!skb_priv)
1401 return;
1402
1403 ethhdr = eth_hdr(skb_priv);
1404 ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
1405
1406 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
1407 if_incoming, if_outgoing);
1408 if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
1409 is_single_hop_neigh = true;
1410
1411 if (dup_status == BATADV_PROTECTED) {
1412 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1413 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1414 ethhdr->h_source);
1415 goto out;
1416 }
1417
1418 if (ogm_packet->tq == 0) {
1419 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1420 "Drop packet: originator packet with tq equal 0\n");
1421 goto out;
1422 }
1423
1424 if (is_single_hop_neigh) {
1425 hardif_neigh = batadv_hardif_neigh_get(if_incoming,
1426 ethhdr->h_source);
1427 if (hardif_neigh)
1428 hardif_neigh->last_seen = jiffies;
1429 }
1430
1431 router = batadv_orig_router_get(orig_node, if_outgoing);
1432 if (router) {
1433 router_router = batadv_orig_router_get(router->orig_node,
1434 if_outgoing);
1435 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
1436 }
1437
1438 if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
1439 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1440 is_from_best_next_hop = true;
1441
1442 prev_sender = ogm_packet->prev_sender;
1443 /* avoid temporary routing loops */
1444 if (router && router_router &&
1445 (batadv_compare_eth(router->addr, prev_sender)) &&
1446 !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
1447 (batadv_compare_eth(router->addr, router_router->addr))) {
1448 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1449 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1450 ethhdr->h_source);
1451 goto out;
1452 }
1453
1454 if (if_outgoing == BATADV_IF_DEFAULT)
1455 batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
1456
1457 /* if sender is a direct neighbor the sender mac equals
1458 * originator mac
1459 */
1460 if (is_single_hop_neigh)
1461 orig_neigh_node = orig_node;
1462 else
1463 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1464 ethhdr->h_source);
1465
1466 if (!orig_neigh_node)
1467 goto out;
1468
1469 /* Update nc_nodes of the originator */
1470 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1471 ogm_packet, is_single_hop_neigh);
1472
1473 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
1474 if_outgoing);
1475
1476 /* drop packet if sender is not a direct neighbor and if we
1477 * don't route towards it
1478 */
1479 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1480 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1481 "Drop packet: OGM via unknown neighbor!\n");
1482 goto out_neigh;
1483 }
1484
1485 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1486 ogm_packet, if_incoming,
1487 if_outgoing);
1488
1489 /* update ranking if it is not a duplicate or has the same
1490 * seqno and similar ttl as the non-duplicate
1491 */
1492 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
1493 if (!orig_ifinfo)
1494 goto out_neigh;
1495
1496 sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
1497 similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
1498
1499 if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
1500 (sameseq && similar_ttl))) {
1501 batadv_iv_ogm_orig_update(bat_priv, orig_node,
1502 orig_ifinfo, ethhdr,
1503 ogm_packet, if_incoming,
1504 if_outgoing, dup_status);
1505 }
1506 batadv_orig_ifinfo_free_ref(orig_ifinfo);
1507
1508 /* only forward for specific interface, not for the default one. */
1509 if (if_outgoing == BATADV_IF_DEFAULT)
1510 goto out_neigh;
1511
1512 /* is single hop (direct) neighbor */
1513 if (is_single_hop_neigh) {
1514 /* OGMs from secondary interfaces should only scheduled once
1515 * per interface where it has been received, not multiple times
1516 */
1517 if ((ogm_packet->ttl <= 2) &&
1518 (if_incoming != if_outgoing)) {
1519 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1520 "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
1521 goto out_neigh;
1522 }
1523 /* mark direct link on incoming interface */
1524 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1525 is_single_hop_neigh,
1526 is_from_best_next_hop, if_incoming,
1527 if_outgoing);
1528
1529 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1530 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1531 goto out_neigh;
1532 }
1533
1534 /* multihop originator */
1535 if (!is_bidirect) {
1536 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1537 "Drop packet: not received via bidirectional link\n");
1538 goto out_neigh;
1539 }
1540
1541 if (dup_status == BATADV_NEIGH_DUP) {
1542 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1543 "Drop packet: duplicate packet received\n");
1544 goto out_neigh;
1545 }
1546
1547 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1548 "Forwarding packet: rebroadcast originator packet\n");
1549 batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
1550 is_single_hop_neigh, is_from_best_next_hop,
1551 if_incoming, if_outgoing);
1552
1553 out_neigh:
1554 if ((orig_neigh_node) && (!is_single_hop_neigh))
1555 batadv_orig_node_free_ref(orig_neigh_node);
1556 out:
1557 if (router_ifinfo)
1558 batadv_neigh_ifinfo_free_ref(router_ifinfo);
1559 if (router)
1560 batadv_neigh_node_free_ref(router);
1561 if (router_router)
1562 batadv_neigh_node_free_ref(router_router);
1563 if (orig_neigh_router)
1564 batadv_neigh_node_free_ref(orig_neigh_router);
1565 if (hardif_neigh)
1566 batadv_hardif_neigh_free_ref(hardif_neigh);
1567
1568 kfree_skb(skb_priv);
1569 }
1570
1571 /**
1572 * batadv_iv_ogm_process - process an incoming batman iv OGM
1573 * @skb: the skb containing the OGM
1574 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
1575 * @if_incoming: the interface where this packet was receved
1576 */
1577 static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
1578 struct batadv_hard_iface *if_incoming)
1579 {
1580 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1581 struct batadv_orig_node *orig_neigh_node, *orig_node;
1582 struct batadv_hard_iface *hard_iface;
1583 struct batadv_ogm_packet *ogm_packet;
1584 u32 if_incoming_seqno;
1585 bool has_directlink_flag;
1586 struct ethhdr *ethhdr;
1587 bool is_my_oldorig = false;
1588 bool is_my_addr = false;
1589 bool is_my_orig = false;
1590
1591 ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
1592 ethhdr = eth_hdr(skb);
1593
1594 /* Silently drop when the batman packet is actually not a
1595 * correct packet.
1596 *
1597 * This might happen if a packet is padded (e.g. Ethernet has a
1598 * minimum frame length of 64 byte) and the aggregation interprets
1599 * it as an additional length.
1600 *
1601 * TODO: A more sane solution would be to have a bit in the
1602 * batadv_ogm_packet to detect whether the packet is the last
1603 * packet in an aggregation. Here we expect that the padding
1604 * is always zero (or not 0x01)
1605 */
1606 if (ogm_packet->packet_type != BATADV_IV_OGM)
1607 return;
1608
1609 /* could be changed by schedule_own_packet() */
1610 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
1611
1612 if (ogm_packet->flags & BATADV_DIRECTLINK)
1613 has_directlink_flag = true;
1614 else
1615 has_directlink_flag = false;
1616
1617 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1618 "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",
1619 ethhdr->h_source, if_incoming->net_dev->name,
1620 if_incoming->net_dev->dev_addr, ogm_packet->orig,
1621 ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
1622 ogm_packet->tq, ogm_packet->ttl,
1623 ogm_packet->version, has_directlink_flag);
1624
1625 rcu_read_lock();
1626 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1627 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1628 continue;
1629
1630 if (hard_iface->soft_iface != if_incoming->soft_iface)
1631 continue;
1632
1633 if (batadv_compare_eth(ethhdr->h_source,
1634 hard_iface->net_dev->dev_addr))
1635 is_my_addr = true;
1636
1637 if (batadv_compare_eth(ogm_packet->orig,
1638 hard_iface->net_dev->dev_addr))
1639 is_my_orig = true;
1640
1641 if (batadv_compare_eth(ogm_packet->prev_sender,
1642 hard_iface->net_dev->dev_addr))
1643 is_my_oldorig = true;
1644 }
1645 rcu_read_unlock();
1646
1647 if (is_my_addr) {
1648 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1649 "Drop packet: received my own broadcast (sender: %pM)\n",
1650 ethhdr->h_source);
1651 return;
1652 }
1653
1654 if (is_my_orig) {
1655 unsigned long *word;
1656 int offset;
1657 s32 bit_pos;
1658 s16 if_num;
1659 u8 *weight;
1660
1661 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1662 ethhdr->h_source);
1663 if (!orig_neigh_node)
1664 return;
1665
1666 /* neighbor has to indicate direct link and it has to
1667 * come via the corresponding interface
1668 * save packet seqno for bidirectional check
1669 */
1670 if (has_directlink_flag &&
1671 batadv_compare_eth(if_incoming->net_dev->dev_addr,
1672 ogm_packet->orig)) {
1673 if_num = if_incoming->if_num;
1674 offset = if_num * BATADV_NUM_WORDS;
1675
1676 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1677 word = &orig_neigh_node->bat_iv.bcast_own[offset];
1678 bit_pos = if_incoming_seqno - 2;
1679 bit_pos -= ntohl(ogm_packet->seqno);
1680 batadv_set_bit(word, bit_pos);
1681 weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
1682 *weight = bitmap_weight(word,
1683 BATADV_TQ_LOCAL_WINDOW_SIZE);
1684 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1685 }
1686
1687 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1688 "Drop packet: originator packet from myself (via neighbor)\n");
1689 batadv_orig_node_free_ref(orig_neigh_node);
1690 return;
1691 }
1692
1693 if (is_my_oldorig) {
1694 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1695 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1696 ethhdr->h_source);
1697 return;
1698 }
1699
1700 if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
1701 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1702 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1703 ethhdr->h_source);
1704 return;
1705 }
1706
1707 orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
1708 if (!orig_node)
1709 return;
1710
1711 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1712 if_incoming, BATADV_IF_DEFAULT);
1713
1714 rcu_read_lock();
1715 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1716 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1717 continue;
1718
1719 if (hard_iface->soft_iface != bat_priv->soft_iface)
1720 continue;
1721
1722 batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
1723 if_incoming, hard_iface);
1724 }
1725 rcu_read_unlock();
1726
1727 batadv_orig_node_free_ref(orig_node);
1728 }
1729
1730 static int batadv_iv_ogm_receive(struct sk_buff *skb,
1731 struct batadv_hard_iface *if_incoming)
1732 {
1733 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1734 struct batadv_ogm_packet *ogm_packet;
1735 u8 *packet_pos;
1736 int ogm_offset;
1737 bool ret;
1738
1739 ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1740 if (!ret)
1741 return NET_RX_DROP;
1742
1743 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1744 * that does not have B.A.T.M.A.N. IV enabled ?
1745 */
1746 if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit)
1747 return NET_RX_DROP;
1748
1749 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1750 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
1751 skb->len + ETH_HLEN);
1752
1753 ogm_offset = 0;
1754 ogm_packet = (struct batadv_ogm_packet *)skb->data;
1755
1756 /* unpack the aggregated packets and process them one by one */
1757 while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1758 ogm_packet->tvlv_len)) {
1759 batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
1760
1761 ogm_offset += BATADV_OGM_HLEN;
1762 ogm_offset += ntohs(ogm_packet->tvlv_len);
1763
1764 packet_pos = skb->data + ogm_offset;
1765 ogm_packet = (struct batadv_ogm_packet *)packet_pos;
1766 }
1767
1768 kfree_skb(skb);
1769 return NET_RX_SUCCESS;
1770 }
1771
1772 /**
1773 * batadv_iv_ogm_orig_print_neigh - print neighbors for the originator table
1774 * @orig_node: the orig_node for which the neighbors are printed
1775 * @if_outgoing: outgoing interface for these entries
1776 * @seq: debugfs table seq_file struct
1777 *
1778 * Must be called while holding an rcu lock.
1779 */
1780 static void
1781 batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
1782 struct batadv_hard_iface *if_outgoing,
1783 struct seq_file *seq)
1784 {
1785 struct batadv_neigh_node *neigh_node;
1786 struct batadv_neigh_ifinfo *n_ifinfo;
1787
1788 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
1789 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1790 if (!n_ifinfo)
1791 continue;
1792
1793 seq_printf(seq, " %pM (%3i)",
1794 neigh_node->addr,
1795 n_ifinfo->bat_iv.tq_avg);
1796
1797 batadv_neigh_ifinfo_free_ref(n_ifinfo);
1798 }
1799 }
1800
1801 /**
1802 * batadv_iv_ogm_orig_print - print the originator table
1803 * @bat_priv: the bat priv with all the soft interface information
1804 * @seq: debugfs table seq_file struct
1805 * @if_outgoing: the outgoing interface for which this should be printed
1806 */
1807 static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
1808 struct seq_file *seq,
1809 struct batadv_hard_iface *if_outgoing)
1810 {
1811 struct batadv_neigh_node *neigh_node;
1812 struct batadv_hashtable *hash = bat_priv->orig_hash;
1813 int last_seen_msecs, last_seen_secs;
1814 struct batadv_orig_node *orig_node;
1815 struct batadv_neigh_ifinfo *n_ifinfo;
1816 unsigned long last_seen_jiffies;
1817 struct hlist_head *head;
1818 int batman_count = 0;
1819 u32 i;
1820
1821 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
1822 "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE,
1823 "Nexthop", "outgoingIF", "Potential nexthops");
1824
1825 for (i = 0; i < hash->size; i++) {
1826 head = &hash->table[i];
1827
1828 rcu_read_lock();
1829 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1830 neigh_node = batadv_orig_router_get(orig_node,
1831 if_outgoing);
1832 if (!neigh_node)
1833 continue;
1834
1835 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
1836 if_outgoing);
1837 if (!n_ifinfo)
1838 goto next;
1839
1840 if (n_ifinfo->bat_iv.tq_avg == 0)
1841 goto next;
1842
1843 last_seen_jiffies = jiffies - orig_node->last_seen;
1844 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1845 last_seen_secs = last_seen_msecs / 1000;
1846 last_seen_msecs = last_seen_msecs % 1000;
1847
1848 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
1849 orig_node->orig, last_seen_secs,
1850 last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
1851 neigh_node->addr,
1852 neigh_node->if_incoming->net_dev->name);
1853
1854 batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
1855 seq);
1856 seq_puts(seq, "\n");
1857 batman_count++;
1858
1859 next:
1860 batadv_neigh_node_free_ref(neigh_node);
1861 if (n_ifinfo)
1862 batadv_neigh_ifinfo_free_ref(n_ifinfo);
1863 }
1864 rcu_read_unlock();
1865 }
1866
1867 if (batman_count == 0)
1868 seq_puts(seq, "No batman nodes in range ...\n");
1869 }
1870
1871 /**
1872 * batadv_iv_hardif_neigh_print - print a single hop neighbour node
1873 * @seq: neighbour table seq_file struct
1874 * @hardif_neigh: hardif neighbour information
1875 */
1876 static void
1877 batadv_iv_hardif_neigh_print(struct seq_file *seq,
1878 struct batadv_hardif_neigh_node *hardif_neigh)
1879 {
1880 int last_secs, last_msecs;
1881
1882 last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
1883 last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
1884
1885 seq_printf(seq, " %10s %pM %4i.%03is\n",
1886 hardif_neigh->if_incoming->net_dev->name,
1887 hardif_neigh->addr, last_secs, last_msecs);
1888 }
1889
1890 /**
1891 * batadv_iv_ogm_neigh_print - print the single hop neighbour list
1892 * @bat_priv: the bat priv with all the soft interface information
1893 * @seq: neighbour table seq_file struct
1894 */
1895 static void batadv_iv_neigh_print(struct batadv_priv *bat_priv,
1896 struct seq_file *seq)
1897 {
1898 struct net_device *net_dev = (struct net_device *)seq->private;
1899 struct batadv_hardif_neigh_node *hardif_neigh;
1900 struct batadv_hard_iface *hard_iface;
1901 int batman_count = 0;
1902
1903 seq_printf(seq, " %10s %-13s %s\n",
1904 "IF", "Neighbor", "last-seen");
1905
1906 rcu_read_lock();
1907 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1908 if (hard_iface->soft_iface != net_dev)
1909 continue;
1910
1911 hlist_for_each_entry_rcu(hardif_neigh,
1912 &hard_iface->neigh_list, list) {
1913 batadv_iv_hardif_neigh_print(seq, hardif_neigh);
1914 batman_count++;
1915 }
1916 }
1917 rcu_read_unlock();
1918
1919 if (batman_count == 0)
1920 seq_puts(seq, "No batman nodes in range ...\n");
1921 }
1922
1923 /**
1924 * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
1925 * @neigh1: the first neighbor object of the comparison
1926 * @if_outgoing1: outgoing interface for the first neighbor
1927 * @neigh2: the second neighbor object of the comparison
1928 * @if_outgoing2: outgoing interface for the second neighbor
1929 *
1930 * Returns a value less, equal to or greater than 0 if the metric via neigh1 is
1931 * lower, the same as or higher than the metric via neigh2
1932 */
1933 static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
1934 struct batadv_hard_iface *if_outgoing1,
1935 struct batadv_neigh_node *neigh2,
1936 struct batadv_hard_iface *if_outgoing2)
1937 {
1938 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
1939 u8 tq1, tq2;
1940 int diff;
1941
1942 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
1943 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
1944
1945 if (!neigh1_ifinfo || !neigh2_ifinfo) {
1946 diff = 0;
1947 goto out;
1948 }
1949
1950 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
1951 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
1952 diff = tq1 - tq2;
1953
1954 out:
1955 if (neigh1_ifinfo)
1956 batadv_neigh_ifinfo_free_ref(neigh1_ifinfo);
1957 if (neigh2_ifinfo)
1958 batadv_neigh_ifinfo_free_ref(neigh2_ifinfo);
1959
1960 return diff;
1961 }
1962
1963 /**
1964 * batadv_iv_ogm_neigh_is_sob - check if neigh1 is similarly good or better
1965 * than neigh2 from the metric prospective
1966 * @neigh1: the first neighbor object of the comparison
1967 * @if_outgoing1: outgoing interface for the first neighbor
1968 * @neigh2: the second neighbor object of the comparison
1969 * @if_outgoing2: outgoing interface for the second neighbor
1970 *
1971 * Returns true if the metric via neigh1 is equally good or better than
1972 * the metric via neigh2, false otherwise.
1973 */
1974 static bool
1975 batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
1976 struct batadv_hard_iface *if_outgoing1,
1977 struct batadv_neigh_node *neigh2,
1978 struct batadv_hard_iface *if_outgoing2)
1979 {
1980 struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
1981 u8 tq1, tq2;
1982 bool ret;
1983
1984 neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
1985 neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
1986
1987 /* we can't say that the metric is better */
1988 if (!neigh1_ifinfo || !neigh2_ifinfo) {
1989 ret = false;
1990 goto out;
1991 }
1992
1993 tq1 = neigh1_ifinfo->bat_iv.tq_avg;
1994 tq2 = neigh2_ifinfo->bat_iv.tq_avg;
1995 ret = (tq1 - tq2) > -BATADV_TQ_SIMILARITY_THRESHOLD;
1996
1997 out:
1998 if (neigh1_ifinfo)
1999 batadv_neigh_ifinfo_free_ref(neigh1_ifinfo);
2000 if (neigh2_ifinfo)
2001 batadv_neigh_ifinfo_free_ref(neigh2_ifinfo);
2002
2003 return ret;
2004 }
2005
2006 static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
2007 .name = "BATMAN_IV",
2008 .bat_iface_enable = batadv_iv_ogm_iface_enable,
2009 .bat_iface_disable = batadv_iv_ogm_iface_disable,
2010 .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac,
2011 .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set,
2012 .bat_ogm_schedule = batadv_iv_ogm_schedule,
2013 .bat_ogm_emit = batadv_iv_ogm_emit,
2014 .bat_neigh_cmp = batadv_iv_ogm_neigh_cmp,
2015 .bat_neigh_is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
2016 .bat_neigh_print = batadv_iv_neigh_print,
2017 .bat_orig_print = batadv_iv_ogm_orig_print,
2018 .bat_orig_free = batadv_iv_ogm_orig_free,
2019 .bat_orig_add_if = batadv_iv_ogm_orig_add_if,
2020 .bat_orig_del_if = batadv_iv_ogm_orig_del_if,
2021 };
2022
2023 int __init batadv_iv_init(void)
2024 {
2025 int ret;
2026
2027 /* batman originator packet */
2028 ret = batadv_recv_handler_register(BATADV_IV_OGM,
2029 batadv_iv_ogm_receive);
2030 if (ret < 0)
2031 goto out;
2032
2033 ret = batadv_algo_register(&batadv_batman_iv);
2034 if (ret < 0)
2035 goto handler_unregister;
2036
2037 goto out;
2038
2039 handler_unregister:
2040 batadv_recv_handler_unregister(BATADV_IV_OGM);
2041 out:
2042 return ret;
2043 }
This page took 0.077725 seconds and 5 git commands to generate.