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