^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Copyright (C) 2007-2020 B.A.T.M.A.N. contributors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Marek Lindner, Simon Wunderlich
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "main.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/byteorder/generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/if_ether.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kref.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/lockdep.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/pkt_sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/rculist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <uapi/linux/batadv_packet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "originator.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include "send.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include "tvlv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * batadv_tvlv_handler_release() - release tvlv handler from lists and queue for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * free after rcu grace period
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * @ref: kref pointer of the tvlv
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static void batadv_tvlv_handler_release(struct kref *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct batadv_tvlv_handler *tvlv_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) tvlv_handler = container_of(ref, struct batadv_tvlv_handler, refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) kfree_rcu(tvlv_handler, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * batadv_tvlv_handler_put() - decrement the tvlv container refcounter and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * possibly release it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * @tvlv_handler: the tvlv handler to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static void batadv_tvlv_handler_put(struct batadv_tvlv_handler *tvlv_handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (!tvlv_handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) kref_put(&tvlv_handler->refcount, batadv_tvlv_handler_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * batadv_tvlv_handler_get() - retrieve tvlv handler from the tvlv handler list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * based on the provided type and version (both need to match)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * @type: tvlv handler type to look for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * @version: tvlv handler version to look for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * Return: tvlv handler if found or NULL otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static struct batadv_tvlv_handler *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) batadv_tvlv_handler_get(struct batadv_priv *bat_priv, u8 type, u8 version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct batadv_tvlv_handler *tvlv_handler_tmp, *tvlv_handler = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) hlist_for_each_entry_rcu(tvlv_handler_tmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) &bat_priv->tvlv.handler_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (tvlv_handler_tmp->type != type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (tvlv_handler_tmp->version != version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (!kref_get_unless_zero(&tvlv_handler_tmp->refcount))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) tvlv_handler = tvlv_handler_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return tvlv_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * batadv_tvlv_container_release() - release tvlv from lists and free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * @ref: kref pointer of the tvlv
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static void batadv_tvlv_container_release(struct kref *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct batadv_tvlv_container *tvlv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) tvlv = container_of(ref, struct batadv_tvlv_container, refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) kfree(tvlv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * batadv_tvlv_container_put() - decrement the tvlv container refcounter and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * possibly release it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * @tvlv: the tvlv container to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static void batadv_tvlv_container_put(struct batadv_tvlv_container *tvlv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (!tvlv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) kref_put(&tvlv->refcount, batadv_tvlv_container_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * batadv_tvlv_container_get() - retrieve tvlv container from the tvlv container
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * list based on the provided type and version (both need to match)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * @type: tvlv container type to look for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * @version: tvlv container version to look for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * Has to be called with the appropriate locks being acquired
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * (tvlv.container_list_lock).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * Return: tvlv container if found or NULL otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static struct batadv_tvlv_container *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) batadv_tvlv_container_get(struct batadv_priv *bat_priv, u8 type, u8 version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct batadv_tvlv_container *tvlv_tmp, *tvlv = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) lockdep_assert_held(&bat_priv->tvlv.container_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) hlist_for_each_entry(tvlv_tmp, &bat_priv->tvlv.container_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (tvlv_tmp->tvlv_hdr.type != type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (tvlv_tmp->tvlv_hdr.version != version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) kref_get(&tvlv_tmp->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) tvlv = tvlv_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return tvlv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * batadv_tvlv_container_list_size() - calculate the size of the tvlv container
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * list entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * Has to be called with the appropriate locks being acquired
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * (tvlv.container_list_lock).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * Return: size of all currently registered tvlv containers in bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static u16 batadv_tvlv_container_list_size(struct batadv_priv *bat_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct batadv_tvlv_container *tvlv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) u16 tvlv_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) lockdep_assert_held(&bat_priv->tvlv.container_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) tvlv_len += sizeof(struct batadv_tvlv_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) tvlv_len += ntohs(tvlv->tvlv_hdr.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return tvlv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * batadv_tvlv_container_remove() - remove tvlv container from the tvlv
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * container list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * @tvlv: the to be removed tvlv container
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * Has to be called with the appropriate locks being acquired
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * (tvlv.container_list_lock).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static void batadv_tvlv_container_remove(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct batadv_tvlv_container *tvlv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) lockdep_assert_held(&bat_priv->tvlv.container_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (!tvlv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) hlist_del(&tvlv->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) /* first call to decrement the counter, second call to free */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) batadv_tvlv_container_put(tvlv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) batadv_tvlv_container_put(tvlv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * batadv_tvlv_container_unregister() - unregister tvlv container based on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * provided type and version (both need to match)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * @type: tvlv container type to unregister
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * @version: tvlv container type to unregister
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) void batadv_tvlv_container_unregister(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) u8 type, u8 version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct batadv_tvlv_container *tvlv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) spin_lock_bh(&bat_priv->tvlv.container_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) tvlv = batadv_tvlv_container_get(bat_priv, type, version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) batadv_tvlv_container_remove(bat_priv, tvlv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * batadv_tvlv_container_register() - register tvlv type, version and content
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * to be propagated with each (primary interface) OGM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * @type: tvlv container type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * @version: tvlv container version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * @tvlv_value: tvlv container content
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * @tvlv_value_len: tvlv container content length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * If a container of the same type and version was already registered the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * content is going to replace the old one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) void batadv_tvlv_container_register(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) u8 type, u8 version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) void *tvlv_value, u16 tvlv_value_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) struct batadv_tvlv_container *tvlv_old, *tvlv_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (!tvlv_value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) tvlv_value_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) tvlv_new = kzalloc(sizeof(*tvlv_new) + tvlv_value_len, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (!tvlv_new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) tvlv_new->tvlv_hdr.version = version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) tvlv_new->tvlv_hdr.type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) tvlv_new->tvlv_hdr.len = htons(tvlv_value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) memcpy(tvlv_new + 1, tvlv_value, ntohs(tvlv_new->tvlv_hdr.len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) INIT_HLIST_NODE(&tvlv_new->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) kref_init(&tvlv_new->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) spin_lock_bh(&bat_priv->tvlv.container_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) tvlv_old = batadv_tvlv_container_get(bat_priv, type, version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) batadv_tvlv_container_remove(bat_priv, tvlv_old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) kref_get(&tvlv_new->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) hlist_add_head(&tvlv_new->list, &bat_priv->tvlv.container_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /* don't return reference to new tvlv_container */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) batadv_tvlv_container_put(tvlv_new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * batadv_tvlv_realloc_packet_buff() - reallocate packet buffer to accommodate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * requested packet size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) * @packet_buff: packet buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * @packet_buff_len: packet buffer size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * @min_packet_len: requested packet minimum size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * @additional_packet_len: requested additional packet size on top of minimum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * Return: true of the packet buffer could be changed to the requested size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static bool batadv_tvlv_realloc_packet_buff(unsigned char **packet_buff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) int *packet_buff_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) int min_packet_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) int additional_packet_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) unsigned char *new_buff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) new_buff = kmalloc(min_packet_len + additional_packet_len, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) /* keep old buffer if kmalloc should fail */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (!new_buff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) memcpy(new_buff, *packet_buff, min_packet_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) kfree(*packet_buff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) *packet_buff = new_buff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) *packet_buff_len = min_packet_len + additional_packet_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * batadv_tvlv_container_ogm_append() - append tvlv container content to given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * OGM packet buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * @packet_buff: ogm packet buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * @packet_buff_len: ogm packet buffer size including ogm header and tvlv
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * content
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * @packet_min_len: ogm header size to be preserved for the OGM itself
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * The ogm packet might be enlarged or shrunk depending on the current size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * and the size of the to-be-appended tvlv containers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * Return: size of all appended tvlv containers in bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) u16 batadv_tvlv_container_ogm_append(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) unsigned char **packet_buff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) int *packet_buff_len, int packet_min_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct batadv_tvlv_container *tvlv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) struct batadv_tvlv_hdr *tvlv_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) u16 tvlv_value_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) void *tvlv_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) spin_lock_bh(&bat_priv->tvlv.container_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) tvlv_value_len = batadv_tvlv_container_list_size(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) ret = batadv_tvlv_realloc_packet_buff(packet_buff, packet_buff_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) packet_min_len, tvlv_value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (!tvlv_value_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) tvlv_value = (*packet_buff) + packet_min_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) tvlv_hdr = tvlv_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) tvlv_hdr->type = tvlv->tvlv_hdr.type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) tvlv_hdr->version = tvlv->tvlv_hdr.version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) tvlv_hdr->len = tvlv->tvlv_hdr.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) tvlv_value = tvlv_hdr + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) memcpy(tvlv_value, tvlv + 1, ntohs(tvlv->tvlv_hdr.len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) tvlv_value = (u8 *)tvlv_value + ntohs(tvlv->tvlv_hdr.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return tvlv_value_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * batadv_tvlv_call_handler() - parse the given tvlv buffer to call the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * appropriate handlers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * @tvlv_handler: tvlv callback function handling the tvlv content
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * @ogm_source: flag indicating whether the tvlv is an ogm or a unicast packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * @orig_node: orig node emitting the ogm packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * @src: source mac address of the unicast packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * @dst: destination mac address of the unicast packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * @tvlv_value: tvlv content
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * @tvlv_value_len: tvlv content length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * Return: success if the handler was not found or the return value of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) * handler callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) static int batadv_tvlv_call_handler(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) struct batadv_tvlv_handler *tvlv_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) bool ogm_source,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct batadv_orig_node *orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) u8 *src, u8 *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) void *tvlv_value, u16 tvlv_value_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (!tvlv_handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (ogm_source) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (!tvlv_handler->ogm_handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (!orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) tvlv_handler->ogm_handler(bat_priv, orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) BATADV_NO_FLAGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) tvlv_value, tvlv_value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) tvlv_handler->flags |= BATADV_TVLV_HANDLER_OGM_CALLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (!src)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (!dst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (!tvlv_handler->unicast_handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) return tvlv_handler->unicast_handler(bat_priv, src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) dst, tvlv_value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) tvlv_value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * batadv_tvlv_containers_process() - parse the given tvlv buffer to call the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) * appropriate handlers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * @ogm_source: flag indicating whether the tvlv is an ogm or a unicast packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * @orig_node: orig node emitting the ogm packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * @src: source mac address of the unicast packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * @dst: destination mac address of the unicast packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * @tvlv_value: tvlv content
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * @tvlv_value_len: tvlv content length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * Return: success when processing an OGM or the return value of all called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * handler callbacks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) int batadv_tvlv_containers_process(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) bool ogm_source,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) struct batadv_orig_node *orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) u8 *src, u8 *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) void *tvlv_value, u16 tvlv_value_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) struct batadv_tvlv_handler *tvlv_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) struct batadv_tvlv_hdr *tvlv_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) u16 tvlv_value_cont_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) u8 cifnotfound = BATADV_TVLV_HANDLER_OGM_CIFNOTFND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) int ret = NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) while (tvlv_value_len >= sizeof(*tvlv_hdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) tvlv_hdr = tvlv_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) tvlv_value_cont_len = ntohs(tvlv_hdr->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) tvlv_value = tvlv_hdr + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) tvlv_value_len -= sizeof(*tvlv_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (tvlv_value_cont_len > tvlv_value_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) tvlv_handler = batadv_tvlv_handler_get(bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) tvlv_hdr->type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) tvlv_hdr->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) ret |= batadv_tvlv_call_handler(bat_priv, tvlv_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) ogm_source, orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) src, dst, tvlv_value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) tvlv_value_cont_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) if (tvlv_handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) batadv_tvlv_handler_put(tvlv_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) tvlv_value = (u8 *)tvlv_value + tvlv_value_cont_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) tvlv_value_len -= tvlv_value_cont_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) if (!ogm_source)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) hlist_for_each_entry_rcu(tvlv_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) &bat_priv->tvlv.handler_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) if ((tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) !(tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CALLED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) tvlv_handler->ogm_handler(bat_priv, orig_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) cifnotfound, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) tvlv_handler->flags &= ~BATADV_TVLV_HANDLER_OGM_CALLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * batadv_tvlv_ogm_receive() - process an incoming ogm and call the appropriate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) * handlers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) * @batadv_ogm_packet: ogm packet containing the tvlv containers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) * @orig_node: orig node emitting the ogm packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) void batadv_tvlv_ogm_receive(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) struct batadv_ogm_packet *batadv_ogm_packet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) struct batadv_orig_node *orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) void *tvlv_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) u16 tvlv_value_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) if (!batadv_ogm_packet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) tvlv_value_len = ntohs(batadv_ogm_packet->tvlv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) if (!tvlv_value_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) tvlv_value = batadv_ogm_packet + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) batadv_tvlv_containers_process(bat_priv, true, orig_node, NULL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) tvlv_value, tvlv_value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) * batadv_tvlv_handler_register() - register tvlv handler based on the provided
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) * type and version (both need to match) for ogm tvlv payload and/or unicast
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) * payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) * @optr: ogm tvlv handler callback function. This function receives the orig
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) * node, flags and the tvlv content as argument to process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) * @uptr: unicast tvlv handler callback function. This function receives the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) * source & destination of the unicast packet as well as the tvlv content
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) * to process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * @type: tvlv handler type to be registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) * @version: tvlv handler version to be registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) * @flags: flags to enable or disable TVLV API behavior
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) void batadv_tvlv_handler_register(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) void (*optr)(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) struct batadv_orig_node *orig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) u8 flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) void *tvlv_value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) u16 tvlv_value_len),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) int (*uptr)(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) u8 *src, u8 *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) void *tvlv_value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) u16 tvlv_value_len),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) u8 type, u8 version, u8 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) struct batadv_tvlv_handler *tvlv_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) if (tvlv_handler) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) batadv_tvlv_handler_put(tvlv_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) tvlv_handler = kzalloc(sizeof(*tvlv_handler), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) if (!tvlv_handler) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) tvlv_handler->ogm_handler = optr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) tvlv_handler->unicast_handler = uptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) tvlv_handler->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) tvlv_handler->version = version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) tvlv_handler->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) kref_init(&tvlv_handler->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) INIT_HLIST_NODE(&tvlv_handler->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) kref_get(&tvlv_handler->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) hlist_add_head_rcu(&tvlv_handler->list, &bat_priv->tvlv.handler_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) /* don't return reference to new tvlv_handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) batadv_tvlv_handler_put(tvlv_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * batadv_tvlv_handler_unregister() - unregister tvlv handler based on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) * provided type and version (both need to match)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * @type: tvlv handler type to be unregistered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * @version: tvlv handler version to be unregistered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) void batadv_tvlv_handler_unregister(struct batadv_priv *bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) u8 type, u8 version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) struct batadv_tvlv_handler *tvlv_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) if (!tvlv_handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) batadv_tvlv_handler_put(tvlv_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) hlist_del_rcu(&tvlv_handler->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) batadv_tvlv_handler_put(tvlv_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) * batadv_tvlv_unicast_send() - send a unicast packet with tvlv payload to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) * specified host
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) * @src: source mac address of the unicast packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) * @dst: destination mac address of the unicast packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) * @type: tvlv type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) * @version: tvlv version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) * @tvlv_value: tvlv content
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) * @tvlv_value_len: tvlv content length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) void batadv_tvlv_unicast_send(struct batadv_priv *bat_priv, u8 *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) u8 *dst, u8 type, u8 version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) void *tvlv_value, u16 tvlv_value_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) struct batadv_tvlv_hdr *tvlv_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) struct batadv_orig_node *orig_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) unsigned char *tvlv_buff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) unsigned int tvlv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) ssize_t hdr_len = sizeof(*unicast_tvlv_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) orig_node = batadv_orig_hash_find(bat_priv, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) if (!orig_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) tvlv_len = sizeof(*tvlv_hdr) + tvlv_value_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + hdr_len + tvlv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) skb->priority = TC_PRIO_CONTROL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) skb_reserve(skb, ETH_HLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) tvlv_buff = skb_put(skb, sizeof(*unicast_tvlv_packet) + tvlv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)tvlv_buff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) unicast_tvlv_packet->packet_type = BATADV_UNICAST_TVLV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) unicast_tvlv_packet->version = BATADV_COMPAT_VERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) unicast_tvlv_packet->ttl = BATADV_TTL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) unicast_tvlv_packet->reserved = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) unicast_tvlv_packet->tvlv_len = htons(tvlv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) unicast_tvlv_packet->align = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) ether_addr_copy(unicast_tvlv_packet->src, src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) ether_addr_copy(unicast_tvlv_packet->dst, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) tvlv_buff = (unsigned char *)(unicast_tvlv_packet + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) tvlv_hdr = (struct batadv_tvlv_hdr *)tvlv_buff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) tvlv_hdr->version = version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) tvlv_hdr->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) tvlv_hdr->len = htons(tvlv_value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) tvlv_buff += sizeof(*tvlv_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) memcpy(tvlv_buff, tvlv_value, tvlv_value_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) batadv_send_skb_to_orig(skb, orig_node, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) batadv_orig_node_put(orig_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }