]> asedeno.scripts.mit.edu Git - linux.git/blob - net/batman-adv/bat_v_ogm.c
batman-adv: BATMAN_V: aggregate OGMv2 packets
[linux.git] / net / batman-adv / bat_v_ogm.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2013-2019  B.A.T.M.A.N. contributors:
3  *
4  * Antonio Quartulli
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/netdevice.h>
22 #include <linux/random.h>
23 #include <linux/rculist.h>
24 #include <linux/rcupdate.h>
25 #include <linux/skbuff.h>
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/stddef.h>
29 #include <linux/string.h>
30 #include <linux/types.h>
31 #include <linux/workqueue.h>
32 #include <uapi/linux/batadv_packet.h>
33
34 #include "bat_algo.h"
35 #include "hard-interface.h"
36 #include "hash.h"
37 #include "log.h"
38 #include "originator.h"
39 #include "routing.h"
40 #include "send.h"
41 #include "translation-table.h"
42 #include "tvlv.h"
43
44 /**
45  * batadv_v_ogm_orig_get() - retrieve and possibly create an originator node
46  * @bat_priv: the bat priv with all the soft interface information
47  * @addr: the address of the originator
48  *
49  * Return: the orig_node corresponding to the specified address. If such object
50  * does not exist it is allocated here. In case of allocation failure returns
51  * NULL.
52  */
53 struct batadv_orig_node *batadv_v_ogm_orig_get(struct batadv_priv *bat_priv,
54                                                const u8 *addr)
55 {
56         struct batadv_orig_node *orig_node;
57         int hash_added;
58
59         orig_node = batadv_orig_hash_find(bat_priv, addr);
60         if (orig_node)
61                 return orig_node;
62
63         orig_node = batadv_orig_node_new(bat_priv, addr);
64         if (!orig_node)
65                 return NULL;
66
67         kref_get(&orig_node->refcount);
68         hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
69                                      batadv_choose_orig, orig_node,
70                                      &orig_node->hash_entry);
71         if (hash_added != 0) {
72                 /* remove refcnt for newly created orig_node and hash entry */
73                 batadv_orig_node_put(orig_node);
74                 batadv_orig_node_put(orig_node);
75                 orig_node = NULL;
76         }
77
78         return orig_node;
79 }
80
81 /**
82  * batadv_v_ogm_start_queue_timer() - restart the OGM aggregation timer
83  * @hard_iface: the interface to use to send the OGM
84  */
85 static void batadv_v_ogm_start_queue_timer(struct batadv_hard_iface *hard_iface)
86 {
87         unsigned int msecs = BATADV_MAX_AGGREGATION_MS * 1000;
88
89         /* msecs * [0.9, 1.1] */
90         msecs += prandom_u32() % (msecs / 5) - (msecs / 10);
91         queue_delayed_work(batadv_event_workqueue, &hard_iface->bat_v.aggr_wq,
92                            msecs_to_jiffies(msecs / 1000));
93 }
94
95 /**
96  * batadv_v_ogm_start_timer() - restart the OGM sending timer
97  * @bat_priv: the bat priv with all the soft interface information
98  */
99 static void batadv_v_ogm_start_timer(struct batadv_priv *bat_priv)
100 {
101         unsigned long msecs;
102         /* this function may be invoked in different contexts (ogm rescheduling
103          * or hard_iface activation), but the work timer should not be reset
104          */
105         if (delayed_work_pending(&bat_priv->bat_v.ogm_wq))
106                 return;
107
108         msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
109         msecs += prandom_u32() % (2 * BATADV_JITTER);
110         queue_delayed_work(batadv_event_workqueue, &bat_priv->bat_v.ogm_wq,
111                            msecs_to_jiffies(msecs));
112 }
113
114 /**
115  * batadv_v_ogm_send_to_if() - send a batman ogm using a given interface
116  * @skb: the OGM to send
117  * @hard_iface: the interface to use to send the OGM
118  */
119 static void batadv_v_ogm_send_to_if(struct sk_buff *skb,
120                                     struct batadv_hard_iface *hard_iface)
121 {
122         struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
123
124         if (hard_iface->if_status != BATADV_IF_ACTIVE)
125                 return;
126
127         batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
128         batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
129                            skb->len + ETH_HLEN);
130
131         batadv_send_broadcast_skb(skb, hard_iface);
132 }
133
134 /**
135  * batadv_v_ogm_len() - OGMv2 packet length
136  * @skb: the OGM to check
137  *
138  * Return: Length of the given OGMv2 packet, including tvlv length, excluding
139  * ethernet header length.
140  */
141 static unsigned int batadv_v_ogm_len(struct sk_buff *skb)
142 {
143         struct batadv_ogm2_packet *ogm_packet;
144
145         ogm_packet = (struct batadv_ogm2_packet *)skb->data;
146         return BATADV_OGM2_HLEN + ntohs(ogm_packet->tvlv_len);
147 }
148
149 /**
150  * batadv_v_ogm_queue_left() - check if given OGM still fits aggregation queue
151  * @skb: the OGM to check
152  * @hard_iface: the interface to use to send the OGM
153  *
154  * Caller needs to hold the hard_iface->bat_v.aggr_list_lock.
155  *
156  * Return: True, if the given OGMv2 packet still fits, false otherwise.
157  */
158 static bool batadv_v_ogm_queue_left(struct sk_buff *skb,
159                                     struct batadv_hard_iface *hard_iface)
160 {
161         unsigned int max = min_t(unsigned int, hard_iface->net_dev->mtu,
162                                  BATADV_MAX_AGGREGATION_BYTES);
163         unsigned int ogm_len = batadv_v_ogm_len(skb);
164
165         lockdep_assert_held(&hard_iface->bat_v.aggr_list_lock);
166
167         return hard_iface->bat_v.aggr_len + ogm_len <= max;
168 }
169
170 /**
171  * batadv_v_ogm_aggr_list_free - free all elements in an aggregation queue
172  * @hard_iface: the interface holding the aggregation queue
173  *
174  * Empties the OGMv2 aggregation queue and frees all the skbs it contained.
175  *
176  * Caller needs to hold the hard_iface->bat_v.aggr_list_lock.
177  */
178 static void batadv_v_ogm_aggr_list_free(struct batadv_hard_iface *hard_iface)
179 {
180         struct sk_buff *skb;
181
182         lockdep_assert_held(&hard_iface->bat_v.aggr_list_lock);
183
184         while ((skb = skb_dequeue(&hard_iface->bat_v.aggr_list)))
185                 kfree_skb(skb);
186
187         hard_iface->bat_v.aggr_len = 0;
188 }
189
190 /**
191  * batadv_v_ogm_aggr_send() - flush & send aggregation queue
192  * @hard_iface: the interface with the aggregation queue to flush
193  *
194  * Aggregates all OGMv2 packets currently in the aggregation queue into a
195  * single OGMv2 packet and transmits this aggregate.
196  *
197  * The aggregation queue is empty after this call.
198  *
199  * Caller needs to hold the hard_iface->bat_v.aggr_list_lock.
200  */
201 static void batadv_v_ogm_aggr_send(struct batadv_hard_iface *hard_iface)
202 {
203         unsigned int aggr_len = hard_iface->bat_v.aggr_len;
204         struct sk_buff *skb_aggr;
205         unsigned int ogm_len;
206         struct sk_buff *skb;
207
208         lockdep_assert_held(&hard_iface->bat_v.aggr_list_lock);
209
210         if (!aggr_len)
211                 return;
212
213         skb_aggr = dev_alloc_skb(aggr_len + ETH_HLEN + NET_IP_ALIGN);
214         if (!skb_aggr) {
215                 batadv_v_ogm_aggr_list_free(hard_iface);
216                 return;
217         }
218
219         skb_reserve(skb_aggr, ETH_HLEN + NET_IP_ALIGN);
220         skb_reset_network_header(skb_aggr);
221
222         while ((skb = skb_dequeue(&hard_iface->bat_v.aggr_list))) {
223                 hard_iface->bat_v.aggr_len -= batadv_v_ogm_len(skb);
224
225                 ogm_len = batadv_v_ogm_len(skb);
226                 skb_put_data(skb_aggr, skb->data, ogm_len);
227
228                 consume_skb(skb);
229         }
230
231         batadv_v_ogm_send_to_if(skb_aggr, hard_iface);
232 }
233
234 /**
235  * batadv_v_ogm_queue_on_if() - queue a batman ogm on a given interface
236  * @skb: the OGM to queue
237  * @hard_iface: the interface to queue the OGM on
238  */
239 static void batadv_v_ogm_queue_on_if(struct sk_buff *skb,
240                                      struct batadv_hard_iface *hard_iface)
241 {
242         struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
243
244         if (!atomic_read(&bat_priv->aggregated_ogms)) {
245                 batadv_v_ogm_send_to_if(skb, hard_iface);
246                 return;
247         }
248
249         spin_lock_bh(&hard_iface->bat_v.aggr_list_lock);
250         if (!batadv_v_ogm_queue_left(skb, hard_iface))
251                 batadv_v_ogm_aggr_send(hard_iface);
252
253         hard_iface->bat_v.aggr_len += batadv_v_ogm_len(skb);
254         skb_queue_tail(&hard_iface->bat_v.aggr_list, skb);
255         spin_unlock_bh(&hard_iface->bat_v.aggr_list_lock);
256 }
257
258 /**
259  * batadv_v_ogm_send() - periodic worker broadcasting the own OGM
260  * @work: work queue item
261  */
262 static void batadv_v_ogm_send(struct work_struct *work)
263 {
264         struct batadv_hard_iface *hard_iface;
265         struct batadv_priv_bat_v *bat_v;
266         struct batadv_priv *bat_priv;
267         struct batadv_ogm2_packet *ogm_packet;
268         struct sk_buff *skb, *skb_tmp;
269         unsigned char *ogm_buff;
270         int ogm_buff_len;
271         u16 tvlv_len = 0;
272         int ret;
273
274         bat_v = container_of(work, struct batadv_priv_bat_v, ogm_wq.work);
275         bat_priv = container_of(bat_v, struct batadv_priv, bat_v);
276
277         if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
278                 goto out;
279
280         ogm_buff = bat_priv->bat_v.ogm_buff;
281         ogm_buff_len = bat_priv->bat_v.ogm_buff_len;
282         /* tt changes have to be committed before the tvlv data is
283          * appended as it may alter the tt tvlv container
284          */
285         batadv_tt_local_commit_changes(bat_priv);
286         tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, &ogm_buff,
287                                                     &ogm_buff_len,
288                                                     BATADV_OGM2_HLEN);
289
290         bat_priv->bat_v.ogm_buff = ogm_buff;
291         bat_priv->bat_v.ogm_buff_len = ogm_buff_len;
292
293         skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + ogm_buff_len);
294         if (!skb)
295                 goto reschedule;
296
297         skb_reserve(skb, ETH_HLEN);
298         skb_put_data(skb, ogm_buff, ogm_buff_len);
299
300         ogm_packet = (struct batadv_ogm2_packet *)skb->data;
301         ogm_packet->seqno = htonl(atomic_read(&bat_priv->bat_v.ogm_seqno));
302         atomic_inc(&bat_priv->bat_v.ogm_seqno);
303         ogm_packet->tvlv_len = htons(tvlv_len);
304
305         /* broadcast on every interface */
306         rcu_read_lock();
307         list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
308                 if (hard_iface->soft_iface != bat_priv->soft_iface)
309                         continue;
310
311                 if (!kref_get_unless_zero(&hard_iface->refcount))
312                         continue;
313
314                 ret = batadv_hardif_no_broadcast(hard_iface, NULL, NULL);
315                 if (ret) {
316                         char *type;
317
318                         switch (ret) {
319                         case BATADV_HARDIF_BCAST_NORECIPIENT:
320                                 type = "no neighbor";
321                                 break;
322                         case BATADV_HARDIF_BCAST_DUPFWD:
323                                 type = "single neighbor is source";
324                                 break;
325                         case BATADV_HARDIF_BCAST_DUPORIG:
326                                 type = "single neighbor is originator";
327                                 break;
328                         default:
329                                 type = "unknown";
330                         }
331
332                         batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 from ourselves on %s suppressed: %s\n",
333                                    hard_iface->net_dev->name, type);
334
335                         batadv_hardif_put(hard_iface);
336                         continue;
337                 }
338
339                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
340                            "Sending own OGM2 packet (originator %pM, seqno %u, throughput %u, TTL %d) on interface %s [%pM]\n",
341                            ogm_packet->orig, ntohl(ogm_packet->seqno),
342                            ntohl(ogm_packet->throughput), ogm_packet->ttl,
343                            hard_iface->net_dev->name,
344                            hard_iface->net_dev->dev_addr);
345
346                 /* this skb gets consumed by batadv_v_ogm_send_to_if() */
347                 skb_tmp = skb_clone(skb, GFP_ATOMIC);
348                 if (!skb_tmp) {
349                         batadv_hardif_put(hard_iface);
350                         break;
351                 }
352
353                 batadv_v_ogm_queue_on_if(skb_tmp, hard_iface);
354                 batadv_hardif_put(hard_iface);
355         }
356         rcu_read_unlock();
357
358         consume_skb(skb);
359
360 reschedule:
361         batadv_v_ogm_start_timer(bat_priv);
362 out:
363         return;
364 }
365
366 /**
367  * batadv_v_ogm_aggr_work() - OGM queue periodic task per interface
368  * @work: work queue item
369  *
370  * Emits aggregated OGM message in regular intervals.
371  */
372 void batadv_v_ogm_aggr_work(struct work_struct *work)
373 {
374         struct batadv_hard_iface_bat_v *batv;
375         struct batadv_hard_iface *hard_iface;
376
377         batv = container_of(work, struct batadv_hard_iface_bat_v, aggr_wq.work);
378         hard_iface = container_of(batv, struct batadv_hard_iface, bat_v);
379
380         spin_lock_bh(&hard_iface->bat_v.aggr_list_lock);
381         batadv_v_ogm_aggr_send(hard_iface);
382         spin_unlock_bh(&hard_iface->bat_v.aggr_list_lock);
383
384         batadv_v_ogm_start_queue_timer(hard_iface);
385 }
386
387 /**
388  * batadv_v_ogm_iface_enable() - prepare an interface for B.A.T.M.A.N. V
389  * @hard_iface: the interface to prepare
390  *
391  * Takes care of scheduling own OGM sending routine for this interface.
392  *
393  * Return: 0 on success or a negative error code otherwise
394  */
395 int batadv_v_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
396 {
397         struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
398
399         batadv_v_ogm_start_queue_timer(hard_iface);
400         batadv_v_ogm_start_timer(bat_priv);
401
402         return 0;
403 }
404
405 /**
406  * batadv_v_ogm_iface_disable() - release OGM interface private resources
407  * @hard_iface: interface for which the resources have to be released
408  */
409 void batadv_v_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
410 {
411         cancel_delayed_work_sync(&hard_iface->bat_v.aggr_wq);
412
413         spin_lock_bh(&hard_iface->bat_v.aggr_list_lock);
414         batadv_v_ogm_aggr_list_free(hard_iface);
415         spin_unlock_bh(&hard_iface->bat_v.aggr_list_lock);
416 }
417
418 /**
419  * batadv_v_ogm_primary_iface_set() - set a new primary interface
420  * @primary_iface: the new primary interface
421  */
422 void batadv_v_ogm_primary_iface_set(struct batadv_hard_iface *primary_iface)
423 {
424         struct batadv_priv *bat_priv = netdev_priv(primary_iface->soft_iface);
425         struct batadv_ogm2_packet *ogm_packet;
426
427         if (!bat_priv->bat_v.ogm_buff)
428                 return;
429
430         ogm_packet = (struct batadv_ogm2_packet *)bat_priv->bat_v.ogm_buff;
431         ether_addr_copy(ogm_packet->orig, primary_iface->net_dev->dev_addr);
432 }
433
434 /**
435  * batadv_v_forward_penalty() - apply a penalty to the throughput metric
436  *  forwarded with B.A.T.M.A.N. V OGMs
437  * @bat_priv: the bat priv with all the soft interface information
438  * @if_incoming: the interface where the OGM has been received
439  * @if_outgoing: the interface where the OGM has to be forwarded to
440  * @throughput: the current throughput
441  *
442  * Apply a penalty on the current throughput metric value based on the
443  * characteristic of the interface where the OGM has been received. The return
444  * value is computed as follows:
445  * - throughput * 50%          if the incoming and outgoing interface are the
446  *                             same WiFi interface and the throughput is above
447  *                             1MBit/s
448  * - throughput                if the outgoing interface is the default
449  *                             interface (i.e. this OGM is processed for the
450  *                             internal table and not forwarded)
451  * - throughput * hop penalty  otherwise
452  *
453  * Return: the penalised throughput metric.
454  */
455 static u32 batadv_v_forward_penalty(struct batadv_priv *bat_priv,
456                                     struct batadv_hard_iface *if_incoming,
457                                     struct batadv_hard_iface *if_outgoing,
458                                     u32 throughput)
459 {
460         int hop_penalty = atomic_read(&bat_priv->hop_penalty);
461         int hop_penalty_max = BATADV_TQ_MAX_VALUE;
462
463         /* Don't apply hop penalty in default originator table. */
464         if (if_outgoing == BATADV_IF_DEFAULT)
465                 return throughput;
466
467         /* Forwarding on the same WiFi interface cuts the throughput in half
468          * due to the store & forward characteristics of WIFI.
469          * Very low throughput values are the exception.
470          */
471         if (throughput > 10 &&
472             if_incoming == if_outgoing &&
473             !(if_incoming->bat_v.flags & BATADV_FULL_DUPLEX))
474                 return throughput / 2;
475
476         /* hop penalty of 255 equals 100% */
477         return throughput * (hop_penalty_max - hop_penalty) / hop_penalty_max;
478 }
479
480 /**
481  * batadv_v_ogm_forward() - check conditions and forward an OGM to the given
482  *  outgoing interface
483  * @bat_priv: the bat priv with all the soft interface information
484  * @ogm_received: previously received OGM to be forwarded
485  * @orig_node: the originator which has been updated
486  * @neigh_node: the neigh_node through with the OGM has been received
487  * @if_incoming: the interface on which this OGM was received on
488  * @if_outgoing: the interface to which the OGM has to be forwarded to
489  *
490  * Forward an OGM to an interface after having altered the throughput metric and
491  * the TTL value contained in it. The original OGM isn't modified.
492  */
493 static void batadv_v_ogm_forward(struct batadv_priv *bat_priv,
494                                  const struct batadv_ogm2_packet *ogm_received,
495                                  struct batadv_orig_node *orig_node,
496                                  struct batadv_neigh_node *neigh_node,
497                                  struct batadv_hard_iface *if_incoming,
498                                  struct batadv_hard_iface *if_outgoing)
499 {
500         struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
501         struct batadv_orig_ifinfo *orig_ifinfo = NULL;
502         struct batadv_neigh_node *router = NULL;
503         struct batadv_ogm2_packet *ogm_forward;
504         unsigned char *skb_buff;
505         struct sk_buff *skb;
506         size_t packet_len;
507         u16 tvlv_len;
508
509         /* only forward for specific interfaces, not for the default one. */
510         if (if_outgoing == BATADV_IF_DEFAULT)
511                 goto out;
512
513         orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
514         if (!orig_ifinfo)
515                 goto out;
516
517         /* acquire possibly updated router */
518         router = batadv_orig_router_get(orig_node, if_outgoing);
519
520         /* strict rule: forward packets coming from the best next hop only */
521         if (neigh_node != router)
522                 goto out;
523
524         /* don't forward the same seqno twice on one interface */
525         if (orig_ifinfo->last_seqno_forwarded == ntohl(ogm_received->seqno))
526                 goto out;
527
528         orig_ifinfo->last_seqno_forwarded = ntohl(ogm_received->seqno);
529
530         if (ogm_received->ttl <= 1) {
531                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
532                 goto out;
533         }
534
535         neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
536         if (!neigh_ifinfo)
537                 goto out;
538
539         tvlv_len = ntohs(ogm_received->tvlv_len);
540
541         packet_len = BATADV_OGM2_HLEN + tvlv_len;
542         skb = netdev_alloc_skb_ip_align(if_outgoing->net_dev,
543                                         ETH_HLEN + packet_len);
544         if (!skb)
545                 goto out;
546
547         skb_reserve(skb, ETH_HLEN);
548         skb_buff = skb_put_data(skb, ogm_received, packet_len);
549
550         /* apply forward penalty */
551         ogm_forward = (struct batadv_ogm2_packet *)skb_buff;
552         ogm_forward->throughput = htonl(neigh_ifinfo->bat_v.throughput);
553         ogm_forward->ttl--;
554
555         batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
556                    "Forwarding OGM2 packet on %s: throughput %u, ttl %u, received via %s\n",
557                    if_outgoing->net_dev->name, ntohl(ogm_forward->throughput),
558                    ogm_forward->ttl, if_incoming->net_dev->name);
559
560         batadv_v_ogm_queue_on_if(skb, if_outgoing);
561
562 out:
563         if (orig_ifinfo)
564                 batadv_orig_ifinfo_put(orig_ifinfo);
565         if (router)
566                 batadv_neigh_node_put(router);
567         if (neigh_ifinfo)
568                 batadv_neigh_ifinfo_put(neigh_ifinfo);
569 }
570
571 /**
572  * batadv_v_ogm_metric_update() - update route metric based on OGM
573  * @bat_priv: the bat priv with all the soft interface information
574  * @ogm2: OGM2 structure
575  * @orig_node: Originator structure for which the OGM has been received
576  * @neigh_node: the neigh_node through with the OGM has been received
577  * @if_incoming: the interface where this packet was received
578  * @if_outgoing: the interface for which the packet should be considered
579  *
580  * Return:
581  *  1  if the OGM is new,
582  *  0  if it is not new but valid,
583  *  <0 on error (e.g. old OGM)
584  */
585 static int batadv_v_ogm_metric_update(struct batadv_priv *bat_priv,
586                                       const struct batadv_ogm2_packet *ogm2,
587                                       struct batadv_orig_node *orig_node,
588                                       struct batadv_neigh_node *neigh_node,
589                                       struct batadv_hard_iface *if_incoming,
590                                       struct batadv_hard_iface *if_outgoing)
591 {
592         struct batadv_orig_ifinfo *orig_ifinfo;
593         struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
594         bool protection_started = false;
595         int ret = -EINVAL;
596         u32 path_throughput;
597         s32 seq_diff;
598
599         orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
600         if (!orig_ifinfo)
601                 goto out;
602
603         seq_diff = ntohl(ogm2->seqno) - orig_ifinfo->last_real_seqno;
604
605         if (!hlist_empty(&orig_node->neigh_list) &&
606             batadv_window_protected(bat_priv, seq_diff,
607                                     BATADV_OGM_MAX_AGE,
608                                     &orig_ifinfo->batman_seqno_reset,
609                                     &protection_started)) {
610                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
611                            "Drop packet: packet within window protection time from %pM\n",
612                            ogm2->orig);
613                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
614                            "Last reset: %ld, %ld\n",
615                            orig_ifinfo->batman_seqno_reset, jiffies);
616                 goto out;
617         }
618
619         /* drop packets with old seqnos, however accept the first packet after
620          * a host has been rebooted.
621          */
622         if (seq_diff < 0 && !protection_started)
623                 goto out;
624
625         neigh_node->last_seen = jiffies;
626
627         orig_node->last_seen = jiffies;
628
629         orig_ifinfo->last_real_seqno = ntohl(ogm2->seqno);
630         orig_ifinfo->last_ttl = ogm2->ttl;
631
632         neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
633         if (!neigh_ifinfo)
634                 goto out;
635
636         path_throughput = batadv_v_forward_penalty(bat_priv, if_incoming,
637                                                    if_outgoing,
638                                                    ntohl(ogm2->throughput));
639         neigh_ifinfo->bat_v.throughput = path_throughput;
640         neigh_ifinfo->bat_v.last_seqno = ntohl(ogm2->seqno);
641         neigh_ifinfo->last_ttl = ogm2->ttl;
642
643         if (seq_diff > 0 || protection_started)
644                 ret = 1;
645         else
646                 ret = 0;
647 out:
648         if (orig_ifinfo)
649                 batadv_orig_ifinfo_put(orig_ifinfo);
650         if (neigh_ifinfo)
651                 batadv_neigh_ifinfo_put(neigh_ifinfo);
652
653         return ret;
654 }
655
656 /**
657  * batadv_v_ogm_route_update() - update routes based on OGM
658  * @bat_priv: the bat priv with all the soft interface information
659  * @ethhdr: the Ethernet header of the OGM2
660  * @ogm2: OGM2 structure
661  * @orig_node: Originator structure for which the OGM has been received
662  * @neigh_node: the neigh_node through with the OGM has been received
663  * @if_incoming: the interface where this packet was received
664  * @if_outgoing: the interface for which the packet should be considered
665  *
666  * Return: true if the packet should be forwarded, false otherwise
667  */
668 static bool batadv_v_ogm_route_update(struct batadv_priv *bat_priv,
669                                       const struct ethhdr *ethhdr,
670                                       const struct batadv_ogm2_packet *ogm2,
671                                       struct batadv_orig_node *orig_node,
672                                       struct batadv_neigh_node *neigh_node,
673                                       struct batadv_hard_iface *if_incoming,
674                                       struct batadv_hard_iface *if_outgoing)
675 {
676         struct batadv_neigh_node *router = NULL;
677         struct batadv_orig_node *orig_neigh_node;
678         struct batadv_neigh_node *orig_neigh_router = NULL;
679         struct batadv_neigh_ifinfo *router_ifinfo = NULL, *neigh_ifinfo = NULL;
680         u32 router_throughput, neigh_throughput;
681         u32 router_last_seqno;
682         u32 neigh_last_seqno;
683         s32 neigh_seq_diff;
684         bool forward = false;
685
686         orig_neigh_node = batadv_v_ogm_orig_get(bat_priv, ethhdr->h_source);
687         if (!orig_neigh_node)
688                 goto out;
689
690         orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
691                                                    if_outgoing);
692
693         /* drop packet if sender is not a direct neighbor and if we
694          * don't route towards it
695          */
696         router = batadv_orig_router_get(orig_node, if_outgoing);
697         if (router && router->orig_node != orig_node && !orig_neigh_router) {
698                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
699                            "Drop packet: OGM via unknown neighbor!\n");
700                 goto out;
701         }
702
703         /* Mark the OGM to be considered for forwarding, and update routes
704          * if needed.
705          */
706         forward = true;
707
708         batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
709                    "Searching and updating originator entry of received packet\n");
710
711         /* if this neighbor already is our next hop there is nothing
712          * to change
713          */
714         if (router == neigh_node)
715                 goto out;
716
717         /* don't consider neighbours with worse throughput.
718          * also switch route if this seqno is BATADV_V_MAX_ORIGDIFF newer than
719          * the last received seqno from our best next hop.
720          */
721         if (router) {
722                 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
723                 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
724
725                 /* if these are not allocated, something is wrong. */
726                 if (!router_ifinfo || !neigh_ifinfo)
727                         goto out;
728
729                 neigh_last_seqno = neigh_ifinfo->bat_v.last_seqno;
730                 router_last_seqno = router_ifinfo->bat_v.last_seqno;
731                 neigh_seq_diff = neigh_last_seqno - router_last_seqno;
732                 router_throughput = router_ifinfo->bat_v.throughput;
733                 neigh_throughput = neigh_ifinfo->bat_v.throughput;
734
735                 if (neigh_seq_diff < BATADV_OGM_MAX_ORIGDIFF &&
736                     router_throughput >= neigh_throughput)
737                         goto out;
738         }
739
740         batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
741 out:
742         if (router)
743                 batadv_neigh_node_put(router);
744         if (orig_neigh_router)
745                 batadv_neigh_node_put(orig_neigh_router);
746         if (orig_neigh_node)
747                 batadv_orig_node_put(orig_neigh_node);
748         if (router_ifinfo)
749                 batadv_neigh_ifinfo_put(router_ifinfo);
750         if (neigh_ifinfo)
751                 batadv_neigh_ifinfo_put(neigh_ifinfo);
752
753         return forward;
754 }
755
756 /**
757  * batadv_v_ogm_process_per_outif() - process a batman v OGM for an outgoing if
758  * @bat_priv: the bat priv with all the soft interface information
759  * @ethhdr: the Ethernet header of the OGM2
760  * @ogm2: OGM2 structure
761  * @orig_node: Originator structure for which the OGM has been received
762  * @neigh_node: the neigh_node through with the OGM has been received
763  * @if_incoming: the interface where this packet was received
764  * @if_outgoing: the interface for which the packet should be considered
765  */
766 static void
767 batadv_v_ogm_process_per_outif(struct batadv_priv *bat_priv,
768                                const struct ethhdr *ethhdr,
769                                const struct batadv_ogm2_packet *ogm2,
770                                struct batadv_orig_node *orig_node,
771                                struct batadv_neigh_node *neigh_node,
772                                struct batadv_hard_iface *if_incoming,
773                                struct batadv_hard_iface *if_outgoing)
774 {
775         int seqno_age;
776         bool forward;
777
778         /* first, update the metric with according sanity checks */
779         seqno_age = batadv_v_ogm_metric_update(bat_priv, ogm2, orig_node,
780                                                neigh_node, if_incoming,
781                                                if_outgoing);
782
783         /* outdated sequence numbers are to be discarded */
784         if (seqno_age < 0)
785                 return;
786
787         /* only unknown & newer OGMs contain TVLVs we are interested in */
788         if (seqno_age > 0 && if_outgoing == BATADV_IF_DEFAULT)
789                 batadv_tvlv_containers_process(bat_priv, true, orig_node,
790                                                NULL, NULL,
791                                                (unsigned char *)(ogm2 + 1),
792                                                ntohs(ogm2->tvlv_len));
793
794         /* if the metric update went through, update routes if needed */
795         forward = batadv_v_ogm_route_update(bat_priv, ethhdr, ogm2, orig_node,
796                                             neigh_node, if_incoming,
797                                             if_outgoing);
798
799         /* if the routes have been processed correctly, check and forward */
800         if (forward)
801                 batadv_v_ogm_forward(bat_priv, ogm2, orig_node, neigh_node,
802                                      if_incoming, if_outgoing);
803 }
804
805 /**
806  * batadv_v_ogm_aggr_packet() - checks if there is another OGM aggregated
807  * @buff_pos: current position in the skb
808  * @packet_len: total length of the skb
809  * @tvlv_len: tvlv length of the previously considered OGM
810  *
811  * Return: true if there is enough space for another OGM, false otherwise.
812  */
813 static bool batadv_v_ogm_aggr_packet(int buff_pos, int packet_len,
814                                      __be16 tvlv_len)
815 {
816         int next_buff_pos = 0;
817
818         next_buff_pos += buff_pos + BATADV_OGM2_HLEN;
819         next_buff_pos += ntohs(tvlv_len);
820
821         return (next_buff_pos <= packet_len) &&
822                (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
823 }
824
825 /**
826  * batadv_v_ogm_process() - process an incoming batman v OGM
827  * @skb: the skb containing the OGM
828  * @ogm_offset: offset to the OGM which should be processed (for aggregates)
829  * @if_incoming: the interface where this packet was receved
830  */
831 static void batadv_v_ogm_process(const struct sk_buff *skb, int ogm_offset,
832                                  struct batadv_hard_iface *if_incoming)
833 {
834         struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
835         struct ethhdr *ethhdr;
836         struct batadv_orig_node *orig_node = NULL;
837         struct batadv_hardif_neigh_node *hardif_neigh = NULL;
838         struct batadv_neigh_node *neigh_node = NULL;
839         struct batadv_hard_iface *hard_iface;
840         struct batadv_ogm2_packet *ogm_packet;
841         u32 ogm_throughput, link_throughput, path_throughput;
842         int ret;
843
844         ethhdr = eth_hdr(skb);
845         ogm_packet = (struct batadv_ogm2_packet *)(skb->data + ogm_offset);
846
847         ogm_throughput = ntohl(ogm_packet->throughput);
848
849         batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
850                    "Received OGM2 packet via NB: %pM, IF: %s [%pM] (from OG: %pM, seqno %u, throughput %u, TTL %u, V %u, tvlv_len %u)\n",
851                    ethhdr->h_source, if_incoming->net_dev->name,
852                    if_incoming->net_dev->dev_addr, ogm_packet->orig,
853                    ntohl(ogm_packet->seqno), ogm_throughput, ogm_packet->ttl,
854                    ogm_packet->version, ntohs(ogm_packet->tvlv_len));
855
856         /* If the throughput metric is 0, immediately drop the packet. No need
857          * to create orig_node / neigh_node for an unusable route.
858          */
859         if (ogm_throughput == 0) {
860                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
861                            "Drop packet: originator packet with throughput metric of 0\n");
862                 return;
863         }
864
865         /* require ELP packets be to received from this neighbor first */
866         hardif_neigh = batadv_hardif_neigh_get(if_incoming, ethhdr->h_source);
867         if (!hardif_neigh) {
868                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
869                            "Drop packet: OGM via unknown neighbor!\n");
870                 goto out;
871         }
872
873         orig_node = batadv_v_ogm_orig_get(bat_priv, ogm_packet->orig);
874         if (!orig_node)
875                 return;
876
877         neigh_node = batadv_neigh_node_get_or_create(orig_node, if_incoming,
878                                                      ethhdr->h_source);
879         if (!neigh_node)
880                 goto out;
881
882         /* Update the received throughput metric to match the link
883          * characteristic:
884          *  - If this OGM traveled one hop so far (emitted by single hop
885          *    neighbor) the path throughput metric equals the link throughput.
886          *  - For OGMs traversing more than hop the path throughput metric is
887          *    the smaller of the path throughput and the link throughput.
888          */
889         link_throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
890         path_throughput = min_t(u32, link_throughput, ogm_throughput);
891         ogm_packet->throughput = htonl(path_throughput);
892
893         batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet, orig_node,
894                                        neigh_node, if_incoming,
895                                        BATADV_IF_DEFAULT);
896
897         rcu_read_lock();
898         list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
899                 if (hard_iface->if_status != BATADV_IF_ACTIVE)
900                         continue;
901
902                 if (hard_iface->soft_iface != bat_priv->soft_iface)
903                         continue;
904
905                 if (!kref_get_unless_zero(&hard_iface->refcount))
906                         continue;
907
908                 ret = batadv_hardif_no_broadcast(hard_iface,
909                                                  ogm_packet->orig,
910                                                  hardif_neigh->orig);
911
912                 if (ret) {
913                         char *type;
914
915                         switch (ret) {
916                         case BATADV_HARDIF_BCAST_NORECIPIENT:
917                                 type = "no neighbor";
918                                 break;
919                         case BATADV_HARDIF_BCAST_DUPFWD:
920                                 type = "single neighbor is source";
921                                 break;
922                         case BATADV_HARDIF_BCAST_DUPORIG:
923                                 type = "single neighbor is originator";
924                                 break;
925                         default:
926                                 type = "unknown";
927                         }
928
929                         batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 packet from %pM on %s suppressed: %s\n",
930                                    ogm_packet->orig, hard_iface->net_dev->name,
931                                    type);
932
933                         batadv_hardif_put(hard_iface);
934                         continue;
935                 }
936
937                 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet,
938                                                orig_node, neigh_node,
939                                                if_incoming, hard_iface);
940
941                 batadv_hardif_put(hard_iface);
942         }
943         rcu_read_unlock();
944 out:
945         if (orig_node)
946                 batadv_orig_node_put(orig_node);
947         if (neigh_node)
948                 batadv_neigh_node_put(neigh_node);
949         if (hardif_neigh)
950                 batadv_hardif_neigh_put(hardif_neigh);
951 }
952
953 /**
954  * batadv_v_ogm_packet_recv() - OGM2 receiving handler
955  * @skb: the received OGM
956  * @if_incoming: the interface where this OGM has been received
957  *
958  * Return: NET_RX_SUCCESS and consume the skb on success or returns NET_RX_DROP
959  * (without freeing the skb) on failure
960  */
961 int batadv_v_ogm_packet_recv(struct sk_buff *skb,
962                              struct batadv_hard_iface *if_incoming)
963 {
964         struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
965         struct batadv_ogm2_packet *ogm_packet;
966         struct ethhdr *ethhdr = eth_hdr(skb);
967         int ogm_offset;
968         u8 *packet_pos;
969         int ret = NET_RX_DROP;
970
971         /* did we receive a OGM2 packet on an interface that does not have
972          * B.A.T.M.A.N. V enabled ?
973          */
974         if (strcmp(bat_priv->algo_ops->name, "BATMAN_V") != 0)
975                 goto free_skb;
976
977         if (!batadv_check_management_packet(skb, if_incoming, BATADV_OGM2_HLEN))
978                 goto free_skb;
979
980         if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
981                 goto free_skb;
982
983         ogm_packet = (struct batadv_ogm2_packet *)skb->data;
984
985         if (batadv_is_my_mac(bat_priv, ogm_packet->orig))
986                 goto free_skb;
987
988         batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
989         batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
990                            skb->len + ETH_HLEN);
991
992         ogm_offset = 0;
993         ogm_packet = (struct batadv_ogm2_packet *)skb->data;
994
995         while (batadv_v_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
996                                         ogm_packet->tvlv_len)) {
997                 batadv_v_ogm_process(skb, ogm_offset, if_incoming);
998
999                 ogm_offset += BATADV_OGM2_HLEN;
1000                 ogm_offset += ntohs(ogm_packet->tvlv_len);
1001
1002                 packet_pos = skb->data + ogm_offset;
1003                 ogm_packet = (struct batadv_ogm2_packet *)packet_pos;
1004         }
1005
1006         ret = NET_RX_SUCCESS;
1007
1008 free_skb:
1009         if (ret == NET_RX_SUCCESS)
1010                 consume_skb(skb);
1011         else
1012                 kfree_skb(skb);
1013
1014         return ret;
1015 }
1016
1017 /**
1018  * batadv_v_ogm_init() - initialise the OGM2 engine
1019  * @bat_priv: the bat priv with all the soft interface information
1020  *
1021  * Return: 0 on success or a negative error code in case of failure
1022  */
1023 int batadv_v_ogm_init(struct batadv_priv *bat_priv)
1024 {
1025         struct batadv_ogm2_packet *ogm_packet;
1026         unsigned char *ogm_buff;
1027         u32 random_seqno;
1028
1029         bat_priv->bat_v.ogm_buff_len = BATADV_OGM2_HLEN;
1030         ogm_buff = kzalloc(bat_priv->bat_v.ogm_buff_len, GFP_ATOMIC);
1031         if (!ogm_buff)
1032                 return -ENOMEM;
1033
1034         bat_priv->bat_v.ogm_buff = ogm_buff;
1035         ogm_packet = (struct batadv_ogm2_packet *)ogm_buff;
1036         ogm_packet->packet_type = BATADV_OGM2;
1037         ogm_packet->version = BATADV_COMPAT_VERSION;
1038         ogm_packet->ttl = BATADV_TTL;
1039         ogm_packet->flags = BATADV_NO_FLAGS;
1040         ogm_packet->throughput = htonl(BATADV_THROUGHPUT_MAX_VALUE);
1041
1042         /* randomize initial seqno to avoid collision */
1043         get_random_bytes(&random_seqno, sizeof(random_seqno));
1044         atomic_set(&bat_priv->bat_v.ogm_seqno, random_seqno);
1045         INIT_DELAYED_WORK(&bat_priv->bat_v.ogm_wq, batadv_v_ogm_send);
1046
1047         return 0;
1048 }
1049
1050 /**
1051  * batadv_v_ogm_free() - free OGM private resources
1052  * @bat_priv: the bat priv with all the soft interface information
1053  */
1054 void batadv_v_ogm_free(struct batadv_priv *bat_priv)
1055 {
1056         cancel_delayed_work_sync(&bat_priv->bat_v.ogm_wq);
1057
1058         kfree(bat_priv->bat_v.ogm_buff);
1059         bat_priv->bat_v.ogm_buff = NULL;
1060         bat_priv->bat_v.ogm_buff_len = 0;
1061 }