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