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