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