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