NatTest.cpp 137 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288
  1. /*
  2. * Copyright (c) 2017-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 <stdio.h>
  30. #include <stdlib.h>
  31. #include <unistd.h>
  32. #include <string.h>
  33. #include <stdint.h>
  34. #include <cstring> // for memcpy
  35. #include "hton.h" // for htonl
  36. #include "InterfaceAbstraction.h"
  37. #include "Constants.h"
  38. #include "Logger.h"
  39. #include "TestsUtils.h"
  40. #include "Filtering.h"
  41. #include "RoutingDriverWrapper.h"
  42. #include "IPAFilteringTable.h"
  43. extern "C" {
  44. #include "ipa_nat_drv.h"
  45. }
  46. //IP offsets
  47. #define IPV4_PROTOCOL_OFFSET (9)
  48. #define IPV4_IP_CHECKSUM_OFFSET (10)
  49. #define IPV4_SRC_ADDR_OFFSET (12)
  50. #define IPV4_DST_ADDR_OFFSET (16)
  51. //TCP offsets
  52. #define IPV4_SRC_PORT_OFFSET (20)
  53. #define IPV4_DST_PORT_OFFSET (20+2)
  54. #define IPV4_TCP_FLAGS_OFFSET (33)
  55. #define IPV4_TCP_CHECKSUM_OFFSET (36)
  56. #define IPA_16BIT_ROUND_UP(val) \
  57. do { \
  58. if (val >> 16) { \
  59. val = (val & 0x0000FFFF);\
  60. val += 1;\
  61. } \
  62. } while(0)
  63. extern Logger g_Logger;
  64. class IpaNatBlockTestFixture : public TestBase
  65. {
  66. public:
  67. IpaNatBlockTestFixture() :
  68. m_sendSize(BUFF_MAX_SIZE),
  69. m_sendSize2(BUFF_MAX_SIZE),
  70. m_sendSize3(BUFF_MAX_SIZE),
  71. m_IpaIPType(IPA_IP_v4),
  72. m_extHdrType(NONE)
  73. {
  74. memset(m_sendBuffer, 0, sizeof(m_sendBuffer)); // First input file / IP packet
  75. memset(m_sendBuffer2, 0, sizeof(m_sendBuffer2)); // Second input file / IP packet
  76. memset(m_sendBuffer3, 0, sizeof(m_sendBuffer3)); // Third input file (default) / IP packet
  77. m_testSuiteName.push_back("Nat");
  78. }
  79. static int SetupKernelModule(bool en_status = 0)
  80. {
  81. int retval;
  82. struct ipa_channel_config from_ipa_channels[3];
  83. struct test_ipa_ep_cfg from_ipa_cfg[3];
  84. struct ipa_channel_config to_ipa_channels[2];
  85. struct test_ipa_ep_cfg to_ipa_cfg[2];
  86. struct ipa_test_config_header header = { 0 };
  87. struct ipa_channel_config *to_ipa_array[2];
  88. struct ipa_channel_config *from_ipa_array[3];
  89. /* From ipa configurations - 3 pipes */
  90. memset(&from_ipa_cfg[0], 0, sizeof(from_ipa_cfg[0]));
  91. prepare_channel_struct(&from_ipa_channels[0],
  92. header.from_ipa_channels_num++,
  93. IPA_CLIENT_TEST2_CONS,
  94. (void *)&from_ipa_cfg[0],
  95. sizeof(from_ipa_cfg[0]),
  96. en_status);
  97. from_ipa_array[0] = &from_ipa_channels[0];
  98. memset(&from_ipa_cfg[1], 0, sizeof(from_ipa_cfg[1]));
  99. prepare_channel_struct(&from_ipa_channels[1],
  100. header.from_ipa_channels_num++,
  101. IPA_CLIENT_TEST3_CONS,
  102. (void *)&from_ipa_cfg[1],
  103. sizeof(from_ipa_cfg[1]),
  104. en_status);
  105. from_ipa_array[1] = &from_ipa_channels[1];
  106. memset(&from_ipa_cfg[2], 0, sizeof(from_ipa_cfg[2]));
  107. prepare_channel_struct(&from_ipa_channels[2],
  108. header.from_ipa_channels_num++,
  109. IPA_CLIENT_TEST4_CONS,
  110. (void *)&from_ipa_cfg[2],
  111. sizeof(from_ipa_cfg[2]),
  112. en_status);
  113. from_ipa_array[2] = &from_ipa_channels[2];
  114. /* To ipa configurations - 2 pipes */
  115. memset(&to_ipa_cfg[0], 0, sizeof(to_ipa_cfg[0]));
  116. prepare_channel_struct(&to_ipa_channels[0],
  117. header.to_ipa_channels_num++,
  118. IPA_CLIENT_TEST_PROD,
  119. (void *)&to_ipa_cfg[0],
  120. sizeof(to_ipa_cfg[0]));
  121. to_ipa_array[0] = &to_ipa_channels[0];
  122. /* header removal for Ethernet header + 8021Q header */
  123. memset(&to_ipa_cfg[1], 0, sizeof(to_ipa_cfg[1]));
  124. to_ipa_cfg[1].hdr.hdr_len = ETH8021Q_HEADER_LEN;
  125. to_ipa_cfg[1].hdr.hdr_ofst_metadata_valid = 1;
  126. to_ipa_cfg[1].hdr.hdr_ofst_metadata =
  127. ETH8021Q_METADATA_OFFSET;
  128. prepare_channel_struct(&to_ipa_channels[1],
  129. header.to_ipa_channels_num++,
  130. IPA_CLIENT_TEST2_PROD,
  131. (void *)&to_ipa_cfg[1],
  132. sizeof(to_ipa_cfg[1]));
  133. to_ipa_array[1] = &to_ipa_channels[1];
  134. prepare_header_struct(&header, from_ipa_array, to_ipa_array);
  135. retval = GenericConfigureScenario(&header);
  136. return retval;
  137. }
  138. bool Setup()
  139. {
  140. bool bRetVal = true;
  141. if (SetupKernelModule() != true)
  142. return bRetVal;
  143. m_producer.Open(INTERFACE0_TO_IPA_DATA_PATH, INTERFACE0_FROM_IPA_DATA_PATH);
  144. m_producer2.Open(INTERFACE4_TO_IPA_DATA_PATH, INTERFACE4_FROM_IPA_DATA_PATH);
  145. m_consumer.Open(INTERFACE1_TO_IPA_DATA_PATH, INTERFACE1_FROM_IPA_DATA_PATH);
  146. m_consumer2.Open(INTERFACE2_TO_IPA_DATA_PATH, INTERFACE2_FROM_IPA_DATA_PATH);
  147. m_defaultConsumer.Open(INTERFACE3_TO_IPA_DATA_PATH, INTERFACE3_FROM_IPA_DATA_PATH);
  148. if (!m_routing.DeviceNodeIsOpened())
  149. {
  150. LOG_MSG_ERROR("Routing block is not ready for immediate commands!\n");
  151. return false;
  152. }
  153. if (!m_filtering.DeviceNodeIsOpened())
  154. {
  155. LOG_MSG_ERROR("Filtering block is not ready for immediate commands!\n");
  156. return false;
  157. }
  158. m_routing.Reset(IPA_IP_v4); // This will issue a Reset command to the Filtering as well
  159. m_routing.Reset(IPA_IP_v6); // This will issue a Reset command to the Filtering as well
  160. return true;
  161. } // Setup()
  162. bool Setup(bool en_status = false)
  163. {
  164. bool bRetVal = true;
  165. if (SetupKernelModule(en_status) != true)
  166. return bRetVal;
  167. m_producer.Open(INTERFACE0_TO_IPA_DATA_PATH, INTERFACE0_FROM_IPA_DATA_PATH);
  168. m_consumer.Open(INTERFACE1_TO_IPA_DATA_PATH, INTERFACE1_FROM_IPA_DATA_PATH);
  169. m_consumer2.Open(INTERFACE2_TO_IPA_DATA_PATH, INTERFACE2_FROM_IPA_DATA_PATH);
  170. m_defaultConsumer.Open(INTERFACE3_TO_IPA_DATA_PATH, INTERFACE3_FROM_IPA_DATA_PATH);
  171. if (!m_routing.DeviceNodeIsOpened())
  172. {
  173. LOG_MSG_ERROR("Routing block is not ready for immediate commands!\n");
  174. return false;
  175. }
  176. if (!m_filtering.DeviceNodeIsOpened())
  177. {
  178. LOG_MSG_ERROR("Filtering block is not ready for immediate commands!\n");
  179. return false;
  180. }
  181. m_routing.Reset(IPA_IP_v4); // This will issue a Reset command to the Filtering as well
  182. m_routing.Reset(IPA_IP_v6); // This will issue a Reset command to the Filtering as well
  183. return true;
  184. } // Setup()
  185. bool Teardown()
  186. {
  187. ipa_nat_dump_ipv4_table(m_tbl_hdl);
  188. ipa_nat_del_ipv4_tbl(m_tbl_hdl);
  189. m_producer.Close();
  190. m_producer2.Close();
  191. m_consumer.Close();
  192. m_consumer2.Close();
  193. m_defaultConsumer.Close();
  194. return true;
  195. } // Teardown()
  196. virtual bool LoadFiles(enum ipa_ip_type ip)
  197. {
  198. string fileName;
  199. if (IPA_IP_v4 == ip) {
  200. fileName = "Input/IPv4_1";
  201. }
  202. else {
  203. fileName = "Input/IPv6";
  204. }
  205. if (!LoadDefaultPacket(ip, m_extHdrType, m_sendBuffer, m_sendSize)) {
  206. LOG_MSG_ERROR("Failed default Packet\n");
  207. return false;
  208. }
  209. LOG_MSG_DEBUG("Loaded %zu Bytes to Buffer 1\n", m_sendSize);
  210. if (!LoadDefaultPacket(ip, m_extHdrType, m_sendBuffer2, m_sendSize2)) {
  211. LOG_MSG_ERROR("Failed default Packet\n");
  212. return false;
  213. }
  214. LOG_MSG_DEBUG("Loaded %zu Bytes to Buffer 2\n", m_sendSize2);
  215. if (!LoadDefaultPacket(ip, m_extHdrType, m_sendBuffer3, m_sendSize3)) {
  216. LOG_MSG_ERROR("Failed default Packet\n");
  217. return false;
  218. }
  219. LOG_MSG_DEBUG("Loaded %zu Bytes to Buffer 3\n", m_sendSize3);
  220. return true;
  221. }
  222. inline bool VerifyStatusReceived(size_t SendSize, size_t RecvSize)
  223. {
  224. size_t stts_size = sizeof(struct ipa3_hw_pkt_status);
  225. if (TestManager::GetInstance()->GetIPAHwType() >= IPA_HW_v5_0) {
  226. stts_size = sizeof(struct ipa3_hw_pkt_status_hw_v5_0);
  227. }
  228. if ((RecvSize <= SendSize) ||
  229. ((RecvSize - SendSize) != stts_size)) {
  230. LOG_MSG_ERROR("received buffer size does not match! sent:receive [%zu]:[%zu]\n", SendSize, RecvSize);
  231. return false;
  232. }
  233. return true;
  234. }
  235. inline bool IsCacheHit(size_t SendSize, size_t RecvSize, void *Buff)
  236. {
  237. struct ipa3_hw_pkt_status *pStatus = (struct ipa3_hw_pkt_status *)Buff;
  238. if (VerifyStatusReceived(SendSize, RecvSize) == false) {
  239. return false;
  240. }
  241. if ((bool)pStatus->route_hash) {
  242. LOG_MSG_DEBUG("cache hit!! \n");
  243. return true;
  244. }
  245. LOG_MSG_ERROR("cache miss!! \n");
  246. return false;
  247. }
  248. inline bool IsCacheHit_v5_0(size_t SendSize, size_t RecvSize, void *Buff)
  249. {
  250. struct ipa3_hw_pkt_status_hw_v5_0 *pStatus = (struct ipa3_hw_pkt_status_hw_v5_0 *)Buff;
  251. if (VerifyStatusReceived(SendSize, RecvSize) == false) {
  252. return false;
  253. }
  254. if ((bool)pStatus->route_hash) {
  255. LOG_MSG_DEBUG("cache hit!! \n");
  256. return true;
  257. }
  258. LOG_MSG_ERROR("cache miss!! \n");
  259. return false;
  260. }
  261. inline bool IsCacheMiss(size_t SendSize, size_t RecvSize, void *Buff)
  262. {
  263. struct ipa3_hw_pkt_status *pStatus = (struct ipa3_hw_pkt_status *)Buff;
  264. if (VerifyStatusReceived(SendSize, RecvSize) == false) {
  265. return false;
  266. }
  267. if (!((bool)pStatus->route_hash)) {
  268. LOG_MSG_DEBUG("cache miss!! \n");
  269. return true;
  270. }
  271. LOG_MSG_ERROR("cache hit!! \n");
  272. return false;
  273. }
  274. inline bool IsCacheMiss_v5_0(size_t SendSize, size_t RecvSize, void *Buff)
  275. {
  276. struct ipa3_hw_pkt_status_hw_v5_0 *pStatus = (struct ipa3_hw_pkt_status_hw_v5_0 *)Buff;
  277. if (VerifyStatusReceived(SendSize, RecvSize) == false) {
  278. return false;
  279. }
  280. if (!((bool)pStatus->route_hash)) {
  281. LOG_MSG_DEBUG("cache miss!! \n");
  282. return true;
  283. }
  284. LOG_MSG_ERROR("cache hit!! \n");
  285. return false;
  286. }
  287. bool CompareResultVsGoldenNat(Byte *goldenBuffer, unsigned int goldenSize,
  288. Byte *receivedBuffer, unsigned int receivedSize, int private_ip, int public_ip,
  289. int private_port, int public_port, bool src_nat, int IPv4_offset = 0, bool with_status = false)
  290. {
  291. bool result;
  292. uint32_t address;
  293. uint16_t port;
  294. uint32_t val;
  295. uint16_t ip_checksum_diff, tcp_checksum_diff;
  296. uint32_t ip_checksum, tcp_checksum;
  297. int recv_offset = 0;
  298. size_t stts_size = sizeof(struct ipa3_hw_pkt_status);
  299. if (TestManager::GetInstance()->GetIPAHwType() >= IPA_HW_v5_0) {
  300. stts_size = sizeof(struct ipa3_hw_pkt_status_hw_v5_0);
  301. }
  302. if (with_status)
  303. recv_offset += stts_size;
  304. ip_checksum_diff = calc_ip_cksum_diff(public_ip, private_ip);
  305. tcp_checksum_diff = calc_tcp_udp_cksum_diff(public_ip, public_port, private_ip, private_port);
  306. //calculate new ip checksum, old checksum + 1's compliment of checksum diff
  307. ip_checksum = *((uint16_t *)&goldenBuffer[IPV4_IP_CHECKSUM_OFFSET + IPv4_offset]);
  308. ip_checksum = ntohs(ip_checksum);
  309. if(src_nat)
  310. ip_checksum += (uint16_t)(~ip_checksum_diff);
  311. else
  312. ip_checksum += (uint16_t)ip_checksum_diff;
  313. IPA_16BIT_ROUND_UP(ip_checksum);
  314. //return to network format
  315. ip_checksum = htons(ip_checksum);
  316. //calculate new tcp checksum, old checksum + 1's compliment of checksum diff
  317. tcp_checksum = *((uint16_t *)&goldenBuffer[IPV4_TCP_CHECKSUM_OFFSET + IPv4_offset]);
  318. tcp_checksum = ntohs(tcp_checksum);
  319. if(src_nat)
  320. tcp_checksum += (uint16_t)(~tcp_checksum_diff);
  321. else
  322. tcp_checksum += (uint16_t)tcp_checksum_diff;
  323. IPA_16BIT_ROUND_UP(tcp_checksum);
  324. //return to network format
  325. tcp_checksum = htons(tcp_checksum);
  326. if ((receivedSize - recv_offset) != goldenSize) {
  327. g_Logger.AddMessage(LOG_VERBOSE, "%s Buffers sizes are different.\n", __FUNCTION__);
  328. return false;
  329. }
  330. Byte *tmp_buff = new Byte[goldenSize];
  331. memcpy(tmp_buff, goldenBuffer, goldenSize);
  332. if (src_nat) {
  333. address = htonl(public_ip);
  334. port = htons(public_port);
  335. memcpy(&tmp_buff[IPV4_SRC_ADDR_OFFSET + IPv4_offset], &address, sizeof(address));
  336. memcpy(&tmp_buff[IPV4_SRC_PORT_OFFSET + IPv4_offset], &port, sizeof(port));
  337. val = (*(uint32_t*)(&receivedBuffer[IPV4_SRC_ADDR_OFFSET + IPv4_offset + recv_offset]));
  338. if (address != val)
  339. LOG_MSG_ERROR("received src ip 0x%X != 0x%X\n", val, address);
  340. val = (*(uint16_t*)(&receivedBuffer[IPV4_SRC_PORT_OFFSET + IPv4_offset + recv_offset]));
  341. if (port != val)
  342. LOG_MSG_ERROR("received src port %d != %d\n", val, port);
  343. }
  344. else {
  345. address = htonl(private_ip);
  346. port = htons(private_port);
  347. memcpy(&tmp_buff[IPV4_DST_ADDR_OFFSET + IPv4_offset], &address, sizeof(address));
  348. memcpy(&tmp_buff[IPV4_DST_PORT_OFFSET + IPv4_offset], &port, sizeof(port));
  349. val = (*(uint32_t*)(&receivedBuffer[IPV4_DST_ADDR_OFFSET + IPv4_offset + +recv_offset]));
  350. if (address != val)
  351. LOG_MSG_ERROR("received dst ip 0x%X != 0x%X\n", val, address);
  352. val = (*(uint16_t*)(&receivedBuffer[IPV4_DST_PORT_OFFSET + IPv4_offset + recv_offset]));
  353. if (port != val)
  354. LOG_MSG_ERROR("received dst port %d != %d\n", val, port);
  355. }
  356. memcpy(&tmp_buff[IPV4_IP_CHECKSUM_OFFSET + IPv4_offset], &ip_checksum, sizeof(uint16_t));
  357. val = (*(uint16_t*)(&receivedBuffer[IPV4_IP_CHECKSUM_OFFSET + IPv4_offset + recv_offset]));
  358. if (ip_checksum != val)
  359. LOG_MSG_ERROR("received checksum %d != %d\n", val, ip_checksum);
  360. memcpy(&tmp_buff[IPV4_TCP_CHECKSUM_OFFSET + IPv4_offset], &tcp_checksum, sizeof(uint16_t));
  361. val = (*(uint16_t*)(&receivedBuffer[IPV4_TCP_CHECKSUM_OFFSET + IPv4_offset + recv_offset]));
  362. if (tcp_checksum != val)
  363. LOG_MSG_ERROR("received checksum %d != %d\n", val, tcp_checksum);
  364. size_t j;
  365. char tmpBuffer[512] = { 0 };
  366. for (j = 0; j < receivedSize; j++)
  367. snprintf(&tmpBuffer[3 * j], sizeof(tmpBuffer) - (3 * j + 1), " %02X", tmp_buff[j]);
  368. LOG_MSG_STACK("expected packet should be (%zu)\n%s\n", receivedSize, tmpBuffer);
  369. result = !memcmp((void*)tmp_buff, (void*)(receivedBuffer + recv_offset), goldenSize);
  370. if (!result)
  371. LOG_MSG_ERROR("buffers comparison failed!!\n");
  372. delete[] tmp_buff;
  373. return result;
  374. }
  375. bool CreateMetdataRoutingRule(const char * bypass0)
  376. {
  377. LOG_MSG_DEBUG("Entering\n");
  378. struct ipa_ioc_add_rt_rule *rt_rule0 = NULL;
  379. struct ipa_rt_rule_add *rt_rule_entry;
  380. rt_rule0 = (struct ipa_ioc_add_rt_rule *)
  381. calloc(1,
  382. sizeof(struct ipa_ioc_add_rt_rule) +
  383. 1 * sizeof(struct ipa_rt_rule_add)
  384. );
  385. if (!rt_rule0) {
  386. LOG_MSG_ERROR("calloc failed to allocate rt_rule0\n");
  387. return false;
  388. }
  389. rt_rule0->num_rules = 1;
  390. rt_rule0->ip = IPA_IP_v4;
  391. rt_rule0->commit = true;
  392. strlcpy(rt_rule0->rt_tbl_name, bypass0, sizeof(rt_rule0->rt_tbl_name));
  393. rt_rule_entry = &rt_rule0->rules[0];
  394. rt_rule_entry->at_rear = 0;
  395. rt_rule_entry->rule.dst = IPA_CLIENT_TEST2_CONS;
  396. rt_rule_entry->rule.attrib.attrib_mask = IPA_FLT_META_DATA;
  397. rt_rule_entry->rule.attrib.meta_data = m_metadata;
  398. rt_rule_entry->rule.attrib.meta_data_mask = 0xFFFFFFFF;// Filter exact metadata value
  399. if (false == m_routing.AddRoutingRule(rt_rule0))
  400. {
  401. LOG_MSG_ERROR("Routing rule addition(rt_rule0) failed!\n");
  402. Free(rt_rule0);
  403. return false;
  404. }
  405. Free(rt_rule0);
  406. LOG_MSG_DEBUG("Leaving\n");
  407. return true;
  408. }
  409. bool CreateHashableRoutingRules(const char * bypass0)
  410. {
  411. LOG_MSG_DEBUG("Entering\n");
  412. struct ipa_ioc_add_rt_rule *rt_rule0 = 0, *rt_rule1 = 0;
  413. struct ipa_rt_rule_add *rt_rule_entry;
  414. rt_rule0 = (struct ipa_ioc_add_rt_rule *)
  415. calloc(1,
  416. sizeof(struct ipa_ioc_add_rt_rule) +
  417. 2 * sizeof(struct ipa_rt_rule_add)
  418. );
  419. if (!rt_rule0) {
  420. LOG_MSG_ERROR("calloc failed to allocate rt_rule0\n");
  421. return false;
  422. }
  423. rt_rule0->num_rules = 2;
  424. rt_rule0->ip = IPA_IP_v4;
  425. rt_rule0->commit = true;
  426. strlcpy(rt_rule0->rt_tbl_name, bypass0, sizeof(rt_rule0->rt_tbl_name));
  427. rt_rule_entry = &rt_rule0->rules[0];
  428. rt_rule_entry->at_rear = 0;
  429. rt_rule_entry->rule.dst = IPA_CLIENT_TEST2_CONS;
  430. rt_rule_entry->rule.attrib.attrib_mask = IPA_FLT_DST_ADDR;
  431. rt_rule_entry->rule.attrib.u.v4.dst_addr = m_private_ip;
  432. rt_rule_entry->rule.attrib.u.v4.dst_addr_mask = 0xFFFFFFFF;// Exact match
  433. rt_rule_entry->rule.hashable = 1;
  434. rt_rule_entry = &rt_rule0->rules[1];
  435. rt_rule_entry->at_rear = 1;
  436. rt_rule_entry->rule.dst = IPA_CLIENT_TEST3_CONS;
  437. rt_rule_entry->rule.attrib.attrib_mask = IPA_FLT_DST_ADDR;
  438. rt_rule_entry->rule.attrib.u.v4.dst_addr = m_private_ip2;
  439. rt_rule_entry->rule.attrib.u.v4.dst_addr_mask = 0xFFFFFFFF;// Exact match
  440. rt_rule_entry->rule.hashable = 1;
  441. if (false == m_routing.AddRoutingRule(rt_rule0))
  442. {
  443. LOG_MSG_ERROR("Routing rule addition(rt_rule0) failed!\n");
  444. Free(rt_rule1);
  445. Free(rt_rule0);
  446. return false;
  447. }
  448. Free(rt_rule0);
  449. LOG_MSG_DEBUG("Leaving\n");
  450. return true;
  451. }
  452. // This function creates three IPv4 bypass routing entries and commits them.
  453. bool CreateThreeIPv4BypassRoutingTables(const char * bypass0, const char * bypass1, const char * bypass2)
  454. {
  455. LOG_MSG_DEBUG("Entering\n");
  456. struct ipa_ioc_add_rt_rule *rt_rule0 = 0, *rt_rule1 = 0, *rt_rule2 = 0;
  457. struct ipa_rt_rule_add *rt_rule_entry;
  458. rt_rule0 = (struct ipa_ioc_add_rt_rule *)
  459. calloc(1,
  460. sizeof(struct ipa_ioc_add_rt_rule) +
  461. 1 * sizeof(struct ipa_rt_rule_add)
  462. );
  463. if (!rt_rule0) {
  464. LOG_MSG_ERROR("calloc failed to allocate rt_rule0\n");
  465. return false;
  466. }
  467. rt_rule1 = (struct ipa_ioc_add_rt_rule *)
  468. calloc(1,
  469. sizeof(struct ipa_ioc_add_rt_rule) +
  470. 1 * sizeof(struct ipa_rt_rule_add)
  471. );
  472. if (!rt_rule1) {
  473. LOG_MSG_ERROR("calloc failed to allocate rt_rule1\n");
  474. Free(rt_rule0);
  475. return false;
  476. }
  477. rt_rule2 = (struct ipa_ioc_add_rt_rule *)
  478. calloc(1,
  479. sizeof(struct ipa_ioc_add_rt_rule) +
  480. 1 * sizeof(struct ipa_rt_rule_add)
  481. );
  482. if (!rt_rule2) {
  483. LOG_MSG_ERROR("calloc failed to allocate rt_rule2\n");
  484. Free(rt_rule0);
  485. Free(rt_rule1);
  486. return false;
  487. }
  488. rt_rule0->num_rules = 1;
  489. rt_rule0->ip = IPA_IP_v4;
  490. rt_rule0->commit = true;
  491. strlcpy(rt_rule0->rt_tbl_name, bypass0, sizeof(rt_rule0->rt_tbl_name));
  492. rt_rule_entry = &rt_rule0->rules[0];
  493. rt_rule_entry->at_rear = 0;
  494. rt_rule_entry->rule.dst = IPA_CLIENT_TEST2_CONS;
  495. rt_rule_entry->rule.attrib.attrib_mask = IPA_FLT_DST_ADDR;
  496. rt_rule_entry->rule.attrib.u.v4.dst_addr = 0xaabbccdd;
  497. rt_rule_entry->rule.attrib.u.v4.dst_addr_mask = 0x00000000;// All Packets will get a "Hit"
  498. if (false == m_routing.AddRoutingRule(rt_rule0))
  499. {
  500. LOG_MSG_ERROR("Routing rule addition(rt_rule0) failed!\n");
  501. Free(rt_rule2);
  502. Free(rt_rule1);
  503. Free(rt_rule0);
  504. return false;
  505. }
  506. rt_rule1->num_rules = 1;
  507. rt_rule1->ip = IPA_IP_v4;
  508. rt_rule1->commit = true;
  509. strlcpy(rt_rule1->rt_tbl_name, bypass1, sizeof(rt_rule1->rt_tbl_name));
  510. rt_rule_entry = &rt_rule1->rules[0];
  511. rt_rule_entry->at_rear = 0;
  512. rt_rule_entry->rule.dst = IPA_CLIENT_TEST3_CONS;
  513. rt_rule_entry->rule.attrib.attrib_mask = IPA_FLT_DST_ADDR;
  514. rt_rule_entry->rule.attrib.u.v4.dst_addr = 0xaabbccdd;
  515. rt_rule_entry->rule.attrib.u.v4.dst_addr_mask = 0x00000000;// All Packets will get a "Hit"
  516. if (false == m_routing.AddRoutingRule(rt_rule1))
  517. {
  518. LOG_MSG_ERROR("Routing rule addition(rt_rule1) failed!\n");
  519. Free(rt_rule2);
  520. Free(rt_rule1);
  521. Free(rt_rule0);
  522. return false;
  523. }
  524. rt_rule2->num_rules = 1;
  525. rt_rule2->ip = IPA_IP_v4;
  526. rt_rule2->commit = true;
  527. strlcpy(rt_rule2->rt_tbl_name, bypass2, sizeof(rt_rule2->rt_tbl_name));
  528. rt_rule_entry = &rt_rule2->rules[0];
  529. rt_rule_entry->at_rear = 0;
  530. rt_rule_entry->rule.dst = IPA_CLIENT_TEST4_CONS;
  531. rt_rule_entry->rule.attrib.attrib_mask = IPA_FLT_DST_ADDR;
  532. rt_rule_entry->rule.attrib.u.v4.dst_addr = 0xaabbccdd;
  533. rt_rule_entry->rule.attrib.u.v4.dst_addr_mask = 0x00000000;// All Packets will get a "Hit"
  534. if (false == m_routing.AddRoutingRule(rt_rule2))
  535. {
  536. LOG_MSG_ERROR("Routing rule addition(rt_rule2) failed!\n");
  537. Free(rt_rule2);
  538. Free(rt_rule1);
  539. Free(rt_rule0);
  540. return false;
  541. }
  542. Free(rt_rule2);
  543. Free(rt_rule1);
  544. Free(rt_rule0);
  545. LOG_MSG_DEBUG("Leaving\n");
  546. return true;
  547. }
  548. void Load8021QPacket()
  549. {
  550. m_sendSize = sizeof(m_sendBuffer);
  551. LoadDefault802_1Q(IPA_IP_v4, m_sendBuffer, m_sendSize);
  552. }
  553. virtual bool ModifyPackets() = 0;
  554. virtual bool AddRules() = 0;
  555. virtual bool SendPackets() = 0;
  556. virtual bool ReceivePacketsAndCompare() = 0;
  557. bool Run()
  558. {
  559. bool res = false;
  560. bool isSuccess = false;
  561. LOG_MSG_DEBUG("Entering\n");
  562. // Add the relevant filtering rules
  563. res = AddRules();
  564. if (false == res) {
  565. LOG_MSG_ERROR("Failed adding filtering rules.\n");
  566. return false;
  567. }
  568. // Load input data (IP packet) from file
  569. res = LoadFiles(m_IpaIPType);
  570. if (false == res) {
  571. LOG_MSG_ERROR("Failed loading files.\n");
  572. return false;
  573. }
  574. res = ModifyPackets();
  575. if (false == res) {
  576. LOG_MSG_ERROR("Failed to modify packets.\n");
  577. return false;
  578. }
  579. res = SendPackets();
  580. if (res == false) {
  581. LOG_MSG_ERROR("failed to send packets\n");
  582. return false;
  583. }
  584. // Receive packets from the channels and compare results
  585. isSuccess = ReceivePacketsAndCompare();
  586. LOG_MSG_DEBUG("Returning %d\n", isSuccess);
  587. return isSuccess;
  588. } // Run()
  589. /**
  590. * calc_ip_cksum_diff() - Calculate the source nat
  591. * IP checksum diff
  592. * @pub_ip_addr: [in] public ip address
  593. * @priv_ip_addr: [in] Private ip address
  594. *
  595. * source nat ip checksum different is calculated as
  596. * public_ip_addr - private_ip_addr
  597. * Here we are using 1's complement to represent -negative number.
  598. * So take 1's complement of private ip addr and add it
  599. * to public ip addr.
  600. *
  601. * Returns: >0 ip checksum diff
  602. */
  603. uint16_t calc_ip_cksum_diff(uint32_t pub_ip_addr,
  604. uint32_t priv_ip_addr)
  605. {
  606. uint16_t ret;
  607. uint32_t cksum = 0;
  608. /* Add LSB(2 bytes) of public ip address to cksum */
  609. cksum += (pub_ip_addr & 0xFFFF);
  610. /* Add MSB(2 bytes) of public ip address to cksum
  611. and check for carry forward(CF), if any add it
  612. */
  613. cksum += (pub_ip_addr >> 16);
  614. IPA_16BIT_ROUND_UP(cksum);
  615. /* Calculate the 1's complement of private ip address */
  616. priv_ip_addr = (~priv_ip_addr);
  617. /* Add LSB(2 bytes) of private ip address to cksum
  618. and check for carry forward(CF), if any add it
  619. */
  620. cksum += (priv_ip_addr & 0xFFFF);
  621. IPA_16BIT_ROUND_UP(cksum);
  622. /* Add MSB(2 bytes) of private ip address to cksum
  623. and check for carry forward(CF), if any add it
  624. */
  625. cksum += (priv_ip_addr >> 16);
  626. IPA_16BIT_ROUND_UP(cksum);
  627. /* Return the LSB(2 bytes) of checksum */
  628. ret = (uint16_t)cksum;
  629. return ret;
  630. }
  631. /**
  632. * calc_tcp_udp_cksum() - Calculate the source nat
  633. * TCP/UDP checksum diff
  634. * @pub_ip_addr: [in] public ip address
  635. * @pub_port: [in] public tcp/udp port
  636. * @priv_ip_addr: [in] Private ip address
  637. * @priv_port: [in] Private tcp/udp prot
  638. *
  639. * source nat tcp/udp checksum is calculated as
  640. * (pub_ip_addr + pub_port) - (priv_ip_addr + priv_port)
  641. * Here we are using 1's complement to represent -ve number.
  642. * So take 1's complement of prviate ip addr &private port
  643. * and add it public ip addr & public port.
  644. *
  645. * Returns: >0 tcp/udp checksum diff
  646. */
  647. uint16_t calc_tcp_udp_cksum_diff(uint32_t pub_ip_addr,
  648. uint16_t pub_port,
  649. uint32_t priv_ip_addr,
  650. uint16_t priv_port)
  651. {
  652. uint16_t ret = 0;
  653. uint32_t cksum = 0;
  654. /* Add LSB(2 bytes) of public ip address to cksum */
  655. cksum += (pub_ip_addr & 0xFFFF);
  656. /* Add MSB(2 bytes) of public ip address to cksum
  657. and check for carry forward(CF), if any add it
  658. */
  659. cksum += (pub_ip_addr >> 16);
  660. IPA_16BIT_ROUND_UP(cksum);
  661. /* Add public port to cksum and
  662. check for carry forward(CF), if any add it */
  663. cksum += pub_port;
  664. IPA_16BIT_ROUND_UP(cksum);
  665. /* Calculate the 1's complement of private ip address */
  666. priv_ip_addr = (~priv_ip_addr);
  667. /* Add LSB(2 bytes) of private ip address to cksum
  668. and check for carry forward(CF), if any add it
  669. */
  670. cksum += (priv_ip_addr & 0xFFFF);
  671. IPA_16BIT_ROUND_UP(cksum);
  672. /* Add MSB(2 bytes) of private ip address to cksum
  673. and check for carry forward(CF), if any add
  674. */
  675. cksum += (priv_ip_addr >> 16);
  676. IPA_16BIT_ROUND_UP(cksum);
  677. /* Calculate the 1's complement of private port */
  678. priv_port = (~priv_port);
  679. /* Add public port to cksum and
  680. check for carry forward(CF), if any add it */
  681. cksum += priv_port;
  682. IPA_16BIT_ROUND_UP(cksum);
  683. /* return the LSB(2 bytes) of checksum */
  684. ret = (uint16_t)cksum;
  685. return ret;
  686. }
  687. ~IpaNatBlockTestFixture()
  688. {
  689. m_sendSize = 0;
  690. m_sendSize2 = 0;
  691. m_sendSize3 = 0;
  692. }
  693. static Filtering m_filtering;
  694. static RoutingDriverWrapper m_routing;
  695. InterfaceAbstraction m_producer;
  696. InterfaceAbstraction m_producer2;
  697. InterfaceAbstraction m_consumer;
  698. InterfaceAbstraction m_consumer2;
  699. InterfaceAbstraction m_defaultConsumer;
  700. static const size_t BUFF_MAX_SIZE = 1024;
  701. Byte m_sendBuffer[BUFF_MAX_SIZE]; // First input file / IP packet
  702. Byte m_sendBuffer2[BUFF_MAX_SIZE]; // Second input file / IP packet
  703. Byte m_sendBuffer3[BUFF_MAX_SIZE]; // Third input file (default) / IP packet
  704. size_t m_sendSize;
  705. size_t m_sendSize2;
  706. size_t m_sendSize3;
  707. enum ipa_ip_type m_IpaIPType;
  708. enum ipv6_ext_hdr_type m_extHdrType;
  709. uint32_t m_tbl_hdl;
  710. uint32_t m_nat_rule_hdl1;
  711. uint32_t m_public_ip;
  712. uint32_t m_public_ip2;
  713. uint32_t m_private_ip;
  714. uint32_t m_private_ip2;
  715. uint32_t m_target_ip;
  716. uint16_t m_public_port;
  717. uint16_t m_public_port2;
  718. uint16_t m_private_port;
  719. uint16_t m_private_port2;
  720. uint16_t m_target_port;
  721. uint32_t m_metadata;
  722. private:
  723. };
  724. RoutingDriverWrapper IpaNatBlockTestFixture::m_routing;
  725. Filtering IpaNatBlockTestFixture::m_filtering;
  726. /*---------------------------------------------------------------------------*/
  727. /* Test001: Single PDN src NAT test */
  728. /* NOTE: other classes are derived from this class - change carefully */
  729. /*---------------------------------------------------------------------------*/
  730. class IpaNatBlockTest001 : public IpaNatBlockTestFixture
  731. {
  732. public:
  733. IpaNatBlockTest001()
  734. {
  735. m_name = "IpaNatBlockTest001";
  736. m_description =
  737. "NAT block test 001 - single PDN src NAT test\
  738. 1. Generate and commit three routing tables (only one is used). \
  739. Each table contains a single \"bypass\" rule (all data goes to output pipe 0, 1 and 2 (accordingly)) \
  740. 2. Generate and commit one filtering rule: (DST & Mask Match). \
  741. action go to src NAT \
  742. All DST_IP == (193.23.22.1 & 0.255.255.255)traffic goes to NAT block \
  743. 3. generate and commit one NAT rule:\
  744. private ip 194.23.22.1 --> public ip 192.23.22.1";
  745. m_private_ip = 0xC2171601; /* 194.23.22.1 */
  746. m_private_port = 5678;
  747. m_public_ip = 0xC0171601; /* "192.23.22.1" */
  748. m_public_port = 9050;
  749. m_target_ip = 0xC1171601; /* 193.23.22.1 */
  750. m_target_port = 1234;
  751. Register(*this);
  752. }
  753. virtual bool AddRules()
  754. {
  755. LOG_MSG_DEBUG("Entering\n");
  756. const char bypass0[20] = "Bypass0";
  757. const char bypass1[20] = "Bypass1";
  758. const char bypass2[20] = "Bypass2";
  759. struct ipa_ioc_get_rt_tbl routing_table0;
  760. if (!CreateThreeIPv4BypassRoutingTables(bypass0, bypass1, bypass2))
  761. {
  762. LOG_MSG_ERROR("CreateThreeBypassRoutingTables Failed\n");
  763. return false;
  764. }
  765. LOG_MSG_DEBUG("CreateThreeBypassRoutingTables completed successfully\n");
  766. routing_table0.ip = IPA_IP_v4;
  767. strlcpy(routing_table0.name, bypass0, sizeof(routing_table0.name));
  768. if (!m_routing.GetRoutingTable(&routing_table0))
  769. {
  770. LOG_MSG_ERROR("m_routing.GetRoutingTable(&routing_table0=0x%p) Failed.\n", &routing_table0);
  771. return false;
  772. }
  773. LOG_MSG_DEBUG("%s route table handle = %u\n", bypass0, routing_table0.hdl);
  774. IPAFilteringTable FilterTable0;
  775. struct ipa_flt_rule_add flt_rule_entry;
  776. FilterTable0.Init(IPA_IP_v4, IPA_CLIENT_TEST_PROD, false, 1);
  777. LOG_MSG_DEBUG("FilterTable*.Init Completed Successfully..\n");
  778. // Configuring Filtering Rule No.0
  779. FilterTable0.GeneratePresetRule(1, flt_rule_entry);
  780. flt_rule_entry.at_rear = true;
  781. flt_rule_entry.flt_rule_hdl = -1; // return Value
  782. flt_rule_entry.status = -1; // return value
  783. flt_rule_entry.rule.action = IPA_PASS_TO_SRC_NAT;
  784. flt_rule_entry.rule.rt_tbl_hdl = routing_table0.hdl; //put here the handle corresponding to Routing Rule 1
  785. flt_rule_entry.rule.attrib.attrib_mask = IPA_FLT_DST_ADDR;
  786. flt_rule_entry.rule.attrib.u.v4.dst_addr_mask = 0x00FFFFFF; // Mask
  787. flt_rule_entry.rule.attrib.u.v4.dst_addr = m_target_ip; // Filter DST_IP == 193.23.22.1
  788. flt_rule_entry.rule.pdn_idx = 0;
  789. flt_rule_entry.rule.set_metadata = 0;
  790. if (
  791. ((uint8_t)-1 == FilterTable0.AddRuleToTable(flt_rule_entry)) ||
  792. !m_filtering.AddFilteringRule(FilterTable0.GetFilteringTable())
  793. )
  794. {
  795. LOG_MSG_ERROR("Error Adding Rule to Filter Table, aborting...\n");
  796. return false;
  797. }
  798. else
  799. {
  800. LOG_MSG_DEBUG("flt rule hdl0=0x%x, status=0x%x\n", FilterTable0.ReadRuleFromTable(0)->flt_rule_hdl, FilterTable0.ReadRuleFromTable(0)->status);
  801. }
  802. //NAT table and rules creation
  803. int total_entries = 20;
  804. int ret;
  805. ipa_nat_ipv4_rule ipv4_rule;
  806. ret = ipa_nat_add_ipv4_tbl(m_public_ip, m_mem_type, total_entries, &m_tbl_hdl);
  807. if (ret) {
  808. LOG_MSG_DEBUG("failed creating NAT table\n");
  809. return false;
  810. }
  811. LOG_MSG_DEBUG("nat table added, hdl %d, public ip 0x%X\n", m_tbl_hdl,
  812. m_public_ip);
  813. ipv4_rule.target_ip = m_target_ip;
  814. ipv4_rule.target_port = m_target_port;
  815. ipv4_rule.private_ip = m_private_ip;
  816. ipv4_rule.private_port = m_private_port;
  817. ipv4_rule.protocol = IPPROTO_TCP;
  818. ipv4_rule.public_port = m_public_port;
  819. ipv4_rule.pdn_index = 0;
  820. ret = ipa_nat_add_ipv4_rule(m_tbl_hdl, &ipv4_rule, &m_nat_rule_hdl1);
  821. if (ret) {
  822. LOG_MSG_ERROR("failed adding NAT rule 0\n");
  823. return false;
  824. }
  825. LOG_MSG_DEBUG("NAT rule added, hdl %d, data: 0x%X, %d, 0x%X, %d, %d, %d\n",
  826. m_nat_rule_hdl1, ipv4_rule.target_ip, ipv4_rule.target_port,
  827. ipv4_rule.private_ip, ipv4_rule.private_port,
  828. ipv4_rule.protocol, ipv4_rule.public_port);
  829. LOG_MSG_DEBUG("Leaving");
  830. return true;
  831. }// AddRules()
  832. virtual bool ModifyPackets()
  833. {
  834. uint32_t address;
  835. uint16_t port;
  836. char flags = 0x18;
  837. address = htonl(m_target_ip);//193.23.22.1
  838. memcpy(&m_sendBuffer[IPV4_DST_ADDR_OFFSET], &address, sizeof(address));
  839. port = htons(m_target_port);
  840. memcpy(&m_sendBuffer[IPV4_DST_PORT_OFFSET], &port, sizeof(port));
  841. address = htonl(m_private_ip);/* 194.23.22.1 */
  842. memcpy(&m_sendBuffer[IPV4_SRC_ADDR_OFFSET], &address, sizeof(address));
  843. port = htons(m_private_port);
  844. memcpy(&m_sendBuffer[IPV4_SRC_PORT_OFFSET], &port, sizeof(port));
  845. //make sure the FIN flag is not set, otherwise we will get a NAT miss
  846. memcpy(&m_sendBuffer[IPV4_TCP_FLAGS_OFFSET],&flags , sizeof(flags));
  847. return true;
  848. }// ModifyPacktes ()
  849. virtual bool SendPackets()
  850. {
  851. bool isSuccess = false;
  852. // Send first packet
  853. isSuccess = m_producer.SendData(m_sendBuffer, m_sendSize);
  854. if (false == isSuccess)
  855. {
  856. LOG_MSG_ERROR("SendData failure.\n");
  857. return false;
  858. }
  859. LOG_MSG_DEBUG("sent successfully one packet\n");
  860. return true;
  861. }
  862. virtual bool ReceivePacketsAndCompare()
  863. {
  864. size_t receivedSize = 0;
  865. bool isSuccess = true;
  866. // Receive results
  867. Byte *rxBuff1 = new Byte[0x400];
  868. if (NULL == rxBuff1)
  869. {
  870. LOG_MSG_ERROR("Memory allocation error.\n");
  871. return false;
  872. }
  873. receivedSize = m_consumer.ReceiveData(rxBuff1, 0x400);
  874. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize, m_consumer.m_fromChannelName.c_str());
  875. // Compare results
  876. if (!CompareResultVsGoldenNat(
  877. m_sendBuffer, m_sendSize,
  878. rxBuff1, receivedSize,
  879. m_private_ip, m_public_ip,
  880. m_private_port, m_public_port,
  881. true))
  882. {
  883. LOG_MSG_ERROR("Comparison of Buffer0 Failed!\n");
  884. isSuccess = false;
  885. }
  886. char recievedBuffer[256] = { 0 };
  887. char SentBuffer[256] = { 0 };
  888. size_t j;
  889. for (j = 0; j < m_sendSize; j++)
  890. snprintf(&SentBuffer[3 * j], sizeof(SentBuffer) - (3 * j + 1), " %02X", m_sendBuffer[j]);
  891. for (j = 0; j < receivedSize; j++)
  892. snprintf(&recievedBuffer[3 * j], sizeof(recievedBuffer) - (3 * j + 1), " %02X", rxBuff1[j]);
  893. LOG_MSG_STACK("sent Value1 (%zu)\n%s\n, Received Value1(%zu)\n%s\n", m_sendSize, SentBuffer, receivedSize, recievedBuffer);
  894. delete[] rxBuff1;
  895. return isSuccess;
  896. }
  897. };
  898. /*---------------------------------------------------------------------------*/
  899. /* Test002: Single PDN dst NAT test */
  900. /* NOTE: other classes are derived from this class - change carefully */
  901. /*---------------------------------------------------------------------------*/
  902. class IpaNatBlockTest002 : public IpaNatBlockTestFixture
  903. {
  904. public:
  905. IpaNatBlockTest002()
  906. {
  907. m_name = "IpaNatBlockTest002";
  908. m_description =
  909. "NAT block test 002 - single PDN dst NAT test\
  910. 1. Generate and commit three routing tables (only one is used). \
  911. Each table contains a single \"bypass\" rule (all data goes to output pipe 0, 1 and 2 (accordingly)) \
  912. 2. Generate and commit one filtering rule: (DST & Mask Match). \
  913. action go to dst NAT \
  914. All DST_IP == (192.23.22.1 & 0.255.255.255)traffic goes to NAT block (public IP filtering) \
  915. 3. generate and commit one NAT rule:\
  916. public ip 192.23.22.1 --> private ip 194.23.22.1 ";
  917. m_private_ip = 0xC2171601; /* 194.23.22.1 */
  918. m_private_port = 5678;
  919. m_public_ip = 0xC0171601; /* "192.23.22.1" */
  920. m_public_port = 9050;
  921. m_target_ip = 0xC1171601; /* 193.23.22.1 */
  922. m_target_port = 1234;
  923. Register(*this);
  924. }
  925. virtual bool AddRules()
  926. {
  927. LOG_MSG_DEBUG("Entering\n");
  928. const char bypass0[20] = "Bypass0";
  929. const char bypass1[20] = "Bypass1";
  930. const char bypass2[20] = "Bypass2";
  931. struct ipa_ioc_get_rt_tbl routing_table0;
  932. if (!CreateThreeIPv4BypassRoutingTables(bypass0, bypass1, bypass2))
  933. {
  934. LOG_MSG_ERROR("CreateThreeBypassRoutingTables Failed\n");
  935. return false;
  936. }
  937. LOG_MSG_DEBUG("CreateThreeBypassRoutingTables completed successfully\n");
  938. routing_table0.ip = IPA_IP_v4;
  939. strlcpy(routing_table0.name, bypass0, sizeof(routing_table0.name));
  940. if (!m_routing.GetRoutingTable(&routing_table0))
  941. {
  942. LOG_MSG_ERROR("m_routing.GetRoutingTable(&routing_table0=0x%p) Failed.\n", &routing_table0);
  943. return false;
  944. }
  945. LOG_MSG_DEBUG("%s route table handle = %u\n", bypass0, routing_table0.hdl);
  946. IPAFilteringTable FilterTable0;
  947. struct ipa_flt_rule_add flt_rule_entry;
  948. FilterTable0.Init(IPA_IP_v4, IPA_CLIENT_TEST_PROD, false, 3);
  949. LOG_MSG_DEBUG("FilterTable*.Init Completed Successfully..\n");
  950. // Configuring Filtering Rule No.0
  951. FilterTable0.GeneratePresetRule(1, flt_rule_entry);
  952. flt_rule_entry.at_rear = true;
  953. flt_rule_entry.flt_rule_hdl = -1; // return Value
  954. flt_rule_entry.status = -1; // return value
  955. flt_rule_entry.rule.action = IPA_PASS_TO_DST_NAT;
  956. flt_rule_entry.rule.rt_tbl_hdl = routing_table0.hdl; //put here the handle corresponding to Routing Rule 1
  957. flt_rule_entry.rule.attrib.attrib_mask = IPA_FLT_DST_ADDR;
  958. flt_rule_entry.rule.attrib.u.v4.dst_addr_mask = 0x00FFFFFF; // Mask
  959. flt_rule_entry.rule.attrib.u.v4.dst_addr = m_public_ip; // Filter DST_IP == 192.23.22.1
  960. flt_rule_entry.rule.pdn_idx = 0;
  961. flt_rule_entry.rule.set_metadata = 0;
  962. if (
  963. ((uint8_t)-1 == FilterTable0.AddRuleToTable(flt_rule_entry)) ||
  964. !m_filtering.AddFilteringRule(FilterTable0.GetFilteringTable())
  965. )
  966. {
  967. LOG_MSG_ERROR("Error Adding Rule to Filter Table, aborting...\n");
  968. return false;
  969. }
  970. else
  971. {
  972. LOG_MSG_DEBUG("flt rule hdl0=0x%x, status=0x%x\n", FilterTable0.ReadRuleFromTable(0)->flt_rule_hdl, FilterTable0.ReadRuleFromTable(0)->status);
  973. }
  974. //NAT table and rules creation
  975. int total_entries = 20;
  976. int ret;
  977. ipa_nat_ipv4_rule ipv4_rule;
  978. uint32_t pub_ip_add = m_public_ip;
  979. ret = ipa_nat_add_ipv4_tbl(pub_ip_add, m_mem_type, total_entries, &m_tbl_hdl);
  980. if (ret) {
  981. LOG_MSG_ERROR("Leaving, failed creating NAT table\n");
  982. return false;
  983. }
  984. LOG_MSG_DEBUG("nat table added, hdl %d, public ip 0x%X\n", m_tbl_hdl,
  985. pub_ip_add);
  986. ipv4_rule.target_ip = m_target_ip;
  987. ipv4_rule.target_port = m_target_port;
  988. ipv4_rule.private_ip = m_private_ip;
  989. ipv4_rule.private_port = m_private_port;
  990. ipv4_rule.protocol = IPPROTO_TCP;
  991. ipv4_rule.public_port = m_public_port;
  992. ipv4_rule.pdn_index = 0;
  993. ret = ipa_nat_add_ipv4_rule(m_tbl_hdl, &ipv4_rule, &m_nat_rule_hdl1);
  994. if (ret) {
  995. LOG_MSG_ERROR("Leaving, failed adding NAT rule 0\n");
  996. return false;
  997. }
  998. LOG_MSG_DEBUG("NAT rule added, hdl %d, data: 0x%X, %d, 0x%X, %d, %d, %d\n",
  999. m_nat_rule_hdl1, ipv4_rule.target_ip, ipv4_rule.target_port,
  1000. ipv4_rule.private_ip, ipv4_rule.private_port,
  1001. ipv4_rule.protocol, ipv4_rule.public_port);
  1002. LOG_MSG_DEBUG("Leaving\n");
  1003. return true;
  1004. }// AddRules()
  1005. virtual bool ModifyPackets()
  1006. {
  1007. uint32_t address;
  1008. uint16_t port;
  1009. char flags = 0x18;
  1010. address = htonl(m_public_ip);//192.23.22.1
  1011. memcpy(&m_sendBuffer[IPV4_DST_ADDR_OFFSET], &address, sizeof(address));
  1012. port = htons(m_public_port);
  1013. memcpy(&m_sendBuffer[IPV4_DST_PORT_OFFSET], &port, sizeof(port));
  1014. address = htonl(m_target_ip);/* 193.23.22.1 */
  1015. memcpy(&m_sendBuffer[IPV4_SRC_ADDR_OFFSET], &address, sizeof(address));
  1016. port = htons(m_target_port);
  1017. memcpy(&m_sendBuffer[IPV4_SRC_PORT_OFFSET], &port, sizeof(port));
  1018. //make sure the FIN flag is not set, otherwise we will get a NAT miss
  1019. memcpy(&m_sendBuffer[IPV4_TCP_FLAGS_OFFSET], &flags, sizeof(flags));
  1020. return true;
  1021. }// ModifyPacktes ()
  1022. virtual bool SendPackets()
  1023. {
  1024. bool isSuccess = false;
  1025. // Send first packet
  1026. isSuccess = m_producer.SendData(m_sendBuffer, m_sendSize);
  1027. if (false == isSuccess)
  1028. {
  1029. LOG_MSG_ERROR("SendData failure.\n");
  1030. return false;
  1031. }
  1032. LOG_MSG_DEBUG("sent successfully one packet\n");
  1033. return true;
  1034. }
  1035. virtual bool ReceivePacketsAndCompare()
  1036. {
  1037. size_t receivedSize = 0;
  1038. bool isSuccess = true;
  1039. // Receive results
  1040. Byte *rxBuff1 = new Byte[0x400];
  1041. if (NULL == rxBuff1)
  1042. {
  1043. LOG_MSG_ERROR("Memory allocation error.\n");
  1044. return false;
  1045. }
  1046. receivedSize = m_consumer.ReceiveData(rxBuff1, 0x400);
  1047. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize, m_consumer.m_fromChannelName.c_str());
  1048. // Compare results
  1049. if (!CompareResultVsGoldenNat(
  1050. m_sendBuffer, m_sendSize,
  1051. rxBuff1, receivedSize,
  1052. m_private_ip, m_public_ip,
  1053. m_private_port, m_public_port,
  1054. false))
  1055. {
  1056. LOG_MSG_ERROR("Comparison of Buffer0 Failed!\n");
  1057. isSuccess = false;
  1058. }
  1059. char recievedBuffer[256] = { 0 };
  1060. char SentBuffer[256] = { 0 };
  1061. size_t j;
  1062. for (j = 0; j < m_sendSize; j++)
  1063. snprintf(&SentBuffer[3 * j], sizeof(SentBuffer) - (3 * j + 1), " %02X", m_sendBuffer[j]);
  1064. for (j = 0; j < receivedSize; j++)
  1065. snprintf(&recievedBuffer[3 * j], sizeof(recievedBuffer) - (3 * j + 1), " %02X", rxBuff1[j]);
  1066. LOG_MSG_STACK("sent Value1 (%zu)\n%s\n, Received Value1(%zu)\n%s\n", m_sendSize, SentBuffer, receivedSize, recievedBuffer);
  1067. delete[] rxBuff1;
  1068. return isSuccess;
  1069. }
  1070. };
  1071. /*---------------------------------------------------------------------------*/
  1072. /* Test003: Multi PDN src NAT test */
  1073. /* NOTE: other classes are derived from this class - change carefully */
  1074. /*---------------------------------------------------------------------------*/
  1075. class IpaNatBlockTest003 : public IpaNatBlockTestFixture
  1076. {
  1077. public:
  1078. IpaNatBlockTest003()
  1079. {
  1080. m_name = "IpaNatBlockTest003";
  1081. m_description =
  1082. "NAT block test 003 - Multi PDN src NAT test\
  1083. 1. Generate and commit three routing tables (two are used). \
  1084. Each table contains a single \"bypass\" rule (all data goes to output pipe 0, 1 and 2 (accordingly)) \
  1085. 2. Generate and commit two filtering rule: (DST & Mask Match). \
  1086. - action go to src NAT \
  1087. All SRC_IP == (194.23.22.1 & 0.255.255.255)traffic goes to NAT block \
  1088. All SRC_IP == (197.23.22.1 & 0.255.255.255)traffic goes to NAT block \
  1089. 3. generate and commit two NAT rules:\
  1090. private ip 194.23.22.1 --> public ip 192.23.22.1 \
  1091. private ip 197.23.22.1 --> public ip 195.23.22.1";
  1092. m_private_ip = 0xC2171601; /* 194.23.22.1 */
  1093. m_private_port = 5678;
  1094. m_private_ip2 = 0xC5171601; /* 197.23.22.1 */
  1095. m_private_port2 = 5679;
  1096. m_public_ip = 0xC0171601; /* "192.23.22.1" */
  1097. m_public_port = 9050;
  1098. m_public_ip2 = 0xC3171601; /* "195.23.22.1" */
  1099. m_public_port2 = 9051;
  1100. m_target_ip = 0xC1171601; /* 193.23.22.1 */
  1101. m_target_port = 1234;
  1102. m_minIPAHwType = IPA_HW_v4_0;
  1103. Register(*this);
  1104. }
  1105. virtual bool AddRules()
  1106. {
  1107. LOG_MSG_DEBUG("Entering\n");
  1108. const char bypass0[20] = "Bypass0";
  1109. const char bypass1[20] = "Bypass1";
  1110. const char bypass2[20] = "Bypass2";
  1111. struct ipa_ioc_get_rt_tbl routing_table0, routing_table1;
  1112. if (!CreateThreeIPv4BypassRoutingTables(bypass0, bypass1, bypass2))
  1113. {
  1114. LOG_MSG_ERROR("CreateThreeBypassRoutingTables Failed\n");
  1115. return false;
  1116. }
  1117. LOG_MSG_DEBUG("CreateThreeBypassRoutingTables completed successfully\n");
  1118. routing_table0.ip = IPA_IP_v4;
  1119. strlcpy(routing_table0.name, bypass0, sizeof(routing_table0.name));
  1120. if (!m_routing.GetRoutingTable(&routing_table0))
  1121. {
  1122. LOG_MSG_ERROR("m_routing.GetRoutingTable(&routing_table0=0x%p) Failed.\n", &routing_table0);
  1123. return false;
  1124. }
  1125. LOG_MSG_DEBUG("%s route table handle = %u\n", bypass0, routing_table0.hdl);
  1126. routing_table1.ip = IPA_IP_v4;
  1127. strlcpy(routing_table1.name, bypass1, sizeof(routing_table1.name));
  1128. if (!m_routing.GetRoutingTable(&routing_table1))
  1129. {
  1130. LOG_MSG_ERROR("m_routing.GetRoutingTable(&routing_table1=0x%p) Failed.\n", &routing_table1);
  1131. return false;
  1132. }
  1133. LOG_MSG_DEBUG("%s route table handle = %u\n", bypass1, routing_table1.hdl);
  1134. IPAFilteringTable FilterTable0;
  1135. struct ipa_flt_rule_add flt_rule_entry;
  1136. FilterTable0.Init(IPA_IP_v4, IPA_CLIENT_TEST_PROD, false, 2);
  1137. LOG_MSG_DEBUG("FilterTable*.Init Completed Successfully..\n");
  1138. // Configuring Filtering Rule No.0
  1139. FilterTable0.GeneratePresetRule(1, flt_rule_entry);
  1140. flt_rule_entry.at_rear = true;
  1141. flt_rule_entry.flt_rule_hdl = -1; // return Value
  1142. flt_rule_entry.status = -1; // return value
  1143. flt_rule_entry.rule.action = IPA_PASS_TO_SRC_NAT;
  1144. flt_rule_entry.rule.rt_tbl_hdl = routing_table0.hdl; //put here the handle corresponding to Routing Rule 1
  1145. flt_rule_entry.rule.attrib.attrib_mask = IPA_FLT_SRC_ADDR;
  1146. flt_rule_entry.rule.attrib.u.v4.src_addr_mask = 0xFFFFFFFF; // Mask
  1147. flt_rule_entry.rule.attrib.u.v4.src_addr = m_private_ip; // Filter SRC_IP == 194.23.22.1
  1148. flt_rule_entry.rule.pdn_idx = 0;
  1149. flt_rule_entry.rule.set_metadata = 0;
  1150. if ((uint8_t)-1 == FilterTable0.AddRuleToTable(flt_rule_entry))
  1151. {
  1152. LOG_MSG_ERROR("Error Adding Rule to Filter Table, aborting...\n");
  1153. return false;
  1154. }
  1155. // Configuring Filtering Rule No.1
  1156. flt_rule_entry.rule.rt_tbl_hdl = routing_table1.hdl; //put here the handle corresponding to Routing Rule 2
  1157. flt_rule_entry.rule.attrib.u.v4.src_addr = m_private_ip2; // Filter SRC_IP == 197.23.22.1
  1158. if (
  1159. ((uint8_t)-1 == FilterTable0.AddRuleToTable(flt_rule_entry)) ||
  1160. !m_filtering.AddFilteringRule(FilterTable0.GetFilteringTable())
  1161. )
  1162. {
  1163. LOG_MSG_ERROR("Error Adding Rule to Filter Table, aborting...\n");
  1164. return false;
  1165. }
  1166. else
  1167. {
  1168. LOG_MSG_DEBUG("flt rule hdl0=0x%x, status=0x%x\n", FilterTable0.ReadRuleFromTable(0)->flt_rule_hdl, FilterTable0.ReadRuleFromTable(0)->status);
  1169. LOG_MSG_DEBUG("flt rule hdl1=0x%x, status=0x%x\n", FilterTable0.ReadRuleFromTable(1)->flt_rule_hdl, FilterTable0.ReadRuleFromTable(1)->status);
  1170. }
  1171. //NAT table and rules creation
  1172. int total_entries = 20;
  1173. int ret;
  1174. ipa_nat_ipv4_rule ipv4_rule;
  1175. uint32_t pub_ip_add = m_public_ip;
  1176. ipa_nat_pdn_entry pdn_info;
  1177. // first create the NAT table
  1178. ret = ipa_nat_add_ipv4_tbl(pub_ip_add, m_mem_type, total_entries, &m_tbl_hdl);
  1179. if (ret) {
  1180. LOG_MSG_ERROR("Leaving, failed creating NAT table\n");
  1181. return false;
  1182. }
  1183. LOG_MSG_DEBUG("nat table added, hdl %d, public ip 0x%X\n", m_tbl_hdl,
  1184. pub_ip_add);
  1185. // modify the PDN entries that will be pointed by the NAT rules
  1186. pdn_info.public_ip = m_public_ip;
  1187. pdn_info.src_metadata = 0;
  1188. pdn_info.dst_metadata = 0;
  1189. ret = ipa_nat_modify_pdn(m_tbl_hdl, 0, &pdn_info);
  1190. if (ret) {
  1191. LOG_MSG_ERROR("Leaving, failed Modifying PDN entry 0 \n");
  1192. return false;
  1193. }
  1194. pdn_info.public_ip = m_public_ip2;
  1195. pdn_info.src_metadata = 0;
  1196. pdn_info.dst_metadata = 0;
  1197. ret = ipa_nat_modify_pdn(m_tbl_hdl, 1, &pdn_info);
  1198. if (ret) {
  1199. LOG_MSG_ERROR("Leaving, failed Modifying PDN entry 1 \n");
  1200. return false;
  1201. }
  1202. ipv4_rule.target_ip = m_target_ip;
  1203. ipv4_rule.target_port = m_target_port;
  1204. ipv4_rule.private_ip = m_private_ip;
  1205. ipv4_rule.private_port = m_private_port;
  1206. ipv4_rule.protocol = IPPROTO_TCP;
  1207. ipv4_rule.public_port = m_public_port;
  1208. ipv4_rule.pdn_index = 0;
  1209. ret = ipa_nat_add_ipv4_rule(m_tbl_hdl, &ipv4_rule, &m_nat_rule_hdl1);
  1210. if (ret) {
  1211. LOG_MSG_ERROR("Leaving, failed adding NAT rule 0\n");
  1212. return false;
  1213. }
  1214. LOG_MSG_DEBUG("NAT rule added, hdl %d, data: 0x%X, %d, 0x%X, %d, %d, %d\n",
  1215. m_nat_rule_hdl1, ipv4_rule.target_ip, ipv4_rule.target_port,
  1216. ipv4_rule.private_ip, ipv4_rule.private_port,
  1217. ipv4_rule.protocol, ipv4_rule.public_port);
  1218. ipv4_rule.private_ip = m_private_ip2;
  1219. ipv4_rule.private_port = m_private_port2;
  1220. ipv4_rule.public_port = m_public_port2;
  1221. ipv4_rule.pdn_index = 1;
  1222. ret = ipa_nat_add_ipv4_rule(m_tbl_hdl, &ipv4_rule, &m_nat_rule_hdl1);
  1223. if (ret) {
  1224. LOG_MSG_ERROR("Leaving, failed adding NAT rule 1\n");
  1225. return false;
  1226. }
  1227. LOG_MSG_DEBUG("NAT rule 2 added, hdl %d, data: 0x%X, %d, 0x%X, %d, %d, %d\n",
  1228. m_nat_rule_hdl1, ipv4_rule.target_ip, ipv4_rule.target_port,
  1229. ipv4_rule.private_ip, ipv4_rule.private_port,
  1230. ipv4_rule.protocol, ipv4_rule.public_port);
  1231. LOG_MSG_DEBUG("Leaving\n");
  1232. return true;
  1233. }// AddRules()
  1234. virtual bool ModifyPackets()
  1235. {
  1236. uint32_t address;
  1237. uint16_t port;
  1238. char flags = 0x18;
  1239. //first packet private ip 194.23.22.1 --> public ip 192.23.22.1
  1240. address = htonl(m_target_ip);//193.23.22.1
  1241. memcpy(&m_sendBuffer[IPV4_DST_ADDR_OFFSET], &address, sizeof(address));
  1242. port = htons(m_target_port);
  1243. memcpy(&m_sendBuffer[IPV4_DST_PORT_OFFSET], &port, sizeof(port));
  1244. address = htonl(m_private_ip);/* 194.23.22.1 */
  1245. memcpy(&m_sendBuffer[IPV4_SRC_ADDR_OFFSET], &address, sizeof(address));
  1246. port = htons(m_private_port);
  1247. memcpy(&m_sendBuffer[IPV4_SRC_PORT_OFFSET], &port, sizeof(port));
  1248. //make sure the FIN flag is not set, otherwise we will get a NAT miss
  1249. memcpy(&m_sendBuffer[IPV4_TCP_FLAGS_OFFSET], &flags, sizeof(flags));
  1250. // second packet private ip 197.23.22.1 --> public ip 195.23.22.1
  1251. address = htonl(m_target_ip);//193.23.22.1
  1252. memcpy(&m_sendBuffer2[IPV4_DST_ADDR_OFFSET], &address, sizeof(address));
  1253. port = htons(m_target_port);
  1254. memcpy(&m_sendBuffer2[IPV4_DST_PORT_OFFSET], &port, sizeof(port));
  1255. address = htonl(m_private_ip2);/* 197.23.22.1 */
  1256. memcpy(&m_sendBuffer2[IPV4_SRC_ADDR_OFFSET], &address, sizeof(address));
  1257. port = htons(m_private_port2);
  1258. memcpy(&m_sendBuffer2[IPV4_SRC_PORT_OFFSET], &port, sizeof(port));
  1259. //make sure the FIN flag is not set, otherwise we will get a NAT miss
  1260. memcpy(&m_sendBuffer2[IPV4_TCP_FLAGS_OFFSET], &flags, sizeof(flags));
  1261. return true;
  1262. }// ModifyPacktes ()
  1263. virtual bool SendPackets()
  1264. {
  1265. bool isSuccess = false;
  1266. // Send first packet
  1267. LOG_MSG_DEBUG("sending first packet\n");
  1268. isSuccess = m_producer.SendData(m_sendBuffer, m_sendSize);
  1269. if (false == isSuccess)
  1270. {
  1271. LOG_MSG_ERROR("SendData failure.\n");
  1272. return false;
  1273. }
  1274. // Send second packet
  1275. LOG_MSG_DEBUG("sending second packet\n");
  1276. isSuccess = m_producer.SendData(m_sendBuffer2, m_sendSize2);
  1277. if (false == isSuccess)
  1278. {
  1279. LOG_MSG_ERROR("SendData failure.\n");
  1280. return false;
  1281. }
  1282. LOG_MSG_DEBUG("sent successfully two packets\n");
  1283. return true;
  1284. }
  1285. virtual bool ReceivePacketsAndCompare()
  1286. {
  1287. size_t receivedSize = 0;
  1288. size_t receivedSize2 = 0;
  1289. bool isSuccess = true;
  1290. // Receive results
  1291. Byte *rxBuff1 = new Byte[0x400];
  1292. Byte *rxBuff2 = new Byte[0x400];
  1293. if (rxBuff1 == NULL)
  1294. {
  1295. LOG_MSG_ERROR("Memory allocation error.\n");
  1296. if (rxBuff2)
  1297. delete[] rxBuff2;
  1298. return false;
  1299. }
  1300. if (rxBuff2 == NULL)
  1301. {
  1302. LOG_MSG_ERROR("Memory allocation error.\n");
  1303. delete[] rxBuff1;
  1304. return false;
  1305. }
  1306. receivedSize = m_consumer.ReceiveData(rxBuff1, 0x400);
  1307. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize, m_consumer.m_fromChannelName.c_str());
  1308. receivedSize2 = m_consumer2.ReceiveData(rxBuff2, 0x400);
  1309. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize2, m_consumer2.m_fromChannelName.c_str());
  1310. // Compare results
  1311. if (!CompareResultVsGoldenNat(
  1312. m_sendBuffer, m_sendSize,
  1313. rxBuff1, receivedSize,
  1314. m_private_ip, m_public_ip,
  1315. m_private_port, m_public_port,
  1316. true))
  1317. {
  1318. LOG_MSG_ERROR("Comparison of Buffer0 Failed!\n");
  1319. isSuccess = false;
  1320. }
  1321. char recievedBuffer[256] = { 0 };
  1322. char SentBuffer[256] = { 0 };
  1323. char recievedBuffer2[256] = { 0 };
  1324. char SentBuffer2[256] = { 0 };
  1325. size_t j;
  1326. for (j = 0; j < m_sendSize; j++)
  1327. snprintf(&SentBuffer[3 * j], sizeof(SentBuffer) - (3 * j + 1), " %02X", m_sendBuffer[j]);
  1328. for (j = 0; j < receivedSize; j++)
  1329. snprintf(&recievedBuffer[3 * j], sizeof(recievedBuffer) - (3 * j + 1), " %02X", rxBuff1[j]);
  1330. LOG_MSG_STACK("sent Value1 (%zu)\n%s\n, Received Value1(%zu)\n%s\n", m_sendSize, SentBuffer, receivedSize, recievedBuffer);
  1331. delete[] rxBuff1;
  1332. isSuccess &= CompareResultVsGoldenNat(
  1333. m_sendBuffer2, m_sendSize2,
  1334. rxBuff2, receivedSize2,
  1335. m_private_ip2, m_public_ip2,
  1336. m_private_port2, m_public_port2,
  1337. true);
  1338. for (j = 0; j < m_sendSize2; j++)
  1339. snprintf(&SentBuffer2[3 * j], sizeof(SentBuffer2) - (3 * j + 1), " %02X", m_sendBuffer2[j]);
  1340. for (j = 0; j < receivedSize2; j++)
  1341. snprintf(&recievedBuffer2[3 * j], sizeof(recievedBuffer2) - (3 * j + 1), " %02X", rxBuff2[j]);
  1342. LOG_MSG_STACK("sent Value2 (%zu)\n%s\n, Received Value2(%zu)\n%s\n", m_sendSize2, SentBuffer2, receivedSize2, recievedBuffer2);
  1343. delete[] rxBuff2;
  1344. return isSuccess;
  1345. }
  1346. };
  1347. /*---------------------------------------------------------------------------*/
  1348. /* Test004: Multi PDN dst NAT test */
  1349. /* NOTE: other classes are derived from this class - change carefully */
  1350. /*---------------------------------------------------------------------------*/
  1351. class IpaNatBlockTest004 : public IpaNatBlockTestFixture
  1352. {
  1353. public:
  1354. IpaNatBlockTest004()
  1355. {
  1356. m_name = "IpaNatBlockTest004";
  1357. m_description =
  1358. "NAT block test 004 - Multi PDN dst NAT test\
  1359. 1. Generate and commit three routing tables (two are used). \
  1360. Each table contains a single \"bypass\" rule (all data goes to output pipe 0, 1 and 2 (accordingly)) \
  1361. 2. Generate and commit two filtering rule: (DST & Mask Match). \
  1362. - action go to dst NAT \
  1363. All DST_IP == (192.23.22.1 & 0.255.255.255)traffic goes to NAT block \
  1364. All DST_IP == (195.23.22.1 & 0.255.255.255)traffic goes to NAT block \
  1365. 3. generate and commit two NAT rules:\
  1366. private ip 194.23.22.1 --> public ip 192.23.22.1 \
  1367. private ip 197.23.22.1 --> public ip 195.23.22.1";
  1368. m_private_ip = 0xC2171601; /* 194.23.22.1 */
  1369. m_private_port = 5678;
  1370. m_private_ip2 = 0xC5171601; /* 197.23.22.1 */
  1371. m_private_port2 = 5679;
  1372. m_public_ip = 0xC0171601; /* "192.23.22.1" */
  1373. m_public_port = 9050;
  1374. m_public_ip2 = 0xC3171601; /* "195.23.22.1" */
  1375. m_public_port2 = 9051;
  1376. m_target_ip = 0xC1171601; /* 193.23.22.1 */
  1377. m_target_port = 1234;
  1378. m_minIPAHwType = IPA_HW_v4_0;
  1379. Register(*this);
  1380. }
  1381. virtual bool AddRules()
  1382. {
  1383. LOG_MSG_DEBUG("Entering\n");
  1384. const char bypass0[20] = "Bypass0";
  1385. const char bypass1[20] = "Bypass1";
  1386. const char bypass2[20] = "Bypass2";
  1387. struct ipa_ioc_get_rt_tbl routing_table0, routing_table1;
  1388. if (!CreateThreeIPv4BypassRoutingTables(bypass0, bypass1, bypass2))
  1389. {
  1390. LOG_MSG_ERROR("CreateThreeBypassRoutingTables Failed\n");
  1391. return false;
  1392. }
  1393. LOG_MSG_DEBUG("CreateThreeBypassRoutingTables completed successfully\n");
  1394. routing_table0.ip = IPA_IP_v4;
  1395. strlcpy(routing_table0.name, bypass0, sizeof(routing_table0.name));
  1396. if (!m_routing.GetRoutingTable(&routing_table0))
  1397. {
  1398. LOG_MSG_ERROR("m_routing.GetRoutingTable(&routing_table0=0x%p) Failed.\n", &routing_table0);
  1399. return false;
  1400. }
  1401. LOG_MSG_DEBUG("%s route table handle = %u\n", bypass0, routing_table0.hdl);
  1402. routing_table1.ip = IPA_IP_v4;
  1403. strlcpy(routing_table1.name, bypass1, sizeof(routing_table1.name));
  1404. if (!m_routing.GetRoutingTable(&routing_table1))
  1405. {
  1406. LOG_MSG_ERROR("m_routing.GetRoutingTable(&routing_table1=0x%p) Failed.\n", &routing_table1);
  1407. return false;
  1408. }
  1409. LOG_MSG_DEBUG("%s route table handle = %u\n", bypass1, routing_table1.hdl);
  1410. IPAFilteringTable FilterTable0;
  1411. struct ipa_flt_rule_add flt_rule_entry;
  1412. FilterTable0.Init(IPA_IP_v4, IPA_CLIENT_TEST_PROD, false, 2);
  1413. LOG_MSG_DEBUG("FilterTable*.Init Completed Successfully..\n");
  1414. // Configuring Filtering Rule No.0
  1415. FilterTable0.GeneratePresetRule(1, flt_rule_entry);
  1416. flt_rule_entry.at_rear = true;
  1417. flt_rule_entry.flt_rule_hdl = -1; // return Value
  1418. flt_rule_entry.status = -1; // return value
  1419. flt_rule_entry.rule.action = IPA_PASS_TO_DST_NAT;
  1420. flt_rule_entry.rule.rt_tbl_hdl = routing_table0.hdl; //put here the handle corresponding to Routing Rule 1
  1421. flt_rule_entry.rule.attrib.attrib_mask = IPA_FLT_DST_ADDR;
  1422. flt_rule_entry.rule.attrib.u.v4.dst_addr_mask = 0xFFFFFFFF; // Mask
  1423. flt_rule_entry.rule.attrib.u.v4.dst_addr = m_public_ip; // Filter DST_IP == 192.23.22.1
  1424. flt_rule_entry.rule.pdn_idx = 0;
  1425. flt_rule_entry.rule.set_metadata = 0;
  1426. if ((uint8_t)-1 == FilterTable0.AddRuleToTable(flt_rule_entry))
  1427. {
  1428. LOG_MSG_ERROR("Error Adding Rule to Filter Table, aborting...\n");
  1429. return false;
  1430. }
  1431. // Configuring Filtering Rule No.1
  1432. flt_rule_entry.rule.rt_tbl_hdl = routing_table1.hdl; //put here the handle corresponding to Routing Rule 2
  1433. flt_rule_entry.rule.attrib.u.v4.dst_addr = m_public_ip2; // Filter DST_IP == 195.23.22.1
  1434. if (
  1435. ((uint8_t)-1 == FilterTable0.AddRuleToTable(flt_rule_entry)) ||
  1436. !m_filtering.AddFilteringRule(FilterTable0.GetFilteringTable())
  1437. )
  1438. {
  1439. LOG_MSG_ERROR("Error Adding Rule to Filter Table, aborting...\n");
  1440. return false;
  1441. }
  1442. else
  1443. {
  1444. LOG_MSG_DEBUG("flt rule hdl0=0x%x, status=0x%x\n", FilterTable0.ReadRuleFromTable(0)->flt_rule_hdl, FilterTable0.ReadRuleFromTable(0)->status);
  1445. LOG_MSG_DEBUG("flt rule hdl1=0x%x, status=0x%x\n", FilterTable0.ReadRuleFromTable(1)->flt_rule_hdl, FilterTable0.ReadRuleFromTable(1)->status);
  1446. }
  1447. //NAT table and rules creation
  1448. int total_entries = 20;
  1449. int ret;
  1450. ipa_nat_ipv4_rule ipv4_rule;
  1451. uint32_t pub_ip_add = m_public_ip;
  1452. ipa_nat_pdn_entry pdn_info;
  1453. // first create the NAT table
  1454. ret = ipa_nat_add_ipv4_tbl(pub_ip_add, m_mem_type, total_entries, &m_tbl_hdl);
  1455. if (ret) {
  1456. LOG_MSG_ERROR("Leaving, failed creating NAT table\n");
  1457. return false;
  1458. }
  1459. LOG_MSG_DEBUG("nat table added, hdl %d, public ip 0x%X\n", m_tbl_hdl,
  1460. pub_ip_add);
  1461. // modify the PDN entries that will be pointed by the NAT rules
  1462. pdn_info.public_ip = m_public_ip;
  1463. pdn_info.src_metadata = 0;
  1464. pdn_info.dst_metadata = 0;
  1465. ret = ipa_nat_modify_pdn(m_tbl_hdl, 0,&pdn_info);
  1466. if (ret) {
  1467. LOG_MSG_ERROR("Leaving, failed Modifying PDN entry 0 \n");
  1468. return false;
  1469. }
  1470. pdn_info.public_ip = m_public_ip2;
  1471. pdn_info.src_metadata = 0;
  1472. pdn_info.dst_metadata = 0;
  1473. ret = ipa_nat_modify_pdn(m_tbl_hdl, 1, &pdn_info);
  1474. if (ret) {
  1475. LOG_MSG_ERROR("Leaving, failed Modifying PDN entry 1 \n");
  1476. return false;
  1477. }
  1478. ipv4_rule.target_ip = m_target_ip;
  1479. ipv4_rule.target_port = m_target_port;
  1480. ipv4_rule.private_ip = m_private_ip;
  1481. ipv4_rule.private_port = m_private_port;
  1482. ipv4_rule.protocol = IPPROTO_TCP;
  1483. ipv4_rule.public_port = m_public_port;
  1484. ipv4_rule.pdn_index = 0;
  1485. ret = ipa_nat_add_ipv4_rule(m_tbl_hdl, &ipv4_rule, &m_nat_rule_hdl1);
  1486. if (ret) {
  1487. LOG_MSG_ERROR("Leaving, failed adding NAT rule 0\n");
  1488. return false;
  1489. }
  1490. LOG_MSG_DEBUG("NAT rule added, hdl %d, data: 0x%X, %d, 0x%X, %d, %d, %d\n",
  1491. m_nat_rule_hdl1, ipv4_rule.target_ip, ipv4_rule.target_port,
  1492. ipv4_rule.private_ip, ipv4_rule.private_port,
  1493. ipv4_rule.protocol, ipv4_rule.public_port);
  1494. ipv4_rule.private_ip = m_private_ip2;
  1495. ipv4_rule.private_port = m_private_port2;
  1496. ipv4_rule.public_port = m_public_port2;
  1497. ipv4_rule.pdn_index = 1;
  1498. ret = ipa_nat_add_ipv4_rule(m_tbl_hdl, &ipv4_rule, &m_nat_rule_hdl1);
  1499. if (ret) {
  1500. LOG_MSG_ERROR("Leaving, failed adding NAT rule 0\n");
  1501. return false;
  1502. }
  1503. LOG_MSG_DEBUG("NAT rule 2 added, hdl %d, data: 0x%X, %d, 0x%X, %d, %d, %d\n",
  1504. m_nat_rule_hdl1, ipv4_rule.target_ip, ipv4_rule.target_port,
  1505. ipv4_rule.private_ip, ipv4_rule.private_port,
  1506. ipv4_rule.protocol, ipv4_rule.public_port);
  1507. LOG_MSG_DEBUG("Leaving\n");
  1508. return true;
  1509. }// AddRules()
  1510. virtual bool ModifyPackets()
  1511. {
  1512. uint32_t address;
  1513. uint16_t port;
  1514. char flags = 0x18;
  1515. //first packet private ip public ip 192.23.22.1 --> 194.23.22.1
  1516. address = htonl(m_public_ip);//192.23.22.1
  1517. memcpy(&m_sendBuffer[IPV4_DST_ADDR_OFFSET], &address, sizeof(address));
  1518. port = htons(m_public_port);
  1519. memcpy(&m_sendBuffer[IPV4_DST_PORT_OFFSET], &port, sizeof(port));
  1520. address = htonl(m_target_ip);/* 194.23.22.1 */
  1521. memcpy(&m_sendBuffer[IPV4_SRC_ADDR_OFFSET], &address, sizeof(address));
  1522. port = htons(m_target_port);
  1523. memcpy(&m_sendBuffer[IPV4_SRC_PORT_OFFSET], &port, sizeof(port));
  1524. //make sure the FIN flag is not set, otherwise we will get a NAT miss
  1525. memcpy(&m_sendBuffer[IPV4_TCP_FLAGS_OFFSET], &flags, sizeof(flags));
  1526. // second packet public ip 195.23.22.1--> private ip 197.23.22.1
  1527. address = htonl(m_public_ip2);//193.23.22.1
  1528. memcpy(&m_sendBuffer2[IPV4_DST_ADDR_OFFSET], &address, sizeof(address));
  1529. port = htons(m_public_port2);
  1530. memcpy(&m_sendBuffer2[IPV4_DST_PORT_OFFSET], &port, sizeof(port));
  1531. address = htonl(m_target_ip);/* 197.23.22.1 */
  1532. memcpy(&m_sendBuffer2[IPV4_SRC_ADDR_OFFSET], &address, sizeof(address));
  1533. port = htons(m_target_port);
  1534. memcpy(&m_sendBuffer2[IPV4_SRC_PORT_OFFSET], &port, sizeof(port));
  1535. //make sure the FIN flag is not set, otherwise we will get a NAT miss
  1536. memcpy(&m_sendBuffer2[IPV4_TCP_FLAGS_OFFSET], &flags, sizeof(flags));
  1537. return true;
  1538. }// ModifyPacktes ()
  1539. virtual bool SendPackets()
  1540. {
  1541. bool isSuccess = false;
  1542. // Send first packet
  1543. isSuccess = m_producer.SendData(m_sendBuffer, m_sendSize);
  1544. if (false == isSuccess)
  1545. {
  1546. LOG_MSG_ERROR("SendData failure.\n");
  1547. return false;
  1548. }
  1549. // Send second packet
  1550. isSuccess = m_producer.SendData(m_sendBuffer2, m_sendSize2);
  1551. if (false == isSuccess)
  1552. {
  1553. LOG_MSG_ERROR("SendData failure.\n");
  1554. return false;
  1555. }
  1556. LOG_MSG_DEBUG("sent successfully two packets\n");
  1557. return true;
  1558. }
  1559. virtual bool ReceivePacketsAndCompare()
  1560. {
  1561. size_t receivedSize = 0;
  1562. size_t receivedSize2 = 0;
  1563. bool isSuccess = true;
  1564. // Receive results
  1565. Byte *rxBuff1 = new Byte[0x400];
  1566. Byte *rxBuff2 = new Byte[0x400];
  1567. if (rxBuff1 == NULL)
  1568. {
  1569. LOG_MSG_ERROR("Memory allocation error.\n");
  1570. if (rxBuff2)
  1571. delete[] rxBuff2;
  1572. return false;
  1573. }
  1574. if (rxBuff2 == NULL)
  1575. {
  1576. LOG_MSG_ERROR("Memory allocation error.\n");
  1577. delete[] rxBuff1;
  1578. return false;
  1579. }
  1580. receivedSize = m_consumer.ReceiveData(rxBuff1, 0x400);
  1581. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize, m_consumer.m_fromChannelName.c_str());
  1582. receivedSize2 = m_consumer2.ReceiveData(rxBuff2, 0x400);
  1583. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize2, m_consumer2.m_fromChannelName.c_str());
  1584. // Compare results
  1585. if (!CompareResultVsGoldenNat(
  1586. m_sendBuffer, m_sendSize,
  1587. rxBuff1, receivedSize,
  1588. m_private_ip, m_public_ip,
  1589. m_private_port, m_public_port,
  1590. false))
  1591. {
  1592. LOG_MSG_ERROR("Comparison of Buffer0 Failed!\n");
  1593. isSuccess = false;
  1594. }
  1595. char recievedBuffer[256] = { 0 };
  1596. char SentBuffer[256] = { 0 };
  1597. char recievedBuffer2[256] = { 0 };
  1598. char SentBuffer2[256] = { 0 };
  1599. size_t j;
  1600. for (j = 0; j < m_sendSize; j++)
  1601. snprintf(&SentBuffer[3 * j], sizeof(SentBuffer) - (3 * j + 1), " %02X", m_sendBuffer[j]);
  1602. for (j = 0; j < receivedSize; j++)
  1603. snprintf(&recievedBuffer[3 * j], sizeof(recievedBuffer) - (3 * j + 1), " %02X", rxBuff1[j]);
  1604. LOG_MSG_STACK("sent Value1 (%zu)\n%s\n, Received Value1(%zu)\n%s\n", m_sendSize, SentBuffer, receivedSize, recievedBuffer);
  1605. delete[] rxBuff1;
  1606. isSuccess &= CompareResultVsGoldenNat(
  1607. m_sendBuffer2, m_sendSize2,
  1608. rxBuff2, receivedSize2,
  1609. m_private_ip2, m_public_ip2,
  1610. m_private_port2, m_public_port2,
  1611. false);
  1612. for (j = 0; j < m_sendSize2; j++)
  1613. snprintf(&SentBuffer2[3 * j], sizeof(SentBuffer2) - (3 * j + 1), " %02X", m_sendBuffer2[j]);
  1614. for (j = 0; j < receivedSize2; j++)
  1615. snprintf(&recievedBuffer2[3 * j], sizeof(recievedBuffer2) - (3 * j + 1), " %02X", rxBuff2[j]);
  1616. LOG_MSG_STACK("sent Value1 (%zu)\n%s\n, Received Value1(%zu)\n%s\n", m_sendSize2, SentBuffer2, receivedSize2, recievedBuffer2);
  1617. delete[] rxBuff2;
  1618. return isSuccess;
  1619. }
  1620. };
  1621. /*---------------------------------------------------------------------------*/
  1622. /* Test005: Single PDN src metadata replacement NAT test */
  1623. /*---------------------------------------------------------------------------*/
  1624. class IpaNatBlockTest005 : public IpaNatBlockTestFixture
  1625. {
  1626. public:
  1627. IpaNatBlockTest005()
  1628. {
  1629. m_name = "IpaNatBlockTest005";
  1630. m_description =
  1631. "NAT block test 005 - single PDN src metadata replacement NAT test\
  1632. source metadata will be replaced and the routing rule equation will be done upon replaced value\
  1633. 1. Generate and commit two routing tables (only one is used). \
  1634. the routing table will catch packets with metadata value 0x34567890 (different from original value)\
  1635. 2. Generate and commit one filtering rule: (DST & Mask Match). \
  1636. action go to src NAT \
  1637. All DST_IP == (193.23.22.1 & 0.255.255.255)traffic goes to NAT block \
  1638. action parameters metadata replacement = true\
  1639. 3. generate and commit one NAT rule:\
  1640. private ip 194.23.22.1 --> public ip 192.23.22.1\
  1641. source metadata value shall be replaced to 0x34567890 (caught by the routing rule)";
  1642. m_private_ip = 0xC2171601; /* 194.23.22.1 */
  1643. m_private_port = 5678;
  1644. m_public_ip = 0xC0171601; /* "192.23.22.1" */
  1645. m_public_port = 9050;
  1646. m_target_ip = 0xC1171601; /* 193.23.22.1 */
  1647. m_target_port = 1234;
  1648. m_metadata = 0x34567890;
  1649. m_minIPAHwType = IPA_HW_v4_0;
  1650. Register(*this);
  1651. }
  1652. virtual bool AddRules()
  1653. {
  1654. LOG_MSG_DEBUG("Entering\n");
  1655. const char bypass0[20] = "Bypass0";
  1656. struct ipa_ioc_get_rt_tbl routing_table0;
  1657. if (!CreateMetdataRoutingRule(bypass0))
  1658. {
  1659. LOG_MSG_ERROR("CreateThreeBypassRoutingTables Failed\n");
  1660. return false;
  1661. }
  1662. LOG_MSG_DEBUG("CreateMetdataRoutingRule completed successfully\n");
  1663. routing_table0.ip = IPA_IP_v4;
  1664. strlcpy(routing_table0.name, bypass0, sizeof(routing_table0.name));
  1665. if (!m_routing.GetRoutingTable(&routing_table0))
  1666. {
  1667. LOG_MSG_ERROR("m_routing.GetRoutingTable(&routing_table0=0x%p) Failed.\n", &routing_table0);
  1668. return false;
  1669. }
  1670. LOG_MSG_DEBUG("%s route table handle = %u\n", bypass0, routing_table0.hdl);
  1671. IPAFilteringTable FilterTable0;
  1672. struct ipa_flt_rule_add flt_rule_entry;
  1673. FilterTable0.Init(IPA_IP_v4, IPA_CLIENT_TEST2_PROD, false, 1);
  1674. LOG_MSG_DEBUG("FilterTable*.Init Completed Successfully..\n");
  1675. // Configuring Filtering Rule No.0
  1676. FilterTable0.GeneratePresetRule(1, flt_rule_entry);
  1677. flt_rule_entry.at_rear = true;
  1678. flt_rule_entry.flt_rule_hdl = -1; // return Value
  1679. flt_rule_entry.status = -1; // return value
  1680. flt_rule_entry.rule.action = IPA_PASS_TO_SRC_NAT;
  1681. flt_rule_entry.rule.rt_tbl_hdl = routing_table0.hdl; //put here the handle corresponding to Routing Rule 1
  1682. flt_rule_entry.rule.attrib.attrib_mask = IPA_FLT_DST_ADDR;
  1683. flt_rule_entry.rule.attrib.u.v4.dst_addr_mask = 0xFFFFFFFF; // Mask
  1684. flt_rule_entry.rule.attrib.u.v4.dst_addr = m_target_ip; // Filter DST_IP == 193.23.22.1
  1685. flt_rule_entry.rule.pdn_idx = 0;
  1686. flt_rule_entry.rule.set_metadata = 1;
  1687. flt_rule_entry.rule.retain_hdr = 1;
  1688. if (
  1689. ((uint8_t)-1 == FilterTable0.AddRuleToTable(flt_rule_entry)) ||
  1690. !m_filtering.AddFilteringRule(FilterTable0.GetFilteringTable())
  1691. )
  1692. {
  1693. LOG_MSG_ERROR("Error Adding Rule to Filter Table, aborting...\n");
  1694. return false;
  1695. }
  1696. else
  1697. {
  1698. LOG_MSG_DEBUG("flt rule hdl0=0x%x, status=0x%x\n", FilterTable0.ReadRuleFromTable(0)->flt_rule_hdl, FilterTable0.ReadRuleFromTable(0)->status);
  1699. }
  1700. //NAT table and rules creation
  1701. int total_entries = 20;
  1702. int ret;
  1703. ipa_nat_ipv4_rule ipv4_rule;
  1704. ipa_nat_pdn_entry pdn_info;
  1705. ret = ipa_nat_add_ipv4_tbl(m_public_ip, m_mem_type, total_entries, &m_tbl_hdl);
  1706. if (ret) {
  1707. LOG_MSG_ERROR("Leaving, failed creating NAT table\n");
  1708. return false;
  1709. }
  1710. LOG_MSG_DEBUG("nat table added, hdl %d, public ip 0x%X\n", m_tbl_hdl,
  1711. m_public_ip);
  1712. ipv4_rule.target_ip = m_target_ip;
  1713. ipv4_rule.target_port = m_target_port;
  1714. ipv4_rule.private_ip = m_private_ip;
  1715. ipv4_rule.private_port = m_private_port;
  1716. ipv4_rule.protocol = IPPROTO_TCP;
  1717. ipv4_rule.public_port = m_public_port;
  1718. ipv4_rule.pdn_index = 0;
  1719. // modify the PDN entries that will be pointed by the NAT rules
  1720. pdn_info.public_ip = m_public_ip;
  1721. pdn_info.src_metadata = m_metadata;
  1722. pdn_info.dst_metadata = 0;
  1723. ret = ipa_nat_modify_pdn(m_tbl_hdl, 0, &pdn_info);
  1724. if (ret) {
  1725. LOG_MSG_ERROR("Leaving, failed modifying PDN index 0\n");
  1726. return false;
  1727. }
  1728. LOG_MSG_DEBUG("PDN 0 was modified to hold ip 0x%X, src_metadata 0x%X\n", m_public_ip, m_metadata);
  1729. ret = ipa_nat_add_ipv4_rule(m_tbl_hdl, &ipv4_rule, &m_nat_rule_hdl1);
  1730. if (ret) {
  1731. LOG_MSG_ERROR("Leaving, failed adding NAT rule 0\n");
  1732. return false;
  1733. }
  1734. LOG_MSG_DEBUG("NAT rule added, hdl %d, data: 0x%X, %d, 0x%X, %d, %d, %d\n",
  1735. m_nat_rule_hdl1, ipv4_rule.target_ip, ipv4_rule.target_port,
  1736. ipv4_rule.private_ip, ipv4_rule.private_port,
  1737. ipv4_rule.protocol, ipv4_rule.public_port);
  1738. LOG_MSG_DEBUG("Leaving\n");
  1739. return true;
  1740. }// AddRules()
  1741. virtual bool ModifyPackets()
  1742. {
  1743. uint32_t address;
  1744. uint16_t port;
  1745. char flags = 0x18;
  1746. Load8021QPacket();
  1747. address = htonl(m_target_ip);//193.23.22.1
  1748. memcpy(&m_sendBuffer[IPV4_DST_ADDR_OFFSET + ETH8021Q_HEADER_LEN], &address, sizeof(address));
  1749. port = htons(m_target_port);
  1750. memcpy(&m_sendBuffer[IPV4_DST_PORT_OFFSET + ETH8021Q_HEADER_LEN], &port, sizeof(port));
  1751. address = htonl(m_private_ip);/* 194.23.22.1 */
  1752. memcpy(&m_sendBuffer[IPV4_SRC_ADDR_OFFSET + ETH8021Q_HEADER_LEN], &address, sizeof(address));
  1753. port = htons(m_private_port);
  1754. memcpy(&m_sendBuffer[IPV4_SRC_PORT_OFFSET + ETH8021Q_HEADER_LEN], &port, sizeof(port));
  1755. //make sure the FIN flag is not set, otherwise we will get a NAT miss
  1756. memcpy(&m_sendBuffer[IPV4_TCP_FLAGS_OFFSET + ETH8021Q_HEADER_LEN], &flags, sizeof(flags));
  1757. return true;
  1758. }// ModifyPacktes ()
  1759. virtual bool SendPackets()
  1760. {
  1761. bool isSuccess = false;
  1762. // Send first packet
  1763. isSuccess = m_producer2.SendData(m_sendBuffer, m_sendSize);
  1764. if (false == isSuccess)
  1765. {
  1766. LOG_MSG_ERROR("SendData failure.\n");
  1767. return false;
  1768. }
  1769. LOG_MSG_DEBUG("sent successfully one packet\n");
  1770. return true;
  1771. }
  1772. virtual bool ReceivePacketsAndCompare()
  1773. {
  1774. size_t receivedSize = 0;
  1775. bool isSuccess = true;
  1776. // Receive results
  1777. Byte *rxBuff1 = new Byte[0x400];
  1778. if (NULL == rxBuff1)
  1779. {
  1780. LOG_MSG_ERROR("Memory allocation error.\n");
  1781. return false;
  1782. }
  1783. receivedSize = m_consumer.ReceiveData(rxBuff1, 0x400);
  1784. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize, m_consumer.m_fromChannelName.c_str());
  1785. // Compare results
  1786. if (!CompareResultVsGoldenNat(
  1787. m_sendBuffer, m_sendSize,
  1788. rxBuff1, receivedSize,
  1789. m_private_ip, m_public_ip,
  1790. m_private_port, m_public_port,
  1791. true, ETH8021Q_HEADER_LEN))
  1792. {
  1793. LOG_MSG_ERROR("Comparison of Buffer0 Failed!\n");
  1794. isSuccess = false;
  1795. }
  1796. char recievedBuffer[256] = { 0 };
  1797. char SentBuffer[256] = { 0 };
  1798. size_t j;
  1799. for (j = 0; j < m_sendSize; j++)
  1800. snprintf(&SentBuffer[3 * j], sizeof(SentBuffer) - (3 * j + 1), " %02X", m_sendBuffer[j]);
  1801. for (j = 0; j < receivedSize; j++)
  1802. snprintf(&recievedBuffer[3 * j], sizeof(recievedBuffer) - (3 * j + 1), " %02X", rxBuff1[j]);
  1803. LOG_MSG_STACK("sent Value1 (%zu)\n%s\n, Received Value1(%zu)\n%s\n", m_sendSize, SentBuffer, receivedSize, recievedBuffer);
  1804. delete[] rxBuff1;
  1805. return isSuccess;
  1806. }
  1807. };
  1808. /*---------------------------------------------------------------------------*/
  1809. /* Test006: Single PDN dst metadata replacement NAT test */
  1810. /*---------------------------------------------------------------------------*/
  1811. class IpaNatBlockTest006 : public IpaNatBlockTestFixture
  1812. {
  1813. public:
  1814. IpaNatBlockTest006()
  1815. {
  1816. m_name = "IpaNatBlockTest006";
  1817. m_description =
  1818. "NAT block test 006 - single PDN dst metadata replacement NAT test\
  1819. destination metadata will be replaced and the routing rule equation will be done upon replaced value\
  1820. 1. Generate and commit two routing tables (only one is used). \
  1821. the routing table will catch packets with metadata value 0x34567890 (different from original value)\
  1822. 2. Generate and commit one filtering rule: (DST & Mask Match). \
  1823. action go to dst NAT \
  1824. All DST_IP == (192.23.22.1 & 0.255.255.255)traffic goes to NAT block \
  1825. action parameters metadata replacement = true\
  1826. 3. generate and commit one NAT rule:\
  1827. public ip 192.23.22.1 --> private ip 194.23.22.1 \
  1828. destination metadata value shall be replaced to 0x34567890 (caught by the routing rule)";
  1829. m_private_ip = 0xC2171601; /* 194.23.22.1 */
  1830. m_private_port = 5678;
  1831. m_public_ip = 0xC0171601; /* "192.23.22.1" */
  1832. m_public_port = 9050;
  1833. m_target_ip = 0xC1171601; /* 193.23.22.1 */
  1834. m_target_port = 1234;
  1835. m_metadata = 0x34567890;
  1836. m_minIPAHwType = IPA_HW_v4_0;
  1837. Register(*this);
  1838. }
  1839. virtual bool AddRules()
  1840. {
  1841. LOG_MSG_DEBUG("Entering\n");
  1842. const char bypass0[20] = "Bypass0";
  1843. struct ipa_ioc_get_rt_tbl routing_table0;
  1844. if (!CreateMetdataRoutingRule(bypass0))
  1845. {
  1846. LOG_MSG_ERROR("CreateThreeBypassRoutingTables Failed\n");
  1847. return false;
  1848. }
  1849. LOG_MSG_DEBUG("CreateMetdataRoutingRule completed successfully\n");
  1850. routing_table0.ip = IPA_IP_v4;
  1851. strlcpy(routing_table0.name, bypass0, sizeof(routing_table0.name));
  1852. if (!m_routing.GetRoutingTable(&routing_table0))
  1853. {
  1854. LOG_MSG_ERROR("m_routing.GetRoutingTable(&routing_table0=0x%p) Failed.\n", &routing_table0);
  1855. return false;
  1856. }
  1857. LOG_MSG_DEBUG("%s route table handle = %u\n", bypass0, routing_table0.hdl);
  1858. IPAFilteringTable FilterTable0;
  1859. struct ipa_flt_rule_add flt_rule_entry;
  1860. FilterTable0.Init(IPA_IP_v4, IPA_CLIENT_TEST2_PROD, false, 1);
  1861. LOG_MSG_DEBUG("FilterTable*.Init Completed Successfully..\n");
  1862. // Configuring Filtering Rule No.0
  1863. FilterTable0.GeneratePresetRule(1, flt_rule_entry);
  1864. flt_rule_entry.at_rear = true;
  1865. flt_rule_entry.flt_rule_hdl = -1; // return Value
  1866. flt_rule_entry.status = -1; // return value
  1867. flt_rule_entry.rule.action = IPA_PASS_TO_DST_NAT;
  1868. flt_rule_entry.rule.rt_tbl_hdl = routing_table0.hdl; //put here the handle corresponding to Routing Rule 1
  1869. flt_rule_entry.rule.attrib.attrib_mask = IPA_FLT_DST_ADDR;
  1870. flt_rule_entry.rule.attrib.u.v4.dst_addr_mask = 0xFFFFFFFF; // Mask
  1871. flt_rule_entry.rule.attrib.u.v4.dst_addr = m_public_ip; // Filter DST_IP == 193.23.22.1
  1872. flt_rule_entry.rule.pdn_idx = 0;
  1873. flt_rule_entry.rule.set_metadata = 1;
  1874. flt_rule_entry.rule.retain_hdr = 1;
  1875. if (
  1876. ((uint8_t)-1 == FilterTable0.AddRuleToTable(flt_rule_entry)) ||
  1877. !m_filtering.AddFilteringRule(FilterTable0.GetFilteringTable())
  1878. )
  1879. {
  1880. LOG_MSG_ERROR("Error Adding Rule to Filter Table, aborting...\n");
  1881. return false;
  1882. }
  1883. else
  1884. {
  1885. LOG_MSG_DEBUG("flt rule hdl0=0x%x, status=0x%x\n", FilterTable0.ReadRuleFromTable(0)->flt_rule_hdl, FilterTable0.ReadRuleFromTable(0)->status);
  1886. }
  1887. //NAT table and rules creation
  1888. int total_entries = 20;
  1889. int ret;
  1890. ipa_nat_ipv4_rule ipv4_rule;
  1891. ipa_nat_pdn_entry pdn_info;
  1892. ret = ipa_nat_add_ipv4_tbl(m_public_ip, m_mem_type, total_entries, &m_tbl_hdl);
  1893. if (ret) {
  1894. LOG_MSG_ERROR("Leaving, failed creating NAT table\n");
  1895. return false;
  1896. }
  1897. LOG_MSG_DEBUG("nat table added, hdl %d, public ip 0x%X\n", m_tbl_hdl,
  1898. m_public_ip);
  1899. ipv4_rule.target_ip = m_target_ip;
  1900. ipv4_rule.target_port = m_target_port;
  1901. ipv4_rule.private_ip = m_private_ip;
  1902. ipv4_rule.private_port = m_private_port;
  1903. ipv4_rule.protocol = IPPROTO_TCP;
  1904. ipv4_rule.public_port = m_public_port;
  1905. ipv4_rule.pdn_index = 0;
  1906. // modify the PDN entries that will be pointed by the NAT rules
  1907. pdn_info.public_ip = m_public_ip;
  1908. pdn_info.src_metadata = 0;
  1909. pdn_info.dst_metadata = m_metadata;
  1910. ret = ipa_nat_modify_pdn(m_tbl_hdl, 0, &pdn_info);
  1911. if (ret) {
  1912. LOG_MSG_ERROR("Leaving, failed modifying PDN index 0\n");
  1913. return false;
  1914. }
  1915. LOG_MSG_DEBUG("PDN 0 was modified to hold ip 0x%X, dst_metadata 0x%X\n", m_public_ip, m_metadata);
  1916. ret = ipa_nat_add_ipv4_rule(m_tbl_hdl, &ipv4_rule, &m_nat_rule_hdl1);
  1917. if (ret) {
  1918. LOG_MSG_ERROR("Leaving, failed adding NAT rule 0\n");
  1919. return false;
  1920. }
  1921. LOG_MSG_ERROR("NAT rule added, hdl %d, data: 0x%X, %d, 0x%X, %d, %d, %d\n",
  1922. m_nat_rule_hdl1, ipv4_rule.target_ip, ipv4_rule.target_port,
  1923. ipv4_rule.private_ip, ipv4_rule.private_port,
  1924. ipv4_rule.protocol, ipv4_rule.public_port);
  1925. LOG_MSG_DEBUG("Leaving\n");
  1926. return true;
  1927. }// AddRules()
  1928. virtual bool ModifyPackets()
  1929. {
  1930. uint32_t address;
  1931. uint16_t port;
  1932. char flags = 0x18;
  1933. Load8021QPacket();
  1934. address = htonl(m_public_ip);//193.23.22.1
  1935. memcpy(&m_sendBuffer[IPV4_DST_ADDR_OFFSET + ETH8021Q_HEADER_LEN], &address, sizeof(address));
  1936. port = htons(m_public_port);
  1937. memcpy(&m_sendBuffer[IPV4_DST_PORT_OFFSET + ETH8021Q_HEADER_LEN], &port, sizeof(port));
  1938. address = htonl(m_target_ip);/* 194.23.22.1 */
  1939. memcpy(&m_sendBuffer[IPV4_SRC_ADDR_OFFSET + ETH8021Q_HEADER_LEN], &address, sizeof(address));
  1940. port = htons(m_target_port);
  1941. memcpy(&m_sendBuffer[IPV4_SRC_PORT_OFFSET + ETH8021Q_HEADER_LEN], &port, sizeof(port));
  1942. //make sure the FIN flag is not set, otherwise we will get a NAT miss
  1943. memcpy(&m_sendBuffer[IPV4_TCP_FLAGS_OFFSET + ETH8021Q_HEADER_LEN], &flags, sizeof(flags));
  1944. return true;
  1945. }// ModifyPacktes ()
  1946. virtual bool SendPackets()
  1947. {
  1948. bool isSuccess = false;
  1949. // Send first packet
  1950. isSuccess = m_producer2.SendData(m_sendBuffer, m_sendSize);
  1951. if (false == isSuccess)
  1952. {
  1953. LOG_MSG_ERROR("SendData failure.\n");
  1954. return false;
  1955. }
  1956. LOG_MSG_DEBUG("sent successfully one packet\n");
  1957. return true;
  1958. }
  1959. virtual bool ReceivePacketsAndCompare()
  1960. {
  1961. size_t receivedSize = 0;
  1962. bool isSuccess = true;
  1963. // Receive results
  1964. Byte *rxBuff1 = new Byte[0x400];
  1965. if (NULL == rxBuff1)
  1966. {
  1967. LOG_MSG_ERROR("Memory allocation error.\n");
  1968. return false;
  1969. }
  1970. receivedSize = m_consumer.ReceiveData(rxBuff1, 0x400);
  1971. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize, m_consumer.m_fromChannelName.c_str());
  1972. // Compare results
  1973. if (!CompareResultVsGoldenNat(
  1974. m_sendBuffer, m_sendSize,
  1975. rxBuff1, receivedSize,
  1976. m_private_ip, m_public_ip,
  1977. m_private_port, m_public_port,
  1978. false, ETH8021Q_HEADER_LEN))
  1979. {
  1980. LOG_MSG_ERROR("Comparison of Buffer0 Failed!\n");
  1981. isSuccess = false;
  1982. }
  1983. char recievedBuffer[256] = { 0 };
  1984. char SentBuffer[256] = { 0 };
  1985. size_t j;
  1986. for (j = 0; j < m_sendSize; j++)
  1987. snprintf(&SentBuffer[3 * j], sizeof(SentBuffer) - (3 * j + 1), " %02X", m_sendBuffer[j]);
  1988. for (j = 0; j < receivedSize; j++)
  1989. snprintf(&recievedBuffer[3 * j], sizeof(recievedBuffer) - (3 * j + 1), " %02X", rxBuff1[j]);
  1990. LOG_MSG_STACK("sent Value1 (%zu)\n%s\n, Received Value1(%zu)\n%s\n", m_sendSize, SentBuffer, receivedSize, recievedBuffer);
  1991. delete[] rxBuff1;
  1992. return isSuccess;
  1993. }
  1994. };
  1995. /*---------------------------------------------------------------------------*/
  1996. /* Test007: Hashable routing rule with dst NAT test */
  1997. /*---------------------------------------------------------------------------*/
  1998. class IpaNatBlockTest007 : public IpaNatBlockTestFixture
  1999. {
  2000. public:
  2001. IpaNatBlockTest007()
  2002. {
  2003. m_name = "IpaNatBlockTest007";
  2004. m_description =
  2005. "NAT block test 007 - single PDN dst NAT with hashable routing rule test\
  2006. test if routing block hash mechanism tests NATed values or pre NAT values\
  2007. 1. Generate and commit routing table with two hashable rules. \
  2008. first routing rule will send packets with ip == 192.168.9.119 to first pipe \
  2009. second routing rule will send packets with ip == 192.168.9.120 to second pipe\
  2010. 2. Generate and commit one filtering rule: (DST & Mask Match). \
  2011. action go to dst NAT \
  2012. All DST_IP == (192.168.9.1 & 255.255.255.0)traffic goes to NAT block \
  2013. 3. generate and commit two NAT rules with target ip 211.1.1.4:\
  2014. 1. public ip 5.5.6.120 --> private ip 192.168.9.119 \
  2015. public port 4501 --> private port 4500 \
  2016. 2. public ip 5.5.6.120 --> private ip 192.168.9.119 \
  2017. public port 4502 --> private port 4500";
  2018. m_private_ip = 0xC0A80977; /* 192.168.9.119 */
  2019. m_private_port = 4500;
  2020. m_private_ip2 = 0xC0A80978; /* 192.168.9.120 */
  2021. m_private_port2 = 4500;
  2022. m_public_ip = 0x05050678; /* 5.5.6.120 */
  2023. m_public_port = 4501;
  2024. m_public_port2 = 4502;
  2025. m_target_ip = 0xD3010104; /* 211.1.1.4 */
  2026. m_target_port = 1234;
  2027. m_minIPAHwType = IPA_HW_v4_0;
  2028. Register(*this);
  2029. }
  2030. bool Setup()
  2031. {
  2032. return IpaNatBlockTestFixture::Setup(true);
  2033. }
  2034. virtual bool AddRules()
  2035. {
  2036. LOG_MSG_DEBUG("Entering\n");
  2037. const char bypass0[20] = "Bypass0";
  2038. struct ipa_ioc_get_rt_tbl routing_table0;
  2039. if (!CreateHashableRoutingRules(bypass0))
  2040. {
  2041. LOG_MSG_ERROR("CreateThreeBypassRoutingTables Failed\n");
  2042. return false;
  2043. }
  2044. LOG_MSG_DEBUG("CreateHashableRoutingRules completed successfully\n");
  2045. routing_table0.ip = IPA_IP_v4;
  2046. strlcpy(routing_table0.name, bypass0, sizeof(routing_table0.name));
  2047. if (!m_routing.GetRoutingTable(&routing_table0))
  2048. {
  2049. LOG_MSG_ERROR("m_routing.GetRoutingTable(&routing_table0=0x%p) Failed.\n", &routing_table0);
  2050. return false;
  2051. }
  2052. LOG_MSG_DEBUG("%s route table handle = %u\n", bypass0, routing_table0.hdl);
  2053. IPAFilteringTable FilterTable0;
  2054. struct ipa_flt_rule_add flt_rule_entry;
  2055. FilterTable0.Init(IPA_IP_v4, IPA_CLIENT_TEST_PROD, false, 1);
  2056. LOG_MSG_DEBUG("FilterTable*.Init Completed Successfully..\n");
  2057. // Configuring Filtering Rule No.0
  2058. FilterTable0.GeneratePresetRule(1, flt_rule_entry);
  2059. flt_rule_entry.at_rear = true;
  2060. flt_rule_entry.flt_rule_hdl = -1; // return Value
  2061. flt_rule_entry.status = -1; // return value
  2062. flt_rule_entry.rule.action = IPA_PASS_TO_DST_NAT;
  2063. flt_rule_entry.rule.rt_tbl_hdl = routing_table0.hdl; //put here the handle corresponding to Routing table 1
  2064. flt_rule_entry.rule.attrib.attrib_mask = IPA_FLT_DST_ADDR;
  2065. flt_rule_entry.rule.attrib.u.v4.dst_addr_mask = 0x00000000; // Mask - catch all
  2066. flt_rule_entry.rule.attrib.u.v4.dst_addr = m_public_ip; // Filter DST_IP == 5.5.6.120
  2067. flt_rule_entry.rule.pdn_idx = 0;
  2068. flt_rule_entry.rule.set_metadata = 0;
  2069. if (
  2070. ((uint8_t)-1 == FilterTable0.AddRuleToTable(flt_rule_entry)) ||
  2071. !m_filtering.AddFilteringRule(FilterTable0.GetFilteringTable())
  2072. )
  2073. {
  2074. LOG_MSG_ERROR("Error Adding Rule to Filter Table, aborting...\n");
  2075. return false;
  2076. }
  2077. else
  2078. {
  2079. LOG_MSG_DEBUG("flt rule hdl0=0x%x, status=0x%x\n", FilterTable0.ReadRuleFromTable(0)->flt_rule_hdl, FilterTable0.ReadRuleFromTable(0)->status);
  2080. }
  2081. //NAT table and rules creation
  2082. int total_entries = 20;
  2083. int ret;
  2084. ipa_nat_ipv4_rule ipv4_rule;
  2085. ret = ipa_nat_add_ipv4_tbl(m_public_ip, m_mem_type, total_entries, &m_tbl_hdl);
  2086. if (ret) {
  2087. LOG_MSG_ERROR("Leaving, failed creating NAT table\n");
  2088. return false;
  2089. }
  2090. LOG_MSG_DEBUG("nat table added, hdl %d, public ip 0x%X\n", m_tbl_hdl,
  2091. m_public_ip);
  2092. ipv4_rule.target_ip = m_target_ip;
  2093. ipv4_rule.target_port = m_target_port;
  2094. ipv4_rule.private_ip = m_private_ip;
  2095. ipv4_rule.private_port = m_private_port;
  2096. ipv4_rule.protocol = IPPROTO_TCP;
  2097. ipv4_rule.public_port = m_public_port;
  2098. ipv4_rule.pdn_index = 0;
  2099. ret = ipa_nat_add_ipv4_rule(m_tbl_hdl, &ipv4_rule, &m_nat_rule_hdl1);
  2100. if (ret) {
  2101. LOG_MSG_ERROR("Leaving, failed adding NAT rule 0\n");
  2102. return false;
  2103. }
  2104. LOG_MSG_DEBUG("NAT rule 1 added, hdl %d, data: 0x%X, %d, 0x%X, %d, %d, %d\n",
  2105. m_nat_rule_hdl1, ipv4_rule.target_ip, ipv4_rule.target_port,
  2106. ipv4_rule.private_ip, ipv4_rule.private_port,
  2107. ipv4_rule.protocol, ipv4_rule.public_port);
  2108. ipv4_rule.target_ip = m_target_ip;
  2109. ipv4_rule.target_port = m_target_port;
  2110. ipv4_rule.private_ip = m_private_ip2;
  2111. ipv4_rule.private_port = m_private_port2;
  2112. ipv4_rule.protocol = IPPROTO_TCP;
  2113. ipv4_rule.public_port = m_public_port2;
  2114. ipv4_rule.pdn_index = 0;
  2115. ret = ipa_nat_add_ipv4_rule(m_tbl_hdl, &ipv4_rule, &m_nat_rule_hdl1);
  2116. if (ret) {
  2117. LOG_MSG_ERROR("Leaving, failed adding NAT rule 0\n");
  2118. return false;
  2119. }
  2120. LOG_MSG_DEBUG("NAT rule 2 added, hdl %d, data: 0x%X, %d, 0x%X, %d, %d, %d\n",
  2121. m_nat_rule_hdl1, ipv4_rule.target_ip, ipv4_rule.target_port,
  2122. ipv4_rule.private_ip, ipv4_rule.private_port,
  2123. ipv4_rule.protocol, ipv4_rule.public_port);
  2124. LOG_MSG_DEBUG("Leaving\n");
  2125. return true;
  2126. }// AddRules()
  2127. virtual bool ModifyPackets()
  2128. {
  2129. uint32_t address;
  2130. uint16_t port;
  2131. char flags = 0x18;
  2132. address = ntohl(m_public_ip);//5.5.6.120
  2133. memcpy(&m_sendBuffer[IPV4_DST_ADDR_OFFSET], &address, sizeof(address));
  2134. port = ntohs(m_public_port); // 4501
  2135. memcpy(&m_sendBuffer[IPV4_DST_PORT_OFFSET], &port, sizeof(port));
  2136. address = ntohl(m_target_ip);/* 211.1.1.4 */
  2137. memcpy(&m_sendBuffer[IPV4_SRC_ADDR_OFFSET], &address, sizeof(address));
  2138. port = ntohs(m_target_port); // 4500
  2139. memcpy(&m_sendBuffer[IPV4_SRC_PORT_OFFSET], &port, sizeof(port));
  2140. //make sure the FIN flag is not set, otherwise we will get a NAT miss
  2141. memcpy(&m_sendBuffer[IPV4_TCP_FLAGS_OFFSET], &flags, sizeof(flags));
  2142. // second packet
  2143. address = ntohl(m_public_ip);//5.5.6.120
  2144. memcpy(&m_sendBuffer2[IPV4_DST_ADDR_OFFSET], &address, sizeof(address));
  2145. port = ntohs(m_public_port2); // 4502
  2146. memcpy(&m_sendBuffer2[IPV4_DST_PORT_OFFSET], &port, sizeof(port));
  2147. address = ntohl(m_target_ip);/* 211.1.1.4 */
  2148. memcpy(&m_sendBuffer2[IPV4_SRC_ADDR_OFFSET], &address, sizeof(address));
  2149. port = ntohs(m_target_port); // 4500
  2150. memcpy(&m_sendBuffer2[IPV4_SRC_PORT_OFFSET], &port, sizeof(port));
  2151. //make sure the FIN flag is not set, otherwise we will get a NAT miss
  2152. memcpy(&m_sendBuffer2[IPV4_TCP_FLAGS_OFFSET], &flags, sizeof(flags));
  2153. return true;
  2154. }// ModifyPacktes ()
  2155. virtual bool SendPackets()
  2156. {
  2157. bool isSuccess = false;
  2158. // Send first packet
  2159. isSuccess = m_producer.SendData(m_sendBuffer, m_sendSize);
  2160. if (false == isSuccess)
  2161. {
  2162. LOG_MSG_ERROR("SendData failure.\n");
  2163. return false;
  2164. }
  2165. LOG_MSG_DEBUG("sent successfully the first packet\n");
  2166. isSuccess = m_producer.SendData(m_sendBuffer2, m_sendSize2);
  2167. if (false == isSuccess)
  2168. {
  2169. LOG_MSG_ERROR("SendData failure.\n");
  2170. return false;
  2171. }
  2172. LOG_MSG_DEBUG("sent successfully two packet\n");
  2173. return true;
  2174. }
  2175. virtual bool ReceivePacketsAndCompare()
  2176. {
  2177. size_t receivedSize = 0;
  2178. size_t receivedSize2 = 0;
  2179. bool isSuccess = true;
  2180. // Receive results
  2181. Byte *rxBuff1 = new Byte[0x400];
  2182. Byte *rxBuff2 = new Byte[0x400];
  2183. if (rxBuff1 == NULL || rxBuff2 == NULL)
  2184. {
  2185. LOG_MSG_ERROR("Memory allocation error.\n");
  2186. return false;
  2187. }
  2188. receivedSize = m_consumer.ReceiveData(rxBuff1, 0x400);
  2189. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize, m_consumer.m_fromChannelName.c_str());
  2190. receivedSize2 = m_consumer2.ReceiveData(rxBuff2, 0x400);
  2191. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize2, m_consumer2.m_fromChannelName.c_str());
  2192. // Compare results
  2193. if (!CompareResultVsGoldenNat(
  2194. m_sendBuffer, m_sendSize,
  2195. rxBuff1, receivedSize,
  2196. m_private_ip, m_public_ip,
  2197. m_private_port, m_public_port,
  2198. false, 0, true))
  2199. {
  2200. LOG_MSG_ERROR("Comparison of Buffer0 Failed!\n");
  2201. isSuccess = false;
  2202. }
  2203. isSuccess &= (TestManager::GetInstance()->GetIPAHwType() >= IPA_HW_v5_0) ?
  2204. IsCacheMiss_v5_0(m_sendSize, receivedSize, rxBuff1) : IsCacheMiss(m_sendSize, receivedSize, rxBuff1);
  2205. char recievedBuffer[0x400] = { 0 };
  2206. char SentBuffer[0x400] = { 0 };
  2207. char recievedBuffer2[0x400] = { 0 };
  2208. char SentBuffer2[0x400] = { 0 };
  2209. size_t j;
  2210. for (j = 0; j < m_sendSize; j++)
  2211. snprintf(&SentBuffer[3 * j], sizeof(SentBuffer) - (3 * j + 1), " %02X", m_sendBuffer[j]);
  2212. for (j = 0; j < receivedSize; j++)
  2213. snprintf(&recievedBuffer[3 * j], sizeof(recievedBuffer) - (3 * j + 1), " %02X", rxBuff1[j]);
  2214. LOG_MSG_STACK("sent Value1 (%zu)\n%s\n, Received Value1(%zu)\n%s\n", m_sendSize, SentBuffer, receivedSize, recievedBuffer);
  2215. delete[] rxBuff1;
  2216. isSuccess &= CompareResultVsGoldenNat(
  2217. m_sendBuffer2, m_sendSize2,
  2218. rxBuff2, receivedSize2,
  2219. m_private_ip2, m_public_ip,
  2220. m_private_port2, m_public_port2,
  2221. false, 0, true);
  2222. isSuccess &= (TestManager::GetInstance()->GetIPAHwType() >= IPA_HW_v5_0) ?
  2223. IsCacheMiss_v5_0(m_sendSize2, receivedSize2, rxBuff2) : IsCacheMiss(m_sendSize2, receivedSize2, rxBuff2);
  2224. for (j = 0; j < m_sendSize2; j++)
  2225. snprintf(&SentBuffer2[3 * j], sizeof(SentBuffer2) - (3 * j + 1), " %02X", m_sendBuffer2[j]);
  2226. for (j = 0; j < receivedSize2; j++)
  2227. snprintf(&recievedBuffer2[3 * j], sizeof(recievedBuffer2) - (3 * j + 1), " %02X", rxBuff2[j]);
  2228. LOG_MSG_STACK("sent Value1 (%zu)\n%s\n, Received Value1(%zu)\n%s\n", m_sendSize2, SentBuffer2, receivedSize2, recievedBuffer2);
  2229. delete[] rxBuff2;
  2230. return isSuccess;
  2231. }
  2232. };
  2233. /*---------------------------------------------------------------------------*/
  2234. /* Test008: Multi PDN src NAT test match PDN by input from filtering block */
  2235. /*---------------------------------------------------------------------------*/
  2236. class IpaNatBlockTest008 : public IpaNatBlockTestFixture
  2237. {
  2238. public:
  2239. IpaNatBlockTest008()
  2240. {
  2241. m_name = "IpaNatBlockTest008";
  2242. m_description =
  2243. "NAT block test 008 - Multi PDN src NAT test match PDN by input from filtering block\
  2244. 1. Generate and commit three routing tables (two are used). \
  2245. Each table contains a single \"bypass\" rule (all data goes to output pipe 0, 1 and 2 (accordingly)) \
  2246. 2. Generate and commit one filtering rule: (DST & Mask Match). \
  2247. - action go to src NAT + PDN index 2\
  2248. All SRC_IP == (194.23.22.1 & 0.255.255.255)traffic goes to NAT block \
  2249. 3. generate and commit two NAT rules:\
  2250. private ip 194.23.22.1 --> public ip 192.23.22.1 PDN index 1\
  2251. private ip 194.23.22.1 --> public ip 195.23.22.1 PDN index 2";
  2252. m_private_ip = 0xC2171601; /* 194.23.22.1 */
  2253. m_private_port = 5678;
  2254. m_public_ip = 0xC0171601; /* "192.23.22.1" */
  2255. m_public_port = 9050;
  2256. m_public_ip2 = 0xC3171601; /* "195.23.22.1" */
  2257. m_target_ip = 0xC1171601; /* 193.23.22.1 */
  2258. m_target_port = 1234;
  2259. m_minIPAHwType = IPA_HW_v4_0;
  2260. Register(*this);
  2261. }
  2262. virtual bool AddRules()
  2263. {
  2264. LOG_MSG_DEBUG("Entering\n");
  2265. const char bypass0[20] = "Bypass0";
  2266. const char bypass1[20] = "Bypass1";
  2267. const char bypass2[20] = "Bypass2";
  2268. struct ipa_ioc_get_rt_tbl routing_table0, routing_table1;
  2269. if (!CreateThreeIPv4BypassRoutingTables(bypass0, bypass1, bypass2))
  2270. {
  2271. LOG_MSG_ERROR("CreateThreeBypassRoutingTables Failed\n");
  2272. return false;
  2273. }
  2274. LOG_MSG_DEBUG("CreateThreeBypassRoutingTables completed successfully\n");
  2275. routing_table0.ip = IPA_IP_v4;
  2276. strlcpy(routing_table0.name, bypass0, sizeof(routing_table0.name));
  2277. if (!m_routing.GetRoutingTable(&routing_table0))
  2278. {
  2279. LOG_MSG_ERROR("m_routing.GetRoutingTable(&routing_table0=0x%p) Failed.\n", &routing_table0);
  2280. return false;
  2281. }
  2282. LOG_MSG_DEBUG("%s route table handle = %u\n", bypass0, routing_table0.hdl);
  2283. routing_table1.ip = IPA_IP_v4;
  2284. strlcpy(routing_table1.name, bypass1, sizeof(routing_table1.name));
  2285. if (!m_routing.GetRoutingTable(&routing_table1))
  2286. {
  2287. LOG_MSG_ERROR("m_routing.GetRoutingTable(&routing_table1=0x%p) Failed.\n", &routing_table1);
  2288. return false;
  2289. }
  2290. LOG_MSG_DEBUG("%s route table handle = %u\n", bypass1, routing_table1.hdl);
  2291. IPAFilteringTable FilterTable0;
  2292. struct ipa_flt_rule_add flt_rule_entry;
  2293. FilterTable0.Init(IPA_IP_v4, IPA_CLIENT_TEST_PROD, false, 2);
  2294. LOG_MSG_DEBUG("FilterTable*.Init Completed Successfully..\n");
  2295. // Configuring Filtering Rule No.0
  2296. FilterTable0.GeneratePresetRule(1, flt_rule_entry);
  2297. flt_rule_entry.at_rear = true;
  2298. flt_rule_entry.flt_rule_hdl = -1; // return Value
  2299. flt_rule_entry.status = -1; // return value
  2300. flt_rule_entry.rule.action = IPA_PASS_TO_SRC_NAT;
  2301. flt_rule_entry.rule.rt_tbl_hdl = routing_table0.hdl; //put here the handle corresponding to Routing Rule 1
  2302. flt_rule_entry.rule.attrib.attrib_mask = IPA_FLT_SRC_ADDR;
  2303. flt_rule_entry.rule.attrib.u.v4.src_addr_mask = 0xFFFFFFFF; // Mask
  2304. flt_rule_entry.rule.attrib.u.v4.src_addr = m_private_ip; // Filter SRC_IP == 194.23.22.1
  2305. flt_rule_entry.rule.pdn_idx = 2;
  2306. flt_rule_entry.rule.set_metadata = 0;
  2307. if (
  2308. ((uint8_t)-1 == FilterTable0.AddRuleToTable(flt_rule_entry)) ||
  2309. !m_filtering.AddFilteringRule(FilterTable0.GetFilteringTable())
  2310. )
  2311. {
  2312. LOG_MSG_ERROR("Error Adding Rule to Filter Table, aborting...\n");
  2313. return false;
  2314. }
  2315. else
  2316. {
  2317. LOG_MSG_ERROR("flt rule hdl0=0x%x, status=0x%x\n", FilterTable0.ReadRuleFromTable(0)->flt_rule_hdl, FilterTable0.ReadRuleFromTable(0)->status);
  2318. }
  2319. //NAT table and rules creation
  2320. int total_entries = 20;
  2321. int ret;
  2322. ipa_nat_ipv4_rule ipv4_rule;
  2323. uint32_t pub_ip_add = m_public_ip;
  2324. ipa_nat_pdn_entry pdn_info;
  2325. // first create the NAT table
  2326. ret = ipa_nat_add_ipv4_tbl(pub_ip_add, m_mem_type, total_entries, &m_tbl_hdl);
  2327. if (ret) {
  2328. LOG_MSG_ERROR("Leaving, failed creating NAT table\n");
  2329. return false;
  2330. }
  2331. LOG_MSG_DEBUG("nat table added, hdl %d, public ip 0x%X\n", m_tbl_hdl,
  2332. pub_ip_add);
  2333. // modify the PDN entries that will be pointed by the NAT rules
  2334. pdn_info.public_ip = m_public_ip;
  2335. pdn_info.src_metadata = 0;
  2336. pdn_info.dst_metadata = 0;
  2337. ret = ipa_nat_modify_pdn(m_tbl_hdl, 1, &pdn_info);
  2338. if (ret) {
  2339. LOG_MSG_ERROR("Leaving, failed Modifying PDN entry 0 \n");
  2340. return false;
  2341. }
  2342. pdn_info.public_ip = m_public_ip2;
  2343. pdn_info.src_metadata = 0;
  2344. pdn_info.dst_metadata = 0;
  2345. ret = ipa_nat_modify_pdn(m_tbl_hdl, 2, &pdn_info);
  2346. if (ret) {
  2347. LOG_MSG_ERROR("Leaving, failed Modifying PDN entry 1 \n");
  2348. return false;
  2349. }
  2350. ipv4_rule.target_ip = m_target_ip;
  2351. ipv4_rule.target_port = m_target_port;
  2352. ipv4_rule.private_ip = m_private_ip;
  2353. ipv4_rule.private_port = m_private_port;
  2354. ipv4_rule.protocol = IPPROTO_TCP;
  2355. ipv4_rule.public_port = m_public_port;
  2356. ipv4_rule.pdn_index = 1;
  2357. ret = ipa_nat_add_ipv4_rule(m_tbl_hdl, &ipv4_rule, &m_nat_rule_hdl1);
  2358. if (ret) {
  2359. LOG_MSG_ERROR("Leaving, failed adding NAT rule 0\n");
  2360. return false;
  2361. }
  2362. LOG_MSG_DEBUG("NAT rule added, hdl %d, data: 0x%X, %d, 0x%X, %d, %d, %d\n",
  2363. m_nat_rule_hdl1, ipv4_rule.target_ip, ipv4_rule.target_port,
  2364. ipv4_rule.private_ip, ipv4_rule.private_port,
  2365. ipv4_rule.protocol, ipv4_rule.public_port);
  2366. // the second rule shall be identical to the first on all parameters except PDN index so the filtering
  2367. // block action parameter will provide the PDN index.
  2368. ipv4_rule.pdn_index = 2;
  2369. ret = ipa_nat_add_ipv4_rule(m_tbl_hdl, &ipv4_rule, &m_nat_rule_hdl1);
  2370. if (ret) {
  2371. LOG_MSG_ERROR("Leaving, failed adding NAT rule 1\n");
  2372. return false;
  2373. }
  2374. LOG_MSG_DEBUG("NAT rule 2 added, hdl %d, data: 0x%X, %d, 0x%X, %d, %d, %d\n",
  2375. m_nat_rule_hdl1, ipv4_rule.target_ip, ipv4_rule.target_port,
  2376. ipv4_rule.private_ip, ipv4_rule.private_port,
  2377. ipv4_rule.protocol, ipv4_rule.public_port);
  2378. LOG_MSG_DEBUG("Leaving\n");
  2379. return true;
  2380. }// AddRules()
  2381. virtual bool ModifyPackets()
  2382. {
  2383. uint32_t address;
  2384. uint16_t port;
  2385. char flags = 0x18;
  2386. //first packet private ip 194.23.22.1 --> public ip 195.23.22.1
  2387. address = htonl(m_target_ip);//193.23.22.1
  2388. memcpy(&m_sendBuffer[IPV4_DST_ADDR_OFFSET], &address, sizeof(address));
  2389. port = htons(m_target_port);
  2390. memcpy(&m_sendBuffer[IPV4_DST_PORT_OFFSET], &port, sizeof(port));
  2391. address = htonl(m_private_ip);/* 194.23.22.1 */
  2392. memcpy(&m_sendBuffer[IPV4_SRC_ADDR_OFFSET], &address, sizeof(address));
  2393. port = htons(m_private_port);
  2394. memcpy(&m_sendBuffer[IPV4_SRC_PORT_OFFSET], &port, sizeof(port));
  2395. //make sure the FIN flag is not set, otherwise we will get a NAT miss
  2396. memcpy(&m_sendBuffer[IPV4_TCP_FLAGS_OFFSET], &flags, sizeof(flags));
  2397. return true;
  2398. }// ModifyPacktes ()
  2399. virtual bool SendPackets()
  2400. {
  2401. bool isSuccess = false;
  2402. // Send first packet
  2403. isSuccess = m_producer.SendData(m_sendBuffer, m_sendSize);
  2404. if (false == isSuccess)
  2405. {
  2406. LOG_MSG_ERROR("SendData failure.\n");
  2407. return false;
  2408. }
  2409. return true;
  2410. }
  2411. virtual bool ReceivePacketsAndCompare()
  2412. {
  2413. size_t receivedSize = 0;
  2414. bool isSuccess = true;
  2415. // Receive results
  2416. Byte *rxBuff1 = new Byte[0x400];
  2417. if (rxBuff1 == NULL)
  2418. {
  2419. LOG_MSG_ERROR("Memory allocation error.\n");
  2420. return false;
  2421. }
  2422. receivedSize = m_consumer.ReceiveData(rxBuff1, 0x400);
  2423. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize, m_consumer.m_fromChannelName.c_str());
  2424. // Compare results - verify that the ip of PDN 2 was selected (m_public_ip2)
  2425. if (!CompareResultVsGoldenNat(
  2426. m_sendBuffer, m_sendSize,
  2427. rxBuff1, receivedSize,
  2428. m_private_ip, m_public_ip2,
  2429. m_private_port, m_public_port,
  2430. true))
  2431. {
  2432. LOG_MSG_ERROR("Comparison of Buffer0 Failed!\n");
  2433. isSuccess = false;
  2434. }
  2435. char recievedBuffer[256] = { 0 };
  2436. char SentBuffer[256] = { 0 };
  2437. size_t j;
  2438. for (j = 0; j < m_sendSize; j++)
  2439. snprintf(&SentBuffer[3 * j], sizeof(SentBuffer) - (3 * j + 1), " %02X", m_sendBuffer[j]);
  2440. for (j = 0; j < receivedSize; j++)
  2441. snprintf(&recievedBuffer[3 * j], sizeof(recievedBuffer) - (3 * j + 1), " %02X", rxBuff1[j]);
  2442. LOG_MSG_STACK("sent Value1 (%zu)\n%s\n, Received Value1(%zu)\n%s\n", m_sendSize, SentBuffer, receivedSize, recievedBuffer);
  2443. delete[] rxBuff1;
  2444. return isSuccess;
  2445. }
  2446. };
  2447. /*---------------------------------------------------------------------------*/
  2448. /* Test009: Single PDN src NAT delete rule test */
  2449. /*---------------------------------------------------------------------------*/
  2450. class IpaNatBlockTest009 : public IpaNatBlockTest001
  2451. {
  2452. public:
  2453. IpaNatBlockTest009()
  2454. {
  2455. m_name = "IpaNatBlockTest009";
  2456. m_description =
  2457. "NAT block test 009 - single PDN src NAT rule deletion test\
  2458. 1. Generate and commit three routing tables (only one is used). \
  2459. Each table contains a single \"bypass\" rule (all data goes to output pipe 0, 1 and 2 (accordingly)) \
  2460. 2. Generate and commit one filtering rule: (DST & Mask Match). \
  2461. action go to src NAT \
  2462. All DST_IP == (193.23.22.1 & 0.255.255.255)traffic goes to NAT block \
  2463. 3. generate and commit one NAT rule:\
  2464. private ip 194.23.22.1 --> public ip 192.23.22.1\
  2465. 4. delete the NAT rule and expect NAT miss";
  2466. }
  2467. virtual bool AddRules()
  2468. {
  2469. LOG_MSG_DEBUG("Entering\n");
  2470. int ret = IpaNatBlockTest001::AddRules();
  2471. if (!ret) {
  2472. LOG_MSG_ERROR("Leaving, failed Adding test 001 rules 0\n");
  2473. return false;
  2474. }
  2475. ret = ipa_nat_del_ipv4_rule(m_tbl_hdl, m_nat_rule_hdl1);
  2476. if (ret) {
  2477. LOG_MSG_ERROR("Leaving, failed deleting NAT rule 0\n");
  2478. return false;
  2479. }
  2480. LOG_MSG_DEBUG("NAT rule deleted\n");
  2481. LOG_MSG_DEBUG("Leaving\n");
  2482. return true;
  2483. }// AddRules()
  2484. virtual bool ReceivePacketsAndCompare()
  2485. {
  2486. size_t receivedSize = 0;
  2487. bool isSuccess = true;
  2488. // Receive results
  2489. Byte *rxBuff1 = new Byte[0x400];
  2490. if (NULL == rxBuff1)
  2491. {
  2492. LOG_MSG_ERROR("Memory allocation error.\n");
  2493. return false;
  2494. }
  2495. receivedSize = m_consumer.ReceiveData(rxBuff1, 0x400);
  2496. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize, m_consumer.m_fromChannelName.c_str());
  2497. if (receivedSize) {
  2498. LOG_MSG_ERROR("Data received - test failed!\n");
  2499. isSuccess = false;
  2500. // Compare results
  2501. if (!CompareResultVsGoldenNat(
  2502. m_sendBuffer, m_sendSize,
  2503. rxBuff1, receivedSize,
  2504. m_private_ip, m_public_ip,
  2505. m_private_port, m_public_port,
  2506. true))
  2507. {
  2508. LOG_MSG_ERROR("Comparison of Buffer0 Failed!\n");
  2509. } else {
  2510. LOG_MSG_ERROR("Comparison of Buffer0 succeeded - NAT rule was hit despite deletion!\n");
  2511. }
  2512. char recievedBuffer[256] = { 0 };
  2513. char SentBuffer[256] = { 0 };
  2514. size_t j;
  2515. for (j = 0; j < m_sendSize; j++)
  2516. snprintf(&SentBuffer[3 * j], sizeof(SentBuffer) - (3 * j + 1), " %02X", m_sendBuffer[j]);
  2517. for (j = 0; j < receivedSize; j++)
  2518. snprintf(&recievedBuffer[3 * j], sizeof(recievedBuffer) - (3 * j + 1), " %02X", rxBuff1[j]);
  2519. LOG_MSG_STACK("sent Value1 (%zu)\n%s\n, Received Value1(%zu)\n%s\n", m_sendSize, SentBuffer, receivedSize, recievedBuffer);
  2520. }
  2521. delete[] rxBuff1;
  2522. return isSuccess;
  2523. }
  2524. };
  2525. /*---------------------------------------------------------------------------*/
  2526. /* Test010: Single PDN dst NAT rule deletion test */
  2527. /*---------------------------------------------------------------------------*/
  2528. class IpaNatBlockTest010 : public IpaNatBlockTest002
  2529. {
  2530. public:
  2531. IpaNatBlockTest010()
  2532. {
  2533. m_name = "IpaNatBlockTest010";
  2534. m_description =
  2535. "NAT block test 010 - single PDN dst NAT rule deletion test\
  2536. 1. Generate and commit three routing tables (only one is used). \
  2537. Each table contains a single \"bypass\" rule (all data goes to output pipe 0, 1 and 2 (accordingly)) \
  2538. 2. Generate and commit one filtering rule: (DST & Mask Match). \
  2539. action go to dst NAT \
  2540. All DST_IP == (192.23.22.1 & 0.255.255.255)traffic goes to NAT block (public IP filtering) \
  2541. 3. generate and commit one NAT rule:\
  2542. public ip 192.23.22.1 --> private ip 194.23.22.1 \
  2543. delete rule and verrify NAT miss";
  2544. }
  2545. virtual bool AddRules()
  2546. {
  2547. LOG_MSG_DEBUG("Entering\n");
  2548. int ret = IpaNatBlockTest002::AddRules();
  2549. if (!ret) {
  2550. LOG_MSG_ERROR("Leaving, failed Adding test 002 rules 0\n");
  2551. return false;
  2552. }
  2553. ret = ipa_nat_del_ipv4_rule(m_tbl_hdl, m_nat_rule_hdl1);
  2554. if (ret) {
  2555. LOG_MSG_ERROR("Leaving, failed deleting NAT rule 0\n");
  2556. return false;
  2557. }
  2558. LOG_MSG_DEBUG("NAT rule deleted\n");
  2559. LOG_MSG_DEBUG("Leaving\n");
  2560. return true;
  2561. }// AddRules()
  2562. virtual bool ReceivePacketsAndCompare()
  2563. {
  2564. size_t receivedSize = 0;
  2565. bool isSuccess = true;
  2566. // Receive results
  2567. Byte *rxBuff1 = new Byte[0x400];
  2568. if (NULL == rxBuff1)
  2569. {
  2570. LOG_MSG_ERROR("Memory allocation error.\n");
  2571. return false;
  2572. }
  2573. receivedSize = m_consumer.ReceiveData(rxBuff1, 0x400);
  2574. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize, m_consumer.m_fromChannelName.c_str());
  2575. if (receivedSize) {
  2576. LOG_MSG_ERROR("Data received - test failed!\n");
  2577. isSuccess = false;
  2578. // Compare results
  2579. if (!CompareResultVsGoldenNat(
  2580. m_sendBuffer, m_sendSize,
  2581. rxBuff1, receivedSize,
  2582. m_private_ip, m_public_ip,
  2583. m_private_port, m_public_port,
  2584. false))
  2585. {
  2586. LOG_MSG_ERROR("Comparison of Buffer0 Failed!\n");
  2587. }
  2588. else {
  2589. LOG_MSG_ERROR("Comparison of Buffer0 succeeded - NAT rule was hit despite deletion!\n");
  2590. }
  2591. char recievedBuffer[256] = { 0 };
  2592. char SentBuffer[256] = { 0 };
  2593. size_t j;
  2594. for (j = 0; j < m_sendSize; j++)
  2595. snprintf(&SentBuffer[3 * j], sizeof(SentBuffer) - (3 * j + 1), " %02X", m_sendBuffer[j]);
  2596. for (j = 0; j < receivedSize; j++)
  2597. snprintf(&recievedBuffer[3 * j], sizeof(recievedBuffer) - (3 * j + 1), " %02X", rxBuff1[j]);
  2598. LOG_MSG_STACK("sent Value1 (%zu)\n%s\n, Received Value1(%zu)\n%s\n", m_sendSize, SentBuffer, receivedSize, recievedBuffer);
  2599. }
  2600. delete[] rxBuff1;
  2601. return isSuccess;
  2602. }
  2603. };
  2604. /*---------------------------------------------------------------------------*/
  2605. /* Test011: Multi PDN src NAT - MAX number of PDNs test */
  2606. /*---------------------------------------------------------------------------*/
  2607. class IpaNatBlockTest011 : public IpaNatBlockTestFixture
  2608. {
  2609. uint32_t m_public_ip3;
  2610. uint32_t m_public_ip4;
  2611. uint32_t m_private_ip3;
  2612. uint32_t m_private_ip4;
  2613. uint16_t m_public_port3;
  2614. uint16_t m_public_port4;
  2615. uint16_t m_private_port3;
  2616. uint16_t m_private_port4;
  2617. Byte m_sendBuffer4[BUFF_MAX_SIZE];
  2618. size_t m_sendSize4;
  2619. public:
  2620. IpaNatBlockTest011()
  2621. {
  2622. m_name = "IpaNatBlockTest011";
  2623. m_description =
  2624. "NAT block test 011 - Multi PDN src NAT test\
  2625. 1. Generate and commit three routing tables (one is used). \
  2626. Each table contains a single \"bypass\" rule (all data goes to output pipe 0, 1 and 2 (accordingly)) \
  2627. 2. Generate and commit one filtering rule: (DST & Mask Match). \
  2628. - action go to src NAT \
  2629. All SRC_IP == (192.23.22.1 & 0.255.255.255)traffic goes to NAT block \
  2630. All SRC_IP == (194.23.22.1 & 0.255.255.255)traffic goes to NAT block \
  2631. All SRC_IP == (196.23.22.1 & 0.255.255.255)traffic goes to NAT block \
  2632. All SRC_IP == (198.23.22.1 & 0.255.255.255)traffic goes to NAT block \
  2633. 3. generate and commit four NAT rules:\
  2634. private ip 192.23.22.1 --> public ip 193.23.22.1 \
  2635. private ip 194.23.22.1 --> public ip 195.23.22.1 \
  2636. private ip 196.23.22.1 --> public ip 197.23.22.1 \
  2637. private ip 198.23.22.1 --> public ip 199.23.22.1";
  2638. m_private_ip = 0xC0171601; /* 192.23.22.1 */
  2639. m_private_port = 5678;
  2640. m_public_ip = 0xC1171601; /* "193.23.22.1" */
  2641. m_public_port = 9050;
  2642. m_private_ip2 = 0xC2171601; /* 194.23.22.1 */
  2643. m_private_port2 = 5679;
  2644. m_public_ip2 = 0xC3171601; /* "195.23.22.1" */
  2645. m_public_port2 = 9051;
  2646. m_private_ip3 = 0xC4171601; /* 196.23.22.1 */
  2647. m_private_port3 = 5680;
  2648. m_public_ip3 = 0xC5171601; /* "197.23.22.1" */
  2649. m_public_port3 = 9052;
  2650. m_private_ip4 = 0xC6171601; /* 198.23.22.1 */
  2651. m_private_port4 = 5681;
  2652. m_public_ip4 = 0xC7171601; /* "199.23.22.1" */
  2653. m_public_port4 = 9053;
  2654. m_target_ip = 0xBF171601; /* 191.23.22.1 */
  2655. m_target_port = 1234;
  2656. m_sendSize4 = BUFF_MAX_SIZE;
  2657. m_minIPAHwType = IPA_HW_v4_0;
  2658. Register(*this);
  2659. }
  2660. virtual bool AddRules()
  2661. {
  2662. LOG_MSG_DEBUG("Entering\n");
  2663. const char bypass0[20] = "Bypass0";
  2664. const char bypass1[20] = "Bypass1";
  2665. const char bypass2[20] = "Bypass2";
  2666. struct ipa_ioc_get_rt_tbl routing_table0, routing_table1;
  2667. if (!CreateThreeIPv4BypassRoutingTables(bypass0, bypass1, bypass2))
  2668. {
  2669. LOG_MSG_ERROR("CreateThreeBypassRoutingTables Failed\n");
  2670. return false;
  2671. }
  2672. LOG_MSG_DEBUG("CreateThreeBypassRoutingTables completed successfully\n");
  2673. routing_table0.ip = IPA_IP_v4;
  2674. strlcpy(routing_table0.name, bypass0, sizeof(routing_table0.name));
  2675. if (!m_routing.GetRoutingTable(&routing_table0))
  2676. {
  2677. LOG_MSG_ERROR("m_routing.GetRoutingTable(&routing_table0=0x%p) Failed.\n", &routing_table0);
  2678. return false;
  2679. }
  2680. LOG_MSG_DEBUG("%s route table handle = %u\n", bypass0, routing_table0.hdl);
  2681. routing_table1.ip = IPA_IP_v4;
  2682. strlcpy(routing_table1.name, bypass1, sizeof(routing_table1.name));
  2683. if (!m_routing.GetRoutingTable(&routing_table1))
  2684. {
  2685. LOG_MSG_ERROR("m_routing.GetRoutingTable(&routing_table1=0x%p) Failed.\n", &routing_table1);
  2686. return false;
  2687. }
  2688. LOG_MSG_DEBUG("%s route table handle = %u\n", bypass1, routing_table1.hdl);
  2689. IPAFilteringTable FilterTable0;
  2690. struct ipa_flt_rule_add flt_rule_entry;
  2691. FilterTable0.Init(IPA_IP_v4, IPA_CLIENT_TEST_PROD, false, 1);
  2692. LOG_MSG_DEBUG("FilterTable*.Init Completed Successfully..\n");
  2693. // Configuring Filtering Rule No.0
  2694. FilterTable0.GeneratePresetRule(1, flt_rule_entry);
  2695. flt_rule_entry.at_rear = true;
  2696. flt_rule_entry.flt_rule_hdl = -1; // return Value
  2697. flt_rule_entry.status = -1; // return value
  2698. flt_rule_entry.rule.action = IPA_PASS_TO_SRC_NAT;
  2699. flt_rule_entry.rule.rt_tbl_hdl = routing_table0.hdl;
  2700. flt_rule_entry.rule.attrib.attrib_mask = IPA_FLT_SRC_ADDR;
  2701. flt_rule_entry.rule.attrib.u.v4.src_addr_mask = 0x00FFFFFF; // Mask - catch all private IPs
  2702. flt_rule_entry.rule.attrib.u.v4.src_addr = m_private_ip; // Filter SRC_IP == 192.23.22.1
  2703. flt_rule_entry.rule.pdn_idx = 0;
  2704. flt_rule_entry.rule.set_metadata = 0;
  2705. if (
  2706. ((uint8_t)-1 == FilterTable0.AddRuleToTable(flt_rule_entry)) ||
  2707. !m_filtering.AddFilteringRule(FilterTable0.GetFilteringTable())
  2708. )
  2709. {
  2710. LOG_MSG_ERROR("Error Adding Rule to Filter Table, aborting...\n");
  2711. return false;
  2712. }
  2713. else
  2714. {
  2715. LOG_MSG_DEBUG("flt rule hdl0=0x%x, status=0x%x\n", FilterTable0.ReadRuleFromTable(0)->flt_rule_hdl, FilterTable0.ReadRuleFromTable(0)->status);
  2716. }
  2717. //NAT table and rules creation
  2718. int total_entries = 20;
  2719. int ret;
  2720. ipa_nat_ipv4_rule ipv4_rule;
  2721. ipa_nat_pdn_entry pdn_info;
  2722. // first create the NAT table
  2723. ret = ipa_nat_add_ipv4_tbl(m_public_ip, m_mem_type, total_entries, &m_tbl_hdl);
  2724. if (ret) {
  2725. LOG_MSG_ERROR("Leaving, failed creating NAT table\n");
  2726. return false;
  2727. }
  2728. LOG_MSG_DEBUG("nat table added, hdl %d, public ip 0x%X\n", m_tbl_hdl,
  2729. m_public_ip);
  2730. // modify the PDN entries that will be pointed by the NAT rules
  2731. pdn_info.public_ip = m_public_ip;
  2732. pdn_info.src_metadata = 0;
  2733. pdn_info.dst_metadata = 0;
  2734. ret = ipa_nat_modify_pdn(m_tbl_hdl, 0, &pdn_info);
  2735. if (ret) {
  2736. LOG_MSG_ERROR("Leaving, failed Modifying PDN entry 0 \n");
  2737. return false;
  2738. }
  2739. pdn_info.public_ip = m_public_ip2;
  2740. ret = ipa_nat_modify_pdn(m_tbl_hdl, 1, &pdn_info);
  2741. if (ret) {
  2742. LOG_MSG_ERROR("Leaving, failed Modifying PDN entry 1 \n");
  2743. return false;
  2744. }
  2745. pdn_info.public_ip = m_public_ip3;
  2746. ret = ipa_nat_modify_pdn(m_tbl_hdl, 2, &pdn_info);
  2747. if (ret) {
  2748. LOG_MSG_ERROR("Leaving, failed Modifying PDN entry 2 \n");
  2749. return false;
  2750. }
  2751. pdn_info.public_ip = m_public_ip4;
  2752. ret = ipa_nat_modify_pdn(m_tbl_hdl, 3, &pdn_info);
  2753. if (ret) {
  2754. LOG_MSG_ERROR("Leaving, failed Modifying PDN entry 3 \n");
  2755. return false;
  2756. }
  2757. LOG_MSG_DEBUG("Added 4 PDNs successfully: 0x%X, 0x%X, 0x%X, 0x%X\n",
  2758. m_public_ip, m_public_ip2, m_public_ip3, m_public_ip4);
  2759. ipv4_rule.target_ip = m_target_ip;
  2760. ipv4_rule.target_port = m_target_port;
  2761. ipv4_rule.private_ip = m_private_ip;
  2762. ipv4_rule.private_port = m_private_port;
  2763. ipv4_rule.protocol = IPPROTO_TCP;
  2764. ipv4_rule.public_port = m_public_port;
  2765. ipv4_rule.pdn_index = 0;
  2766. ret = ipa_nat_add_ipv4_rule(m_tbl_hdl, &ipv4_rule, &m_nat_rule_hdl1);
  2767. if (ret) {
  2768. LOG_MSG_ERROR("Leaving, failed adding NAT rule 0\n");
  2769. return false;
  2770. }
  2771. LOG_MSG_DEBUG("NAT rule added, hdl %d, data: 0x%X, %d, 0x%X, %d, %d, %d, $d\n",
  2772. m_nat_rule_hdl1, ipv4_rule.target_ip, ipv4_rule.target_port,
  2773. ipv4_rule.private_ip, ipv4_rule.private_port,
  2774. ipv4_rule.protocol, ipv4_rule.public_port, ipv4_rule.pdn_index);
  2775. ipv4_rule.private_ip = m_private_ip2;
  2776. ipv4_rule.private_port = m_private_port2;
  2777. ipv4_rule.public_port = m_public_port2;
  2778. ipv4_rule.pdn_index = 1;
  2779. ret = ipa_nat_add_ipv4_rule(m_tbl_hdl, &ipv4_rule, &m_nat_rule_hdl1);
  2780. if (ret) {
  2781. LOG_MSG_ERROR("Leaving, failed adding NAT rule 1\n");
  2782. return false;
  2783. }
  2784. LOG_MSG_DEBUG("NAT rule 2 added, hdl %d, data: 0x%X, %d, 0x%X, %d, %d, %d, $d\n",
  2785. m_nat_rule_hdl1, ipv4_rule.target_ip, ipv4_rule.target_port,
  2786. ipv4_rule.private_ip, ipv4_rule.private_port,
  2787. ipv4_rule.protocol, ipv4_rule.public_port, ipv4_rule.pdn_index);
  2788. ipv4_rule.private_ip = m_private_ip3;
  2789. ipv4_rule.private_port = m_private_port3;
  2790. ipv4_rule.public_port = m_public_port3;
  2791. ipv4_rule.pdn_index = 2;
  2792. ret = ipa_nat_add_ipv4_rule(m_tbl_hdl, &ipv4_rule, &m_nat_rule_hdl1);
  2793. if (ret) {
  2794. LOG_MSG_ERROR("Leaving, failed adding NAT rule 2\n");
  2795. return false;
  2796. }
  2797. LOG_MSG_DEBUG("NAT rule 3 added, hdl %d, data: 0x%X, %d, 0x%X, %d, %d, %d, $d\n",
  2798. m_nat_rule_hdl1, ipv4_rule.target_ip, ipv4_rule.target_port,
  2799. ipv4_rule.private_ip, ipv4_rule.private_port,
  2800. ipv4_rule.protocol, ipv4_rule.public_port, ipv4_rule.pdn_index);
  2801. ipv4_rule.private_ip = m_private_ip4;
  2802. ipv4_rule.private_port = m_private_port4;
  2803. ipv4_rule.public_port = m_public_port4;
  2804. ipv4_rule.pdn_index = 3;
  2805. ret = ipa_nat_add_ipv4_rule(m_tbl_hdl, &ipv4_rule, &m_nat_rule_hdl1);
  2806. if (ret) {
  2807. LOG_MSG_ERROR("Leaving, failed adding NAT rule 3\n");
  2808. return false;
  2809. }
  2810. LOG_MSG_DEBUG("NAT rule 4 added, hdl %d, data: 0x%X, %d, 0x%X, %d, %d, %d, $d\n",
  2811. m_nat_rule_hdl1, ipv4_rule.target_ip, ipv4_rule.target_port,
  2812. ipv4_rule.private_ip, ipv4_rule.private_port,
  2813. ipv4_rule.protocol, ipv4_rule.public_port, ipv4_rule.pdn_index);
  2814. LOG_MSG_DEBUG("Leaving\n");
  2815. return true;
  2816. }// AddRules()
  2817. virtual bool ModifyPackets()
  2818. {
  2819. uint32_t address;
  2820. uint16_t port;
  2821. char flags = 0x18;
  2822. if (!LoadDefaultPacket(IPA_IP_v4, m_extHdrType, m_sendBuffer4, m_sendSize4)) {
  2823. LOG_MSG_ERROR("Failed default Packet buffer 4\n");
  2824. return false;
  2825. }
  2826. LOG_MSG_DEBUG("Loaded %zu Bytes to Buffer 4\n", m_sendSize4);
  2827. //first packet private ip 192.23.22.1 --> public ip 193.23.22.1
  2828. address = htonl(m_target_ip);//191.23.22.1
  2829. memcpy(&m_sendBuffer[IPV4_DST_ADDR_OFFSET], &address, sizeof(address));
  2830. port = htons(m_target_port);
  2831. memcpy(&m_sendBuffer[IPV4_DST_PORT_OFFSET], &port, sizeof(port));
  2832. address = htonl(m_private_ip);/* 192.23.22.1 */
  2833. memcpy(&m_sendBuffer[IPV4_SRC_ADDR_OFFSET], &address, sizeof(address));
  2834. port = htons(m_private_port);
  2835. memcpy(&m_sendBuffer[IPV4_SRC_PORT_OFFSET], &port, sizeof(port));
  2836. //make sure the FIN flag is not set, otherwise we will get a NAT miss
  2837. memcpy(&m_sendBuffer[IPV4_TCP_FLAGS_OFFSET], &flags, sizeof(flags));
  2838. // second packet private ip 194.23.22.1 --> public ip 195.23.22.1
  2839. address = htonl(m_target_ip);//191.23.22.1
  2840. memcpy(&m_sendBuffer2[IPV4_DST_ADDR_OFFSET], &address, sizeof(address));
  2841. port = htons(m_target_port);
  2842. memcpy(&m_sendBuffer2[IPV4_DST_PORT_OFFSET], &port, sizeof(port));
  2843. address = htonl(m_private_ip2);/* 194.23.22.1 */
  2844. memcpy(&m_sendBuffer2[IPV4_SRC_ADDR_OFFSET], &address, sizeof(address));
  2845. port = htons(m_private_port2);
  2846. memcpy(&m_sendBuffer2[IPV4_SRC_PORT_OFFSET], &port, sizeof(port));
  2847. //make sure the FIN flag is not set, otherwise we will get a NAT miss
  2848. memcpy(&m_sendBuffer2[IPV4_TCP_FLAGS_OFFSET], &flags, sizeof(flags));
  2849. // third packet private ip 196.23.22.1 --> public ip 197.23.22.1
  2850. address = htonl(m_target_ip);//191.23.22.1
  2851. memcpy(&m_sendBuffer3[IPV4_DST_ADDR_OFFSET], &address, sizeof(address));
  2852. port = htons(m_target_port);
  2853. memcpy(&m_sendBuffer3[IPV4_DST_PORT_OFFSET], &port, sizeof(port));
  2854. address = htonl(m_private_ip3);/* 196.23.22.1 */
  2855. memcpy(&m_sendBuffer3[IPV4_SRC_ADDR_OFFSET], &address, sizeof(address));
  2856. port = htons(m_private_port3);
  2857. memcpy(&m_sendBuffer3[IPV4_SRC_PORT_OFFSET], &port, sizeof(port));
  2858. //make sure the FIN flag is not set, otherwise we will get a NAT miss
  2859. memcpy(&m_sendBuffer3[IPV4_TCP_FLAGS_OFFSET], &flags, sizeof(flags));
  2860. // third packet private ip 198.23.22.1 --> public ip 199.23.22.1
  2861. address = htonl(m_target_ip);//191.23.22.1
  2862. memcpy(&m_sendBuffer4[IPV4_DST_ADDR_OFFSET], &address, sizeof(address));
  2863. port = htons(m_target_port);
  2864. memcpy(&m_sendBuffer4[IPV4_DST_PORT_OFFSET], &port, sizeof(port));
  2865. address = htonl(m_private_ip4);/* 198.23.22.1 */
  2866. memcpy(&m_sendBuffer4[IPV4_SRC_ADDR_OFFSET], &address, sizeof(address));
  2867. port = htons(m_private_port4);
  2868. memcpy(&m_sendBuffer4[IPV4_SRC_PORT_OFFSET], &port, sizeof(port));
  2869. //make sure the FIN flag is not set, otherwise we will get a NAT miss
  2870. memcpy(&m_sendBuffer4[IPV4_TCP_FLAGS_OFFSET], &flags, sizeof(flags));
  2871. return true;
  2872. }// ModifyPacktes ()
  2873. virtual bool SendPackets()
  2874. {
  2875. bool isSuccess = false;
  2876. // Send first packet
  2877. LOG_MSG_DEBUG("sending first packet\n");
  2878. isSuccess = m_producer.SendData(m_sendBuffer, m_sendSize);
  2879. if (false == isSuccess)
  2880. {
  2881. LOG_MSG_ERROR("SendData failure.\n");
  2882. return false;
  2883. }
  2884. // Send second packet
  2885. LOG_MSG_DEBUG("sending second packet\n");
  2886. isSuccess = m_producer.SendData(m_sendBuffer2, m_sendSize2);
  2887. if (false == isSuccess)
  2888. {
  2889. LOG_MSG_ERROR("SendData failure.\n");
  2890. return false;
  2891. }
  2892. // Send third packet
  2893. LOG_MSG_DEBUG("sending third packet\n");
  2894. isSuccess = m_producer.SendData(m_sendBuffer3, m_sendSize3);
  2895. if (false == isSuccess)
  2896. {
  2897. LOG_MSG_ERROR("SendData failure.\n");
  2898. return false;
  2899. }
  2900. // Send fourth packet
  2901. LOG_MSG_DEBUG("sending fourth packet\n");
  2902. isSuccess = m_producer.SendData(m_sendBuffer4, m_sendSize4);
  2903. if (false == isSuccess)
  2904. {
  2905. LOG_MSG_ERROR("SendData failure.\n");
  2906. return false;
  2907. }
  2908. LOG_MSG_DEBUG("sent successfully four packets\n");
  2909. return true;
  2910. }
  2911. virtual bool ReceivePacketsAndCompare()
  2912. {
  2913. size_t receivedSize = 0;
  2914. size_t receivedSize2 = 0;
  2915. size_t receivedSize3 = 0;
  2916. size_t receivedSize4 = 0;
  2917. bool isSuccess = true;
  2918. // Receive results
  2919. Byte *rxBuff1 = new Byte[0x400];
  2920. Byte *rxBuff2 = new Byte[0x400];
  2921. Byte *rxBuff3 = new Byte[0x400];
  2922. Byte *rxBuff4 = new Byte[0x400];
  2923. if (rxBuff1 == NULL)
  2924. {
  2925. LOG_MSG_ERROR("Memory allocation error 1.\n");
  2926. if (rxBuff2)
  2927. delete[] rxBuff2;
  2928. if (rxBuff3)
  2929. delete[] rxBuff3;
  2930. if (rxBuff4)
  2931. delete[] rxBuff4;
  2932. return false;
  2933. }
  2934. if (rxBuff2 == NULL)
  2935. {
  2936. LOG_MSG_ERROR("Memory allocation error 2.\n");
  2937. delete[] rxBuff1;
  2938. if (rxBuff3)
  2939. delete[] rxBuff3;
  2940. if (rxBuff4)
  2941. delete[] rxBuff4;
  2942. return false;
  2943. }
  2944. if (rxBuff3 == NULL)
  2945. {
  2946. LOG_MSG_ERROR("Memory allocation error 3.\n");
  2947. delete[] rxBuff1;
  2948. delete[] rxBuff2;
  2949. if (rxBuff4)
  2950. delete[] rxBuff4;
  2951. return false;
  2952. }
  2953. if (rxBuff4 == NULL)
  2954. {
  2955. LOG_MSG_ERROR("Memory allocation error 4.\n");
  2956. delete[] rxBuff1;
  2957. delete[] rxBuff2;
  2958. delete[] rxBuff3;
  2959. return false;
  2960. }
  2961. receivedSize = m_consumer.ReceiveData(rxBuff1, 0x400);
  2962. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize, m_consumer.m_fromChannelName.c_str());
  2963. receivedSize2 = m_consumer.ReceiveData(rxBuff2, 0x400);
  2964. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize2, m_consumer.m_fromChannelName.c_str());
  2965. receivedSize3 = m_consumer.ReceiveData(rxBuff3, 0x400);
  2966. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize3, m_consumer.m_fromChannelName.c_str());
  2967. receivedSize4 = m_consumer.ReceiveData(rxBuff4, 0x400);
  2968. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize4, m_consumer.m_fromChannelName.c_str());
  2969. // Compare results
  2970. if (!CompareResultVsGoldenNat(
  2971. m_sendBuffer, m_sendSize,
  2972. rxBuff1, receivedSize,
  2973. m_private_ip, m_public_ip,
  2974. m_private_port, m_public_port,
  2975. true))
  2976. {
  2977. LOG_MSG_ERROR("Comparison of Buffer0 Failed!\n");
  2978. isSuccess = false;
  2979. }
  2980. char recievedBuffer[256] = { 0 };
  2981. char SentBuffer[256] = { 0 };
  2982. char recievedBuffer2[256] = { 0 };
  2983. char SentBuffer2[256] = { 0 };
  2984. char recievedBuffer3[256] = { 0 };
  2985. char SentBuffer3[256] = { 0 };
  2986. char recievedBuffer4[256] = { 0 };
  2987. char SentBuffer4[256] = { 0 };
  2988. size_t j;
  2989. for (j = 0; j < m_sendSize; j++)
  2990. snprintf(&SentBuffer[3 * j], sizeof(SentBuffer) - (3 * j + 1), " %02X", m_sendBuffer[j]);
  2991. for (j = 0; j < receivedSize; j++)
  2992. snprintf(&recievedBuffer[3 * j], sizeof(recievedBuffer) - (3 * j + 1), " %02X", rxBuff1[j]);
  2993. LOG_MSG_STACK("sent Value1 (%zu)\n%s\n, Received Value1(%zu)\n%s\n", m_sendSize, SentBuffer, receivedSize, recievedBuffer);
  2994. delete[] rxBuff1;
  2995. isSuccess &= CompareResultVsGoldenNat(
  2996. m_sendBuffer2, m_sendSize2,
  2997. rxBuff2, receivedSize2,
  2998. m_private_ip2, m_public_ip2,
  2999. m_private_port2, m_public_port2,
  3000. true);
  3001. for (j = 0; j < m_sendSize2; j++)
  3002. snprintf(&SentBuffer2[3 * j], sizeof(SentBuffer2) - (3 * j + 1), " %02X", m_sendBuffer2[j]);
  3003. for (j = 0; j < receivedSize2; j++)
  3004. snprintf(&recievedBuffer2[3 * j], sizeof(recievedBuffer2) - (3 * j + 1), " %02X", rxBuff2[j]);
  3005. LOG_MSG_STACK("sent Value2 (%zu)\n%s\n, Received Value2(%zu)\n%s\n", m_sendSize2, SentBuffer2, receivedSize2, recievedBuffer2);
  3006. delete[] rxBuff2;
  3007. isSuccess &= CompareResultVsGoldenNat(
  3008. m_sendBuffer3, m_sendSize3,
  3009. rxBuff3, receivedSize3,
  3010. m_private_ip3, m_public_ip3,
  3011. m_private_port3, m_public_port3,
  3012. true);
  3013. for (j = 0; j < m_sendSize3; j++)
  3014. snprintf(&SentBuffer3[3 * j], sizeof(SentBuffer3) - (3 * j + 1), " %02X", m_sendBuffer3[j]);
  3015. for (j = 0; j < receivedSize3; j++)
  3016. snprintf(&recievedBuffer3[3 * j], sizeof(recievedBuffer3) - (3 * j + 1), " %02X", rxBuff3[j]);
  3017. LOG_MSG_STACK("sent Value3 (%zu)\n%s\n, Received Value3(%zu)\n%s\n", m_sendSize3, SentBuffer3, receivedSize3, recievedBuffer3);
  3018. delete[] rxBuff3;
  3019. isSuccess &= CompareResultVsGoldenNat(
  3020. m_sendBuffer4, m_sendSize4,
  3021. rxBuff4, receivedSize4,
  3022. m_private_ip4, m_public_ip4,
  3023. m_private_port4, m_public_port4,
  3024. true);
  3025. for (j = 0; j < m_sendSize4; j++)
  3026. snprintf(&SentBuffer4[3 * j], sizeof(SentBuffer4) - (3 * j + 1), " %02X", m_sendBuffer4[j]);
  3027. for (j = 0; j < receivedSize4; j++)
  3028. snprintf(&recievedBuffer4[3 * j], sizeof(recievedBuffer4) - (3 * j + 1), " %02X", rxBuff4[j]);
  3029. LOG_MSG_STACK("sent Value4 (%zu)\n%s\n, Received Value4(%zu)\n%s\n", m_sendSize4, SentBuffer4, receivedSize4, recievedBuffer4);
  3030. delete[] rxBuff4;
  3031. return isSuccess;
  3032. }
  3033. };
  3034. /*---------------------------------------------------------------------------*/
  3035. /* Test012: Single PDN dst NAT test expansion table usage */
  3036. /* NOTE: other classes are derived from this class - change carefully */
  3037. /*---------------------------------------------------------------------------*/
  3038. class IpaNatBlockTest012 : public IpaNatBlockTestFixture
  3039. {
  3040. uint32_t m_target_ip2;
  3041. uint16_t m_target_port2;
  3042. public:
  3043. IpaNatBlockTest012()
  3044. {
  3045. m_name = "IpaNatBlockTest012";
  3046. m_description =
  3047. "NAT block test 012 - single PDN dst NAT test - expansion table usage\
  3048. 1. Generate and commit three routing tables (only one is used). \
  3049. Each table contains a single \"bypass\" rule (all data goes to output pipe 0, 1 and 2 (accordingly)) \
  3050. 2. Generate and commit one filtering rule: (DST & Mask Match). \
  3051. action go to dst NAT \
  3052. All DST_IP == (192.23.22.1 & 0.255.255.255)traffic goes to NAT block (public IP filtering) \
  3053. 3. generate and commit two NAT rules so second one is located in expansion table:\
  3054. since we use a single public ip this test should work also on pre IPAv4 targets\
  3055. public ip 192.23.22.1 --> private ip 194.23.22.1 ";
  3056. m_private_ip = 0xC2171601; /* 194.23.22.1 */
  3057. m_private_port = 5678;
  3058. m_private_ip2 = 0xC5171601; /* 197.23.22.1 */
  3059. m_private_port2 = 5679;
  3060. m_public_ip = 0xC0171601; /* "192.23.22.1" */
  3061. m_public_port = 9050;
  3062. m_target_ip = 0xC1171601; /* 193.23.22.1 */
  3063. m_target_port = 1234;
  3064. m_target_ip2 = 0x1601C117; /* swap m_target_ip to get same hash*/
  3065. m_target_port2 = m_target_port;
  3066. Register(*this);
  3067. }
  3068. virtual bool AddRules()
  3069. {
  3070. LOG_MSG_DEBUG("Entering\n");
  3071. const char bypass0[20] = "Bypass0";
  3072. const char bypass1[20] = "Bypass1";
  3073. const char bypass2[20] = "Bypass2";
  3074. struct ipa_ioc_get_rt_tbl routing_table0;
  3075. if (!CreateThreeIPv4BypassRoutingTables(bypass0, bypass1, bypass2))
  3076. {
  3077. LOG_MSG_ERROR("CreateThreeBypassRoutingTables Failed\n");
  3078. return false;
  3079. }
  3080. LOG_MSG_DEBUG("CreateThreeBypassRoutingTables completed successfully\n");
  3081. routing_table0.ip = IPA_IP_v4;
  3082. strlcpy(routing_table0.name, bypass0, sizeof(routing_table0.name));
  3083. if (!m_routing.GetRoutingTable(&routing_table0))
  3084. {
  3085. LOG_MSG_ERROR("m_routing.GetRoutingTable(&routing_table0=0x%p) Failed.\n", &routing_table0);
  3086. return false;
  3087. }
  3088. LOG_MSG_DEBUG("%s route table handle = %u\n", bypass0, routing_table0.hdl);
  3089. IPAFilteringTable FilterTable0;
  3090. struct ipa_flt_rule_add flt_rule_entry;
  3091. FilterTable0.Init(IPA_IP_v4, IPA_CLIENT_TEST_PROD, false, 3);
  3092. LOG_MSG_DEBUG("FilterTable*.Init Completed Successfully..\n");
  3093. // Configuring Filtering Rule No.0
  3094. FilterTable0.GeneratePresetRule(1, flt_rule_entry);
  3095. flt_rule_entry.at_rear = true;
  3096. flt_rule_entry.flt_rule_hdl = -1; // return Value
  3097. flt_rule_entry.status = -1; // return value
  3098. flt_rule_entry.rule.action = IPA_PASS_TO_DST_NAT;
  3099. flt_rule_entry.rule.rt_tbl_hdl = routing_table0.hdl;
  3100. flt_rule_entry.rule.attrib.attrib_mask = IPA_FLT_DST_ADDR;
  3101. flt_rule_entry.rule.attrib.u.v4.dst_addr_mask = 0x00FFFFFF; // Mask
  3102. flt_rule_entry.rule.attrib.u.v4.dst_addr = m_public_ip; // Filter DST_IP == 192.23.22.1
  3103. flt_rule_entry.rule.pdn_idx = 0;
  3104. flt_rule_entry.rule.set_metadata = 0;
  3105. if (
  3106. ((uint8_t)-1 == FilterTable0.AddRuleToTable(flt_rule_entry)) ||
  3107. !m_filtering.AddFilteringRule(FilterTable0.GetFilteringTable())
  3108. )
  3109. {
  3110. LOG_MSG_ERROR("Error Adding Rule to Filter Table, aborting...\n");
  3111. return false;
  3112. }
  3113. else
  3114. {
  3115. LOG_MSG_DEBUG("flt rule hdl0=0x%x, status=0x%x\n", FilterTable0.ReadRuleFromTable(0)->flt_rule_hdl, FilterTable0.ReadRuleFromTable(0)->status);
  3116. }
  3117. //NAT table and rules creation
  3118. int total_entries = 20;
  3119. int ret;
  3120. ipa_nat_ipv4_rule ipv4_rule;
  3121. uint32_t pub_ip_add = m_public_ip;
  3122. ret = ipa_nat_add_ipv4_tbl(pub_ip_add, m_mem_type, total_entries, &m_tbl_hdl);
  3123. if (ret) {
  3124. LOG_MSG_ERROR("Leaving, failed creating NAT table\n");
  3125. return false;
  3126. }
  3127. LOG_MSG_DEBUG("nat table added, hdl %d, public ip 0x%X\n", m_tbl_hdl,
  3128. pub_ip_add);
  3129. ipv4_rule.target_ip = m_target_ip;
  3130. ipv4_rule.target_port = m_target_port;
  3131. ipv4_rule.private_ip = m_private_ip;
  3132. ipv4_rule.private_port = m_private_port;
  3133. ipv4_rule.protocol = IPPROTO_TCP;
  3134. ipv4_rule.public_port = m_public_port;
  3135. ipv4_rule.pdn_index = 0;
  3136. ret = ipa_nat_add_ipv4_rule(m_tbl_hdl, &ipv4_rule, &m_nat_rule_hdl1);
  3137. if (ret) {
  3138. LOG_MSG_ERROR("Leaving, failed adding NAT rule 0\n");
  3139. return false;
  3140. }
  3141. LOG_MSG_DEBUG("NAT rule added, hdl %d, data: 0x%X, %d, 0x%X, %d, %d, %d, $d\n",
  3142. m_nat_rule_hdl1, ipv4_rule.target_ip, ipv4_rule.target_port,
  3143. ipv4_rule.private_ip, ipv4_rule.private_port,
  3144. ipv4_rule.protocol, ipv4_rule.public_port, ipv4_rule.pdn_index);
  3145. ipv4_rule.target_ip = m_target_ip2;
  3146. ipv4_rule.target_port = m_target_port2;
  3147. // private IPs are not part of the dst NAT entry hash calculation
  3148. ipv4_rule.private_ip = m_private_ip2;
  3149. ipv4_rule.private_port = m_private_port2;
  3150. ret = ipa_nat_add_ipv4_rule(m_tbl_hdl, &ipv4_rule, &m_nat_rule_hdl1);
  3151. if (ret) {
  3152. LOG_MSG_ERROR("Leaving, failed adding NAT rule 0\n");
  3153. return false;
  3154. }
  3155. LOG_MSG_DEBUG("NAT rule 2 added, hdl %d, data: 0x%X, %d, 0x%X, %d, %d, %d, $d\n",
  3156. m_nat_rule_hdl1, ipv4_rule.target_ip, ipv4_rule.target_port,
  3157. ipv4_rule.private_ip, ipv4_rule.private_port,
  3158. ipv4_rule.protocol, ipv4_rule.public_port, ipv4_rule.pdn_index);
  3159. LOG_MSG_DEBUG("Leaving\n");
  3160. return true;
  3161. }// AddRules()
  3162. virtual bool ModifyPackets()
  3163. {
  3164. uint32_t address;
  3165. uint16_t port;
  3166. char flags = 0x18;
  3167. address = htonl(m_public_ip);
  3168. memcpy(&m_sendBuffer[IPV4_DST_ADDR_OFFSET], &address, sizeof(address));
  3169. port = htons(m_public_port);
  3170. memcpy(&m_sendBuffer[IPV4_DST_PORT_OFFSET], &port, sizeof(port));
  3171. address = htonl(m_target_ip2);
  3172. memcpy(&m_sendBuffer[IPV4_SRC_ADDR_OFFSET], &address, sizeof(address));
  3173. port = htons(m_target_port2);
  3174. memcpy(&m_sendBuffer[IPV4_SRC_PORT_OFFSET], &port, sizeof(port));
  3175. //make sure the FIN flag is not set, otherwise we will get a NAT miss
  3176. memcpy(&m_sendBuffer[IPV4_TCP_FLAGS_OFFSET], &flags, sizeof(flags));
  3177. return true;
  3178. }// ModifyPacktes ()
  3179. virtual bool SendPackets()
  3180. {
  3181. bool isSuccess = false;
  3182. // Send first packet
  3183. isSuccess = m_producer.SendData(m_sendBuffer, m_sendSize);
  3184. if (false == isSuccess)
  3185. {
  3186. LOG_MSG_ERROR("SendData failure.\n");
  3187. return false;
  3188. }
  3189. LOG_MSG_DEBUG("sent successfully one packet\n");
  3190. return true;
  3191. }
  3192. virtual bool ReceivePacketsAndCompare()
  3193. {
  3194. size_t receivedSize = 0;
  3195. bool isSuccess = true;
  3196. // Receive results
  3197. Byte *rxBuff1 = new Byte[0x400];
  3198. if (NULL == rxBuff1)
  3199. {
  3200. LOG_MSG_ERROR("Memory allocation error.\n");
  3201. return false;
  3202. }
  3203. receivedSize = m_consumer.ReceiveData(rxBuff1, 0x400);
  3204. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize, m_consumer.m_fromChannelName.c_str());
  3205. // Compare results
  3206. if (!CompareResultVsGoldenNat(
  3207. m_sendBuffer, m_sendSize,
  3208. rxBuff1, receivedSize,
  3209. m_private_ip2, m_public_ip,
  3210. m_private_port2, m_public_port,
  3211. false))
  3212. {
  3213. LOG_MSG_ERROR("Comparison of Buffer0 Failed!\n");
  3214. isSuccess = false;
  3215. }
  3216. char recievedBuffer[256] = { 0 };
  3217. char SentBuffer[256] = { 0 };
  3218. size_t j;
  3219. for (j = 0; j < m_sendSize; j++)
  3220. snprintf(&SentBuffer[3 * j], sizeof(SentBuffer) - (3 * j + 1), " %02X", m_sendBuffer[j]);
  3221. for (j = 0; j < receivedSize; j++)
  3222. snprintf(&recievedBuffer[3 * j], sizeof(recievedBuffer) - (3 * j + 1), " %02X", rxBuff1[j]);
  3223. LOG_MSG_STACK("sent Value1 (%zu)\n%s\n, Received Value1(%zu)\n%s\n", m_sendSize, SentBuffer, receivedSize, recievedBuffer);
  3224. delete[] rxBuff1;
  3225. return isSuccess;
  3226. }
  3227. };
  3228. /*---------------------------------------------------------------------------*/
  3229. /* Test013: Single PDN dst NAT expansion rule deletion test */
  3230. /*---------------------------------------------------------------------------*/
  3231. class IpaNatBlockTest013 : public IpaNatBlockTest012
  3232. {
  3233. public:
  3234. IpaNatBlockTest013()
  3235. {
  3236. m_name = "IpaNatBlockTest013";
  3237. m_description =
  3238. "NAT block test 013 - single PDN dst NAT test - expansion table rule deletion\
  3239. 1. Generate and commit three routing tables (only one is used). \
  3240. Each table contains a single \"bypass\" rule (all data goes to output pipe 0, 1 and 2 (accordingly)) \
  3241. 2. Generate and commit one filtering rule: (DST & Mask Match). \
  3242. action go to dst NAT \
  3243. All DST_IP == (192.23.22.1 & 0.255.255.255)traffic goes to NAT block (public IP filtering) \
  3244. 3. generate and commit two NAT rules so second one is located in expansion table:\
  3245. since we use a single public ip this test should work also on pre IPAv4 targets\
  3246. public ip 192.23.22.1 --> private ip 194.23.22.1\
  3247. 4. delete NAT rule and expect NAT miss";
  3248. }
  3249. virtual bool AddRules()
  3250. {
  3251. LOG_MSG_DEBUG("Entering\n");
  3252. int ret = IpaNatBlockTest012::AddRules();
  3253. if (!ret) {
  3254. LOG_MSG_ERROR("Leaving, failed Adding test 012 rules 0\n");
  3255. return false;
  3256. }
  3257. ret = ipa_nat_del_ipv4_rule(m_tbl_hdl, m_nat_rule_hdl1);
  3258. if (ret) {
  3259. LOG_MSG_ERROR("Leaving, failed deleting NAT rule 1\n");
  3260. return false;
  3261. }
  3262. LOG_MSG_DEBUG("NAT rule deleted\n");
  3263. LOG_MSG_DEBUG("Leaving\n");
  3264. return true;
  3265. }// AddRules()
  3266. virtual bool ReceivePacketsAndCompare()
  3267. {
  3268. size_t receivedSize = 0;
  3269. bool isSuccess = true;
  3270. // Receive results
  3271. Byte *rxBuff1 = new Byte[0x400];
  3272. if (NULL == rxBuff1)
  3273. {
  3274. LOG_MSG_ERROR("Memory allocation error.\n");
  3275. return false;
  3276. }
  3277. receivedSize = m_consumer.ReceiveData(rxBuff1, 0x400);
  3278. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize, m_consumer.m_fromChannelName.c_str());
  3279. if (receivedSize) {
  3280. LOG_MSG_ERROR("Data received - test failed!\n");
  3281. isSuccess = false;
  3282. // Compare results for debug in the case of failure only
  3283. if (!CompareResultVsGoldenNat(
  3284. m_sendBuffer, m_sendSize,
  3285. rxBuff1, receivedSize,
  3286. m_private_ip2, m_public_ip,
  3287. m_private_port2, m_public_port,
  3288. false))
  3289. {
  3290. LOG_MSG_ERROR("Comparison of Buffer0 Failed!\n");
  3291. }
  3292. else {
  3293. LOG_MSG_ERROR("Comparison of Buffer0 succeeded - NAT rule was hit despite deletion!\n");
  3294. }
  3295. char recievedBuffer[256] = { 0 };
  3296. char SentBuffer[256] = { 0 };
  3297. size_t j;
  3298. for (j = 0; j < m_sendSize; j++)
  3299. snprintf(&SentBuffer[3 * j], sizeof(SentBuffer) - (3 * j + 1), " %02X", m_sendBuffer[j]);
  3300. for (j = 0; j < receivedSize; j++)
  3301. snprintf(&recievedBuffer[3 * j], sizeof(recievedBuffer) - (3 * j + 1), " %02X", rxBuff1[j]);
  3302. LOG_MSG_STACK("sent Value1 (%zu)\n%s\n, Received Value1(%zu)\n%s\n", m_sendSize, SentBuffer, receivedSize, recievedBuffer);
  3303. }
  3304. delete[] rxBuff1;
  3305. return isSuccess;
  3306. }
  3307. };
  3308. /*---------------------------------------------------------------------------*/
  3309. /* Test014: Single PDN src NAT zero PDN test */
  3310. /*---------------------------------------------------------------------------*/
  3311. class IpaNatBlockTest014 : public IpaNatBlockTest001
  3312. {
  3313. public:
  3314. IpaNatBlockTest014()
  3315. {
  3316. m_name = "IpaNatBlockTest014";
  3317. m_description =
  3318. "NAT block test 014 - single PDN src NAT PDN zeroing test\
  3319. 1. Generate and commit three routing tables (only one is used). \
  3320. Each table contains a single \"bypass\" rule (all data goes to output pipe 0, 1 and 2 (accordingly)) \
  3321. 2. Generate and commit one filtering rule: (DST & Mask Match). \
  3322. action go to src NAT \
  3323. All DST_IP == (193.23.22.1 & 0.255.255.255)traffic goes to NAT block \
  3324. 3. generate and commit one NAT rule:\
  3325. private ip 194.23.22.1 --> public ip 192.23.22.1\
  3326. 4. modify PDN entry 0 and expect NAT miss";
  3327. m_minIPAHwType = IPA_HW_v4_0;
  3328. }
  3329. virtual bool AddRules()
  3330. {
  3331. ipa_nat_pdn_entry pdn_info;
  3332. LOG_MSG_DEBUG("Entering\n");
  3333. int ret = IpaNatBlockTest001::AddRules();
  3334. if (!ret) {
  3335. LOG_MSG_ERROR("Leaving, failed Adding test 001 rules 0\n");
  3336. return false;
  3337. }
  3338. // modify PDN entry 0 so the NAT rule will get a NAT miss
  3339. pdn_info.public_ip = 0;
  3340. pdn_info.src_metadata = 0;
  3341. pdn_info.dst_metadata = 0;
  3342. ret = ipa_nat_modify_pdn(m_tbl_hdl, 0, &pdn_info);
  3343. if (ret) {
  3344. LOG_MSG_ERROR("Leaving, failed Modifying PDN entry 0 \n");
  3345. return false;
  3346. }
  3347. LOG_MSG_DEBUG("PDN modified\n");
  3348. LOG_MSG_DEBUG("Leaving\n");
  3349. return true;
  3350. }// AddRules()
  3351. virtual bool ReceivePacketsAndCompare()
  3352. {
  3353. size_t receivedSize = 0;
  3354. bool isSuccess = true;
  3355. // Receive results
  3356. Byte *rxBuff1 = new Byte[0x400];
  3357. if (NULL == rxBuff1)
  3358. {
  3359. LOG_MSG_ERROR("Memory allocation error.\n");
  3360. return false;
  3361. }
  3362. receivedSize = m_consumer.ReceiveData(rxBuff1, 0x400);
  3363. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize, m_consumer.m_fromChannelName.c_str());
  3364. if (receivedSize) {
  3365. LOG_MSG_ERROR("Data received - test failed!\n");
  3366. isSuccess = false;
  3367. // Compare results
  3368. if (!CompareResultVsGoldenNat(
  3369. m_sendBuffer, m_sendSize,
  3370. rxBuff1, receivedSize,
  3371. m_private_ip, m_public_ip,
  3372. m_private_port, m_public_port,
  3373. true))
  3374. {
  3375. LOG_MSG_ERROR("Comparison of Buffer0 Failed!\n");
  3376. }
  3377. else {
  3378. LOG_MSG_ERROR("Comparison of Buffer0 succeeded - NAT rule was hit despite deletion!\n");
  3379. }
  3380. char recievedBuffer[256] = { 0 };
  3381. char SentBuffer[256] = { 0 };
  3382. size_t j;
  3383. for (j = 0; j < m_sendSize; j++)
  3384. snprintf(&SentBuffer[3 * j], sizeof(SentBuffer) - (3 * j + 1), " %02X", m_sendBuffer[j]);
  3385. for (j = 0; j < receivedSize; j++)
  3386. snprintf(&recievedBuffer[3 * j], sizeof(recievedBuffer) - (3 * j + 1), " %02X", rxBuff1[j]);
  3387. LOG_MSG_STACK("sent Value1 (%zu)\n%s\n, Received Value1(%zu)\n%s\n", m_sendSize, SentBuffer, receivedSize, recievedBuffer);
  3388. }
  3389. delete[] rxBuff1;
  3390. return isSuccess;
  3391. }
  3392. };
  3393. /*---------------------------------------------------------------------------*/
  3394. /* Test015: Single PDN src NAT send two packets that will hit the same rule */
  3395. /*---------------------------------------------------------------------------*/
  3396. class IpaNatBlockTest015 : public IpaNatBlockTest001
  3397. {
  3398. public:
  3399. IpaNatBlockTest015()
  3400. {
  3401. m_name = "IpaNatBlockTest015";
  3402. m_description =
  3403. "NAT block test 015 - single PDN src NAT hit the same rule twice\
  3404. 1. Generate and commit three routing tables (only one is used). \
  3405. Each table contains a single \"bypass\" rule (all data goes to output pipe 0, 1 and 2 (accordingly)) \
  3406. 2. Generate and commit one filtering rule: (DST & Mask Match). \
  3407. action go to src NAT \
  3408. All DST_IP == (193.23.22.1 & 0.255.255.255)traffic goes to NAT block \
  3409. 3. generate and commit one NAT rule:\
  3410. private ip 194.23.22.1 --> public ip 192.23.22.1\
  3411. 4. send two pakcets and verify NATing";
  3412. }
  3413. virtual bool SendPackets()
  3414. {
  3415. bool ret = IpaNatBlockTest001::SendPackets();
  3416. if (!ret)
  3417. {
  3418. LOG_MSG_ERROR("failed sending first pakcket.\n");
  3419. return false;
  3420. }
  3421. LOG_MSG_DEBUG("first packet sent succesfully.\n");
  3422. ret = IpaNatBlockTest001::SendPackets();
  3423. if (!ret)
  3424. {
  3425. LOG_MSG_ERROR("failed sending second pakcket.\n");
  3426. return false;
  3427. }
  3428. LOG_MSG_DEBUG("second packet sent succesfully.\n");
  3429. return true;
  3430. }
  3431. virtual bool ReceivePacketsAndCompare()
  3432. {
  3433. bool isSuccess = true;
  3434. isSuccess &= IpaNatBlockTest001::ReceivePacketsAndCompare();
  3435. if (!isSuccess)
  3436. {
  3437. LOG_MSG_ERROR("failed to receive\\compare first packet.\n");
  3438. }
  3439. isSuccess &= IpaNatBlockTest001::ReceivePacketsAndCompare();
  3440. if (!isSuccess)
  3441. {
  3442. LOG_MSG_ERROR("failed to receive\\compare second packet.\n");
  3443. }
  3444. return isSuccess;
  3445. }
  3446. };
  3447. /*---------------------------------------------------------------------------*/
  3448. /* Test016: Single PDN dst NAT send two packets that will hit the same rule */
  3449. /*---------------------------------------------------------------------------*/
  3450. class IpaNatBlockTest016 : public IpaNatBlockTest002
  3451. {
  3452. public:
  3453. IpaNatBlockTest016()
  3454. {
  3455. m_name = "IpaNatBlockTest016";
  3456. m_description =
  3457. "NAT block test 016 - single PDN dst NAT hit the same rule twice\
  3458. 1. Generate and commit three routing tables (only one is used). \
  3459. Each table contains a single \"bypass\" rule (all data goes to output pipe 0, 1 and 2 (accordingly)) \
  3460. 2. Generate and commit one filtering rule: (DST & Mask Match). \
  3461. action go to dst NAT \
  3462. All DST_IP == (192.23.22.1 & 0.255.255.255)traffic goes to NAT block \
  3463. 3. generate and commit one NAT rule:\
  3464. public ip 192.23.22.1 --> private ip 194.23.22.1\
  3465. 4. send two pakcets and verify NATing";
  3466. }
  3467. virtual bool SendPackets()
  3468. {
  3469. bool ret = IpaNatBlockTest002::SendPackets();
  3470. if (!ret)
  3471. {
  3472. LOG_MSG_ERROR("failed sending first pakcket.\n");
  3473. return false;
  3474. }
  3475. LOG_MSG_DEBUG("first packet sent succesfully.\n");
  3476. ret = IpaNatBlockTest002::SendPackets();
  3477. if (!ret)
  3478. {
  3479. LOG_MSG_ERROR("failed sending second pakcket.\n");
  3480. return false;
  3481. }
  3482. LOG_MSG_DEBUG("second packet sent succesfully.\n");
  3483. return true;
  3484. }
  3485. virtual bool ReceivePacketsAndCompare()
  3486. {
  3487. bool isSuccess = true;
  3488. isSuccess &= IpaNatBlockTest002::ReceivePacketsAndCompare();
  3489. if (!isSuccess)
  3490. {
  3491. LOG_MSG_ERROR("failed to receive\\compare first packet.\n");
  3492. }
  3493. isSuccess &= IpaNatBlockTest002::ReceivePacketsAndCompare();
  3494. if (!isSuccess)
  3495. {
  3496. LOG_MSG_ERROR("failed to receive\\compare second packet.\n");
  3497. }
  3498. return isSuccess;
  3499. }
  3500. };
  3501. /*---------------------------------------------------------------------------------*/
  3502. /* Test017: Multi PDN src NAT test with identical private IPs and different ports */
  3503. /*---------------------------------------------------------------------------------*/
  3504. class IpaNatBlockTest017 : public IpaNatBlockTest003
  3505. {
  3506. public:
  3507. IpaNatBlockTest017()
  3508. {
  3509. m_name = "IpaNatBlockTest017";
  3510. m_description =
  3511. "NAT block test 017 - Multi PDN src NAT test with identical private IPs\
  3512. 1. Generate and commit three routing tables (two are used). \
  3513. Each table contains a single \"bypass\" rule (all data goes to output pipe 0, 1 and 2 (accordingly)) \
  3514. 2. Generate and commit two filtering rule: (DST & Mask Match). \
  3515. - action go to src NAT \
  3516. All SRC_IP == (194.23.22.1 & 0.255.255.255)traffic goes to NAT block \
  3517. All SRC_IP == (197.23.22.1 & 0.255.255.255)traffic goes to NAT block - not relevant for this test \
  3518. 3. generate and commit two NAT rules:\
  3519. private ip 194.23.22.1 && port 5678--> public ip 192.23.22.1 \
  3520. private ip 194.23.22.1 && port 5679--> public ip 195.23.22.1";
  3521. m_private_ip2 = m_private_ip;
  3522. }
  3523. virtual bool ReceivePacketsAndCompare()
  3524. {
  3525. // we cannot just use test 003 ReceivePacketsAndCompare since the filtering rules send the
  3526. // packets to two different pipes, but since the private IPs are now equal the second filtering rule
  3527. // won't be hit so we need to recive the second packet on the first pipe
  3528. size_t receivedSize = 0;
  3529. size_t receivedSize2 = 0;
  3530. bool isSuccess = true;
  3531. // Receive results
  3532. Byte *rxBuff1 = new Byte[0x400];
  3533. Byte *rxBuff2 = new Byte[0x400];
  3534. if (rxBuff1 == NULL)
  3535. {
  3536. LOG_MSG_ERROR("Memory allocation error.\n");
  3537. if (rxBuff2)
  3538. delete[] rxBuff2;
  3539. return false;
  3540. }
  3541. if (rxBuff2 == NULL)
  3542. {
  3543. LOG_MSG_ERROR("Memory allocation error.\n");
  3544. delete[] rxBuff1;
  3545. return false;
  3546. }
  3547. receivedSize = m_consumer.ReceiveData(rxBuff1, 0x400);
  3548. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize, m_consumer.m_fromChannelName.c_str());
  3549. receivedSize2 = m_consumer.ReceiveData(rxBuff2, 0x400);
  3550. LOG_MSG_DEBUG("Received %zu bytes on %s.\n", receivedSize2, m_consumer.m_fromChannelName.c_str());
  3551. // Compare results
  3552. if (!CompareResultVsGoldenNat(
  3553. m_sendBuffer, m_sendSize,
  3554. rxBuff1, receivedSize,
  3555. m_private_ip, m_public_ip,
  3556. m_private_port, m_public_port,
  3557. true))
  3558. {
  3559. LOG_MSG_ERROR("Comparison of Buffer0 Failed!\n");
  3560. isSuccess = false;
  3561. }
  3562. char recievedBuffer[256] = { 0 };
  3563. char SentBuffer[256] = { 0 };
  3564. char recievedBuffer2[256] = { 0 };
  3565. char SentBuffer2[256] = { 0 };
  3566. size_t j;
  3567. for (j = 0; j < m_sendSize; j++)
  3568. snprintf(&SentBuffer[3 * j], sizeof(SentBuffer) - (3 * j + 1), " %02X", m_sendBuffer[j]);
  3569. for (j = 0; j < receivedSize; j++)
  3570. snprintf(&recievedBuffer[3 * j], sizeof(recievedBuffer) - (3 * j + 1), " %02X", rxBuff1[j]);
  3571. LOG_MSG_STACK("sent Value1 (%zu)\n%s\n, Received Value1(%zu)\n%s\n", m_sendSize, SentBuffer, receivedSize, recievedBuffer);
  3572. delete[] rxBuff1;
  3573. isSuccess &= CompareResultVsGoldenNat(
  3574. m_sendBuffer2, m_sendSize2,
  3575. rxBuff2, receivedSize2,
  3576. m_private_ip2, m_public_ip2,
  3577. m_private_port2, m_public_port2,
  3578. true);
  3579. for (j = 0; j < m_sendSize2; j++)
  3580. snprintf(&SentBuffer2[3 * j], sizeof(SentBuffer2) - (3 * j + 1), " %02X", m_sendBuffer2[j]);
  3581. for (j = 0; j < receivedSize2; j++)
  3582. snprintf(&recievedBuffer2[3 * j], sizeof(recievedBuffer2) - (3 * j + 1), " %02X", rxBuff2[j]);
  3583. LOG_MSG_STACK("sent Value2 (%zu)\n%s\n, Received Value2(%zu)\n%s\n", m_sendSize2, SentBuffer2, receivedSize2, recievedBuffer2);
  3584. delete[] rxBuff2;
  3585. return isSuccess;
  3586. }
  3587. };
  3588. /*---------------------------------------------------------------------------------*/
  3589. /* Test018: Multi PDN dst NAT test with identical private IPs and different ports */
  3590. /*---------------------------------------------------------------------------------*/
  3591. class IpaNatBlockTest018 : public IpaNatBlockTest004
  3592. {
  3593. public:
  3594. IpaNatBlockTest018()
  3595. {
  3596. m_name = "IpaNatBlockTest018";
  3597. m_description =
  3598. "NAT block test 018 - Multi PDN dst NAT test with identical private IPs\
  3599. 1. Generate and commit three routing tables (two are used). \
  3600. Each table contains a single \"bypass\" rule (all data goes to output pipe 0, 1 and 2 (accordingly)) \
  3601. 2. Generate and commit two filtering rule: (DST & Mask Match). \
  3602. - action go to dst NAT \
  3603. All DST_IP == (192.23.22.1 & 0.255.255.255)traffic goes to NAT block \
  3604. All DST_IP == (195.23.22.1 & 0.255.255.255)traffic goes to NAT block - not releveant for this test\
  3605. 3. generate and commit two NAT rules:\
  3606. private ip 194.23.22.1 --> public ip 192.23.22.1 && port 9050\
  3607. private ip 194.23.22.1 --> public ip 192.23.22.1 && port 9051";
  3608. m_private_ip2 = m_private_ip;
  3609. }
  3610. };
  3611. static class IpaNatBlockTest001 IpaNatBlockTest001;//single PDN src NAT test
  3612. static class IpaNatBlockTest002 IpaNatBlockTest002;//single PDN dst NAT test
  3613. static class IpaNatBlockTest003 IpaNatBlockTest003;//multi PDN (tuple) src NAT test
  3614. static class IpaNatBlockTest004 IpaNatBlockTest004;//multi PDN (tuple) dst NAT test
  3615. static class IpaNatBlockTest005 IpaNatBlockTest005;//single PDN src NAT test - src metadata replacement
  3616. static class IpaNatBlockTest006 IpaNatBlockTest006;//single PDN dst NAT test - dst metadata replacement
  3617. static class IpaNatBlockTest007 IpaNatBlockTest007;//hashable routing rule with dst NAT test
  3618. static class IpaNatBlockTest008 IpaNatBlockTest008;//Multi PDN src NAT test match PDN by input from filtering block
  3619. static class IpaNatBlockTest009 IpaNatBlockTest009;//single PDN src NAT rule deletion test
  3620. static class IpaNatBlockTest010 IpaNatBlockTest010;//single PDN dst NAT rule deletion test
  3621. static class IpaNatBlockTest011 IpaNatBlockTest011;//Multi PDN src NAT - MAX number of PDNs test
  3622. static class IpaNatBlockTest012 IpaNatBlockTest012;//Single PDN dst NAT test expansion table usage
  3623. static class IpaNatBlockTest013 IpaNatBlockTest013;//Single PDN dst NAT test expansion table delete test
  3624. static class IpaNatBlockTest014 IpaNatBlockTest014;//single PDN src NAT zero PDN test
  3625. static class IpaNatBlockTest015 IpaNatBlockTest015;//single PDN src NAT test - send two packets that will hit the same rule
  3626. static class IpaNatBlockTest016 IpaNatBlockTest016;//single PDN dst NAT test - send two packets that will hit the same rule
  3627. static class IpaNatBlockTest017 IpaNatBlockTest017;//multi PDN (tuple) src NAT test - identical private IPs different ports
  3628. static class IpaNatBlockTest018 IpaNatBlockTest018;//multi PDN (tuple) dst NAT test - identical private IPs different ports