multicast_forw.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) B.A.T.M.A.N. contributors:
  3. *
  4. * Linus Lüssing
  5. */
  6. #include "multicast.h"
  7. #include "main.h"
  8. #include <linux/bug.h>
  9. #include <linux/build_bug.h>
  10. #include <linux/byteorder/generic.h>
  11. #include <linux/compiler.h>
  12. #include <linux/errno.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/gfp.h>
  15. #include <linux/if_ether.h>
  16. #include <linux/if_vlan.h>
  17. #include <linux/ipv6.h>
  18. #include <linux/limits.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/rculist.h>
  21. #include <linux/rcupdate.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/stddef.h>
  24. #include <linux/string.h>
  25. #include <linux/types.h>
  26. #include <uapi/linux/batadv_packet.h>
  27. #include "bridge_loop_avoidance.h"
  28. #include "originator.h"
  29. #include "send.h"
  30. #include "translation-table.h"
  31. #define batadv_mcast_forw_tracker_for_each_dest(dest, num_dests) \
  32. for (; num_dests; num_dests--, (dest) += ETH_ALEN)
  33. #define batadv_mcast_forw_tracker_for_each_dest2(dest1, dest2, num_dests) \
  34. for (; num_dests; num_dests--, (dest1) += ETH_ALEN, (dest2) += ETH_ALEN)
  35. /**
  36. * batadv_mcast_forw_skb_push() - skb_push and memorize amount of pushed bytes
  37. * @skb: the skb to push onto
  38. * @size: the amount of bytes to push
  39. * @len: stores the total amount of bytes pushed
  40. *
  41. * Performs an skb_push() onto the given skb and adds the amount of pushed bytes
  42. * to the given len pointer.
  43. *
  44. * Return: the return value of the skb_push() call.
  45. */
  46. static void *batadv_mcast_forw_skb_push(struct sk_buff *skb, size_t size,
  47. unsigned short *len)
  48. {
  49. *len += size;
  50. return skb_push(skb, size);
  51. }
  52. /**
  53. * batadv_mcast_forw_push_padding() - push 2 padding bytes to skb's front
  54. * @skb: the skb to push onto
  55. * @tvlv_len: stores the amount of currently pushed TVLV bytes
  56. *
  57. * Pushes two padding bytes to the front of the given skb.
  58. *
  59. * Return: On success a pointer to the first byte of the two pushed padding
  60. * bytes within the skb. NULL otherwise.
  61. */
  62. static char *
  63. batadv_mcast_forw_push_padding(struct sk_buff *skb, unsigned short *tvlv_len)
  64. {
  65. const int pad_len = 2;
  66. char *padding;
  67. if (skb_headroom(skb) < pad_len)
  68. return NULL;
  69. padding = batadv_mcast_forw_skb_push(skb, pad_len, tvlv_len);
  70. memset(padding, 0, pad_len);
  71. return padding;
  72. }
  73. /**
  74. * batadv_mcast_forw_push_est_padding() - push padding bytes if necessary
  75. * @skb: the skb to potentially push the padding onto
  76. * @count: the (estimated) number of originators the multicast packet needs to
  77. * be sent to
  78. * @tvlv_len: stores the amount of currently pushed TVLV bytes
  79. *
  80. * If the number of destination entries is even then this adds two
  81. * padding bytes to the end of the tracker TVLV.
  82. *
  83. * Return: true on success or if no padding is needed, false otherwise.
  84. */
  85. static bool
  86. batadv_mcast_forw_push_est_padding(struct sk_buff *skb, int count,
  87. unsigned short *tvlv_len)
  88. {
  89. if (!(count % 2) && !batadv_mcast_forw_push_padding(skb, tvlv_len))
  90. return false;
  91. return true;
  92. }
  93. /**
  94. * batadv_mcast_forw_orig_entry() - get orig_node from an hlist node
  95. * @node: the hlist node to get the orig_node from
  96. * @entry_offset: the offset of the hlist node within the orig_node struct
  97. *
  98. * Return: The orig_node containing the hlist node on success, NULL on error.
  99. */
  100. static struct batadv_orig_node *
  101. batadv_mcast_forw_orig_entry(struct hlist_node *node,
  102. size_t entry_offset)
  103. {
  104. /* sanity check */
  105. switch (entry_offset) {
  106. case offsetof(struct batadv_orig_node, mcast_want_all_ipv4_node):
  107. case offsetof(struct batadv_orig_node, mcast_want_all_ipv6_node):
  108. case offsetof(struct batadv_orig_node, mcast_want_all_rtr4_node):
  109. case offsetof(struct batadv_orig_node, mcast_want_all_rtr6_node):
  110. break;
  111. default:
  112. WARN_ON(1);
  113. return NULL;
  114. }
  115. return (struct batadv_orig_node *)((void *)node - entry_offset);
  116. }
  117. /**
  118. * batadv_mcast_forw_push_dest() - push an originator MAC address onto an skb
  119. * @bat_priv: the bat priv with all the soft interface information
  120. * @skb: the skb to push the destination address onto
  121. * @vid: the vlan identifier
  122. * @orig_node: the originator node to get the MAC address from
  123. * @num_dests: a pointer to store the number of pushed addresses in
  124. * @tvlv_len: stores the amount of currently pushed TVLV bytes
  125. *
  126. * If the orig_node is a BLA backbone gateway, if there is not enough skb
  127. * headroom available or if num_dests is already at its maximum (65535) then
  128. * neither the skb nor num_dests is changed. Otherwise the originator's MAC
  129. * address is pushed onto the given skb and num_dests incremented by one.
  130. *
  131. * Return: true if the orig_node is a backbone gateway or if an orig address
  132. * was pushed successfully, false otherwise.
  133. */
  134. static bool batadv_mcast_forw_push_dest(struct batadv_priv *bat_priv,
  135. struct sk_buff *skb, unsigned short vid,
  136. struct batadv_orig_node *orig_node,
  137. unsigned short *num_dests,
  138. unsigned short *tvlv_len)
  139. {
  140. BUILD_BUG_ON(sizeof_field(struct batadv_tvlv_mcast_tracker, num_dests)
  141. != sizeof(__be16));
  142. /* Avoid sending to other BLA gateways - they already got the frame from
  143. * the LAN side we share with them.
  144. * TODO: Refactor to take BLA into account earlier in mode check.
  145. */
  146. if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
  147. return true;
  148. if (skb_headroom(skb) < ETH_ALEN || *num_dests == U16_MAX)
  149. return false;
  150. batadv_mcast_forw_skb_push(skb, ETH_ALEN, tvlv_len);
  151. ether_addr_copy(skb->data, orig_node->orig);
  152. (*num_dests)++;
  153. return true;
  154. }
  155. /**
  156. * batadv_mcast_forw_push_dests_list() - push originators from list onto an skb
  157. * @bat_priv: the bat priv with all the soft interface information
  158. * @skb: the skb to push the destination addresses onto
  159. * @vid: the vlan identifier
  160. * @head: the list to gather originators from
  161. * @entry_offset: offset of an hlist node in an orig_node structure
  162. * @num_dests: a pointer to store the number of pushed addresses in
  163. * @tvlv_len: stores the amount of currently pushed TVLV bytes
  164. *
  165. * Push the MAC addresses of all originators in the given list onto the given
  166. * skb.
  167. *
  168. * Return: true on success, false otherwise.
  169. */
  170. static int batadv_mcast_forw_push_dests_list(struct batadv_priv *bat_priv,
  171. struct sk_buff *skb,
  172. unsigned short vid,
  173. struct hlist_head *head,
  174. size_t entry_offset,
  175. unsigned short *num_dests,
  176. unsigned short *tvlv_len)
  177. {
  178. struct hlist_node *node;
  179. struct batadv_orig_node *orig_node;
  180. rcu_read_lock();
  181. __hlist_for_each_rcu(node, head) {
  182. orig_node = batadv_mcast_forw_orig_entry(node, entry_offset);
  183. if (!orig_node ||
  184. !batadv_mcast_forw_push_dest(bat_priv, skb, vid, orig_node,
  185. num_dests, tvlv_len)) {
  186. rcu_read_unlock();
  187. return false;
  188. }
  189. }
  190. rcu_read_unlock();
  191. return true;
  192. }
  193. /**
  194. * batadv_mcast_forw_push_tt() - push originators with interest through TT
  195. * @bat_priv: the bat priv with all the soft interface information
  196. * @skb: the skb to push the destination addresses onto
  197. * @vid: the vlan identifier
  198. * @num_dests: a pointer to store the number of pushed addresses in
  199. * @tvlv_len: stores the amount of currently pushed TVLV bytes
  200. *
  201. * Push the MAC addresses of all originators which have indicated interest in
  202. * this multicast packet through the translation table onto the given skb.
  203. *
  204. * Return: true on success, false otherwise.
  205. */
  206. static bool
  207. batadv_mcast_forw_push_tt(struct batadv_priv *bat_priv, struct sk_buff *skb,
  208. unsigned short vid, unsigned short *num_dests,
  209. unsigned short *tvlv_len)
  210. {
  211. struct batadv_tt_orig_list_entry *orig_entry;
  212. struct batadv_tt_global_entry *tt_global;
  213. const u8 *addr = eth_hdr(skb)->h_dest;
  214. /* ok */
  215. int ret = true;
  216. tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
  217. if (!tt_global)
  218. goto out;
  219. rcu_read_lock();
  220. hlist_for_each_entry_rcu(orig_entry, &tt_global->orig_list, list) {
  221. if (!batadv_mcast_forw_push_dest(bat_priv, skb, vid,
  222. orig_entry->orig_node,
  223. num_dests, tvlv_len)) {
  224. ret = false;
  225. break;
  226. }
  227. }
  228. rcu_read_unlock();
  229. batadv_tt_global_entry_put(tt_global);
  230. out:
  231. return ret;
  232. }
  233. /**
  234. * batadv_mcast_forw_push_want_all() - push originators with want-all flag
  235. * @bat_priv: the bat priv with all the soft interface information
  236. * @skb: the skb to push the destination addresses onto
  237. * @vid: the vlan identifier
  238. * @num_dests: a pointer to store the number of pushed addresses in
  239. * @tvlv_len: stores the amount of currently pushed TVLV bytes
  240. *
  241. * Push the MAC addresses of all originators which have indicated interest in
  242. * this multicast packet through the want-all flag onto the given skb.
  243. *
  244. * Return: true on success, false otherwise.
  245. */
  246. static bool batadv_mcast_forw_push_want_all(struct batadv_priv *bat_priv,
  247. struct sk_buff *skb,
  248. unsigned short vid,
  249. unsigned short *num_dests,
  250. unsigned short *tvlv_len)
  251. {
  252. struct hlist_head *head = NULL;
  253. size_t offset;
  254. int ret;
  255. switch (eth_hdr(skb)->h_proto) {
  256. case htons(ETH_P_IP):
  257. head = &bat_priv->mcast.want_all_ipv4_list;
  258. offset = offsetof(struct batadv_orig_node,
  259. mcast_want_all_ipv4_node);
  260. break;
  261. case htons(ETH_P_IPV6):
  262. head = &bat_priv->mcast.want_all_ipv6_list;
  263. offset = offsetof(struct batadv_orig_node,
  264. mcast_want_all_ipv6_node);
  265. break;
  266. default:
  267. return false;
  268. }
  269. ret = batadv_mcast_forw_push_dests_list(bat_priv, skb, vid, head,
  270. offset, num_dests, tvlv_len);
  271. if (!ret)
  272. return false;
  273. return true;
  274. }
  275. /**
  276. * batadv_mcast_forw_push_want_rtr() - push originators with want-router flag
  277. * @bat_priv: the bat priv with all the soft interface information
  278. * @skb: the skb to push the destination addresses onto
  279. * @vid: the vlan identifier
  280. * @num_dests: a pointer to store the number of pushed addresses in
  281. * @tvlv_len: stores the amount of currently pushed TVLV bytes
  282. *
  283. * Push the MAC addresses of all originators which have indicated interest in
  284. * this multicast packet through the want-all-rtr flag onto the given skb.
  285. *
  286. * Return: true on success, false otherwise.
  287. */
  288. static bool batadv_mcast_forw_push_want_rtr(struct batadv_priv *bat_priv,
  289. struct sk_buff *skb,
  290. unsigned short vid,
  291. unsigned short *num_dests,
  292. unsigned short *tvlv_len)
  293. {
  294. struct hlist_head *head = NULL;
  295. size_t offset;
  296. int ret;
  297. switch (eth_hdr(skb)->h_proto) {
  298. case htons(ETH_P_IP):
  299. head = &bat_priv->mcast.want_all_rtr4_list;
  300. offset = offsetof(struct batadv_orig_node,
  301. mcast_want_all_rtr4_node);
  302. break;
  303. case htons(ETH_P_IPV6):
  304. head = &bat_priv->mcast.want_all_rtr6_list;
  305. offset = offsetof(struct batadv_orig_node,
  306. mcast_want_all_rtr6_node);
  307. break;
  308. default:
  309. return false;
  310. }
  311. ret = batadv_mcast_forw_push_dests_list(bat_priv, skb, vid, head,
  312. offset, num_dests, tvlv_len);
  313. if (!ret)
  314. return false;
  315. return true;
  316. }
  317. /**
  318. * batadv_mcast_forw_scrape() - remove bytes within skb data
  319. * @skb: the skb to remove bytes from
  320. * @offset: the offset from the skb data from which to scrape
  321. * @len: the amount of bytes to scrape starting from the offset
  322. *
  323. * Scrapes/removes len bytes from the given skb at the given offset from the
  324. * skb data.
  325. *
  326. * Caller needs to ensure that the region from the skb data's start up
  327. * to/including the to be removed bytes are linearized.
  328. */
  329. static void batadv_mcast_forw_scrape(struct sk_buff *skb,
  330. unsigned short offset,
  331. unsigned short len)
  332. {
  333. char *to, *from;
  334. SKB_LINEAR_ASSERT(skb);
  335. to = skb_pull(skb, len);
  336. from = to - len;
  337. memmove(to, from, offset);
  338. }
  339. /**
  340. * batadv_mcast_forw_push_scrape_padding() - remove TVLV padding
  341. * @skb: the skb to potentially adjust the TVLV's padding on
  342. * @tvlv_len: stores the amount of currently pushed TVLV bytes
  343. *
  344. * Remove two padding bytes from the end of the multicast tracker TVLV,
  345. * from before the payload data.
  346. *
  347. * Caller needs to ensure that the TVLV bytes are linearized.
  348. */
  349. static void batadv_mcast_forw_push_scrape_padding(struct sk_buff *skb,
  350. unsigned short *tvlv_len)
  351. {
  352. const int pad_len = 2;
  353. batadv_mcast_forw_scrape(skb, *tvlv_len - pad_len, pad_len);
  354. *tvlv_len -= pad_len;
  355. }
  356. /**
  357. * batadv_mcast_forw_push_insert_padding() - insert TVLV padding
  358. * @skb: the skb to potentially adjust the TVLV's padding on
  359. * @tvlv_len: stores the amount of currently pushed TVLV bytes
  360. *
  361. * Inserts two padding bytes at the end of the multicast tracker TVLV,
  362. * before the payload data in the given skb.
  363. *
  364. * Return: true on success, false otherwise.
  365. */
  366. static bool batadv_mcast_forw_push_insert_padding(struct sk_buff *skb,
  367. unsigned short *tvlv_len)
  368. {
  369. unsigned short offset = *tvlv_len;
  370. char *to, *from = skb->data;
  371. to = batadv_mcast_forw_push_padding(skb, tvlv_len);
  372. if (!to)
  373. return false;
  374. memmove(to, from, offset);
  375. memset(to + offset, 0, *tvlv_len - offset);
  376. return true;
  377. }
  378. /**
  379. * batadv_mcast_forw_push_adjust_padding() - adjust padding if necessary
  380. * @skb: the skb to potentially adjust the TVLV's padding on
  381. * @count: the estimated number of originators the multicast packet needs to
  382. * be sent to
  383. * @num_dests_pushed: the number of originators that were actually added to the
  384. * multicast packet's tracker TVLV
  385. * @tvlv_len: stores the amount of currently pushed TVLV bytes
  386. *
  387. * Adjusts the padding in the multicast packet's tracker TVLV depending on the
  388. * initially estimated amount of destinations versus the amount of destinations
  389. * that were actually added to the tracker TVLV.
  390. *
  391. * If the initial estimate was correct or at least the oddness was the same then
  392. * no padding adjustment is performed.
  393. * If the initially estimated number was even, so padding was initially added,
  394. * but it turned out to be odd then padding is removed.
  395. * If the initially estimated number was odd, so no padding was initially added,
  396. * but it turned out to be even then padding is added.
  397. *
  398. * Return: true if no padding adjustment is needed or the adjustment was
  399. * successful, false otherwise.
  400. */
  401. static bool
  402. batadv_mcast_forw_push_adjust_padding(struct sk_buff *skb, int *count,
  403. unsigned short num_dests_pushed,
  404. unsigned short *tvlv_len)
  405. {
  406. int ret = true;
  407. if (likely((num_dests_pushed % 2) == (*count % 2)))
  408. goto out;
  409. /**
  410. * estimated even number of destinations, but turned out to be odd
  411. * -> remove padding
  412. */
  413. if (!(*count % 2) && (num_dests_pushed % 2))
  414. batadv_mcast_forw_push_scrape_padding(skb, tvlv_len);
  415. /**
  416. * estimated odd number of destinations, but turned out to be even
  417. * -> add padding
  418. */
  419. else if ((*count % 2) && (!(num_dests_pushed % 2)))
  420. ret = batadv_mcast_forw_push_insert_padding(skb, tvlv_len);
  421. out:
  422. *count = num_dests_pushed;
  423. return ret;
  424. }
  425. /**
  426. * batadv_mcast_forw_push_dests() - push originator addresses onto an skb
  427. * @bat_priv: the bat priv with all the soft interface information
  428. * @skb: the skb to push the destination addresses onto
  429. * @vid: the vlan identifier
  430. * @is_routable: indicates whether the destination is routable
  431. * @count: the number of originators the multicast packet needs to be sent to
  432. * @tvlv_len: stores the amount of currently pushed TVLV bytes
  433. *
  434. * Push the MAC addresses of all originators which have indicated interest in
  435. * this multicast packet onto the given skb.
  436. *
  437. * Return: -ENOMEM if there is not enough skb headroom available. Otherwise, on
  438. * success 0.
  439. */
  440. static int
  441. batadv_mcast_forw_push_dests(struct batadv_priv *bat_priv, struct sk_buff *skb,
  442. unsigned short vid, int is_routable, int *count,
  443. unsigned short *tvlv_len)
  444. {
  445. unsigned short num_dests = 0;
  446. if (!batadv_mcast_forw_push_est_padding(skb, *count, tvlv_len))
  447. goto err;
  448. if (!batadv_mcast_forw_push_tt(bat_priv, skb, vid, &num_dests,
  449. tvlv_len))
  450. goto err;
  451. if (!batadv_mcast_forw_push_want_all(bat_priv, skb, vid, &num_dests,
  452. tvlv_len))
  453. goto err;
  454. if (is_routable &&
  455. !batadv_mcast_forw_push_want_rtr(bat_priv, skb, vid, &num_dests,
  456. tvlv_len))
  457. goto err;
  458. if (!batadv_mcast_forw_push_adjust_padding(skb, count, num_dests,
  459. tvlv_len))
  460. goto err;
  461. return 0;
  462. err:
  463. return -ENOMEM;
  464. }
  465. /**
  466. * batadv_mcast_forw_push_tracker() - push a multicast tracker TVLV header
  467. * @skb: the skb to push the tracker TVLV onto
  468. * @num_dests: the number of destination addresses to set in the header
  469. * @tvlv_len: stores the amount of currently pushed TVLV bytes
  470. *
  471. * Pushes a multicast tracker TVLV header onto the given skb, including the
  472. * generic TVLV header but excluding the destination MAC addresses.
  473. *
  474. * The provided num_dests value is taken into consideration to set the
  475. * num_dests field in the tracker header and to set the appropriate TVLV length
  476. * value fields.
  477. *
  478. * Return: -ENOMEM if there is not enough skb headroom available. Otherwise, on
  479. * success 0.
  480. */
  481. static int batadv_mcast_forw_push_tracker(struct sk_buff *skb, int num_dests,
  482. unsigned short *tvlv_len)
  483. {
  484. struct batadv_tvlv_mcast_tracker *mcast_tracker;
  485. struct batadv_tvlv_hdr *tvlv_hdr;
  486. unsigned int tvlv_value_len;
  487. if (skb_headroom(skb) < sizeof(*mcast_tracker) + sizeof(*tvlv_hdr))
  488. return -ENOMEM;
  489. tvlv_value_len = sizeof(*mcast_tracker) + *tvlv_len;
  490. if (tvlv_value_len + sizeof(*tvlv_hdr) > U16_MAX)
  491. return -ENOMEM;
  492. batadv_mcast_forw_skb_push(skb, sizeof(*mcast_tracker), tvlv_len);
  493. mcast_tracker = (struct batadv_tvlv_mcast_tracker *)skb->data;
  494. mcast_tracker->num_dests = htons(num_dests);
  495. skb_reset_network_header(skb);
  496. batadv_mcast_forw_skb_push(skb, sizeof(*tvlv_hdr), tvlv_len);
  497. tvlv_hdr = (struct batadv_tvlv_hdr *)skb->data;
  498. tvlv_hdr->type = BATADV_TVLV_MCAST_TRACKER;
  499. tvlv_hdr->version = 1;
  500. tvlv_hdr->len = htons(tvlv_value_len);
  501. return 0;
  502. }
  503. /**
  504. * batadv_mcast_forw_push_tvlvs() - push a multicast tracker TVLV onto an skb
  505. * @bat_priv: the bat priv with all the soft interface information
  506. * @skb: the skb to push the tracker TVLV onto
  507. * @vid: the vlan identifier
  508. * @is_routable: indicates whether the destination is routable
  509. * @count: the number of originators the multicast packet needs to be sent to
  510. * @tvlv_len: stores the amount of currently pushed TVLV bytes
  511. *
  512. * Pushes a multicast tracker TVLV onto the given skb, including the collected
  513. * destination MAC addresses and the generic TVLV header.
  514. *
  515. * Return: -ENOMEM if there is not enough skb headroom available. Otherwise, on
  516. * success 0.
  517. */
  518. static int
  519. batadv_mcast_forw_push_tvlvs(struct batadv_priv *bat_priv, struct sk_buff *skb,
  520. unsigned short vid, int is_routable, int count,
  521. unsigned short *tvlv_len)
  522. {
  523. int ret;
  524. ret = batadv_mcast_forw_push_dests(bat_priv, skb, vid, is_routable,
  525. &count, tvlv_len);
  526. if (ret < 0)
  527. return ret;
  528. ret = batadv_mcast_forw_push_tracker(skb, count, tvlv_len);
  529. if (ret < 0)
  530. return ret;
  531. return 0;
  532. }
  533. /**
  534. * batadv_mcast_forw_push_hdr() - push a multicast packet header onto an skb
  535. * @skb: the skb to push the header onto
  536. * @tvlv_len: the total TVLV length value to set in the header
  537. *
  538. * Pushes a batman-adv multicast packet header onto the given skb and sets
  539. * the provided total TVLV length value in it.
  540. *
  541. * Caller needs to ensure enough skb headroom is available.
  542. *
  543. * Return: -ENOMEM if there is not enough skb headroom available. Otherwise, on
  544. * success 0.
  545. */
  546. static int
  547. batadv_mcast_forw_push_hdr(struct sk_buff *skb, unsigned short tvlv_len)
  548. {
  549. struct batadv_mcast_packet *mcast_packet;
  550. if (skb_headroom(skb) < sizeof(*mcast_packet))
  551. return -ENOMEM;
  552. skb_push(skb, sizeof(*mcast_packet));
  553. mcast_packet = (struct batadv_mcast_packet *)skb->data;
  554. mcast_packet->version = BATADV_COMPAT_VERSION;
  555. mcast_packet->ttl = BATADV_TTL;
  556. mcast_packet->packet_type = BATADV_MCAST;
  557. mcast_packet->reserved = 0;
  558. mcast_packet->tvlv_len = htons(tvlv_len);
  559. return 0;
  560. }
  561. /**
  562. * batadv_mcast_forw_scrub_dests() - scrub destinations in a tracker TVLV
  563. * @bat_priv: the bat priv with all the soft interface information
  564. * @comp_neigh: next hop neighbor to scrub+collect destinations for
  565. * @dest: start MAC entry in original skb's tracker TVLV
  566. * @next_dest: start MAC entry in to be sent skb's tracker TVLV
  567. * @num_dests: number of remaining destination MAC entries to iterate over
  568. *
  569. * This sorts destination entries into either the original batman-adv
  570. * multicast packet or the skb (copy) that is going to be sent to comp_neigh
  571. * next.
  572. *
  573. * In preparation for the next, to be (unicast) transmitted batman-adv multicast
  574. * packet skb to be sent to the given neighbor node, tries to collect all
  575. * originator MAC addresses that have the given neighbor node as their next hop
  576. * in the to be transmitted skb (copy), which next_dest points into. That is we
  577. * zero all destination entries in next_dest which do not have comp_neigh as
  578. * their next hop. And zero all destination entries in the original skb that
  579. * would have comp_neigh as their next hop (to avoid redundant transmissions and
  580. * duplicated payload later).
  581. */
  582. static void
  583. batadv_mcast_forw_scrub_dests(struct batadv_priv *bat_priv,
  584. struct batadv_neigh_node *comp_neigh, u8 *dest,
  585. u8 *next_dest, u16 num_dests)
  586. {
  587. struct batadv_neigh_node *next_neigh;
  588. /* skip first entry, this is what we are comparing with */
  589. eth_zero_addr(dest);
  590. dest += ETH_ALEN;
  591. next_dest += ETH_ALEN;
  592. num_dests--;
  593. batadv_mcast_forw_tracker_for_each_dest2(dest, next_dest, num_dests) {
  594. if (is_zero_ether_addr(next_dest))
  595. continue;
  596. /* sanity check, we expect unicast destinations */
  597. if (is_multicast_ether_addr(next_dest)) {
  598. eth_zero_addr(dest);
  599. eth_zero_addr(next_dest);
  600. continue;
  601. }
  602. next_neigh = batadv_orig_to_router(bat_priv, next_dest, NULL);
  603. if (!next_neigh) {
  604. eth_zero_addr(next_dest);
  605. continue;
  606. }
  607. if (!batadv_compare_eth(next_neigh->addr, comp_neigh->addr)) {
  608. eth_zero_addr(next_dest);
  609. batadv_neigh_node_put(next_neigh);
  610. continue;
  611. }
  612. /* found an entry for our next packet to transmit, so remove it
  613. * from the original packet
  614. */
  615. eth_zero_addr(dest);
  616. batadv_neigh_node_put(next_neigh);
  617. }
  618. }
  619. /**
  620. * batadv_mcast_forw_shrink_fill() - swap slot with next non-zero destination
  621. * @slot: the to be filled zero-MAC destination entry in a tracker TVLV
  622. * @num_dests_slot: remaining entries in tracker TVLV from/including slot
  623. *
  624. * Searches for the next non-zero-MAC destination entry in a tracker TVLV after
  625. * the given slot pointer. And if found, swaps it with the zero-MAC destination
  626. * entry which the slot points to.
  627. *
  628. * Return: true if slot was swapped/filled successfully, false otherwise.
  629. */
  630. static bool batadv_mcast_forw_shrink_fill(u8 *slot, u16 num_dests_slot)
  631. {
  632. u16 num_dests_filler;
  633. u8 *filler;
  634. /* sanity check, should not happen */
  635. if (!num_dests_slot)
  636. return false;
  637. num_dests_filler = num_dests_slot - 1;
  638. filler = slot + ETH_ALEN;
  639. /* find a candidate to fill the empty slot */
  640. batadv_mcast_forw_tracker_for_each_dest(filler, num_dests_filler) {
  641. if (is_zero_ether_addr(filler))
  642. continue;
  643. ether_addr_copy(slot, filler);
  644. eth_zero_addr(filler);
  645. return true;
  646. }
  647. return false;
  648. }
  649. /**
  650. * batadv_mcast_forw_shrink_pack_dests() - pack destinations of a tracker TVLV
  651. * @skb: the batman-adv multicast packet to compact destinations in
  652. *
  653. * Compacts the originator destination MAC addresses in the multicast tracker
  654. * TVLV of the given multicast packet. This is done by moving all non-zero
  655. * MAC addresses in direction of the skb head and all zero MAC addresses in skb
  656. * tail direction, within the multicast tracker TVLV.
  657. *
  658. * Return: The number of consecutive zero MAC address destinations which are
  659. * now at the end of the multicast tracker TVLV.
  660. */
  661. static int batadv_mcast_forw_shrink_pack_dests(struct sk_buff *skb)
  662. {
  663. struct batadv_tvlv_mcast_tracker *mcast_tracker;
  664. unsigned char *skb_net_hdr;
  665. u16 num_dests_slot;
  666. u8 *slot;
  667. skb_net_hdr = skb_network_header(skb);
  668. mcast_tracker = (struct batadv_tvlv_mcast_tracker *)skb_net_hdr;
  669. num_dests_slot = ntohs(mcast_tracker->num_dests);
  670. slot = (u8 *)mcast_tracker + sizeof(*mcast_tracker);
  671. batadv_mcast_forw_tracker_for_each_dest(slot, num_dests_slot) {
  672. /* find an empty slot */
  673. if (!is_zero_ether_addr(slot))
  674. continue;
  675. if (!batadv_mcast_forw_shrink_fill(slot, num_dests_slot))
  676. /* could not find a filler, so we successfully packed
  677. * and can stop - and must not reduce num_dests_slot!
  678. */
  679. break;
  680. }
  681. /* num_dests_slot is now the amount of reduced, zeroed
  682. * destinations at the end of the tracker TVLV
  683. */
  684. return num_dests_slot;
  685. }
  686. /**
  687. * batadv_mcast_forw_shrink_align_offset() - get new alignment offset
  688. * @num_dests_old: the old, to be updated amount of destination nodes
  689. * @num_dests_reduce: the number of destinations that were removed
  690. *
  691. * Calculates the amount of potential extra alignment offset that is needed to
  692. * adjust the TVLV padding after the change in destination nodes.
  693. *
  694. * Return:
  695. * 0: If no change to padding is needed.
  696. * 2: If padding needs to be removed.
  697. * -2: If padding needs to be added.
  698. */
  699. static short
  700. batadv_mcast_forw_shrink_align_offset(unsigned int num_dests_old,
  701. unsigned int num_dests_reduce)
  702. {
  703. /* even amount of removed destinations -> no alignment change */
  704. if (!(num_dests_reduce % 2))
  705. return 0;
  706. /* even to odd amount of destinations -> remove padding */
  707. if (!(num_dests_old % 2))
  708. return 2;
  709. /* odd to even amount of destinations -> add padding */
  710. return -2;
  711. }
  712. /**
  713. * batadv_mcast_forw_shrink_update_headers() - update shrunk mc packet headers
  714. * @skb: the batman-adv multicast packet to update headers of
  715. * @num_dests_reduce: the number of destinations that were removed
  716. *
  717. * This updates any fields of a batman-adv multicast packet that are affected
  718. * by the reduced number of destinations in the multicast tracket TVLV. In
  719. * particular this updates:
  720. *
  721. * The num_dest field of the multicast tracker TVLV.
  722. * The TVLV length field of the according generic TVLV header.
  723. * The batman-adv multicast packet's total TVLV length field.
  724. *
  725. * Return: The offset in skb's tail direction at which the new batman-adv
  726. * multicast packet header needs to start.
  727. */
  728. static unsigned int
  729. batadv_mcast_forw_shrink_update_headers(struct sk_buff *skb,
  730. unsigned int num_dests_reduce)
  731. {
  732. struct batadv_tvlv_mcast_tracker *mcast_tracker;
  733. struct batadv_mcast_packet *mcast_packet;
  734. struct batadv_tvlv_hdr *tvlv_hdr;
  735. unsigned char *skb_net_hdr;
  736. unsigned int offset;
  737. short align_offset;
  738. u16 num_dests;
  739. skb_net_hdr = skb_network_header(skb);
  740. mcast_tracker = (struct batadv_tvlv_mcast_tracker *)skb_net_hdr;
  741. num_dests = ntohs(mcast_tracker->num_dests);
  742. align_offset = batadv_mcast_forw_shrink_align_offset(num_dests,
  743. num_dests_reduce);
  744. offset = ETH_ALEN * num_dests_reduce + align_offset;
  745. num_dests -= num_dests_reduce;
  746. /* update tracker header */
  747. mcast_tracker->num_dests = htons(num_dests);
  748. /* update tracker's tvlv header's length field */
  749. tvlv_hdr = (struct batadv_tvlv_hdr *)(skb_network_header(skb) -
  750. sizeof(*tvlv_hdr));
  751. tvlv_hdr->len = htons(ntohs(tvlv_hdr->len) - offset);
  752. /* update multicast packet header's tvlv length field */
  753. mcast_packet = (struct batadv_mcast_packet *)skb->data;
  754. mcast_packet->tvlv_len = htons(ntohs(mcast_packet->tvlv_len) - offset);
  755. return offset;
  756. }
  757. /**
  758. * batadv_mcast_forw_shrink_move_headers() - move multicast headers by offset
  759. * @skb: the batman-adv multicast packet to move headers for
  760. * @offset: a non-negative offset to move headers by, towards the skb tail
  761. *
  762. * Moves the batman-adv multicast packet header, its multicast tracker TVLV and
  763. * any TVLVs in between by the given offset in direction towards the tail.
  764. */
  765. static void
  766. batadv_mcast_forw_shrink_move_headers(struct sk_buff *skb, unsigned int offset)
  767. {
  768. struct batadv_tvlv_mcast_tracker *mcast_tracker;
  769. unsigned char *skb_net_hdr;
  770. unsigned int len;
  771. u16 num_dests;
  772. skb_net_hdr = skb_network_header(skb);
  773. mcast_tracker = (struct batadv_tvlv_mcast_tracker *)skb_net_hdr;
  774. num_dests = ntohs(mcast_tracker->num_dests);
  775. len = skb_network_offset(skb) + sizeof(*mcast_tracker);
  776. len += num_dests * ETH_ALEN;
  777. batadv_mcast_forw_scrape(skb, len, offset);
  778. }
  779. /**
  780. * batadv_mcast_forw_shrink_tracker() - remove zero addresses in a tracker tvlv
  781. * @skb: the batman-adv multicast packet to (potentially) shrink
  782. *
  783. * Removes all destinations with a zero MAC addresses (00:00:00:00:00:00) from
  784. * the given batman-adv multicast packet's tracker TVLV and updates headers
  785. * accordingly to maintain a valid batman-adv multicast packet.
  786. */
  787. static void batadv_mcast_forw_shrink_tracker(struct sk_buff *skb)
  788. {
  789. unsigned int offset;
  790. u16 dests_reduced;
  791. dests_reduced = batadv_mcast_forw_shrink_pack_dests(skb);
  792. if (!dests_reduced)
  793. return;
  794. offset = batadv_mcast_forw_shrink_update_headers(skb, dests_reduced);
  795. batadv_mcast_forw_shrink_move_headers(skb, offset);
  796. }
  797. /**
  798. * batadv_mcast_forw_packet() - forward a batman-adv multicast packet
  799. * @bat_priv: the bat priv with all the soft interface information
  800. * @skb: the received or locally generated batman-adv multicast packet
  801. * @local_xmit: indicates that the packet was locally generated and not received
  802. *
  803. * Parses the tracker TVLV of a batman-adv multicast packet and forwards the
  804. * packet as indicated in this TVLV.
  805. *
  806. * Caller needs to set the skb network header to the start of the multicast
  807. * tracker TVLV (excluding the generic TVLV header) and the skb transport header
  808. * to the next byte after this multicast tracker TVLV.
  809. *
  810. * Caller needs to free the skb.
  811. *
  812. * Return: NET_RX_SUCCESS or NET_RX_DROP on success or a negative error
  813. * code on failure. NET_RX_SUCCESS if the received packet is supposed to be
  814. * decapsulated and forwarded to the own soft interface, NET_RX_DROP otherwise.
  815. */
  816. static int batadv_mcast_forw_packet(struct batadv_priv *bat_priv,
  817. struct sk_buff *skb, bool local_xmit)
  818. {
  819. struct batadv_tvlv_mcast_tracker *mcast_tracker;
  820. struct batadv_neigh_node *neigh_node;
  821. unsigned long offset, num_dests_off;
  822. struct sk_buff *nexthop_skb;
  823. unsigned char *skb_net_hdr;
  824. bool local_recv = false;
  825. unsigned int tvlv_len;
  826. bool xmitted = false;
  827. u8 *dest, *next_dest;
  828. u16 num_dests;
  829. int ret;
  830. /* (at least) TVLV part needs to be linearized */
  831. SKB_LINEAR_ASSERT(skb);
  832. /* check if num_dests is within skb length */
  833. num_dests_off = offsetof(struct batadv_tvlv_mcast_tracker, num_dests);
  834. if (num_dests_off > skb_network_header_len(skb))
  835. return -EINVAL;
  836. skb_net_hdr = skb_network_header(skb);
  837. mcast_tracker = (struct batadv_tvlv_mcast_tracker *)skb_net_hdr;
  838. num_dests = ntohs(mcast_tracker->num_dests);
  839. dest = (u8 *)mcast_tracker + sizeof(*mcast_tracker);
  840. /* check if full tracker tvlv is within skb length */
  841. tvlv_len = sizeof(*mcast_tracker) + ETH_ALEN * num_dests;
  842. if (tvlv_len > skb_network_header_len(skb))
  843. return -EINVAL;
  844. /* invalidate checksum: */
  845. skb->ip_summed = CHECKSUM_NONE;
  846. batadv_mcast_forw_tracker_for_each_dest(dest, num_dests) {
  847. if (is_zero_ether_addr(dest))
  848. continue;
  849. /* only unicast originator addresses supported */
  850. if (is_multicast_ether_addr(dest)) {
  851. eth_zero_addr(dest);
  852. continue;
  853. }
  854. if (batadv_is_my_mac(bat_priv, dest)) {
  855. eth_zero_addr(dest);
  856. local_recv = true;
  857. continue;
  858. }
  859. neigh_node = batadv_orig_to_router(bat_priv, dest, NULL);
  860. if (!neigh_node) {
  861. eth_zero_addr(dest);
  862. continue;
  863. }
  864. nexthop_skb = skb_copy(skb, GFP_ATOMIC);
  865. if (!nexthop_skb) {
  866. batadv_neigh_node_put(neigh_node);
  867. return -ENOMEM;
  868. }
  869. offset = dest - skb->data;
  870. next_dest = nexthop_skb->data + offset;
  871. batadv_mcast_forw_scrub_dests(bat_priv, neigh_node, dest,
  872. next_dest, num_dests);
  873. batadv_mcast_forw_shrink_tracker(nexthop_skb);
  874. batadv_inc_counter(bat_priv, BATADV_CNT_MCAST_TX);
  875. batadv_add_counter(bat_priv, BATADV_CNT_MCAST_TX_BYTES,
  876. nexthop_skb->len + ETH_HLEN);
  877. xmitted = true;
  878. ret = batadv_send_unicast_skb(nexthop_skb, neigh_node);
  879. batadv_neigh_node_put(neigh_node);
  880. if (ret < 0)
  881. return ret;
  882. }
  883. if (xmitted) {
  884. if (local_xmit) {
  885. batadv_inc_counter(bat_priv, BATADV_CNT_MCAST_TX_LOCAL);
  886. batadv_add_counter(bat_priv,
  887. BATADV_CNT_MCAST_TX_LOCAL_BYTES,
  888. skb->len -
  889. skb_transport_offset(skb));
  890. } else {
  891. batadv_inc_counter(bat_priv, BATADV_CNT_MCAST_FWD);
  892. batadv_add_counter(bat_priv, BATADV_CNT_MCAST_FWD_BYTES,
  893. skb->len + ETH_HLEN);
  894. }
  895. }
  896. if (local_recv)
  897. return NET_RX_SUCCESS;
  898. else
  899. return NET_RX_DROP;
  900. }
  901. /**
  902. * batadv_mcast_forw_tracker_tvlv_handler() - handle an mcast tracker tvlv
  903. * @bat_priv: the bat priv with all the soft interface information
  904. * @skb: the received batman-adv multicast packet
  905. *
  906. * Parses the tracker TVLV of an incoming batman-adv multicast packet and
  907. * forwards the packet as indicated in this TVLV.
  908. *
  909. * Caller needs to set the skb network header to the start of the multicast
  910. * tracker TVLV (excluding the generic TVLV header) and the skb transport header
  911. * to the next byte after this multicast tracker TVLV.
  912. *
  913. * Caller needs to free the skb.
  914. *
  915. * Return: NET_RX_SUCCESS or NET_RX_DROP on success or a negative error
  916. * code on failure. NET_RX_SUCCESS if the received packet is supposed to be
  917. * decapsulated and forwarded to the own soft interface, NET_RX_DROP otherwise.
  918. */
  919. int batadv_mcast_forw_tracker_tvlv_handler(struct batadv_priv *bat_priv,
  920. struct sk_buff *skb)
  921. {
  922. return batadv_mcast_forw_packet(bat_priv, skb, false);
  923. }
  924. /**
  925. * batadv_mcast_forw_packet_hdrlen() - multicast packet header length
  926. * @num_dests: number of destination nodes
  927. *
  928. * Calculates the total batman-adv multicast packet header length for a given
  929. * number of destination nodes (excluding the outer ethernet frame).
  930. *
  931. * Return: The calculated total batman-adv multicast packet header length.
  932. */
  933. unsigned int batadv_mcast_forw_packet_hdrlen(unsigned int num_dests)
  934. {
  935. /**
  936. * If the number of destination entries is even then we need to add
  937. * two byte padding to the tracker TVLV.
  938. */
  939. int padding = (!(num_dests % 2)) ? 2 : 0;
  940. return padding + num_dests * ETH_ALEN +
  941. sizeof(struct batadv_tvlv_mcast_tracker) +
  942. sizeof(struct batadv_tvlv_hdr) +
  943. sizeof(struct batadv_mcast_packet);
  944. }
  945. /**
  946. * batadv_mcast_forw_expand_head() - expand headroom for an mcast packet
  947. * @bat_priv: the bat priv with all the soft interface information
  948. * @skb: the multicast packet to send
  949. *
  950. * Tries to expand an skb's headroom so that its head to tail is 1298
  951. * bytes (minimum IPv6 MTU + vlan ethernet header size) large.
  952. *
  953. * Return: -EINVAL if the given skb's length is too large or -ENOMEM on memory
  954. * allocation failure. Otherwise, on success, zero is returned.
  955. */
  956. static int batadv_mcast_forw_expand_head(struct batadv_priv *bat_priv,
  957. struct sk_buff *skb)
  958. {
  959. int hdr_size = VLAN_ETH_HLEN + IPV6_MIN_MTU - skb->len;
  960. /* TODO: Could be tightened to actual number of destination nodes?
  961. * But it's tricky, number of destinations might have increased since
  962. * we last checked.
  963. */
  964. if (hdr_size < 0) {
  965. /* batadv_mcast_forw_mode_check_count() should ensure we do not
  966. * end up here
  967. */
  968. WARN_ON(1);
  969. return -EINVAL;
  970. }
  971. if (skb_headroom(skb) < hdr_size &&
  972. pskb_expand_head(skb, hdr_size, 0, GFP_ATOMIC) < 0)
  973. return -ENOMEM;
  974. return 0;
  975. }
  976. /**
  977. * batadv_mcast_forw_push() - encapsulate skb in a batman-adv multicast packet
  978. * @bat_priv: the bat priv with all the soft interface information
  979. * @skb: the multicast packet to encapsulate and send
  980. * @vid: the vlan identifier
  981. * @is_routable: indicates whether the destination is routable
  982. * @count: the number of originators the multicast packet needs to be sent to
  983. *
  984. * Encapsulates the given multicast packet in a batman-adv multicast packet.
  985. * A multicast tracker TVLV with destination originator addresses for any node
  986. * that signaled interest in it, that is either via the translation table or the
  987. * according want-all flags, is attached accordingly.
  988. *
  989. * Return: true on success, false otherwise.
  990. */
  991. bool batadv_mcast_forw_push(struct batadv_priv *bat_priv, struct sk_buff *skb,
  992. unsigned short vid, int is_routable, int count)
  993. {
  994. unsigned short tvlv_len = 0;
  995. int ret;
  996. if (batadv_mcast_forw_expand_head(bat_priv, skb) < 0)
  997. goto err;
  998. skb_reset_transport_header(skb);
  999. ret = batadv_mcast_forw_push_tvlvs(bat_priv, skb, vid, is_routable,
  1000. count, &tvlv_len);
  1001. if (ret < 0)
  1002. goto err;
  1003. ret = batadv_mcast_forw_push_hdr(skb, tvlv_len);
  1004. if (ret < 0)
  1005. goto err;
  1006. return true;
  1007. err:
  1008. if (tvlv_len)
  1009. skb_pull(skb, tvlv_len);
  1010. return false;
  1011. }
  1012. /**
  1013. * batadv_mcast_forw_mcsend() - send a self prepared batman-adv multicast packet
  1014. * @bat_priv: the bat priv with all the soft interface information
  1015. * @skb: the multicast packet to encapsulate and send
  1016. *
  1017. * Transmits a batman-adv multicast packet that was locally prepared and
  1018. * consumes/frees it.
  1019. *
  1020. * Return: NET_XMIT_DROP on memory allocation failure. NET_XMIT_SUCCESS
  1021. * otherwise.
  1022. */
  1023. int batadv_mcast_forw_mcsend(struct batadv_priv *bat_priv,
  1024. struct sk_buff *skb)
  1025. {
  1026. int ret = batadv_mcast_forw_packet(bat_priv, skb, true);
  1027. if (ret < 0) {
  1028. kfree_skb(skb);
  1029. return NET_XMIT_DROP;
  1030. }
  1031. consume_skb(skb);
  1032. return NET_XMIT_SUCCESS;
  1033. }