tvlv.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2007-2020 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/etherdevice.h>
  9. #include <linux/gfp.h>
  10. #include <linux/if_ether.h>
  11. #include <linux/kernel.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. * @ogm_source: flag indicating whether the tvlv is an ogm or a unicast packet
  301. * @orig_node: orig node emitting the ogm packet
  302. * @src: source mac address of the unicast packet
  303. * @dst: destination mac address of the unicast packet
  304. * @tvlv_value: tvlv content
  305. * @tvlv_value_len: tvlv content length
  306. *
  307. * Return: success if the handler was not found or the return value of the
  308. * handler callback.
  309. */
  310. static int batadv_tvlv_call_handler(struct batadv_priv *bat_priv,
  311. struct batadv_tvlv_handler *tvlv_handler,
  312. bool ogm_source,
  313. struct batadv_orig_node *orig_node,
  314. u8 *src, u8 *dst,
  315. void *tvlv_value, u16 tvlv_value_len)
  316. {
  317. if (!tvlv_handler)
  318. return NET_RX_SUCCESS;
  319. if (ogm_source) {
  320. if (!tvlv_handler->ogm_handler)
  321. return NET_RX_SUCCESS;
  322. if (!orig_node)
  323. return NET_RX_SUCCESS;
  324. tvlv_handler->ogm_handler(bat_priv, orig_node,
  325. BATADV_NO_FLAGS,
  326. tvlv_value, tvlv_value_len);
  327. tvlv_handler->flags |= BATADV_TVLV_HANDLER_OGM_CALLED;
  328. } else {
  329. if (!src)
  330. return NET_RX_SUCCESS;
  331. if (!dst)
  332. return NET_RX_SUCCESS;
  333. if (!tvlv_handler->unicast_handler)
  334. return NET_RX_SUCCESS;
  335. return tvlv_handler->unicast_handler(bat_priv, src,
  336. dst, tvlv_value,
  337. tvlv_value_len);
  338. }
  339. return NET_RX_SUCCESS;
  340. }
  341. /**
  342. * batadv_tvlv_containers_process() - parse the given tvlv buffer to call the
  343. * appropriate handlers
  344. * @bat_priv: the bat priv with all the soft interface information
  345. * @ogm_source: flag indicating whether the tvlv is an ogm or a unicast packet
  346. * @orig_node: orig node emitting the ogm packet
  347. * @src: source mac address of the unicast packet
  348. * @dst: destination mac address of the unicast packet
  349. * @tvlv_value: tvlv content
  350. * @tvlv_value_len: tvlv content length
  351. *
  352. * Return: success when processing an OGM or the return value of all called
  353. * handler callbacks.
  354. */
  355. int batadv_tvlv_containers_process(struct batadv_priv *bat_priv,
  356. bool ogm_source,
  357. struct batadv_orig_node *orig_node,
  358. u8 *src, u8 *dst,
  359. void *tvlv_value, u16 tvlv_value_len)
  360. {
  361. struct batadv_tvlv_handler *tvlv_handler;
  362. struct batadv_tvlv_hdr *tvlv_hdr;
  363. u16 tvlv_value_cont_len;
  364. u8 cifnotfound = BATADV_TVLV_HANDLER_OGM_CIFNOTFND;
  365. int ret = NET_RX_SUCCESS;
  366. while (tvlv_value_len >= sizeof(*tvlv_hdr)) {
  367. tvlv_hdr = tvlv_value;
  368. tvlv_value_cont_len = ntohs(tvlv_hdr->len);
  369. tvlv_value = tvlv_hdr + 1;
  370. tvlv_value_len -= sizeof(*tvlv_hdr);
  371. if (tvlv_value_cont_len > tvlv_value_len)
  372. break;
  373. tvlv_handler = batadv_tvlv_handler_get(bat_priv,
  374. tvlv_hdr->type,
  375. tvlv_hdr->version);
  376. ret |= batadv_tvlv_call_handler(bat_priv, tvlv_handler,
  377. ogm_source, orig_node,
  378. src, dst, tvlv_value,
  379. tvlv_value_cont_len);
  380. if (tvlv_handler)
  381. batadv_tvlv_handler_put(tvlv_handler);
  382. tvlv_value = (u8 *)tvlv_value + tvlv_value_cont_len;
  383. tvlv_value_len -= tvlv_value_cont_len;
  384. }
  385. if (!ogm_source)
  386. return ret;
  387. rcu_read_lock();
  388. hlist_for_each_entry_rcu(tvlv_handler,
  389. &bat_priv->tvlv.handler_list, list) {
  390. if ((tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) &&
  391. !(tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CALLED))
  392. tvlv_handler->ogm_handler(bat_priv, orig_node,
  393. cifnotfound, NULL, 0);
  394. tvlv_handler->flags &= ~BATADV_TVLV_HANDLER_OGM_CALLED;
  395. }
  396. rcu_read_unlock();
  397. return NET_RX_SUCCESS;
  398. }
  399. /**
  400. * batadv_tvlv_ogm_receive() - process an incoming ogm and call the appropriate
  401. * handlers
  402. * @bat_priv: the bat priv with all the soft interface information
  403. * @batadv_ogm_packet: ogm packet containing the tvlv containers
  404. * @orig_node: orig node emitting the ogm packet
  405. */
  406. void batadv_tvlv_ogm_receive(struct batadv_priv *bat_priv,
  407. struct batadv_ogm_packet *batadv_ogm_packet,
  408. struct batadv_orig_node *orig_node)
  409. {
  410. void *tvlv_value;
  411. u16 tvlv_value_len;
  412. if (!batadv_ogm_packet)
  413. return;
  414. tvlv_value_len = ntohs(batadv_ogm_packet->tvlv_len);
  415. if (!tvlv_value_len)
  416. return;
  417. tvlv_value = batadv_ogm_packet + 1;
  418. batadv_tvlv_containers_process(bat_priv, true, orig_node, NULL, NULL,
  419. tvlv_value, tvlv_value_len);
  420. }
  421. /**
  422. * batadv_tvlv_handler_register() - register tvlv handler based on the provided
  423. * type and version (both need to match) for ogm tvlv payload and/or unicast
  424. * payload
  425. * @bat_priv: the bat priv with all the soft interface information
  426. * @optr: ogm tvlv handler callback function. This function receives the orig
  427. * node, flags and the tvlv content as argument to process.
  428. * @uptr: unicast tvlv handler callback function. This function receives the
  429. * source & destination of the unicast packet as well as the tvlv content
  430. * to process.
  431. * @type: tvlv handler type to be registered
  432. * @version: tvlv handler version to be registered
  433. * @flags: flags to enable or disable TVLV API behavior
  434. */
  435. void batadv_tvlv_handler_register(struct batadv_priv *bat_priv,
  436. void (*optr)(struct batadv_priv *bat_priv,
  437. struct batadv_orig_node *orig,
  438. u8 flags,
  439. void *tvlv_value,
  440. u16 tvlv_value_len),
  441. int (*uptr)(struct batadv_priv *bat_priv,
  442. u8 *src, u8 *dst,
  443. void *tvlv_value,
  444. u16 tvlv_value_len),
  445. u8 type, u8 version, u8 flags)
  446. {
  447. struct batadv_tvlv_handler *tvlv_handler;
  448. spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
  449. tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
  450. if (tvlv_handler) {
  451. spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
  452. batadv_tvlv_handler_put(tvlv_handler);
  453. return;
  454. }
  455. tvlv_handler = kzalloc(sizeof(*tvlv_handler), GFP_ATOMIC);
  456. if (!tvlv_handler) {
  457. spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
  458. return;
  459. }
  460. tvlv_handler->ogm_handler = optr;
  461. tvlv_handler->unicast_handler = uptr;
  462. tvlv_handler->type = type;
  463. tvlv_handler->version = version;
  464. tvlv_handler->flags = flags;
  465. kref_init(&tvlv_handler->refcount);
  466. INIT_HLIST_NODE(&tvlv_handler->list);
  467. kref_get(&tvlv_handler->refcount);
  468. hlist_add_head_rcu(&tvlv_handler->list, &bat_priv->tvlv.handler_list);
  469. spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
  470. /* don't return reference to new tvlv_handler */
  471. batadv_tvlv_handler_put(tvlv_handler);
  472. }
  473. /**
  474. * batadv_tvlv_handler_unregister() - unregister tvlv handler based on the
  475. * provided type and version (both need to match)
  476. * @bat_priv: the bat priv with all the soft interface information
  477. * @type: tvlv handler type to be unregistered
  478. * @version: tvlv handler version to be unregistered
  479. */
  480. void batadv_tvlv_handler_unregister(struct batadv_priv *bat_priv,
  481. u8 type, u8 version)
  482. {
  483. struct batadv_tvlv_handler *tvlv_handler;
  484. tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
  485. if (!tvlv_handler)
  486. return;
  487. batadv_tvlv_handler_put(tvlv_handler);
  488. spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
  489. hlist_del_rcu(&tvlv_handler->list);
  490. spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
  491. batadv_tvlv_handler_put(tvlv_handler);
  492. }
  493. /**
  494. * batadv_tvlv_unicast_send() - send a unicast packet with tvlv payload to the
  495. * specified host
  496. * @bat_priv: the bat priv with all the soft interface information
  497. * @src: source mac address of the unicast packet
  498. * @dst: destination mac address of the unicast packet
  499. * @type: tvlv type
  500. * @version: tvlv version
  501. * @tvlv_value: tvlv content
  502. * @tvlv_value_len: tvlv content length
  503. */
  504. void batadv_tvlv_unicast_send(struct batadv_priv *bat_priv, u8 *src,
  505. u8 *dst, u8 type, u8 version,
  506. void *tvlv_value, u16 tvlv_value_len)
  507. {
  508. struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
  509. struct batadv_tvlv_hdr *tvlv_hdr;
  510. struct batadv_orig_node *orig_node;
  511. struct sk_buff *skb;
  512. unsigned char *tvlv_buff;
  513. unsigned int tvlv_len;
  514. ssize_t hdr_len = sizeof(*unicast_tvlv_packet);
  515. orig_node = batadv_orig_hash_find(bat_priv, dst);
  516. if (!orig_node)
  517. return;
  518. tvlv_len = sizeof(*tvlv_hdr) + tvlv_value_len;
  519. skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + hdr_len + tvlv_len);
  520. if (!skb)
  521. goto out;
  522. skb->priority = TC_PRIO_CONTROL;
  523. skb_reserve(skb, ETH_HLEN);
  524. tvlv_buff = skb_put(skb, sizeof(*unicast_tvlv_packet) + tvlv_len);
  525. unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)tvlv_buff;
  526. unicast_tvlv_packet->packet_type = BATADV_UNICAST_TVLV;
  527. unicast_tvlv_packet->version = BATADV_COMPAT_VERSION;
  528. unicast_tvlv_packet->ttl = BATADV_TTL;
  529. unicast_tvlv_packet->reserved = 0;
  530. unicast_tvlv_packet->tvlv_len = htons(tvlv_len);
  531. unicast_tvlv_packet->align = 0;
  532. ether_addr_copy(unicast_tvlv_packet->src, src);
  533. ether_addr_copy(unicast_tvlv_packet->dst, dst);
  534. tvlv_buff = (unsigned char *)(unicast_tvlv_packet + 1);
  535. tvlv_hdr = (struct batadv_tvlv_hdr *)tvlv_buff;
  536. tvlv_hdr->version = version;
  537. tvlv_hdr->type = type;
  538. tvlv_hdr->len = htons(tvlv_value_len);
  539. tvlv_buff += sizeof(*tvlv_hdr);
  540. memcpy(tvlv_buff, tvlv_value, tvlv_value_len);
  541. batadv_send_skb_to_orig(skb, orig_node, NULL);
  542. out:
  543. batadv_orig_node_put(orig_node);
  544. }