bridge_loop_avoidance.c 70 KB

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