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