blob: c251445a42a076fcdb06a25b2ddbe67bbb1ac052 [file] [log] [blame]
Sven Eckelmannac79cbb2017-01-01 00:00:00 +01001/* Copyright (C) 2013-2017 B.A.T.M.A.N. contributors:
Antonio Quartulli0da00352016-01-16 16:40:12 +08002 *
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>
Antonio Quartulli0da00352016-01-16 16:40:12 +080031#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
Sven Eckelmann01d350d2016-05-15 11:07:44 +020042#include "bat_algo.h"
Antonio Quartulli0da00352016-01-16 16:40:12 +080043#include "hard-interface.h"
Antonio Quartulli93231582016-01-16 16:40:13 +080044#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020045#include "log.h"
Antonio Quartulli93231582016-01-16 16:40:13 +080046#include "originator.h"
Antonio Quartulli0da00352016-01-16 16:40:12 +080047#include "packet.h"
48#include "routing.h"
49#include "send.h"
50#include "translation-table.h"
Markus Pargmann1f8dce42016-05-15 11:07:43 +020051#include "tvlv.h"
Antonio Quartulli0da00352016-01-16 16:40:12 +080052
53/**
Antonio Quartulli93231582016-01-16 16:40:13 +080054 * 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 */
62struct 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
Sven Eckelmann55db2d52016-07-15 17:39:21 +020076 kref_get(&orig_node->refcount);
Antonio Quartulli93231582016-01-16 16:40:13 +080077 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) {
Sven Eckelmann55db2d52016-07-15 17:39:21 +020081 /* remove refcnt for newly created orig_node and hash entry */
Antonio Quartulli93231582016-01-16 16:40:13 +080082 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/**
Antonio Quartulli0da00352016-01-16 16:40:12 +080091 * batadv_v_ogm_start_timer - restart the OGM sending timer
92 * @bat_priv: the bat priv with all the soft interface information
93 */
94static 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 */
114static 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
Antonio Quartulli95d39272016-01-16 16:40:15 +0800126 batadv_send_broadcast_skb(skb, hard_iface);
Antonio Quartulli0da00352016-01-16 16:40:12 +0800127}
128
129/**
130 * batadv_v_ogm_send - periodic worker broadcasting the own OGM
131 * @work: work queue item
132 */
133static 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;
Sven Eckelmanne04de482017-06-18 09:59:28 +0200140 unsigned char *ogm_buff;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800141 int ogm_buff_len;
142 u16 tvlv_len = 0;
Linus Lüssing3111bee2016-08-07 12:34:19 +0200143 int ret;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800144
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);
Sven Eckelmanne04de482017-06-18 09:59:28 +0200169 skb_put_data(skb, ogm_buff, ogm_buff_len);
Antonio Quartulli0da00352016-01-16 16:40:12 +0800170
171 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
172 ogm_packet->seqno = htonl(atomic_read(&bat_priv->bat_v.ogm_seqno));
173 atomic_inc(&bat_priv->bat_v.ogm_seqno);
174 ogm_packet->tvlv_len = htons(tvlv_len);
175
176 /* broadcast on every interface */
177 rcu_read_lock();
178 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
179 if (hard_iface->soft_iface != bat_priv->soft_iface)
180 continue;
181
Sven Eckelmann27353442016-03-05 16:09:16 +0100182 if (!kref_get_unless_zero(&hard_iface->refcount))
183 continue;
184
Linus Lüssing3111bee2016-08-07 12:34:19 +0200185 ret = batadv_hardif_no_broadcast(hard_iface, NULL, NULL);
186 if (ret) {
187 char *type;
188
189 switch (ret) {
190 case BATADV_HARDIF_BCAST_NORECIPIENT:
191 type = "no neighbor";
192 break;
193 case BATADV_HARDIF_BCAST_DUPFWD:
194 type = "single neighbor is source";
195 break;
196 case BATADV_HARDIF_BCAST_DUPORIG:
197 type = "single neighbor is originator";
198 break;
199 default:
200 type = "unknown";
201 }
202
Colin Ian Kingf25cbb22017-06-26 11:26:44 +0100203 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 from ourselves on %s suppressed: %s\n",
Linus Lüssing3111bee2016-08-07 12:34:19 +0200204 hard_iface->net_dev->name, type);
205
206 batadv_hardif_put(hard_iface);
207 continue;
208 }
209
Antonio Quartulli0da00352016-01-16 16:40:12 +0800210 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
211 "Sending own OGM2 packet (originator %pM, seqno %u, throughput %u, TTL %d) on interface %s [%pM]\n",
212 ogm_packet->orig, ntohl(ogm_packet->seqno),
213 ntohl(ogm_packet->throughput), ogm_packet->ttl,
214 hard_iface->net_dev->name,
215 hard_iface->net_dev->dev_addr);
216
217 /* this skb gets consumed by batadv_v_ogm_send_to_if() */
218 skb_tmp = skb_clone(skb, GFP_ATOMIC);
Sven Eckelmann27353442016-03-05 16:09:16 +0100219 if (!skb_tmp) {
220 batadv_hardif_put(hard_iface);
Antonio Quartulli0da00352016-01-16 16:40:12 +0800221 break;
Sven Eckelmann27353442016-03-05 16:09:16 +0100222 }
Antonio Quartulli0da00352016-01-16 16:40:12 +0800223
224 batadv_v_ogm_send_to_if(skb_tmp, hard_iface);
Sven Eckelmann27353442016-03-05 16:09:16 +0100225 batadv_hardif_put(hard_iface);
Antonio Quartulli0da00352016-01-16 16:40:12 +0800226 }
227 rcu_read_unlock();
228
229 consume_skb(skb);
230
231reschedule:
232 batadv_v_ogm_start_timer(bat_priv);
233out:
234 return;
235}
236
237/**
238 * batadv_v_ogm_iface_enable - prepare an interface for B.A.T.M.A.N. V
239 * @hard_iface: the interface to prepare
240 *
241 * Takes care of scheduling own OGM sending routine for this interface.
242 *
243 * Return: 0 on success or a negative error code otherwise
244 */
245int batadv_v_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
246{
247 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
248
249 batadv_v_ogm_start_timer(bat_priv);
250
251 return 0;
252}
253
254/**
255 * batadv_v_ogm_primary_iface_set - set a new primary interface
256 * @primary_iface: the new primary interface
257 */
258void batadv_v_ogm_primary_iface_set(struct batadv_hard_iface *primary_iface)
259{
260 struct batadv_priv *bat_priv = netdev_priv(primary_iface->soft_iface);
261 struct batadv_ogm2_packet *ogm_packet;
262
263 if (!bat_priv->bat_v.ogm_buff)
264 return;
265
266 ogm_packet = (struct batadv_ogm2_packet *)bat_priv->bat_v.ogm_buff;
267 ether_addr_copy(ogm_packet->orig, primary_iface->net_dev->dev_addr);
268}
269
270/**
Antonio Quartulli93231582016-01-16 16:40:13 +0800271 * batadv_v_forward_penalty - apply a penalty to the throughput metric forwarded
272 * with B.A.T.M.A.N. V OGMs
273 * @bat_priv: the bat priv with all the soft interface information
274 * @if_incoming: the interface where the OGM has been received
275 * @if_outgoing: the interface where the OGM has to be forwarded to
276 * @throughput: the current throughput
277 *
278 * Apply a penalty on the current throughput metric value based on the
279 * characteristic of the interface where the OGM has been received. The return
280 * value is computed as follows:
281 * - throughput * 50% if the incoming and outgoing interface are the
282 * same WiFi interface and the throughput is above
283 * 1MBit/s
284 * - throughput if the outgoing interface is the default
285 * interface (i.e. this OGM is processed for the
286 * internal table and not forwarded)
287 * - throughput * hop penalty otherwise
288 *
289 * Return: the penalised throughput metric.
290 */
291static u32 batadv_v_forward_penalty(struct batadv_priv *bat_priv,
292 struct batadv_hard_iface *if_incoming,
293 struct batadv_hard_iface *if_outgoing,
294 u32 throughput)
295{
296 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
297 int hop_penalty_max = BATADV_TQ_MAX_VALUE;
298
299 /* Don't apply hop penalty in default originator table. */
300 if (if_outgoing == BATADV_IF_DEFAULT)
301 return throughput;
302
303 /* Forwarding on the same WiFi interface cuts the throughput in half
304 * due to the store & forward characteristics of WIFI.
305 * Very low throughput values are the exception.
306 */
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200307 if (throughput > 10 &&
308 if_incoming == if_outgoing &&
Antonio Quartullic8334842015-11-10 18:50:51 +0100309 !(if_incoming->bat_v.flags & BATADV_FULL_DUPLEX))
Antonio Quartulli93231582016-01-16 16:40:13 +0800310 return throughput / 2;
311
312 /* hop penalty of 255 equals 100% */
313 return throughput * (hop_penalty_max - hop_penalty) / hop_penalty_max;
314}
315
316/**
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100317 * batadv_v_ogm_forward - check conditions and forward an OGM to the given
318 * outgoing interface
Antonio Quartulli93231582016-01-16 16:40:13 +0800319 * @bat_priv: the bat priv with all the soft interface information
320 * @ogm_received: previously received OGM to be forwarded
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100321 * @orig_node: the originator which has been updated
322 * @neigh_node: the neigh_node through with the OGM has been received
Antonio Quartulli93231582016-01-16 16:40:13 +0800323 * @if_incoming: the interface on which this OGM was received on
324 * @if_outgoing: the interface to which the OGM has to be forwarded to
325 *
326 * Forward an OGM to an interface after having altered the throughput metric and
327 * the TTL value contained in it. The original OGM isn't modified.
328 */
329static void batadv_v_ogm_forward(struct batadv_priv *bat_priv,
330 const struct batadv_ogm2_packet *ogm_received,
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100331 struct batadv_orig_node *orig_node,
332 struct batadv_neigh_node *neigh_node,
Antonio Quartulli93231582016-01-16 16:40:13 +0800333 struct batadv_hard_iface *if_incoming,
334 struct batadv_hard_iface *if_outgoing)
335{
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100336 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
337 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
338 struct batadv_neigh_node *router = NULL;
Antonio Quartulli93231582016-01-16 16:40:13 +0800339 struct batadv_ogm2_packet *ogm_forward;
340 unsigned char *skb_buff;
341 struct sk_buff *skb;
342 size_t packet_len;
343 u16 tvlv_len;
344
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100345 /* only forward for specific interfaces, not for the default one. */
346 if (if_outgoing == BATADV_IF_DEFAULT)
347 goto out;
348
349 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
350 if (!orig_ifinfo)
351 goto out;
352
353 /* acquire possibly updated router */
354 router = batadv_orig_router_get(orig_node, if_outgoing);
355
356 /* strict rule: forward packets coming from the best next hop only */
357 if (neigh_node != router)
358 goto out;
359
360 /* don't forward the same seqno twice on one interface */
361 if (orig_ifinfo->last_seqno_forwarded == ntohl(ogm_received->seqno))
362 goto out;
363
364 orig_ifinfo->last_seqno_forwarded = ntohl(ogm_received->seqno);
365
Antonio Quartulli93231582016-01-16 16:40:13 +0800366 if (ogm_received->ttl <= 1) {
367 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100368 goto out;
Antonio Quartulli93231582016-01-16 16:40:13 +0800369 }
370
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100371 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
372 if (!neigh_ifinfo)
373 goto out;
374
Antonio Quartulli93231582016-01-16 16:40:13 +0800375 tvlv_len = ntohs(ogm_received->tvlv_len);
376
377 packet_len = BATADV_OGM2_HLEN + tvlv_len;
378 skb = netdev_alloc_skb_ip_align(if_outgoing->net_dev,
379 ETH_HLEN + packet_len);
380 if (!skb)
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100381 goto out;
Antonio Quartulli93231582016-01-16 16:40:13 +0800382
383 skb_reserve(skb, ETH_HLEN);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200384 skb_buff = skb_put_data(skb, ogm_received, packet_len);
Antonio Quartulli93231582016-01-16 16:40:13 +0800385
386 /* apply forward penalty */
387 ogm_forward = (struct batadv_ogm2_packet *)skb_buff;
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100388 ogm_forward->throughput = htonl(neigh_ifinfo->bat_v.throughput);
Antonio Quartulli93231582016-01-16 16:40:13 +0800389 ogm_forward->ttl--;
390
391 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
392 "Forwarding OGM2 packet on %s: throughput %u, ttl %u, received via %s\n",
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100393 if_outgoing->net_dev->name, ntohl(ogm_forward->throughput),
394 ogm_forward->ttl, if_incoming->net_dev->name);
Antonio Quartulli93231582016-01-16 16:40:13 +0800395
396 batadv_v_ogm_send_to_if(skb, if_outgoing);
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100397
398out:
399 if (orig_ifinfo)
400 batadv_orig_ifinfo_put(orig_ifinfo);
401 if (router)
402 batadv_neigh_node_put(router);
403 if (neigh_ifinfo)
404 batadv_neigh_ifinfo_put(neigh_ifinfo);
Antonio Quartulli93231582016-01-16 16:40:13 +0800405}
406
407/**
408 * batadv_v_ogm_metric_update - update route metric based on OGM
409 * @bat_priv: the bat priv with all the soft interface information
410 * @ogm2: OGM2 structure
411 * @orig_node: Originator structure for which the OGM has been received
412 * @neigh_node: the neigh_node through with the OGM has been received
413 * @if_incoming: the interface where this packet was received
414 * @if_outgoing: the interface for which the packet should be considered
415 *
416 * Return:
417 * 1 if the OGM is new,
418 * 0 if it is not new but valid,
419 * <0 on error (e.g. old OGM)
420 */
421static int batadv_v_ogm_metric_update(struct batadv_priv *bat_priv,
422 const struct batadv_ogm2_packet *ogm2,
423 struct batadv_orig_node *orig_node,
424 struct batadv_neigh_node *neigh_node,
425 struct batadv_hard_iface *if_incoming,
426 struct batadv_hard_iface *if_outgoing)
427{
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200428 struct batadv_orig_ifinfo *orig_ifinfo;
Antonio Quartulli93231582016-01-16 16:40:13 +0800429 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
430 bool protection_started = false;
431 int ret = -EINVAL;
432 u32 path_throughput;
433 s32 seq_diff;
434
435 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
436 if (!orig_ifinfo)
437 goto out;
438
439 seq_diff = ntohl(ogm2->seqno) - orig_ifinfo->last_real_seqno;
440
441 if (!hlist_empty(&orig_node->neigh_list) &&
442 batadv_window_protected(bat_priv, seq_diff,
443 BATADV_OGM_MAX_AGE,
444 &orig_ifinfo->batman_seqno_reset,
445 &protection_started)) {
446 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
447 "Drop packet: packet within window protection time from %pM\n",
448 ogm2->orig);
449 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
450 "Last reset: %ld, %ld\n",
451 orig_ifinfo->batman_seqno_reset, jiffies);
452 goto out;
453 }
454
455 /* drop packets with old seqnos, however accept the first packet after
456 * a host has been rebooted.
457 */
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200458 if (seq_diff < 0 && !protection_started)
Antonio Quartulli93231582016-01-16 16:40:13 +0800459 goto out;
460
461 neigh_node->last_seen = jiffies;
462
463 orig_node->last_seen = jiffies;
464
465 orig_ifinfo->last_real_seqno = ntohl(ogm2->seqno);
466 orig_ifinfo->last_ttl = ogm2->ttl;
467
468 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
469 if (!neigh_ifinfo)
470 goto out;
471
472 path_throughput = batadv_v_forward_penalty(bat_priv, if_incoming,
473 if_outgoing,
474 ntohl(ogm2->throughput));
475 neigh_ifinfo->bat_v.throughput = path_throughput;
476 neigh_ifinfo->bat_v.last_seqno = ntohl(ogm2->seqno);
477 neigh_ifinfo->last_ttl = ogm2->ttl;
478
479 if (seq_diff > 0 || protection_started)
480 ret = 1;
481 else
482 ret = 0;
483out:
484 if (orig_ifinfo)
485 batadv_orig_ifinfo_put(orig_ifinfo);
486 if (neigh_ifinfo)
487 batadv_neigh_ifinfo_put(neigh_ifinfo);
488
489 return ret;
490}
491
492/**
493 * batadv_v_ogm_route_update - update routes based on OGM
494 * @bat_priv: the bat priv with all the soft interface information
495 * @ethhdr: the Ethernet header of the OGM2
496 * @ogm2: OGM2 structure
497 * @orig_node: Originator structure for which the OGM has been received
498 * @neigh_node: the neigh_node through with the OGM has been received
499 * @if_incoming: the interface where this packet was received
500 * @if_outgoing: the interface for which the packet should be considered
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100501 *
502 * Return: true if the packet should be forwarded, false otherwise
Antonio Quartulli93231582016-01-16 16:40:13 +0800503 */
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100504static bool batadv_v_ogm_route_update(struct batadv_priv *bat_priv,
Antonio Quartulli93231582016-01-16 16:40:13 +0800505 const struct ethhdr *ethhdr,
506 const struct batadv_ogm2_packet *ogm2,
507 struct batadv_orig_node *orig_node,
508 struct batadv_neigh_node *neigh_node,
509 struct batadv_hard_iface *if_incoming,
510 struct batadv_hard_iface *if_outgoing)
511{
512 struct batadv_neigh_node *router = NULL;
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200513 struct batadv_orig_node *orig_neigh_node;
Antonio Quartulli93231582016-01-16 16:40:13 +0800514 struct batadv_neigh_node *orig_neigh_router = NULL;
Simon Wunderlich86de37c2016-02-01 15:21:38 +0100515 struct batadv_neigh_ifinfo *router_ifinfo = NULL, *neigh_ifinfo = NULL;
516 u32 router_throughput, neigh_throughput;
517 u32 router_last_seqno;
518 u32 neigh_last_seqno;
519 s32 neigh_seq_diff;
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100520 bool forward = false;
Antonio Quartulli93231582016-01-16 16:40:13 +0800521
522 orig_neigh_node = batadv_v_ogm_orig_get(bat_priv, ethhdr->h_source);
523 if (!orig_neigh_node)
524 goto out;
525
526 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
527 if_outgoing);
528
529 /* drop packet if sender is not a direct neighbor and if we
530 * don't route towards it
531 */
532 router = batadv_orig_router_get(orig_node, if_outgoing);
533 if (router && router->orig_node != orig_node && !orig_neigh_router) {
534 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
535 "Drop packet: OGM via unknown neighbor!\n");
536 goto out;
537 }
538
Simon Wunderlich86de37c2016-02-01 15:21:38 +0100539 /* Mark the OGM to be considered for forwarding, and update routes
540 * if needed.
541 */
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100542 forward = true;
Simon Wunderlich86de37c2016-02-01 15:21:38 +0100543
544 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
545 "Searching and updating originator entry of received packet\n");
546
547 /* if this neighbor already is our next hop there is nothing
548 * to change
549 */
550 if (router == neigh_node)
551 goto out;
552
553 /* don't consider neighbours with worse throughput.
554 * also switch route if this seqno is BATADV_V_MAX_ORIGDIFF newer than
555 * the last received seqno from our best next hop.
556 */
557 if (router) {
558 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
559 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
560
561 /* if these are not allocated, something is wrong. */
562 if (!router_ifinfo || !neigh_ifinfo)
563 goto out;
564
565 neigh_last_seqno = neigh_ifinfo->bat_v.last_seqno;
566 router_last_seqno = router_ifinfo->bat_v.last_seqno;
567 neigh_seq_diff = neigh_last_seqno - router_last_seqno;
568 router_throughput = router_ifinfo->bat_v.throughput;
569 neigh_throughput = neigh_ifinfo->bat_v.throughput;
570
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200571 if (neigh_seq_diff < BATADV_OGM_MAX_ORIGDIFF &&
572 router_throughput >= neigh_throughput)
Simon Wunderlich86de37c2016-02-01 15:21:38 +0100573 goto out;
574 }
575
576 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
Antonio Quartulli93231582016-01-16 16:40:13 +0800577out:
Antonio Quartulli93231582016-01-16 16:40:13 +0800578 if (router)
579 batadv_neigh_node_put(router);
580 if (orig_neigh_router)
581 batadv_neigh_node_put(orig_neigh_router);
582 if (orig_neigh_node)
583 batadv_orig_node_put(orig_neigh_node);
Simon Wunderlich86de37c2016-02-01 15:21:38 +0100584 if (router_ifinfo)
585 batadv_neigh_ifinfo_put(router_ifinfo);
586 if (neigh_ifinfo)
587 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100588
589 return forward;
Antonio Quartulli93231582016-01-16 16:40:13 +0800590}
591
592/**
593 * batadv_v_ogm_process_per_outif - process a batman v OGM for an outgoing if
594 * @bat_priv: the bat priv with all the soft interface information
595 * @ethhdr: the Ethernet header of the OGM2
596 * @ogm2: OGM2 structure
597 * @orig_node: Originator structure for which the OGM has been received
598 * @neigh_node: the neigh_node through with the OGM has been received
599 * @if_incoming: the interface where this packet was received
600 * @if_outgoing: the interface for which the packet should be considered
601 */
602static void
603batadv_v_ogm_process_per_outif(struct batadv_priv *bat_priv,
604 const struct ethhdr *ethhdr,
605 const struct batadv_ogm2_packet *ogm2,
606 struct batadv_orig_node *orig_node,
607 struct batadv_neigh_node *neigh_node,
608 struct batadv_hard_iface *if_incoming,
609 struct batadv_hard_iface *if_outgoing)
610{
611 int seqno_age;
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100612 bool forward;
Antonio Quartulli93231582016-01-16 16:40:13 +0800613
614 /* first, update the metric with according sanity checks */
615 seqno_age = batadv_v_ogm_metric_update(bat_priv, ogm2, orig_node,
616 neigh_node, if_incoming,
617 if_outgoing);
618
619 /* outdated sequence numbers are to be discarded */
620 if (seqno_age < 0)
621 return;
622
623 /* only unknown & newer OGMs contain TVLVs we are interested in */
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200624 if (seqno_age > 0 && if_outgoing == BATADV_IF_DEFAULT)
Antonio Quartulli93231582016-01-16 16:40:13 +0800625 batadv_tvlv_containers_process(bat_priv, true, orig_node,
626 NULL, NULL,
627 (unsigned char *)(ogm2 + 1),
628 ntohs(ogm2->tvlv_len));
629
630 /* if the metric update went through, update routes if needed */
Simon Wunderlichefcc9d32016-02-01 15:21:37 +0100631 forward = batadv_v_ogm_route_update(bat_priv, ethhdr, ogm2, orig_node,
632 neigh_node, if_incoming,
633 if_outgoing);
634
635 /* if the routes have been processed correctly, check and forward */
636 if (forward)
637 batadv_v_ogm_forward(bat_priv, ogm2, orig_node, neigh_node,
638 if_incoming, if_outgoing);
Antonio Quartulli93231582016-01-16 16:40:13 +0800639}
640
641/**
642 * batadv_v_ogm_aggr_packet - checks if there is another OGM aggregated
643 * @buff_pos: current position in the skb
644 * @packet_len: total length of the skb
645 * @tvlv_len: tvlv length of the previously considered OGM
646 *
647 * Return: true if there is enough space for another OGM, false otherwise.
648 */
649static bool batadv_v_ogm_aggr_packet(int buff_pos, int packet_len,
650 __be16 tvlv_len)
651{
652 int next_buff_pos = 0;
653
654 next_buff_pos += buff_pos + BATADV_OGM2_HLEN;
655 next_buff_pos += ntohs(tvlv_len);
656
657 return (next_buff_pos <= packet_len) &&
658 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
659}
660
661/**
662 * batadv_v_ogm_process - process an incoming batman v OGM
663 * @skb: the skb containing the OGM
664 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
665 * @if_incoming: the interface where this packet was receved
666 */
667static void batadv_v_ogm_process(const struct sk_buff *skb, int ogm_offset,
668 struct batadv_hard_iface *if_incoming)
669{
670 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
671 struct ethhdr *ethhdr;
672 struct batadv_orig_node *orig_node = NULL;
673 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
674 struct batadv_neigh_node *neigh_node = NULL;
675 struct batadv_hard_iface *hard_iface;
676 struct batadv_ogm2_packet *ogm_packet;
677 u32 ogm_throughput, link_throughput, path_throughput;
Linus Lüssing3111bee2016-08-07 12:34:19 +0200678 int ret;
Antonio Quartulli93231582016-01-16 16:40:13 +0800679
680 ethhdr = eth_hdr(skb);
681 ogm_packet = (struct batadv_ogm2_packet *)(skb->data + ogm_offset);
682
683 ogm_throughput = ntohl(ogm_packet->throughput);
684
685 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Colin Ian Kingf25cbb22017-06-26 11:26:44 +0100686 "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 +0800687 ethhdr->h_source, if_incoming->net_dev->name,
688 if_incoming->net_dev->dev_addr, ogm_packet->orig,
689 ntohl(ogm_packet->seqno), ogm_throughput, ogm_packet->ttl,
690 ogm_packet->version, ntohs(ogm_packet->tvlv_len));
691
Colin Ian Kingf25cbb22017-06-26 11:26:44 +0100692 /* If the throughput metric is 0, immediately drop the packet. No need
693 * to create orig_node / neigh_node for an unusable route.
Antonio Quartulli93231582016-01-16 16:40:13 +0800694 */
695 if (ogm_throughput == 0) {
696 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Colin Ian Kingf25cbb22017-06-26 11:26:44 +0100697 "Drop packet: originator packet with throughput metric of 0\n");
Antonio Quartulli93231582016-01-16 16:40:13 +0800698 return;
699 }
700
701 /* require ELP packets be to received from this neighbor first */
702 hardif_neigh = batadv_hardif_neigh_get(if_incoming, ethhdr->h_source);
703 if (!hardif_neigh) {
704 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
705 "Drop packet: OGM via unknown neighbor!\n");
706 goto out;
707 }
708
709 orig_node = batadv_v_ogm_orig_get(bat_priv, ogm_packet->orig);
710 if (!orig_node)
711 return;
712
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800713 neigh_node = batadv_neigh_node_get_or_create(orig_node, if_incoming,
714 ethhdr->h_source);
Antonio Quartulli93231582016-01-16 16:40:13 +0800715 if (!neigh_node)
716 goto out;
717
718 /* Update the received throughput metric to match the link
719 * characteristic:
720 * - If this OGM traveled one hop so far (emitted by single hop
721 * neighbor) the path throughput metric equals the link throughput.
722 * - For OGMs traversing more than hop the path throughput metric is
723 * the smaller of the path throughput and the link throughput.
724 */
725 link_throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
726 path_throughput = min_t(u32, link_throughput, ogm_throughput);
727 ogm_packet->throughput = htonl(path_throughput);
728
729 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet, orig_node,
730 neigh_node, if_incoming,
731 BATADV_IF_DEFAULT);
732
733 rcu_read_lock();
734 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
735 if (hard_iface->if_status != BATADV_IF_ACTIVE)
736 continue;
737
738 if (hard_iface->soft_iface != bat_priv->soft_iface)
739 continue;
740
Sven Eckelmann27353442016-03-05 16:09:16 +0100741 if (!kref_get_unless_zero(&hard_iface->refcount))
742 continue;
743
Linus Lüssing3111bee2016-08-07 12:34:19 +0200744 ret = batadv_hardif_no_broadcast(hard_iface,
745 ogm_packet->orig,
746 hardif_neigh->orig);
747
748 if (ret) {
749 char *type;
750
751 switch (ret) {
752 case BATADV_HARDIF_BCAST_NORECIPIENT:
753 type = "no neighbor";
754 break;
755 case BATADV_HARDIF_BCAST_DUPFWD:
756 type = "single neighbor is source";
757 break;
758 case BATADV_HARDIF_BCAST_DUPORIG:
759 type = "single neighbor is originator";
760 break;
761 default:
762 type = "unknown";
763 }
764
Colin Ian Kingf25cbb22017-06-26 11:26:44 +0100765 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 packet from %pM on %s suppressed: %s\n",
Linus Lüssing3111bee2016-08-07 12:34:19 +0200766 ogm_packet->orig, hard_iface->net_dev->name,
767 type);
768
769 batadv_hardif_put(hard_iface);
770 continue;
771 }
772
Antonio Quartulli93231582016-01-16 16:40:13 +0800773 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet,
774 orig_node, neigh_node,
775 if_incoming, hard_iface);
Sven Eckelmann27353442016-03-05 16:09:16 +0100776
777 batadv_hardif_put(hard_iface);
Antonio Quartulli93231582016-01-16 16:40:13 +0800778 }
779 rcu_read_unlock();
780out:
781 if (orig_node)
782 batadv_orig_node_put(orig_node);
783 if (neigh_node)
784 batadv_neigh_node_put(neigh_node);
785 if (hardif_neigh)
786 batadv_hardif_neigh_put(hardif_neigh);
787}
788
789/**
Antonio Quartulli0da00352016-01-16 16:40:12 +0800790 * batadv_v_ogm_packet_recv - OGM2 receiving handler
791 * @skb: the received OGM
792 * @if_incoming: the interface where this OGM has been received
793 *
794 * Return: NET_RX_SUCCESS and consume the skb on success or returns NET_RX_DROP
795 * (without freeing the skb) on failure
796 */
797int batadv_v_ogm_packet_recv(struct sk_buff *skb,
798 struct batadv_hard_iface *if_incoming)
799{
800 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
801 struct batadv_ogm2_packet *ogm_packet;
802 struct ethhdr *ethhdr = eth_hdr(skb);
Antonio Quartulli93231582016-01-16 16:40:13 +0800803 int ogm_offset;
804 u8 *packet_pos;
805 int ret = NET_RX_DROP;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800806
807 /* did we receive a OGM2 packet on an interface that does not have
808 * B.A.T.M.A.N. V enabled ?
809 */
Antonio Quartulli29824a52016-05-25 23:27:31 +0800810 if (strcmp(bat_priv->algo_ops->name, "BATMAN_V") != 0)
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200811 goto free_skb;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800812
813 if (!batadv_check_management_packet(skb, if_incoming, BATADV_OGM2_HLEN))
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200814 goto free_skb;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800815
816 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200817 goto free_skb;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800818
819 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
820
821 if (batadv_is_my_mac(bat_priv, ogm_packet->orig))
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200822 goto free_skb;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800823
824 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
825 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
826 skb->len + ETH_HLEN);
827
Antonio Quartulli93231582016-01-16 16:40:13 +0800828 ogm_offset = 0;
829 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
830
831 while (batadv_v_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
832 ogm_packet->tvlv_len)) {
833 batadv_v_ogm_process(skb, ogm_offset, if_incoming);
834
835 ogm_offset += BATADV_OGM2_HLEN;
836 ogm_offset += ntohs(ogm_packet->tvlv_len);
837
838 packet_pos = skb->data + ogm_offset;
839 ogm_packet = (struct batadv_ogm2_packet *)packet_pos;
840 }
841
842 ret = NET_RX_SUCCESS;
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200843
844free_skb:
845 if (ret == NET_RX_SUCCESS)
846 consume_skb(skb);
847 else
848 kfree_skb(skb);
Antonio Quartulli93231582016-01-16 16:40:13 +0800849
850 return ret;
Antonio Quartulli0da00352016-01-16 16:40:12 +0800851}
852
853/**
854 * batadv_v_ogm_init - initialise the OGM2 engine
855 * @bat_priv: the bat priv with all the soft interface information
856 *
857 * Return: 0 on success or a negative error code in case of failure
858 */
859int batadv_v_ogm_init(struct batadv_priv *bat_priv)
860{
861 struct batadv_ogm2_packet *ogm_packet;
862 unsigned char *ogm_buff;
863 u32 random_seqno;
864
865 bat_priv->bat_v.ogm_buff_len = BATADV_OGM2_HLEN;
866 ogm_buff = kzalloc(bat_priv->bat_v.ogm_buff_len, GFP_ATOMIC);
867 if (!ogm_buff)
868 return -ENOMEM;
869
870 bat_priv->bat_v.ogm_buff = ogm_buff;
871 ogm_packet = (struct batadv_ogm2_packet *)ogm_buff;
872 ogm_packet->packet_type = BATADV_OGM2;
873 ogm_packet->version = BATADV_COMPAT_VERSION;
874 ogm_packet->ttl = BATADV_TTL;
875 ogm_packet->flags = BATADV_NO_FLAGS;
876 ogm_packet->throughput = htonl(BATADV_THROUGHPUT_MAX_VALUE);
877
878 /* randomize initial seqno to avoid collision */
879 get_random_bytes(&random_seqno, sizeof(random_seqno));
880 atomic_set(&bat_priv->bat_v.ogm_seqno, random_seqno);
881 INIT_DELAYED_WORK(&bat_priv->bat_v.ogm_wq, batadv_v_ogm_send);
882
883 return 0;
884}
885
886/**
887 * batadv_v_ogm_free - free OGM private resources
888 * @bat_priv: the bat priv with all the soft interface information
889 */
890void batadv_v_ogm_free(struct batadv_priv *bat_priv)
891{
892 cancel_delayed_work_sync(&bat_priv->bat_v.ogm_wq);
893
894 kfree(bat_priv->bat_v.ogm_buff);
895 bat_priv->bat_v.ogm_buff = NULL;
896 bat_priv->bat_v.ogm_buff_len = 0;
897}