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