1
2
3
4
5
6
7#include "bat_v_ogm.h"
8#include "main.h"
9
10#include <linux/atomic.h>
11#include <linux/byteorder/generic.h>
12#include <linux/errno.h>
13#include <linux/etherdevice.h>
14#include <linux/gfp.h>
15#include <linux/if_ether.h>
16#include <linux/jiffies.h>
17#include <linux/kernel.h>
18#include <linux/kref.h>
19#include <linux/list.h>
20#include <linux/lockdep.h>
21#include <linux/mutex.h>
22#include <linux/netdevice.h>
23#include <linux/random.h>
24#include <linux/rculist.h>
25#include <linux/rcupdate.h>
26#include <linux/skbuff.h>
27#include <linux/slab.h>
28#include <linux/spinlock.h>
29#include <linux/stddef.h>
30#include <linux/string.h>
31#include <linux/types.h>
32#include <linux/workqueue.h>
33#include <uapi/linux/batadv_packet.h>
34
35#include "bat_algo.h"
36#include "hard-interface.h"
37#include "hash.h"
38#include "log.h"
39#include "originator.h"
40#include "routing.h"
41#include "send.h"
42#include "translation-table.h"
43#include "tvlv.h"
44
45
46
47
48
49
50
51
52
53
54struct batadv_orig_node *batadv_v_ogm_orig_get(struct batadv_priv *bat_priv,
55 const u8 *addr)
56{
57 struct batadv_orig_node *orig_node;
58 int hash_added;
59
60 orig_node = batadv_orig_hash_find(bat_priv, addr);
61 if (orig_node)
62 return orig_node;
63
64 orig_node = batadv_orig_node_new(bat_priv, addr);
65 if (!orig_node)
66 return NULL;
67
68 kref_get(&orig_node->refcount);
69 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
70 batadv_choose_orig, orig_node,
71 &orig_node->hash_entry);
72 if (hash_added != 0) {
73
74 batadv_orig_node_put(orig_node);
75 batadv_orig_node_put(orig_node);
76 orig_node = NULL;
77 }
78
79 return orig_node;
80}
81
82
83
84
85
86static void batadv_v_ogm_start_queue_timer(struct batadv_hard_iface *hard_iface)
87{
88 unsigned int msecs = BATADV_MAX_AGGREGATION_MS * 1000;
89
90
91 msecs += prandom_u32_max(msecs / 5) - (msecs / 10);
92 queue_delayed_work(batadv_event_workqueue, &hard_iface->bat_v.aggr_wq,
93 msecs_to_jiffies(msecs / 1000));
94}
95
96
97
98
99
100static void batadv_v_ogm_start_timer(struct batadv_priv *bat_priv)
101{
102 unsigned long msecs;
103
104
105
106 if (delayed_work_pending(&bat_priv->bat_v.ogm_wq))
107 return;
108
109 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
110 msecs += prandom_u32_max(2 * BATADV_JITTER);
111 queue_delayed_work(batadv_event_workqueue, &bat_priv->bat_v.ogm_wq,
112 msecs_to_jiffies(msecs));
113}
114
115
116
117
118
119
120static void batadv_v_ogm_send_to_if(struct sk_buff *skb,
121 struct batadv_hard_iface *hard_iface)
122{
123 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
124
125 if (hard_iface->if_status != BATADV_IF_ACTIVE)
126 return;
127
128 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
129 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
130 skb->len + ETH_HLEN);
131
132 batadv_send_broadcast_skb(skb, hard_iface);
133}
134
135
136
137
138
139
140
141
142static unsigned int batadv_v_ogm_len(struct sk_buff *skb)
143{
144 struct batadv_ogm2_packet *ogm_packet;
145
146 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
147 return BATADV_OGM2_HLEN + ntohs(ogm_packet->tvlv_len);
148}
149
150
151
152
153
154
155
156
157
158
159static bool batadv_v_ogm_queue_left(struct sk_buff *skb,
160 struct batadv_hard_iface *hard_iface)
161{
162 unsigned int max = min_t(unsigned int, hard_iface->net_dev->mtu,
163 BATADV_MAX_AGGREGATION_BYTES);
164 unsigned int ogm_len = batadv_v_ogm_len(skb);
165
166 lockdep_assert_held(&hard_iface->bat_v.aggr_list.lock);
167
168 return hard_iface->bat_v.aggr_len + ogm_len <= max;
169}
170
171
172
173
174
175
176
177
178
179static void batadv_v_ogm_aggr_list_free(struct batadv_hard_iface *hard_iface)
180{
181 lockdep_assert_held(&hard_iface->bat_v.aggr_list.lock);
182
183 __skb_queue_purge(&hard_iface->bat_v.aggr_list);
184 hard_iface->bat_v.aggr_len = 0;
185}
186
187
188
189
190
191
192
193
194
195
196
197
198static void batadv_v_ogm_aggr_send(struct batadv_hard_iface *hard_iface)
199{
200 unsigned int aggr_len = hard_iface->bat_v.aggr_len;
201 struct sk_buff *skb_aggr;
202 unsigned int ogm_len;
203 struct sk_buff *skb;
204
205 lockdep_assert_held(&hard_iface->bat_v.aggr_list.lock);
206
207 if (!aggr_len)
208 return;
209
210 skb_aggr = dev_alloc_skb(aggr_len + ETH_HLEN + NET_IP_ALIGN);
211 if (!skb_aggr) {
212 batadv_v_ogm_aggr_list_free(hard_iface);
213 return;
214 }
215
216 skb_reserve(skb_aggr, ETH_HLEN + NET_IP_ALIGN);
217 skb_reset_network_header(skb_aggr);
218
219 while ((skb = __skb_dequeue(&hard_iface->bat_v.aggr_list))) {
220 hard_iface->bat_v.aggr_len -= batadv_v_ogm_len(skb);
221
222 ogm_len = batadv_v_ogm_len(skb);
223 skb_put_data(skb_aggr, skb->data, ogm_len);
224
225 consume_skb(skb);
226 }
227
228 batadv_v_ogm_send_to_if(skb_aggr, hard_iface);
229}
230
231
232
233
234
235
236static void batadv_v_ogm_queue_on_if(struct sk_buff *skb,
237 struct batadv_hard_iface *hard_iface)
238{
239 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
240
241 if (!atomic_read(&bat_priv->aggregated_ogms)) {
242 batadv_v_ogm_send_to_if(skb, hard_iface);
243 return;
244 }
245
246 spin_lock_bh(&hard_iface->bat_v.aggr_list.lock);
247 if (!batadv_v_ogm_queue_left(skb, hard_iface))
248 batadv_v_ogm_aggr_send(hard_iface);
249
250 hard_iface->bat_v.aggr_len += batadv_v_ogm_len(skb);
251 __skb_queue_tail(&hard_iface->bat_v.aggr_list, skb);
252 spin_unlock_bh(&hard_iface->bat_v.aggr_list.lock);
253}
254
255
256
257
258
259static void batadv_v_ogm_send_softif(struct batadv_priv *bat_priv)
260{
261 struct batadv_hard_iface *hard_iface;
262 struct batadv_ogm2_packet *ogm_packet;
263 struct sk_buff *skb, *skb_tmp;
264 unsigned char *ogm_buff;
265 int ogm_buff_len;
266 u16 tvlv_len = 0;
267 int ret;
268
269 lockdep_assert_held(&bat_priv->bat_v.ogm_buff_mutex);
270
271 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
272 goto out;
273
274 ogm_buff = bat_priv->bat_v.ogm_buff;
275 ogm_buff_len = bat_priv->bat_v.ogm_buff_len;
276
277
278
279 batadv_tt_local_commit_changes(bat_priv);
280 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, &ogm_buff,
281 &ogm_buff_len,
282 BATADV_OGM2_HLEN);
283
284 bat_priv->bat_v.ogm_buff = ogm_buff;
285 bat_priv->bat_v.ogm_buff_len = ogm_buff_len;
286
287 skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + ogm_buff_len);
288 if (!skb)
289 goto reschedule;
290
291 skb_reserve(skb, ETH_HLEN);
292 skb_put_data(skb, ogm_buff, ogm_buff_len);
293
294 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
295 ogm_packet->seqno = htonl(atomic_read(&bat_priv->bat_v.ogm_seqno));
296 atomic_inc(&bat_priv->bat_v.ogm_seqno);
297 ogm_packet->tvlv_len = htons(tvlv_len);
298
299
300 rcu_read_lock();
301 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
302 if (hard_iface->soft_iface != bat_priv->soft_iface)
303 continue;
304
305 if (!kref_get_unless_zero(&hard_iface->refcount))
306 continue;
307
308 ret = batadv_hardif_no_broadcast(hard_iface, NULL, NULL);
309 if (ret) {
310 char *type;
311
312 switch (ret) {
313 case BATADV_HARDIF_BCAST_NORECIPIENT:
314 type = "no neighbor";
315 break;
316 case BATADV_HARDIF_BCAST_DUPFWD:
317 type = "single neighbor is source";
318 break;
319 case BATADV_HARDIF_BCAST_DUPORIG:
320 type = "single neighbor is originator";
321 break;
322 default:
323 type = "unknown";
324 }
325
326 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 from ourselves on %s suppressed: %s\n",
327 hard_iface->net_dev->name, type);
328
329 batadv_hardif_put(hard_iface);
330 continue;
331 }
332
333 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
334 "Sending own OGM2 packet (originator %pM, seqno %u, throughput %u, TTL %d) on interface %s [%pM]\n",
335 ogm_packet->orig, ntohl(ogm_packet->seqno),
336 ntohl(ogm_packet->throughput), ogm_packet->ttl,
337 hard_iface->net_dev->name,
338 hard_iface->net_dev->dev_addr);
339
340
341 skb_tmp = skb_clone(skb, GFP_ATOMIC);
342 if (!skb_tmp) {
343 batadv_hardif_put(hard_iface);
344 break;
345 }
346
347 batadv_v_ogm_queue_on_if(skb_tmp, hard_iface);
348 batadv_hardif_put(hard_iface);
349 }
350 rcu_read_unlock();
351
352 consume_skb(skb);
353
354reschedule:
355 batadv_v_ogm_start_timer(bat_priv);
356out:
357 return;
358}
359
360
361
362
363
364static void batadv_v_ogm_send(struct work_struct *work)
365{
366 struct batadv_priv_bat_v *bat_v;
367 struct batadv_priv *bat_priv;
368
369 bat_v = container_of(work, struct batadv_priv_bat_v, ogm_wq.work);
370 bat_priv = container_of(bat_v, struct batadv_priv, bat_v);
371
372 mutex_lock(&bat_priv->bat_v.ogm_buff_mutex);
373 batadv_v_ogm_send_softif(bat_priv);
374 mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex);
375}
376
377
378
379
380
381
382
383void batadv_v_ogm_aggr_work(struct work_struct *work)
384{
385 struct batadv_hard_iface_bat_v *batv;
386 struct batadv_hard_iface *hard_iface;
387
388 batv = container_of(work, struct batadv_hard_iface_bat_v, aggr_wq.work);
389 hard_iface = container_of(batv, struct batadv_hard_iface, bat_v);
390
391 spin_lock_bh(&hard_iface->bat_v.aggr_list.lock);
392 batadv_v_ogm_aggr_send(hard_iface);
393 spin_unlock_bh(&hard_iface->bat_v.aggr_list.lock);
394
395 batadv_v_ogm_start_queue_timer(hard_iface);
396}
397
398
399
400
401
402
403
404
405
406int batadv_v_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
407{
408 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
409
410 batadv_v_ogm_start_queue_timer(hard_iface);
411 batadv_v_ogm_start_timer(bat_priv);
412
413 return 0;
414}
415
416
417
418
419
420void batadv_v_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
421{
422 cancel_delayed_work_sync(&hard_iface->bat_v.aggr_wq);
423
424 spin_lock_bh(&hard_iface->bat_v.aggr_list.lock);
425 batadv_v_ogm_aggr_list_free(hard_iface);
426 spin_unlock_bh(&hard_iface->bat_v.aggr_list.lock);
427}
428
429
430
431
432
433void batadv_v_ogm_primary_iface_set(struct batadv_hard_iface *primary_iface)
434{
435 struct batadv_priv *bat_priv = netdev_priv(primary_iface->soft_iface);
436 struct batadv_ogm2_packet *ogm_packet;
437
438 mutex_lock(&bat_priv->bat_v.ogm_buff_mutex);
439 if (!bat_priv->bat_v.ogm_buff)
440 goto unlock;
441
442 ogm_packet = (struct batadv_ogm2_packet *)bat_priv->bat_v.ogm_buff;
443 ether_addr_copy(ogm_packet->orig, primary_iface->net_dev->dev_addr);
444
445unlock:
446 mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex);
447}
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472static u32 batadv_v_forward_penalty(struct batadv_priv *bat_priv,
473 struct batadv_hard_iface *if_incoming,
474 struct batadv_hard_iface *if_outgoing,
475 u32 throughput)
476{
477 int if_hop_penalty = atomic_read(&if_incoming->hop_penalty);
478 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
479 int hop_penalty_max = BATADV_TQ_MAX_VALUE;
480
481
482 throughput = throughput * (hop_penalty_max - if_hop_penalty) /
483 hop_penalty_max;
484
485
486 if (if_outgoing == BATADV_IF_DEFAULT)
487 return throughput;
488
489
490
491
492
493 if (throughput > 10 &&
494 if_incoming == if_outgoing &&
495 !(if_incoming->bat_v.flags & BATADV_FULL_DUPLEX))
496 return throughput / 2;
497
498
499 return throughput * (hop_penalty_max - hop_penalty) / hop_penalty_max;
500}
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515static void batadv_v_ogm_forward(struct batadv_priv *bat_priv,
516 const struct batadv_ogm2_packet *ogm_received,
517 struct batadv_orig_node *orig_node,
518 struct batadv_neigh_node *neigh_node,
519 struct batadv_hard_iface *if_incoming,
520 struct batadv_hard_iface *if_outgoing)
521{
522 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
523 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
524 struct batadv_neigh_node *router = NULL;
525 struct batadv_ogm2_packet *ogm_forward;
526 unsigned char *skb_buff;
527 struct sk_buff *skb;
528 size_t packet_len;
529 u16 tvlv_len;
530
531
532 if (if_outgoing == BATADV_IF_DEFAULT)
533 goto out;
534
535 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
536 if (!orig_ifinfo)
537 goto out;
538
539
540 router = batadv_orig_router_get(orig_node, if_outgoing);
541
542
543 if (neigh_node != router)
544 goto out;
545
546
547 if (orig_ifinfo->last_seqno_forwarded == ntohl(ogm_received->seqno))
548 goto out;
549
550 orig_ifinfo->last_seqno_forwarded = ntohl(ogm_received->seqno);
551
552 if (ogm_received->ttl <= 1) {
553 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
554 goto out;
555 }
556
557 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
558 if (!neigh_ifinfo)
559 goto out;
560
561 tvlv_len = ntohs(ogm_received->tvlv_len);
562
563 packet_len = BATADV_OGM2_HLEN + tvlv_len;
564 skb = netdev_alloc_skb_ip_align(if_outgoing->net_dev,
565 ETH_HLEN + packet_len);
566 if (!skb)
567 goto out;
568
569 skb_reserve(skb, ETH_HLEN);
570 skb_buff = skb_put_data(skb, ogm_received, packet_len);
571
572
573 ogm_forward = (struct batadv_ogm2_packet *)skb_buff;
574 ogm_forward->throughput = htonl(neigh_ifinfo->bat_v.throughput);
575 ogm_forward->ttl--;
576
577 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
578 "Forwarding OGM2 packet on %s: throughput %u, ttl %u, received via %s\n",
579 if_outgoing->net_dev->name, ntohl(ogm_forward->throughput),
580 ogm_forward->ttl, if_incoming->net_dev->name);
581
582 batadv_v_ogm_queue_on_if(skb, if_outgoing);
583
584out:
585 if (orig_ifinfo)
586 batadv_orig_ifinfo_put(orig_ifinfo);
587 if (router)
588 batadv_neigh_node_put(router);
589 if (neigh_ifinfo)
590 batadv_neigh_ifinfo_put(neigh_ifinfo);
591}
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607static int batadv_v_ogm_metric_update(struct batadv_priv *bat_priv,
608 const struct batadv_ogm2_packet *ogm2,
609 struct batadv_orig_node *orig_node,
610 struct batadv_neigh_node *neigh_node,
611 struct batadv_hard_iface *if_incoming,
612 struct batadv_hard_iface *if_outgoing)
613{
614 struct batadv_orig_ifinfo *orig_ifinfo;
615 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
616 bool protection_started = false;
617 int ret = -EINVAL;
618 u32 path_throughput;
619 s32 seq_diff;
620
621 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
622 if (!orig_ifinfo)
623 goto out;
624
625 seq_diff = ntohl(ogm2->seqno) - orig_ifinfo->last_real_seqno;
626
627 if (!hlist_empty(&orig_node->neigh_list) &&
628 batadv_window_protected(bat_priv, seq_diff,
629 BATADV_OGM_MAX_AGE,
630 &orig_ifinfo->batman_seqno_reset,
631 &protection_started)) {
632 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
633 "Drop packet: packet within window protection time from %pM\n",
634 ogm2->orig);
635 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
636 "Last reset: %ld, %ld\n",
637 orig_ifinfo->batman_seqno_reset, jiffies);
638 goto out;
639 }
640
641
642
643
644 if (seq_diff < 0 && !protection_started)
645 goto out;
646
647 neigh_node->last_seen = jiffies;
648
649 orig_node->last_seen = jiffies;
650
651 orig_ifinfo->last_real_seqno = ntohl(ogm2->seqno);
652 orig_ifinfo->last_ttl = ogm2->ttl;
653
654 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
655 if (!neigh_ifinfo)
656 goto out;
657
658 path_throughput = batadv_v_forward_penalty(bat_priv, if_incoming,
659 if_outgoing,
660 ntohl(ogm2->throughput));
661 neigh_ifinfo->bat_v.throughput = path_throughput;
662 neigh_ifinfo->bat_v.last_seqno = ntohl(ogm2->seqno);
663 neigh_ifinfo->last_ttl = ogm2->ttl;
664
665 if (seq_diff > 0 || protection_started)
666 ret = 1;
667 else
668 ret = 0;
669out:
670 if (orig_ifinfo)
671 batadv_orig_ifinfo_put(orig_ifinfo);
672 if (neigh_ifinfo)
673 batadv_neigh_ifinfo_put(neigh_ifinfo);
674
675 return ret;
676}
677
678
679
680
681
682
683
684
685
686
687
688
689
690static bool batadv_v_ogm_route_update(struct batadv_priv *bat_priv,
691 const struct ethhdr *ethhdr,
692 const struct batadv_ogm2_packet *ogm2,
693 struct batadv_orig_node *orig_node,
694 struct batadv_neigh_node *neigh_node,
695 struct batadv_hard_iface *if_incoming,
696 struct batadv_hard_iface *if_outgoing)
697{
698 struct batadv_neigh_node *router = NULL;
699 struct batadv_orig_node *orig_neigh_node;
700 struct batadv_neigh_node *orig_neigh_router = NULL;
701 struct batadv_neigh_ifinfo *router_ifinfo = NULL, *neigh_ifinfo = NULL;
702 u32 router_throughput, neigh_throughput;
703 u32 router_last_seqno;
704 u32 neigh_last_seqno;
705 s32 neigh_seq_diff;
706 bool forward = false;
707
708 orig_neigh_node = batadv_v_ogm_orig_get(bat_priv, ethhdr->h_source);
709 if (!orig_neigh_node)
710 goto out;
711
712 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
713 if_outgoing);
714
715
716
717
718 router = batadv_orig_router_get(orig_node, if_outgoing);
719 if (router && router->orig_node != orig_node && !orig_neigh_router) {
720 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
721 "Drop packet: OGM via unknown neighbor!\n");
722 goto out;
723 }
724
725
726
727
728 forward = true;
729
730 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
731 "Searching and updating originator entry of received packet\n");
732
733
734
735
736 if (router == neigh_node)
737 goto out;
738
739
740
741
742
743 if (router) {
744 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
745 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
746
747
748 if (!router_ifinfo || !neigh_ifinfo)
749 goto out;
750
751 neigh_last_seqno = neigh_ifinfo->bat_v.last_seqno;
752 router_last_seqno = router_ifinfo->bat_v.last_seqno;
753 neigh_seq_diff = neigh_last_seqno - router_last_seqno;
754 router_throughput = router_ifinfo->bat_v.throughput;
755 neigh_throughput = neigh_ifinfo->bat_v.throughput;
756
757 if (neigh_seq_diff < BATADV_OGM_MAX_ORIGDIFF &&
758 router_throughput >= neigh_throughput)
759 goto out;
760 }
761
762 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
763out:
764 if (router)
765 batadv_neigh_node_put(router);
766 if (orig_neigh_router)
767 batadv_neigh_node_put(orig_neigh_router);
768 if (orig_neigh_node)
769 batadv_orig_node_put(orig_neigh_node);
770 if (router_ifinfo)
771 batadv_neigh_ifinfo_put(router_ifinfo);
772 if (neigh_ifinfo)
773 batadv_neigh_ifinfo_put(neigh_ifinfo);
774
775 return forward;
776}
777
778
779
780
781
782
783
784
785
786
787
788static void
789batadv_v_ogm_process_per_outif(struct batadv_priv *bat_priv,
790 const struct ethhdr *ethhdr,
791 const struct batadv_ogm2_packet *ogm2,
792 struct batadv_orig_node *orig_node,
793 struct batadv_neigh_node *neigh_node,
794 struct batadv_hard_iface *if_incoming,
795 struct batadv_hard_iface *if_outgoing)
796{
797 int seqno_age;
798 bool forward;
799
800
801 seqno_age = batadv_v_ogm_metric_update(bat_priv, ogm2, orig_node,
802 neigh_node, if_incoming,
803 if_outgoing);
804
805
806 if (seqno_age < 0)
807 return;
808
809
810 if (seqno_age > 0 && if_outgoing == BATADV_IF_DEFAULT)
811 batadv_tvlv_containers_process(bat_priv, true, orig_node,
812 NULL, NULL,
813 (unsigned char *)(ogm2 + 1),
814 ntohs(ogm2->tvlv_len));
815
816
817 forward = batadv_v_ogm_route_update(bat_priv, ethhdr, ogm2, orig_node,
818 neigh_node, if_incoming,
819 if_outgoing);
820
821
822 if (forward)
823 batadv_v_ogm_forward(bat_priv, ogm2, orig_node, neigh_node,
824 if_incoming, if_outgoing);
825}
826
827
828
829
830
831
832
833
834
835static bool
836batadv_v_ogm_aggr_packet(int buff_pos, int packet_len,
837 const struct batadv_ogm2_packet *ogm2_packet)
838{
839 int next_buff_pos = 0;
840
841
842 next_buff_pos += buff_pos + sizeof(*ogm2_packet);
843 if (next_buff_pos > packet_len)
844 return false;
845
846
847 next_buff_pos += ntohs(ogm2_packet->tvlv_len);
848
849 return (next_buff_pos <= packet_len) &&
850 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
851}
852
853
854
855
856
857
858
859static void batadv_v_ogm_process(const struct sk_buff *skb, int ogm_offset,
860 struct batadv_hard_iface *if_incoming)
861{
862 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
863 struct ethhdr *ethhdr;
864 struct batadv_orig_node *orig_node = NULL;
865 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
866 struct batadv_neigh_node *neigh_node = NULL;
867 struct batadv_hard_iface *hard_iface;
868 struct batadv_ogm2_packet *ogm_packet;
869 u32 ogm_throughput, link_throughput, path_throughput;
870 int ret;
871
872 ethhdr = eth_hdr(skb);
873 ogm_packet = (struct batadv_ogm2_packet *)(skb->data + ogm_offset);
874
875 ogm_throughput = ntohl(ogm_packet->throughput);
876
877 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
878 "Received OGM2 packet via NB: %pM, IF: %s [%pM] (from OG: %pM, seqno %u, throughput %u, TTL %u, V %u, tvlv_len %u)\n",
879 ethhdr->h_source, if_incoming->net_dev->name,
880 if_incoming->net_dev->dev_addr, ogm_packet->orig,
881 ntohl(ogm_packet->seqno), ogm_throughput, ogm_packet->ttl,
882 ogm_packet->version, ntohs(ogm_packet->tvlv_len));
883
884 if (batadv_is_my_mac(bat_priv, ogm_packet->orig)) {
885 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
886 "Drop packet: originator packet from ourself\n");
887 return;
888 }
889
890
891
892
893 if (ogm_throughput == 0) {
894 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
895 "Drop packet: originator packet with throughput metric of 0\n");
896 return;
897 }
898
899
900 hardif_neigh = batadv_hardif_neigh_get(if_incoming, ethhdr->h_source);
901 if (!hardif_neigh) {
902 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
903 "Drop packet: OGM via unknown neighbor!\n");
904 goto out;
905 }
906
907 orig_node = batadv_v_ogm_orig_get(bat_priv, ogm_packet->orig);
908 if (!orig_node)
909 goto out;
910
911 neigh_node = batadv_neigh_node_get_or_create(orig_node, if_incoming,
912 ethhdr->h_source);
913 if (!neigh_node)
914 goto out;
915
916
917
918
919
920
921
922
923 link_throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
924 path_throughput = min_t(u32, link_throughput, ogm_throughput);
925 ogm_packet->throughput = htonl(path_throughput);
926
927 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet, orig_node,
928 neigh_node, if_incoming,
929 BATADV_IF_DEFAULT);
930
931 rcu_read_lock();
932 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
933 if (hard_iface->if_status != BATADV_IF_ACTIVE)
934 continue;
935
936 if (hard_iface->soft_iface != bat_priv->soft_iface)
937 continue;
938
939 if (!kref_get_unless_zero(&hard_iface->refcount))
940 continue;
941
942 ret = batadv_hardif_no_broadcast(hard_iface,
943 ogm_packet->orig,
944 hardif_neigh->orig);
945
946 if (ret) {
947 char *type;
948
949 switch (ret) {
950 case BATADV_HARDIF_BCAST_NORECIPIENT:
951 type = "no neighbor";
952 break;
953 case BATADV_HARDIF_BCAST_DUPFWD:
954 type = "single neighbor is source";
955 break;
956 case BATADV_HARDIF_BCAST_DUPORIG:
957 type = "single neighbor is originator";
958 break;
959 default:
960 type = "unknown";
961 }
962
963 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 packet from %pM on %s suppressed: %s\n",
964 ogm_packet->orig, hard_iface->net_dev->name,
965 type);
966
967 batadv_hardif_put(hard_iface);
968 continue;
969 }
970
971 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet,
972 orig_node, neigh_node,
973 if_incoming, hard_iface);
974
975 batadv_hardif_put(hard_iface);
976 }
977 rcu_read_unlock();
978out:
979 if (orig_node)
980 batadv_orig_node_put(orig_node);
981 if (neigh_node)
982 batadv_neigh_node_put(neigh_node);
983 if (hardif_neigh)
984 batadv_hardif_neigh_put(hardif_neigh);
985}
986
987
988
989
990
991
992
993
994
995int batadv_v_ogm_packet_recv(struct sk_buff *skb,
996 struct batadv_hard_iface *if_incoming)
997{
998 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
999 struct batadv_ogm2_packet *ogm_packet;
1000 struct ethhdr *ethhdr = eth_hdr(skb);
1001 int ogm_offset;
1002 u8 *packet_pos;
1003 int ret = NET_RX_DROP;
1004
1005
1006
1007
1008 if (strcmp(bat_priv->algo_ops->name, "BATMAN_V") != 0)
1009 goto free_skb;
1010
1011 if (!batadv_check_management_packet(skb, if_incoming, BATADV_OGM2_HLEN))
1012 goto free_skb;
1013
1014 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
1015 goto free_skb;
1016
1017 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1018 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
1019 skb->len + ETH_HLEN);
1020
1021 ogm_offset = 0;
1022 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
1023
1024 while (batadv_v_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
1025 ogm_packet)) {
1026 batadv_v_ogm_process(skb, ogm_offset, if_incoming);
1027
1028 ogm_offset += BATADV_OGM2_HLEN;
1029 ogm_offset += ntohs(ogm_packet->tvlv_len);
1030
1031 packet_pos = skb->data + ogm_offset;
1032 ogm_packet = (struct batadv_ogm2_packet *)packet_pos;
1033 }
1034
1035 ret = NET_RX_SUCCESS;
1036
1037free_skb:
1038 if (ret == NET_RX_SUCCESS)
1039 consume_skb(skb);
1040 else
1041 kfree_skb(skb);
1042
1043 return ret;
1044}
1045
1046
1047
1048
1049
1050
1051
1052int batadv_v_ogm_init(struct batadv_priv *bat_priv)
1053{
1054 struct batadv_ogm2_packet *ogm_packet;
1055 unsigned char *ogm_buff;
1056 u32 random_seqno;
1057
1058 bat_priv->bat_v.ogm_buff_len = BATADV_OGM2_HLEN;
1059 ogm_buff = kzalloc(bat_priv->bat_v.ogm_buff_len, GFP_ATOMIC);
1060 if (!ogm_buff)
1061 return -ENOMEM;
1062
1063 bat_priv->bat_v.ogm_buff = ogm_buff;
1064 ogm_packet = (struct batadv_ogm2_packet *)ogm_buff;
1065 ogm_packet->packet_type = BATADV_OGM2;
1066 ogm_packet->version = BATADV_COMPAT_VERSION;
1067 ogm_packet->ttl = BATADV_TTL;
1068 ogm_packet->flags = BATADV_NO_FLAGS;
1069 ogm_packet->throughput = htonl(BATADV_THROUGHPUT_MAX_VALUE);
1070
1071
1072 get_random_bytes(&random_seqno, sizeof(random_seqno));
1073 atomic_set(&bat_priv->bat_v.ogm_seqno, random_seqno);
1074 INIT_DELAYED_WORK(&bat_priv->bat_v.ogm_wq, batadv_v_ogm_send);
1075
1076 mutex_init(&bat_priv->bat_v.ogm_buff_mutex);
1077
1078 return 0;
1079}
1080
1081
1082
1083
1084
1085void batadv_v_ogm_free(struct batadv_priv *bat_priv)
1086{
1087 cancel_delayed_work_sync(&bat_priv->bat_v.ogm_wq);
1088
1089 mutex_lock(&bat_priv->bat_v.ogm_buff_mutex);
1090
1091 kfree(bat_priv->bat_v.ogm_buff);
1092 bat_priv->bat_v.ogm_buff = NULL;
1093 bat_priv->bat_v.ogm_buff_len = 0;
1094
1095 mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex);
1096}
1097