Linux 4.13.16
[linux/fpc-iii.git] / net / batman-adv / bat_v_ogm.c
blob1e3dc374bfde960bde0731ad1acc26fc2cbe482d
1 /* Copyright (C) 2013-2017 B.A.T.M.A.N. contributors:
3 * Antonio Quartulli
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "bat_v_ogm.h"
19 #include "main.h"
21 #include <linux/atomic.h>
22 #include <linux/byteorder/generic.h>
23 #include <linux/errno.h>
24 #include <linux/etherdevice.h>
25 #include <linux/fs.h>
26 #include <linux/if_ether.h>
27 #include <linux/jiffies.h>
28 #include <linux/kernel.h>
29 #include <linux/kref.h>
30 #include <linux/list.h>
31 #include <linux/netdevice.h>
32 #include <linux/random.h>
33 #include <linux/rculist.h>
34 #include <linux/rcupdate.h>
35 #include <linux/skbuff.h>
36 #include <linux/slab.h>
37 #include <linux/stddef.h>
38 #include <linux/string.h>
39 #include <linux/types.h>
40 #include <linux/workqueue.h>
42 #include "bat_algo.h"
43 #include "hard-interface.h"
44 #include "hash.h"
45 #include "log.h"
46 #include "originator.h"
47 #include "packet.h"
48 #include "routing.h"
49 #include "send.h"
50 #include "translation-table.h"
51 #include "tvlv.h"
53 /**
54 * batadv_v_ogm_orig_get - retrieve and possibly create an originator node
55 * @bat_priv: the bat priv with all the soft interface information
56 * @addr: the address of the originator
58 * Return: the orig_node corresponding to the specified address. If such object
59 * does not exist it is allocated here. In case of allocation failure returns
60 * NULL.
62 struct batadv_orig_node *batadv_v_ogm_orig_get(struct batadv_priv *bat_priv,
63 const u8 *addr)
65 struct batadv_orig_node *orig_node;
66 int hash_added;
68 orig_node = batadv_orig_hash_find(bat_priv, addr);
69 if (orig_node)
70 return orig_node;
72 orig_node = batadv_orig_node_new(bat_priv, addr);
73 if (!orig_node)
74 return NULL;
76 kref_get(&orig_node->refcount);
77 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
78 batadv_choose_orig, orig_node,
79 &orig_node->hash_entry);
80 if (hash_added != 0) {
81 /* remove refcnt for newly created orig_node and hash entry */
82 batadv_orig_node_put(orig_node);
83 batadv_orig_node_put(orig_node);
84 orig_node = NULL;
87 return orig_node;
90 /**
91 * batadv_v_ogm_start_timer - restart the OGM sending timer
92 * @bat_priv: the bat priv with all the soft interface information
94 static void batadv_v_ogm_start_timer(struct batadv_priv *bat_priv)
96 unsigned long msecs;
97 /* this function may be invoked in different contexts (ogm rescheduling
98 * or hard_iface activation), but the work timer should not be reset
100 if (delayed_work_pending(&bat_priv->bat_v.ogm_wq))
101 return;
103 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
104 msecs += prandom_u32() % (2 * BATADV_JITTER);
105 queue_delayed_work(batadv_event_workqueue, &bat_priv->bat_v.ogm_wq,
106 msecs_to_jiffies(msecs));
110 * batadv_v_ogm_send_to_if - send a batman ogm using a given interface
111 * @skb: the OGM to send
112 * @hard_iface: the interface to use to send the OGM
114 static void batadv_v_ogm_send_to_if(struct sk_buff *skb,
115 struct batadv_hard_iface *hard_iface)
117 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
119 if (hard_iface->if_status != BATADV_IF_ACTIVE)
120 return;
122 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
123 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
124 skb->len + ETH_HLEN);
126 batadv_send_broadcast_skb(skb, hard_iface);
130 * batadv_v_ogm_send - periodic worker broadcasting the own OGM
131 * @work: work queue item
133 static void batadv_v_ogm_send(struct work_struct *work)
135 struct batadv_hard_iface *hard_iface;
136 struct batadv_priv_bat_v *bat_v;
137 struct batadv_priv *bat_priv;
138 struct batadv_ogm2_packet *ogm_packet;
139 struct sk_buff *skb, *skb_tmp;
140 unsigned char *ogm_buff, *pkt_buff;
141 int ogm_buff_len;
142 u16 tvlv_len = 0;
143 int ret;
145 bat_v = container_of(work, struct batadv_priv_bat_v, ogm_wq.work);
146 bat_priv = container_of(bat_v, struct batadv_priv, bat_v);
148 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
149 goto out;
151 ogm_buff = bat_priv->bat_v.ogm_buff;
152 ogm_buff_len = bat_priv->bat_v.ogm_buff_len;
153 /* tt changes have to be committed before the tvlv data is
154 * appended as it may alter the tt tvlv container
156 batadv_tt_local_commit_changes(bat_priv);
157 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, &ogm_buff,
158 &ogm_buff_len,
159 BATADV_OGM2_HLEN);
161 bat_priv->bat_v.ogm_buff = ogm_buff;
162 bat_priv->bat_v.ogm_buff_len = ogm_buff_len;
164 skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + ogm_buff_len);
165 if (!skb)
166 goto reschedule;
168 skb_reserve(skb, ETH_HLEN);
169 pkt_buff = skb_put_data(skb, ogm_buff, ogm_buff_len);
171 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
172 ogm_packet->seqno = htonl(atomic_read(&bat_priv->bat_v.ogm_seqno));
173 atomic_inc(&bat_priv->bat_v.ogm_seqno);
174 ogm_packet->tvlv_len = htons(tvlv_len);
176 /* broadcast on every interface */
177 rcu_read_lock();
178 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
179 if (hard_iface->soft_iface != bat_priv->soft_iface)
180 continue;
182 if (!kref_get_unless_zero(&hard_iface->refcount))
183 continue;
185 ret = batadv_hardif_no_broadcast(hard_iface, NULL, NULL);
186 if (ret) {
187 char *type;
189 switch (ret) {
190 case BATADV_HARDIF_BCAST_NORECIPIENT:
191 type = "no neighbor";
192 break;
193 case BATADV_HARDIF_BCAST_DUPFWD:
194 type = "single neighbor is source";
195 break;
196 case BATADV_HARDIF_BCAST_DUPORIG:
197 type = "single neighbor is originator";
198 break;
199 default:
200 type = "unknown";
203 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 from ourselve on %s surpressed: %s\n",
204 hard_iface->net_dev->name, type);
206 batadv_hardif_put(hard_iface);
207 continue;
210 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
211 "Sending own OGM2 packet (originator %pM, seqno %u, throughput %u, TTL %d) on interface %s [%pM]\n",
212 ogm_packet->orig, ntohl(ogm_packet->seqno),
213 ntohl(ogm_packet->throughput), ogm_packet->ttl,
214 hard_iface->net_dev->name,
215 hard_iface->net_dev->dev_addr);
217 /* this skb gets consumed by batadv_v_ogm_send_to_if() */
218 skb_tmp = skb_clone(skb, GFP_ATOMIC);
219 if (!skb_tmp) {
220 batadv_hardif_put(hard_iface);
221 break;
224 batadv_v_ogm_send_to_if(skb_tmp, hard_iface);
225 batadv_hardif_put(hard_iface);
227 rcu_read_unlock();
229 consume_skb(skb);
231 reschedule:
232 batadv_v_ogm_start_timer(bat_priv);
233 out:
234 return;
238 * batadv_v_ogm_iface_enable - prepare an interface for B.A.T.M.A.N. V
239 * @hard_iface: the interface to prepare
241 * Takes care of scheduling own OGM sending routine for this interface.
243 * Return: 0 on success or a negative error code otherwise
245 int batadv_v_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
247 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
249 batadv_v_ogm_start_timer(bat_priv);
251 return 0;
255 * batadv_v_ogm_primary_iface_set - set a new primary interface
256 * @primary_iface: the new primary interface
258 void batadv_v_ogm_primary_iface_set(struct batadv_hard_iface *primary_iface)
260 struct batadv_priv *bat_priv = netdev_priv(primary_iface->soft_iface);
261 struct batadv_ogm2_packet *ogm_packet;
263 if (!bat_priv->bat_v.ogm_buff)
264 return;
266 ogm_packet = (struct batadv_ogm2_packet *)bat_priv->bat_v.ogm_buff;
267 ether_addr_copy(ogm_packet->orig, primary_iface->net_dev->dev_addr);
271 * batadv_v_forward_penalty - apply a penalty to the throughput metric forwarded
272 * with B.A.T.M.A.N. V OGMs
273 * @bat_priv: the bat priv with all the soft interface information
274 * @if_incoming: the interface where the OGM has been received
275 * @if_outgoing: the interface where the OGM has to be forwarded to
276 * @throughput: the current throughput
278 * Apply a penalty on the current throughput metric value based on the
279 * characteristic of the interface where the OGM has been received. The return
280 * value is computed as follows:
281 * - throughput * 50% if the incoming and outgoing interface are the
282 * same WiFi interface and the throughput is above
283 * 1MBit/s
284 * - throughput if the outgoing interface is the default
285 * interface (i.e. this OGM is processed for the
286 * internal table and not forwarded)
287 * - throughput * hop penalty otherwise
289 * Return: the penalised throughput metric.
291 static u32 batadv_v_forward_penalty(struct batadv_priv *bat_priv,
292 struct batadv_hard_iface *if_incoming,
293 struct batadv_hard_iface *if_outgoing,
294 u32 throughput)
296 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
297 int hop_penalty_max = BATADV_TQ_MAX_VALUE;
299 /* Don't apply hop penalty in default originator table. */
300 if (if_outgoing == BATADV_IF_DEFAULT)
301 return throughput;
303 /* Forwarding on the same WiFi interface cuts the throughput in half
304 * due to the store & forward characteristics of WIFI.
305 * Very low throughput values are the exception.
307 if ((throughput > 10) &&
308 (if_incoming == if_outgoing) &&
309 !(if_incoming->bat_v.flags & BATADV_FULL_DUPLEX))
310 return throughput / 2;
312 /* hop penalty of 255 equals 100% */
313 return throughput * (hop_penalty_max - hop_penalty) / hop_penalty_max;
317 * batadv_v_ogm_forward - check conditions and forward an OGM to the given
318 * outgoing interface
319 * @bat_priv: the bat priv with all the soft interface information
320 * @ogm_received: previously received OGM to be forwarded
321 * @orig_node: the originator which has been updated
322 * @neigh_node: the neigh_node through with the OGM has been received
323 * @if_incoming: the interface on which this OGM was received on
324 * @if_outgoing: the interface to which the OGM has to be forwarded to
326 * Forward an OGM to an interface after having altered the throughput metric and
327 * the TTL value contained in it. The original OGM isn't modified.
329 static void batadv_v_ogm_forward(struct batadv_priv *bat_priv,
330 const struct batadv_ogm2_packet *ogm_received,
331 struct batadv_orig_node *orig_node,
332 struct batadv_neigh_node *neigh_node,
333 struct batadv_hard_iface *if_incoming,
334 struct batadv_hard_iface *if_outgoing)
336 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
337 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
338 struct batadv_neigh_node *router = NULL;
339 struct batadv_ogm2_packet *ogm_forward;
340 unsigned char *skb_buff;
341 struct sk_buff *skb;
342 size_t packet_len;
343 u16 tvlv_len;
345 /* only forward for specific interfaces, not for the default one. */
346 if (if_outgoing == BATADV_IF_DEFAULT)
347 goto out;
349 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
350 if (!orig_ifinfo)
351 goto out;
353 /* acquire possibly updated router */
354 router = batadv_orig_router_get(orig_node, if_outgoing);
356 /* strict rule: forward packets coming from the best next hop only */
357 if (neigh_node != router)
358 goto out;
360 /* don't forward the same seqno twice on one interface */
361 if (orig_ifinfo->last_seqno_forwarded == ntohl(ogm_received->seqno))
362 goto out;
364 orig_ifinfo->last_seqno_forwarded = ntohl(ogm_received->seqno);
366 if (ogm_received->ttl <= 1) {
367 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
368 goto out;
371 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
372 if (!neigh_ifinfo)
373 goto out;
375 tvlv_len = ntohs(ogm_received->tvlv_len);
377 packet_len = BATADV_OGM2_HLEN + tvlv_len;
378 skb = netdev_alloc_skb_ip_align(if_outgoing->net_dev,
379 ETH_HLEN + packet_len);
380 if (!skb)
381 goto out;
383 skb_reserve(skb, ETH_HLEN);
384 skb_buff = skb_put_data(skb, ogm_received, packet_len);
386 /* apply forward penalty */
387 ogm_forward = (struct batadv_ogm2_packet *)skb_buff;
388 ogm_forward->throughput = htonl(neigh_ifinfo->bat_v.throughput);
389 ogm_forward->ttl--;
391 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
392 "Forwarding OGM2 packet on %s: throughput %u, ttl %u, received via %s\n",
393 if_outgoing->net_dev->name, ntohl(ogm_forward->throughput),
394 ogm_forward->ttl, if_incoming->net_dev->name);
396 batadv_v_ogm_send_to_if(skb, if_outgoing);
398 out:
399 if (orig_ifinfo)
400 batadv_orig_ifinfo_put(orig_ifinfo);
401 if (router)
402 batadv_neigh_node_put(router);
403 if (neigh_ifinfo)
404 batadv_neigh_ifinfo_put(neigh_ifinfo);
408 * batadv_v_ogm_metric_update - update route metric based on OGM
409 * @bat_priv: the bat priv with all the soft interface information
410 * @ogm2: OGM2 structure
411 * @orig_node: Originator structure for which the OGM has been received
412 * @neigh_node: the neigh_node through with the OGM has been received
413 * @if_incoming: the interface where this packet was received
414 * @if_outgoing: the interface for which the packet should be considered
416 * Return:
417 * 1 if the OGM is new,
418 * 0 if it is not new but valid,
419 * <0 on error (e.g. old OGM)
421 static int batadv_v_ogm_metric_update(struct batadv_priv *bat_priv,
422 const struct batadv_ogm2_packet *ogm2,
423 struct batadv_orig_node *orig_node,
424 struct batadv_neigh_node *neigh_node,
425 struct batadv_hard_iface *if_incoming,
426 struct batadv_hard_iface *if_outgoing)
428 struct batadv_orig_ifinfo *orig_ifinfo;
429 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
430 bool protection_started = false;
431 int ret = -EINVAL;
432 u32 path_throughput;
433 s32 seq_diff;
435 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
436 if (!orig_ifinfo)
437 goto out;
439 seq_diff = ntohl(ogm2->seqno) - orig_ifinfo->last_real_seqno;
441 if (!hlist_empty(&orig_node->neigh_list) &&
442 batadv_window_protected(bat_priv, seq_diff,
443 BATADV_OGM_MAX_AGE,
444 &orig_ifinfo->batman_seqno_reset,
445 &protection_started)) {
446 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
447 "Drop packet: packet within window protection time from %pM\n",
448 ogm2->orig);
449 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
450 "Last reset: %ld, %ld\n",
451 orig_ifinfo->batman_seqno_reset, jiffies);
452 goto out;
455 /* drop packets with old seqnos, however accept the first packet after
456 * a host has been rebooted.
458 if ((seq_diff < 0) && !protection_started)
459 goto out;
461 neigh_node->last_seen = jiffies;
463 orig_node->last_seen = jiffies;
465 orig_ifinfo->last_real_seqno = ntohl(ogm2->seqno);
466 orig_ifinfo->last_ttl = ogm2->ttl;
468 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
469 if (!neigh_ifinfo)
470 goto out;
472 path_throughput = batadv_v_forward_penalty(bat_priv, if_incoming,
473 if_outgoing,
474 ntohl(ogm2->throughput));
475 neigh_ifinfo->bat_v.throughput = path_throughput;
476 neigh_ifinfo->bat_v.last_seqno = ntohl(ogm2->seqno);
477 neigh_ifinfo->last_ttl = ogm2->ttl;
479 if (seq_diff > 0 || protection_started)
480 ret = 1;
481 else
482 ret = 0;
483 out:
484 if (orig_ifinfo)
485 batadv_orig_ifinfo_put(orig_ifinfo);
486 if (neigh_ifinfo)
487 batadv_neigh_ifinfo_put(neigh_ifinfo);
489 return ret;
493 * batadv_v_ogm_route_update - update routes based on OGM
494 * @bat_priv: the bat priv with all the soft interface information
495 * @ethhdr: the Ethernet header of the OGM2
496 * @ogm2: OGM2 structure
497 * @orig_node: Originator structure for which the OGM has been received
498 * @neigh_node: the neigh_node through with the OGM has been received
499 * @if_incoming: the interface where this packet was received
500 * @if_outgoing: the interface for which the packet should be considered
502 * Return: true if the packet should be forwarded, false otherwise
504 static bool batadv_v_ogm_route_update(struct batadv_priv *bat_priv,
505 const struct ethhdr *ethhdr,
506 const struct batadv_ogm2_packet *ogm2,
507 struct batadv_orig_node *orig_node,
508 struct batadv_neigh_node *neigh_node,
509 struct batadv_hard_iface *if_incoming,
510 struct batadv_hard_iface *if_outgoing)
512 struct batadv_neigh_node *router = NULL;
513 struct batadv_orig_node *orig_neigh_node;
514 struct batadv_neigh_node *orig_neigh_router = NULL;
515 struct batadv_neigh_ifinfo *router_ifinfo = NULL, *neigh_ifinfo = NULL;
516 u32 router_throughput, neigh_throughput;
517 u32 router_last_seqno;
518 u32 neigh_last_seqno;
519 s32 neigh_seq_diff;
520 bool forward = false;
522 orig_neigh_node = batadv_v_ogm_orig_get(bat_priv, ethhdr->h_source);
523 if (!orig_neigh_node)
524 goto out;
526 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
527 if_outgoing);
529 /* drop packet if sender is not a direct neighbor and if we
530 * don't route towards it
532 router = batadv_orig_router_get(orig_node, if_outgoing);
533 if (router && router->orig_node != orig_node && !orig_neigh_router) {
534 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
535 "Drop packet: OGM via unknown neighbor!\n");
536 goto out;
539 /* Mark the OGM to be considered for forwarding, and update routes
540 * if needed.
542 forward = true;
544 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
545 "Searching and updating originator entry of received packet\n");
547 /* if this neighbor already is our next hop there is nothing
548 * to change
550 if (router == neigh_node)
551 goto out;
553 /* don't consider neighbours with worse throughput.
554 * also switch route if this seqno is BATADV_V_MAX_ORIGDIFF newer than
555 * the last received seqno from our best next hop.
557 if (router) {
558 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
559 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
561 /* if these are not allocated, something is wrong. */
562 if (!router_ifinfo || !neigh_ifinfo)
563 goto out;
565 neigh_last_seqno = neigh_ifinfo->bat_v.last_seqno;
566 router_last_seqno = router_ifinfo->bat_v.last_seqno;
567 neigh_seq_diff = neigh_last_seqno - router_last_seqno;
568 router_throughput = router_ifinfo->bat_v.throughput;
569 neigh_throughput = neigh_ifinfo->bat_v.throughput;
571 if ((neigh_seq_diff < BATADV_OGM_MAX_ORIGDIFF) &&
572 (router_throughput >= neigh_throughput))
573 goto out;
576 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
577 out:
578 if (router)
579 batadv_neigh_node_put(router);
580 if (orig_neigh_router)
581 batadv_neigh_node_put(orig_neigh_router);
582 if (orig_neigh_node)
583 batadv_orig_node_put(orig_neigh_node);
584 if (router_ifinfo)
585 batadv_neigh_ifinfo_put(router_ifinfo);
586 if (neigh_ifinfo)
587 batadv_neigh_ifinfo_put(neigh_ifinfo);
589 return forward;
593 * batadv_v_ogm_process_per_outif - process a batman v OGM for an outgoing if
594 * @bat_priv: the bat priv with all the soft interface information
595 * @ethhdr: the Ethernet header of the OGM2
596 * @ogm2: OGM2 structure
597 * @orig_node: Originator structure for which the OGM has been received
598 * @neigh_node: the neigh_node through with the OGM has been received
599 * @if_incoming: the interface where this packet was received
600 * @if_outgoing: the interface for which the packet should be considered
602 static void
603 batadv_v_ogm_process_per_outif(struct batadv_priv *bat_priv,
604 const struct ethhdr *ethhdr,
605 const struct batadv_ogm2_packet *ogm2,
606 struct batadv_orig_node *orig_node,
607 struct batadv_neigh_node *neigh_node,
608 struct batadv_hard_iface *if_incoming,
609 struct batadv_hard_iface *if_outgoing)
611 int seqno_age;
612 bool forward;
614 /* first, update the metric with according sanity checks */
615 seqno_age = batadv_v_ogm_metric_update(bat_priv, ogm2, orig_node,
616 neigh_node, if_incoming,
617 if_outgoing);
619 /* outdated sequence numbers are to be discarded */
620 if (seqno_age < 0)
621 return;
623 /* only unknown & newer OGMs contain TVLVs we are interested in */
624 if ((seqno_age > 0) && (if_outgoing == BATADV_IF_DEFAULT))
625 batadv_tvlv_containers_process(bat_priv, true, orig_node,
626 NULL, NULL,
627 (unsigned char *)(ogm2 + 1),
628 ntohs(ogm2->tvlv_len));
630 /* if the metric update went through, update routes if needed */
631 forward = batadv_v_ogm_route_update(bat_priv, ethhdr, ogm2, orig_node,
632 neigh_node, if_incoming,
633 if_outgoing);
635 /* if the routes have been processed correctly, check and forward */
636 if (forward)
637 batadv_v_ogm_forward(bat_priv, ogm2, orig_node, neigh_node,
638 if_incoming, if_outgoing);
642 * batadv_v_ogm_aggr_packet - checks if there is another OGM aggregated
643 * @buff_pos: current position in the skb
644 * @packet_len: total length of the skb
645 * @tvlv_len: tvlv length of the previously considered OGM
647 * Return: true if there is enough space for another OGM, false otherwise.
649 static bool batadv_v_ogm_aggr_packet(int buff_pos, int packet_len,
650 __be16 tvlv_len)
652 int next_buff_pos = 0;
654 next_buff_pos += buff_pos + BATADV_OGM2_HLEN;
655 next_buff_pos += ntohs(tvlv_len);
657 return (next_buff_pos <= packet_len) &&
658 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
662 * batadv_v_ogm_process - process an incoming batman v OGM
663 * @skb: the skb containing the OGM
664 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
665 * @if_incoming: the interface where this packet was receved
667 static void batadv_v_ogm_process(const struct sk_buff *skb, int ogm_offset,
668 struct batadv_hard_iface *if_incoming)
670 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
671 struct ethhdr *ethhdr;
672 struct batadv_orig_node *orig_node = NULL;
673 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
674 struct batadv_neigh_node *neigh_node = NULL;
675 struct batadv_hard_iface *hard_iface;
676 struct batadv_ogm2_packet *ogm_packet;
677 u32 ogm_throughput, link_throughput, path_throughput;
678 int ret;
680 ethhdr = eth_hdr(skb);
681 ogm_packet = (struct batadv_ogm2_packet *)(skb->data + ogm_offset);
683 ogm_throughput = ntohl(ogm_packet->throughput);
685 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
686 "Received OGM2 packet via NB: %pM, IF: %s [%pM] (from OG: %pM, seqno %u, troughput %u, TTL %u, V %u, tvlv_len %u)\n",
687 ethhdr->h_source, if_incoming->net_dev->name,
688 if_incoming->net_dev->dev_addr, ogm_packet->orig,
689 ntohl(ogm_packet->seqno), ogm_throughput, ogm_packet->ttl,
690 ogm_packet->version, ntohs(ogm_packet->tvlv_len));
692 /* If the troughput metric is 0, immediately drop the packet. No need to
693 * create orig_node / neigh_node for an unusable route.
695 if (ogm_throughput == 0) {
696 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
697 "Drop packet: originator packet with troughput metric of 0\n");
698 return;
701 /* require ELP packets be to received from this neighbor first */
702 hardif_neigh = batadv_hardif_neigh_get(if_incoming, ethhdr->h_source);
703 if (!hardif_neigh) {
704 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
705 "Drop packet: OGM via unknown neighbor!\n");
706 goto out;
709 orig_node = batadv_v_ogm_orig_get(bat_priv, ogm_packet->orig);
710 if (!orig_node)
711 return;
713 neigh_node = batadv_neigh_node_get_or_create(orig_node, if_incoming,
714 ethhdr->h_source);
715 if (!neigh_node)
716 goto out;
718 /* Update the received throughput metric to match the link
719 * characteristic:
720 * - If this OGM traveled one hop so far (emitted by single hop
721 * neighbor) the path throughput metric equals the link throughput.
722 * - For OGMs traversing more than hop the path throughput metric is
723 * the smaller of the path throughput and the link throughput.
725 link_throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
726 path_throughput = min_t(u32, link_throughput, ogm_throughput);
727 ogm_packet->throughput = htonl(path_throughput);
729 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet, orig_node,
730 neigh_node, if_incoming,
731 BATADV_IF_DEFAULT);
733 rcu_read_lock();
734 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
735 if (hard_iface->if_status != BATADV_IF_ACTIVE)
736 continue;
738 if (hard_iface->soft_iface != bat_priv->soft_iface)
739 continue;
741 if (!kref_get_unless_zero(&hard_iface->refcount))
742 continue;
744 ret = batadv_hardif_no_broadcast(hard_iface,
745 ogm_packet->orig,
746 hardif_neigh->orig);
748 if (ret) {
749 char *type;
751 switch (ret) {
752 case BATADV_HARDIF_BCAST_NORECIPIENT:
753 type = "no neighbor";
754 break;
755 case BATADV_HARDIF_BCAST_DUPFWD:
756 type = "single neighbor is source";
757 break;
758 case BATADV_HARDIF_BCAST_DUPORIG:
759 type = "single neighbor is originator";
760 break;
761 default:
762 type = "unknown";
765 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "OGM2 packet from %pM on %s surpressed: %s\n",
766 ogm_packet->orig, hard_iface->net_dev->name,
767 type);
769 batadv_hardif_put(hard_iface);
770 continue;
773 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet,
774 orig_node, neigh_node,
775 if_incoming, hard_iface);
777 batadv_hardif_put(hard_iface);
779 rcu_read_unlock();
780 out:
781 if (orig_node)
782 batadv_orig_node_put(orig_node);
783 if (neigh_node)
784 batadv_neigh_node_put(neigh_node);
785 if (hardif_neigh)
786 batadv_hardif_neigh_put(hardif_neigh);
790 * batadv_v_ogm_packet_recv - OGM2 receiving handler
791 * @skb: the received OGM
792 * @if_incoming: the interface where this OGM has been received
794 * Return: NET_RX_SUCCESS and consume the skb on success or returns NET_RX_DROP
795 * (without freeing the skb) on failure
797 int batadv_v_ogm_packet_recv(struct sk_buff *skb,
798 struct batadv_hard_iface *if_incoming)
800 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
801 struct batadv_ogm2_packet *ogm_packet;
802 struct ethhdr *ethhdr = eth_hdr(skb);
803 int ogm_offset;
804 u8 *packet_pos;
805 int ret = NET_RX_DROP;
807 /* did we receive a OGM2 packet on an interface that does not have
808 * B.A.T.M.A.N. V enabled ?
810 if (strcmp(bat_priv->algo_ops->name, "BATMAN_V") != 0)
811 goto free_skb;
813 if (!batadv_check_management_packet(skb, if_incoming, BATADV_OGM2_HLEN))
814 goto free_skb;
816 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
817 goto free_skb;
819 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
821 if (batadv_is_my_mac(bat_priv, ogm_packet->orig))
822 goto free_skb;
824 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
825 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
826 skb->len + ETH_HLEN);
828 ogm_offset = 0;
829 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
831 while (batadv_v_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
832 ogm_packet->tvlv_len)) {
833 batadv_v_ogm_process(skb, ogm_offset, if_incoming);
835 ogm_offset += BATADV_OGM2_HLEN;
836 ogm_offset += ntohs(ogm_packet->tvlv_len);
838 packet_pos = skb->data + ogm_offset;
839 ogm_packet = (struct batadv_ogm2_packet *)packet_pos;
842 ret = NET_RX_SUCCESS;
844 free_skb:
845 if (ret == NET_RX_SUCCESS)
846 consume_skb(skb);
847 else
848 kfree_skb(skb);
850 return ret;
854 * batadv_v_ogm_init - initialise the OGM2 engine
855 * @bat_priv: the bat priv with all the soft interface information
857 * Return: 0 on success or a negative error code in case of failure
859 int batadv_v_ogm_init(struct batadv_priv *bat_priv)
861 struct batadv_ogm2_packet *ogm_packet;
862 unsigned char *ogm_buff;
863 u32 random_seqno;
865 bat_priv->bat_v.ogm_buff_len = BATADV_OGM2_HLEN;
866 ogm_buff = kzalloc(bat_priv->bat_v.ogm_buff_len, GFP_ATOMIC);
867 if (!ogm_buff)
868 return -ENOMEM;
870 bat_priv->bat_v.ogm_buff = ogm_buff;
871 ogm_packet = (struct batadv_ogm2_packet *)ogm_buff;
872 ogm_packet->packet_type = BATADV_OGM2;
873 ogm_packet->version = BATADV_COMPAT_VERSION;
874 ogm_packet->ttl = BATADV_TTL;
875 ogm_packet->flags = BATADV_NO_FLAGS;
876 ogm_packet->throughput = htonl(BATADV_THROUGHPUT_MAX_VALUE);
878 /* randomize initial seqno to avoid collision */
879 get_random_bytes(&random_seqno, sizeof(random_seqno));
880 atomic_set(&bat_priv->bat_v.ogm_seqno, random_seqno);
881 INIT_DELAYED_WORK(&bat_priv->bat_v.ogm_wq, batadv_v_ogm_send);
883 return 0;
887 * batadv_v_ogm_free - free OGM private resources
888 * @bat_priv: the bat priv with all the soft interface information
890 void batadv_v_ogm_free(struct batadv_priv *bat_priv)
892 cancel_delayed_work_sync(&bat_priv->bat_v.ogm_wq);
894 kfree(bat_priv->bat_v.ogm_buff);
895 bat_priv->bat_v.ogm_buff = NULL;
896 bat_priv->bat_v.ogm_buff_len = 0;