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