NatTest.cpp 165 KB

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