blob: b0cae59bd327c3443c15e3053c16dec1ca1f3916 [file] [log] [blame]
Antonio Quartulli0da00352016-01-16 16:40:12 +08001/* Copyright (C) 2013-2016 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>
Sven Eckelmann27353442016-03-05 16:09:16 +010029#include <linux/kref.h>
Antonio Quartulli93231582016-01-16 16:40:13 +080030#include <linux/list.h>
Sven Eckelmann5841c1b2020-03-16 23:31:03 +010031#include <linux/lockdep.h>
32#include <linux/mutex.h>
Antonio Quartulli0da00352016-01-16 16:40:12 +080033#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
Sven Eckelmann01d350d2016-05-15 11:07:44 +020044#include "bat_algo.h"
Antonio Quartulli0da00352016-01-16 16:40:12 +080045#include "hard-interface.h"
Antonio Quartulli93231582016-01-16 16:40:13 +080046#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020047#include "log.h"
Antonio Quartulli93231582016-01-16 16:40:13 +080048#include "originator.h"
Antonio Quartulli0da00352016-01-16 16:40:12 +080049#include "packet.h"
50#include "routing.h"
51#include "send.h"
52#include "translation-table.h"
Markus Pargmann1f8dce42016-05-15 11:07:43 +020053#include "tvlv.h"
Antonio Quartulli0da00352016-01-16 16:40:12 +080054
55/**
Antonio Quartulli93231582016-01-16 16:40:13 +080056 * 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 */
64struct 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
Sven Eckelmann55db2d52016-07-15 17:39:21 +020078 kref_get(&orig_node->refcount);
Antonio Quartulli93231582016-01-16 16:40:13 +080079 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) {
Sven Eckelmann55db2d52016-07-15 17:39:21 +020083 /* remove refcnt for newly created orig_node and hash entry */
Antonio Quartulli93231582016-01-16 16:40:13 +080084 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/**
Antonio Quartulli0da00352016-01-16 16:40:12 +080093 * batadv_v_ogm_start_timer - restart the OGM sending timer
94 * @bat_priv: the bat priv with all the soft interface information
95 */
96static 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 */
116static 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
Antonio Quartulli95d39272016-01-16 16:40:15 +0800128 batadv_send_broadcast_skb(skb, hard_iface);
Antonio Quartulli0da00352016-01-16 16:40:12 +0800129}
130
131/**
Sven Eckelmann5841c1b2020-03-16 23:31:03 +0100132 * batadv_v_ogm_send_softif() - periodic worker broadcasting the own OGM
133 * @bat_priv: the bat priv with all the soft interface information
Antonio Quartulli0da00352016-01-16 16:40:12 +0800134 */
Sven Eckelmann5841c1b2020-03-16 23:31:03 +0100135static void batadv_v_ogm_send_softif(struct batadv_priv *bat_priv)
Antonio Quartulli0da00352016-01-16 16:40:12 +0800136{
137 struct batadv_hard_iface *hard_iface;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800138 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
Sven Eckelmann5841c1b2020-03-16 23:31:03 +0100144 lockdep_assert_held(&bat_priv->bat_v.ogm_buff_mutex);
Antonio Quartulli0da00352016-01-16 16:40:12 +0800145
146 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
147 goto out;
148
149 ogm_buff = bat_priv->bat_v.ogm_buff;
150 ogm_buff_len = bat_priv->bat_v.ogm_buff_len;
151 /* tt changes have to be committed before the tvlv data is
152 * appended as it may alter the tt tvlv container
153 */
154 batadv_tt_local_commit_changes(bat_priv);
155 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, &ogm_buff,
156 &ogm_buff_len,
157 BATADV_OGM2_HLEN);
158
159 bat_priv->bat_v.ogm_buff = ogm_buff;
160 bat_priv->bat_v.ogm_buff_len = ogm_buff_len;
161
162 skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + ogm_buff_len);
163 if (!skb)
164 goto reschedule;
165
166 skb_reserve(skb, ETH_HLEN);
167 pkt_buff = skb_put(skb, ogm_buff_len);
168 memcpy(pkt_buff, 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
Sven Eckelmann27353442016-03-05 16:09:16 +0100181 if (!kref_get_unless_zero(&hard_iface->refcount))
182 continue;
183
Antonio Quartulli0da00352016-01-16 16:40:12 +0800184 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
185 "Sending own OGM2 packet (originator %pM, seqno %u, throughput %u, TTL %d) on interface %s [%pM]\n",
186 ogm_packet->orig, ntohl(ogm_packet->seqno),
187 ntohl(ogm_packet->throughput), ogm_packet->ttl,
188 hard_iface->net_dev->name,
189 hard_iface->net_dev->dev_addr);
190
191 /* this skb gets consumed by batadv_v_ogm_send_to_if() */
192 skb_tmp = skb_clone(skb, GFP_ATOMIC);
Sven Eckelmann27353442016-03-05 16:09:16 +0100193 if (!skb_tmp) {
194 batadv_hardif_put(hard_iface);
Antonio Quartulli0da00352016-01-16 16:40:12 +0800195 break;
Sven Eckelmann27353442016-03-05 16:09:16 +0100196 }
Antonio Quartulli0da00352016-01-16 16:40:12 +0800197
198 batadv_v_ogm_send_to_if(skb_tmp, hard_iface);
Sven Eckelmann27353442016-03-05 16:09:16 +0100199 batadv_hardif_put(hard_iface);
Antonio Quartulli0da00352016-01-16 16:40:12 +0800200 }
201 rcu_read_unlock();
202
203 consume_skb(skb);
204
205reschedule:
206 batadv_v_ogm_start_timer(bat_priv);
207out:
208 return;
209}
210
211/**
Sven Eckelmann5841c1b2020-03-16 23:31:03 +0100212 * batadv_v_ogm_send() - periodic worker broadcasting the own OGM
213 * @work: work queue item
214 */
215static void batadv_v_ogm_send(struct work_struct *work)
216{
217 struct batadv_priv_bat_v *bat_v;
218 struct batadv_priv *bat_priv;
219
220 bat_v = container_of(work, struct batadv_priv_bat_v, ogm_wq.work);
221 bat_priv = container_of(bat_v, struct batadv_priv, bat_v);
222
223 mutex_lock(&bat_priv->bat_v.ogm_buff_mutex);
224 batadv_v_ogm_send_softif(bat_priv);
225 mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex);
226}
227
228/**
Antonio Quartulli0da00352016-01-16 16:40:12 +0800229 * batadv_v_ogm_iface_enable - prepare an interface for B.A.T.M.A.N. V
230 * @hard_iface: the interface to prepare
231 *
232 * Takes care of scheduling own OGM sending routine for this interface.
233 *
234 * Return: 0 on success or a negative error code otherwise
235 */
236int batadv_v_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
237{
238 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
239
240 batadv_v_ogm_start_timer(bat_priv);
241
242 return 0;
243}
244
245/**
246 * batadv_v_ogm_primary_iface_set - set a new primary interface
247 * @primary_iface: the new primary interface
248 */
249void batadv_v_ogm_primary_iface_set(struct batadv_hard_iface *primary_iface)
250{
251 struct batadv_priv *bat_priv = netdev_priv(primary_iface->soft_iface);
252 struct batadv_ogm2_packet *ogm_packet;
253
Sven Eckelmann5841c1b2020-03-16 23:31:03 +0100254 mutex_lock(&bat_priv->bat_v.ogm_buff_mutex);
Antonio Quartulli0da00352016-01-16 16:40:12 +0800255 if (!bat_priv->bat_v.ogm_buff)
Sven Eckelmann5841c1b2020-03-16 23:31:03 +0100256 goto unlock;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800257
258 ogm_packet = (struct batadv_ogm2_packet *)bat_priv->bat_v.ogm_buff;
259 ether_addr_copy(ogm_packet->orig, primary_iface->net_dev->dev_addr);
Sven Eckelmann5841c1b2020-03-16 23:31:03 +0100260
261unlock:
262 mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex);
Antonio Quartulli0da00352016-01-16 16:40:12 +0800263}
264
265/**
Antonio Quartulli93231582016-01-16 16:40:13 +0800266 * batadv_v_forward_penalty - apply a penalty to the throughput metric forwarded
267 * with B.A.T.M.A.N. V OGMs
268 * @bat_priv: the bat priv with all the soft interface information
269 * @if_incoming: the interface where the OGM has been received
270 * @if_outgoing: the interface where the OGM has to be forwarded to
271 * @throughput: the current throughput
272 *
273 * Apply a penalty on the current throughput metric value based on the
274 * characteristic of the interface where the OGM has been received. The return
275 * value is computed as follows:
276 * - throughput * 50% if the incoming and outgoing interface are the
277 * same WiFi interface and the throughput is above
278 * 1MBit/s
279 * - throughput if the outgoing interface is the default
280 * interface (i.e. this OGM is processed for the
281 * internal table and not forwarded)
282 * - throughput * hop penalty otherwise
283 *
284 * Return: the penalised throughput metric.
285 */
286static u32 batadv_v_forward_penalty(struct batadv_priv *bat_priv,
287 struct batadv_hard_iface *if_incoming,
288 struct batadv_hard_iface *if_outgoing,
289 u32 throughput)
290{
291 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
292 int hop_penalty_max = BATADV_TQ_MAX_VALUE;
293
294 /* Don't apply hop penalty in default originator table. */
295 if (if_outgoing == BATADV_IF_DEFAULT)
296 return throughput;
297
298 /* Forwarding on the same WiFi interface cuts the throughput in half
299 * due to the store & forward characteristics of WIFI.
300 * Very low throughput values are the exception.
301 */
302 if ((throughput > 10) &&
303 (if_incoming == if_outgoing) &&
Antonio Quartullic8334842015-11-10 18:50:51 +0100304 !(if_incoming->bat_v.flags & BATADV_FULL_DUPLEX))
Antonio Quartulli93231582016-01-16 16:40:13 +0800305 return throughput / 2;
306
307 /* hop penalty of 255 equals 100% */
308 return throughput * (hop_penalty_max - hop_penalty) / hop_penalty_max;
309}
310
311/**
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100312 * batadv_v_ogm_forward - check conditions and forward an OGM to the given
313 * outgoing interface
Antonio Quartulli93231582016-01-16 16:40:13 +0800314 * @bat_priv: the bat priv with all the soft interface information
315 * @ogm_received: previously received OGM to be forwarded
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100316 * @orig_node: the originator which has been updated
317 * @neigh_node: the neigh_node through with the OGM has been received
Antonio Quartulli93231582016-01-16 16:40:13 +0800318 * @if_incoming: the interface on which this OGM was received on
319 * @if_outgoing: the interface to which the OGM has to be forwarded to
320 *
321 * Forward an OGM to an interface after having altered the throughput metric and
322 * the TTL value contained in it. The original OGM isn't modified.
323 */
324static void batadv_v_ogm_forward(struct batadv_priv *bat_priv,
325 const struct batadv_ogm2_packet *ogm_received,
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100326 struct batadv_orig_node *orig_node,
327 struct batadv_neigh_node *neigh_node,
Antonio Quartulli93231582016-01-16 16:40:13 +0800328 struct batadv_hard_iface *if_incoming,
329 struct batadv_hard_iface *if_outgoing)
330{
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100331 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
332 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
333 struct batadv_neigh_node *router = NULL;
Antonio Quartulli93231582016-01-16 16:40:13 +0800334 struct batadv_ogm2_packet *ogm_forward;
335 unsigned char *skb_buff;
336 struct sk_buff *skb;
337 size_t packet_len;
338 u16 tvlv_len;
339
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100340 /* only forward for specific interfaces, not for the default one. */
341 if (if_outgoing == BATADV_IF_DEFAULT)
342 goto out;
343
344 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
345 if (!orig_ifinfo)
346 goto out;
347
348 /* acquire possibly updated router */
349 router = batadv_orig_router_get(orig_node, if_outgoing);
350
351 /* strict rule: forward packets coming from the best next hop only */
352 if (neigh_node != router)
353 goto out;
354
355 /* don't forward the same seqno twice on one interface */
356 if (orig_ifinfo->last_seqno_forwarded == ntohl(ogm_received->seqno))
357 goto out;
358
359 orig_ifinfo->last_seqno_forwarded = ntohl(ogm_received->seqno);
360
Antonio Quartulli93231582016-01-16 16:40:13 +0800361 if (ogm_received->ttl <= 1) {
362 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100363 goto out;
Antonio Quartulli93231582016-01-16 16:40:13 +0800364 }
365
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100366 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
367 if (!neigh_ifinfo)
368 goto out;
369
Antonio Quartulli93231582016-01-16 16:40:13 +0800370 tvlv_len = ntohs(ogm_received->tvlv_len);
371
372 packet_len = BATADV_OGM2_HLEN + tvlv_len;
373 skb = netdev_alloc_skb_ip_align(if_outgoing->net_dev,
374 ETH_HLEN + packet_len);
375 if (!skb)
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100376 goto out;
Antonio Quartulli93231582016-01-16 16:40:13 +0800377
378 skb_reserve(skb, ETH_HLEN);
379 skb_buff = skb_put(skb, packet_len);
380 memcpy(skb_buff, ogm_received, packet_len);
381
382 /* apply forward penalty */
383 ogm_forward = (struct batadv_ogm2_packet *)skb_buff;
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100384 ogm_forward->throughput = htonl(neigh_ifinfo->bat_v.throughput);
Antonio Quartulli93231582016-01-16 16:40:13 +0800385 ogm_forward->ttl--;
386
387 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
388 "Forwarding OGM2 packet on %s: throughput %u, ttl %u, received via %s\n",
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100389 if_outgoing->net_dev->name, ntohl(ogm_forward->throughput),
390 ogm_forward->ttl, if_incoming->net_dev->name);
Antonio Quartulli93231582016-01-16 16:40:13 +0800391
392 batadv_v_ogm_send_to_if(skb, if_outgoing);
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100393
394out:
395 if (orig_ifinfo)
396 batadv_orig_ifinfo_put(orig_ifinfo);
397 if (router)
398 batadv_neigh_node_put(router);
399 if (neigh_ifinfo)
400 batadv_neigh_ifinfo_put(neigh_ifinfo);
Antonio Quartulli93231582016-01-16 16:40:13 +0800401}
402
403/**
404 * batadv_v_ogm_metric_update - update route metric based on OGM
405 * @bat_priv: the bat priv with all the soft interface information
406 * @ogm2: OGM2 structure
407 * @orig_node: Originator structure for which the OGM has been received
408 * @neigh_node: the neigh_node through with the OGM has been received
409 * @if_incoming: the interface where this packet was received
410 * @if_outgoing: the interface for which the packet should be considered
411 *
412 * Return:
413 * 1 if the OGM is new,
414 * 0 if it is not new but valid,
415 * <0 on error (e.g. old OGM)
416 */
417static int batadv_v_ogm_metric_update(struct batadv_priv *bat_priv,
418 const struct batadv_ogm2_packet *ogm2,
419 struct batadv_orig_node *orig_node,
420 struct batadv_neigh_node *neigh_node,
421 struct batadv_hard_iface *if_incoming,
422 struct batadv_hard_iface *if_outgoing)
423{
424 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
425 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
426 bool protection_started = false;
427 int ret = -EINVAL;
428 u32 path_throughput;
429 s32 seq_diff;
430
431 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
432 if (!orig_ifinfo)
433 goto out;
434
435 seq_diff = ntohl(ogm2->seqno) - orig_ifinfo->last_real_seqno;
436
437 if (!hlist_empty(&orig_node->neigh_list) &&
438 batadv_window_protected(bat_priv, seq_diff,
439 BATADV_OGM_MAX_AGE,
440 &orig_ifinfo->batman_seqno_reset,
441 &protection_started)) {
442 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
443 "Drop packet: packet within window protection time from %pM\n",
444 ogm2->orig);
445 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
446 "Last reset: %ld, %ld\n",
447 orig_ifinfo->batman_seqno_reset, jiffies);
448 goto out;
449 }
450
451 /* drop packets with old seqnos, however accept the first packet after
452 * a host has been rebooted.
453 */
454 if ((seq_diff < 0) && !protection_started)
455 goto out;
456
457 neigh_node->last_seen = jiffies;
458
459 orig_node->last_seen = jiffies;
460
461 orig_ifinfo->last_real_seqno = ntohl(ogm2->seqno);
462 orig_ifinfo->last_ttl = ogm2->ttl;
463
464 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
465 if (!neigh_ifinfo)
466 goto out;
467
468 path_throughput = batadv_v_forward_penalty(bat_priv, if_incoming,
469 if_outgoing,
470 ntohl(ogm2->throughput));
471 neigh_ifinfo->bat_v.throughput = path_throughput;
472 neigh_ifinfo->bat_v.last_seqno = ntohl(ogm2->seqno);
473 neigh_ifinfo->last_ttl = ogm2->ttl;
474
475 if (seq_diff > 0 || protection_started)
476 ret = 1;
477 else
478 ret = 0;
479out:
480 if (orig_ifinfo)
481 batadv_orig_ifinfo_put(orig_ifinfo);
482 if (neigh_ifinfo)
483 batadv_neigh_ifinfo_put(neigh_ifinfo);
484
485 return ret;
486}
487
488/**
489 * batadv_v_ogm_route_update - update routes based on OGM
490 * @bat_priv: the bat priv with all the soft interface information
491 * @ethhdr: the Ethernet header of the OGM2
492 * @ogm2: OGM2 structure
493 * @orig_node: Originator structure for which the OGM has been received
494 * @neigh_node: the neigh_node through with the OGM has been received
495 * @if_incoming: the interface where this packet was received
496 * @if_outgoing: the interface for which the packet should be considered
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100497 *
498 * Return: true if the packet should be forwarded, false otherwise
Antonio Quartulli93231582016-01-16 16:40:13 +0800499 */
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100500static bool batadv_v_ogm_route_update(struct batadv_priv *bat_priv,
Antonio Quartulli93231582016-01-16 16:40:13 +0800501 const struct ethhdr *ethhdr,
502 const struct batadv_ogm2_packet *ogm2,
503 struct batadv_orig_node *orig_node,
504 struct batadv_neigh_node *neigh_node,
505 struct batadv_hard_iface *if_incoming,
506 struct batadv_hard_iface *if_outgoing)
507{
508 struct batadv_neigh_node *router = NULL;
Antonio Quartulli93231582016-01-16 16:40:13 +0800509 struct batadv_orig_node *orig_neigh_node = NULL;
Antonio Quartulli93231582016-01-16 16:40:13 +0800510 struct batadv_neigh_node *orig_neigh_router = NULL;
Simon Wunderlich86de37c2016-02-01 15:21:38 +0100511 struct batadv_neigh_ifinfo *router_ifinfo = NULL, *neigh_ifinfo = NULL;
512 u32 router_throughput, neigh_throughput;
513 u32 router_last_seqno;
514 u32 neigh_last_seqno;
515 s32 neigh_seq_diff;
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100516 bool forward = false;
Antonio Quartulli93231582016-01-16 16:40:13 +0800517
518 orig_neigh_node = batadv_v_ogm_orig_get(bat_priv, ethhdr->h_source);
519 if (!orig_neigh_node)
520 goto out;
521
522 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
523 if_outgoing);
524
525 /* drop packet if sender is not a direct neighbor and if we
526 * don't route towards it
527 */
528 router = batadv_orig_router_get(orig_node, if_outgoing);
529 if (router && router->orig_node != orig_node && !orig_neigh_router) {
530 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
531 "Drop packet: OGM via unknown neighbor!\n");
532 goto out;
533 }
534
Simon Wunderlich86de37c2016-02-01 15:21:38 +0100535 /* Mark the OGM to be considered for forwarding, and update routes
536 * if needed.
537 */
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100538 forward = true;
Simon Wunderlich86de37c2016-02-01 15:21:38 +0100539
540 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
541 "Searching and updating originator entry of received packet\n");
542
543 /* if this neighbor already is our next hop there is nothing
544 * to change
545 */
546 if (router == neigh_node)
547 goto out;
548
549 /* don't consider neighbours with worse throughput.
550 * also switch route if this seqno is BATADV_V_MAX_ORIGDIFF newer than
551 * the last received seqno from our best next hop.
552 */
553 if (router) {
554 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
555 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
556
557 /* if these are not allocated, something is wrong. */
558 if (!router_ifinfo || !neigh_ifinfo)
559 goto out;
560
561 neigh_last_seqno = neigh_ifinfo->bat_v.last_seqno;
562 router_last_seqno = router_ifinfo->bat_v.last_seqno;
563 neigh_seq_diff = neigh_last_seqno - router_last_seqno;
564 router_throughput = router_ifinfo->bat_v.throughput;
565 neigh_throughput = neigh_ifinfo->bat_v.throughput;
566
567 if ((neigh_seq_diff < BATADV_OGM_MAX_ORIGDIFF) &&
568 (router_throughput >= neigh_throughput))
569 goto out;
570 }
571
572 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
Antonio Quartulli93231582016-01-16 16:40:13 +0800573out:
Antonio Quartulli93231582016-01-16 16:40:13 +0800574 if (router)
575 batadv_neigh_node_put(router);
576 if (orig_neigh_router)
577 batadv_neigh_node_put(orig_neigh_router);
578 if (orig_neigh_node)
579 batadv_orig_node_put(orig_neigh_node);
Simon Wunderlich86de37c2016-02-01 15:21:38 +0100580 if (router_ifinfo)
581 batadv_neigh_ifinfo_put(router_ifinfo);
582 if (neigh_ifinfo)
583 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100584
585 return forward;
Antonio Quartulli93231582016-01-16 16:40:13 +0800586}
587
588/**
589 * batadv_v_ogm_process_per_outif - process a batman v OGM for an outgoing if
590 * @bat_priv: the bat priv with all the soft interface information
591 * @ethhdr: the Ethernet header of the OGM2
592 * @ogm2: OGM2 structure
593 * @orig_node: Originator structure for which the OGM has been received
594 * @neigh_node: the neigh_node through with the OGM has been received
595 * @if_incoming: the interface where this packet was received
596 * @if_outgoing: the interface for which the packet should be considered
597 */
598static void
599batadv_v_ogm_process_per_outif(struct batadv_priv *bat_priv,
600 const struct ethhdr *ethhdr,
601 const struct batadv_ogm2_packet *ogm2,
602 struct batadv_orig_node *orig_node,
603 struct batadv_neigh_node *neigh_node,
604 struct batadv_hard_iface *if_incoming,
605 struct batadv_hard_iface *if_outgoing)
606{
607 int seqno_age;
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100608 bool forward;
Antonio Quartulli93231582016-01-16 16:40:13 +0800609
610 /* first, update the metric with according sanity checks */
611 seqno_age = batadv_v_ogm_metric_update(bat_priv, ogm2, orig_node,
612 neigh_node, if_incoming,
613 if_outgoing);
614
615 /* outdated sequence numbers are to be discarded */
616 if (seqno_age < 0)
617 return;
618
619 /* only unknown & newer OGMs contain TVLVs we are interested in */
620 if ((seqno_age > 0) && (if_outgoing == BATADV_IF_DEFAULT))
621 batadv_tvlv_containers_process(bat_priv, true, orig_node,
622 NULL, NULL,
623 (unsigned char *)(ogm2 + 1),
624 ntohs(ogm2->tvlv_len));
625
626 /* if the metric update went through, update routes if needed */
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100627 forward = batadv_v_ogm_route_update(bat_priv, ethhdr, ogm2, orig_node,
628 neigh_node, if_incoming,
629 if_outgoing);
630
631 /* if the routes have been processed correctly, check and forward */
632 if (forward)
633 batadv_v_ogm_forward(bat_priv, ogm2, orig_node, neigh_node,
634 if_incoming, if_outgoing);
Antonio Quartulli93231582016-01-16 16:40:13 +0800635}
636
637/**
638 * batadv_v_ogm_aggr_packet - checks if there is another OGM aggregated
639 * @buff_pos: current position in the skb
640 * @packet_len: total length of the skb
Sven Eckelmanna1653da2019-08-22 08:55:36 +0200641 * @ogm2_packet: potential OGM2 in buffer
Antonio Quartulli93231582016-01-16 16:40:13 +0800642 *
643 * Return: true if there is enough space for another OGM, false otherwise.
644 */
Sven Eckelmanna1653da2019-08-22 08:55:36 +0200645static bool
646batadv_v_ogm_aggr_packet(int buff_pos, int packet_len,
647 const struct batadv_ogm2_packet *ogm2_packet)
Antonio Quartulli93231582016-01-16 16:40:13 +0800648{
649 int next_buff_pos = 0;
650
Sven Eckelmanna1653da2019-08-22 08:55:36 +0200651 /* check if there is enough space for the header */
652 next_buff_pos += buff_pos + sizeof(*ogm2_packet);
653 if (next_buff_pos > packet_len)
654 return false;
655
656 /* check if there is enough space for the optional TVLV */
657 next_buff_pos += ntohs(ogm2_packet->tvlv_len);
Antonio Quartulli93231582016-01-16 16:40:13 +0800658
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 */
669static 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
681 ethhdr = eth_hdr(skb);
682 ogm_packet = (struct batadv_ogm2_packet *)(skb->data + ogm_offset);
683
684 ogm_throughput = ntohl(ogm_packet->throughput);
685
686 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
687 "Received OGM2 packet via NB: %pM, IF: %s [%pM] (from OG: %pM, seqno %u, troughput %u, TTL %u, V %u, tvlv_len %u)\n",
688 ethhdr->h_source, if_incoming->net_dev->name,
689 if_incoming->net_dev->dev_addr, ogm_packet->orig,
690 ntohl(ogm_packet->seqno), ogm_throughput, ogm_packet->ttl,
691 ogm_packet->version, ntohs(ogm_packet->tvlv_len));
692
693 /* If the troughput metric is 0, immediately drop the packet. No need to
694 * create orig_node / neigh_node for an unusable route.
695 */
696 if (ogm_throughput == 0) {
697 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
698 "Drop packet: originator packet with troughput metric of 0\n");
699 return;
700 }
701
702 /* require ELP packets be to received from this neighbor first */
703 hardif_neigh = batadv_hardif_neigh_get(if_incoming, ethhdr->h_source);
704 if (!hardif_neigh) {
705 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
706 "Drop packet: OGM via unknown neighbor!\n");
707 goto out;
708 }
709
710 orig_node = batadv_v_ogm_orig_get(bat_priv, ogm_packet->orig);
711 if (!orig_node)
712 return;
713
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800714 neigh_node = batadv_neigh_node_get_or_create(orig_node, if_incoming,
715 ethhdr->h_source);
Antonio Quartulli93231582016-01-16 16:40:13 +0800716 if (!neigh_node)
717 goto out;
718
719 /* Update the received throughput metric to match the link
720 * characteristic:
721 * - If this OGM traveled one hop so far (emitted by single hop
722 * neighbor) the path throughput metric equals the link throughput.
723 * - For OGMs traversing more than hop the path throughput metric is
724 * the smaller of the path throughput and the link throughput.
725 */
726 link_throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
727 path_throughput = min_t(u32, link_throughput, ogm_throughput);
728 ogm_packet->throughput = htonl(path_throughput);
729
730 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet, orig_node,
731 neigh_node, if_incoming,
732 BATADV_IF_DEFAULT);
733
734 rcu_read_lock();
735 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
736 if (hard_iface->if_status != BATADV_IF_ACTIVE)
737 continue;
738
739 if (hard_iface->soft_iface != bat_priv->soft_iface)
740 continue;
741
Sven Eckelmann27353442016-03-05 16:09:16 +0100742 if (!kref_get_unless_zero(&hard_iface->refcount))
743 continue;
744
Antonio Quartulli93231582016-01-16 16:40:13 +0800745 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet,
746 orig_node, neigh_node,
747 if_incoming, hard_iface);
Sven Eckelmann27353442016-03-05 16:09:16 +0100748
749 batadv_hardif_put(hard_iface);
Antonio Quartulli93231582016-01-16 16:40:13 +0800750 }
751 rcu_read_unlock();
752out:
753 if (orig_node)
754 batadv_orig_node_put(orig_node);
755 if (neigh_node)
756 batadv_neigh_node_put(neigh_node);
757 if (hardif_neigh)
758 batadv_hardif_neigh_put(hardif_neigh);
759}
760
761/**
Antonio Quartulli0da00352016-01-16 16:40:12 +0800762 * batadv_v_ogm_packet_recv - OGM2 receiving handler
763 * @skb: the received OGM
764 * @if_incoming: the interface where this OGM has been received
765 *
766 * Return: NET_RX_SUCCESS and consume the skb on success or returns NET_RX_DROP
767 * (without freeing the skb) on failure
768 */
769int batadv_v_ogm_packet_recv(struct sk_buff *skb,
770 struct batadv_hard_iface *if_incoming)
771{
772 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
773 struct batadv_ogm2_packet *ogm_packet;
774 struct ethhdr *ethhdr = eth_hdr(skb);
Antonio Quartulli93231582016-01-16 16:40:13 +0800775 int ogm_offset;
776 u8 *packet_pos;
777 int ret = NET_RX_DROP;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800778
779 /* did we receive a OGM2 packet on an interface that does not have
780 * B.A.T.M.A.N. V enabled ?
781 */
Antonio Quartulli29824a52016-05-25 23:27:31 +0800782 if (strcmp(bat_priv->algo_ops->name, "BATMAN_V") != 0)
Antonio Quartulli0da00352016-01-16 16:40:12 +0800783 return NET_RX_DROP;
784
785 if (!batadv_check_management_packet(skb, if_incoming, BATADV_OGM2_HLEN))
786 return NET_RX_DROP;
787
788 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
789 return NET_RX_DROP;
790
791 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
792
793 if (batadv_is_my_mac(bat_priv, ogm_packet->orig))
794 return NET_RX_DROP;
795
796 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
797 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
798 skb->len + ETH_HLEN);
799
Antonio Quartulli93231582016-01-16 16:40:13 +0800800 ogm_offset = 0;
801 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
802
803 while (batadv_v_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
Sven Eckelmanna1653da2019-08-22 08:55:36 +0200804 ogm_packet)) {
Antonio Quartulli93231582016-01-16 16:40:13 +0800805 batadv_v_ogm_process(skb, ogm_offset, if_incoming);
806
807 ogm_offset += BATADV_OGM2_HLEN;
808 ogm_offset += ntohs(ogm_packet->tvlv_len);
809
810 packet_pos = skb->data + ogm_offset;
811 ogm_packet = (struct batadv_ogm2_packet *)packet_pos;
812 }
813
814 ret = NET_RX_SUCCESS;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800815 consume_skb(skb);
Antonio Quartulli93231582016-01-16 16:40:13 +0800816
817 return ret;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800818}
819
820/**
821 * batadv_v_ogm_init - initialise the OGM2 engine
822 * @bat_priv: the bat priv with all the soft interface information
823 *
824 * Return: 0 on success or a negative error code in case of failure
825 */
826int batadv_v_ogm_init(struct batadv_priv *bat_priv)
827{
828 struct batadv_ogm2_packet *ogm_packet;
829 unsigned char *ogm_buff;
830 u32 random_seqno;
831
832 bat_priv->bat_v.ogm_buff_len = BATADV_OGM2_HLEN;
833 ogm_buff = kzalloc(bat_priv->bat_v.ogm_buff_len, GFP_ATOMIC);
834 if (!ogm_buff)
835 return -ENOMEM;
836
837 bat_priv->bat_v.ogm_buff = ogm_buff;
838 ogm_packet = (struct batadv_ogm2_packet *)ogm_buff;
839 ogm_packet->packet_type = BATADV_OGM2;
840 ogm_packet->version = BATADV_COMPAT_VERSION;
841 ogm_packet->ttl = BATADV_TTL;
842 ogm_packet->flags = BATADV_NO_FLAGS;
843 ogm_packet->throughput = htonl(BATADV_THROUGHPUT_MAX_VALUE);
844
845 /* randomize initial seqno to avoid collision */
846 get_random_bytes(&random_seqno, sizeof(random_seqno));
847 atomic_set(&bat_priv->bat_v.ogm_seqno, random_seqno);
848 INIT_DELAYED_WORK(&bat_priv->bat_v.ogm_wq, batadv_v_ogm_send);
849
Sven Eckelmann5841c1b2020-03-16 23:31:03 +0100850 mutex_init(&bat_priv->bat_v.ogm_buff_mutex);
851
Antonio Quartulli0da00352016-01-16 16:40:12 +0800852 return 0;
853}
854
855/**
856 * batadv_v_ogm_free - free OGM private resources
857 * @bat_priv: the bat priv with all the soft interface information
858 */
859void batadv_v_ogm_free(struct batadv_priv *bat_priv)
860{
861 cancel_delayed_work_sync(&bat_priv->bat_v.ogm_wq);
862
Sven Eckelmann5841c1b2020-03-16 23:31:03 +0100863 mutex_lock(&bat_priv->bat_v.ogm_buff_mutex);
864
Antonio Quartulli0da00352016-01-16 16:40:12 +0800865 kfree(bat_priv->bat_v.ogm_buff);
866 bat_priv->bat_v.ogm_buff = NULL;
867 bat_priv->bat_v.ogm_buff_len = 0;
Sven Eckelmann5841c1b2020-03-16 23:31:03 +0100868
869 mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex);
Antonio Quartulli0da00352016-01-16 16:40:12 +0800870}