bridge_loop_avoidance.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) B.A.T.M.A.N. contributors:
  3. *
  4. * Simon Wunderlich
  5. */
  6. #include "bridge_loop_avoidance.h"
  7. #include "main.h"
  8. #include <linux/atomic.h>
  9. #include <linux/byteorder/generic.h>
  10. #include <linux/compiler.h>
  11. #include <linux/container_of.h>
  12. #include <linux/crc16.h>
  13. #include <linux/err.h>
  14. #include <linux/errno.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/gfp.h>
  17. #include <linux/if_arp.h>
  18. #include <linux/if_ether.h>
  19. #include <linux/if_vlan.h>
  20. #include <linux/jhash.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/kref.h>
  23. #include <linux/list.h>
  24. #include <linux/lockdep.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/netlink.h>
  27. #include <linux/rculist.h>
  28. #include <linux/rcupdate.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/slab.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/sprintf.h>
  33. #include <linux/stddef.h>
  34. #include <linux/string.h>
  35. #include <linux/string_choices.h>
  36. #include <linux/workqueue.h>
  37. #include <net/arp.h>
  38. #include <net/genetlink.h>
  39. #include <net/netlink.h>
  40. #include <uapi/linux/batadv_packet.h>
  41. #include <uapi/linux/batman_adv.h>
  42. #include "hard-interface.h"
  43. #include "hash.h"
  44. #include "log.h"
  45. #include "netlink.h"
  46. #include "originator.h"
  47. #include "translation-table.h"
  48. static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
  49. static void batadv_bla_periodic_work(struct work_struct *work);
  50. static void
  51. batadv_bla_send_announce(struct batadv_priv *bat_priv,
  52. struct batadv_bla_backbone_gw *backbone_gw);
  53. /**
  54. * batadv_choose_claim() - choose the right bucket for a claim.
  55. * @data: data to hash
  56. * @size: size of the hash table
  57. *
  58. * Return: the hash index of the claim
  59. */
  60. static inline u32 batadv_choose_claim(const void *data, u32 size)
  61. {
  62. const struct batadv_bla_claim *claim = data;
  63. u32 hash = 0;
  64. hash = jhash(&claim->addr, sizeof(claim->addr), hash);
  65. hash = jhash(&claim->vid, sizeof(claim->vid), hash);
  66. return hash % size;
  67. }
  68. /**
  69. * batadv_choose_backbone_gw() - choose the right bucket for a backbone gateway.
  70. * @data: data to hash
  71. * @size: size of the hash table
  72. *
  73. * Return: the hash index of the backbone gateway
  74. */
  75. static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)
  76. {
  77. const struct batadv_bla_backbone_gw *gw;
  78. u32 hash = 0;
  79. gw = data;
  80. hash = jhash(&gw->orig, sizeof(gw->orig), hash);
  81. hash = jhash(&gw->vid, sizeof(gw->vid), hash);
  82. return hash % size;
  83. }
  84. /**
  85. * batadv_compare_backbone_gw() - compare address and vid of two backbone gws
  86. * @node: list node of the first entry to compare
  87. * @data2: pointer to the second backbone gateway
  88. *
  89. * Return: true if the backbones have the same data, false otherwise
  90. */
  91. static bool batadv_compare_backbone_gw(const struct hlist_node *node,
  92. const void *data2)
  93. {
  94. const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
  95. hash_entry);
  96. const struct batadv_bla_backbone_gw *gw1 = data1;
  97. const struct batadv_bla_backbone_gw *gw2 = data2;
  98. if (!batadv_compare_eth(gw1->orig, gw2->orig))
  99. return false;
  100. if (gw1->vid != gw2->vid)
  101. return false;
  102. return true;
  103. }
  104. /**
  105. * batadv_compare_claim() - compare address and vid of two claims
  106. * @node: list node of the first entry to compare
  107. * @data2: pointer to the second claims
  108. *
  109. * Return: true if the claim have the same data, 0 otherwise
  110. */
  111. static bool batadv_compare_claim(const struct hlist_node *node,
  112. const void *data2)
  113. {
  114. const void *data1 = container_of(node, struct batadv_bla_claim,
  115. hash_entry);
  116. const struct batadv_bla_claim *cl1 = data1;
  117. const struct batadv_bla_claim *cl2 = data2;
  118. if (!batadv_compare_eth(cl1->addr, cl2->addr))
  119. return false;
  120. if (cl1->vid != cl2->vid)
  121. return false;
  122. return true;
  123. }
  124. /**
  125. * batadv_backbone_gw_release() - release backbone gw from lists and queue for
  126. * free after rcu grace period
  127. * @ref: kref pointer of the backbone gw
  128. */
  129. static void batadv_backbone_gw_release(struct kref *ref)
  130. {
  131. struct batadv_bla_backbone_gw *backbone_gw;
  132. backbone_gw = container_of(ref, struct batadv_bla_backbone_gw,
  133. refcount);
  134. kfree_rcu(backbone_gw, rcu);
  135. }
  136. /**
  137. * batadv_backbone_gw_put() - decrement the backbone gw refcounter and possibly
  138. * release it
  139. * @backbone_gw: backbone gateway to be free'd
  140. */
  141. static void batadv_backbone_gw_put(struct batadv_bla_backbone_gw *backbone_gw)
  142. {
  143. if (!backbone_gw)
  144. return;
  145. kref_put(&backbone_gw->refcount, batadv_backbone_gw_release);
  146. }
  147. /**
  148. * batadv_claim_release() - release claim from lists and queue for free after
  149. * rcu grace period
  150. * @ref: kref pointer of the claim
  151. */
  152. static void batadv_claim_release(struct kref *ref)
  153. {
  154. struct batadv_bla_claim *claim;
  155. struct batadv_bla_backbone_gw *old_backbone_gw;
  156. claim = container_of(ref, struct batadv_bla_claim, refcount);
  157. spin_lock_bh(&claim->backbone_lock);
  158. old_backbone_gw = claim->backbone_gw;
  159. claim->backbone_gw = NULL;
  160. spin_unlock_bh(&claim->backbone_lock);
  161. spin_lock_bh(&old_backbone_gw->crc_lock);
  162. old_backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
  163. spin_unlock_bh(&old_backbone_gw->crc_lock);
  164. batadv_backbone_gw_put(old_backbone_gw);
  165. kfree_rcu(claim, rcu);
  166. }
  167. /**
  168. * batadv_claim_put() - decrement the claim refcounter and possibly release it
  169. * @claim: claim to be free'd
  170. */
  171. static void batadv_claim_put(struct batadv_bla_claim *claim)
  172. {
  173. if (!claim)
  174. return;
  175. kref_put(&claim->refcount, batadv_claim_release);
  176. }
  177. /**
  178. * batadv_claim_hash_find() - looks for a claim in the claim hash
  179. * @bat_priv: the bat priv with all the mesh interface information
  180. * @data: search data (may be local/static data)
  181. *
  182. * Return: claim if found or NULL otherwise.
  183. */
  184. static struct batadv_bla_claim *
  185. batadv_claim_hash_find(struct batadv_priv *bat_priv,
  186. struct batadv_bla_claim *data)
  187. {
  188. struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
  189. struct hlist_head *head;
  190. struct batadv_bla_claim *claim;
  191. struct batadv_bla_claim *claim_tmp = NULL;
  192. int index;
  193. if (!hash)
  194. return NULL;
  195. index = batadv_choose_claim(data, hash->size);
  196. head = &hash->table[index];
  197. rcu_read_lock();
  198. hlist_for_each_entry_rcu(claim, head, hash_entry) {
  199. if (!batadv_compare_claim(&claim->hash_entry, data))
  200. continue;
  201. if (!kref_get_unless_zero(&claim->refcount))
  202. continue;
  203. claim_tmp = claim;
  204. break;
  205. }
  206. rcu_read_unlock();
  207. return claim_tmp;
  208. }
  209. /**
  210. * batadv_backbone_hash_find() - looks for a backbone gateway in the hash
  211. * @bat_priv: the bat priv with all the mesh interface information
  212. * @addr: the address of the originator
  213. * @vid: the VLAN ID
  214. *
  215. * Return: backbone gateway if found or NULL otherwise
  216. */
  217. static struct batadv_bla_backbone_gw *
  218. batadv_backbone_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
  219. unsigned short vid)
  220. {
  221. struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
  222. struct hlist_head *head;
  223. struct batadv_bla_backbone_gw search_entry, *backbone_gw;
  224. struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
  225. int index;
  226. if (!hash)
  227. return NULL;
  228. ether_addr_copy(search_entry.orig, addr);
  229. search_entry.vid = vid;
  230. index = batadv_choose_backbone_gw(&search_entry, hash->size);
  231. head = &hash->table[index];
  232. rcu_read_lock();
  233. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  234. if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
  235. &search_entry))
  236. continue;
  237. if (!kref_get_unless_zero(&backbone_gw->refcount))
  238. continue;
  239. backbone_gw_tmp = backbone_gw;
  240. break;
  241. }
  242. rcu_read_unlock();
  243. return backbone_gw_tmp;
  244. }
  245. /**
  246. * batadv_bla_del_backbone_claims() - delete all claims for a backbone
  247. * @backbone_gw: backbone gateway where the claims should be removed
  248. */
  249. static void
  250. batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
  251. {
  252. struct batadv_hashtable *hash;
  253. struct hlist_node *node_tmp;
  254. struct hlist_head *head;
  255. struct batadv_bla_claim *claim;
  256. int i;
  257. spinlock_t *list_lock; /* protects write access to the hash lists */
  258. hash = backbone_gw->bat_priv->bla.claim_hash;
  259. if (!hash)
  260. return;
  261. for (i = 0; i < hash->size; i++) {
  262. head = &hash->table[i];
  263. list_lock = &hash->list_locks[i];
  264. spin_lock_bh(list_lock);
  265. hlist_for_each_entry_safe(claim, node_tmp,
  266. head, hash_entry) {
  267. if (claim->backbone_gw != backbone_gw)
  268. continue;
  269. batadv_claim_put(claim);
  270. hlist_del_rcu(&claim->hash_entry);
  271. }
  272. spin_unlock_bh(list_lock);
  273. }
  274. /* all claims gone, initialize CRC */
  275. spin_lock_bh(&backbone_gw->crc_lock);
  276. backbone_gw->crc = BATADV_BLA_CRC_INIT;
  277. spin_unlock_bh(&backbone_gw->crc_lock);
  278. }
  279. /**
  280. * batadv_bla_send_claim() - sends a claim frame according to the provided info
  281. * @bat_priv: the bat priv with all the mesh interface information
  282. * @mac: the mac address to be announced within the claim
  283. * @vid: the VLAN ID
  284. * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
  285. */
  286. static void batadv_bla_send_claim(struct batadv_priv *bat_priv, const u8 *mac,
  287. unsigned short vid, int claimtype)
  288. {
  289. struct sk_buff *skb;
  290. struct ethhdr *ethhdr;
  291. struct batadv_hard_iface *primary_if;
  292. struct net_device *mesh_iface;
  293. u8 *hw_src;
  294. struct batadv_bla_claim_dst local_claim_dest;
  295. __be32 zeroip = 0;
  296. primary_if = batadv_primary_if_get_selected(bat_priv);
  297. if (!primary_if)
  298. return;
  299. memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
  300. sizeof(local_claim_dest));
  301. local_claim_dest.type = claimtype;
  302. mesh_iface = primary_if->mesh_iface;
  303. skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
  304. /* IP DST: 0.0.0.0 */
  305. zeroip,
  306. primary_if->mesh_iface,
  307. /* IP SRC: 0.0.0.0 */
  308. zeroip,
  309. /* Ethernet DST: Broadcast */
  310. NULL,
  311. /* Ethernet SRC/HW SRC: originator mac */
  312. primary_if->net_dev->dev_addr,
  313. /* HW DST: FF:43:05:XX:YY:YY
  314. * with XX = claim type
  315. * and YY:YY = group id
  316. */
  317. (u8 *)&local_claim_dest);
  318. if (!skb)
  319. goto out;
  320. ethhdr = (struct ethhdr *)skb->data;
  321. hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
  322. /* now we pretend that the client would have sent this ... */
  323. switch (claimtype) {
  324. case BATADV_CLAIM_TYPE_CLAIM:
  325. /* normal claim frame
  326. * set Ethernet SRC to the clients mac
  327. */
  328. ether_addr_copy(ethhdr->h_source, mac);
  329. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  330. "%s(): CLAIM %pM on vid %d\n", __func__, mac,
  331. batadv_print_vid(vid));
  332. break;
  333. case BATADV_CLAIM_TYPE_UNCLAIM:
  334. /* unclaim frame
  335. * set HW SRC to the clients mac
  336. */
  337. ether_addr_copy(hw_src, mac);
  338. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  339. "%s(): UNCLAIM %pM on vid %d\n", __func__, mac,
  340. batadv_print_vid(vid));
  341. break;
  342. case BATADV_CLAIM_TYPE_ANNOUNCE:
  343. /* announcement frame
  344. * set HW SRC to the special mac containing the crc
  345. */
  346. ether_addr_copy(hw_src, mac);
  347. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  348. "%s(): ANNOUNCE of %pM on vid %d\n", __func__,
  349. ethhdr->h_source, batadv_print_vid(vid));
  350. break;
  351. case BATADV_CLAIM_TYPE_REQUEST:
  352. /* request frame
  353. * set HW SRC and header destination to the receiving backbone
  354. * gws mac
  355. */
  356. ether_addr_copy(hw_src, mac);
  357. ether_addr_copy(ethhdr->h_dest, mac);
  358. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  359. "%s(): REQUEST of %pM to %pM on vid %d\n", __func__,
  360. ethhdr->h_source, ethhdr->h_dest,
  361. batadv_print_vid(vid));
  362. break;
  363. case BATADV_CLAIM_TYPE_LOOPDETECT:
  364. ether_addr_copy(ethhdr->h_source, mac);
  365. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  366. "%s(): LOOPDETECT of %pM to %pM on vid %d\n",
  367. __func__, ethhdr->h_source, ethhdr->h_dest,
  368. batadv_print_vid(vid));
  369. break;
  370. }
  371. if (vid & BATADV_VLAN_HAS_TAG) {
  372. skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
  373. vid & VLAN_VID_MASK);
  374. if (!skb)
  375. goto out;
  376. }
  377. skb_reset_mac_header(skb);
  378. skb->protocol = eth_type_trans(skb, mesh_iface);
  379. batadv_inc_counter(bat_priv, BATADV_CNT_RX);
  380. batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
  381. skb->len + ETH_HLEN);
  382. netif_rx(skb);
  383. out:
  384. batadv_hardif_put(primary_if);
  385. }
  386. /**
  387. * batadv_bla_loopdetect_report() - worker for reporting the loop
  388. * @work: work queue item
  389. *
  390. * Throws an uevent, as the loopdetect check function can't do that itself
  391. * since the kernel may sleep while throwing uevents.
  392. */
  393. static void batadv_bla_loopdetect_report(struct work_struct *work)
  394. {
  395. struct batadv_bla_backbone_gw *backbone_gw;
  396. struct batadv_priv *bat_priv;
  397. char vid_str[6] = { '\0' };
  398. backbone_gw = container_of(work, struct batadv_bla_backbone_gw,
  399. report_work);
  400. bat_priv = backbone_gw->bat_priv;
  401. batadv_info(bat_priv->mesh_iface,
  402. "Possible loop on VLAN %d detected which can't be handled by BLA - please check your network setup!\n",
  403. batadv_print_vid(backbone_gw->vid));
  404. snprintf(vid_str, sizeof(vid_str), "%d",
  405. batadv_print_vid(backbone_gw->vid));
  406. vid_str[sizeof(vid_str) - 1] = 0;
  407. batadv_throw_uevent(bat_priv, BATADV_UEV_BLA, BATADV_UEV_LOOPDETECT,
  408. vid_str);
  409. batadv_backbone_gw_put(backbone_gw);
  410. }
  411. /**
  412. * batadv_bla_get_backbone_gw() - finds or creates a backbone gateway
  413. * @bat_priv: the bat priv with all the mesh interface information
  414. * @orig: the mac address of the originator
  415. * @vid: the VLAN ID
  416. * @own_backbone: set if the requested backbone is local
  417. *
  418. * Return: the (possibly created) backbone gateway or NULL on error
  419. */
  420. static struct batadv_bla_backbone_gw *
  421. batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, const u8 *orig,
  422. unsigned short vid, bool own_backbone)
  423. {
  424. struct batadv_bla_backbone_gw *entry;
  425. struct batadv_orig_node *orig_node;
  426. int hash_added;
  427. entry = batadv_backbone_hash_find(bat_priv, orig, vid);
  428. if (entry)
  429. return entry;
  430. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  431. "%s(): not found (%pM, %d), creating new entry\n", __func__,
  432. orig, batadv_print_vid(vid));
  433. entry = kzalloc_obj(*entry, GFP_ATOMIC);
  434. if (!entry)
  435. return NULL;
  436. entry->vid = vid;
  437. entry->lasttime = jiffies;
  438. entry->crc = BATADV_BLA_CRC_INIT;
  439. entry->bat_priv = bat_priv;
  440. spin_lock_init(&entry->crc_lock);
  441. atomic_set(&entry->request_sent, 0);
  442. atomic_set(&entry->wait_periods, 0);
  443. ether_addr_copy(entry->orig, orig);
  444. INIT_WORK(&entry->report_work, batadv_bla_loopdetect_report);
  445. kref_init(&entry->refcount);
  446. kref_get(&entry->refcount);
  447. hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
  448. batadv_compare_backbone_gw,
  449. batadv_choose_backbone_gw, entry,
  450. &entry->hash_entry);
  451. if (unlikely(hash_added != 0)) {
  452. /* hash failed, free the structure */
  453. kfree(entry);
  454. return NULL;
  455. }
  456. /* this is a gateway now, remove any TT entry on this VLAN */
  457. orig_node = batadv_orig_hash_find(bat_priv, orig);
  458. if (orig_node) {
  459. batadv_tt_global_del_orig(bat_priv, orig_node, vid,
  460. "became a backbone gateway");
  461. batadv_orig_node_put(orig_node);
  462. }
  463. if (own_backbone) {
  464. batadv_bla_send_announce(bat_priv, entry);
  465. /* this will be decreased in the worker thread */
  466. atomic_inc(&entry->request_sent);
  467. atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
  468. atomic_inc(&bat_priv->bla.num_requests);
  469. }
  470. return entry;
  471. }
  472. /**
  473. * batadv_bla_update_own_backbone_gw() - updates the own backbone gw for a VLAN
  474. * @bat_priv: the bat priv with all the mesh interface information
  475. * @primary_if: the selected primary interface
  476. * @vid: VLAN identifier
  477. *
  478. * update or add the own backbone gw to make sure we announce
  479. * where we receive other backbone gws
  480. */
  481. static void
  482. batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
  483. struct batadv_hard_iface *primary_if,
  484. unsigned short vid)
  485. {
  486. struct batadv_bla_backbone_gw *backbone_gw;
  487. backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
  488. primary_if->net_dev->dev_addr,
  489. vid, true);
  490. if (unlikely(!backbone_gw))
  491. return;
  492. backbone_gw->lasttime = jiffies;
  493. batadv_backbone_gw_put(backbone_gw);
  494. }
  495. /**
  496. * batadv_bla_answer_request() - answer a bla request by sending own claims
  497. * @bat_priv: the bat priv with all the mesh interface information
  498. * @primary_if: interface where the request came on
  499. * @vid: the vid where the request came on
  500. *
  501. * Repeat all of our own claims, and finally send an ANNOUNCE frame
  502. * to allow the requester another check if the CRC is correct now.
  503. */
  504. static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
  505. struct batadv_hard_iface *primary_if,
  506. unsigned short vid)
  507. {
  508. struct hlist_head *head;
  509. struct batadv_hashtable *hash;
  510. struct batadv_bla_claim *claim;
  511. struct batadv_bla_backbone_gw *backbone_gw;
  512. int i;
  513. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  514. "%s(): received a claim request, send all of our own claims again\n",
  515. __func__);
  516. backbone_gw = batadv_backbone_hash_find(bat_priv,
  517. primary_if->net_dev->dev_addr,
  518. vid);
  519. if (!backbone_gw)
  520. return;
  521. hash = bat_priv->bla.claim_hash;
  522. for (i = 0; i < hash->size; i++) {
  523. head = &hash->table[i];
  524. rcu_read_lock();
  525. hlist_for_each_entry_rcu(claim, head, hash_entry) {
  526. /* only own claims are interesting */
  527. if (claim->backbone_gw != backbone_gw)
  528. continue;
  529. batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
  530. BATADV_CLAIM_TYPE_CLAIM);
  531. }
  532. rcu_read_unlock();
  533. }
  534. /* finally, send an announcement frame */
  535. batadv_bla_send_announce(bat_priv, backbone_gw);
  536. batadv_backbone_gw_put(backbone_gw);
  537. }
  538. /**
  539. * batadv_bla_send_request() - send a request to repeat claims
  540. * @backbone_gw: the backbone gateway from whom we are out of sync
  541. *
  542. * When the crc is wrong, ask the backbone gateway for a full table update.
  543. * After the request, it will repeat all of his own claims and finally
  544. * send an announcement claim with which we can check again.
  545. */
  546. static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
  547. {
  548. /* first, remove all old entries */
  549. batadv_bla_del_backbone_claims(backbone_gw);
  550. batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
  551. "Sending REQUEST to %pM\n", backbone_gw->orig);
  552. /* send request */
  553. batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
  554. backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
  555. /* no local broadcasts should be sent or received, for now. */
  556. if (!atomic_read(&backbone_gw->request_sent)) {
  557. atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
  558. atomic_set(&backbone_gw->request_sent, 1);
  559. }
  560. }
  561. /**
  562. * batadv_bla_send_announce() - Send an announcement frame
  563. * @bat_priv: the bat priv with all the mesh interface information
  564. * @backbone_gw: our backbone gateway which should be announced
  565. */
  566. static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
  567. struct batadv_bla_backbone_gw *backbone_gw)
  568. {
  569. u8 mac[ETH_ALEN];
  570. __be16 crc;
  571. memcpy(mac, batadv_announce_mac, 4);
  572. spin_lock_bh(&backbone_gw->crc_lock);
  573. crc = htons(backbone_gw->crc);
  574. spin_unlock_bh(&backbone_gw->crc_lock);
  575. memcpy(&mac[4], &crc, 2);
  576. batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
  577. BATADV_CLAIM_TYPE_ANNOUNCE);
  578. }
  579. /**
  580. * batadv_bla_add_claim() - Adds a claim in the claim hash
  581. * @bat_priv: the bat priv with all the mesh interface information
  582. * @mac: the mac address of the claim
  583. * @vid: the VLAN ID of the frame
  584. * @backbone_gw: the backbone gateway which claims it
  585. */
  586. static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
  587. const u8 *mac, const unsigned short vid,
  588. struct batadv_bla_backbone_gw *backbone_gw)
  589. {
  590. struct batadv_bla_backbone_gw *old_backbone_gw;
  591. struct batadv_bla_claim *claim;
  592. struct batadv_bla_claim search_claim;
  593. bool remove_crc = false;
  594. int hash_added;
  595. ether_addr_copy(search_claim.addr, mac);
  596. search_claim.vid = vid;
  597. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  598. /* create a new claim entry if it does not exist yet. */
  599. if (!claim) {
  600. claim = kzalloc_obj(*claim, GFP_ATOMIC);
  601. if (!claim)
  602. return;
  603. ether_addr_copy(claim->addr, mac);
  604. spin_lock_init(&claim->backbone_lock);
  605. claim->vid = vid;
  606. claim->lasttime = jiffies;
  607. kref_get(&backbone_gw->refcount);
  608. claim->backbone_gw = backbone_gw;
  609. kref_init(&claim->refcount);
  610. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  611. "%s(): adding new entry %pM, vid %d to hash ...\n",
  612. __func__, mac, batadv_print_vid(vid));
  613. kref_get(&claim->refcount);
  614. hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
  615. batadv_compare_claim,
  616. batadv_choose_claim, claim,
  617. &claim->hash_entry);
  618. if (unlikely(hash_added != 0)) {
  619. /* only local changes happened. */
  620. kfree(claim);
  621. return;
  622. }
  623. } else {
  624. claim->lasttime = jiffies;
  625. if (claim->backbone_gw == backbone_gw)
  626. /* no need to register a new backbone */
  627. goto claim_free_ref;
  628. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  629. "%s(): changing ownership for %pM, vid %d to gw %pM\n",
  630. __func__, mac, batadv_print_vid(vid),
  631. backbone_gw->orig);
  632. remove_crc = true;
  633. }
  634. /* replace backbone_gw atomically and adjust reference counters */
  635. spin_lock_bh(&claim->backbone_lock);
  636. old_backbone_gw = claim->backbone_gw;
  637. kref_get(&backbone_gw->refcount);
  638. claim->backbone_gw = backbone_gw;
  639. spin_unlock_bh(&claim->backbone_lock);
  640. if (remove_crc) {
  641. /* remove claim address from old backbone_gw */
  642. spin_lock_bh(&old_backbone_gw->crc_lock);
  643. old_backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
  644. spin_unlock_bh(&old_backbone_gw->crc_lock);
  645. }
  646. batadv_backbone_gw_put(old_backbone_gw);
  647. /* add claim address to new backbone_gw */
  648. spin_lock_bh(&backbone_gw->crc_lock);
  649. backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
  650. spin_unlock_bh(&backbone_gw->crc_lock);
  651. backbone_gw->lasttime = jiffies;
  652. claim_free_ref:
  653. batadv_claim_put(claim);
  654. }
  655. /**
  656. * batadv_bla_claim_get_backbone_gw() - Get valid reference for backbone_gw of
  657. * claim
  658. * @claim: claim whose backbone_gw should be returned
  659. *
  660. * Return: valid reference to claim::backbone_gw
  661. */
  662. static struct batadv_bla_backbone_gw *
  663. batadv_bla_claim_get_backbone_gw(struct batadv_bla_claim *claim)
  664. {
  665. struct batadv_bla_backbone_gw *backbone_gw;
  666. spin_lock_bh(&claim->backbone_lock);
  667. backbone_gw = claim->backbone_gw;
  668. kref_get(&backbone_gw->refcount);
  669. spin_unlock_bh(&claim->backbone_lock);
  670. return backbone_gw;
  671. }
  672. /**
  673. * batadv_bla_del_claim() - delete a claim from the claim hash
  674. * @bat_priv: the bat priv with all the mesh interface information
  675. * @mac: mac address of the claim to be removed
  676. * @vid: VLAN id for the claim to be removed
  677. */
  678. static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
  679. const u8 *mac, const unsigned short vid)
  680. {
  681. struct batadv_bla_claim search_claim, *claim;
  682. struct batadv_bla_claim *claim_removed_entry;
  683. struct hlist_node *claim_removed_node;
  684. ether_addr_copy(search_claim.addr, mac);
  685. search_claim.vid = vid;
  686. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  687. if (!claim)
  688. return;
  689. batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): %pM, vid %d\n", __func__,
  690. mac, batadv_print_vid(vid));
  691. claim_removed_node = batadv_hash_remove(bat_priv->bla.claim_hash,
  692. batadv_compare_claim,
  693. batadv_choose_claim, claim);
  694. if (!claim_removed_node)
  695. goto free_claim;
  696. /* reference from the hash is gone */
  697. claim_removed_entry = hlist_entry(claim_removed_node,
  698. struct batadv_bla_claim, hash_entry);
  699. batadv_claim_put(claim_removed_entry);
  700. free_claim:
  701. /* don't need the reference from hash_find() anymore */
  702. batadv_claim_put(claim);
  703. }
  704. /**
  705. * batadv_handle_announce() - check for ANNOUNCE frame
  706. * @bat_priv: the bat priv with all the mesh interface information
  707. * @an_addr: announcement mac address (ARP Sender HW address)
  708. * @backbone_addr: originator address of the sender (Ethernet source MAC)
  709. * @vid: the VLAN ID of the frame
  710. *
  711. * Return: true if handled
  712. */
  713. static bool batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
  714. u8 *backbone_addr, unsigned short vid)
  715. {
  716. struct batadv_bla_backbone_gw *backbone_gw;
  717. u16 backbone_crc, crc;
  718. if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
  719. return false;
  720. backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
  721. false);
  722. if (unlikely(!backbone_gw))
  723. return true;
  724. /* handle as ANNOUNCE frame */
  725. backbone_gw->lasttime = jiffies;
  726. crc = ntohs(*((__force __be16 *)(&an_addr[4])));
  727. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  728. "%s(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
  729. __func__, batadv_print_vid(vid), backbone_gw->orig, crc);
  730. spin_lock_bh(&backbone_gw->crc_lock);
  731. backbone_crc = backbone_gw->crc;
  732. spin_unlock_bh(&backbone_gw->crc_lock);
  733. if (backbone_crc != crc) {
  734. batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
  735. "%s(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
  736. __func__, backbone_gw->orig,
  737. batadv_print_vid(backbone_gw->vid),
  738. backbone_crc, crc);
  739. batadv_bla_send_request(backbone_gw);
  740. } else {
  741. /* if we have sent a request and the crc was OK,
  742. * we can allow traffic again.
  743. */
  744. if (atomic_read(&backbone_gw->request_sent)) {
  745. atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
  746. atomic_set(&backbone_gw->request_sent, 0);
  747. }
  748. }
  749. batadv_backbone_gw_put(backbone_gw);
  750. return true;
  751. }
  752. /**
  753. * batadv_handle_request() - check for REQUEST frame
  754. * @bat_priv: the bat priv with all the mesh interface information
  755. * @primary_if: the primary hard interface of this batman mesh interface
  756. * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
  757. * @ethhdr: ethernet header of a packet
  758. * @vid: the VLAN ID of the frame
  759. *
  760. * Return: true if handled
  761. */
  762. static bool batadv_handle_request(struct batadv_priv *bat_priv,
  763. struct batadv_hard_iface *primary_if,
  764. u8 *backbone_addr, struct ethhdr *ethhdr,
  765. unsigned short vid)
  766. {
  767. /* check for REQUEST frame */
  768. if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
  769. return false;
  770. /* sanity check, this should not happen on a normal switch,
  771. * we ignore it in this case.
  772. */
  773. if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
  774. return true;
  775. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  776. "%s(): REQUEST vid %d (sent by %pM)...\n",
  777. __func__, batadv_print_vid(vid), ethhdr->h_source);
  778. batadv_bla_answer_request(bat_priv, primary_if, vid);
  779. return true;
  780. }
  781. /**
  782. * batadv_handle_unclaim() - check for UNCLAIM frame
  783. * @bat_priv: the bat priv with all the mesh interface information
  784. * @primary_if: the primary hard interface of this batman mesh interface
  785. * @backbone_addr: originator address of the backbone (Ethernet source)
  786. * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
  787. * @vid: the VLAN ID of the frame
  788. *
  789. * Return: true if handled
  790. */
  791. static bool batadv_handle_unclaim(struct batadv_priv *bat_priv,
  792. struct batadv_hard_iface *primary_if,
  793. const u8 *backbone_addr, const u8 *claim_addr,
  794. unsigned short vid)
  795. {
  796. struct batadv_bla_backbone_gw *backbone_gw;
  797. /* unclaim in any case if it is our own */
  798. if (primary_if && batadv_compare_eth(backbone_addr,
  799. primary_if->net_dev->dev_addr))
  800. batadv_bla_send_claim(bat_priv, claim_addr, vid,
  801. BATADV_CLAIM_TYPE_UNCLAIM);
  802. backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
  803. if (!backbone_gw)
  804. return true;
  805. /* this must be an UNCLAIM frame */
  806. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  807. "%s(): UNCLAIM %pM on vid %d (sent by %pM)...\n", __func__,
  808. claim_addr, batadv_print_vid(vid), backbone_gw->orig);
  809. batadv_bla_del_claim(bat_priv, claim_addr, vid);
  810. batadv_backbone_gw_put(backbone_gw);
  811. return true;
  812. }
  813. /**
  814. * batadv_handle_claim() - check for CLAIM frame
  815. * @bat_priv: the bat priv with all the mesh interface information
  816. * @primary_if: the primary hard interface of this batman mesh interface
  817. * @backbone_addr: originator address of the backbone (Ethernet Source)
  818. * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
  819. * @vid: the VLAN ID of the frame
  820. *
  821. * Return: true if handled
  822. */
  823. static bool batadv_handle_claim(struct batadv_priv *bat_priv,
  824. struct batadv_hard_iface *primary_if,
  825. const u8 *backbone_addr, const u8 *claim_addr,
  826. unsigned short vid)
  827. {
  828. struct batadv_bla_backbone_gw *backbone_gw;
  829. /* register the gateway if not yet available, and add the claim. */
  830. backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
  831. false);
  832. if (unlikely(!backbone_gw))
  833. return true;
  834. /* this must be a CLAIM frame */
  835. batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
  836. if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
  837. batadv_bla_send_claim(bat_priv, claim_addr, vid,
  838. BATADV_CLAIM_TYPE_CLAIM);
  839. /* TODO: we could call something like tt_local_del() here. */
  840. batadv_backbone_gw_put(backbone_gw);
  841. return true;
  842. }
  843. /**
  844. * batadv_check_claim_group() - check for claim group membership
  845. * @bat_priv: the bat priv with all the mesh interface information
  846. * @primary_if: the primary interface of this batman interface
  847. * @hw_src: the Hardware source in the ARP Header
  848. * @hw_dst: the Hardware destination in the ARP Header
  849. * @ethhdr: pointer to the Ethernet header of the claim frame
  850. *
  851. * checks if it is a claim packet and if it's on the same group.
  852. * This function also applies the group ID of the sender
  853. * if it is in the same mesh.
  854. *
  855. * Return:
  856. * 2 - if it is a claim packet and on the same group
  857. * 1 - if is a claim packet from another group
  858. * 0 - if it is not a claim packet
  859. */
  860. static int batadv_check_claim_group(struct batadv_priv *bat_priv,
  861. struct batadv_hard_iface *primary_if,
  862. u8 *hw_src, u8 *hw_dst,
  863. struct ethhdr *ethhdr)
  864. {
  865. u8 *backbone_addr;
  866. struct batadv_orig_node *orig_node;
  867. struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
  868. bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
  869. bla_dst_own = &bat_priv->bla.claim_dest;
  870. /* if announcement packet, use the source,
  871. * otherwise assume it is in the hw_src
  872. */
  873. switch (bla_dst->type) {
  874. case BATADV_CLAIM_TYPE_CLAIM:
  875. backbone_addr = hw_src;
  876. break;
  877. case BATADV_CLAIM_TYPE_REQUEST:
  878. case BATADV_CLAIM_TYPE_ANNOUNCE:
  879. case BATADV_CLAIM_TYPE_UNCLAIM:
  880. backbone_addr = ethhdr->h_source;
  881. break;
  882. default:
  883. return 0;
  884. }
  885. /* don't accept claim frames from ourselves */
  886. if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
  887. return 0;
  888. /* if its already the same group, it is fine. */
  889. if (bla_dst->group == bla_dst_own->group)
  890. return 2;
  891. /* lets see if this originator is in our mesh */
  892. orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
  893. /* don't accept claims from gateways which are not in
  894. * the same mesh or group.
  895. */
  896. if (!orig_node)
  897. return 1;
  898. /* if our mesh friends mac is bigger, use it for ourselves. */
  899. if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
  900. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  901. "taking other backbones claim group: %#.4x\n",
  902. ntohs(bla_dst->group));
  903. bla_dst_own->group = bla_dst->group;
  904. }
  905. batadv_orig_node_put(orig_node);
  906. return 2;
  907. }
  908. /**
  909. * batadv_bla_process_claim() - Check if this is a claim frame, and process it
  910. * @bat_priv: the bat priv with all the mesh interface information
  911. * @primary_if: the primary hard interface of this batman mesh interface
  912. * @skb: the frame to be checked
  913. *
  914. * Return: true if it was a claim frame, otherwise return false to
  915. * tell the callee that it can use the frame on its own.
  916. */
  917. static bool batadv_bla_process_claim(struct batadv_priv *bat_priv,
  918. struct batadv_hard_iface *primary_if,
  919. struct sk_buff *skb)
  920. {
  921. struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
  922. u8 *hw_src, *hw_dst;
  923. struct vlan_hdr *vhdr, vhdr_buf;
  924. struct ethhdr *ethhdr;
  925. struct arphdr *arphdr;
  926. unsigned short vid;
  927. int vlan_depth = 0;
  928. __be16 proto;
  929. int headlen;
  930. int ret;
  931. vid = batadv_get_vid(skb, 0);
  932. ethhdr = eth_hdr(skb);
  933. proto = ethhdr->h_proto;
  934. headlen = ETH_HLEN;
  935. if (vid & BATADV_VLAN_HAS_TAG) {
  936. /* Traverse the VLAN/Ethertypes.
  937. *
  938. * At this point it is known that the first protocol is a VLAN
  939. * header, so start checking at the encapsulated protocol.
  940. *
  941. * The depth of the VLAN headers is recorded to drop BLA claim
  942. * frames encapsulated into multiple VLAN headers (QinQ).
  943. */
  944. do {
  945. vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
  946. &vhdr_buf);
  947. if (!vhdr)
  948. return false;
  949. proto = vhdr->h_vlan_encapsulated_proto;
  950. headlen += VLAN_HLEN;
  951. vlan_depth++;
  952. } while (proto == htons(ETH_P_8021Q));
  953. }
  954. if (proto != htons(ETH_P_ARP))
  955. return false; /* not a claim frame */
  956. /* this must be a ARP frame. check if it is a claim. */
  957. if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
  958. return false;
  959. /* pskb_may_pull() may have modified the pointers, get ethhdr again */
  960. ethhdr = eth_hdr(skb);
  961. arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
  962. /* Check whether the ARP frame carries a valid
  963. * IP information
  964. */
  965. if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
  966. return false;
  967. if (arphdr->ar_pro != htons(ETH_P_IP))
  968. return false;
  969. if (arphdr->ar_hln != ETH_ALEN)
  970. return false;
  971. if (arphdr->ar_pln != 4)
  972. return false;
  973. hw_src = (u8 *)arphdr + sizeof(struct arphdr);
  974. hw_dst = hw_src + ETH_ALEN + 4;
  975. bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
  976. bla_dst_own = &bat_priv->bla.claim_dest;
  977. /* check if it is a claim frame in general */
  978. if (memcmp(bla_dst->magic, bla_dst_own->magic,
  979. sizeof(bla_dst->magic)) != 0)
  980. return false;
  981. /* check if there is a claim frame encapsulated deeper in (QinQ) and
  982. * drop that, as this is not supported by BLA but should also not be
  983. * sent via the mesh.
  984. */
  985. if (vlan_depth > 1)
  986. return true;
  987. /* Let the loopdetect frames on the mesh in any case. */
  988. if (bla_dst->type == BATADV_CLAIM_TYPE_LOOPDETECT)
  989. return false;
  990. /* check if it is a claim frame. */
  991. ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
  992. ethhdr);
  993. if (ret == 1)
  994. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  995. "%s(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
  996. __func__, ethhdr->h_source, batadv_print_vid(vid),
  997. hw_src, hw_dst);
  998. if (ret < 2)
  999. return !!ret;
  1000. /* become a backbone gw ourselves on this vlan if not happened yet */
  1001. batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
  1002. /* check for the different types of claim frames ... */
  1003. switch (bla_dst->type) {
  1004. case BATADV_CLAIM_TYPE_CLAIM:
  1005. if (batadv_handle_claim(bat_priv, primary_if, hw_src,
  1006. ethhdr->h_source, vid))
  1007. return true;
  1008. break;
  1009. case BATADV_CLAIM_TYPE_UNCLAIM:
  1010. if (batadv_handle_unclaim(bat_priv, primary_if,
  1011. ethhdr->h_source, hw_src, vid))
  1012. return true;
  1013. break;
  1014. case BATADV_CLAIM_TYPE_ANNOUNCE:
  1015. if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
  1016. vid))
  1017. return true;
  1018. break;
  1019. case BATADV_CLAIM_TYPE_REQUEST:
  1020. if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
  1021. vid))
  1022. return true;
  1023. break;
  1024. }
  1025. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  1026. "%s(): ERROR - this looks like a claim frame, but is useless. eth src %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
  1027. __func__, ethhdr->h_source, batadv_print_vid(vid), hw_src,
  1028. hw_dst);
  1029. return true;
  1030. }
  1031. /**
  1032. * batadv_bla_purge_backbone_gw() - Remove backbone gateways after a timeout or
  1033. * immediately
  1034. * @bat_priv: the bat priv with all the mesh interface information
  1035. * @now: whether the whole hash shall be wiped now
  1036. *
  1037. * Check when we last heard from other nodes, and remove them in case of
  1038. * a time out, or clean all backbone gws if now is set.
  1039. */
  1040. static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
  1041. {
  1042. struct batadv_bla_backbone_gw *backbone_gw;
  1043. struct hlist_node *node_tmp;
  1044. struct hlist_head *head;
  1045. struct batadv_hashtable *hash;
  1046. spinlock_t *list_lock; /* protects write access to the hash lists */
  1047. int i;
  1048. hash = bat_priv->bla.backbone_hash;
  1049. if (!hash)
  1050. return;
  1051. for (i = 0; i < hash->size; i++) {
  1052. head = &hash->table[i];
  1053. list_lock = &hash->list_locks[i];
  1054. spin_lock_bh(list_lock);
  1055. hlist_for_each_entry_safe(backbone_gw, node_tmp,
  1056. head, hash_entry) {
  1057. if (now)
  1058. goto purge_now;
  1059. if (!batadv_has_timed_out(backbone_gw->lasttime,
  1060. BATADV_BLA_BACKBONE_TIMEOUT))
  1061. continue;
  1062. batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
  1063. "%s(): backbone gw %pM timed out\n",
  1064. __func__, backbone_gw->orig);
  1065. purge_now:
  1066. /* don't wait for the pending request anymore */
  1067. if (atomic_read(&backbone_gw->request_sent))
  1068. atomic_dec(&bat_priv->bla.num_requests);
  1069. batadv_bla_del_backbone_claims(backbone_gw);
  1070. hlist_del_rcu(&backbone_gw->hash_entry);
  1071. batadv_backbone_gw_put(backbone_gw);
  1072. }
  1073. spin_unlock_bh(list_lock);
  1074. }
  1075. }
  1076. /**
  1077. * batadv_bla_purge_claims() - Remove claims after a timeout or immediately
  1078. * @bat_priv: the bat priv with all the mesh interface information
  1079. * @primary_if: the selected primary interface, may be NULL if now is set
  1080. * @now: whether the whole hash shall be wiped now
  1081. *
  1082. * Check when we heard last time from our own claims, and remove them in case of
  1083. * a time out, or clean all claims if now is set
  1084. */
  1085. static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
  1086. struct batadv_hard_iface *primary_if,
  1087. int now)
  1088. {
  1089. struct batadv_bla_backbone_gw *backbone_gw;
  1090. struct batadv_bla_claim *claim;
  1091. struct hlist_head *head;
  1092. struct batadv_hashtable *hash;
  1093. int i;
  1094. hash = bat_priv->bla.claim_hash;
  1095. if (!hash)
  1096. return;
  1097. for (i = 0; i < hash->size; i++) {
  1098. head = &hash->table[i];
  1099. rcu_read_lock();
  1100. hlist_for_each_entry_rcu(claim, head, hash_entry) {
  1101. backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
  1102. if (now)
  1103. goto purge_now;
  1104. if (!batadv_compare_eth(backbone_gw->orig,
  1105. primary_if->net_dev->dev_addr))
  1106. goto skip;
  1107. if (!batadv_has_timed_out(claim->lasttime,
  1108. BATADV_BLA_CLAIM_TIMEOUT))
  1109. goto skip;
  1110. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  1111. "%s(): timed out.\n", __func__);
  1112. purge_now:
  1113. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  1114. "%s(): %pM, vid %d\n", __func__,
  1115. claim->addr, claim->vid);
  1116. batadv_handle_unclaim(bat_priv, primary_if,
  1117. backbone_gw->orig,
  1118. claim->addr, claim->vid);
  1119. skip:
  1120. batadv_backbone_gw_put(backbone_gw);
  1121. }
  1122. rcu_read_unlock();
  1123. }
  1124. }
  1125. /**
  1126. * batadv_bla_update_orig_address() - Update the backbone gateways when the own
  1127. * originator address changes
  1128. * @bat_priv: the bat priv with all the mesh interface information
  1129. * @primary_if: the new selected primary_if
  1130. * @oldif: the old primary interface, may be NULL
  1131. */
  1132. void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
  1133. struct batadv_hard_iface *primary_if,
  1134. struct batadv_hard_iface *oldif)
  1135. {
  1136. struct batadv_bla_backbone_gw *backbone_gw;
  1137. struct hlist_head *head;
  1138. struct batadv_hashtable *hash;
  1139. __be16 group;
  1140. int i;
  1141. /* reset bridge loop avoidance group id */
  1142. group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
  1143. bat_priv->bla.claim_dest.group = group;
  1144. /* purge everything when bridge loop avoidance is turned off */
  1145. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1146. oldif = NULL;
  1147. if (!oldif) {
  1148. batadv_bla_purge_claims(bat_priv, NULL, 1);
  1149. batadv_bla_purge_backbone_gw(bat_priv, 1);
  1150. return;
  1151. }
  1152. hash = bat_priv->bla.backbone_hash;
  1153. if (!hash)
  1154. return;
  1155. for (i = 0; i < hash->size; i++) {
  1156. head = &hash->table[i];
  1157. rcu_read_lock();
  1158. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  1159. /* own orig still holds the old value. */
  1160. if (!batadv_compare_eth(backbone_gw->orig,
  1161. oldif->net_dev->dev_addr))
  1162. continue;
  1163. ether_addr_copy(backbone_gw->orig,
  1164. primary_if->net_dev->dev_addr);
  1165. /* send an announce frame so others will ask for our
  1166. * claims and update their tables.
  1167. */
  1168. batadv_bla_send_announce(bat_priv, backbone_gw);
  1169. }
  1170. rcu_read_unlock();
  1171. }
  1172. }
  1173. /**
  1174. * batadv_bla_send_loopdetect() - send a loopdetect frame
  1175. * @bat_priv: the bat priv with all the mesh interface information
  1176. * @backbone_gw: the backbone gateway for which a loop should be detected
  1177. *
  1178. * To detect loops that the bridge loop avoidance can't handle, send a loop
  1179. * detection packet on the backbone. Unlike other BLA frames, this frame will
  1180. * be allowed on the mesh by other nodes. If it is received on the mesh, this
  1181. * indicates that there is a loop.
  1182. */
  1183. static void
  1184. batadv_bla_send_loopdetect(struct batadv_priv *bat_priv,
  1185. struct batadv_bla_backbone_gw *backbone_gw)
  1186. {
  1187. batadv_dbg(BATADV_DBG_BLA, bat_priv, "Send loopdetect frame for vid %d\n",
  1188. backbone_gw->vid);
  1189. batadv_bla_send_claim(bat_priv, bat_priv->bla.loopdetect_addr,
  1190. backbone_gw->vid, BATADV_CLAIM_TYPE_LOOPDETECT);
  1191. }
  1192. /**
  1193. * batadv_bla_status_update() - purge bla interfaces if necessary
  1194. * @net_dev: the mesh interface net device
  1195. */
  1196. void batadv_bla_status_update(struct net_device *net_dev)
  1197. {
  1198. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1199. struct batadv_hard_iface *primary_if;
  1200. primary_if = batadv_primary_if_get_selected(bat_priv);
  1201. if (!primary_if)
  1202. return;
  1203. /* this function already purges everything when bla is disabled,
  1204. * so just call that one.
  1205. */
  1206. batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
  1207. batadv_hardif_put(primary_if);
  1208. }
  1209. /**
  1210. * batadv_bla_periodic_work() - performs periodic bla work
  1211. * @work: kernel work struct
  1212. *
  1213. * periodic work to do:
  1214. * * purge structures when they are too old
  1215. * * send announcements
  1216. */
  1217. static void batadv_bla_periodic_work(struct work_struct *work)
  1218. {
  1219. struct delayed_work *delayed_work;
  1220. struct batadv_priv *bat_priv;
  1221. struct batadv_priv_bla *priv_bla;
  1222. struct hlist_head *head;
  1223. struct batadv_bla_backbone_gw *backbone_gw;
  1224. struct batadv_hashtable *hash;
  1225. struct batadv_hard_iface *primary_if;
  1226. bool send_loopdetect = false;
  1227. int i;
  1228. delayed_work = to_delayed_work(work);
  1229. priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
  1230. bat_priv = container_of(priv_bla, struct batadv_priv, bla);
  1231. primary_if = batadv_primary_if_get_selected(bat_priv);
  1232. if (!primary_if)
  1233. goto out;
  1234. batadv_bla_purge_claims(bat_priv, primary_if, 0);
  1235. batadv_bla_purge_backbone_gw(bat_priv, 0);
  1236. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1237. goto out;
  1238. if (atomic_dec_and_test(&bat_priv->bla.loopdetect_next)) {
  1239. /* set a new random mac address for the next bridge loop
  1240. * detection frames. Set the locally administered bit to avoid
  1241. * collisions with users mac addresses.
  1242. */
  1243. eth_random_addr(bat_priv->bla.loopdetect_addr);
  1244. bat_priv->bla.loopdetect_addr[0] = 0xba;
  1245. bat_priv->bla.loopdetect_addr[1] = 0xbe;
  1246. bat_priv->bla.loopdetect_lasttime = jiffies;
  1247. atomic_set(&bat_priv->bla.loopdetect_next,
  1248. BATADV_BLA_LOOPDETECT_PERIODS);
  1249. /* mark for sending loop detect on all VLANs */
  1250. send_loopdetect = true;
  1251. }
  1252. hash = bat_priv->bla.backbone_hash;
  1253. if (!hash)
  1254. goto out;
  1255. for (i = 0; i < hash->size; i++) {
  1256. head = &hash->table[i];
  1257. rcu_read_lock();
  1258. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  1259. if (!batadv_compare_eth(backbone_gw->orig,
  1260. primary_if->net_dev->dev_addr))
  1261. continue;
  1262. backbone_gw->lasttime = jiffies;
  1263. batadv_bla_send_announce(bat_priv, backbone_gw);
  1264. if (send_loopdetect)
  1265. batadv_bla_send_loopdetect(bat_priv,
  1266. backbone_gw);
  1267. /* request_sent is only set after creation to avoid
  1268. * problems when we are not yet known as backbone gw
  1269. * in the backbone.
  1270. *
  1271. * We can reset this now after we waited some periods
  1272. * to give bridge forward delays and bla group forming
  1273. * some grace time.
  1274. */
  1275. if (atomic_read(&backbone_gw->request_sent) == 0)
  1276. continue;
  1277. if (!atomic_dec_and_test(&backbone_gw->wait_periods))
  1278. continue;
  1279. atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
  1280. atomic_set(&backbone_gw->request_sent, 0);
  1281. }
  1282. rcu_read_unlock();
  1283. }
  1284. out:
  1285. batadv_hardif_put(primary_if);
  1286. queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
  1287. msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
  1288. }
  1289. /* The hash for claim and backbone hash receive the same key because they
  1290. * are getting initialized by hash_new with the same key. Reinitializing
  1291. * them with to different keys to allow nested locking without generating
  1292. * lockdep warnings
  1293. */
  1294. static struct lock_class_key batadv_claim_hash_lock_class_key;
  1295. static struct lock_class_key batadv_backbone_hash_lock_class_key;
  1296. /**
  1297. * batadv_bla_init() - initialize all bla structures
  1298. * @bat_priv: the bat priv with all the mesh interface information
  1299. *
  1300. * Return: 0 on success, < 0 on error.
  1301. */
  1302. int batadv_bla_init(struct batadv_priv *bat_priv)
  1303. {
  1304. int i;
  1305. u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
  1306. struct batadv_hard_iface *primary_if;
  1307. u16 crc;
  1308. unsigned long entrytime;
  1309. spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
  1310. batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
  1311. /* setting claim destination address */
  1312. memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
  1313. bat_priv->bla.claim_dest.type = 0;
  1314. primary_if = batadv_primary_if_get_selected(bat_priv);
  1315. if (primary_if) {
  1316. crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
  1317. bat_priv->bla.claim_dest.group = htons(crc);
  1318. batadv_hardif_put(primary_if);
  1319. } else {
  1320. bat_priv->bla.claim_dest.group = 0; /* will be set later */
  1321. }
  1322. /* initialize the duplicate list */
  1323. entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
  1324. for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
  1325. bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
  1326. bat_priv->bla.bcast_duplist_curr = 0;
  1327. atomic_set(&bat_priv->bla.loopdetect_next,
  1328. BATADV_BLA_LOOPDETECT_PERIODS);
  1329. if (bat_priv->bla.claim_hash)
  1330. return 0;
  1331. bat_priv->bla.claim_hash = batadv_hash_new(128);
  1332. if (!bat_priv->bla.claim_hash)
  1333. return -ENOMEM;
  1334. bat_priv->bla.backbone_hash = batadv_hash_new(32);
  1335. if (!bat_priv->bla.backbone_hash) {
  1336. batadv_hash_destroy(bat_priv->bla.claim_hash);
  1337. return -ENOMEM;
  1338. }
  1339. batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
  1340. &batadv_claim_hash_lock_class_key);
  1341. batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
  1342. &batadv_backbone_hash_lock_class_key);
  1343. batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
  1344. INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
  1345. queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
  1346. msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
  1347. return 0;
  1348. }
  1349. /**
  1350. * batadv_bla_check_duplist() - Check if a frame is in the broadcast dup.
  1351. * @bat_priv: the bat priv with all the mesh interface information
  1352. * @skb: contains the multicast packet to be checked
  1353. * @payload_offset: offset in the skb, marking the start of the data to be CRC'ed
  1354. * @orig: originator mac address, NULL if unknown
  1355. *
  1356. * Check if it is on our broadcast list. Another gateway might have sent the
  1357. * same packet because it is connected to the same backbone, so we have to
  1358. * remove this duplicate.
  1359. *
  1360. * This is performed by checking the CRC, which will tell us
  1361. * with a good chance that it is the same packet. If it is furthermore
  1362. * sent by another host, drop it. We allow equal packets from
  1363. * the same host however as this might be intended.
  1364. *
  1365. * Return: true if a packet is in the duplicate list, false otherwise.
  1366. */
  1367. static bool batadv_bla_check_duplist(struct batadv_priv *bat_priv,
  1368. struct sk_buff *skb, int payload_offset,
  1369. const u8 *orig)
  1370. {
  1371. struct batadv_bcast_duplist_entry *entry;
  1372. bool ret = false;
  1373. int payload_len;
  1374. int i, curr;
  1375. u32 crc;
  1376. /* calculate the crc ... */
  1377. payload_len = skb->len - payload_offset;
  1378. crc = skb_crc32c(skb, payload_offset, payload_len, 0);
  1379. spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
  1380. for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
  1381. curr = (bat_priv->bla.bcast_duplist_curr + i);
  1382. curr %= BATADV_DUPLIST_SIZE;
  1383. entry = &bat_priv->bla.bcast_duplist[curr];
  1384. /* we can stop searching if the entry is too old ;
  1385. * later entries will be even older
  1386. */
  1387. if (batadv_has_timed_out(entry->entrytime,
  1388. BATADV_DUPLIST_TIMEOUT))
  1389. break;
  1390. if (entry->crc != crc)
  1391. continue;
  1392. /* are the originators both known and not anonymous? */
  1393. if (orig && !is_zero_ether_addr(orig) &&
  1394. !is_zero_ether_addr(entry->orig)) {
  1395. /* If known, check if the new frame came from
  1396. * the same originator:
  1397. * We are safe to take identical frames from the
  1398. * same orig, if known, as multiplications in
  1399. * the mesh are detected via the (orig, seqno) pair.
  1400. * So we can be a bit more liberal here and allow
  1401. * identical frames from the same orig which the source
  1402. * host might have sent multiple times on purpose.
  1403. */
  1404. if (batadv_compare_eth(entry->orig, orig))
  1405. continue;
  1406. }
  1407. /* this entry seems to match: same crc, not too old,
  1408. * and from another gw. therefore return true to forbid it.
  1409. */
  1410. ret = true;
  1411. goto out;
  1412. }
  1413. /* not found, add a new entry (overwrite the oldest entry)
  1414. * and allow it, its the first occurrence.
  1415. */
  1416. curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
  1417. curr %= BATADV_DUPLIST_SIZE;
  1418. entry = &bat_priv->bla.bcast_duplist[curr];
  1419. entry->crc = crc;
  1420. entry->entrytime = jiffies;
  1421. /* known originator */
  1422. if (orig)
  1423. ether_addr_copy(entry->orig, orig);
  1424. /* anonymous originator */
  1425. else
  1426. eth_zero_addr(entry->orig);
  1427. bat_priv->bla.bcast_duplist_curr = curr;
  1428. out:
  1429. spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
  1430. return ret;
  1431. }
  1432. /**
  1433. * batadv_bla_check_ucast_duplist() - Check if a frame is in the broadcast dup.
  1434. * @bat_priv: the bat priv with all the mesh interface information
  1435. * @skb: contains the multicast packet to be checked, decapsulated from a
  1436. * unicast_packet
  1437. *
  1438. * Check if it is on our broadcast list. Another gateway might have sent the
  1439. * same packet because it is connected to the same backbone, so we have to
  1440. * remove this duplicate.
  1441. *
  1442. * Return: true if a packet is in the duplicate list, false otherwise.
  1443. */
  1444. static bool batadv_bla_check_ucast_duplist(struct batadv_priv *bat_priv,
  1445. struct sk_buff *skb)
  1446. {
  1447. return batadv_bla_check_duplist(bat_priv, skb, 0, NULL);
  1448. }
  1449. /**
  1450. * batadv_bla_check_bcast_duplist() - Check if a frame is in the broadcast dup.
  1451. * @bat_priv: the bat priv with all the mesh interface information
  1452. * @skb: contains the bcast_packet to be checked
  1453. *
  1454. * Check if it is on our broadcast list. Another gateway might have sent the
  1455. * same packet because it is connected to the same backbone, so we have to
  1456. * remove this duplicate.
  1457. *
  1458. * Return: true if a packet is in the duplicate list, false otherwise.
  1459. */
  1460. bool batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
  1461. struct sk_buff *skb)
  1462. {
  1463. struct batadv_bcast_packet *bcast_packet;
  1464. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  1465. return batadv_bla_check_duplist(bat_priv, skb, sizeof(*bcast_packet),
  1466. bcast_packet->orig);
  1467. }
  1468. /**
  1469. * batadv_bla_is_backbone_gw_orig() - Check if the originator is a gateway for
  1470. * the VLAN identified by vid.
  1471. * @bat_priv: the bat priv with all the mesh interface information
  1472. * @orig: originator mac address
  1473. * @vid: VLAN identifier
  1474. *
  1475. * Return: true if orig is a backbone for this vid, false otherwise.
  1476. */
  1477. bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
  1478. unsigned short vid)
  1479. {
  1480. struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
  1481. struct hlist_head *head;
  1482. struct batadv_bla_backbone_gw *backbone_gw;
  1483. int i;
  1484. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1485. return false;
  1486. if (!hash)
  1487. return false;
  1488. for (i = 0; i < hash->size; i++) {
  1489. head = &hash->table[i];
  1490. rcu_read_lock();
  1491. hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
  1492. if (batadv_compare_eth(backbone_gw->orig, orig) &&
  1493. backbone_gw->vid == vid) {
  1494. rcu_read_unlock();
  1495. return true;
  1496. }
  1497. }
  1498. rcu_read_unlock();
  1499. }
  1500. return false;
  1501. }
  1502. /**
  1503. * batadv_bla_is_backbone_gw() - check if originator is a backbone gw for a VLAN
  1504. * @skb: the frame to be checked
  1505. * @orig_node: the orig_node of the frame
  1506. * @hdr_size: maximum length of the frame
  1507. *
  1508. * Return: true if the orig_node is also a gateway on the mesh interface,
  1509. * otherwise it returns false.
  1510. */
  1511. bool batadv_bla_is_backbone_gw(struct sk_buff *skb,
  1512. struct batadv_orig_node *orig_node, int hdr_size)
  1513. {
  1514. struct batadv_bla_backbone_gw *backbone_gw;
  1515. unsigned short vid;
  1516. if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
  1517. return false;
  1518. /* first, find out the vid. */
  1519. if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
  1520. return false;
  1521. vid = batadv_get_vid(skb, hdr_size);
  1522. /* see if this originator is a backbone gw for this VLAN */
  1523. backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
  1524. orig_node->orig, vid);
  1525. if (!backbone_gw)
  1526. return false;
  1527. batadv_backbone_gw_put(backbone_gw);
  1528. return true;
  1529. }
  1530. /**
  1531. * batadv_bla_free() - free all bla structures
  1532. * @bat_priv: the bat priv with all the mesh interface information
  1533. *
  1534. * for meshinterface free or module unload
  1535. */
  1536. void batadv_bla_free(struct batadv_priv *bat_priv)
  1537. {
  1538. struct batadv_hard_iface *primary_if;
  1539. cancel_delayed_work_sync(&bat_priv->bla.work);
  1540. primary_if = batadv_primary_if_get_selected(bat_priv);
  1541. if (bat_priv->bla.claim_hash) {
  1542. batadv_bla_purge_claims(bat_priv, primary_if, 1);
  1543. batadv_hash_destroy(bat_priv->bla.claim_hash);
  1544. bat_priv->bla.claim_hash = NULL;
  1545. }
  1546. if (bat_priv->bla.backbone_hash) {
  1547. batadv_bla_purge_backbone_gw(bat_priv, 1);
  1548. batadv_hash_destroy(bat_priv->bla.backbone_hash);
  1549. bat_priv->bla.backbone_hash = NULL;
  1550. }
  1551. batadv_hardif_put(primary_if);
  1552. }
  1553. /**
  1554. * batadv_bla_loopdetect_check() - check and handle a detected loop
  1555. * @bat_priv: the bat priv with all the mesh interface information
  1556. * @skb: the packet to check
  1557. * @primary_if: interface where the request came on
  1558. * @vid: the VLAN ID of the frame
  1559. *
  1560. * Checks if this packet is a loop detect frame which has been sent by us,
  1561. * throws an uevent and logs the event if that is the case.
  1562. *
  1563. * Return: true if it is a loop detect frame which is to be dropped, false
  1564. * otherwise.
  1565. */
  1566. static bool
  1567. batadv_bla_loopdetect_check(struct batadv_priv *bat_priv, struct sk_buff *skb,
  1568. struct batadv_hard_iface *primary_if,
  1569. unsigned short vid)
  1570. {
  1571. struct batadv_bla_backbone_gw *backbone_gw;
  1572. struct ethhdr *ethhdr;
  1573. bool ret;
  1574. ethhdr = eth_hdr(skb);
  1575. /* Only check for the MAC address and skip more checks here for
  1576. * performance reasons - this function is on the hotpath, after all.
  1577. */
  1578. if (!batadv_compare_eth(ethhdr->h_source,
  1579. bat_priv->bla.loopdetect_addr))
  1580. return false;
  1581. /* If the packet came too late, don't forward it on the mesh
  1582. * but don't consider that as loop. It might be a coincidence.
  1583. */
  1584. if (batadv_has_timed_out(bat_priv->bla.loopdetect_lasttime,
  1585. BATADV_BLA_LOOPDETECT_TIMEOUT))
  1586. return true;
  1587. backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
  1588. primary_if->net_dev->dev_addr,
  1589. vid, true);
  1590. if (unlikely(!backbone_gw))
  1591. return true;
  1592. ret = queue_work(batadv_event_workqueue, &backbone_gw->report_work);
  1593. /* backbone_gw is unreferenced in the report work function
  1594. * if queue_work() call was successful
  1595. */
  1596. if (!ret)
  1597. batadv_backbone_gw_put(backbone_gw);
  1598. return true;
  1599. }
  1600. /**
  1601. * batadv_bla_rx() - check packets coming from the mesh.
  1602. * @bat_priv: the bat priv with all the mesh interface information
  1603. * @skb: the frame to be checked
  1604. * @vid: the VLAN ID of the frame
  1605. * @packet_type: the batman packet type this frame came in
  1606. *
  1607. * batadv_bla_rx avoidance checks if:
  1608. * * we have to race for a claim
  1609. * * if the frame is allowed on the LAN
  1610. *
  1611. * In these cases, the skb is further handled by this function
  1612. *
  1613. * Return: true if handled, otherwise it returns false and the caller shall
  1614. * further process the skb.
  1615. */
  1616. bool batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
  1617. unsigned short vid, int packet_type)
  1618. {
  1619. struct batadv_bla_backbone_gw *backbone_gw;
  1620. struct ethhdr *ethhdr;
  1621. struct batadv_bla_claim search_claim, *claim = NULL;
  1622. struct batadv_hard_iface *primary_if;
  1623. bool own_claim;
  1624. bool ret;
  1625. ethhdr = eth_hdr(skb);
  1626. primary_if = batadv_primary_if_get_selected(bat_priv);
  1627. if (!primary_if)
  1628. goto handled;
  1629. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1630. goto allow;
  1631. if (batadv_bla_loopdetect_check(bat_priv, skb, primary_if, vid))
  1632. goto handled;
  1633. if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
  1634. /* don't allow multicast packets while requests are in flight */
  1635. if (is_multicast_ether_addr(ethhdr->h_dest))
  1636. /* Both broadcast flooding or multicast-via-unicasts
  1637. * delivery might send to multiple backbone gateways
  1638. * sharing the same LAN and therefore need to coordinate
  1639. * which backbone gateway forwards into the LAN,
  1640. * by claiming the payload source address.
  1641. *
  1642. * Broadcast flooding and multicast-via-unicasts
  1643. * delivery use the following two batman packet types.
  1644. * Note: explicitly exclude BATADV_UNICAST_4ADDR,
  1645. * as the DHCP gateway feature will send explicitly
  1646. * to only one BLA gateway, so the claiming process
  1647. * should be avoided there.
  1648. */
  1649. if (packet_type == BATADV_BCAST ||
  1650. packet_type == BATADV_UNICAST)
  1651. goto handled;
  1652. /* potential duplicates from foreign BLA backbone gateways via
  1653. * multicast-in-unicast packets
  1654. */
  1655. if (is_multicast_ether_addr(ethhdr->h_dest) &&
  1656. packet_type == BATADV_UNICAST &&
  1657. batadv_bla_check_ucast_duplist(bat_priv, skb))
  1658. goto handled;
  1659. ether_addr_copy(search_claim.addr, ethhdr->h_source);
  1660. search_claim.vid = vid;
  1661. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  1662. if (!claim) {
  1663. bool local = batadv_is_my_client(bat_priv, ethhdr->h_source, vid);
  1664. /* possible optimization: race for a claim */
  1665. /* No claim exists yet, claim it for us!
  1666. */
  1667. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  1668. "%s(): Unclaimed MAC %pM found. Claim it. Local: %s\n",
  1669. __func__, ethhdr->h_source, str_yes_no(local));
  1670. batadv_handle_claim(bat_priv, primary_if,
  1671. primary_if->net_dev->dev_addr,
  1672. ethhdr->h_source, vid);
  1673. goto allow;
  1674. }
  1675. /* if it is our own claim ... */
  1676. backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
  1677. own_claim = batadv_compare_eth(backbone_gw->orig,
  1678. primary_if->net_dev->dev_addr);
  1679. batadv_backbone_gw_put(backbone_gw);
  1680. if (own_claim) {
  1681. /* ... allow it in any case */
  1682. claim->lasttime = jiffies;
  1683. goto allow;
  1684. }
  1685. /* if it is a multicast ... */
  1686. if (is_multicast_ether_addr(ethhdr->h_dest) &&
  1687. (packet_type == BATADV_BCAST || packet_type == BATADV_UNICAST)) {
  1688. /* ... drop it. the responsible gateway is in charge.
  1689. *
  1690. * We need to check packet type because with the gateway
  1691. * feature, broadcasts (like DHCP requests) may be sent
  1692. * using a unicast 4 address packet type. See comment above.
  1693. */
  1694. goto handled;
  1695. } else {
  1696. /* seems the client considers us as its best gateway.
  1697. * send a claim and update the claim table
  1698. * immediately.
  1699. */
  1700. batadv_handle_claim(bat_priv, primary_if,
  1701. primary_if->net_dev->dev_addr,
  1702. ethhdr->h_source, vid);
  1703. goto allow;
  1704. }
  1705. allow:
  1706. batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
  1707. ret = false;
  1708. goto out;
  1709. handled:
  1710. kfree_skb(skb);
  1711. ret = true;
  1712. out:
  1713. batadv_hardif_put(primary_if);
  1714. batadv_claim_put(claim);
  1715. return ret;
  1716. }
  1717. /**
  1718. * batadv_bla_tx() - check packets going into the mesh
  1719. * @bat_priv: the bat priv with all the mesh interface information
  1720. * @skb: the frame to be checked
  1721. * @vid: the VLAN ID of the frame
  1722. *
  1723. * batadv_bla_tx checks if:
  1724. * * a claim was received which has to be processed
  1725. * * the frame is allowed on the mesh
  1726. *
  1727. * in these cases, the skb is further handled by this function.
  1728. *
  1729. * This call might reallocate skb data.
  1730. *
  1731. * Return: true if handled, otherwise it returns false and the caller shall
  1732. * further process the skb.
  1733. */
  1734. bool batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
  1735. unsigned short vid)
  1736. {
  1737. struct ethhdr *ethhdr;
  1738. struct batadv_bla_claim search_claim, *claim = NULL;
  1739. struct batadv_bla_backbone_gw *backbone_gw;
  1740. struct batadv_hard_iface *primary_if;
  1741. bool client_roamed;
  1742. bool ret = false;
  1743. primary_if = batadv_primary_if_get_selected(bat_priv);
  1744. if (!primary_if)
  1745. goto out;
  1746. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  1747. goto allow;
  1748. if (batadv_bla_process_claim(bat_priv, primary_if, skb))
  1749. goto handled;
  1750. ethhdr = eth_hdr(skb);
  1751. if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
  1752. /* don't allow broadcasts while requests are in flight */
  1753. if (is_multicast_ether_addr(ethhdr->h_dest))
  1754. goto handled;
  1755. ether_addr_copy(search_claim.addr, ethhdr->h_source);
  1756. search_claim.vid = vid;
  1757. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  1758. /* if no claim exists, allow it. */
  1759. if (!claim)
  1760. goto allow;
  1761. /* check if we are responsible. */
  1762. backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
  1763. client_roamed = batadv_compare_eth(backbone_gw->orig,
  1764. primary_if->net_dev->dev_addr);
  1765. batadv_backbone_gw_put(backbone_gw);
  1766. if (client_roamed) {
  1767. /* if yes, the client has roamed and we have
  1768. * to unclaim it.
  1769. */
  1770. if (batadv_has_timed_out(claim->lasttime, 100)) {
  1771. /* only unclaim if the last claim entry is
  1772. * older than 100 ms to make sure we really
  1773. * have a roaming client here.
  1774. */
  1775. batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): Roaming client %pM detected. Unclaim it.\n",
  1776. __func__, ethhdr->h_source);
  1777. batadv_handle_unclaim(bat_priv, primary_if,
  1778. primary_if->net_dev->dev_addr,
  1779. ethhdr->h_source, vid);
  1780. goto allow;
  1781. } else {
  1782. batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): Race for claim %pM detected. Drop packet.\n",
  1783. __func__, ethhdr->h_source);
  1784. goto handled;
  1785. }
  1786. }
  1787. /* check if it is a multicast/broadcast frame */
  1788. if (is_multicast_ether_addr(ethhdr->h_dest)) {
  1789. /* drop it. the responsible gateway has forwarded it into
  1790. * the backbone network.
  1791. */
  1792. goto handled;
  1793. } else {
  1794. /* we must allow it. at least if we are
  1795. * responsible for the DESTINATION.
  1796. */
  1797. goto allow;
  1798. }
  1799. allow:
  1800. batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
  1801. ret = false;
  1802. goto out;
  1803. handled:
  1804. ret = true;
  1805. out:
  1806. batadv_hardif_put(primary_if);
  1807. batadv_claim_put(claim);
  1808. return ret;
  1809. }
  1810. /**
  1811. * batadv_bla_claim_dump_entry() - dump one entry of the claim table
  1812. * to a netlink socket
  1813. * @msg: buffer for the message
  1814. * @portid: netlink port
  1815. * @cb: Control block containing additional options
  1816. * @primary_if: primary interface
  1817. * @claim: entry to dump
  1818. *
  1819. * Return: 0 or error code.
  1820. */
  1821. static int
  1822. batadv_bla_claim_dump_entry(struct sk_buff *msg, u32 portid,
  1823. struct netlink_callback *cb,
  1824. struct batadv_hard_iface *primary_if,
  1825. struct batadv_bla_claim *claim)
  1826. {
  1827. const u8 *primary_addr = primary_if->net_dev->dev_addr;
  1828. struct batadv_bla_backbone_gw *backbone_gw;
  1829. u16 backbone_crc;
  1830. bool is_own;
  1831. void *hdr;
  1832. int ret = -EINVAL;
  1833. hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
  1834. &batadv_netlink_family, NLM_F_MULTI,
  1835. BATADV_CMD_GET_BLA_CLAIM);
  1836. if (!hdr) {
  1837. ret = -ENOBUFS;
  1838. goto out;
  1839. }
  1840. genl_dump_check_consistent(cb, hdr);
  1841. backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
  1842. is_own = batadv_compare_eth(backbone_gw->orig, primary_addr);
  1843. spin_lock_bh(&backbone_gw->crc_lock);
  1844. backbone_crc = backbone_gw->crc;
  1845. spin_unlock_bh(&backbone_gw->crc_lock);
  1846. if (is_own)
  1847. if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) {
  1848. genlmsg_cancel(msg, hdr);
  1849. goto put_backbone_gw;
  1850. }
  1851. if (nla_put(msg, BATADV_ATTR_BLA_ADDRESS, ETH_ALEN, claim->addr) ||
  1852. nla_put_u16(msg, BATADV_ATTR_BLA_VID, claim->vid) ||
  1853. nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN,
  1854. backbone_gw->orig) ||
  1855. nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
  1856. backbone_crc)) {
  1857. genlmsg_cancel(msg, hdr);
  1858. goto put_backbone_gw;
  1859. }
  1860. genlmsg_end(msg, hdr);
  1861. ret = 0;
  1862. put_backbone_gw:
  1863. batadv_backbone_gw_put(backbone_gw);
  1864. out:
  1865. return ret;
  1866. }
  1867. /**
  1868. * batadv_bla_claim_dump_bucket() - dump one bucket of the claim table
  1869. * to a netlink socket
  1870. * @msg: buffer for the message
  1871. * @portid: netlink port
  1872. * @cb: Control block containing additional options
  1873. * @primary_if: primary interface
  1874. * @hash: hash to dump
  1875. * @bucket: bucket index to dump
  1876. * @idx_skip: How many entries to skip
  1877. *
  1878. * Return: always 0.
  1879. */
  1880. static int
  1881. batadv_bla_claim_dump_bucket(struct sk_buff *msg, u32 portid,
  1882. struct netlink_callback *cb,
  1883. struct batadv_hard_iface *primary_if,
  1884. struct batadv_hashtable *hash, unsigned int bucket,
  1885. int *idx_skip)
  1886. {
  1887. struct batadv_bla_claim *claim;
  1888. int idx = 0;
  1889. int ret = 0;
  1890. spin_lock_bh(&hash->list_locks[bucket]);
  1891. cb->seq = atomic_read(&hash->generation) << 1 | 1;
  1892. hlist_for_each_entry(claim, &hash->table[bucket], hash_entry) {
  1893. if (idx++ < *idx_skip)
  1894. continue;
  1895. ret = batadv_bla_claim_dump_entry(msg, portid, cb,
  1896. primary_if, claim);
  1897. if (ret) {
  1898. *idx_skip = idx - 1;
  1899. goto unlock;
  1900. }
  1901. }
  1902. *idx_skip = 0;
  1903. unlock:
  1904. spin_unlock_bh(&hash->list_locks[bucket]);
  1905. return ret;
  1906. }
  1907. /**
  1908. * batadv_bla_claim_dump() - dump claim table to a netlink socket
  1909. * @msg: buffer for the message
  1910. * @cb: callback structure containing arguments
  1911. *
  1912. * Return: message length.
  1913. */
  1914. int batadv_bla_claim_dump(struct sk_buff *msg, struct netlink_callback *cb)
  1915. {
  1916. struct batadv_hard_iface *primary_if = NULL;
  1917. int portid = NETLINK_CB(cb->skb).portid;
  1918. struct net_device *mesh_iface;
  1919. struct batadv_hashtable *hash;
  1920. struct batadv_priv *bat_priv;
  1921. int bucket = cb->args[0];
  1922. int idx = cb->args[1];
  1923. int ret = 0;
  1924. mesh_iface = batadv_netlink_get_meshif(cb);
  1925. if (IS_ERR(mesh_iface))
  1926. return PTR_ERR(mesh_iface);
  1927. bat_priv = netdev_priv(mesh_iface);
  1928. hash = bat_priv->bla.claim_hash;
  1929. primary_if = batadv_primary_if_get_selected(bat_priv);
  1930. if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
  1931. ret = -ENOENT;
  1932. goto out;
  1933. }
  1934. while (bucket < hash->size) {
  1935. if (batadv_bla_claim_dump_bucket(msg, portid, cb, primary_if,
  1936. hash, bucket, &idx))
  1937. break;
  1938. bucket++;
  1939. }
  1940. cb->args[0] = bucket;
  1941. cb->args[1] = idx;
  1942. ret = msg->len;
  1943. out:
  1944. batadv_hardif_put(primary_if);
  1945. dev_put(mesh_iface);
  1946. return ret;
  1947. }
  1948. /**
  1949. * batadv_bla_backbone_dump_entry() - dump one entry of the backbone table to a
  1950. * netlink socket
  1951. * @msg: buffer for the message
  1952. * @portid: netlink port
  1953. * @cb: Control block containing additional options
  1954. * @primary_if: primary interface
  1955. * @backbone_gw: entry to dump
  1956. *
  1957. * Return: 0 or error code.
  1958. */
  1959. static int
  1960. batadv_bla_backbone_dump_entry(struct sk_buff *msg, u32 portid,
  1961. struct netlink_callback *cb,
  1962. struct batadv_hard_iface *primary_if,
  1963. struct batadv_bla_backbone_gw *backbone_gw)
  1964. {
  1965. const u8 *primary_addr = primary_if->net_dev->dev_addr;
  1966. u16 backbone_crc;
  1967. bool is_own;
  1968. int msecs;
  1969. void *hdr;
  1970. int ret = -EINVAL;
  1971. hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
  1972. &batadv_netlink_family, NLM_F_MULTI,
  1973. BATADV_CMD_GET_BLA_BACKBONE);
  1974. if (!hdr) {
  1975. ret = -ENOBUFS;
  1976. goto out;
  1977. }
  1978. genl_dump_check_consistent(cb, hdr);
  1979. is_own = batadv_compare_eth(backbone_gw->orig, primary_addr);
  1980. spin_lock_bh(&backbone_gw->crc_lock);
  1981. backbone_crc = backbone_gw->crc;
  1982. spin_unlock_bh(&backbone_gw->crc_lock);
  1983. msecs = jiffies_to_msecs(jiffies - backbone_gw->lasttime);
  1984. if (is_own)
  1985. if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) {
  1986. genlmsg_cancel(msg, hdr);
  1987. goto out;
  1988. }
  1989. if (nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN,
  1990. backbone_gw->orig) ||
  1991. nla_put_u16(msg, BATADV_ATTR_BLA_VID, backbone_gw->vid) ||
  1992. nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
  1993. backbone_crc) ||
  1994. nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {
  1995. genlmsg_cancel(msg, hdr);
  1996. goto out;
  1997. }
  1998. genlmsg_end(msg, hdr);
  1999. ret = 0;
  2000. out:
  2001. return ret;
  2002. }
  2003. /**
  2004. * batadv_bla_backbone_dump_bucket() - dump one bucket of the backbone table to
  2005. * a netlink socket
  2006. * @msg: buffer for the message
  2007. * @portid: netlink port
  2008. * @cb: Control block containing additional options
  2009. * @primary_if: primary interface
  2010. * @hash: hash to dump
  2011. * @bucket: bucket index to dump
  2012. * @idx_skip: How many entries to skip
  2013. *
  2014. * Return: always 0.
  2015. */
  2016. static int
  2017. batadv_bla_backbone_dump_bucket(struct sk_buff *msg, u32 portid,
  2018. struct netlink_callback *cb,
  2019. struct batadv_hard_iface *primary_if,
  2020. struct batadv_hashtable *hash,
  2021. unsigned int bucket, int *idx_skip)
  2022. {
  2023. struct batadv_bla_backbone_gw *backbone_gw;
  2024. int idx = 0;
  2025. int ret = 0;
  2026. spin_lock_bh(&hash->list_locks[bucket]);
  2027. cb->seq = atomic_read(&hash->generation) << 1 | 1;
  2028. hlist_for_each_entry(backbone_gw, &hash->table[bucket], hash_entry) {
  2029. if (idx++ < *idx_skip)
  2030. continue;
  2031. ret = batadv_bla_backbone_dump_entry(msg, portid, cb,
  2032. primary_if, backbone_gw);
  2033. if (ret) {
  2034. *idx_skip = idx - 1;
  2035. goto unlock;
  2036. }
  2037. }
  2038. *idx_skip = 0;
  2039. unlock:
  2040. spin_unlock_bh(&hash->list_locks[bucket]);
  2041. return ret;
  2042. }
  2043. /**
  2044. * batadv_bla_backbone_dump() - dump backbone table to a netlink socket
  2045. * @msg: buffer for the message
  2046. * @cb: callback structure containing arguments
  2047. *
  2048. * Return: message length.
  2049. */
  2050. int batadv_bla_backbone_dump(struct sk_buff *msg, struct netlink_callback *cb)
  2051. {
  2052. struct batadv_hard_iface *primary_if = NULL;
  2053. int portid = NETLINK_CB(cb->skb).portid;
  2054. struct net_device *mesh_iface;
  2055. struct batadv_hashtable *hash;
  2056. struct batadv_priv *bat_priv;
  2057. int bucket = cb->args[0];
  2058. int idx = cb->args[1];
  2059. int ret = 0;
  2060. mesh_iface = batadv_netlink_get_meshif(cb);
  2061. if (IS_ERR(mesh_iface))
  2062. return PTR_ERR(mesh_iface);
  2063. bat_priv = netdev_priv(mesh_iface);
  2064. hash = bat_priv->bla.backbone_hash;
  2065. primary_if = batadv_primary_if_get_selected(bat_priv);
  2066. if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
  2067. ret = -ENOENT;
  2068. goto out;
  2069. }
  2070. while (bucket < hash->size) {
  2071. if (batadv_bla_backbone_dump_bucket(msg, portid, cb, primary_if,
  2072. hash, bucket, &idx))
  2073. break;
  2074. bucket++;
  2075. }
  2076. cb->args[0] = bucket;
  2077. cb->args[1] = idx;
  2078. ret = msg->len;
  2079. out:
  2080. batadv_hardif_put(primary_if);
  2081. dev_put(mesh_iface);
  2082. return ret;
  2083. }
  2084. #ifdef CONFIG_BATMAN_ADV_DAT
  2085. /**
  2086. * batadv_bla_check_claim() - check if address is claimed
  2087. *
  2088. * @bat_priv: the bat priv with all the mesh interface information
  2089. * @addr: mac address of which the claim status is checked
  2090. * @vid: the VLAN ID
  2091. *
  2092. * addr is checked if this address is claimed by the local device itself.
  2093. *
  2094. * Return: true if bla is disabled or the mac is claimed by the device,
  2095. * false if the device addr is already claimed by another gateway
  2096. */
  2097. bool batadv_bla_check_claim(struct batadv_priv *bat_priv,
  2098. u8 *addr, unsigned short vid)
  2099. {
  2100. struct batadv_bla_backbone_gw *backbone_gw;
  2101. struct batadv_bla_claim search_claim;
  2102. struct batadv_bla_claim *claim = NULL;
  2103. struct batadv_hard_iface *primary_if = NULL;
  2104. bool ret = true;
  2105. if (!atomic_read(&bat_priv->bridge_loop_avoidance))
  2106. return ret;
  2107. primary_if = batadv_primary_if_get_selected(bat_priv);
  2108. if (!primary_if)
  2109. return ret;
  2110. /* First look if the mac address is claimed */
  2111. ether_addr_copy(search_claim.addr, addr);
  2112. search_claim.vid = vid;
  2113. claim = batadv_claim_hash_find(bat_priv, &search_claim);
  2114. /* If there is a claim and we are not owner of the claim,
  2115. * return false.
  2116. */
  2117. if (claim) {
  2118. backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
  2119. if (!batadv_compare_eth(backbone_gw->orig,
  2120. primary_if->net_dev->dev_addr))
  2121. ret = false;
  2122. batadv_backbone_gw_put(backbone_gw);
  2123. batadv_claim_put(claim);
  2124. }
  2125. batadv_hardif_put(primary_if);
  2126. return ret;
  2127. }
  2128. #endif