tvlv.c 20 KB

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