blob: fad95ef64e01a9670ed66f9a81658718a14fc716 [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmann7a79d712018-12-31 23:59:59 +01002/* Copyright (C) 2013-2019 B.A.T.M.A.N. contributors:
Antonio Quartulli0da00352016-01-16 16:40:12 +08003 *
4 * Antonio Quartulli
Antonio Quartulli0da00352016-01-16 16:40:12 +08005 */
6
7#include "bat_v_ogm.h"
8#include "main.h"
9
10#include <linux/atomic.h>
11#include <linux/byteorder/generic.h>
12#include <linux/errno.h>
13#include <linux/etherdevice.h>
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010014#include <linux/gfp.h>
Antonio Quartulli0da00352016-01-16 16:40:12 +080015#include <linux/if_ether.h>
16#include <linux/jiffies.h>
17#include <linux/kernel.h>
Sven Eckelmann27353442016-03-05 16:09:16 +010018#include <linux/kref.h>
Antonio Quartulli93231582016-01-16 16:40:13 +080019#include <linux/list.h>
Antonio Quartulli0da00352016-01-16 16:40:12 +080020#include <linux/netdevice.h>
21#include <linux/random.h>
22#include <linux/rculist.h>
23#include <linux/rcupdate.h>
24#include <linux/skbuff.h>
25#include <linux/slab.h>
26#include <linux/stddef.h>
27#include <linux/string.h>
28#include <linux/types.h>
29#include <linux/workqueue.h>
Sven Eckelmannfec149f2017-12-21 10:17:41 +010030#include <uapi/linux/batadv_packet.h>
Antonio Quartulli0da00352016-01-16 16:40:12 +080031
Sven Eckelmann01d350d2016-05-15 11:07:44 +020032#include "bat_algo.h"
Antonio Quartulli0da00352016-01-16 16:40:12 +080033#include "hard-interface.h"
Antonio Quartulli93231582016-01-16 16:40:13 +080034#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020035#include "log.h"
Antonio Quartulli93231582016-01-16 16:40:13 +080036#include "originator.h"
Antonio Quartulli0da00352016-01-16 16:40:12 +080037#include "routing.h"
38#include "send.h"
39#include "translation-table.h"
Markus Pargmann1f8dce42016-05-15 11:07:43 +020040#include "tvlv.h"
Antonio Quartulli0da00352016-01-16 16:40:12 +080041
42/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010043 * batadv_v_ogm_orig_get() - retrieve and possibly create an originator node
Antonio Quartulli93231582016-01-16 16:40:13 +080044 * @bat_priv: the bat priv with all the soft interface information
45 * @addr: the address of the originator
46 *
47 * Return: the orig_node corresponding to the specified address. If such object
48 * does not exist it is allocated here. In case of allocation failure returns
49 * NULL.
50 */
51struct batadv_orig_node *batadv_v_ogm_orig_get(struct batadv_priv *bat_priv,
52 const u8 *addr)
53{
54 struct batadv_orig_node *orig_node;
55 int hash_added;
56
57 orig_node = batadv_orig_hash_find(bat_priv, addr);
58 if (orig_node)
59 return orig_node;
60
61 orig_node = batadv_orig_node_new(bat_priv, addr);
62 if (!orig_node)
63 return NULL;
64
Sven Eckelmann55db2d52016-07-15 17:39:21 +020065 kref_get(&orig_node->refcount);
Antonio Quartulli93231582016-01-16 16:40:13 +080066 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
67 batadv_choose_orig, orig_node,
68 &orig_node->hash_entry);
69 if (hash_added != 0) {
Sven Eckelmann55db2d52016-07-15 17:39:21 +020070 /* remove refcnt for newly created orig_node and hash entry */
Antonio Quartulli93231582016-01-16 16:40:13 +080071 batadv_orig_node_put(orig_node);
72 batadv_orig_node_put(orig_node);
73 orig_node = NULL;
74 }
75
76 return orig_node;
77}
78
79/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010080 * batadv_v_ogm_start_timer() - restart the OGM sending timer
Antonio Quartulli0da00352016-01-16 16:40:12 +080081 * @bat_priv: the bat priv with all the soft interface information
82 */
83static void batadv_v_ogm_start_timer(struct batadv_priv *bat_priv)
84{
85 unsigned long msecs;
86 /* this function may be invoked in different contexts (ogm rescheduling
87 * or hard_iface activation), but the work timer should not be reset
88 */
89 if (delayed_work_pending(&bat_priv->bat_v.ogm_wq))
90 return;
91
92 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
93 msecs += prandom_u32() % (2 * BATADV_JITTER);
94 queue_delayed_work(batadv_event_workqueue, &bat_priv->bat_v.ogm_wq,
95 msecs_to_jiffies(msecs));
96}
97
98/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010099 * batadv_v_ogm_send_to_if() - send a batman ogm using a given interface
Antonio Quartulli0da00352016-01-16 16:40:12 +0800100 * @skb: the OGM to send
101 * @hard_iface: the interface to use to send the OGM
102 */
103static void batadv_v_ogm_send_to_if(struct sk_buff *skb,
104 struct batadv_hard_iface *hard_iface)
105{
106 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
107
108 if (hard_iface->if_status != BATADV_IF_ACTIVE)
109 return;
110
111 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
112 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
113 skb->len + ETH_HLEN);
114
Antonio Quartulli95d39272016-01-16 16:40:15 +0800115 batadv_send_broadcast_skb(skb, hard_iface);
Antonio Quartulli0da00352016-01-16 16:40:12 +0800116}
117
118/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100119 * batadv_v_ogm_send() - periodic worker broadcasting the own OGM
Antonio Quartulli0da00352016-01-16 16:40:12 +0800120 * @work: work queue item
121 */
122static void batadv_v_ogm_send(struct work_struct *work)
123{
124 struct batadv_hard_iface *hard_iface;
125 struct batadv_priv_bat_v *bat_v;
126 struct batadv_priv *bat_priv;
127 struct batadv_ogm2_packet *ogm_packet;
128 struct sk_buff *skb, *skb_tmp;
Sven Eckelmanne04de482017-06-18 09:59:28 +0200129 unsigned char *ogm_buff;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800130 int ogm_buff_len;
131 u16 tvlv_len = 0;
Linus Lüssing3111bee2016-08-07 12:34:19 +0200132 int ret;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800133
134 bat_v = container_of(work, struct batadv_priv_bat_v, ogm_wq.work);
135 bat_priv = container_of(bat_v, struct batadv_priv, bat_v);
136
137 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
138 goto out;
139
140 ogm_buff = bat_priv->bat_v.ogm_buff;
141 ogm_buff_len = bat_priv->bat_v.ogm_buff_len;
142 /* tt changes have to be committed before the tvlv data is
143 * appended as it may alter the tt tvlv container
144 */
145 batadv_tt_local_commit_changes(bat_priv);
146 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, &ogm_buff,
147 &ogm_buff_len,
148 BATADV_OGM2_HLEN);
149
150 bat_priv->bat_v.ogm_buff = ogm_buff;
151 bat_priv->bat_v.ogm_buff_len = ogm_buff_len;
152
153 skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + ogm_buff_len);
154 if (!skb)
155 goto reschedule;
156
157 skb_reserve(skb, ETH_HLEN);
Sven Eckelmanne04de482017-06-18 09:59:28 +0200158 skb_put_data(skb, ogm_buff, ogm_buff_len);
Antonio Quartulli0da00352016-01-16 16:40:12 +0800159
160 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
161 ogm_packet->seqno = htonl(atomic_read(&bat_priv->bat_v.ogm_seqno));
162 atomic_inc(&bat_priv->bat_v.ogm_seqno);
163 ogm_packet->tvlv_len = htons(tvlv_len);
164
165 /* broadcast on every interface */
166 rcu_read_lock();
167 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
168 if (hard_iface->soft_iface != bat_priv->soft_iface)
169 continue;
170
Sven Eckelmann27353442016-03-05 16:09:16 +0100171 if (!kref_get_unless_zero(&hard_iface->refcount))
172 continue;
173
Linus Lüssing3111bee2016-08-07 12:34:19 +0200174 ret = batadv_hardif_no_broadcast(hard_iface, NULL, NULL);
175 if (ret) {
176 char *type;
177
178 switch (ret) {
179 case BATADV_HARDIF_BCAST_NORECIPIENT:
180 type = "no neighbor";
181 break;
182 case BATADV_HARDIF_BCAST_DUPFWD:
183 type = "single neighbor is source";
184 break;
185 case BATADV_HARDIF_BCAST_DUPORIG:
186 type = "single neighbor is originator";
187 break;
188 default:
189 type = "unknown";
190 }
191
Colin Ian Kingf25cbb22017-06-26 11:26:44 +0100192 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 from ourselves on %s suppressed: %s\n",
Linus Lüssing3111bee2016-08-07 12:34:19 +0200193 hard_iface->net_dev->name, type);
194
195 batadv_hardif_put(hard_iface);
196 continue;
197 }
198
Antonio Quartulli0da00352016-01-16 16:40:12 +0800199 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
200 "Sending own OGM2 packet (originator %pM, seqno %u, throughput %u, TTL %d) on interface %s [%pM]\n",
201 ogm_packet->orig, ntohl(ogm_packet->seqno),
202 ntohl(ogm_packet->throughput), ogm_packet->ttl,
203 hard_iface->net_dev->name,
204 hard_iface->net_dev->dev_addr);
205
206 /* this skb gets consumed by batadv_v_ogm_send_to_if() */
207 skb_tmp = skb_clone(skb, GFP_ATOMIC);
Sven Eckelmann27353442016-03-05 16:09:16 +0100208 if (!skb_tmp) {
209 batadv_hardif_put(hard_iface);
Antonio Quartulli0da00352016-01-16 16:40:12 +0800210 break;
Sven Eckelmann27353442016-03-05 16:09:16 +0100211 }
Antonio Quartulli0da00352016-01-16 16:40:12 +0800212
213 batadv_v_ogm_send_to_if(skb_tmp, hard_iface);
Sven Eckelmann27353442016-03-05 16:09:16 +0100214 batadv_hardif_put(hard_iface);
Antonio Quartulli0da00352016-01-16 16:40:12 +0800215 }
216 rcu_read_unlock();
217
218 consume_skb(skb);
219
220reschedule:
221 batadv_v_ogm_start_timer(bat_priv);
222out:
223 return;
224}
225
226/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100227 * batadv_v_ogm_iface_enable() - prepare an interface for B.A.T.M.A.N. V
Antonio Quartulli0da00352016-01-16 16:40:12 +0800228 * @hard_iface: the interface to prepare
229 *
230 * Takes care of scheduling own OGM sending routine for this interface.
231 *
232 * Return: 0 on success or a negative error code otherwise
233 */
234int batadv_v_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
235{
236 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
237
238 batadv_v_ogm_start_timer(bat_priv);
239
240 return 0;
241}
242
243/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100244 * batadv_v_ogm_primary_iface_set() - set a new primary interface
Antonio Quartulli0da00352016-01-16 16:40:12 +0800245 * @primary_iface: the new primary interface
246 */
247void batadv_v_ogm_primary_iface_set(struct batadv_hard_iface *primary_iface)
248{
249 struct batadv_priv *bat_priv = netdev_priv(primary_iface->soft_iface);
250 struct batadv_ogm2_packet *ogm_packet;
251
252 if (!bat_priv->bat_v.ogm_buff)
253 return;
254
255 ogm_packet = (struct batadv_ogm2_packet *)bat_priv->bat_v.ogm_buff;
256 ether_addr_copy(ogm_packet->orig, primary_iface->net_dev->dev_addr);
257}
258
259/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100260 * batadv_v_forward_penalty() - apply a penalty to the throughput metric
261 * forwarded with B.A.T.M.A.N. V OGMs
Antonio Quartulli93231582016-01-16 16:40:13 +0800262 * @bat_priv: the bat priv with all the soft interface information
263 * @if_incoming: the interface where the OGM has been received
264 * @if_outgoing: the interface where the OGM has to be forwarded to
265 * @throughput: the current throughput
266 *
267 * Apply a penalty on the current throughput metric value based on the
268 * characteristic of the interface where the OGM has been received. The return
269 * value is computed as follows:
270 * - throughput * 50% if the incoming and outgoing interface are the
271 * same WiFi interface and the throughput is above
272 * 1MBit/s
273 * - throughput if the outgoing interface is the default
274 * interface (i.e. this OGM is processed for the
275 * internal table and not forwarded)
276 * - throughput * hop penalty otherwise
277 *
278 * Return: the penalised throughput metric.
279 */
280static u32 batadv_v_forward_penalty(struct batadv_priv *bat_priv,
281 struct batadv_hard_iface *if_incoming,
282 struct batadv_hard_iface *if_outgoing,
283 u32 throughput)
284{
285 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
286 int hop_penalty_max = BATADV_TQ_MAX_VALUE;
287
288 /* Don't apply hop penalty in default originator table. */
289 if (if_outgoing == BATADV_IF_DEFAULT)
290 return throughput;
291
292 /* Forwarding on the same WiFi interface cuts the throughput in half
293 * due to the store & forward characteristics of WIFI.
294 * Very low throughput values are the exception.
295 */
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200296 if (throughput > 10 &&
297 if_incoming == if_outgoing &&
Antonio Quartullic8334842015-11-10 18:50:51 +0100298 !(if_incoming->bat_v.flags & BATADV_FULL_DUPLEX))
Antonio Quartulli93231582016-01-16 16:40:13 +0800299 return throughput / 2;
300
301 /* hop penalty of 255 equals 100% */
302 return throughput * (hop_penalty_max - hop_penalty) / hop_penalty_max;
303}
304
305/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100306 * batadv_v_ogm_forward() - check conditions and forward an OGM to the given
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100307 * outgoing interface
Antonio Quartulli93231582016-01-16 16:40:13 +0800308 * @bat_priv: the bat priv with all the soft interface information
309 * @ogm_received: previously received OGM to be forwarded
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100310 * @orig_node: the originator which has been updated
311 * @neigh_node: the neigh_node through with the OGM has been received
Antonio Quartulli93231582016-01-16 16:40:13 +0800312 * @if_incoming: the interface on which this OGM was received on
313 * @if_outgoing: the interface to which the OGM has to be forwarded to
314 *
315 * Forward an OGM to an interface after having altered the throughput metric and
316 * the TTL value contained in it. The original OGM isn't modified.
317 */
318static void batadv_v_ogm_forward(struct batadv_priv *bat_priv,
319 const struct batadv_ogm2_packet *ogm_received,
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100320 struct batadv_orig_node *orig_node,
321 struct batadv_neigh_node *neigh_node,
Antonio Quartulli93231582016-01-16 16:40:13 +0800322 struct batadv_hard_iface *if_incoming,
323 struct batadv_hard_iface *if_outgoing)
324{
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100325 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
326 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
327 struct batadv_neigh_node *router = NULL;
Antonio Quartulli93231582016-01-16 16:40:13 +0800328 struct batadv_ogm2_packet *ogm_forward;
329 unsigned char *skb_buff;
330 struct sk_buff *skb;
331 size_t packet_len;
332 u16 tvlv_len;
333
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100334 /* only forward for specific interfaces, not for the default one. */
335 if (if_outgoing == BATADV_IF_DEFAULT)
336 goto out;
337
338 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
339 if (!orig_ifinfo)
340 goto out;
341
342 /* acquire possibly updated router */
343 router = batadv_orig_router_get(orig_node, if_outgoing);
344
345 /* strict rule: forward packets coming from the best next hop only */
346 if (neigh_node != router)
347 goto out;
348
349 /* don't forward the same seqno twice on one interface */
350 if (orig_ifinfo->last_seqno_forwarded == ntohl(ogm_received->seqno))
351 goto out;
352
353 orig_ifinfo->last_seqno_forwarded = ntohl(ogm_received->seqno);
354
Antonio Quartulli93231582016-01-16 16:40:13 +0800355 if (ogm_received->ttl <= 1) {
356 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100357 goto out;
Antonio Quartulli93231582016-01-16 16:40:13 +0800358 }
359
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100360 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
361 if (!neigh_ifinfo)
362 goto out;
363
Antonio Quartulli93231582016-01-16 16:40:13 +0800364 tvlv_len = ntohs(ogm_received->tvlv_len);
365
366 packet_len = BATADV_OGM2_HLEN + tvlv_len;
367 skb = netdev_alloc_skb_ip_align(if_outgoing->net_dev,
368 ETH_HLEN + packet_len);
369 if (!skb)
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100370 goto out;
Antonio Quartulli93231582016-01-16 16:40:13 +0800371
372 skb_reserve(skb, ETH_HLEN);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200373 skb_buff = skb_put_data(skb, ogm_received, packet_len);
Antonio Quartulli93231582016-01-16 16:40:13 +0800374
375 /* apply forward penalty */
376 ogm_forward = (struct batadv_ogm2_packet *)skb_buff;
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100377 ogm_forward->throughput = htonl(neigh_ifinfo->bat_v.throughput);
Antonio Quartulli93231582016-01-16 16:40:13 +0800378 ogm_forward->ttl--;
379
380 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
381 "Forwarding OGM2 packet on %s: throughput %u, ttl %u, received via %s\n",
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100382 if_outgoing->net_dev->name, ntohl(ogm_forward->throughput),
383 ogm_forward->ttl, if_incoming->net_dev->name);
Antonio Quartulli93231582016-01-16 16:40:13 +0800384
385 batadv_v_ogm_send_to_if(skb, if_outgoing);
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100386
387out:
388 if (orig_ifinfo)
389 batadv_orig_ifinfo_put(orig_ifinfo);
390 if (router)
391 batadv_neigh_node_put(router);
392 if (neigh_ifinfo)
393 batadv_neigh_ifinfo_put(neigh_ifinfo);
Antonio Quartulli93231582016-01-16 16:40:13 +0800394}
395
396/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100397 * batadv_v_ogm_metric_update() - update route metric based on OGM
Antonio Quartulli93231582016-01-16 16:40:13 +0800398 * @bat_priv: the bat priv with all the soft interface information
399 * @ogm2: OGM2 structure
400 * @orig_node: Originator structure for which the OGM has been received
401 * @neigh_node: the neigh_node through with the OGM has been received
402 * @if_incoming: the interface where this packet was received
403 * @if_outgoing: the interface for which the packet should be considered
404 *
405 * Return:
406 * 1 if the OGM is new,
407 * 0 if it is not new but valid,
408 * <0 on error (e.g. old OGM)
409 */
410static int batadv_v_ogm_metric_update(struct batadv_priv *bat_priv,
411 const struct batadv_ogm2_packet *ogm2,
412 struct batadv_orig_node *orig_node,
413 struct batadv_neigh_node *neigh_node,
414 struct batadv_hard_iface *if_incoming,
415 struct batadv_hard_iface *if_outgoing)
416{
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200417 struct batadv_orig_ifinfo *orig_ifinfo;
Antonio Quartulli93231582016-01-16 16:40:13 +0800418 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
419 bool protection_started = false;
420 int ret = -EINVAL;
421 u32 path_throughput;
422 s32 seq_diff;
423
424 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
425 if (!orig_ifinfo)
426 goto out;
427
428 seq_diff = ntohl(ogm2->seqno) - orig_ifinfo->last_real_seqno;
429
430 if (!hlist_empty(&orig_node->neigh_list) &&
431 batadv_window_protected(bat_priv, seq_diff,
432 BATADV_OGM_MAX_AGE,
433 &orig_ifinfo->batman_seqno_reset,
434 &protection_started)) {
435 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
436 "Drop packet: packet within window protection time from %pM\n",
437 ogm2->orig);
438 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
439 "Last reset: %ld, %ld\n",
440 orig_ifinfo->batman_seqno_reset, jiffies);
441 goto out;
442 }
443
444 /* drop packets with old seqnos, however accept the first packet after
445 * a host has been rebooted.
446 */
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200447 if (seq_diff < 0 && !protection_started)
Antonio Quartulli93231582016-01-16 16:40:13 +0800448 goto out;
449
450 neigh_node->last_seen = jiffies;
451
452 orig_node->last_seen = jiffies;
453
454 orig_ifinfo->last_real_seqno = ntohl(ogm2->seqno);
455 orig_ifinfo->last_ttl = ogm2->ttl;
456
457 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
458 if (!neigh_ifinfo)
459 goto out;
460
461 path_throughput = batadv_v_forward_penalty(bat_priv, if_incoming,
462 if_outgoing,
463 ntohl(ogm2->throughput));
464 neigh_ifinfo->bat_v.throughput = path_throughput;
465 neigh_ifinfo->bat_v.last_seqno = ntohl(ogm2->seqno);
466 neigh_ifinfo->last_ttl = ogm2->ttl;
467
468 if (seq_diff > 0 || protection_started)
469 ret = 1;
470 else
471 ret = 0;
472out:
473 if (orig_ifinfo)
474 batadv_orig_ifinfo_put(orig_ifinfo);
475 if (neigh_ifinfo)
476 batadv_neigh_ifinfo_put(neigh_ifinfo);
477
478 return ret;
479}
480
481/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100482 * batadv_v_ogm_route_update() - update routes based on OGM
Antonio Quartulli93231582016-01-16 16:40:13 +0800483 * @bat_priv: the bat priv with all the soft interface information
484 * @ethhdr: the Ethernet header of the OGM2
485 * @ogm2: OGM2 structure
486 * @orig_node: Originator structure for which the OGM has been received
487 * @neigh_node: the neigh_node through with the OGM has been received
488 * @if_incoming: the interface where this packet was received
489 * @if_outgoing: the interface for which the packet should be considered
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100490 *
491 * Return: true if the packet should be forwarded, false otherwise
Antonio Quartulli93231582016-01-16 16:40:13 +0800492 */
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100493static bool batadv_v_ogm_route_update(struct batadv_priv *bat_priv,
Antonio Quartulli93231582016-01-16 16:40:13 +0800494 const struct ethhdr *ethhdr,
495 const struct batadv_ogm2_packet *ogm2,
496 struct batadv_orig_node *orig_node,
497 struct batadv_neigh_node *neigh_node,
498 struct batadv_hard_iface *if_incoming,
499 struct batadv_hard_iface *if_outgoing)
500{
501 struct batadv_neigh_node *router = NULL;
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200502 struct batadv_orig_node *orig_neigh_node;
Antonio Quartulli93231582016-01-16 16:40:13 +0800503 struct batadv_neigh_node *orig_neigh_router = NULL;
Simon Wunderlich86de37c2016-02-01 15:21:38 +0100504 struct batadv_neigh_ifinfo *router_ifinfo = NULL, *neigh_ifinfo = NULL;
505 u32 router_throughput, neigh_throughput;
506 u32 router_last_seqno;
507 u32 neigh_last_seqno;
508 s32 neigh_seq_diff;
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100509 bool forward = false;
Antonio Quartulli93231582016-01-16 16:40:13 +0800510
511 orig_neigh_node = batadv_v_ogm_orig_get(bat_priv, ethhdr->h_source);
512 if (!orig_neigh_node)
513 goto out;
514
515 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
516 if_outgoing);
517
518 /* drop packet if sender is not a direct neighbor and if we
519 * don't route towards it
520 */
521 router = batadv_orig_router_get(orig_node, if_outgoing);
522 if (router && router->orig_node != orig_node && !orig_neigh_router) {
523 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
524 "Drop packet: OGM via unknown neighbor!\n");
525 goto out;
526 }
527
Simon Wunderlich86de37c2016-02-01 15:21:38 +0100528 /* Mark the OGM to be considered for forwarding, and update routes
529 * if needed.
530 */
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100531 forward = true;
Simon Wunderlich86de37c2016-02-01 15:21:38 +0100532
533 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
534 "Searching and updating originator entry of received packet\n");
535
536 /* if this neighbor already is our next hop there is nothing
537 * to change
538 */
539 if (router == neigh_node)
540 goto out;
541
542 /* don't consider neighbours with worse throughput.
543 * also switch route if this seqno is BATADV_V_MAX_ORIGDIFF newer than
544 * the last received seqno from our best next hop.
545 */
546 if (router) {
547 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
548 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
549
550 /* if these are not allocated, something is wrong. */
551 if (!router_ifinfo || !neigh_ifinfo)
552 goto out;
553
554 neigh_last_seqno = neigh_ifinfo->bat_v.last_seqno;
555 router_last_seqno = router_ifinfo->bat_v.last_seqno;
556 neigh_seq_diff = neigh_last_seqno - router_last_seqno;
557 router_throughput = router_ifinfo->bat_v.throughput;
558 neigh_throughput = neigh_ifinfo->bat_v.throughput;
559
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200560 if (neigh_seq_diff < BATADV_OGM_MAX_ORIGDIFF &&
561 router_throughput >= neigh_throughput)
Simon Wunderlich86de37c2016-02-01 15:21:38 +0100562 goto out;
563 }
564
565 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
Antonio Quartulli93231582016-01-16 16:40:13 +0800566out:
Antonio Quartulli93231582016-01-16 16:40:13 +0800567 if (router)
568 batadv_neigh_node_put(router);
569 if (orig_neigh_router)
570 batadv_neigh_node_put(orig_neigh_router);
571 if (orig_neigh_node)
572 batadv_orig_node_put(orig_neigh_node);
Simon Wunderlich86de37c2016-02-01 15:21:38 +0100573 if (router_ifinfo)
574 batadv_neigh_ifinfo_put(router_ifinfo);
575 if (neigh_ifinfo)
576 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100577
578 return forward;
Antonio Quartulli93231582016-01-16 16:40:13 +0800579}
580
581/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100582 * batadv_v_ogm_process_per_outif() - process a batman v OGM for an outgoing if
Antonio Quartulli93231582016-01-16 16:40:13 +0800583 * @bat_priv: the bat priv with all the soft interface information
584 * @ethhdr: the Ethernet header of the OGM2
585 * @ogm2: OGM2 structure
586 * @orig_node: Originator structure for which the OGM has been received
587 * @neigh_node: the neigh_node through with the OGM has been received
588 * @if_incoming: the interface where this packet was received
589 * @if_outgoing: the interface for which the packet should be considered
590 */
591static void
592batadv_v_ogm_process_per_outif(struct batadv_priv *bat_priv,
593 const struct ethhdr *ethhdr,
594 const struct batadv_ogm2_packet *ogm2,
595 struct batadv_orig_node *orig_node,
596 struct batadv_neigh_node *neigh_node,
597 struct batadv_hard_iface *if_incoming,
598 struct batadv_hard_iface *if_outgoing)
599{
600 int seqno_age;
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100601 bool forward;
Antonio Quartulli93231582016-01-16 16:40:13 +0800602
603 /* first, update the metric with according sanity checks */
604 seqno_age = batadv_v_ogm_metric_update(bat_priv, ogm2, orig_node,
605 neigh_node, if_incoming,
606 if_outgoing);
607
608 /* outdated sequence numbers are to be discarded */
609 if (seqno_age < 0)
610 return;
611
612 /* only unknown & newer OGMs contain TVLVs we are interested in */
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200613 if (seqno_age > 0 && if_outgoing == BATADV_IF_DEFAULT)
Antonio Quartulli93231582016-01-16 16:40:13 +0800614 batadv_tvlv_containers_process(bat_priv, true, orig_node,
615 NULL, NULL,
616 (unsigned char *)(ogm2 + 1),
617 ntohs(ogm2->tvlv_len));
618
619 /* if the metric update went through, update routes if needed */
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100620 forward = batadv_v_ogm_route_update(bat_priv, ethhdr, ogm2, orig_node,
621 neigh_node, if_incoming,
622 if_outgoing);
623
624 /* if the routes have been processed correctly, check and forward */
625 if (forward)
626 batadv_v_ogm_forward(bat_priv, ogm2, orig_node, neigh_node,
627 if_incoming, if_outgoing);
Antonio Quartulli93231582016-01-16 16:40:13 +0800628}
629
630/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100631 * batadv_v_ogm_aggr_packet() - checks if there is another OGM aggregated
Antonio Quartulli93231582016-01-16 16:40:13 +0800632 * @buff_pos: current position in the skb
633 * @packet_len: total length of the skb
634 * @tvlv_len: tvlv length of the previously considered OGM
635 *
636 * Return: true if there is enough space for another OGM, false otherwise.
637 */
638static bool batadv_v_ogm_aggr_packet(int buff_pos, int packet_len,
639 __be16 tvlv_len)
640{
641 int next_buff_pos = 0;
642
643 next_buff_pos += buff_pos + BATADV_OGM2_HLEN;
644 next_buff_pos += ntohs(tvlv_len);
645
646 return (next_buff_pos <= packet_len) &&
647 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
648}
649
650/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100651 * batadv_v_ogm_process() - process an incoming batman v OGM
Antonio Quartulli93231582016-01-16 16:40:13 +0800652 * @skb: the skb containing the OGM
653 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
654 * @if_incoming: the interface where this packet was receved
655 */
656static void batadv_v_ogm_process(const struct sk_buff *skb, int ogm_offset,
657 struct batadv_hard_iface *if_incoming)
658{
659 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
660 struct ethhdr *ethhdr;
661 struct batadv_orig_node *orig_node = NULL;
662 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
663 struct batadv_neigh_node *neigh_node = NULL;
664 struct batadv_hard_iface *hard_iface;
665 struct batadv_ogm2_packet *ogm_packet;
666 u32 ogm_throughput, link_throughput, path_throughput;
Linus Lüssing3111bee2016-08-07 12:34:19 +0200667 int ret;
Antonio Quartulli93231582016-01-16 16:40:13 +0800668
669 ethhdr = eth_hdr(skb);
670 ogm_packet = (struct batadv_ogm2_packet *)(skb->data + ogm_offset);
671
672 ogm_throughput = ntohl(ogm_packet->throughput);
673
674 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Colin Ian Kingf25cbb22017-06-26 11:26:44 +0100675 "Received OGM2 packet via NB: %pM, IF: %s [%pM] (from OG: %pM, seqno %u, throughput %u, TTL %u, V %u, tvlv_len %u)\n",
Antonio Quartulli93231582016-01-16 16:40:13 +0800676 ethhdr->h_source, if_incoming->net_dev->name,
677 if_incoming->net_dev->dev_addr, ogm_packet->orig,
678 ntohl(ogm_packet->seqno), ogm_throughput, ogm_packet->ttl,
679 ogm_packet->version, ntohs(ogm_packet->tvlv_len));
680
Colin Ian Kingf25cbb22017-06-26 11:26:44 +0100681 /* If the throughput metric is 0, immediately drop the packet. No need
682 * to create orig_node / neigh_node for an unusable route.
Antonio Quartulli93231582016-01-16 16:40:13 +0800683 */
684 if (ogm_throughput == 0) {
685 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Colin Ian Kingf25cbb22017-06-26 11:26:44 +0100686 "Drop packet: originator packet with throughput metric of 0\n");
Antonio Quartulli93231582016-01-16 16:40:13 +0800687 return;
688 }
689
690 /* require ELP packets be to received from this neighbor first */
691 hardif_neigh = batadv_hardif_neigh_get(if_incoming, ethhdr->h_source);
692 if (!hardif_neigh) {
693 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
694 "Drop packet: OGM via unknown neighbor!\n");
695 goto out;
696 }
697
698 orig_node = batadv_v_ogm_orig_get(bat_priv, ogm_packet->orig);
699 if (!orig_node)
700 return;
701
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800702 neigh_node = batadv_neigh_node_get_or_create(orig_node, if_incoming,
703 ethhdr->h_source);
Antonio Quartulli93231582016-01-16 16:40:13 +0800704 if (!neigh_node)
705 goto out;
706
707 /* Update the received throughput metric to match the link
708 * characteristic:
709 * - If this OGM traveled one hop so far (emitted by single hop
710 * neighbor) the path throughput metric equals the link throughput.
711 * - For OGMs traversing more than hop the path throughput metric is
712 * the smaller of the path throughput and the link throughput.
713 */
714 link_throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
715 path_throughput = min_t(u32, link_throughput, ogm_throughput);
716 ogm_packet->throughput = htonl(path_throughput);
717
718 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet, orig_node,
719 neigh_node, if_incoming,
720 BATADV_IF_DEFAULT);
721
722 rcu_read_lock();
723 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
724 if (hard_iface->if_status != BATADV_IF_ACTIVE)
725 continue;
726
727 if (hard_iface->soft_iface != bat_priv->soft_iface)
728 continue;
729
Sven Eckelmann27353442016-03-05 16:09:16 +0100730 if (!kref_get_unless_zero(&hard_iface->refcount))
731 continue;
732
Linus Lüssing3111bee2016-08-07 12:34:19 +0200733 ret = batadv_hardif_no_broadcast(hard_iface,
734 ogm_packet->orig,
735 hardif_neigh->orig);
736
737 if (ret) {
738 char *type;
739
740 switch (ret) {
741 case BATADV_HARDIF_BCAST_NORECIPIENT:
742 type = "no neighbor";
743 break;
744 case BATADV_HARDIF_BCAST_DUPFWD:
745 type = "single neighbor is source";
746 break;
747 case BATADV_HARDIF_BCAST_DUPORIG:
748 type = "single neighbor is originator";
749 break;
750 default:
751 type = "unknown";
752 }
753
Colin Ian Kingf25cbb22017-06-26 11:26:44 +0100754 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 packet from %pM on %s suppressed: %s\n",
Linus Lüssing3111bee2016-08-07 12:34:19 +0200755 ogm_packet->orig, hard_iface->net_dev->name,
756 type);
757
758 batadv_hardif_put(hard_iface);
759 continue;
760 }
761
Antonio Quartulli93231582016-01-16 16:40:13 +0800762 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet,
763 orig_node, neigh_node,
764 if_incoming, hard_iface);
Sven Eckelmann27353442016-03-05 16:09:16 +0100765
766 batadv_hardif_put(hard_iface);
Antonio Quartulli93231582016-01-16 16:40:13 +0800767 }
768 rcu_read_unlock();
769out:
770 if (orig_node)
771 batadv_orig_node_put(orig_node);
772 if (neigh_node)
773 batadv_neigh_node_put(neigh_node);
774 if (hardif_neigh)
775 batadv_hardif_neigh_put(hardif_neigh);
776}
777
778/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100779 * batadv_v_ogm_packet_recv() - OGM2 receiving handler
Antonio Quartulli0da00352016-01-16 16:40:12 +0800780 * @skb: the received OGM
781 * @if_incoming: the interface where this OGM has been received
782 *
783 * Return: NET_RX_SUCCESS and consume the skb on success or returns NET_RX_DROP
784 * (without freeing the skb) on failure
785 */
786int batadv_v_ogm_packet_recv(struct sk_buff *skb,
787 struct batadv_hard_iface *if_incoming)
788{
789 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
790 struct batadv_ogm2_packet *ogm_packet;
791 struct ethhdr *ethhdr = eth_hdr(skb);
Antonio Quartulli93231582016-01-16 16:40:13 +0800792 int ogm_offset;
793 u8 *packet_pos;
794 int ret = NET_RX_DROP;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800795
796 /* did we receive a OGM2 packet on an interface that does not have
797 * B.A.T.M.A.N. V enabled ?
798 */
Antonio Quartulli29824a52016-05-25 23:27:31 +0800799 if (strcmp(bat_priv->algo_ops->name, "BATMAN_V") != 0)
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200800 goto free_skb;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800801
802 if (!batadv_check_management_packet(skb, if_incoming, BATADV_OGM2_HLEN))
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200803 goto free_skb;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800804
805 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200806 goto free_skb;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800807
808 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
809
810 if (batadv_is_my_mac(bat_priv, ogm_packet->orig))
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200811 goto free_skb;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800812
813 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
814 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
815 skb->len + ETH_HLEN);
816
Antonio Quartulli93231582016-01-16 16:40:13 +0800817 ogm_offset = 0;
818 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
819
820 while (batadv_v_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
821 ogm_packet->tvlv_len)) {
822 batadv_v_ogm_process(skb, ogm_offset, if_incoming);
823
824 ogm_offset += BATADV_OGM2_HLEN;
825 ogm_offset += ntohs(ogm_packet->tvlv_len);
826
827 packet_pos = skb->data + ogm_offset;
828 ogm_packet = (struct batadv_ogm2_packet *)packet_pos;
829 }
830
831 ret = NET_RX_SUCCESS;
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200832
833free_skb:
834 if (ret == NET_RX_SUCCESS)
835 consume_skb(skb);
836 else
837 kfree_skb(skb);
Antonio Quartulli93231582016-01-16 16:40:13 +0800838
839 return ret;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800840}
841
842/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100843 * batadv_v_ogm_init() - initialise the OGM2 engine
Antonio Quartulli0da00352016-01-16 16:40:12 +0800844 * @bat_priv: the bat priv with all the soft interface information
845 *
846 * Return: 0 on success or a negative error code in case of failure
847 */
848int batadv_v_ogm_init(struct batadv_priv *bat_priv)
849{
850 struct batadv_ogm2_packet *ogm_packet;
851 unsigned char *ogm_buff;
852 u32 random_seqno;
853
854 bat_priv->bat_v.ogm_buff_len = BATADV_OGM2_HLEN;
855 ogm_buff = kzalloc(bat_priv->bat_v.ogm_buff_len, GFP_ATOMIC);
856 if (!ogm_buff)
857 return -ENOMEM;
858
859 bat_priv->bat_v.ogm_buff = ogm_buff;
860 ogm_packet = (struct batadv_ogm2_packet *)ogm_buff;
861 ogm_packet->packet_type = BATADV_OGM2;
862 ogm_packet->version = BATADV_COMPAT_VERSION;
863 ogm_packet->ttl = BATADV_TTL;
864 ogm_packet->flags = BATADV_NO_FLAGS;
865 ogm_packet->throughput = htonl(BATADV_THROUGHPUT_MAX_VALUE);
866
867 /* randomize initial seqno to avoid collision */
868 get_random_bytes(&random_seqno, sizeof(random_seqno));
869 atomic_set(&bat_priv->bat_v.ogm_seqno, random_seqno);
870 INIT_DELAYED_WORK(&bat_priv->bat_v.ogm_wq, batadv_v_ogm_send);
871
872 return 0;
873}
874
875/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100876 * batadv_v_ogm_free() - free OGM private resources
Antonio Quartulli0da00352016-01-16 16:40:12 +0800877 * @bat_priv: the bat priv with all the soft interface information
878 */
879void batadv_v_ogm_free(struct batadv_priv *bat_priv)
880{
881 cancel_delayed_work_sync(&bat_priv->bat_v.ogm_wq);
882
883 kfree(bat_priv->bat_v.ogm_buff);
884 bat_priv->bat_v.ogm_buff = NULL;
885 bat_priv->bat_v.ogm_buff_len = 0;
886}