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