ipa_nat_drvi.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686
  1. /*
  2. * Copyright (c) 2013-2020 The Linux Foundation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above
  10. * copyright notice, this list of conditions and the following
  11. * disclaimer in the documentation and/or other materials provided
  12. * with the distribution.
  13. * * Neither the name of The Linux Foundation nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  24. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  26. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  27. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include "ipa_nat_drv.h"
  30. #include "ipa_nat_drvi.h"
  31. #include <stdio.h>
  32. #include <stdint.h>
  33. #include <stdbool.h>
  34. #include <sys/ioctl.h>
  35. #include <stdlib.h>
  36. #include <netinet/in.h>
  37. #include <errno.h>
  38. #include <pthread.h>
  39. #include <unistd.h>
  40. #include <linux/msm_ipa.h>
  41. #define IPA_NAT_DEBUG_FILE_PATH "/sys/kernel/debug/ipa/ip4_nat"
  42. #define IPA_NAT_TABLE_NAME "IPA NAT table"
  43. #define IPA_NAT_INDEX_TABLE_NAME "IPA NAT index table"
  44. #undef min
  45. #define min(a, b) ((a) < (b)) ? (a) : (b)
  46. #undef max
  47. #define max(a, b) ((a) > (b)) ? (a) : (b)
  48. static struct ipa_nat_cache ipv4_nat_cache[IPA_NAT_MEM_IN_MAX];
  49. static struct ipa_nat_cache *active_nat_cache_ptr = NULL;
  50. #undef DDR_IS_ACTIVE
  51. #define DDR_IS_ACTIVE() \
  52. (active_nat_cache_ptr) ? \
  53. (active_nat_cache_ptr->nmi == IPA_NAT_MEM_IN_DDR) : \
  54. false
  55. #undef SRAM_IS_ACTIVE
  56. #define SRAM_IS_ACTIVE() \
  57. (active_nat_cache_ptr) ? \
  58. (active_nat_cache_ptr->nmi == IPA_NAT_MEM_IN_SRAM) : \
  59. false
  60. extern pthread_mutex_t nat_mutex;
  61. static ipa_nat_pdn_entry pdns[IPA_MAX_PDN_NUM];
  62. static int num_pdns = 0;
  63. static int Hash_token = 69;
  64. /*
  65. * ----------------------------------------------------------------------------
  66. * Private helpers for manipulating regular tables
  67. * ----------------------------------------------------------------------------
  68. */
  69. static int table_entry_is_valid(
  70. void* entry)
  71. {
  72. struct ipa_nat_rule* rule = (struct ipa_nat_rule*) entry;
  73. IPADBG("In\n");
  74. IPADBG("enable(%u)\n", rule->enable);
  75. IPADBG("Out\n");
  76. return rule->enable;
  77. }
  78. static uint16_t table_entry_get_next_index(
  79. void* entry)
  80. {
  81. uint16_t result;
  82. struct ipa_nat_rule* rule = (struct ipa_nat_rule*)entry;
  83. IPADBG("In\n");
  84. result = rule->next_index;
  85. IPADBG("Next entry of %pK is %u\n", entry, result);
  86. IPADBG("Out\n");
  87. return result;
  88. }
  89. static uint16_t table_entry_get_prev_index(
  90. void* entry,
  91. uint16_t entry_index,
  92. void* meta,
  93. uint16_t base_table_size)
  94. {
  95. uint16_t result;
  96. struct ipa_nat_rule* rule = (struct ipa_nat_rule*)entry;
  97. IPADBG("In\n");
  98. result = rule->prev_index;
  99. IPADBG("Previous entry of %u is %u\n", entry_index, result);
  100. IPADBG("Out\n");
  101. return result;
  102. }
  103. static void table_entry_set_prev_index(
  104. void* entry,
  105. uint16_t entry_index,
  106. uint16_t prev_index,
  107. void* meta,
  108. uint16_t base_table_size)
  109. {
  110. struct ipa_nat_rule* rule = (struct ipa_nat_rule*) entry;
  111. IPADBG("In\n");
  112. IPADBG("Previous entry of %u is %u\n", entry_index, prev_index);
  113. rule->prev_index = prev_index;
  114. IPADBG("Out\n");
  115. }
  116. /**
  117. * ipa_nati_calc_ip_cksum() - Calculate the source nat IP checksum diff
  118. * @pub_ip_addr: [in] public ip address
  119. * @priv_ip_addr: [in] Private ip address
  120. *
  121. * source nat ip checksum different is calculated as
  122. * public_ip_addr - private_ip_addr
  123. * Here we are using 1's complement to represent -ve number.
  124. * So take 1's complement of private ip addr and add it
  125. * to public ip addr.
  126. *
  127. * Returns: >0 ip checksum diff
  128. */
  129. static uint16_t ipa_nati_calc_ip_cksum(
  130. uint32_t pub_ip_addr,
  131. uint32_t priv_ip_addr)
  132. {
  133. uint16_t ret;
  134. uint32_t cksum = 0;
  135. IPADBG("In\n");
  136. /* Add LSB(2 bytes) of public ip address to cksum */
  137. cksum += (pub_ip_addr & 0xFFFF);
  138. /* Add MSB(2 bytes) of public ip address to cksum
  139. and check for carry forward(CF), if any add it
  140. */
  141. cksum += (pub_ip_addr>>16);
  142. if (cksum >> 16) {
  143. cksum = (cksum & 0x0000FFFF);
  144. cksum += 1;
  145. }
  146. /* Calculate the 1's complement of private ip address */
  147. priv_ip_addr = (~priv_ip_addr);
  148. /* Add LSB(2 bytes) of private ip address to cksum
  149. and check for carry forward(CF), if any add it
  150. */
  151. cksum += (priv_ip_addr & 0xFFFF);
  152. if (cksum >> 16) {
  153. cksum = (cksum & 0x0000FFFF);
  154. cksum += 1;
  155. }
  156. /* Add MSB(2 bytes) of private ip address to cksum
  157. and check for carry forward(CF), if any add it
  158. */
  159. cksum += (priv_ip_addr>>16);
  160. if (cksum >> 16) {
  161. cksum = (cksum & 0x0000FFFF);
  162. cksum += 1;
  163. }
  164. /* Return the LSB(2 bytes) of checksum */
  165. ret = (uint16_t)cksum;
  166. IPADBG("Out\n");
  167. return ret;
  168. }
  169. /**
  170. * ipa_nati_calc_tcp_udp_cksum() - Calculate the source nat TCP/UDP checksum diff
  171. * @pub_ip_addr: [in] public ip address
  172. * @pub_port: [in] public tcp/udp port
  173. * @priv_ip_addr: [in] Private ip address
  174. * @priv_port: [in] Private tcp/udp prot
  175. *
  176. * source nat tcp/udp checksum is calculated as
  177. * (pub_ip_addr + pub_port) - (priv_ip_addr + priv_port)
  178. * Here we are using 1's complement to represent -ve number.
  179. * So take 1's complement of prviate ip addr &private port
  180. * and add it public ip addr & public port.
  181. *
  182. * Returns: >0 tcp/udp checksum diff
  183. */
  184. static uint16_t ipa_nati_calc_tcp_udp_cksum(
  185. uint32_t pub_ip_addr,
  186. uint16_t pub_port,
  187. uint32_t priv_ip_addr,
  188. uint16_t priv_port)
  189. {
  190. uint16_t ret = 0;
  191. uint32_t cksum = 0;
  192. IPADBG("In\n");
  193. /* Add LSB(2 bytes) of public ip address to cksum */
  194. cksum += (pub_ip_addr & 0xFFFF);
  195. /* Add MSB(2 bytes) of public ip address to cksum
  196. and check for carry forward(CF), if any add it
  197. */
  198. cksum += (pub_ip_addr>>16);
  199. if (cksum >> 16) {
  200. cksum = (cksum & 0x0000FFFF);
  201. cksum += 1;
  202. }
  203. /* Add public port to cksum and
  204. check for carry forward(CF), if any add it */
  205. cksum += pub_port;
  206. if (cksum >> 16) {
  207. cksum = (cksum & 0x0000FFFF);
  208. cksum += 1;
  209. }
  210. /* Calculate the 1's complement of private ip address */
  211. priv_ip_addr = (~priv_ip_addr);
  212. /* Add LSB(2 bytes) of private ip address to cksum
  213. and check for carry forward(CF), if any add it
  214. */
  215. cksum += (priv_ip_addr & 0xFFFF);
  216. if (cksum >> 16) {
  217. cksum = (cksum & 0x0000FFFF);
  218. cksum += 1;
  219. }
  220. /* Add MSB(2 bytes) of private ip address to cksum
  221. and check for carry forward(CF), if any add
  222. */
  223. cksum += (priv_ip_addr>>16);
  224. if (cksum >> 16) {
  225. cksum = (cksum & 0x0000FFFF);
  226. cksum += 1;
  227. }
  228. /* Calculate the 1's complement of private port */
  229. priv_port = (~priv_port);
  230. /* Add public port to cksum and
  231. check for carry forward(CF), if any add it */
  232. cksum += priv_port;
  233. if (cksum >> 16) {
  234. cksum = (cksum & 0x0000FFFF);
  235. cksum += 1;
  236. }
  237. /* return the LSB(2 bytes) of checksum */
  238. ret = (uint16_t)cksum;
  239. IPADBG("Out\n");
  240. return ret;
  241. }
  242. static int table_entry_copy_from_user(
  243. void* entry,
  244. void* user_data)
  245. {
  246. uint32_t pub_ip_addr;
  247. struct ipa_nat_rule* nat_entry = (struct ipa_nat_rule*) entry;
  248. const ipa_nat_ipv4_rule* user_rule = (const ipa_nat_ipv4_rule*) user_data;
  249. IPADBG("In\n");
  250. pub_ip_addr = pdns[user_rule->pdn_index].public_ip;
  251. nat_entry->private_ip = user_rule->private_ip;
  252. nat_entry->private_port = user_rule->private_port;
  253. nat_entry->protocol = user_rule->protocol;
  254. nat_entry->public_port = user_rule->public_port;
  255. nat_entry->target_ip = user_rule->target_ip;
  256. nat_entry->target_port = user_rule->target_port;
  257. nat_entry->pdn_index = user_rule->pdn_index;
  258. nat_entry->uc_activation_index = user_rule->uc_activation_index;
  259. nat_entry->s = user_rule->s;
  260. nat_entry->ucp = user_rule->ucp;
  261. nat_entry->dst_only = user_rule->dst_only;
  262. nat_entry->src_only = user_rule->src_only;
  263. nat_entry->ip_chksum =
  264. ipa_nati_calc_ip_cksum(pub_ip_addr, user_rule->private_ip);
  265. if (IPPROTO_TCP == nat_entry->protocol ||
  266. IPPROTO_UDP == nat_entry->protocol) {
  267. nat_entry->tcp_udp_chksum = ipa_nati_calc_tcp_udp_cksum(
  268. pub_ip_addr,
  269. user_rule->public_port,
  270. user_rule->private_ip,
  271. user_rule->private_port);
  272. }
  273. IPADBG("Out\n");
  274. return 0;
  275. }
  276. static int table_entry_head_insert(
  277. void* entry,
  278. void* user_data,
  279. uint16_t* dma_command_data)
  280. {
  281. int ret;
  282. struct ipa_nat_rule* nat_entry = (struct ipa_nat_rule*) entry;
  283. IPADBG("In\n");
  284. IPADBG("entry(%p) user_data(%p) dma_command_data(%p)\n",
  285. entry,
  286. user_data,
  287. dma_command_data);
  288. ret = table_entry_copy_from_user(entry, user_data);
  289. if (ret) {
  290. IPAERR("unable to copy from user a new entry\n");
  291. goto bail;
  292. }
  293. *dma_command_data = 0;
  294. ((ipa_nat_flags*)dma_command_data)->enable = IPA_NAT_FLAG_ENABLE_BIT;
  295. ((ipa_nat_flags*)dma_command_data)->uc_activation_index =
  296. nat_entry->uc_activation_index;
  297. ((ipa_nat_flags*)dma_command_data)->s = nat_entry->s;
  298. ;
  299. bail:
  300. IPADBG("Out\n");
  301. return ret;
  302. }
  303. static int table_entry_tail_insert(
  304. void* entry,
  305. void* user_data)
  306. {
  307. struct ipa_nat_rule* nat_entry = (struct ipa_nat_rule*) entry;
  308. int ret;
  309. IPADBG("In\n");
  310. IPADBG("entry(%p) user_data(%p)\n",
  311. entry,
  312. user_data);
  313. ret = table_entry_copy_from_user(entry, user_data);
  314. if (ret) {
  315. IPAERR("unable to copy from user a new entry\n");
  316. goto bail;
  317. }
  318. nat_entry->enable = IPA_NAT_FLAG_ENABLE_BIT;
  319. bail:
  320. IPADBG("Out\n");
  321. return ret;
  322. }
  323. static uint16_t table_entry_get_delete_head_dma_command_data(
  324. void* head,
  325. void* next_entry)
  326. {
  327. IPADBG("In\n");
  328. IPADBG("Out\n");
  329. return IPA_NAT_INVALID_PROTO_FIELD_VALUE;
  330. }
  331. /*
  332. * ----------------------------------------------------------------------------
  333. * Private helpers for manipulating index tables
  334. * ----------------------------------------------------------------------------
  335. */
  336. static int index_table_entry_is_valid(
  337. void* entry)
  338. {
  339. struct ipa_nat_indx_tbl_rule* rule =
  340. (struct ipa_nat_indx_tbl_rule*) entry;
  341. int ret;
  342. IPADBG("In\n");
  343. ret = (rule->tbl_entry) ? 1 : 0;
  344. IPADBG("enable(%d)\n", ret);
  345. IPADBG("Out\n");
  346. return ret;
  347. }
  348. static uint16_t index_table_entry_get_next_index(
  349. void* entry)
  350. {
  351. uint16_t result;
  352. struct ipa_nat_indx_tbl_rule* rule = (struct ipa_nat_indx_tbl_rule*)entry;
  353. IPADBG("In\n");
  354. result = rule->next_index;
  355. IPADBG("Next entry of %pK is %d\n", entry, result);
  356. IPADBG("Out\n");
  357. return result;
  358. }
  359. static uint16_t index_table_entry_get_prev_index(
  360. void* entry,
  361. uint16_t entry_index,
  362. void* meta,
  363. uint16_t base_table_size)
  364. {
  365. uint16_t result = 0;
  366. struct ipa_nat_indx_tbl_meta_info* index_expn_table_meta =
  367. (struct ipa_nat_indx_tbl_meta_info*)meta;
  368. IPADBG("In\n");
  369. if (entry_index >= base_table_size)
  370. result = index_expn_table_meta[entry_index - base_table_size].prev_index;
  371. IPADBG("Previous entry of %d is %d\n", entry_index, result);
  372. IPADBG("Out\n");
  373. return result;
  374. }
  375. static void index_table_entry_set_prev_index(
  376. void* entry,
  377. uint16_t entry_index,
  378. uint16_t prev_index,
  379. void* meta,
  380. uint16_t base_table_size)
  381. {
  382. struct ipa_nat_indx_tbl_meta_info* index_expn_table_meta =
  383. (struct ipa_nat_indx_tbl_meta_info*) meta;
  384. IPADBG("In\n");
  385. IPADBG("Previous entry of %u is %u\n", entry_index, prev_index);
  386. if ( entry_index >= base_table_size )
  387. {
  388. index_expn_table_meta[entry_index - base_table_size].prev_index = prev_index;
  389. }
  390. else if ( VALID_INDEX(prev_index) )
  391. {
  392. IPAERR("Base table entry %u can't has prev entry %u, but only %u",
  393. entry_index, prev_index, IPA_TABLE_INVALID_ENTRY);
  394. }
  395. IPADBG("Out\n");
  396. }
  397. static int index_table_entry_head_insert(
  398. void* entry,
  399. void* user_data,
  400. uint16_t* dma_command_data)
  401. {
  402. IPADBG("In\n");
  403. IPADBG("entry(%p) user_data(%p) dma_command_data(%p)\n",
  404. entry,
  405. user_data,
  406. dma_command_data);
  407. *dma_command_data = *((uint16_t*)user_data);
  408. IPADBG("Out\n");
  409. return 0;
  410. }
  411. static int index_table_entry_tail_insert(
  412. void* entry,
  413. void* user_data)
  414. {
  415. struct ipa_nat_indx_tbl_rule* rule_ptr =
  416. (struct ipa_nat_indx_tbl_rule*) entry;
  417. IPADBG("In\n");
  418. IPADBG("entry(%p) user_data(%p)\n",
  419. entry,
  420. user_data);
  421. rule_ptr->tbl_entry = *((uint16_t*)user_data);
  422. IPADBG("Out\n");
  423. return 0;
  424. }
  425. static uint16_t index_table_entry_get_delete_head_dma_command_data(
  426. void* head,
  427. void* next_entry)
  428. {
  429. uint16_t result;
  430. struct ipa_nat_indx_tbl_rule* rule =
  431. (struct ipa_nat_indx_tbl_rule*)next_entry;
  432. IPADBG("In\n");
  433. result = rule->tbl_entry;
  434. IPADBG("Out\n");
  435. return result;
  436. }
  437. /*
  438. * ----------------------------------------------------------------------------
  439. * Private data and functions used by this file's API
  440. * ----------------------------------------------------------------------------
  441. */
  442. static ipa_table_entry_interface entry_interface = {
  443. table_entry_is_valid,
  444. table_entry_get_next_index,
  445. table_entry_get_prev_index,
  446. table_entry_set_prev_index,
  447. table_entry_head_insert,
  448. table_entry_tail_insert,
  449. table_entry_get_delete_head_dma_command_data
  450. };
  451. static ipa_table_entry_interface index_entry_interface = {
  452. index_table_entry_is_valid,
  453. index_table_entry_get_next_index,
  454. index_table_entry_get_prev_index,
  455. index_table_entry_set_prev_index,
  456. index_table_entry_head_insert,
  457. index_table_entry_tail_insert,
  458. index_table_entry_get_delete_head_dma_command_data
  459. };
  460. /**
  461. * ipa_nati_create_table_dma_cmd_helpers()
  462. *
  463. * Creates dma_cmd_helpers for base and index tables in the received
  464. * NAT table
  465. *
  466. * @nat_table: [in] NAT table
  467. * @table_indx: [in] The index of the NAT table
  468. *
  469. * A DMA command helper helps to generate the DMA command for one
  470. * specific field change. Each table has 3 different types of field
  471. * change: update_head, update_entry and delete_head. This function
  472. * creates the helpers for base and index tables and updates the
  473. * tables correspondingly.
  474. */
  475. static void ipa_nati_create_table_dma_cmd_helpers(
  476. struct ipa_nat_ip4_table_cache* nat_table,
  477. uint8_t table_indx)
  478. {
  479. IPADBG("In\n");
  480. /*
  481. * Create helpers for base table
  482. */
  483. ipa_table_dma_cmd_helper_init(
  484. &nat_table->table_dma_cmd_helpers[IPA_NAT_TABLE_FLAGS],
  485. table_indx,
  486. IPA_NAT_BASE_TBL,
  487. IPA_NAT_EXPN_TBL,
  488. nat_table->mem_desc.addr_offset + IPA_NAT_RULE_FLAG_FIELD_OFFSET);
  489. ipa_table_dma_cmd_helper_init(
  490. &nat_table->table_dma_cmd_helpers[IPA_NAT_TABLE_NEXT_INDEX],
  491. table_indx,
  492. IPA_NAT_BASE_TBL,
  493. IPA_NAT_EXPN_TBL,
  494. nat_table->mem_desc.addr_offset + IPA_NAT_RULE_NEXT_FIELD_OFFSET);
  495. ipa_table_dma_cmd_helper_init(
  496. &nat_table->table_dma_cmd_helpers[IPA_NAT_TABLE_PROTOCOL],
  497. table_indx,
  498. IPA_NAT_BASE_TBL,
  499. IPA_NAT_EXPN_TBL,
  500. nat_table->mem_desc.addr_offset + IPA_NAT_RULE_PROTO_FIELD_OFFSET);
  501. /*
  502. * Create helpers for index table
  503. */
  504. ipa_table_dma_cmd_helper_init(
  505. &nat_table->table_dma_cmd_helpers[IPA_NAT_INDEX_TABLE_ENTRY],
  506. table_indx,
  507. IPA_NAT_INDX_TBL,
  508. IPA_NAT_INDEX_EXPN_TBL,
  509. nat_table->mem_desc.addr_offset + IPA_NAT_INDEX_RULE_NAT_INDEX_FIELD_OFFSET);
  510. ipa_table_dma_cmd_helper_init(
  511. &nat_table->table_dma_cmd_helpers[IPA_NAT_INDEX_TABLE_NEXT_INDEX],
  512. table_indx,
  513. IPA_NAT_INDX_TBL,
  514. IPA_NAT_INDEX_EXPN_TBL,
  515. nat_table->mem_desc.addr_offset + IPA_NAT_INDEX_RULE_NEXT_FIELD_OFFSET);
  516. /*
  517. * Init helpers for base table
  518. */
  519. nat_table->table.dma_help[HELP_UPDATE_HEAD] =
  520. &nat_table->table_dma_cmd_helpers[IPA_NAT_TABLE_FLAGS];
  521. nat_table->table.dma_help[HELP_UPDATE_ENTRY] =
  522. &nat_table->table_dma_cmd_helpers[IPA_NAT_TABLE_NEXT_INDEX];
  523. nat_table->table.dma_help[HELP_DELETE_HEAD] =
  524. &nat_table->table_dma_cmd_helpers[IPA_NAT_TABLE_PROTOCOL];
  525. /*
  526. * Init helpers for index table
  527. */
  528. nat_table->index_table.dma_help[HELP_UPDATE_HEAD] =
  529. &nat_table->table_dma_cmd_helpers[IPA_NAT_INDEX_TABLE_ENTRY];
  530. nat_table->index_table.dma_help[HELP_UPDATE_ENTRY] =
  531. &nat_table->table_dma_cmd_helpers[IPA_NAT_INDEX_TABLE_NEXT_INDEX];
  532. nat_table->index_table.dma_help[HELP_DELETE_HEAD] =
  533. &nat_table->table_dma_cmd_helpers[IPA_NAT_INDEX_TABLE_ENTRY];
  534. IPADBG("Out\n");
  535. }
  536. /**
  537. * ipa_nati_create_table() - Creates a new IPv4 NAT table
  538. * @nat_table: [in] IPv4 NAT table
  539. * @public_ip_addr: [in] public IPv4 address
  540. * @number_of_entries: [in] number of NAT entries
  541. * @table_index: [in] the index of the IPv4 NAT table
  542. *
  543. * This function creates new IPv4 NAT table:
  544. * - Initializes table, index table, memory descriptor and
  545. * table_dma_cmd_helpers structures
  546. * - Allocates the index expansion table meta data
  547. * - Allocates, maps and clears the memory for table and index table
  548. *
  549. * Returns: 0 On Success, negative on failure
  550. */
  551. static int ipa_nati_create_table(
  552. struct ipa_nat_cache* nat_cache_ptr,
  553. struct ipa_nat_ip4_table_cache* nat_table,
  554. uint32_t public_ip_addr,
  555. uint16_t number_of_entries,
  556. uint8_t table_index)
  557. {
  558. int ret, size;
  559. void* base_addr;
  560. #ifdef IPA_ON_R3PC
  561. uint32_t nat_mem_offset = 0;
  562. #endif
  563. IPADBG("In\n");
  564. nat_table->public_addr = public_ip_addr;
  565. ipa_table_init(
  566. &nat_table->table,
  567. IPA_NAT_TABLE_NAME,
  568. nat_cache_ptr->nmi,
  569. sizeof(struct ipa_nat_rule),
  570. NULL,
  571. 0,
  572. &entry_interface);
  573. ret = ipa_table_calculate_entries_num(
  574. &nat_table->table,
  575. number_of_entries,
  576. nat_cache_ptr->nmi);
  577. if (ret) {
  578. IPAERR(
  579. "unable to calculate number of entries in "
  580. "nat table %d, while required by user %d\n",
  581. table_index, number_of_entries);
  582. goto done;
  583. }
  584. /*
  585. * Allocate memory for NAT index expansion table meta data
  586. */
  587. nat_table->index_expn_table_meta = (struct ipa_nat_indx_tbl_meta_info*)
  588. calloc(nat_table->table.expn_table_entries,
  589. sizeof(struct ipa_nat_indx_tbl_meta_info));
  590. if (nat_table->index_expn_table_meta == NULL) {
  591. IPAERR(
  592. "Fail to allocate ipv4 index expansion table meta with size %d\n",
  593. nat_table->table.expn_table_entries *
  594. sizeof(struct ipa_nat_indx_tbl_meta_info));
  595. ret = -ENOMEM;
  596. goto done;
  597. }
  598. ipa_table_init(
  599. &nat_table->index_table,
  600. IPA_NAT_INDEX_TABLE_NAME,
  601. nat_cache_ptr->nmi,
  602. sizeof(struct ipa_nat_indx_tbl_rule),
  603. nat_table->index_expn_table_meta,
  604. sizeof(struct ipa_nat_indx_tbl_meta_info),
  605. &index_entry_interface);
  606. nat_table->index_table.table_entries =
  607. nat_table->table.table_entries;
  608. nat_table->index_table.expn_table_entries =
  609. nat_table->table.expn_table_entries;
  610. nat_table->index_table.tot_tbl_ents =
  611. nat_table->table.tot_tbl_ents;
  612. size = ipa_table_calculate_size(&nat_table->table);
  613. size += ipa_table_calculate_size(&nat_table->index_table);
  614. IPADBG("Nat Base and Index Table size: %d\n", size);
  615. ipa_mem_descriptor_init(
  616. &nat_table->mem_desc,
  617. IPA_NAT_DEV_NAME,
  618. size,
  619. table_index,
  620. IPA_IOC_ALLOC_NAT_TABLE,
  621. IPA_IOC_DEL_NAT_TABLE,
  622. true); /* true here means do consider using sram */
  623. ret = ipa_mem_descriptor_allocate_memory(
  624. &nat_table->mem_desc,
  625. nat_cache_ptr->ipa_desc->fd);
  626. if (ret) {
  627. IPAERR("unable to allocate nat memory descriptor Error: %d\n", ret);
  628. goto bail_meta;
  629. }
  630. base_addr = nat_table->mem_desc.base_addr;
  631. #ifdef IPA_ON_R3PC
  632. ret = ioctl(nat_cache_ptr->ipa_desc->fd,
  633. IPA_IOC_GET_NAT_OFFSET,
  634. &nat_mem_offset);
  635. if (ret) {
  636. IPAERR("unable to post ant offset cmd Error: %d IPA fd %d\n",
  637. ret, nat_cache_ptr->ipa_desc->fd);
  638. goto bail_mem_desc;
  639. }
  640. base_addr += nat_mem_offset;
  641. #endif
  642. base_addr =
  643. ipa_table_calculate_addresses(&nat_table->table, base_addr);
  644. ipa_table_calculate_addresses(&nat_table->index_table, base_addr);
  645. ipa_table_reset(&nat_table->table);
  646. ipa_table_reset(&nat_table->index_table);
  647. ipa_nati_create_table_dma_cmd_helpers(nat_table, table_index);
  648. goto done;
  649. #ifdef IPA_ON_R3PC
  650. bail_mem_desc:
  651. ipa_mem_descriptor_delete(&nat_table->mem_desc, nat_cache_ptr->ipa_desc->fd);
  652. #endif
  653. bail_meta:
  654. free(nat_table->index_expn_table_meta);
  655. memset(nat_table, 0, sizeof(*nat_table));
  656. done:
  657. IPADBG("Out\n");
  658. return ret;
  659. }
  660. static int ipa_nati_destroy_table(
  661. struct ipa_nat_cache* nat_cache_ptr,
  662. struct ipa_nat_ip4_table_cache* nat_table)
  663. {
  664. int ret;
  665. IPADBG("In\n");
  666. ret = ipa_mem_descriptor_delete(
  667. &nat_table->mem_desc, nat_cache_ptr->ipa_desc->fd);
  668. if (ret)
  669. IPAERR("unable to delete NAT descriptor\n");
  670. free(nat_table->index_expn_table_meta);
  671. memset(nat_table, 0, sizeof(*nat_table));
  672. IPADBG("Out\n");
  673. return ret;
  674. }
  675. static int ipa_nati_post_ipv4_init_cmd(
  676. struct ipa_nat_cache* nat_cache_ptr,
  677. struct ipa_nat_ip4_table_cache* nat_table,
  678. uint8_t tbl_index,
  679. bool focus_change )
  680. {
  681. struct ipa_ioc_v4_nat_init cmd;
  682. char buf[1024];
  683. int ret;
  684. IPADBG("In\n");
  685. IPADBG("nat_cache_ptr(%p) nat_table(%p) tbl_index(%u) focus_change(%u)\n",
  686. nat_cache_ptr, nat_table, tbl_index, focus_change);
  687. memset(&cmd, 0, sizeof(cmd));
  688. cmd.tbl_index = tbl_index;
  689. cmd.focus_change = focus_change;
  690. cmd.mem_type = nat_cache_ptr->nmi;
  691. cmd.ipv4_rules_offset =
  692. nat_table->mem_desc.addr_offset;
  693. cmd.expn_rules_offset =
  694. cmd.ipv4_rules_offset +
  695. (nat_table->table.table_entries * sizeof(struct ipa_nat_rule));
  696. cmd.index_offset =
  697. cmd.expn_rules_offset +
  698. (nat_table->table.expn_table_entries * sizeof(struct ipa_nat_rule));
  699. cmd.index_expn_offset =
  700. cmd.index_offset +
  701. (nat_table->index_table.table_entries * sizeof(struct ipa_nat_indx_tbl_rule));
  702. /*
  703. * Driverr/HW expected base table size to be power^2-1 due to H/W
  704. * hash calculation
  705. */
  706. cmd.table_entries =
  707. nat_table->table.table_entries - 1;
  708. cmd.expn_table_entries =
  709. nat_table->table.expn_table_entries;
  710. cmd.ip_addr = nat_table->public_addr;
  711. IPADBG("%s\n", ipa_ioc_v4_nat_init_as_str(&cmd, buf, sizeof(buf)));
  712. ret = ioctl(nat_cache_ptr->ipa_desc->fd, IPA_IOC_V4_INIT_NAT, &cmd);
  713. if (ret) {
  714. IPAERR("unable to post init cmd Error: %d IPA fd %d\n",
  715. ret, nat_cache_ptr->ipa_desc->fd);
  716. goto bail;
  717. }
  718. IPADBG("Posted IPA_IOC_V4_INIT_NAT to kernel successfully\n");
  719. bail:
  720. IPADBG("Out\n");
  721. return ret;
  722. }
  723. static void ipa_nati_copy_second_index_entry_to_head(
  724. struct ipa_nat_ip4_table_cache* nat_table,
  725. ipa_table_iterator* index_table_iterator,
  726. struct ipa_ioc_nat_dma_cmd* cmd)
  727. {
  728. uint16_t index;
  729. struct ipa_nat_rule* table;
  730. struct ipa_nat_indx_tbl_rule* index_table_rule =
  731. (struct ipa_nat_indx_tbl_rule*)index_table_iterator->next_entry;
  732. IPADBG("In\n");
  733. /*
  734. * The DMA command for field tbl_entry already added by the
  735. * index_table.ipa_table_create_delete_command()
  736. */
  737. ipa_table_add_dma_cmd(
  738. &nat_table->index_table,
  739. HELP_UPDATE_ENTRY,
  740. index_table_iterator->curr_entry,
  741. index_table_iterator->curr_index,
  742. index_table_rule->next_index,
  743. cmd);
  744. /* Change the indx_tbl_entry field in the related table rule */
  745. if (index_table_rule->tbl_entry < nat_table->table.table_entries) {
  746. index = index_table_rule->tbl_entry;
  747. table = (struct ipa_nat_rule*)nat_table->table.table_addr;
  748. } else {
  749. index = index_table_rule->tbl_entry - nat_table->table.table_entries;
  750. table = (struct ipa_nat_rule*)nat_table->table.expn_table_addr;
  751. }
  752. table[index].indx_tbl_entry = index_table_iterator->curr_index;
  753. IPADBG("Out\n");
  754. }
  755. /**
  756. * dst_hash() - Find the index into ipv4 base table
  757. * @public_ip: [in] public_ip
  758. * @trgt_ip: [in] Target IP address
  759. * @trgt_port: [in] Target port
  760. * @public_port: [in] Public port
  761. * @proto: [in] Protocol (TCP/IP)
  762. * @size: [in] size of the ipv4 base Table
  763. *
  764. * This hash method is used to find the hash index of new nat
  765. * entry into ipv4 base table. In case of zero index, the
  766. * new entry will be stored into N-1 index where N is size of
  767. * ipv4 base table
  768. *
  769. * Returns: >0 index into ipv4 base table, negative on failure
  770. */
  771. static uint16_t dst_hash(
  772. struct ipa_nat_cache* nat_cache_ptr,
  773. uint32_t public_ip,
  774. uint32_t trgt_ip,
  775. uint16_t trgt_port,
  776. uint16_t public_port,
  777. uint8_t proto,
  778. uint16_t size)
  779. {
  780. uint16_t hash =
  781. ((uint16_t)(trgt_ip)) ^
  782. ((uint16_t)(trgt_ip >> 16)) ^
  783. (trgt_port) ^
  784. (public_port) ^
  785. (proto);
  786. IPADBG("In\n");
  787. IPADBG("public_ip: 0x%08X public_port: 0x%04X\n", public_ip, public_port);
  788. IPADBG("target_ip: 0x%08X target_port: 0x%04X\n", trgt_ip, trgt_port);
  789. IPADBG("proto: 0x%02X size: 0x%04X\n", proto, size);
  790. if (nat_cache_ptr->ipa_desc->ver >= IPA_HW_v4_0)
  791. hash ^=
  792. ((uint16_t)(public_ip)) ^
  793. ((uint16_t)(public_ip >> 16));
  794. /*
  795. * The size passed to hash function expected be power^2-1, while
  796. * the actual size is power^2, actual_size = size + 1
  797. */
  798. hash = (hash & size);
  799. /*
  800. * If the hash resulted to zero then set it to maximum value as
  801. * zero is unused entry in nat tables
  802. */
  803. if (hash == 0) {
  804. hash = size;
  805. }
  806. IPADBG("dst_hash returning value: %d\n", hash);
  807. IPADBG("Out\n");
  808. return hash;
  809. }
  810. /**
  811. * src_hash() - Find the index into ipv4 index base table
  812. * @priv_ip: [in] Private IP address
  813. * @priv_port: [in] Private port
  814. * @trgt_ip: [in] Target IP address
  815. * @trgt_port: [in] Target Port
  816. * @proto: [in] Protocol (TCP/IP)
  817. * @size: [in] size of the ipv4 index base Table
  818. *
  819. * This hash method is used to find the hash index of new nat
  820. * entry into ipv4 index base table. In case of zero index, the
  821. * new entry will be stored into N-1 index where N is size of
  822. * ipv4 index base table
  823. *
  824. * Returns: >0 index into ipv4 index base table, negative on failure
  825. */
  826. static uint16_t src_hash(
  827. uint32_t priv_ip,
  828. uint16_t priv_port,
  829. uint32_t trgt_ip,
  830. uint16_t trgt_port,
  831. uint8_t proto,
  832. uint16_t size)
  833. {
  834. uint16_t hash =
  835. ((uint16_t)(priv_ip)) ^
  836. ((uint16_t)(priv_ip >> 16)) ^
  837. (priv_port) ^
  838. ((uint16_t)(trgt_ip)) ^
  839. ((uint16_t)(trgt_ip >> 16)) ^
  840. (trgt_port) ^
  841. (proto);
  842. IPADBG("In\n");
  843. IPADBG("private_ip: 0x%08X private_port: 0x%04X\n", priv_ip, priv_port);
  844. IPADBG(" target_ip: 0x%08X target_port: 0x%04X\n", trgt_ip, trgt_port);
  845. IPADBG("proto: 0x%02X size: 0x%04X\n", proto, size);
  846. /*
  847. * The size passed to hash function expected be power^2-1, while
  848. * the actual size is power^2, actual_size = size + 1
  849. */
  850. hash = (hash & size);
  851. /*
  852. * If the hash resulted to zero then set it to maximum value as
  853. * zero is unused entry in nat tables
  854. */
  855. if (hash == 0) {
  856. hash = size;
  857. }
  858. IPADBG("src_hash returning value: %d\n", hash);
  859. IPADBG("Out\n");
  860. return hash;
  861. }
  862. static int ipa_nati_post_ipv4_dma_cmd(
  863. struct ipa_nat_cache* nat_cache_ptr,
  864. struct ipa_ioc_nat_dma_cmd* cmd)
  865. {
  866. char buf[4096];
  867. int ret = 0;
  868. IPADBG("In\n");
  869. cmd->mem_type = nat_cache_ptr->nmi;
  870. IPADBG("%s\n", prep_ioc_nat_dma_cmd_4print(cmd, buf, sizeof(buf)));
  871. if (ioctl(nat_cache_ptr->ipa_desc->fd, IPA_IOC_TABLE_DMA_CMD, cmd)) {
  872. IPAERR("ioctl (IPA_IOC_TABLE_DMA_CMD) on fd %d has failed\n",
  873. nat_cache_ptr->ipa_desc->fd);
  874. ret = -EIO;
  875. goto bail;
  876. }
  877. IPADBG("Posted IPA_IOC_TABLE_DMA_CMD to kernel successfully\n");
  878. bail:
  879. IPADBG("Out\n");
  880. return ret;
  881. }
  882. /*
  883. * ----------------------------------------------------------------------------
  884. * API functions exposed to the upper layers
  885. * ----------------------------------------------------------------------------
  886. */
  887. int ipa_nati_modify_pdn(
  888. struct ipa_ioc_nat_pdn_entry *entry)
  889. {
  890. struct ipa_nat_cache* nat_cache_ptr;
  891. int ret = 0;
  892. IPADBG("In\n");
  893. nat_cache_ptr =
  894. (ipv4_nat_cache[IPA_NAT_MEM_IN_DDR].ipa_desc) ?
  895. &ipv4_nat_cache[IPA_NAT_MEM_IN_DDR] :
  896. &ipv4_nat_cache[IPA_NAT_MEM_IN_SRAM];
  897. if ( nat_cache_ptr->ipa_desc == NULL )
  898. {
  899. IPAERR("Uninitialized cache file descriptor\n");
  900. ret = -EIO;
  901. goto done;
  902. }
  903. if (entry->public_ip == 0)
  904. IPADBG("PDN %d public ip will be set to 0\n", entry->pdn_index);
  905. ret = ioctl(nat_cache_ptr->ipa_desc->fd, IPA_IOC_NAT_MODIFY_PDN, entry);
  906. if ( ret ) {
  907. IPAERR("unable to call modify pdn icotl\nindex %d, ip 0x%X, src_metdata 0x%X, dst_metadata 0x%X IPA fd %d\n",
  908. entry->pdn_index,
  909. entry->public_ip,
  910. entry->src_metadata,
  911. entry->dst_metadata,
  912. nat_cache_ptr->ipa_desc->fd);
  913. goto done;
  914. }
  915. pdns[entry->pdn_index].public_ip = entry->public_ip;
  916. pdns[entry->pdn_index].dst_metadata = entry->dst_metadata;
  917. pdns[entry->pdn_index].src_metadata = entry->src_metadata;
  918. IPADBG("posted IPA_IOC_NAT_MODIFY_PDN to kernel successfully and stored in cache\n index %d, ip 0x%X, src_metdata 0x%X, dst_metadata 0x%X\n",
  919. entry->pdn_index,
  920. entry->public_ip,
  921. entry->src_metadata,
  922. entry->dst_metadata);
  923. done:
  924. IPADBG("Out\n");
  925. return ret;
  926. }
  927. int ipa_nati_get_pdn_index(
  928. uint32_t public_ip,
  929. uint8_t *pdn_index)
  930. {
  931. int i = 0;
  932. for(i = 0; i < (IPA_MAX_PDN_NUM - 1); i++) {
  933. if(pdns[i].public_ip == public_ip) {
  934. IPADBG("ip 0x%X matches PDN index %d\n", public_ip, i);
  935. *pdn_index = i;
  936. return 0;
  937. }
  938. }
  939. IPAERR("ip 0x%X does not match any PDN\n", public_ip);
  940. return -EIO;
  941. }
  942. int ipa_nati_alloc_pdn(
  943. ipa_nat_pdn_entry *pdn_info,
  944. uint8_t *pdn_index)
  945. {
  946. ipa_nat_pdn_entry zero_test;
  947. struct ipa_ioc_nat_pdn_entry pdn_data;
  948. int i, ret;
  949. IPADBG("alloc PDN for ip 0x%x\n", pdn_info->public_ip);
  950. memset(&zero_test, 0, sizeof(zero_test));
  951. if(num_pdns >= (IPA_MAX_PDN_NUM - 1)) {
  952. IPAERR("exceeded max num of PDNs, num_pdns %d\n", num_pdns);
  953. return -EIO;
  954. }
  955. for(i = 0; i < (IPA_MAX_PDN_NUM - 1); i++) {
  956. if(pdns[i].public_ip == pdn_info->public_ip)
  957. {
  958. IPADBG("found the same pdn in index %d\n", i);
  959. *pdn_index = i;
  960. if((pdns[i].src_metadata != pdn_info->src_metadata) ||
  961. (pdns[i].dst_metadata != pdn_info->dst_metadata))
  962. {
  963. IPAERR("WARNING: metadata values don't match! [%d, %d], [%d, %d]\n\n",
  964. pdns[i].src_metadata, pdn_info->src_metadata,
  965. pdns[i].dst_metadata, pdn_info->dst_metadata);
  966. }
  967. return 0;
  968. }
  969. if(!memcmp((pdns + i), &zero_test, sizeof(ipa_nat_pdn_entry)))
  970. {
  971. IPADBG("found an empty pdn in index %d\n", i);
  972. break;
  973. }
  974. }
  975. if(i >= (IPA_MAX_PDN_NUM - 1))
  976. {
  977. IPAERR("couldn't find an empty entry while num is %d\n",
  978. num_pdns);
  979. return -EIO;
  980. }
  981. pdn_data.pdn_index = i;
  982. pdn_data.public_ip = pdn_info->public_ip;
  983. pdn_data.src_metadata = pdn_info->src_metadata;
  984. pdn_data.dst_metadata = pdn_info->dst_metadata;
  985. ret = ipa_nati_modify_pdn(&pdn_data);
  986. if(!ret)
  987. {
  988. num_pdns++;
  989. *pdn_index = i;
  990. IPADBG("modify num_pdns (%d)\n", num_pdns);
  991. }
  992. return ret;
  993. }
  994. int ipa_nati_get_pdn_cnt(void)
  995. {
  996. return num_pdns;
  997. }
  998. int ipa_nati_dealloc_pdn(
  999. uint8_t pdn_index)
  1000. {
  1001. ipa_nat_pdn_entry zero_test;
  1002. struct ipa_ioc_nat_pdn_entry pdn_data;
  1003. int ret;
  1004. IPADBG(" trying to deallocate PDN index %d\n", pdn_index);
  1005. if(!num_pdns)
  1006. {
  1007. IPAERR("pdn table is already empty\n");
  1008. return -EIO;
  1009. }
  1010. memset(&zero_test, 0, sizeof(zero_test));
  1011. if(!memcmp((pdns + pdn_index), &zero_test, sizeof(ipa_nat_pdn_entry)))
  1012. {
  1013. IPAERR("pdn entry is a zero entry\n");
  1014. return -EIO;
  1015. }
  1016. IPADBG("PDN in index %d has ip 0x%X\n", pdn_index, pdns[pdn_index].public_ip);
  1017. pdn_data.pdn_index = pdn_index;
  1018. pdn_data.src_metadata = 0;
  1019. pdn_data.dst_metadata = 0;
  1020. pdn_data.public_ip = 0;
  1021. ret = ipa_nati_modify_pdn(&pdn_data);
  1022. if(ret)
  1023. {
  1024. IPAERR("failed modifying PDN\n");
  1025. return -EIO;
  1026. }
  1027. memset((pdns + pdn_index), 0, sizeof(ipa_nat_pdn_entry));
  1028. num_pdns--;
  1029. IPADBG("successfully removed pdn from index %d num_pdns %d\n", pdn_index, num_pdns);
  1030. return 0;
  1031. }
  1032. /*
  1033. * ----------------------------------------------------------------------------
  1034. * Previously public API functions, but have been hijacked (in
  1035. * ipa_nat_statemach.c). The new definitions that replaced these, now
  1036. * call the functions below.
  1037. * ----------------------------------------------------------------------------
  1038. */
  1039. int ipa_NATI_post_ipv4_init_cmd(
  1040. uint32_t tbl_hdl )
  1041. {
  1042. enum ipa3_nat_mem_in nmi;
  1043. struct ipa_nat_cache* nat_cache_ptr;
  1044. struct ipa_nat_ip4_table_cache* nat_table;
  1045. int ret;
  1046. IPADBG("In\n");
  1047. BREAK_TBL_HDL(tbl_hdl, nmi, tbl_hdl);
  1048. if ( ! IPA_VALID_NAT_MEM_IN(nmi) ) {
  1049. IPAERR("Bad cache type argument passed\n");
  1050. ret = -EINVAL;
  1051. goto bail;
  1052. }
  1053. IPADBG("nmi(%s)\n", ipa3_nat_mem_in_as_str(nmi));
  1054. nat_cache_ptr = &ipv4_nat_cache[nmi];
  1055. if (pthread_mutex_lock(&nat_mutex)) {
  1056. IPAERR("unable to lock the nat mutex\n");
  1057. ret = -EINVAL;
  1058. goto bail;
  1059. }
  1060. if ( ! nat_cache_ptr->table_cnt ) {
  1061. IPAERR("No initialized table in NAT cache\n");
  1062. ret = -EINVAL;
  1063. goto unlock;
  1064. }
  1065. nat_table = &nat_cache_ptr->ip4_tbl[tbl_hdl - 1];
  1066. ret = ipa_nati_post_ipv4_init_cmd(
  1067. nat_cache_ptr,
  1068. nat_table,
  1069. tbl_hdl - 1,
  1070. true);
  1071. if (ret) {
  1072. IPAERR("unable to post nat_init command Error %d\n", ret);
  1073. goto unlock;
  1074. }
  1075. active_nat_cache_ptr = nat_cache_ptr;
  1076. unlock:
  1077. if (pthread_mutex_unlock(&nat_mutex)) {
  1078. IPAERR("unable to unlock the nat mutex\n");
  1079. ret = (ret) ? ret : -EPERM;
  1080. }
  1081. bail:
  1082. IPADBG("Out\n");
  1083. return ret;
  1084. }
  1085. /**
  1086. * ipa_NATI_add_ipv4_tbl() - Adds a new IPv4 NAT table
  1087. * @ct: [in] the desired cache type to use
  1088. * @public_ip_addr: [in] public IPv4 address
  1089. * @number_of_entries: [in] number of NAT entries
  1090. * @table_handle: [out] handle of new IPv4 NAT table
  1091. *
  1092. * This function creates new IPv4 NAT table and posts IPv4 NAT init command to HW
  1093. *
  1094. * Returns: 0 On Success, negative on failure
  1095. */
  1096. int ipa_NATI_add_ipv4_tbl(
  1097. enum ipa3_nat_mem_in nmi,
  1098. uint32_t public_ip_addr,
  1099. uint16_t number_of_entries,
  1100. uint32_t* tbl_hdl )
  1101. {
  1102. struct ipa_nat_cache* nat_cache_ptr;
  1103. struct ipa_nat_ip4_table_cache* nat_table;
  1104. int ret = 0;
  1105. IPADBG("In\n");
  1106. *tbl_hdl = 0;
  1107. if ( ! IPA_VALID_NAT_MEM_IN(nmi) ) {
  1108. IPAERR("Bad cache type argument passed\n");
  1109. ret = -EINVAL;
  1110. goto bail;
  1111. }
  1112. IPADBG("nmi(%s)\n", ipa3_nat_mem_in_as_str(nmi));
  1113. nat_cache_ptr = &ipv4_nat_cache[nmi];
  1114. if (pthread_mutex_lock(&nat_mutex)) {
  1115. IPAERR("unable to lock the nat mutex\n");
  1116. ret = -EINVAL;
  1117. goto bail;
  1118. }
  1119. nat_cache_ptr->nmi = nmi;
  1120. if (nat_cache_ptr->table_cnt >= IPA_NAT_MAX_IP4_TBLS) {
  1121. IPAERR(
  1122. "Can't add addition NAT table. Maximum %d tables allowed\n",
  1123. IPA_NAT_MAX_IP4_TBLS);
  1124. ret = -EINVAL;
  1125. goto unlock;
  1126. }
  1127. if ( ! nat_cache_ptr->ipa_desc ) {
  1128. nat_cache_ptr->ipa_desc = ipa_descriptor_open();
  1129. if ( nat_cache_ptr->ipa_desc == NULL ) {
  1130. IPAERR("failed to open IPA driver file descriptor\n");
  1131. ret = -EIO;
  1132. goto unlock;
  1133. }
  1134. }
  1135. nat_table = &nat_cache_ptr->ip4_tbl[nat_cache_ptr->table_cnt];
  1136. ret = ipa_nati_create_table(
  1137. nat_cache_ptr,
  1138. nat_table,
  1139. public_ip_addr,
  1140. number_of_entries,
  1141. nat_cache_ptr->table_cnt);
  1142. if (ret) {
  1143. IPAERR("unable to create nat table Error: %d\n", ret);
  1144. goto failed_create_table;
  1145. }
  1146. /*
  1147. * Initialize the ipa hw with nat table dimensions
  1148. */
  1149. ret = ipa_nati_post_ipv4_init_cmd(
  1150. nat_cache_ptr,
  1151. nat_table,
  1152. nat_cache_ptr->table_cnt,
  1153. false);
  1154. if (ret) {
  1155. IPAERR("unable to post nat_init command Error %d\n", ret);
  1156. goto failed_post_init_cmd;
  1157. }
  1158. active_nat_cache_ptr = nat_cache_ptr;
  1159. /*
  1160. * Store the initial public ip address in the cached pdn table
  1161. * this is backward compatible for pre IPAv4 versions, we will
  1162. * always use this ip as the single PDN address
  1163. */
  1164. pdns[0].public_ip = public_ip_addr;
  1165. num_pdns = 1;
  1166. nat_cache_ptr->table_cnt++;
  1167. /*
  1168. * Return table handle
  1169. */
  1170. *tbl_hdl = MAKE_TBL_HDL(nat_cache_ptr->table_cnt, nmi);
  1171. IPADBG("tbl_hdl value(0x%08X) num_pdns (%d)\n", *tbl_hdl, num_pdns);
  1172. goto unlock;
  1173. failed_post_init_cmd:
  1174. ipa_nati_destroy_table(nat_cache_ptr, nat_table);
  1175. failed_create_table:
  1176. if (!nat_cache_ptr->table_cnt) {
  1177. ipa_descriptor_close(nat_cache_ptr->ipa_desc);
  1178. nat_cache_ptr->ipa_desc = NULL;
  1179. }
  1180. unlock:
  1181. if (pthread_mutex_unlock(&nat_mutex)) {
  1182. IPAERR("unable to unlock the nat mutex\n");
  1183. ret = -EPERM;
  1184. goto bail;
  1185. }
  1186. bail:
  1187. IPADBG("Out\n");
  1188. return ret;
  1189. }
  1190. int ipa_NATI_del_ipv4_table(
  1191. uint32_t tbl_hdl )
  1192. {
  1193. enum ipa3_nat_mem_in nmi;
  1194. struct ipa_nat_cache* nat_cache_ptr;
  1195. struct ipa_nat_ip4_table_cache* nat_table;
  1196. int ret;
  1197. IPADBG("In\n");
  1198. BREAK_TBL_HDL(tbl_hdl, nmi, tbl_hdl);
  1199. if ( ! IPA_VALID_NAT_MEM_IN(nmi) ) {
  1200. IPAERR("Bad cache type argument passed\n");
  1201. ret = -EINVAL;
  1202. goto bail;
  1203. }
  1204. IPADBG("nmi(%s)\n", ipa3_nat_mem_in_as_str(nmi));
  1205. nat_cache_ptr = &ipv4_nat_cache[nmi];
  1206. nat_table = &nat_cache_ptr->ip4_tbl[tbl_hdl - 1];
  1207. if (pthread_mutex_lock(&nat_mutex)) {
  1208. IPAERR("unable to lock the nat mutex\n");
  1209. ret = -EINVAL;
  1210. goto bail;
  1211. }
  1212. if (! nat_table->mem_desc.valid) {
  1213. IPAERR("invalid table handle %d\n", tbl_hdl);
  1214. ret = -EINVAL;
  1215. goto unlock;
  1216. }
  1217. ret = ipa_nati_destroy_table(nat_cache_ptr, nat_table);
  1218. if (ret) {
  1219. IPAERR("unable to delete NAT table with handle %d\n", tbl_hdl);
  1220. goto unlock;
  1221. }
  1222. if (! --nat_cache_ptr->table_cnt) {
  1223. ipa_descriptor_close(nat_cache_ptr->ipa_desc);
  1224. nat_cache_ptr->ipa_desc = NULL;
  1225. }
  1226. unlock:
  1227. if (pthread_mutex_unlock(&nat_mutex)) {
  1228. IPAERR("unable to unlock the nat mutex\n");
  1229. ret = (ret) ? ret : -EPERM;
  1230. }
  1231. bail:
  1232. IPADBG("Out\n");
  1233. return ret;
  1234. }
  1235. int ipa_NATI_query_timestamp(
  1236. uint32_t tbl_hdl,
  1237. uint32_t rule_hdl,
  1238. uint32_t* time_stamp )
  1239. {
  1240. enum ipa3_nat_mem_in nmi;
  1241. struct ipa_nat_cache* nat_cache_ptr;
  1242. struct ipa_nat_ip4_table_cache* nat_table;
  1243. struct ipa_nat_rule* rule_ptr;
  1244. char buf[1024];
  1245. int ret;
  1246. IPADBG("In\n");
  1247. BREAK_TBL_HDL(tbl_hdl, nmi, tbl_hdl);
  1248. if ( ! IPA_VALID_NAT_MEM_IN(nmi) ) {
  1249. IPAERR("Bad cache type argument passed\n");
  1250. ret = -EINVAL;
  1251. goto bail;
  1252. }
  1253. IPADBG("nmi(%s)\n", ipa3_nat_mem_in_as_str(nmi));
  1254. nat_cache_ptr = &ipv4_nat_cache[nmi];
  1255. nat_table = &nat_cache_ptr->ip4_tbl[tbl_hdl - 1];
  1256. if (pthread_mutex_lock(&nat_mutex)) {
  1257. IPAERR("unable to lock the nat mutex\n");
  1258. ret = -EINVAL;
  1259. goto bail;
  1260. }
  1261. if ( ! nat_table->mem_desc.valid ) {
  1262. IPAERR("invalid table handle %d\n", tbl_hdl);
  1263. ret = -EINVAL;
  1264. goto unlock;
  1265. }
  1266. ret = ipa_table_get_entry(
  1267. &nat_table->table,
  1268. rule_hdl,
  1269. (void**) &rule_ptr,
  1270. NULL);
  1271. if (ret) {
  1272. IPAERR("Unable to retrive the entry with "
  1273. "handle=%u in NAT table with handle=0x%08X\n",
  1274. rule_hdl, tbl_hdl);
  1275. goto unlock;
  1276. }
  1277. IPADBG("rule_hdl(0x%08X) -> %s\n",
  1278. rule_hdl,
  1279. prep_nat_rule_4print(rule_ptr, buf, sizeof(buf)));
  1280. *time_stamp = rule_ptr->time_stamp;
  1281. unlock:
  1282. if (pthread_mutex_unlock(&nat_mutex)) {
  1283. IPAERR("unable to unlock the nat mutex\n");
  1284. ret = (ret) ? ret : -EPERM;
  1285. }
  1286. bail:
  1287. IPADBG("Out\n");
  1288. return ret;
  1289. }
  1290. int ipa_NATI_add_ipv4_rule(
  1291. uint32_t tbl_hdl,
  1292. const ipa_nat_ipv4_rule* clnt_rule,
  1293. uint32_t* rule_hdl)
  1294. {
  1295. uint32_t cmd_sz =
  1296. sizeof(struct ipa_ioc_nat_dma_cmd) +
  1297. (MAX_DMA_ENTRIES_FOR_ADD * sizeof(struct ipa_ioc_nat_dma_one));
  1298. char cmd_buf[cmd_sz];
  1299. struct ipa_ioc_nat_dma_cmd* cmd =
  1300. (struct ipa_ioc_nat_dma_cmd*) cmd_buf;
  1301. enum ipa3_nat_mem_in nmi;
  1302. struct ipa_nat_cache* nat_cache_ptr;
  1303. struct ipa_nat_ip4_table_cache* nat_table;
  1304. struct ipa_nat_rule* rule;
  1305. uint16_t new_entry_index;
  1306. uint16_t new_index_tbl_entry_index;
  1307. uint32_t new_entry_handle;
  1308. char buf[1024];
  1309. int ret = 0;
  1310. IPADBG("In\n");
  1311. memset(cmd_buf, 0, sizeof(cmd_buf));
  1312. if ( ! VALID_TBL_HDL(tbl_hdl) ||
  1313. ! clnt_rule ||
  1314. ! rule_hdl )
  1315. {
  1316. IPAERR("Bad arg: tbl_hdl(0x%08X) and/or clnt_rule(%p) and/or rule_hdl(%p)\n",
  1317. tbl_hdl, clnt_rule, rule_hdl);
  1318. ret = -EINVAL;
  1319. goto done;
  1320. }
  1321. *rule_hdl = 0;
  1322. IPADBG("tbl_hdl(0x%08X)\n", tbl_hdl);
  1323. BREAK_TBL_HDL(tbl_hdl, nmi, tbl_hdl);
  1324. if ( ! IPA_VALID_NAT_MEM_IN(nmi) ) {
  1325. IPAERR("Bad cache type argument passed\n");
  1326. ret = -EINVAL;
  1327. goto done;
  1328. }
  1329. IPADBG("tbl_hdl(0x%08X) nmi(%s) %s\n",
  1330. tbl_hdl,
  1331. ipa3_nat_mem_in_as_str(nmi),
  1332. prep_nat_ipv4_rule_4print(clnt_rule, buf, sizeof(buf)));
  1333. nat_cache_ptr = &ipv4_nat_cache[nmi];
  1334. nat_table = &nat_cache_ptr->ip4_tbl[tbl_hdl - 1];
  1335. if (clnt_rule->protocol == IPAHAL_NAT_INVALID_PROTOCOL) {
  1336. IPAERR("invalid parameter protocol=%d\n", clnt_rule->protocol);
  1337. ret = -EINVAL;
  1338. goto done;
  1339. }
  1340. /*
  1341. * Verify that the rule's PDN is valid
  1342. */
  1343. if (clnt_rule->pdn_index >= IPA_MAX_PDN_NUM ||
  1344. pdns[clnt_rule->pdn_index].public_ip == 0) {
  1345. IPAERR("invalid parameters, pdn index %d, public ip = 0x%X\n",
  1346. clnt_rule->pdn_index, pdns[clnt_rule->pdn_index].public_ip);
  1347. ret = -EINVAL;
  1348. goto done;
  1349. }
  1350. if (pthread_mutex_lock(&nat_mutex)) {
  1351. IPAERR("unable to lock the nat mutex\n");
  1352. ret = -EINVAL;
  1353. goto done;
  1354. }
  1355. if (! nat_table->mem_desc.valid) {
  1356. IPAERR("invalid table handle %d\n", tbl_hdl);
  1357. ret = -EINVAL;
  1358. goto unlock;
  1359. }
  1360. /* src_only */
  1361. if (clnt_rule->src_only) {
  1362. new_entry_index = dst_hash(
  1363. nat_cache_ptr,
  1364. pdns[clnt_rule->pdn_index].public_ip,
  1365. clnt_rule->target_ip,
  1366. clnt_rule->target_port,
  1367. clnt_rule->public_port,
  1368. clnt_rule->protocol,
  1369. nat_table->table.table_entries - 1) + Hash_token;
  1370. new_entry_index = (new_entry_index & (nat_table->table.table_entries - 1));
  1371. if (new_entry_index == 0) {
  1372. new_entry_index = nat_table->table.table_entries - 1;
  1373. }
  1374. Hash_token++;
  1375. } else {
  1376. new_entry_index = dst_hash(
  1377. nat_cache_ptr,
  1378. pdns[clnt_rule->pdn_index].public_ip,
  1379. clnt_rule->target_ip,
  1380. clnt_rule->target_port,
  1381. clnt_rule->public_port,
  1382. clnt_rule->protocol,
  1383. nat_table->table.table_entries - 1);
  1384. }
  1385. ret = ipa_table_add_entry(
  1386. &nat_table->table,
  1387. (void*) clnt_rule,
  1388. &new_entry_index,
  1389. &new_entry_handle,
  1390. cmd);
  1391. if (ret) {
  1392. IPAERR("Failed to add a new NAT entry\n");
  1393. goto unlock;
  1394. }
  1395. /* dst_only */
  1396. if (clnt_rule->dst_only) {
  1397. new_index_tbl_entry_index =
  1398. src_hash(clnt_rule->private_ip,
  1399. clnt_rule->private_port,
  1400. clnt_rule->target_ip,
  1401. clnt_rule->target_port,
  1402. clnt_rule->protocol,
  1403. nat_table->table.table_entries - 1) + Hash_token;
  1404. new_index_tbl_entry_index = (new_index_tbl_entry_index & (nat_table->table.table_entries - 1));
  1405. if (new_index_tbl_entry_index == 0) {
  1406. new_index_tbl_entry_index = nat_table->table.table_entries - 1;
  1407. }
  1408. Hash_token++;
  1409. } else {
  1410. new_index_tbl_entry_index =
  1411. src_hash(clnt_rule->private_ip,
  1412. clnt_rule->private_port,
  1413. clnt_rule->target_ip,
  1414. clnt_rule->target_port,
  1415. clnt_rule->protocol,
  1416. nat_table->table.table_entries - 1);
  1417. }
  1418. ret = ipa_table_add_entry(
  1419. &nat_table->index_table,
  1420. (void*) &new_entry_index,
  1421. &new_index_tbl_entry_index,
  1422. NULL,
  1423. cmd);
  1424. if (ret) {
  1425. IPAERR("failed to add a new NAT index entry\n");
  1426. goto fail_add_index_entry;
  1427. }
  1428. rule = ipa_table_get_entry_by_index(
  1429. &nat_table->table,
  1430. new_entry_index);
  1431. if (rule == NULL) {
  1432. IPAERR("Failed to retrieve the entry in index %d for NAT table with handle=%d\n",
  1433. new_entry_index, tbl_hdl);
  1434. ret = -EPERM;
  1435. goto bail;
  1436. }
  1437. rule->indx_tbl_entry = new_index_tbl_entry_index;
  1438. rule->redirect = clnt_rule->redirect;
  1439. rule->enable = clnt_rule->enable;
  1440. rule->time_stamp = clnt_rule->time_stamp;
  1441. IPADBG("new entry:%d, new index entry: %d\n",
  1442. new_entry_index, new_index_tbl_entry_index);
  1443. IPADBG("rule_hdl(0x%08X) -> %s\n",
  1444. new_entry_handle,
  1445. prep_nat_rule_4print(rule, buf, sizeof(buf)));
  1446. ret = ipa_nati_post_ipv4_dma_cmd(nat_cache_ptr, cmd);
  1447. if (ret) {
  1448. IPAERR("unable to post dma command\n");
  1449. goto bail;
  1450. }
  1451. if (pthread_mutex_unlock(&nat_mutex)) {
  1452. IPAERR("unable to unlock the nat mutex\n");
  1453. ret = -EPERM;
  1454. goto done;
  1455. }
  1456. *rule_hdl = new_entry_handle;
  1457. IPADBG("rule_hdl value(%u)\n", *rule_hdl);
  1458. goto done;
  1459. bail:
  1460. ipa_table_erase_entry(&nat_table->index_table, new_index_tbl_entry_index);
  1461. fail_add_index_entry:
  1462. ipa_table_erase_entry(&nat_table->table, new_entry_index);
  1463. unlock:
  1464. if (pthread_mutex_unlock(&nat_mutex))
  1465. IPAERR("unable to unlock the nat mutex\n");
  1466. done:
  1467. IPADBG("Out\n");
  1468. return ret;
  1469. }
  1470. int ipa_NATI_del_ipv4_rule(
  1471. uint32_t tbl_hdl,
  1472. uint32_t rule_hdl )
  1473. {
  1474. uint32_t cmd_sz =
  1475. sizeof(struct ipa_ioc_nat_dma_cmd) +
  1476. (MAX_DMA_ENTRIES_FOR_DEL * sizeof(struct ipa_ioc_nat_dma_one));
  1477. char cmd_buf[cmd_sz];
  1478. struct ipa_ioc_nat_dma_cmd* cmd =
  1479. (struct ipa_ioc_nat_dma_cmd*) cmd_buf;
  1480. enum ipa3_nat_mem_in nmi;
  1481. struct ipa_nat_cache* nat_cache_ptr;
  1482. struct ipa_nat_ip4_table_cache* nat_table;
  1483. struct ipa_nat_rule* table_rule;
  1484. struct ipa_nat_indx_tbl_rule* index_table_rule;
  1485. ipa_table_iterator table_iterator;
  1486. ipa_table_iterator index_table_iterator;
  1487. uint16_t index;
  1488. char buf[1024];
  1489. int ret = 0;
  1490. IPADBG("In\n");
  1491. memset(cmd_buf, 0, sizeof(cmd_buf));
  1492. IPADBG("tbl_hdl(0x%08X) rule_hdl(%u)\n", tbl_hdl, rule_hdl);
  1493. BREAK_TBL_HDL(tbl_hdl, nmi, tbl_hdl);
  1494. if ( ! IPA_VALID_NAT_MEM_IN(nmi) ) {
  1495. IPAERR("Bad cache type argument passed\n");
  1496. ret = -EINVAL;
  1497. goto done;
  1498. }
  1499. IPADBG("nmi(%s)\n", ipa3_nat_mem_in_as_str(nmi));
  1500. nat_cache_ptr = &ipv4_nat_cache[nmi];
  1501. nat_table = &nat_cache_ptr->ip4_tbl[tbl_hdl - 1];
  1502. if (pthread_mutex_lock(&nat_mutex)) {
  1503. IPAERR("Unable to lock the nat mutex\n");
  1504. ret = -EINVAL;
  1505. goto done;
  1506. }
  1507. if (! nat_table->mem_desc.valid) {
  1508. IPAERR("Invalid table handle 0x%08X\n", tbl_hdl);
  1509. ret = -EINVAL;
  1510. goto unlock;
  1511. }
  1512. ret = ipa_table_get_entry(
  1513. &nat_table->table,
  1514. rule_hdl,
  1515. (void**) &table_rule,
  1516. &index);
  1517. if (ret) {
  1518. IPAERR("Unable to retrive the entry with rule_hdl=%u\n", rule_hdl);
  1519. goto unlock;
  1520. }
  1521. IPADBG("rule_hdl(0x%08X) -> %s\n",
  1522. rule_hdl,
  1523. prep_nat_rule_4print(table_rule, buf, sizeof(buf)));
  1524. ret = ipa_table_iterator_init(
  1525. &table_iterator,
  1526. &nat_table->table,
  1527. table_rule,
  1528. index);
  1529. if (ret) {
  1530. IPAERR("Unable to create iterator which points to the "
  1531. "entry %u in NAT table with handle=0x%08X\n",
  1532. index, tbl_hdl);
  1533. goto unlock;
  1534. }
  1535. index = table_rule->indx_tbl_entry;
  1536. index_table_rule = (struct ipa_nat_indx_tbl_rule*)
  1537. ipa_table_get_entry_by_index(&nat_table->index_table, index);
  1538. if (index_table_rule == NULL) {
  1539. IPAERR("Unable to retrieve the entry in index %u "
  1540. "in NAT index table with handle=0x%08X\n",
  1541. index, tbl_hdl);
  1542. ret = -EPERM;
  1543. goto unlock;
  1544. }
  1545. ret = ipa_table_iterator_init(
  1546. &index_table_iterator,
  1547. &nat_table->index_table,
  1548. index_table_rule,
  1549. index);
  1550. if (ret) {
  1551. IPAERR("Unable to create iterator which points to the "
  1552. "entry %u in NAT index table with handle=0x%08X\n",
  1553. index, tbl_hdl);
  1554. goto unlock;
  1555. }
  1556. ipa_table_create_delete_command(
  1557. &nat_table->index_table,
  1558. cmd,
  1559. &index_table_iterator);
  1560. if (ipa_table_iterator_is_head_with_tail(&index_table_iterator)) {
  1561. ipa_nati_copy_second_index_entry_to_head(
  1562. nat_table, &index_table_iterator, cmd);
  1563. /*
  1564. * Iterate to the next entry which should be deleted
  1565. */
  1566. ret = ipa_table_iterator_next(
  1567. &index_table_iterator, &nat_table->index_table);
  1568. if (ret) {
  1569. IPAERR("Unable to move the iterator to the next entry "
  1570. "(points to the entry %u in NAT index table)\n",
  1571. index);
  1572. goto unlock;
  1573. }
  1574. }
  1575. ipa_table_create_delete_command(
  1576. &nat_table->table,
  1577. cmd,
  1578. &table_iterator);
  1579. ret = ipa_nati_post_ipv4_dma_cmd(nat_cache_ptr, cmd);
  1580. if (ret) {
  1581. IPAERR("Unable to post dma command\n");
  1582. goto unlock;
  1583. }
  1584. if (! ipa_table_iterator_is_head_with_tail(&table_iterator)) {
  1585. /* The entry can be deleted */
  1586. uint8_t is_prev_empty =
  1587. (table_iterator.prev_entry != NULL &&
  1588. ((struct ipa_nat_rule*)table_iterator.prev_entry)->protocol ==
  1589. IPAHAL_NAT_INVALID_PROTOCOL);
  1590. ipa_table_delete_entry(
  1591. &nat_table->table, &table_iterator, is_prev_empty);
  1592. }
  1593. ipa_table_delete_entry(
  1594. &nat_table->index_table,
  1595. &index_table_iterator,
  1596. FALSE);
  1597. if (index_table_iterator.curr_index >= nat_table->index_table.table_entries)
  1598. nat_table->index_expn_table_meta[
  1599. index_table_iterator.curr_index - nat_table->index_table.table_entries].
  1600. prev_index = IPA_TABLE_INVALID_ENTRY;
  1601. unlock:
  1602. if (pthread_mutex_unlock(&nat_mutex)) {
  1603. IPAERR("Unable to unlock the nat mutex\n");
  1604. ret = (ret) ? ret : -EPERM;
  1605. }
  1606. done:
  1607. IPADBG("Out\n");
  1608. return ret;
  1609. }
  1610. /*
  1611. * ----------------------------------------------------------------------------
  1612. * New function to get sram size.
  1613. * ----------------------------------------------------------------------------
  1614. */
  1615. int ipa_nati_get_sram_size(
  1616. uint32_t* size_ptr)
  1617. {
  1618. struct ipa_nat_cache* nat_cache_ptr =
  1619. &ipv4_nat_cache[IPA_NAT_MEM_IN_SRAM];
  1620. struct ipa_nat_in_sram_info nat_sram_info;
  1621. int ret;
  1622. IPADBG("In\n");
  1623. if (pthread_mutex_lock(&nat_mutex)) {
  1624. IPAERR("unable to lock the nat mutex\n");
  1625. ret = -EINVAL;
  1626. goto bail;
  1627. }
  1628. if ( ! nat_cache_ptr->ipa_desc ) {
  1629. nat_cache_ptr->ipa_desc = ipa_descriptor_open();
  1630. if ( nat_cache_ptr->ipa_desc == NULL ) {
  1631. IPAERR("failed to open IPA driver file descriptor\n");
  1632. ret = -EIO;
  1633. goto unlock;
  1634. }
  1635. }
  1636. memset(&nat_sram_info, 0, sizeof(nat_sram_info));
  1637. ret = ioctl(nat_cache_ptr->ipa_desc->fd,
  1638. IPA_IOC_GET_NAT_IN_SRAM_INFO,
  1639. &nat_sram_info);
  1640. if (ret) {
  1641. IPAERR("NAT_IN_SRAM_INFO ioctl failure %d on IPA fd %d\n",
  1642. ret, nat_cache_ptr->ipa_desc->fd);
  1643. goto unlock;
  1644. }
  1645. if ( (*size_ptr = nat_sram_info.sram_mem_available_for_nat) == 0 )
  1646. {
  1647. IPAERR("sram_mem_available_for_nat is zero\n");
  1648. ret = -EINVAL;
  1649. goto unlock;
  1650. }
  1651. unlock:
  1652. if (pthread_mutex_unlock(&nat_mutex)) {
  1653. IPAERR("unable to unlock the nat mutex\n");
  1654. ret = (ret) ? ret : -EPERM;
  1655. }
  1656. bail:
  1657. IPADBG("Out\n");
  1658. return ret;
  1659. }
  1660. /*
  1661. * ----------------------------------------------------------------------------
  1662. * Utility functions
  1663. * ----------------------------------------------------------------------------
  1664. */
  1665. static int print_nat_rule(
  1666. ipa_table* table_ptr,
  1667. uint32_t rule_hdl,
  1668. void* record_ptr,
  1669. uint16_t record_index,
  1670. void* meta_record_ptr,
  1671. uint16_t meta_record_index,
  1672. void* arb_data_ptr )
  1673. {
  1674. enum ipa3_nat_mem_in nmi;
  1675. uint8_t is_expn_tbl;
  1676. uint16_t rule_index;
  1677. char buf[1024];
  1678. struct ipa_nat_rule* rule_ptr =
  1679. (struct ipa_nat_rule*) record_ptr;
  1680. if ( rule_ptr->protocol == IPA_NAT_INVALID_PROTO_FIELD_VALUE_IN_RULE )
  1681. {
  1682. goto bail;
  1683. }
  1684. BREAK_RULE_HDL(table_ptr, rule_hdl, nmi, is_expn_tbl, rule_index);
  1685. printf(" %s %s (0x%04X) (0x%08X) -> %s\n",
  1686. (table_ptr->nmi == IPA_NAT_MEM_IN_DDR) ? "DDR" : "SRAM",
  1687. (is_expn_tbl) ? "EXP " : "BASE",
  1688. record_index,
  1689. rule_hdl,
  1690. prep_nat_rule_4print(rule_ptr, buf, sizeof(buf)));
  1691. fflush(stdout);
  1692. *((bool*) arb_data_ptr) = false;
  1693. bail:
  1694. return 0;
  1695. }
  1696. static int print_meta_data(
  1697. ipa_table* table_ptr,
  1698. uint32_t rule_hdl,
  1699. void* record_ptr,
  1700. uint16_t record_index,
  1701. void* meta_record_ptr,
  1702. uint16_t meta_record_index,
  1703. void* arb_data_ptr )
  1704. {
  1705. struct ipa_nat_indx_tbl_rule* index_entry =
  1706. (struct ipa_nat_indx_tbl_rule *) record_ptr;
  1707. struct ipa_nat_indx_tbl_meta_info* mi_ptr =
  1708. (struct ipa_nat_indx_tbl_meta_info*) meta_record_ptr;
  1709. enum ipa3_nat_mem_in nmi;
  1710. uint8_t is_expn_tbl;
  1711. uint16_t rule_index;
  1712. BREAK_RULE_HDL(table_ptr, rule_hdl, nmi, is_expn_tbl, rule_index);
  1713. if ( mi_ptr )
  1714. {
  1715. printf(" %s %s Entry_Index=0x%04X Table_Entry=0x%04X -> "
  1716. "Prev_Index=0x%04X Next_Index=0x%04X\n",
  1717. (table_ptr->nmi == IPA_NAT_MEM_IN_DDR) ? "DDR" : "SRAM",
  1718. (is_expn_tbl) ? "EXP " : "BASE",
  1719. record_index,
  1720. index_entry->tbl_entry,
  1721. mi_ptr->prev_index,
  1722. index_entry->next_index);
  1723. }
  1724. else
  1725. {
  1726. printf(" %s %s Entry_Index=0x%04X Table_Entry=0x%04X -> "
  1727. "Prev_Index=0xXXXX Next_Index=0x%04X\n",
  1728. (table_ptr->nmi == IPA_NAT_MEM_IN_DDR) ? "DDR" : "SRAM",
  1729. (is_expn_tbl) ? "EXP " : "BASE",
  1730. record_index,
  1731. index_entry->tbl_entry,
  1732. index_entry->next_index);
  1733. }
  1734. fflush(stdout);
  1735. *((bool*) arb_data_ptr) = false;
  1736. return 0;
  1737. }
  1738. void ipa_nat_dump_ipv4_table(
  1739. uint32_t tbl_hdl )
  1740. {
  1741. bool empty;
  1742. if (pthread_mutex_lock(&nat_mutex)) {
  1743. IPAERR("unable to lock the nat mutex\n");
  1744. return;
  1745. }
  1746. printf("\nIPv4 active rules:\n");
  1747. empty = true;
  1748. ipa_nati_walk_ipv4_tbl(tbl_hdl, USE_NAT_TABLE, print_nat_rule, &empty);
  1749. if ( empty )
  1750. {
  1751. printf(" Empty\n");
  1752. }
  1753. printf("\nExpansion Index Table Meta Data:\n");
  1754. empty = true;
  1755. ipa_nati_walk_ipv4_tbl(tbl_hdl, USE_INDEX_TABLE, print_meta_data, &empty);
  1756. if ( empty )
  1757. {
  1758. printf(" Empty\n");
  1759. }
  1760. printf("\n");
  1761. if (pthread_mutex_unlock(&nat_mutex)) {
  1762. IPAERR("unable to unlock the nat mutex\n");
  1763. }
  1764. }
  1765. int ipa_NATI_clear_ipv4_tbl(
  1766. uint32_t tbl_hdl )
  1767. {
  1768. enum ipa3_nat_mem_in nmi;
  1769. struct ipa_nat_cache* nat_cache_ptr;
  1770. struct ipa_nat_ip4_table_cache* nat_table;
  1771. int ret = 0;
  1772. IPADBG("In\n");
  1773. BREAK_TBL_HDL(tbl_hdl, nmi, tbl_hdl);
  1774. if ( ! IPA_VALID_NAT_MEM_IN(nmi) ) {
  1775. IPAERR("Bad cache type argument passed\n");
  1776. ret = -EINVAL;
  1777. goto bail;
  1778. }
  1779. IPADBG("nmi(%s)\n", ipa3_nat_mem_in_as_str(nmi));
  1780. nat_cache_ptr = &ipv4_nat_cache[nmi];
  1781. if (pthread_mutex_lock(&nat_mutex)) {
  1782. IPAERR("unable to lock the nat mutex\n");
  1783. ret = -EINVAL;
  1784. goto bail;
  1785. }
  1786. if ( ! nat_cache_ptr->table_cnt ) {
  1787. IPAERR("No initialized table in NAT cache\n");
  1788. ret = -EINVAL;
  1789. goto unlock;
  1790. }
  1791. nat_table = &nat_cache_ptr->ip4_tbl[tbl_hdl - 1];
  1792. ipa_table_reset(&nat_table->table);
  1793. nat_table->table.cur_tbl_cnt =
  1794. nat_table->table.cur_expn_tbl_cnt = 0;
  1795. ipa_table_reset(&nat_table->index_table);
  1796. nat_table->index_table.cur_tbl_cnt =
  1797. nat_table->index_table.cur_expn_tbl_cnt = 0;
  1798. unlock:
  1799. if (pthread_mutex_unlock(&nat_mutex)) {
  1800. IPAERR("unable to unlock the nat mutex\n");
  1801. ret = (ret) ? ret : -EPERM;
  1802. }
  1803. bail:
  1804. IPADBG("Out\n");
  1805. return ret;
  1806. }
  1807. int ipa_nati_copy_ipv4_tbl(
  1808. uint32_t src_tbl_hdl,
  1809. uint32_t dst_tbl_hdl,
  1810. ipa_table_walk_cb copy_cb )
  1811. {
  1812. int ret = 0;
  1813. IPADBG("In\n");
  1814. if ( ! copy_cb )
  1815. {
  1816. IPAERR("copy_cb is null\n");
  1817. ret = -EINVAL;
  1818. goto bail;
  1819. }
  1820. if (pthread_mutex_lock(&nat_mutex))
  1821. {
  1822. IPAERR("unable to lock the nat mutex\n");
  1823. ret = -EINVAL;
  1824. goto bail;
  1825. }
  1826. /*
  1827. * Clear the destination table...
  1828. */
  1829. ret = ipa_NATI_clear_ipv4_tbl(dst_tbl_hdl);
  1830. if ( ret == 0 )
  1831. {
  1832. /*
  1833. * Now walk the source table and pass the valid records to the
  1834. * user's copy callback...
  1835. */
  1836. ret = ipa_NATI_walk_ipv4_tbl(
  1837. src_tbl_hdl, USE_NAT_TABLE, copy_cb, dst_tbl_hdl);
  1838. if ( ret != 0 )
  1839. {
  1840. IPAERR("ipa_table_walk returned non-zero (%d)\n", ret);
  1841. goto unlock;
  1842. }
  1843. }
  1844. unlock:
  1845. if (pthread_mutex_unlock(&nat_mutex))
  1846. {
  1847. IPAERR("unable to unlock the nat mutex\n");
  1848. ret = (ret) ? ret : -EPERM;
  1849. }
  1850. bail:
  1851. IPADBG("Out\n");
  1852. return ret;
  1853. }
  1854. int ipa_NATI_walk_ipv4_tbl(
  1855. uint32_t tbl_hdl,
  1856. WhichTbl2Use which,
  1857. ipa_table_walk_cb walk_cb,
  1858. void* arb_data_ptr )
  1859. {
  1860. enum ipa3_nat_mem_in nmi;
  1861. uint32_t broken_tbl_hdl;
  1862. struct ipa_nat_cache* nat_cache_ptr;
  1863. struct ipa_nat_ip4_table_cache* nat_table;
  1864. ipa_table* ipa_tbl_ptr;
  1865. int ret = 0;
  1866. IPADBG("In\n");
  1867. if ( ! VALID_TBL_HDL(tbl_hdl) ||
  1868. ! VALID_WHICHTBL2USE(which) ||
  1869. ! walk_cb )
  1870. {
  1871. IPAERR("Bad arg: tbl_hdl(0x%08X) and/or WhichTbl2Use(%u) and/or walk_cb(%p)\n",
  1872. tbl_hdl, which, walk_cb);
  1873. ret = -EINVAL;
  1874. goto bail;
  1875. }
  1876. if ( pthread_mutex_lock(&nat_mutex) )
  1877. {
  1878. IPAERR("unable to lock the nat mutex\n");
  1879. ret = -EINVAL;
  1880. goto bail;
  1881. }
  1882. /*
  1883. * Now walk the table and pass the valid records to the user's
  1884. * walk callback...
  1885. */
  1886. BREAK_TBL_HDL(tbl_hdl, nmi, broken_tbl_hdl);
  1887. if ( ! IPA_VALID_NAT_MEM_IN(nmi) )
  1888. {
  1889. IPAERR("Bad cache type argument passed\n");
  1890. ret = -EINVAL;
  1891. goto unlock;
  1892. }
  1893. nat_cache_ptr = &ipv4_nat_cache[nmi];
  1894. if ( ! nat_cache_ptr->table_cnt )
  1895. {
  1896. IPAERR("No initialized table in NAT cache\n");
  1897. ret = -EINVAL;
  1898. goto unlock;
  1899. }
  1900. nat_table = &nat_cache_ptr->ip4_tbl[broken_tbl_hdl - 1];
  1901. ipa_tbl_ptr =
  1902. (which == USE_NAT_TABLE) ?
  1903. &nat_table->table :
  1904. &nat_table->index_table;
  1905. ret = ipa_table_walk(ipa_tbl_ptr, 0, WHEN_SLOT_FILLED, walk_cb, arb_data_ptr);
  1906. if ( ret != 0 )
  1907. {
  1908. IPAERR("ipa_table_walk returned non-zero (%d)\n", ret);
  1909. goto unlock;
  1910. }
  1911. unlock:
  1912. if ( pthread_mutex_unlock(&nat_mutex) )
  1913. {
  1914. IPAERR("unable to unlock the nat mutex\n");
  1915. ret = (ret) ? ret : -EPERM;
  1916. }
  1917. bail:
  1918. IPADBG("Out\n");
  1919. return ret;
  1920. }
  1921. typedef struct
  1922. {
  1923. WhichTbl2Use which;
  1924. uint32_t tot_for_avg;
  1925. ipa_nati_tbl_stats* stats_ptr;
  1926. } chain_stat_help;
  1927. static int gen_chain_stats(
  1928. ipa_table* table_ptr,
  1929. uint32_t rule_hdl,
  1930. void* record_ptr,
  1931. uint16_t record_index,
  1932. void* meta_record_ptr,
  1933. uint16_t meta_record_index,
  1934. void* arb_data_ptr )
  1935. {
  1936. chain_stat_help* csh_ptr = (chain_stat_help*) arb_data_ptr;
  1937. enum ipa3_nat_mem_in nmi;
  1938. uint8_t is_expn_tbl;
  1939. uint16_t rule_index;
  1940. uint32_t chain_len = 0;
  1941. BREAK_RULE_HDL(table_ptr, rule_hdl, nmi, is_expn_tbl, rule_index);
  1942. if ( is_expn_tbl )
  1943. {
  1944. return 1;
  1945. }
  1946. if ( csh_ptr->which == USE_NAT_TABLE )
  1947. {
  1948. struct ipa_nat_rule* list_elem_ptr =
  1949. (struct ipa_nat_rule*) record_ptr;
  1950. if ( list_elem_ptr->next_index )
  1951. {
  1952. chain_len = 1;
  1953. while ( list_elem_ptr->next_index )
  1954. {
  1955. chain_len++;
  1956. list_elem_ptr = GOTO_REC(table_ptr, list_elem_ptr->next_index);
  1957. }
  1958. }
  1959. }
  1960. else
  1961. {
  1962. struct ipa_nat_indx_tbl_rule* list_elem_ptr =
  1963. (struct ipa_nat_indx_tbl_rule*) record_ptr;
  1964. if ( list_elem_ptr->next_index )
  1965. {
  1966. chain_len = 1;
  1967. while ( list_elem_ptr->next_index )
  1968. {
  1969. chain_len++;
  1970. list_elem_ptr = GOTO_REC(table_ptr, list_elem_ptr->next_index);
  1971. }
  1972. }
  1973. }
  1974. if ( chain_len )
  1975. {
  1976. csh_ptr->stats_ptr->tot_chains += 1;
  1977. csh_ptr->tot_for_avg += chain_len;
  1978. if ( csh_ptr->stats_ptr->min_chain_len == 0 )
  1979. {
  1980. csh_ptr->stats_ptr->min_chain_len = chain_len;
  1981. }
  1982. else
  1983. {
  1984. csh_ptr->stats_ptr->min_chain_len =
  1985. min(csh_ptr->stats_ptr->min_chain_len, chain_len);
  1986. }
  1987. csh_ptr->stats_ptr->max_chain_len =
  1988. max(csh_ptr->stats_ptr->max_chain_len, chain_len);
  1989. }
  1990. return 0;
  1991. }
  1992. int ipa_NATI_ipv4_tbl_stats(
  1993. uint32_t tbl_hdl,
  1994. ipa_nati_tbl_stats* nat_stats_ptr,
  1995. ipa_nati_tbl_stats* idx_stats_ptr )
  1996. {
  1997. enum ipa3_nat_mem_in nmi;
  1998. uint32_t broken_tbl_hdl;
  1999. struct ipa_nat_cache* nat_cache_ptr;
  2000. struct ipa_nat_ip4_table_cache* nat_table;
  2001. ipa_table* ipa_tbl_ptr;
  2002. chain_stat_help csh;
  2003. int ret = 0;
  2004. IPADBG("In\n");
  2005. if ( ! VALID_TBL_HDL(tbl_hdl) ||
  2006. ! nat_stats_ptr ||
  2007. ! idx_stats_ptr )
  2008. {
  2009. IPAERR("Bad arg: "
  2010. "tbl_hdl(0x%08X) and/or "
  2011. "nat_stats_ptr(%p) and/or "
  2012. "idx_stats_ptr(%p)\n",
  2013. tbl_hdl,
  2014. nat_stats_ptr,
  2015. idx_stats_ptr );
  2016. ret = -EINVAL;
  2017. goto bail;
  2018. }
  2019. if ( pthread_mutex_lock(&nat_mutex) )
  2020. {
  2021. IPAERR("unable to lock the nat mutex\n");
  2022. ret = -EINVAL;
  2023. goto bail;
  2024. }
  2025. memset(nat_stats_ptr, 0, sizeof(ipa_nati_tbl_stats));
  2026. memset(idx_stats_ptr, 0, sizeof(ipa_nati_tbl_stats));
  2027. BREAK_TBL_HDL(tbl_hdl, nmi, broken_tbl_hdl);
  2028. if ( ! IPA_VALID_NAT_MEM_IN(nmi) )
  2029. {
  2030. IPAERR("Bad cache type argument passed\n");
  2031. ret = -EINVAL;
  2032. goto unlock;
  2033. }
  2034. nat_cache_ptr = &ipv4_nat_cache[nmi];
  2035. if ( ! nat_cache_ptr->table_cnt )
  2036. {
  2037. IPAERR("No initialized table in NAT cache\n");
  2038. ret = -EINVAL;
  2039. goto unlock;
  2040. }
  2041. nat_table = &nat_cache_ptr->ip4_tbl[broken_tbl_hdl - 1];
  2042. /*
  2043. * Gather NAT table stats...
  2044. */
  2045. ipa_tbl_ptr = &nat_table->table;
  2046. nat_stats_ptr->nmi = nmi;
  2047. nat_stats_ptr->tot_base_ents = ipa_tbl_ptr->table_entries;
  2048. nat_stats_ptr->tot_expn_ents = ipa_tbl_ptr->expn_table_entries;
  2049. nat_stats_ptr->tot_ents =
  2050. nat_stats_ptr->tot_base_ents + nat_stats_ptr->tot_expn_ents;
  2051. nat_stats_ptr->tot_base_ents_filled = ipa_tbl_ptr->cur_tbl_cnt;
  2052. nat_stats_ptr->tot_expn_ents_filled = ipa_tbl_ptr->cur_expn_tbl_cnt;
  2053. memset(&csh, 0, sizeof(chain_stat_help));
  2054. csh.which = USE_NAT_TABLE;
  2055. csh.stats_ptr = nat_stats_ptr;
  2056. ret = ipa_table_walk(
  2057. ipa_tbl_ptr, 0, WHEN_SLOT_FILLED, gen_chain_stats, &csh);
  2058. if ( ret < 0 )
  2059. {
  2060. IPAERR("Error gathering chain stats\n");
  2061. ret = -EINVAL;
  2062. goto unlock;
  2063. }
  2064. if ( csh.tot_for_avg && nat_stats_ptr->tot_chains )
  2065. {
  2066. nat_stats_ptr->avg_chain_len =
  2067. (float) csh.tot_for_avg / (float) nat_stats_ptr->tot_chains;
  2068. }
  2069. /*
  2070. * Now lets gather index table stats...
  2071. */
  2072. ipa_tbl_ptr = &nat_table->index_table;
  2073. idx_stats_ptr->nmi = nmi;
  2074. idx_stats_ptr->tot_base_ents = ipa_tbl_ptr->table_entries;
  2075. idx_stats_ptr->tot_expn_ents = ipa_tbl_ptr->expn_table_entries;
  2076. idx_stats_ptr->tot_ents =
  2077. idx_stats_ptr->tot_base_ents + idx_stats_ptr->tot_expn_ents;
  2078. idx_stats_ptr->tot_base_ents_filled = ipa_tbl_ptr->cur_tbl_cnt;
  2079. idx_stats_ptr->tot_expn_ents_filled = ipa_tbl_ptr->cur_expn_tbl_cnt;
  2080. memset(&csh, 0, sizeof(chain_stat_help));
  2081. csh.which = USE_INDEX_TABLE;
  2082. csh.stats_ptr = idx_stats_ptr;
  2083. ret = ipa_table_walk(
  2084. ipa_tbl_ptr, 0, WHEN_SLOT_FILLED, gen_chain_stats, &csh);
  2085. if ( ret < 0 )
  2086. {
  2087. IPAERR("Error gathering chain stats\n");
  2088. ret = -EINVAL;
  2089. goto unlock;
  2090. }
  2091. if ( csh.tot_for_avg && idx_stats_ptr->tot_chains )
  2092. {
  2093. idx_stats_ptr->avg_chain_len =
  2094. (float) csh.tot_for_avg / (float) idx_stats_ptr->tot_chains;
  2095. }
  2096. ret = 0;
  2097. unlock:
  2098. if ( pthread_mutex_unlock(&nat_mutex) )
  2099. {
  2100. IPAERR("unable to unlock the nat mutex\n");
  2101. ret = (ret) ? ret : -EPERM;
  2102. }
  2103. bail:
  2104. IPADBG("Out\n");
  2105. return ret;
  2106. }
  2107. int ipa_nati_vote_clock(
  2108. enum ipa_app_clock_vote_type vote_type )
  2109. {
  2110. struct ipa_nat_cache* nat_cache_ptr =
  2111. &ipv4_nat_cache[IPA_NAT_MEM_IN_SRAM];
  2112. int ret = 0;
  2113. IPADBG("In\n");
  2114. if ( ! nat_cache_ptr->ipa_desc ) {
  2115. nat_cache_ptr->ipa_desc = ipa_descriptor_open();
  2116. if ( nat_cache_ptr->ipa_desc == NULL ) {
  2117. IPAERR("failed to open IPA driver file descriptor\n");
  2118. ret = -EIO;
  2119. goto bail;
  2120. }
  2121. }
  2122. ret = ioctl(nat_cache_ptr->ipa_desc->fd,
  2123. IPA_IOC_APP_CLOCK_VOTE,
  2124. vote_type);
  2125. if (ret) {
  2126. IPAERR("APP_CLOCK_VOTE ioctl failure %d on IPA fd %d\n",
  2127. ret, nat_cache_ptr->ipa_desc->fd);
  2128. goto bail;
  2129. }
  2130. bail:
  2131. IPADBG("Out\n");
  2132. return ret;
  2133. }