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