bridge_loop_avoidance.c 67 KB

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