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