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