linux-2.6.22.6-openswan-2.4.9.kernel-2.6-klips.patch 1.8 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265492664926749268492694927049271492724927349274492754927649277492784927949280492814928249283492844928549286492874928849289492904929149292492934929449295492964929749298492994930049301493024930349304493054930649307493084930949310493114931249313493144931549316493174931849319493204932149322493234932449325493264932749328493294933049331493324933349334493354933649337493384933949340493414934249343493444934549346493474934849349493504935149352493534935449355493564935749358493594936049361493624936349364493654936649367493684936949370493714937249373493744937549376493774937849379493804938149382493834938449385493864938749388493894939049391493924939349394493954939649397493984939949400494014940249403494044940549406494074940849409494104941149412494134941449415494164941749418494194942049421494224942349424494254942649427494284942949430494314943249433494344943549436494374943849439494404944149442494434944449445494464944749448494494945049451494524945349454494554945649457494584945949460494614946249463494644946549466494674946849469494704947149472494734947449475494764947749478494794948049481494824948349484494854948649487494884948949490494914949249493494944949549496494974949849499495004950149502495034950449505495064950749508495094951049511495124951349514495154951649517495184951949520495214952249523495244952549526495274952849529495304953149532495334953449535495364953749538495394954049541495424954349544495454954649547495484954949550495514955249553495544955549556495574955849559495604956149562495634956449565495664956749568495694957049571495724957349574495754957649577495784957949580495814958249583495844958549586495874958849589495904959149592495934959449595495964959749598495994960049601496024960349604496054960649607496084960949610496114961249613496144961549616496174961849619496204962149622496234962449625496264962749628496294963049631496324963349634496354963649637496384963949640496414964249643496444964549646496474964849649496504965149652496534965449655496564965749658496594966049661496624966349664496654966649667496684966949670496714967249673496744967549676496774967849679496804968149682496834968449685496864968749688496894969049691496924969349694496954969649697496984969949700497014970249703497044970549706497074970849709497104971149712497134971449715497164971749718497194972049721497224972349724497254972649727497284972949730497314973249733497344973549736497374973849739497404974149742497434974449745497464974749748497494975049751497524975349754497554975649757497584975949760497614976249763497644976549766497674976849769497704977149772497734977449775497764977749778497794978049781497824978349784497854978649787497884978949790497914979249793497944979549796497974979849799498004980149802498034980449805498064980749808498094981049811498124981349814498154981649817498184981949820498214982249823498244982549826498274982849829498304983149832498334983449835498364983749838498394984049841498424984349844498454984649847498484984949850498514985249853498544985549856498574985849859498604986149862498634986449865498664986749868498694987049871498724987349874498754987649877498784987949880498814988249883498844988549886498874988849889498904989149892498934989449895498964989749898498994990049901499024990349904499054990649907499084990949910499114991249913499144991549916499174991849919499204992149922499234992449925499264992749928499294993049931499324993349934499354993649937499384993949940499414994249943499444994549946499474994849949499504995149952499534995449955499564995749958499594996049961499624996349964499654996649967499684996949970499714997249973499744997549976499774997849979499804998149982499834998449985499864998749988499894999049991499924999349994499954999649997499984999950000500015000250003500045000550006500075000850009500105001150012500135001450015500165001750018500195002050021500225002350024500255002650027500285002950030500315003250033500345003550036500375003850039500405004150042500435004450045500465004750048500495005050051500525005350054500555005650057500585005950060500615006250063500645006550066500675006850069500705007150072500735007450075500765007750078500795008050081500825008350084500855008650087500885008950090500915009250093500945009550096500975009850099501005010150102501035010450105501065010750108501095011050111501125011350114501155011650117501185011950120501215012250123501245012550126501275012850129501305013150132501335013450135501365013750138501395014050141501425014350144501455014650147501485014950150501515015250153501545015550156501575015850159501605016150162501635016450165501665016750168501695017050171501725017350174501755017650177501785017950180501815018250183501845018550186501875018850189501905019150192501935019450195501965019750198501995020050201502025020350204502055020650207502085020950210502115021250213502145021550216502175021850219502205022150222502235022450225502265022750228502295023050231502325023350234502355023650237502385023950240502415024250243502445024550246502475024850249502505025150252502535025450255502565025750258502595026050261502625026350264502655026650267502685026950270502715027250273502745027550276502775027850279502805028150282502835028450285502865028750288502895029050291502925029350294502955029650297502985029950300503015030250303503045030550306503075030850309503105031150312503135031450315503165031750318503195032050321503225032350324503255032650327503285032950330503315033250333503345033550336503375033850339503405034150342503435034450345503465034750348503495035050351503525035350354503555035650357503585035950360503615036250363503645036550366503675036850369503705037150372503735037450375503765037750378503795038050381503825038350384503855038650387503885038950390503915039250393503945039550396503975039850399504005040150402504035040450405504065040750408504095041050411504125041350414504155041650417504185041950420504215042250423504245042550426504275042850429504305043150432504335043450435504365043750438504395044050441504425044350444504455044650447504485044950450504515045250453504545045550456504575045850459504605046150462504635046450465504665046750468504695047050471504725047350474504755047650477504785047950480504815048250483504845048550486504875048850489504905049150492504935049450495504965049750498504995050050501505025050350504505055050650507505085050950510505115051250513505145051550516505175051850519505205052150522505235052450525505265052750528505295053050531505325053350534505355053650537505385053950540505415054250543505445054550546505475054850549505505055150552505535055450555505565055750558505595056050561505625056350564505655056650567505685056950570505715057250573505745057550576505775057850579505805058150582505835058450585505865058750588505895059050591505925059350594505955059650597505985059950600506015060250603506045060550606506075060850609506105061150612506135061450615506165061750618506195062050621506225062350624506255062650627506285062950630506315063250633506345063550636506375063850639506405064150642506435064450645506465064750648506495065050651506525065350654506555065650657506585065950660506615066250663506645066550666506675066850669506705067150672506735067450675506765067750678506795068050681506825068350684506855068650687506885068950690506915069250693506945069550696506975069850699507005070150702507035070450705507065070750708507095071050711507125071350714507155071650717507185071950720507215072250723507245072550726507275072850729507305073150732507335073450735507365073750738507395074050741507425074350744507455074650747507485074950750507515075250753507545075550756507575075850759507605076150762507635076450765507665076750768507695077050771507725077350774507755077650777507785077950780507815078250783507845078550786507875078850789507905079150792507935079450795507965079750798507995080050801508025080350804508055080650807508085080950810508115081250813508145081550816508175081850819508205082150822508235082450825508265082750828508295083050831508325083350834508355083650837508385083950840508415084250843508445084550846508475084850849508505085150852508535085450855508565085750858508595086050861508625086350864508655086650867508685086950870508715087250873508745087550876508775087850879508805088150882508835088450885508865088750888508895089050891508925089350894508955089650897508985089950900509015090250903509045090550906509075090850909509105091150912509135091450915509165091750918509195092050921509225092350924509255092650927509285092950930509315093250933509345093550936509375093850939509405094150942509435094450945509465094750948509495095050951509525095350954509555095650957509585095950960509615096250963509645096550966509675096850969509705097150972509735097450975509765097750978509795098050981509825098350984509855098650987509885098950990509915099250993509945099550996509975099850999510005100151002510035100451005510065100751008510095101051011510125101351014510155101651017510185101951020510215102251023510245102551026510275102851029510305103151032510335103451035510365103751038510395104051041510425104351044510455104651047510485104951050510515105251053510545105551056510575105851059510605106151062510635106451065510665106751068510695107051071510725107351074510755107651077510785107951080510815108251083510845108551086510875108851089510905109151092510935109451095510965109751098510995110051101511025110351104511055110651107511085110951110511115111251113511145111551116511175111851119511205112151122511235112451125511265112751128511295113051131511325113351134511355113651137511385113951140511415114251143511445114551146511475114851149511505115151152511535115451155511565115751158511595116051161511625116351164511655116651167511685116951170511715117251173511745117551176511775117851179511805118151182511835118451185511865118751188511895119051191511925119351194511955119651197511985119951200512015120251203512045120551206512075120851209512105121151212512135121451215512165121751218512195122051221512225122351224512255122651227512285122951230512315123251233512345123551236512375123851239512405124151242512435124451245512465124751248512495125051251512525125351254512555125651257512585125951260512615126251263512645126551266512675126851269512705127151272512735127451275512765127751278512795128051281512825128351284512855128651287512885128951290512915129251293512945129551296512975129851299513005130151302513035130451305513065130751308513095131051311513125131351314513155131651317513185131951320513215132251323513245132551326513275132851329513305133151332513335133451335513365133751338513395134051341513425134351344513455134651347513485134951350513515135251353513545135551356513575135851359513605136151362513635136451365513665136751368513695137051371513725137351374513755137651377513785137951380513815138251383513845138551386513875138851389513905139151392513935139451395513965139751398513995140051401514025140351404514055140651407514085140951410514115141251413514145141551416514175141851419514205142151422514235142451425514265142751428514295143051431514325143351434514355143651437514385143951440514415144251443514445144551446514475144851449514505145151452514535145451455514565145751458514595146051461514625146351464514655146651467514685146951470514715147251473514745147551476514775147851479514805148151482514835148451485514865148751488514895149051491514925149351494514955149651497514985149951500515015150251503515045150551506515075150851509515105151151512515135151451515515165151751518515195152051521515225152351524515255152651527515285152951530515315153251533515345153551536515375153851539515405154151542515435154451545515465154751548515495155051551515525155351554515555155651557515585155951560515615156251563515645156551566515675156851569515705157151572515735157451575515765157751578515795158051581515825158351584515855158651587515885158951590515915159251593515945159551596515975159851599516005160151602516035160451605516065160751608516095161051611516125161351614516155161651617516185161951620516215162251623516245162551626516275162851629516305163151632516335163451635516365163751638516395164051641516425164351644516455164651647516485164951650516515165251653516545165551656516575165851659516605166151662516635166451665516665166751668516695167051671516725167351674516755167651677516785167951680516815168251683516845168551686516875168851689516905169151692516935169451695516965169751698516995170051701517025170351704517055170651707517085170951710517115171251713517145171551716517175171851719517205172151722517235172451725517265172751728517295173051731517325173351734517355173651737517385173951740517415174251743517445174551746517475174851749517505175151752517535175451755517565175751758517595176051761517625176351764517655176651767517685176951770517715177251773517745177551776517775177851779517805178151782517835178451785517865178751788517895179051791517925179351794517955179651797517985179951800518015180251803518045180551806518075180851809518105181151812518135181451815518165181751818518195182051821518225182351824518255182651827518285182951830518315183251833518345183551836518375183851839518405184151842518435184451845518465184751848518495185051851518525185351854518555185651857518585185951860518615186251863518645186551866518675186851869518705187151872518735187451875518765187751878518795188051881518825188351884518855188651887518885188951890518915189251893518945189551896518975189851899519005190151902519035190451905519065190751908519095191051911519125191351914519155191651917519185191951920519215192251923519245192551926519275192851929519305193151932519335193451935519365193751938519395194051941519425194351944519455194651947519485194951950519515195251953519545195551956519575195851959519605196151962519635196451965519665196751968519695197051971519725197351974519755197651977519785197951980519815198251983519845198551986519875198851989519905199151992519935199451995519965199751998519995200052001520025200352004520055200652007520085200952010520115201252013520145201552016520175201852019520205202152022520235202452025520265202752028520295203052031520325203352034520355203652037520385203952040520415204252043520445204552046520475204852049520505205152052520535205452055520565205752058520595206052061520625206352064520655206652067520685206952070520715207252073520745207552076520775207852079520805208152082520835208452085520865208752088520895209052091520925209352094520955209652097520985209952100521015210252103521045210552106521075210852109521105211152112521135211452115521165211752118521195212052121521225212352124521255212652127521285212952130521315213252133521345213552136521375213852139521405214152142521435214452145521465214752148521495215052151521525215352154521555215652157521585215952160521615216252163521645216552166521675216852169521705217152172521735217452175521765217752178521795218052181521825218352184521855218652187521885218952190521915219252193521945219552196521975219852199522005220152202522035220452205522065220752208522095221052211522125221352214522155221652217522185221952220522215222252223522245222552226522275222852229522305223152232522335223452235522365223752238522395224052241522425224352244522455224652247522485224952250522515225252253522545225552256522575225852259522605226152262522635226452265522665226752268522695227052271522725227352274522755227652277522785227952280522815228252283522845228552286522875228852289522905229152292522935229452295522965229752298522995230052301523025230352304523055230652307523085230952310523115231252313523145231552316523175231852319523205232152322523235232452325523265232752328523295233052331523325233352334523355233652337523385233952340523415234252343523445234552346523475234852349523505235152352523535235452355523565235752358523595236052361523625236352364523655236652367523685236952370523715237252373523745237552376523775237852379523805238152382523835238452385523865238752388523895239052391523925239352394523955239652397523985239952400524015240252403524045240552406524075240852409524105241152412524135241452415524165241752418524195242052421524225242352424524255242652427524285242952430524315243252433524345243552436524375243852439524405244152442524435244452445524465244752448524495245052451524525245352454524555245652457524585245952460524615246252463524645246552466524675246852469524705247152472524735247452475524765247752478524795248052481524825248352484524855248652487524885248952490524915249252493524945249552496524975249852499525005250152502525035250452505525065250752508525095251052511525125251352514525155251652517525185251952520525215252252523525245252552526525275252852529525305253152532525335253452535525365253752538525395254052541525425254352544525455254652547525485254952550525515255252553525545255552556525575255852559525605256152562525635256452565525665256752568525695257052571525725257352574525755257652577525785257952580525815258252583525845258552586525875258852589525905259152592525935259452595525965259752598525995260052601526025260352604526055260652607526085260952610526115261252613526145261552616526175261852619526205262152622526235262452625526265262752628526295263052631526325263352634526355263652637526385263952640526415264252643526445264552646526475264852649526505265152652526535265452655526565265752658526595266052661526625266352664526655266652667526685266952670526715267252673526745267552676526775267852679526805268152682526835268452685526865268752688526895269052691526925269352694526955269652697526985269952700527015270252703527045270552706527075270852709527105271152712527135271452715527165271752718527195272052721527225272352724527255272652727527285272952730527315273252733527345273552736527375273852739527405274152742527435274452745527465274752748527495275052751527525275352754527555275652757527585275952760527615276252763527645276552766527675276852769527705277152772527735277452775527765277752778527795278052781527825278352784527855278652787527885278952790527915279252793527945279552796527975279852799528005280152802528035280452805528065280752808528095281052811528125281352814528155281652817528185281952820528215282252823528245282552826528275282852829528305283152832528335283452835528365283752838528395284052841528425284352844528455284652847528485284952850528515285252853528545285552856528575285852859528605286152862528635286452865528665286752868528695287052871528725287352874528755287652877528785287952880528815288252883528845288552886528875288852889528905289152892528935289452895528965289752898528995290052901529025290352904529055290652907529085290952910529115291252913529145291552916529175291852919529205292152922529235292452925529265292752928529295293052931529325293352934529355293652937529385293952940529415294252943529445294552946529475294852949529505295152952529535295452955529565295752958529595296052961529625296352964529655296652967529685296952970529715297252973529745297552976529775297852979529805298152982529835298452985529865298752988529895299052991529925299352994529955299652997529985299953000530015300253003530045300553006530075300853009530105301153012530135301453015530165301753018530195302053021530225302353024530255302653027530285302953030530315303253033530345303553036530375303853039530405304153042530435304453045530465304753048530495305053051530525305353054530555305653057530585305953060530615306253063530645306553066530675306853069530705307153072530735307453075530765307753078530795308053081530825308353084530855308653087530885308953090530915309253093530945309553096530975309853099531005310153102531035310453105531065310753108531095311053111531125311353114531155311653117531185311953120531215312253123531245312553126531275312853129531305313153132531335313453135531365313753138531395314053141531425314353144531455314653147531485314953150531515315253153531545315553156531575315853159531605316153162531635316453165531665316753168531695317053171531725317353174531755317653177531785317953180531815318253183531845318553186531875318853189531905319153192531935319453195531965319753198531995320053201532025320353204532055320653207532085320953210532115321253213532145321553216532175321853219532205322153222532235322453225532265322753228532295323053231532325323353234532355323653237532385323953240532415324253243532445324553246532475324853249532505325153252532535325453255532565325753258532595326053261532625326353264532655326653267532685326953270532715327253273532745327553276532775327853279532805328153282532835328453285532865328753288532895329053291532925329353294532955329653297532985329953300533015330253303533045330553306533075330853309533105331153312533135331453315533165331753318533195332053321533225332353324533255332653327533285332953330533315333253333533345333553336533375333853339533405334153342533435334453345533465334753348533495335053351533525335353354533555335653357533585335953360533615336253363533645336553366533675336853369533705337153372533735337453375533765337753378533795338053381533825338353384533855338653387533885338953390533915339253393533945339553396533975339853399534005340153402534035340453405534065340753408534095341053411534125341353414534155341653417534185341953420534215342253423534245342553426534275342853429534305343153432534335343453435534365343753438534395344053441534425344353444534455344653447534485344953450534515345253453534545345553456534575345853459534605346153462534635346453465534665346753468534695347053471534725347353474534755347653477534785347953480534815348253483534845348553486534875348853489534905349153492534935349453495534965349753498534995350053501535025350353504535055350653507535085350953510535115351253513535145351553516535175351853519535205352153522535235352453525535265352753528535295353053531535325353353534535355353653537535385353953540535415354253543535445354553546535475354853549535505355153552535535355453555535565355753558535595356053561535625356353564535655356653567535685356953570535715357253573535745357553576535775357853579535805358153582535835358453585535865358753588535895359053591535925359353594535955359653597535985359953600536015360253603536045360553606536075360853609536105361153612536135361453615536165361753618536195362053621536225362353624536255362653627536285362953630536315363253633536345363553636536375363853639536405364153642536435364453645536465364753648536495365053651536525365353654536555365653657536585365953660536615366253663536645366553666536675366853669536705367153672536735367453675536765367753678536795368053681536825368353684536855368653687536885368953690536915369253693536945369553696536975369853699537005370153702537035370453705537065370753708537095371053711537125371353714537155371653717537185371953720537215372253723537245372553726537275372853729537305373153732537335373453735537365373753738537395374053741537425374353744537455374653747537485374953750537515375253753537545375553756537575375853759537605376153762537635376453765537665376753768537695377053771537725377353774537755377653777537785377953780537815378253783537845378553786537875378853789537905379153792537935379453795537965379753798537995380053801538025380353804538055380653807538085380953810538115381253813538145381553816538175381853819538205382153822538235382453825538265382753828538295383053831538325383353834538355383653837538385383953840538415384253843538445384553846538475384853849538505385153852538535385453855538565385753858538595386053861538625386353864538655386653867538685386953870538715387253873538745387553876538775387853879538805388153882538835388453885538865388753888538895389053891538925389353894538955389653897538985389953900539015390253903539045390553906539075390853909539105391153912539135391453915539165391753918539195392053921539225392353924539255392653927539285392953930539315393253933539345393553936539375393853939539405394153942539435394453945539465394753948539495395053951539525395353954539555395653957539585395953960539615396253963539645396553966539675396853969539705397153972539735397453975539765397753978539795398053981539825398353984539855398653987539885398953990539915399253993539945399553996539975399853999540005400154002540035400454005540065400754008540095401054011540125401354014540155401654017540185401954020540215402254023540245402554026540275402854029540305403154032540335403454035540365403754038540395404054041540425404354044540455404654047540485404954050540515405254053540545405554056540575405854059540605406154062540635406454065540665406754068540695407054071540725407354074540755407654077540785407954080540815408254083540845408554086540875408854089540905409154092540935409454095540965409754098540995410054101541025410354104541055410654107541085410954110541115411254113541145411554116541175411854119541205412154122541235412454125541265412754128541295413054131541325413354134541355413654137541385413954140541415414254143541445414554146541475414854149541505415154152541535415454155541565415754158541595416054161541625416354164541655416654167541685416954170541715417254173541745417554176541775417854179541805418154182541835418454185541865418754188541895419054191541925419354194541955419654197541985419954200542015420254203542045420554206542075420854209542105421154212542135421454215542165421754218542195422054221542225422354224542255422654227542285422954230542315423254233542345423554236542375423854239542405424154242542435424454245542465424754248542495425054251542525425354254542555425654257542585425954260542615426254263542645426554266542675426854269542705427154272542735427454275542765427754278542795428054281542825428354284542855428654287542885428954290542915429254293542945429554296542975429854299543005430154302543035430454305543065430754308543095431054311543125431354314543155431654317543185431954320543215432254323543245432554326543275432854329543305433154332543335433454335543365433754338543395434054341543425434354344543455434654347543485434954350543515435254353543545435554356543575435854359543605436154362543635436454365543665436754368543695437054371543725437354374543755437654377543785437954380543815438254383543845438554386543875438854389543905439154392543935439454395543965439754398543995440054401544025440354404544055440654407544085440954410544115441254413544145441554416544175441854419544205442154422544235442454425544265442754428544295443054431544325443354434544355443654437544385443954440544415444254443544445444554446544475444854449544505445154452544535445454455544565445754458544595446054461544625446354464544655446654467544685446954470544715447254473544745447554476544775447854479544805448154482544835448454485544865448754488544895449054491544925449354494544955449654497544985449954500545015450254503545045450554506545075450854509545105451154512545135451454515545165451754518545195452054521545225452354524545255452654527545285452954530545315453254533545345453554536545375453854539545405454154542545435454454545545465454754548545495455054551545525455354554545555455654557545585455954560545615456254563545645456554566545675456854569545705457154572545735457454575545765457754578545795458054581545825458354584545855458654587545885458954590545915459254593545945459554596545975459854599546005460154602546035460454605546065460754608546095461054611546125461354614546155461654617546185461954620546215462254623546245462554626546275462854629546305463154632546335463454635546365463754638546395464054641546425464354644546455464654647546485464954650546515465254653546545465554656546575465854659546605466154662546635466454665546665466754668546695467054671546725467354674546755467654677546785467954680546815468254683546845468554686546875468854689546905469154692546935469454695546965469754698546995470054701547025470354704547055470654707547085470954710547115471254713547145471554716547175471854719547205472154722547235472454725547265472754728547295473054731547325473354734547355473654737547385473954740547415474254743547445474554746547475474854749547505475154752547535475454755547565475754758547595476054761547625476354764547655476654767547685476954770547715477254773547745477554776547775477854779547805478154782547835478454785547865478754788547895479054791547925479354794547955479654797547985479954800548015480254803548045480554806548075480854809548105481154812548135481454815548165481754818548195482054821548225482354824548255482654827548285482954830548315483254833548345483554836548375483854839548405484154842548435484454845548465484754848548495485054851548525485354854548555485654857548585485954860548615486254863548645486554866548675486854869548705487154872548735487454875548765487754878548795488054881548825488354884548855488654887548885488954890548915489254893548945489554896548975489854899549005490154902549035490454905549065490754908549095491054911549125491354914549155491654917549185491954920549215492254923549245492554926549275492854929549305493154932549335493454935549365493754938549395494054941549425494354944549455494654947549485494954950549515495254953549545495554956549575495854959549605496154962549635496454965549665496754968549695497054971549725497354974549755497654977549785497954980549815498254983549845498554986549875498854989549905499154992549935499454995549965499754998549995500055001550025500355004550055500655007550085500955010550115501255013550145501555016550175501855019550205502155022550235502455025550265502755028550295503055031550325503355034550355503655037550385503955040550415504255043550445504555046550475504855049550505505155052550535505455055550565505755058550595506055061550625506355064550655506655067550685506955070550715507255073550745507555076550775507855079550805508155082550835508455085550865508755088550895509055091550925509355094550955509655097550985509955100551015510255103551045510555106551075510855109551105511155112551135511455115551165511755118551195512055121551225512355124551255512655127551285512955130551315513255133551345513555136551375513855139551405514155142551435514455145551465514755148551495515055151551525515355154551555515655157551585515955160551615516255163551645516555166551675516855169551705517155172551735517455175551765517755178551795518055181551825518355184551855518655187551885518955190551915519255193551945519555196551975519855199552005520155202552035520455205552065520755208552095521055211552125521355214552155521655217552185521955220552215522255223552245522555226552275522855229552305523155232552335523455235552365523755238552395524055241552425524355244552455524655247552485524955250552515525255253552545525555256552575525855259552605526155262552635526455265552665526755268552695527055271552725527355274552755527655277552785527955280552815528255283552845528555286552875528855289552905529155292552935529455295552965529755298552995530055301553025530355304553055530655307553085530955310553115531255313553145531555316553175531855319553205532155322553235532455325553265532755328553295533055331553325533355334553355533655337553385533955340553415534255343553445534555346553475534855349553505535155352553535535455355553565535755358553595536055361553625536355364553655536655367553685536955370553715537255373553745537555376553775537855379553805538155382553835538455385553865538755388553895539055391553925539355394553955539655397553985539955400554015540255403554045540555406554075540855409554105541155412554135541455415554165541755418554195542055421554225542355424554255542655427554285542955430554315543255433554345543555436554375543855439554405544155442554435544455445554465544755448554495545055451554525545355454554555545655457554585545955460554615546255463554645546555466554675546855469554705547155472554735547455475554765547755478554795548055481554825548355484554855548655487554885548955490554915549255493554945549555496554975549855499555005550155502555035550455505555065550755508555095551055511555125551355514555155551655517555185551955520555215552255523555245552555526555275552855529555305553155532555335553455535555365553755538555395554055541555425554355544555455554655547555485554955550555515555255553555545555555556555575555855559555605556155562555635556455565555665556755568555695557055571555725557355574555755557655577555785557955580555815558255583555845558555586555875558855589555905559155592555935559455595555965559755598555995560055601556025560355604556055560655607556085560955610556115561255613556145561555616556175561855619556205562155622556235562455625556265562755628556295563055631556325563355634556355563655637556385563955640556415564255643556445564555646556475564855649556505565155652556535565455655556565565755658556595566055661556625566355664556655566655667556685566955670556715567255673556745567555676556775567855679556805568155682556835568455685556865568755688556895569055691556925569355694556955569655697556985569955700557015570255703557045570555706557075570855709557105571155712557135571455715557165571755718557195572055721557225572355724557255572655727557285572955730557315573255733557345573555736557375573855739557405574155742557435574455745557465574755748557495575055751557525575355754557555575655757557585575955760557615576255763557645576555766557675576855769557705577155772557735577455775557765577755778557795578055781557825578355784557855578655787557885578955790557915579255793557945579555796557975579855799558005580155802558035580455805558065580755808558095581055811558125581355814558155581655817558185581955820558215582255823558245582555826558275582855829558305583155832558335583455835558365583755838558395584055841558425584355844558455584655847558485584955850558515585255853558545585555856558575585855859558605586155862558635586455865558665586755868558695587055871558725587355874558755587655877558785587955880558815588255883558845588555886558875588855889558905589155892558935589455895558965589755898558995590055901559025590355904559055590655907559085590955910559115591255913559145591555916559175591855919559205592155922559235592455925559265592755928559295593055931559325593355934559355593655937559385593955940559415594255943559445594555946559475594855949559505595155952559535595455955559565595755958559595596055961559625596355964559655596655967559685596955970559715597255973559745597555976559775597855979559805598155982559835598455985559865598755988559895599055991559925599355994559955599655997559985599956000560015600256003560045600556006560075600856009560105601156012560135601456015560165601756018560195602056021560225602356024560255602656027560285602956030560315603256033560345603556036560375603856039560405604156042560435604456045560465604756048560495605056051560525605356054560555605656057560585605956060560615606256063560645606556066560675606856069560705607156072560735607456075560765607756078560795608056081560825608356084560855608656087560885608956090560915609256093560945609556096560975609856099561005610156102561035610456105561065610756108561095611056111561125611356114561155611656117561185611956120561215612256123561245612556126561275612856129561305613156132561335613456135561365613756138561395614056141561425614356144561455614656147561485614956150561515615256153561545615556156561575615856159561605616156162561635616456165561665616756168561695617056171561725617356174561755617656177561785617956180561815618256183561845618556186561875618856189561905619156192561935619456195561965619756198561995620056201562025620356204562055620656207562085620956210562115621256213562145621556216562175621856219562205622156222562235622456225562265622756228562295623056231562325623356234562355623656237562385623956240562415624256243562445624556246562475624856249562505625156252562535625456255562565625756258562595626056261562625626356264562655626656267562685626956270562715627256273562745627556276562775627856279562805628156282562835628456285562865628756288562895629056291562925629356294562955629656297562985629956300563015630256303563045630556306563075630856309563105631156312563135631456315563165631756318563195632056321563225632356324563255632656327563285632956330563315633256333563345633556336563375633856339563405634156342563435634456345563465634756348563495635056351563525635356354563555635656357563585635956360563615636256363563645636556366563675636856369563705637156372563735637456375563765637756378563795638056381563825638356384563855638656387563885638956390563915639256393563945639556396563975639856399564005640156402564035640456405564065640756408564095641056411564125641356414564155641656417564185641956420564215642256423564245642556426564275642856429564305643156432564335643456435564365643756438564395644056441564425644356444564455644656447564485644956450564515645256453564545645556456564575645856459564605646156462564635646456465564665646756468564695647056471564725647356474564755647656477564785647956480564815648256483564845648556486564875648856489564905649156492564935649456495564965649756498564995650056501565025650356504565055650656507565085650956510565115651256513565145651556516565175651856519565205652156522565235652456525565265652756528565295653056531565325653356534565355653656537565385653956540565415654256543565445654556546565475654856549565505655156552565535655456555565565655756558565595656056561565625656356564565655656656567565685656956570565715657256573565745657556576565775657856579565805658156582565835658456585565865658756588565895659056591565925659356594565955659656597565985659956600566015660256603566045660556606566075660856609566105661156612566135661456615566165661756618566195662056621566225662356624566255662656627566285662956630566315663256633566345663556636566375663856639566405664156642566435664456645566465664756648566495665056651566525665356654566555665656657566585665956660566615666256663566645666556666566675666856669566705667156672566735667456675566765667756678566795668056681566825668356684566855668656687566885668956690566915669256693566945669556696566975669856699567005670156702567035670456705567065670756708567095671056711567125671356714567155671656717567185671956720567215672256723567245672556726567275672856729567305673156732567335673456735567365673756738567395674056741567425674356744567455674656747567485674956750567515675256753567545675556756567575675856759567605676156762567635676456765567665676756768567695677056771567725677356774567755677656777567785677956780567815678256783567845678556786567875678856789567905679156792567935679456795567965679756798567995680056801568025680356804568055680656807568085680956810568115681256813568145681556816568175681856819568205682156822568235682456825568265682756828568295683056831568325683356834568355683656837568385683956840568415684256843568445684556846568475684856849568505685156852568535685456855568565685756858568595686056861568625686356864568655686656867568685686956870568715687256873568745687556876568775687856879568805688156882568835688456885568865688756888568895689056891568925689356894568955689656897568985689956900569015690256903569045690556906569075690856909569105691156912569135691456915569165691756918569195692056921569225692356924569255692656927569285692956930569315693256933569345693556936569375693856939569405694156942569435694456945569465694756948569495695056951569525695356954569555695656957569585695956960569615696256963569645696556966569675696856969569705697156972569735697456975569765697756978569795698056981569825698356984569855698656987569885698956990569915699256993569945699556996569975699856999570005700157002570035700457005570065700757008570095701057011570125701357014570155701657017570185701957020570215702257023570245702557026570275702857029570305703157032570335703457035570365703757038570395704057041570425704357044570455704657047570485704957050570515705257053570545705557056570575705857059570605706157062570635706457065570665706757068570695707057071570725707357074570755707657077570785707957080570815708257083570845708557086570875708857089570905709157092570935709457095570965709757098570995710057101571025710357104571055710657107571085710957110571115711257113571145711557116571175711857119571205712157122571235712457125571265712757128571295713057131571325713357134571355713657137571385713957140571415714257143571445714557146571475714857149571505715157152571535715457155571565715757158571595716057161571625716357164571655716657167571685716957170571715717257173571745717557176571775717857179571805718157182571835718457185571865718757188571895719057191571925719357194571955719657197571985719957200572015720257203572045720557206572075720857209572105721157212572135721457215572165721757218572195722057221572225722357224572255722657227572285722957230572315723257233572345723557236572375723857239572405724157242572435724457245572465724757248572495725057251572525725357254572555725657257572585725957260572615726257263572645726557266572675726857269572705727157272572735727457275572765727757278572795728057281572825728357284572855728657287572885728957290572915729257293572945729557296572975729857299573005730157302573035730457305573065730757308573095731057311573125731357314573155731657317573185731957320573215732257323573245732557326573275732857329573305733157332573335733457335573365733757338573395734057341573425734357344573455734657347573485734957350573515735257353573545735557356573575735857359573605736157362573635736457365573665736757368573695737057371573725737357374573755737657377573785737957380573815738257383573845738557386573875738857389573905739157392573935739457395573965739757398573995740057401574025740357404574055740657407574085740957410574115741257413574145741557416574175741857419574205742157422574235742457425574265742757428574295743057431574325743357434574355743657437574385743957440574415744257443574445744557446574475744857449574505745157452574535745457455574565745757458574595746057461574625746357464574655746657467574685746957470574715747257473574745747557476574775747857479574805748157482574835748457485574865748757488574895749057491574925749357494574955749657497574985749957500575015750257503575045750557506575075750857509575105751157512575135751457515575165751757518575195752057521575225752357524575255752657527575285752957530575315753257533575345753557536575375753857539575405754157542575435754457545575465754757548575495755057551575525755357554575555755657557575585755957560575615756257563575645756557566575675756857569575705757157572575735757457575575765757757578575795758057581575825758357584575855758657587575885758957590575915759257593575945759557596575975759857599576005760157602576035760457605576065760757608576095761057611576125761357614576155761657617576185761957620576215762257623576245762557626576275762857629576305763157632576335763457635576365763757638576395764057641576425764357644576455764657647576485764957650576515765257653576545765557656576575765857659576605766157662576635766457665576665766757668576695767057671576725767357674576755767657677576785767957680576815768257683576845768557686576875768857689576905769157692576935769457695576965769757698576995770057701577025770357704577055770657707577085770957710577115771257713577145771557716577175771857719577205772157722577235772457725577265772757728577295773057731577325773357734577355773657737577385773957740577415774257743577445774557746577475774857749577505775157752577535775457755577565775757758577595776057761577625776357764577655776657767577685776957770577715777257773577745777557776577775777857779577805778157782577835778457785577865778757788577895779057791577925779357794577955779657797577985779957800578015780257803578045780557806578075780857809578105781157812578135781457815578165781757818578195782057821578225782357824578255782657827578285782957830578315783257833578345783557836578375783857839578405784157842578435784457845578465784757848578495785057851578525785357854578555785657857578585785957860578615786257863578645786557866578675786857869578705787157872578735787457875578765787757878578795788057881578825788357884578855788657887578885788957890578915789257893578945789557896578975789857899579005790157902579035790457905579065790757908579095791057911579125791357914579155791657917579185791957920579215792257923579245792557926579275792857929579305793157932579335793457935579365793757938579395794057941579425794357944579455794657947579485794957950579515795257953579545795557956579575795857959579605796157962579635796457965579665796757968579695797057971579725797357974579755797657977579785797957980579815798257983579845798557986579875798857989579905799157992579935799457995579965799757998579995800058001580025800358004580055800658007580085800958010580115801258013580145801558016580175801858019580205802158022580235802458025580265802758028580295803058031580325803358034580355803658037580385803958040580415804258043580445804558046580475804858049580505805158052580535805458055580565805758058580595806058061580625806358064580655806658067580685806958070580715807258073580745807558076580775807858079580805808158082580835808458085580865808758088580895809058091580925809358094580955809658097580985809958100581015810258103581045810558106581075810858109581105811158112581135811458115581165811758118581195812058121581225812358124581255812658127581285812958130581315813258133581345813558136581375813858139581405814158142581435814458145581465814758148581495815058151581525815358154581555815658157581585815958160581615816258163581645816558166581675816858169581705817158172581735817458175581765817758178581795818058181581825818358184581855818658187581885818958190581915819258193581945819558196581975819858199582005820158202582035820458205582065820758208582095821058211582125821358214582155821658217582185821958220582215822258223582245822558226582275822858229582305823158232582335823458235582365823758238582395824058241582425824358244582455824658247582485824958250582515825258253582545825558256582575825858259582605826158262582635826458265582665826758268582695827058271582725827358274582755827658277582785827958280582815828258283582845828558286582875828858289582905829158292582935829458295582965829758298582995830058301583025830358304583055830658307583085830958310583115831258313583145831558316583175831858319583205832158322583235832458325583265832758328583295833058331583325833358334583355833658337583385833958340583415834258343583445834558346583475834858349583505835158352583535835458355583565835758358583595836058361583625836358364583655836658367583685836958370583715837258373583745837558376583775837858379583805838158382583835838458385583865838758388583895839058391583925839358394583955839658397583985839958400584015840258403584045840558406584075840858409584105841158412584135841458415584165841758418584195842058421584225842358424584255842658427584285842958430584315843258433584345843558436584375843858439584405844158442584435844458445584465844758448584495845058451584525845358454584555845658457584585845958460584615846258463584645846558466584675846858469584705847158472584735847458475584765847758478584795848058481584825848358484584855848658487584885848958490584915849258493584945849558496584975849858499585005850158502585035850458505585065850758508585095851058511585125851358514585155851658517585185851958520585215852258523585245852558526585275852858529585305853158532585335853458535585365853758538585395854058541585425854358544585455854658547585485854958550585515855258553585545855558556585575855858559585605856158562585635856458565585665856758568585695857058571585725857358574585755857658577585785857958580585815858258583585845858558586585875858858589585905859158592585935859458595585965859758598585995860058601586025860358604586055860658607586085860958610586115861258613586145861558616586175861858619586205862158622586235862458625586265862758628586295863058631586325863358634586355863658637586385863958640586415864258643586445864558646586475864858649586505865158652586535865458655586565865758658586595866058661586625866358664586655866658667586685866958670586715867258673586745867558676586775867858679586805868158682586835868458685586865868758688586895869058691586925869358694586955869658697586985869958700587015870258703587045870558706587075870858709587105871158712587135871458715587165871758718587195872058721587225872358724587255872658727587285872958730587315873258733587345873558736587375873858739587405874158742587435874458745587465874758748587495875058751587525875358754587555875658757587585875958760587615876258763587645876558766587675876858769587705877158772587735877458775587765877758778587795878058781587825878358784587855878658787587885878958790587915879258793587945879558796587975879858799588005880158802588035880458805588065880758808588095881058811588125881358814588155881658817588185881958820588215882258823588245882558826588275882858829588305883158832588335883458835588365883758838588395884058841588425884358844588455884658847588485884958850588515885258853588545885558856588575885858859588605886158862588635886458865588665886758868588695887058871588725887358874588755887658877588785887958880588815888258883588845888558886588875888858889588905889158892588935889458895588965889758898588995890058901589025890358904589055890658907589085890958910589115891258913589145891558916589175891858919589205892158922589235892458925589265892758928589295893058931589325893358934589355893658937589385893958940589415894258943589445894558946589475894858949589505895158952589535895458955589565895758958589595896058961589625896358964589655896658967589685896958970589715897258973589745897558976589775897858979589805898158982589835898458985589865898758988589895899058991589925899358994589955899658997589985899959000590015900259003590045900559006590075900859009590105901159012590135901459015590165901759018590195902059021590225902359024590255902659027590285902959030590315903259033590345903559036590375903859039590405904159042590435904459045590465904759048590495905059051590525905359054590555905659057590585905959060590615906259063590645906559066590675906859069590705907159072590735907459075590765907759078590795908059081590825908359084590855908659087590885908959090590915909259093590945909559096590975909859099591005910159102591035910459105591065910759108591095911059111591125911359114591155911659117591185911959120591215912259123591245912559126591275912859129591305913159132591335913459135591365913759138591395914059141591425914359144591455914659147591485914959150591515915259153591545915559156591575915859159591605916159162591635916459165591665916759168591695917059171591725917359174591755917659177591785917959180591815918259183591845918559186591875918859189591905919159192591935919459195591965919759198591995920059201592025920359204592055920659207592085920959210592115921259213592145921559216592175921859219592205922159222592235922459225592265922759228592295923059231592325923359234592355923659237592385923959240592415924259243592445924559246592475924859249592505925159252592535925459255592565925759258592595926059261592625926359264592655926659267592685926959270592715927259273592745927559276592775927859279592805928159282592835928459285592865928759288592895929059291592925929359294592955929659297592985929959300593015930259303593045930559306593075930859309593105931159312593135931459315593165931759318593195932059321593225932359324593255932659327593285932959330593315933259333593345933559336593375933859339593405934159342593435934459345593465934759348593495935059351593525935359354593555935659357593585935959360593615936259363593645936559366593675936859369593705937159372593735937459375593765937759378593795938059381593825938359384593855938659387
  1. packaging/utils/kernelpatch 2.6
  2. --- /dev/null Tue Mar 11 13:02:56 2003
  3. +++ linux/README.openswan-2 Mon Feb 9 13:51:03 2004
  4. @@ -0,0 +1,112 @@
  5. +*
  6. +* RCSID $Id: README.openswan-2,v 1.1 2003/12/10 01:07:49 mcr Exp $
  7. +*
  8. +
  9. + ****************************************
  10. + * IPSEC for Linux, Release 2.xx series *
  11. + ****************************************
  12. +
  13. +
  14. +
  15. +1. Files
  16. +
  17. +The contents of linux/net/ipsec/ (see below) join the linux kernel source tree.
  18. +as provided for higher up.
  19. +
  20. +The programs/ directory contains the user-level utilities which you need
  21. +to run IPSEC. See the top-level top/INSTALL to compile and install them.
  22. +
  23. +The testing/ directory contains test scripts.
  24. +
  25. +The doc/ directory contains -- what else -- documentation.
  26. +
  27. +1.1. Kernel files
  28. +
  29. +The following are found in net/ipsec/:
  30. +
  31. +Makefile The Makefile
  32. +Config.in The configuration script for make menuconfig
  33. +defconfig Configuration defaults for first time.
  34. +
  35. +radij.c General-purpose radix-tree operations
  36. +
  37. +ipsec_ipcomp.c IPCOMP encapsulate/decapsulate code.
  38. +ipsec_ah.c Authentication Header (AH) encapsulate/decapsulate code.
  39. +ipsec_esp.c Encapsulated Security Payload (ESP) encap/decap code.
  40. +
  41. +pfkey_v2.c PF_KEYv2 socket interface code.
  42. +pfkey_v2_parser.c PF_KEYv2 message parsing and processing code.
  43. +
  44. +ipsec_init.c Initialization code, /proc interface.
  45. +ipsec_radij.c Interface with the radix tree code.
  46. +ipsec_netlink.c Interface with the netlink code.
  47. +ipsec_xform.c Routines and structures common to transforms.
  48. +ipsec_tunnel.c The outgoing packet processing code.
  49. +ipsec_rcv.c The incoming packet processing code.
  50. +ipsec_md5c.c Somewhat modified RSADSI MD5 C code.
  51. +ipsec_sha1.c Somewhat modified Steve Reid SHA-1 C code.
  52. +
  53. +sysctl_net_ipsec.c /proc/sys/net/ipsec/* variable definitions.
  54. +
  55. +version.c symbolic link to project version.
  56. +
  57. +radij.h Headers for radij.c
  58. +
  59. +ipcomp.h Headers used by IPCOMP code.
  60. +
  61. +ipsec_radij.h Interface with the radix tree code.
  62. +ipsec_netlink.h Headers used by the netlink interface.
  63. +ipsec_encap.h Headers defining encapsulation structures.
  64. +ipsec_xform.h Transform headers.
  65. +ipsec_tunnel.h Headers used by tunneling code.
  66. +ipsec_ipe4.h Headers for the IP-in-IP code.
  67. +ipsec_ah.h Headers common to AH transforms.
  68. +ipsec_md5h.h RSADSI MD5 headers.
  69. +ipsec_sha1.h SHA-1 headers.
  70. +ipsec_esp.h Headers common to ESP transfroms.
  71. +ipsec_rcv.h Headers for incoming packet processing code.
  72. +
  73. +1.2. User-level files.
  74. +
  75. +The following are found in utils/:
  76. +
  77. +eroute.c Create an "extended route" source code
  78. +spi.c Set up Security Associations source code
  79. +spigrp.c Link SPIs together source code.
  80. +tncfg.c Configure the tunneling features of the virtual interface
  81. + source code
  82. +klipsdebug.c Set/reset klips debugging features source code.
  83. +version.c symbolic link to project version.
  84. +
  85. +eroute.8 Create an "extended route" manual page
  86. +spi.8 Set up Security Associations manual page
  87. +spigrp.8 Link SPIs together manual page
  88. +tncfg.8 Configure the tunneling features of the virtual interface
  89. + manual page
  90. +klipsdebug.8 Set/reset klips debugging features manual page
  91. +
  92. +eroute.5 /proc/net/ipsec_eroute format manual page
  93. +spi.5 /proc/net/ipsec_spi format manual page
  94. +spigrp.5 /proc/net/ipsec_spigrp format manual page
  95. +tncfg.5 /proc/net/ipsec_tncfg format manual page
  96. +klipsdebug.5 /proc/net/ipsec_klipsdebug format manual page
  97. +version.5 /proc/net/ipsec_version format manual page
  98. +pf_key.5 /proc/net/pf_key format manual page
  99. +
  100. +Makefile Utilities makefile.
  101. +
  102. +*.8 Manpages for the respective utils.
  103. +
  104. +
  105. +1.3. Test files
  106. +
  107. +The test scripts are locate in testing/ and and documentation is found
  108. +at doc/src/umltesting.html. Automated testing via "make check" is available
  109. +provided that the User-Mode-Linux patches are available.
  110. +
  111. +*
  112. +* $Log: README.openswan-2,v $
  113. +* Revision 1.1 2003/12/10 01:07:49 mcr
  114. +* documentation for additions.
  115. +*
  116. +*
  117. --- /dev/null Tue Mar 11 13:02:56 2003
  118. +++ linux/crypto/ciphers/aes/test_main.c Mon Feb 9 13:51:03 2004
  119. @@ -0,0 +1,41 @@
  120. +#include <stdio.h>
  121. +#include <string.h>
  122. +#include <sys/types.h>
  123. +#include "aes_cbc.h"
  124. +#define AES_BLOCK_SIZE 16
  125. +#define KEY_SIZE 128 /* bits */
  126. +#define KEY "1234567890123456"
  127. +#define STR "hola guaso como estaisss ... 012"
  128. +#define STRSZ (sizeof(STR)-1)
  129. +
  130. +#define EMT_AESCBC_BLKLEN AES_BLOCK_SIZE
  131. +#define AES_CONTEXT_T aes_context
  132. +#define EMT_ESPAES_KEY_SZ 16
  133. +int pretty_print(const unsigned char *buf, int count) {
  134. + int i=0;
  135. + for (;i<count;i++) {
  136. + if (i%8==0) putchar(' ');
  137. + if (i%16==0) putchar('\n');
  138. + printf ("%02hhx ", buf[i]);
  139. + }
  140. + putchar('\n');
  141. + return i;
  142. +}
  143. +//#define SIZE STRSZ/2
  144. +#define SIZE STRSZ
  145. +int main() {
  146. + int ret;
  147. + char buf0[SIZE+1], buf1[SIZE+1];
  148. + char IV[AES_BLOCK_SIZE]="\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0";
  149. + aes_context ac;
  150. + AES_set_key(&ac, KEY, KEY_SIZE);
  151. + //pretty_print((char *)&ac.aes_e_key, sizeof(ac.aes_e_key));
  152. + memset(buf0, 0, sizeof (buf0));
  153. + memset(buf1, 0, sizeof (buf1));
  154. + ret=AES_cbc_encrypt(&ac, STR, buf0, SIZE, IV, 1);
  155. + pretty_print(buf0, SIZE);
  156. + printf("size=%d ret=%d\n%s\n", SIZE, ret, buf0);
  157. + ret=AES_cbc_encrypt(&ac, buf0, buf1, SIZE, IV, 0);
  158. + printf("size=%d ret=%d\n%s\n", SIZE, ret, buf1);
  159. + return 0;
  160. +}
  161. --- /dev/null Tue Mar 11 13:02:56 2003
  162. +++ linux/crypto/ciphers/aes/test_main_mac.c Mon Feb 9 13:51:03 2004
  163. @@ -0,0 +1,30 @@
  164. +#include <stdio.h>
  165. +#include <sys/types.h>
  166. +#include <string.h>
  167. +#include "aes.h"
  168. +#include "aes_xcbc_mac.h"
  169. +#define STR "Hola guasssso c|mo estais ...012"
  170. +void print_hash(const __u8 *hash) {
  171. + printf("%08x %08x %08x %08x\n",
  172. + *(__u32*)(&hash[0]),
  173. + *(__u32*)(&hash[4]),
  174. + *(__u32*)(&hash[8]),
  175. + *(__u32*)(&hash[12]));
  176. +}
  177. +int main(int argc, char *argv[]) {
  178. + aes_block key= { 0xdeadbeef, 0xceedcaca, 0xcafebabe, 0xff010204 };
  179. + __u8 hash[16];
  180. + char *str = argv[1];
  181. + aes_context_mac ctx;
  182. + if (str==NULL) {
  183. + fprintf(stderr, "pasame el str\n");
  184. + return 255;
  185. + }
  186. + AES_xcbc_mac_set_key(&ctx, (__u8 *)&key, sizeof(key));
  187. + AES_xcbc_mac_hash(&ctx, str, strlen(str), hash);
  188. + print_hash(hash);
  189. + str[2]='x';
  190. + AES_xcbc_mac_hash(&ctx, str, strlen(str), hash);
  191. + print_hash(hash);
  192. + return 0;
  193. +}
  194. --- /dev/null Tue Mar 11 13:02:56 2003
  195. +++ linux/include/crypto/aes.h Mon Feb 9 13:51:03 2004
  196. @@ -0,0 +1,97 @@
  197. +// I retain copyright in this code but I encourage its free use provided
  198. +// that I don't carry any responsibility for the results. I am especially
  199. +// happy to see it used in free and open source software. If you do use
  200. +// it I would appreciate an acknowledgement of its origin in the code or
  201. +// the product that results and I would also appreciate knowing a little
  202. +// about the use to which it is being put. I am grateful to Frank Yellin
  203. +// for some ideas that are used in this implementation.
  204. +//
  205. +// Dr B. R. Gladman <brg@gladman.uk.net> 6th April 2001.
  206. +//
  207. +// This is an implementation of the AES encryption algorithm (Rijndael)
  208. +// designed by Joan Daemen and Vincent Rijmen. This version is designed
  209. +// to provide both fixed and dynamic block and key lengths and can also
  210. +// run with either big or little endian internal byte order (see aes.h).
  211. +// It inputs block and key lengths in bytes with the legal values being
  212. +// 16, 24 and 32.
  213. +
  214. +/*
  215. + * Modified by Jari Ruusu, May 1 2001
  216. + * - Fixed some compile warnings, code was ok but gcc warned anyway.
  217. + * - Changed basic types: byte -> unsigned char, word -> u_int32_t
  218. + * - Major name space cleanup: Names visible to outside now begin
  219. + * with "aes_" or "AES_". A lot of stuff moved from aes.h to aes.c
  220. + * - Removed C++ and DLL support as part of name space cleanup.
  221. + * - Eliminated unnecessary recomputation of tables. (actual bug fix)
  222. + * - Merged precomputed constant tables to aes.c file.
  223. + * - Removed data alignment restrictions for portability reasons.
  224. + * - Made block and key lengths accept bit count (128/192/256)
  225. + * as well byte count (16/24/32).
  226. + * - Removed all error checks. This change also eliminated the need
  227. + * to preinitialize the context struct to zero.
  228. + * - Removed some totally unused constants.
  229. + */
  230. +
  231. +#ifndef _AES_H
  232. +#define _AES_H
  233. +
  234. +#if defined(__linux__) && defined(__KERNEL__)
  235. +# include <linux/types.h>
  236. +#else
  237. +# include <sys/types.h>
  238. +#endif
  239. +
  240. +// CONFIGURATION OPTIONS (see also aes.c)
  241. +//
  242. +// Define AES_BLOCK_SIZE to set the cipher block size (16, 24 or 32) or
  243. +// leave this undefined for dynamically variable block size (this will
  244. +// result in much slower code).
  245. +// IMPORTANT NOTE: AES_BLOCK_SIZE is in BYTES (16, 24, 32 or undefined). If
  246. +// left undefined a slower version providing variable block length is compiled
  247. +
  248. +#define AES_BLOCK_SIZE 16
  249. +
  250. +// The number of key schedule words for different block and key lengths
  251. +// allowing for method of computation which requires the length to be a
  252. +// multiple of the key length
  253. +//
  254. +// Nk = 4 6 8
  255. +// -------------
  256. +// Nb = 4 | 60 60 64
  257. +// 6 | 96 90 96
  258. +// 8 | 120 120 120
  259. +
  260. +#if !defined(AES_BLOCK_SIZE) || (AES_BLOCK_SIZE == 32)
  261. +#define AES_KS_LENGTH 120
  262. +#define AES_RC_LENGTH 29
  263. +#else
  264. +#define AES_KS_LENGTH 4 * AES_BLOCK_SIZE
  265. +#define AES_RC_LENGTH (9 * AES_BLOCK_SIZE) / 8 - 8
  266. +#endif
  267. +
  268. +typedef struct
  269. +{
  270. + u_int32_t aes_Nkey; // the number of words in the key input block
  271. + u_int32_t aes_Nrnd; // the number of cipher rounds
  272. + u_int32_t aes_e_key[AES_KS_LENGTH]; // the encryption key schedule
  273. + u_int32_t aes_d_key[AES_KS_LENGTH]; // the decryption key schedule
  274. +#if !defined(AES_BLOCK_SIZE)
  275. + u_int32_t aes_Ncol; // the number of columns in the cipher state
  276. +#endif
  277. +} aes_context;
  278. +
  279. +// THE CIPHER INTERFACE
  280. +
  281. +#if !defined(AES_BLOCK_SIZE)
  282. +extern void aes_set_blk(aes_context *, const int);
  283. +#endif
  284. +extern void aes_set_key(aes_context *, const unsigned char [], const int, const int);
  285. +extern void aes_encrypt(const aes_context *, const unsigned char [], unsigned char []);
  286. +extern void aes_decrypt(const aes_context *, const unsigned char [], unsigned char []);
  287. +
  288. +// The block length inputs to aes_set_block and aes_set_key are in numbers
  289. +// of bytes or bits. The calls to subroutines must be made in the above
  290. +// order but multiple calls can be made without repeating earlier calls
  291. +// if their parameters have not changed.
  292. +
  293. +#endif // _AES_H
  294. --- /dev/null Tue Mar 11 13:02:56 2003
  295. +++ linux/include/crypto/aes_cbc.h Mon Feb 9 13:51:03 2004
  296. @@ -0,0 +1,4 @@
  297. +/* Glue header */
  298. +#include "aes.h"
  299. +int AES_set_key(aes_context *aes_ctx, const u_int8_t * key, int keysize);
  300. +int AES_cbc_encrypt(aes_context *ctx, const u_int8_t * in, u_int8_t * out, int ilen, const u_int8_t * iv, int encrypt);
  301. --- /dev/null Tue Mar 11 13:02:56 2003
  302. +++ linux/include/crypto/aes_xcbc_mac.h Mon Feb 9 13:51:03 2004
  303. @@ -0,0 +1,12 @@
  304. +#ifndef _AES_XCBC_MAC_H
  305. +#define _AES_XCBC_MAC_H
  306. +
  307. +typedef u_int32_t aes_block[4];
  308. +typedef struct {
  309. + aes_context ctx_k1;
  310. + aes_block k2;
  311. + aes_block k3;
  312. +} aes_context_mac;
  313. +int AES_xcbc_mac_set_key(aes_context_mac *ctxm, const u_int8_t *key, int keylen);
  314. +int AES_xcbc_mac_hash(const aes_context_mac *ctxm, const u_int8_t * in, int ilen, u_int8_t hash[16]);
  315. +#endif /* _AES_XCBC_MAC_H */
  316. --- /dev/null Tue Mar 11 13:02:56 2003
  317. +++ linux/include/crypto/cbc_generic.h Mon Feb 9 13:51:03 2004
  318. @@ -0,0 +1,110 @@
  319. +#ifndef _CBC_GENERIC_H
  320. +#define _CBC_GENERIC_H
  321. +/*
  322. + * CBC macro helpers
  323. + *
  324. + * Author: JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  325. + *
  326. + * This program is free software; you can redistribute it and/or modify it
  327. + * under the terms of the GNU General Public License as published by the
  328. + * Free Software Foundation; either version 2 of the License, or (at your
  329. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  330. + *
  331. + * This program is distributed in the hope that it will be useful, but
  332. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  333. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  334. + * for more details.
  335. + *
  336. + */
  337. +
  338. +/*
  339. + * Heavily inspired in loop_AES
  340. + */
  341. +#define CBC_IMPL_BLK16(name, ctx_type, addr_type, enc_func, dec_func) \
  342. +int name(ctx_type *ctx, const u_int8_t * in, u_int8_t * out, int ilen, const u_int8_t * iv, int encrypt) { \
  343. + int ret=ilen, pos; \
  344. + const u_int32_t *iv_i; \
  345. + if ((ilen) % 16) return 0; \
  346. + if (encrypt) { \
  347. + pos=0; \
  348. + while(pos<ilen) { \
  349. + if (pos==0) \
  350. + iv_i=(const u_int32_t*) iv; \
  351. + else \
  352. + iv_i=(const u_int32_t*) (out-16); \
  353. + *((u_int32_t *)(&out[ 0])) = iv_i[0]^*((const u_int32_t *)(&in[ 0])); \
  354. + *((u_int32_t *)(&out[ 4])) = iv_i[1]^*((const u_int32_t *)(&in[ 4])); \
  355. + *((u_int32_t *)(&out[ 8])) = iv_i[2]^*((const u_int32_t *)(&in[ 8])); \
  356. + *((u_int32_t *)(&out[12])) = iv_i[3]^*((const u_int32_t *)(&in[12])); \
  357. + enc_func(ctx, (addr_type) out, (addr_type) out); \
  358. + in+=16; \
  359. + out+=16; \
  360. + pos+=16; \
  361. + } \
  362. + } else { \
  363. + pos=ilen-16; \
  364. + in+=pos; \
  365. + out+=pos; \
  366. + while(pos>=0) { \
  367. + dec_func(ctx, (const addr_type) in, (addr_type) out); \
  368. + if (pos==0) \
  369. + iv_i=(const u_int32_t*) (iv); \
  370. + else \
  371. + iv_i=(const u_int32_t*) (in-16); \
  372. + *((u_int32_t *)(&out[ 0])) ^= iv_i[0]; \
  373. + *((u_int32_t *)(&out[ 4])) ^= iv_i[1]; \
  374. + *((u_int32_t *)(&out[ 8])) ^= iv_i[2]; \
  375. + *((u_int32_t *)(&out[12])) ^= iv_i[3]; \
  376. + in-=16; \
  377. + out-=16; \
  378. + pos-=16; \
  379. + } \
  380. + } \
  381. + return ret; \
  382. +}
  383. +#define CBC_IMPL_BLK8(name, ctx_type, addr_type, enc_func, dec_func) \
  384. +int name(ctx_type *ctx, u_int8_t * in, u_int8_t * out, int ilen, const u_int8_t * iv, int encrypt) { \
  385. + int ret=ilen, pos; \
  386. + const u_int32_t *iv_i; \
  387. + if ((ilen) % 8) return 0; \
  388. + if (encrypt) { \
  389. + pos=0; \
  390. + while(pos<ilen) { \
  391. + if (pos==0) \
  392. + iv_i=(const u_int32_t*) iv; \
  393. + else \
  394. + iv_i=(const u_int32_t*) (out-8); \
  395. + *((u_int32_t *)(&out[ 0])) = iv_i[0]^*((const u_int32_t *)(&in[ 0])); \
  396. + *((u_int32_t *)(&out[ 4])) = iv_i[1]^*((const u_int32_t *)(&in[ 4])); \
  397. + enc_func(ctx, (addr_type)out, (addr_type)out); \
  398. + in+=8; \
  399. + out+=8; \
  400. + pos+=8; \
  401. + } \
  402. + } else { \
  403. + pos=ilen-8; \
  404. + in+=pos; \
  405. + out+=pos; \
  406. + while(pos>=0) { \
  407. + dec_func(ctx, (const addr_type)in, (addr_type)out); \
  408. + if (pos==0) \
  409. + iv_i=(const u_int32_t*) (iv); \
  410. + else \
  411. + iv_i=(const u_int32_t*) (in-8); \
  412. + *((u_int32_t *)(&out[ 0])) ^= iv_i[0]; \
  413. + *((u_int32_t *)(&out[ 4])) ^= iv_i[1]; \
  414. + in-=8; \
  415. + out-=8; \
  416. + pos-=8; \
  417. + } \
  418. + } \
  419. + return ret; \
  420. +}
  421. +#define CBC_DECL(name, ctx_type) \
  422. +int name(ctx_type *ctx, u_int8_t * in, u_int8_t * out, int ilen, const u_int8_t * iv, int encrypt)
  423. +/*
  424. +Eg.:
  425. +CBC_IMPL_BLK16(AES_cbc_encrypt, aes_context, u_int8_t *, aes_encrypt, aes_decrypt);
  426. +CBC_DECL(AES_cbc_encrypt, aes_context);
  427. +*/
  428. +#endif /* _CBC_GENERIC_H */
  429. --- /dev/null Tue Mar 11 13:02:56 2003
  430. +++ linux/include/crypto/des.h Mon Feb 9 13:51:03 2004
  431. @@ -0,0 +1,298 @@
  432. +/* crypto/des/des.org */
  433. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  434. + * All rights reserved.
  435. + *
  436. + * This package is an SSL implementation written
  437. + * by Eric Young (eay@cryptsoft.com).
  438. + * The implementation was written so as to conform with Netscapes SSL.
  439. + *
  440. + * This library is free for commercial and non-commercial use as long as
  441. + * the following conditions are aheared to. The following conditions
  442. + * apply to all code found in this distribution, be it the RC4, RSA,
  443. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  444. + * included with this distribution is covered by the same copyright terms
  445. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  446. + *
  447. + * Copyright remains Eric Young's, and as such any Copyright notices in
  448. + * the code are not to be removed.
  449. + * If this package is used in a product, Eric Young should be given attribution
  450. + * as the author of the parts of the library used.
  451. + * This can be in the form of a textual message at program startup or
  452. + * in documentation (online or textual) provided with the package.
  453. + *
  454. + * Redistribution and use in source and binary forms, with or without
  455. + * modification, are permitted provided that the following conditions
  456. + * are met:
  457. + * 1. Redistributions of source code must retain the copyright
  458. + * notice, this list of conditions and the following disclaimer.
  459. + * 2. Redistributions in binary form must reproduce the above copyright
  460. + * notice, this list of conditions and the following disclaimer in the
  461. + * documentation and/or other materials provided with the distribution.
  462. + * 3. All advertising materials mentioning features or use of this software
  463. + * must display the following acknowledgement:
  464. + * "This product includes cryptographic software written by
  465. + * Eric Young (eay@cryptsoft.com)"
  466. + * The word 'cryptographic' can be left out if the rouines from the library
  467. + * being used are not cryptographic related :-).
  468. + * 4. If you include any Windows specific code (or a derivative thereof) from
  469. + * the apps directory (application code) you must include an acknowledgement:
  470. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  471. + *
  472. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  473. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  474. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  475. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  476. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  477. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  478. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  479. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  480. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  481. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  482. + * SUCH DAMAGE.
  483. + *
  484. + * The licence and distribution terms for any publically available version or
  485. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  486. + * copied and put under another distribution licence
  487. + * [including the GNU Public Licence.]
  488. + */
  489. +
  490. +/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  491. + *
  492. + * Always modify des.org since des.h is automatically generated from
  493. + * it during SSLeay configuration.
  494. + *
  495. + * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  496. + */
  497. +
  498. +#ifndef HEADER_DES_H
  499. +#define HEADER_DES_H
  500. +
  501. +#ifdef __cplusplus
  502. +extern "C" {
  503. +#endif
  504. +
  505. +
  506. +/* If this is set to 'unsigned int' on a DEC Alpha, this gives about a
  507. + * %20 speed up (longs are 8 bytes, int's are 4). */
  508. +/* Must be unsigned int on ia64/Itanium or DES breaks badly */
  509. +
  510. +#ifdef __KERNEL__
  511. +#include <linux/types.h>
  512. +#else
  513. +#include <sys/types.h>
  514. +#endif
  515. +
  516. +#ifndef DES_LONG
  517. +#define DES_LONG u_int32_t
  518. +#endif
  519. +
  520. +typedef unsigned char des_cblock[8];
  521. +typedef struct { des_cblock ks; } des_key_schedule[16];
  522. +
  523. +#define DES_KEY_SZ (sizeof(des_cblock))
  524. +#define DES_SCHEDULE_SZ (sizeof(des_key_schedule))
  525. +
  526. +#define DES_ENCRYPT 1
  527. +#define DES_DECRYPT 0
  528. +
  529. +#define DES_CBC_MODE 0
  530. +#define DES_PCBC_MODE 1
  531. +
  532. +#define des_ecb2_encrypt(i,o,k1,k2,e) \
  533. + des_ecb3_encrypt((i),(o),(k1),(k2),(k1),(e))
  534. +
  535. +#define des_ede2_cbc_encrypt(i,o,l,k1,k2,iv,e) \
  536. + des_ede3_cbc_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(e))
  537. +
  538. +#define des_ede2_cfb64_encrypt(i,o,l,k1,k2,iv,n,e) \
  539. + des_ede3_cfb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n),(e))
  540. +
  541. +#define des_ede2_ofb64_encrypt(i,o,l,k1,k2,iv,n) \
  542. + des_ede3_ofb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n))
  543. +
  544. +#define C_Block des_cblock
  545. +#define Key_schedule des_key_schedule
  546. +#ifdef KERBEROS
  547. +#define ENCRYPT DES_ENCRYPT
  548. +#define DECRYPT DES_DECRYPT
  549. +#endif
  550. +#define KEY_SZ DES_KEY_SZ
  551. +#define string_to_key des_string_to_key
  552. +#define read_pw_string des_read_pw_string
  553. +#define random_key des_random_key
  554. +#define pcbc_encrypt des_pcbc_encrypt
  555. +#define set_key des_set_key
  556. +#define key_sched des_key_sched
  557. +#define ecb_encrypt des_ecb_encrypt
  558. +#define cbc_encrypt des_cbc_encrypt
  559. +#define ncbc_encrypt des_ncbc_encrypt
  560. +#define xcbc_encrypt des_xcbc_encrypt
  561. +#define cbc_cksum des_cbc_cksum
  562. +#define quad_cksum des_quad_cksum
  563. +
  564. +/* For compatibility with the MIT lib - eay 20/05/92 */
  565. +typedef des_key_schedule bit_64;
  566. +#define des_fixup_key_parity des_set_odd_parity
  567. +#define des_check_key_parity check_parity
  568. +
  569. +extern int des_check_key; /* defaults to false */
  570. +extern int des_rw_mode; /* defaults to DES_PCBC_MODE */
  571. +
  572. +/* The next line is used to disable full ANSI prototypes, if your
  573. + * compiler has problems with the prototypes, make sure this line always
  574. + * evaluates to true :-) */
  575. +#if defined(MSDOS) || defined(__STDC__)
  576. +#undef NOPROTO
  577. +#endif
  578. +#ifndef NOPROTO
  579. +char *des_options(void);
  580. +void des_ecb3_encrypt(des_cblock *input,des_cblock *output,
  581. + des_key_schedule ks1,des_key_schedule ks2,
  582. + des_key_schedule ks3, int enc);
  583. +DES_LONG des_cbc_cksum(des_cblock *input,des_cblock *output,
  584. + long length,des_key_schedule schedule,des_cblock *ivec);
  585. +void des_cbc_encrypt(des_cblock *input,des_cblock *output,long length,
  586. + des_key_schedule schedule,des_cblock *ivec,int enc);
  587. +void des_ncbc_encrypt(des_cblock *input,des_cblock *output,long length,
  588. + des_key_schedule schedule,des_cblock *ivec,int enc);
  589. +void des_xcbc_encrypt(des_cblock *input,des_cblock *output,long length,
  590. + des_key_schedule schedule,des_cblock *ivec,
  591. + des_cblock *inw,des_cblock *outw,int enc);
  592. +void des_cfb_encrypt(unsigned char *in,unsigned char *out,int numbits,
  593. + long length,des_key_schedule schedule,des_cblock *ivec,int enc);
  594. +void des_ecb_encrypt(des_cblock *input,des_cblock *output,
  595. + des_key_schedule ks,int enc);
  596. +void des_encrypt(DES_LONG *data,des_key_schedule ks, int enc);
  597. +void des_encrypt2(DES_LONG *data,des_key_schedule ks, int enc);
  598. +void des_encrypt3(DES_LONG *data, des_key_schedule ks1,
  599. + des_key_schedule ks2, des_key_schedule ks3);
  600. +void des_decrypt3(DES_LONG *data, des_key_schedule ks1,
  601. + des_key_schedule ks2, des_key_schedule ks3);
  602. +void des_ede3_cbc_encrypt(des_cblock *input, des_cblock *output,
  603. + long length, des_key_schedule ks1, des_key_schedule ks2,
  604. + des_key_schedule ks3, des_cblock *ivec, int enc);
  605. +void des_ede3_cfb64_encrypt(unsigned char *in, unsigned char *out,
  606. + long length, des_key_schedule ks1, des_key_schedule ks2,
  607. + des_key_schedule ks3, des_cblock *ivec, int *num, int enc);
  608. +void des_ede3_ofb64_encrypt(unsigned char *in, unsigned char *out,
  609. + long length, des_key_schedule ks1, des_key_schedule ks2,
  610. + des_key_schedule ks3, des_cblock *ivec, int *num);
  611. +
  612. +void des_xwhite_in2out(des_cblock (*des_key), des_cblock (*in_white),
  613. + des_cblock (*out_white));
  614. +
  615. +int des_enc_read(int fd,char *buf,int len,des_key_schedule sched,
  616. + des_cblock *iv);
  617. +int des_enc_write(int fd,char *buf,int len,des_key_schedule sched,
  618. + des_cblock *iv);
  619. +char *des_fcrypt(const char *buf,const char *salt, char *ret);
  620. +#ifdef PERL5
  621. +char *des_crypt(const char *buf,const char *salt);
  622. +#else
  623. +/* some stupid compilers complain because I have declared char instead
  624. + * of const char */
  625. +#ifndef __KERNEL__
  626. +#ifdef HEADER_DES_LOCL_H
  627. +char *crypt(const char *buf,const char *salt);
  628. +#else /* HEADER_DES_LOCL_H */
  629. +char *crypt(void);
  630. +#endif /* HEADER_DES_LOCL_H */
  631. +#endif /* __KERNEL__ */
  632. +#endif /* PERL5 */
  633. +void des_ofb_encrypt(unsigned char *in,unsigned char *out,
  634. + int numbits,long length,des_key_schedule schedule,des_cblock *ivec);
  635. +void des_pcbc_encrypt(des_cblock *input,des_cblock *output,long length,
  636. + des_key_schedule schedule,des_cblock *ivec,int enc);
  637. +DES_LONG des_quad_cksum(des_cblock *input,des_cblock *output,
  638. + long length,int out_count,des_cblock *seed);
  639. +void des_random_seed(des_cblock key);
  640. +void des_random_key(des_cblock ret);
  641. +int des_read_password(des_cblock *key,char *prompt,int verify);
  642. +int des_read_2passwords(des_cblock *key1,des_cblock *key2,
  643. + char *prompt,int verify);
  644. +int des_read_pw_string(char *buf,int length,char *prompt,int verify);
  645. +void des_set_odd_parity(des_cblock *key);
  646. +int des_is_weak_key(des_cblock *key);
  647. +int des_set_key(des_cblock *key,des_key_schedule schedule);
  648. +int des_key_sched(des_cblock *key,des_key_schedule schedule);
  649. +void des_string_to_key(char *str,des_cblock *key);
  650. +void des_string_to_2keys(char *str,des_cblock *key1,des_cblock *key2);
  651. +void des_cfb64_encrypt(unsigned char *in, unsigned char *out, long length,
  652. + des_key_schedule schedule, des_cblock *ivec, int *num, int enc);
  653. +void des_ofb64_encrypt(unsigned char *in, unsigned char *out, long length,
  654. + des_key_schedule schedule, des_cblock *ivec, int *num);
  655. +int des_read_pw(char *buf, char *buff, int size, char *prompt, int verify);
  656. +
  657. +/* Extra functions from Mark Murray <mark@grondar.za> */
  658. +/* The following functions are not in the normal unix build or the
  659. + * SSLeay build. When using the SSLeay build, use RAND_seed()
  660. + * and RAND_bytes() instead. */
  661. +int des_new_random_key(des_cblock *key);
  662. +void des_init_random_number_generator(des_cblock *key);
  663. +void des_set_random_generator_seed(des_cblock *key);
  664. +void des_set_sequence_number(des_cblock new_sequence_number);
  665. +void des_generate_random_block(des_cblock *block);
  666. +
  667. +#else
  668. +
  669. +char *des_options();
  670. +void des_ecb3_encrypt();
  671. +DES_LONG des_cbc_cksum();
  672. +void des_cbc_encrypt();
  673. +void des_ncbc_encrypt();
  674. +void des_xcbc_encrypt();
  675. +void des_cfb_encrypt();
  676. +void des_ede3_cfb64_encrypt();
  677. +void des_ede3_ofb64_encrypt();
  678. +void des_ecb_encrypt();
  679. +void des_encrypt();
  680. +void des_encrypt2();
  681. +void des_encrypt3();
  682. +void des_decrypt3();
  683. +void des_ede3_cbc_encrypt();
  684. +int des_enc_read();
  685. +int des_enc_write();
  686. +char *des_fcrypt();
  687. +#ifdef PERL5
  688. +char *des_crypt();
  689. +#else
  690. +char *crypt();
  691. +#endif
  692. +void des_ofb_encrypt();
  693. +void des_pcbc_encrypt();
  694. +DES_LONG des_quad_cksum();
  695. +void des_random_seed();
  696. +void des_random_key();
  697. +int des_read_password();
  698. +int des_read_2passwords();
  699. +int des_read_pw_string();
  700. +void des_set_odd_parity();
  701. +int des_is_weak_key();
  702. +int des_set_key();
  703. +int des_key_sched();
  704. +void des_string_to_key();
  705. +void des_string_to_2keys();
  706. +void des_cfb64_encrypt();
  707. +void des_ofb64_encrypt();
  708. +int des_read_pw();
  709. +void des_xwhite_in2out();
  710. +
  711. +/* Extra functions from Mark Murray <mark@grondar.za> */
  712. +/* The following functions are not in the normal unix build or the
  713. + * SSLeay build. When using the SSLeay build, use RAND_seed()
  714. + * and RAND_bytes() instead. */
  715. +#ifdef FreeBSD
  716. +int des_new_random_key();
  717. +void des_init_random_number_generator();
  718. +void des_set_random_generator_seed();
  719. +void des_set_sequence_number();
  720. +void des_generate_random_block();
  721. +#endif
  722. +
  723. +#endif
  724. +
  725. +#ifdef __cplusplus
  726. +}
  727. +#endif
  728. +
  729. +#endif
  730. --- /dev/null Tue Mar 11 13:02:56 2003
  731. +++ linux/include/des/des_locl.h Mon Feb 9 13:51:03 2004
  732. @@ -0,0 +1,515 @@
  733. +/* crypto/des/des_locl.org */
  734. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  735. + * All rights reserved.
  736. + *
  737. + * This package is an SSL implementation written
  738. + * by Eric Young (eay@cryptsoft.com).
  739. + * The implementation was written so as to conform with Netscapes SSL.
  740. + *
  741. + * This library is free for commercial and non-commercial use as long as
  742. + * the following conditions are aheared to. The following conditions
  743. + * apply to all code found in this distribution, be it the RC4, RSA,
  744. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  745. + * included with this distribution is covered by the same copyright terms
  746. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  747. + *
  748. + * Copyright remains Eric Young's, and as such any Copyright notices in
  749. + * the code are not to be removed.
  750. + * If this package is used in a product, Eric Young should be given attribution
  751. + * as the author of the parts of the library used.
  752. + * This can be in the form of a textual message at program startup or
  753. + * in documentation (online or textual) provided with the package.
  754. + *
  755. + * Redistribution and use in source and binary forms, with or without
  756. + * modification, are permitted provided that the following conditions
  757. + * are met:
  758. + * 1. Redistributions of source code must retain the copyright
  759. + * notice, this list of conditions and the following disclaimer.
  760. + * 2. Redistributions in binary form must reproduce the above copyright
  761. + * notice, this list of conditions and the following disclaimer in the
  762. + * documentation and/or other materials provided with the distribution.
  763. + * 3. All advertising materials mentioning features or use of this software
  764. + * must display the following acknowledgement:
  765. + * "This product includes cryptographic software written by
  766. + * Eric Young (eay@cryptsoft.com)"
  767. + * The word 'cryptographic' can be left out if the rouines from the library
  768. + * being used are not cryptographic related :-).
  769. + * 4. If you include any Windows specific code (or a derivative thereof) from
  770. + * the apps directory (application code) you must include an acknowledgement:
  771. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  772. + *
  773. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  774. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  775. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  776. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  777. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  778. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  779. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  780. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  781. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  782. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  783. + * SUCH DAMAGE.
  784. + *
  785. + * The licence and distribution terms for any publically available version or
  786. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  787. + * copied and put under another distribution licence
  788. + * [including the GNU Public Licence.]
  789. + */
  790. +
  791. +/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  792. + *
  793. + * Always modify des_locl.org since des_locl.h is automatically generated from
  794. + * it during SSLeay configuration.
  795. + *
  796. + * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  797. + */
  798. +
  799. +#ifndef HEADER_DES_LOCL_H
  800. +#define HEADER_DES_LOCL_H
  801. +
  802. +#if defined(WIN32) || defined(WIN16)
  803. +#ifndef MSDOS
  804. +#define MSDOS
  805. +#endif
  806. +#endif
  807. +
  808. +#include "crypto/des.h"
  809. +
  810. +#ifndef DES_DEFAULT_OPTIONS
  811. +/* the following is tweaked from a config script, that is why it is a
  812. + * protected undef/define */
  813. +#ifndef DES_PTR
  814. +#define DES_PTR
  815. +#endif
  816. +
  817. +/* This helps C compiler generate the correct code for multiple functional
  818. + * units. It reduces register dependancies at the expense of 2 more
  819. + * registers */
  820. +#ifndef DES_RISC1
  821. +#define DES_RISC1
  822. +#endif
  823. +
  824. +#ifndef DES_RISC2
  825. +#undef DES_RISC2
  826. +#endif
  827. +
  828. +#if defined(DES_RISC1) && defined(DES_RISC2)
  829. +YOU SHOULD NOT HAVE BOTH DES_RISC1 AND DES_RISC2 DEFINED!!!!!
  830. +#endif
  831. +
  832. +/* Unroll the inner loop, this sometimes helps, sometimes hinders.
  833. + * Very mucy CPU dependant */
  834. +#ifndef DES_UNROLL
  835. +#define DES_UNROLL
  836. +#endif
  837. +
  838. +/* These default values were supplied by
  839. + * Peter Gutman <pgut001@cs.auckland.ac.nz>
  840. + * They are only used if nothing else has been defined */
  841. +#if !defined(DES_PTR) && !defined(DES_RISC1) && !defined(DES_RISC2) && !defined(DES_UNROLL)
  842. +/* Special defines which change the way the code is built depending on the
  843. + CPU and OS. For SGI machines you can use _MIPS_SZLONG (32 or 64) to find
  844. + even newer MIPS CPU's, but at the moment one size fits all for
  845. + optimization options. Older Sparc's work better with only UNROLL, but
  846. + there's no way to tell at compile time what it is you're running on */
  847. +
  848. +#if defined( sun ) /* Newer Sparc's */
  849. + #define DES_PTR
  850. + #define DES_RISC1
  851. + #define DES_UNROLL
  852. +#elif defined( __ultrix ) /* Older MIPS */
  853. + #define DES_PTR
  854. + #define DES_RISC2
  855. + #define DES_UNROLL
  856. +#elif defined( __osf1__ ) /* Alpha */
  857. + #define DES_PTR
  858. + #define DES_RISC2
  859. +#elif defined ( _AIX ) /* RS6000 */
  860. + /* Unknown */
  861. +#elif defined( __hpux ) /* HP-PA */
  862. + /* Unknown */
  863. +#elif defined( __aux ) /* 68K */
  864. + /* Unknown */
  865. +#elif defined( __dgux ) /* 88K (but P6 in latest boxes) */
  866. + #define DES_UNROLL
  867. +#elif defined( __sgi ) /* Newer MIPS */
  868. + #define DES_PTR
  869. + #define DES_RISC2
  870. + #define DES_UNROLL
  871. +#elif defined( i386 ) /* x86 boxes, should be gcc */
  872. + #define DES_PTR
  873. + #define DES_RISC1
  874. + #define DES_UNROLL
  875. +#endif /* Systems-specific speed defines */
  876. +#endif
  877. +
  878. +#endif /* DES_DEFAULT_OPTIONS */
  879. +
  880. +#ifdef MSDOS /* Visual C++ 2.1 (Windows NT/95) */
  881. +#include <stdlib.h>
  882. +#include <errno.h>
  883. +#include <time.h>
  884. +#include <io.h>
  885. +#ifndef RAND
  886. +#define RAND
  887. +#endif
  888. +#undef NOPROTO
  889. +#endif
  890. +
  891. +#if defined(__STDC__) || defined(VMS) || defined(M_XENIX) || defined(MSDOS)
  892. +#ifndef __KERNEL__
  893. +#include <string.h>
  894. +#else
  895. +#include <linux/string.h>
  896. +#endif
  897. +#endif
  898. +
  899. +#ifndef RAND
  900. +#define RAND
  901. +#endif
  902. +
  903. +#ifdef linux
  904. +#undef RAND
  905. +#endif
  906. +
  907. +#ifdef MSDOS
  908. +#define getpid() 2
  909. +#define RAND
  910. +#undef NOPROTO
  911. +#endif
  912. +
  913. +#if defined(NOCONST)
  914. +#define const
  915. +#endif
  916. +
  917. +#ifdef __STDC__
  918. +#undef NOPROTO
  919. +#endif
  920. +
  921. +#ifdef RAND
  922. +#define srandom(s) srand(s)
  923. +#define random rand
  924. +#endif
  925. +
  926. +#define ITERATIONS 16
  927. +#define HALF_ITERATIONS 8
  928. +
  929. +/* used in des_read and des_write */
  930. +#define MAXWRITE (1024*16)
  931. +#define BSIZE (MAXWRITE+4)
  932. +
  933. +#define c2l(c,l) (l =((DES_LONG)(*((c)++))) , \
  934. + l|=((DES_LONG)(*((c)++)))<< 8L, \
  935. + l|=((DES_LONG)(*((c)++)))<<16L, \
  936. + l|=((DES_LONG)(*((c)++)))<<24L)
  937. +
  938. +/* NOTE - c is not incremented as per c2l */
  939. +#define c2ln(c,l1,l2,n) { \
  940. + c+=n; \
  941. + l1=l2=0; \
  942. + switch (n) { \
  943. + case 8: l2 =((DES_LONG)(*(--(c))))<<24L; \
  944. + case 7: l2|=((DES_LONG)(*(--(c))))<<16L; \
  945. + case 6: l2|=((DES_LONG)(*(--(c))))<< 8L; \
  946. + case 5: l2|=((DES_LONG)(*(--(c)))); \
  947. + case 4: l1 =((DES_LONG)(*(--(c))))<<24L; \
  948. + case 3: l1|=((DES_LONG)(*(--(c))))<<16L; \
  949. + case 2: l1|=((DES_LONG)(*(--(c))))<< 8L; \
  950. + case 1: l1|=((DES_LONG)(*(--(c)))); \
  951. + } \
  952. + }
  953. +
  954. +#define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
  955. + *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
  956. + *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
  957. + *((c)++)=(unsigned char)(((l)>>24L)&0xff))
  958. +
  959. +/* replacements for htonl and ntohl since I have no idea what to do
  960. + * when faced with machines with 8 byte longs. */
  961. +#define HDRSIZE 4
  962. +
  963. +#define n2l(c,l) (l =((DES_LONG)(*((c)++)))<<24L, \
  964. + l|=((DES_LONG)(*((c)++)))<<16L, \
  965. + l|=((DES_LONG)(*((c)++)))<< 8L, \
  966. + l|=((DES_LONG)(*((c)++))))
  967. +
  968. +#define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
  969. + *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
  970. + *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
  971. + *((c)++)=(unsigned char)(((l) )&0xff))
  972. +
  973. +/* NOTE - c is not incremented as per l2c */
  974. +#define l2cn(l1,l2,c,n) { \
  975. + c+=n; \
  976. + switch (n) { \
  977. + case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
  978. + case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
  979. + case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
  980. + case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \
  981. + case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
  982. + case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
  983. + case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
  984. + case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \
  985. + } \
  986. + }
  987. +
  988. +#if defined(WIN32)
  989. +#define ROTATE(a,n) (_lrotr(a,n))
  990. +#else
  991. +#define ROTATE(a,n) (((a)>>(n))+((a)<<(32-(n))))
  992. +#endif
  993. +
  994. +/* Don't worry about the LOAD_DATA() stuff, that is used by
  995. + * fcrypt() to add it's little bit to the front */
  996. +
  997. +#ifdef DES_FCRYPT
  998. +
  999. +#define LOAD_DATA_tmp(R,S,u,t,E0,E1) \
  1000. + { DES_LONG tmp; LOAD_DATA(R,S,u,t,E0,E1,tmp); }
  1001. +
  1002. +#define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
  1003. + t=R^(R>>16L); \
  1004. + u=t&E0; t&=E1; \
  1005. + tmp=(u<<16); u^=R^s[S ]; u^=tmp; \
  1006. + tmp=(t<<16); t^=R^s[S+1]; t^=tmp
  1007. +#else
  1008. +#define LOAD_DATA_tmp(a,b,c,d,e,f) LOAD_DATA(a,b,c,d,e,f,g)
  1009. +#define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
  1010. + u=R^s[S ]; \
  1011. + t=R^s[S+1]
  1012. +#endif
  1013. +
  1014. +/* The changes to this macro may help or hinder, depending on the
  1015. + * compiler and the achitecture. gcc2 always seems to do well :-).
  1016. + * Inspired by Dana How <how@isl.stanford.edu>
  1017. + * DO NOT use the alternative version on machines with 8 byte longs.
  1018. + * It does not seem to work on the Alpha, even when DES_LONG is 4
  1019. + * bytes, probably an issue of accessing non-word aligned objects :-( */
  1020. +#ifdef DES_PTR
  1021. +
  1022. +/* It recently occured to me that 0^0^0^0^0^0^0 == 0, so there
  1023. + * is no reason to not xor all the sub items together. This potentially
  1024. + * saves a register since things can be xored directly into L */
  1025. +
  1026. +#if defined(DES_RISC1) || defined(DES_RISC2)
  1027. +#ifdef DES_RISC1
  1028. +#define D_ENCRYPT(LL,R,S) { \
  1029. + unsigned int u1,u2,u3; \
  1030. + LOAD_DATA(R,S,u,t,E0,E1,u1); \
  1031. + u2=(int)u>>8L; \
  1032. + u1=(int)u&0xfc; \
  1033. + u2&=0xfc; \
  1034. + t=ROTATE(t,4); \
  1035. + u>>=16L; \
  1036. + LL^= *(DES_LONG *)((unsigned char *)des_SP +u1); \
  1037. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x200+u2); \
  1038. + u3=(int)(u>>8L); \
  1039. + u1=(int)u&0xfc; \
  1040. + u3&=0xfc; \
  1041. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x400+u1); \
  1042. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x600+u3); \
  1043. + u2=(int)t>>8L; \
  1044. + u1=(int)t&0xfc; \
  1045. + u2&=0xfc; \
  1046. + t>>=16L; \
  1047. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x100+u1); \
  1048. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x300+u2); \
  1049. + u3=(int)t>>8L; \
  1050. + u1=(int)t&0xfc; \
  1051. + u3&=0xfc; \
  1052. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x500+u1); \
  1053. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x700+u3); }
  1054. +#endif
  1055. +#ifdef DES_RISC2
  1056. +#define D_ENCRYPT(LL,R,S) { \
  1057. + unsigned int u1,u2,s1,s2; \
  1058. + LOAD_DATA(R,S,u,t,E0,E1,u1); \
  1059. + u2=(int)u>>8L; \
  1060. + u1=(int)u&0xfc; \
  1061. + u2&=0xfc; \
  1062. + t=ROTATE(t,4); \
  1063. + LL^= *(DES_LONG *)((unsigned char *)des_SP +u1); \
  1064. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x200+u2); \
  1065. + s1=(int)(u>>16L); \
  1066. + s2=(int)(u>>24L); \
  1067. + s1&=0xfc; \
  1068. + s2&=0xfc; \
  1069. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x400+s1); \
  1070. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x600+s2); \
  1071. + u2=(int)t>>8L; \
  1072. + u1=(int)t&0xfc; \
  1073. + u2&=0xfc; \
  1074. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x100+u1); \
  1075. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x300+u2); \
  1076. + s1=(int)(t>>16L); \
  1077. + s2=(int)(t>>24L); \
  1078. + s1&=0xfc; \
  1079. + s2&=0xfc; \
  1080. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x500+s1); \
  1081. + LL^= *(DES_LONG *)((unsigned char *)des_SP+0x700+s2); }
  1082. +#endif
  1083. +#else
  1084. +#define D_ENCRYPT(LL,R,S) { \
  1085. + LOAD_DATA_tmp(R,S,u,t,E0,E1); \
  1086. + t=ROTATE(t,4); \
  1087. + LL^= \
  1088. + *(DES_LONG *)((unsigned char *)des_SP +((u )&0xfc))^ \
  1089. + *(DES_LONG *)((unsigned char *)des_SP+0x200+((u>> 8L)&0xfc))^ \
  1090. + *(DES_LONG *)((unsigned char *)des_SP+0x400+((u>>16L)&0xfc))^ \
  1091. + *(DES_LONG *)((unsigned char *)des_SP+0x600+((u>>24L)&0xfc))^ \
  1092. + *(DES_LONG *)((unsigned char *)des_SP+0x100+((t )&0xfc))^ \
  1093. + *(DES_LONG *)((unsigned char *)des_SP+0x300+((t>> 8L)&0xfc))^ \
  1094. + *(DES_LONG *)((unsigned char *)des_SP+0x500+((t>>16L)&0xfc))^ \
  1095. + *(DES_LONG *)((unsigned char *)des_SP+0x700+((t>>24L)&0xfc)); }
  1096. +#endif
  1097. +
  1098. +#else /* original version */
  1099. +
  1100. +#if defined(DES_RISC1) || defined(DES_RISC2)
  1101. +#ifdef DES_RISC1
  1102. +#define D_ENCRYPT(LL,R,S) {\
  1103. + unsigned int u1,u2,u3; \
  1104. + LOAD_DATA(R,S,u,t,E0,E1,u1); \
  1105. + u>>=2L; \
  1106. + t=ROTATE(t,6); \
  1107. + u2=(int)u>>8L; \
  1108. + u1=(int)u&0x3f; \
  1109. + u2&=0x3f; \
  1110. + u>>=16L; \
  1111. + LL^=des_SPtrans[0][u1]; \
  1112. + LL^=des_SPtrans[2][u2]; \
  1113. + u3=(int)u>>8L; \
  1114. + u1=(int)u&0x3f; \
  1115. + u3&=0x3f; \
  1116. + LL^=des_SPtrans[4][u1]; \
  1117. + LL^=des_SPtrans[6][u3]; \
  1118. + u2=(int)t>>8L; \
  1119. + u1=(int)t&0x3f; \
  1120. + u2&=0x3f; \
  1121. + t>>=16L; \
  1122. + LL^=des_SPtrans[1][u1]; \
  1123. + LL^=des_SPtrans[3][u2]; \
  1124. + u3=(int)t>>8L; \
  1125. + u1=(int)t&0x3f; \
  1126. + u3&=0x3f; \
  1127. + LL^=des_SPtrans[5][u1]; \
  1128. + LL^=des_SPtrans[7][u3]; }
  1129. +#endif
  1130. +#ifdef DES_RISC2
  1131. +#define D_ENCRYPT(LL,R,S) {\
  1132. + unsigned int u1,u2,s1,s2; \
  1133. + LOAD_DATA(R,S,u,t,E0,E1,u1); \
  1134. + u>>=2L; \
  1135. + t=ROTATE(t,6); \
  1136. + u2=(int)u>>8L; \
  1137. + u1=(int)u&0x3f; \
  1138. + u2&=0x3f; \
  1139. + LL^=des_SPtrans[0][u1]; \
  1140. + LL^=des_SPtrans[2][u2]; \
  1141. + s1=(int)u>>16L; \
  1142. + s2=(int)u>>24L; \
  1143. + s1&=0x3f; \
  1144. + s2&=0x3f; \
  1145. + LL^=des_SPtrans[4][s1]; \
  1146. + LL^=des_SPtrans[6][s2]; \
  1147. + u2=(int)t>>8L; \
  1148. + u1=(int)t&0x3f; \
  1149. + u2&=0x3f; \
  1150. + LL^=des_SPtrans[1][u1]; \
  1151. + LL^=des_SPtrans[3][u2]; \
  1152. + s1=(int)t>>16; \
  1153. + s2=(int)t>>24L; \
  1154. + s1&=0x3f; \
  1155. + s2&=0x3f; \
  1156. + LL^=des_SPtrans[5][s1]; \
  1157. + LL^=des_SPtrans[7][s2]; }
  1158. +#endif
  1159. +
  1160. +#else
  1161. +
  1162. +#define D_ENCRYPT(LL,R,S) {\
  1163. + LOAD_DATA_tmp(R,S,u,t,E0,E1); \
  1164. + t=ROTATE(t,4); \
  1165. + LL^=\
  1166. + des_SPtrans[0][(u>> 2L)&0x3f]^ \
  1167. + des_SPtrans[2][(u>>10L)&0x3f]^ \
  1168. + des_SPtrans[4][(u>>18L)&0x3f]^ \
  1169. + des_SPtrans[6][(u>>26L)&0x3f]^ \
  1170. + des_SPtrans[1][(t>> 2L)&0x3f]^ \
  1171. + des_SPtrans[3][(t>>10L)&0x3f]^ \
  1172. + des_SPtrans[5][(t>>18L)&0x3f]^ \
  1173. + des_SPtrans[7][(t>>26L)&0x3f]; }
  1174. +#endif
  1175. +#endif
  1176. +
  1177. + /* IP and FP
  1178. + * The problem is more of a geometric problem that random bit fiddling.
  1179. + 0 1 2 3 4 5 6 7 62 54 46 38 30 22 14 6
  1180. + 8 9 10 11 12 13 14 15 60 52 44 36 28 20 12 4
  1181. + 16 17 18 19 20 21 22 23 58 50 42 34 26 18 10 2
  1182. + 24 25 26 27 28 29 30 31 to 56 48 40 32 24 16 8 0
  1183. +
  1184. + 32 33 34 35 36 37 38 39 63 55 47 39 31 23 15 7
  1185. + 40 41 42 43 44 45 46 47 61 53 45 37 29 21 13 5
  1186. + 48 49 50 51 52 53 54 55 59 51 43 35 27 19 11 3
  1187. + 56 57 58 59 60 61 62 63 57 49 41 33 25 17 9 1
  1188. +
  1189. + The output has been subject to swaps of the form
  1190. + 0 1 -> 3 1 but the odd and even bits have been put into
  1191. + 2 3 2 0
  1192. + different words. The main trick is to remember that
  1193. + t=((l>>size)^r)&(mask);
  1194. + r^=t;
  1195. + l^=(t<<size);
  1196. + can be used to swap and move bits between words.
  1197. +
  1198. + So l = 0 1 2 3 r = 16 17 18 19
  1199. + 4 5 6 7 20 21 22 23
  1200. + 8 9 10 11 24 25 26 27
  1201. + 12 13 14 15 28 29 30 31
  1202. + becomes (for size == 2 and mask == 0x3333)
  1203. + t = 2^16 3^17 -- -- l = 0 1 16 17 r = 2 3 18 19
  1204. + 6^20 7^21 -- -- 4 5 20 21 6 7 22 23
  1205. + 10^24 11^25 -- -- 8 9 24 25 10 11 24 25
  1206. + 14^28 15^29 -- -- 12 13 28 29 14 15 28 29
  1207. +
  1208. + Thanks for hints from Richard Outerbridge - he told me IP&FP
  1209. + could be done in 15 xor, 10 shifts and 5 ands.
  1210. + When I finally started to think of the problem in 2D
  1211. + I first got ~42 operations without xors. When I remembered
  1212. + how to use xors :-) I got it to its final state.
  1213. + */
  1214. +#define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
  1215. + (b)^=(t),\
  1216. + (a)^=((t)<<(n)))
  1217. +
  1218. +#define IP(l,r) \
  1219. + { \
  1220. + register DES_LONG tt; \
  1221. + PERM_OP(r,l,tt, 4,0x0f0f0f0fL); \
  1222. + PERM_OP(l,r,tt,16,0x0000ffffL); \
  1223. + PERM_OP(r,l,tt, 2,0x33333333L); \
  1224. + PERM_OP(l,r,tt, 8,0x00ff00ffL); \
  1225. + PERM_OP(r,l,tt, 1,0x55555555L); \
  1226. + }
  1227. +
  1228. +#define FP(l,r) \
  1229. + { \
  1230. + register DES_LONG tt; \
  1231. + PERM_OP(l,r,tt, 1,0x55555555L); \
  1232. + PERM_OP(r,l,tt, 8,0x00ff00ffL); \
  1233. + PERM_OP(l,r,tt, 2,0x33333333L); \
  1234. + PERM_OP(r,l,tt,16,0x0000ffffL); \
  1235. + PERM_OP(l,r,tt, 4,0x0f0f0f0fL); \
  1236. + }
  1237. +
  1238. +extern const DES_LONG des_SPtrans[8][64];
  1239. +
  1240. +#ifndef NOPROTO
  1241. +void fcrypt_body(DES_LONG *out,des_key_schedule ks,
  1242. + DES_LONG Eswap0, DES_LONG Eswap1);
  1243. +#else
  1244. +void fcrypt_body();
  1245. +#endif
  1246. +
  1247. +#endif
  1248. --- /dev/null Tue Mar 11 13:02:56 2003
  1249. +++ linux/include/des/des_ver.h Mon Feb 9 13:51:03 2004
  1250. @@ -0,0 +1,60 @@
  1251. +/* crypto/des/des_ver.h */
  1252. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  1253. + * All rights reserved.
  1254. + *
  1255. + * This package is an SSL implementation written
  1256. + * by Eric Young (eay@cryptsoft.com).
  1257. + * The implementation was written so as to conform with Netscapes SSL.
  1258. + *
  1259. + * This library is free for commercial and non-commercial use as long as
  1260. + * the following conditions are aheared to. The following conditions
  1261. + * apply to all code found in this distribution, be it the RC4, RSA,
  1262. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  1263. + * included with this distribution is covered by the same copyright terms
  1264. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  1265. + *
  1266. + * Copyright remains Eric Young's, and as such any Copyright notices in
  1267. + * the code are not to be removed.
  1268. + * If this package is used in a product, Eric Young should be given attribution
  1269. + * as the author of the parts of the library used.
  1270. + * This can be in the form of a textual message at program startup or
  1271. + * in documentation (online or textual) provided with the package.
  1272. + *
  1273. + * Redistribution and use in source and binary forms, with or without
  1274. + * modification, are permitted provided that the following conditions
  1275. + * are met:
  1276. + * 1. Redistributions of source code must retain the copyright
  1277. + * notice, this list of conditions and the following disclaimer.
  1278. + * 2. Redistributions in binary form must reproduce the above copyright
  1279. + * notice, this list of conditions and the following disclaimer in the
  1280. + * documentation and/or other materials provided with the distribution.
  1281. + * 3. All advertising materials mentioning features or use of this software
  1282. + * must display the following acknowledgement:
  1283. + * "This product includes cryptographic software written by
  1284. + * Eric Young (eay@cryptsoft.com)"
  1285. + * The word 'cryptographic' can be left out if the rouines from the library
  1286. + * being used are not cryptographic related :-).
  1287. + * 4. If you include any Windows specific code (or a derivative thereof) from
  1288. + * the apps directory (application code) you must include an acknowledgement:
  1289. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  1290. + *
  1291. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  1292. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1293. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1294. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  1295. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1296. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1297. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1298. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1299. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1300. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1301. + * SUCH DAMAGE.
  1302. + *
  1303. + * The licence and distribution terms for any publically available version or
  1304. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  1305. + * copied and put under another distribution licence
  1306. + * [including the GNU Public Licence.]
  1307. + */
  1308. +
  1309. +extern char *DES_version; /* SSLeay version string */
  1310. +extern char *libdes_version; /* old libdes version string */
  1311. --- /dev/null Tue Mar 11 13:02:56 2003
  1312. +++ linux/include/des/podd.h Mon Feb 9 13:51:03 2004
  1313. @@ -0,0 +1,75 @@
  1314. +/* crypto/des/podd.h */
  1315. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  1316. + * All rights reserved.
  1317. + *
  1318. + * This package is an SSL implementation written
  1319. + * by Eric Young (eay@cryptsoft.com).
  1320. + * The implementation was written so as to conform with Netscapes SSL.
  1321. + *
  1322. + * This library is free for commercial and non-commercial use as long as
  1323. + * the following conditions are aheared to. The following conditions
  1324. + * apply to all code found in this distribution, be it the RC4, RSA,
  1325. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  1326. + * included with this distribution is covered by the same copyright terms
  1327. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  1328. + *
  1329. + * Copyright remains Eric Young's, and as such any Copyright notices in
  1330. + * the code are not to be removed.
  1331. + * If this package is used in a product, Eric Young should be given attribution
  1332. + * as the author of the parts of the library used.
  1333. + * This can be in the form of a textual message at program startup or
  1334. + * in documentation (online or textual) provided with the package.
  1335. + *
  1336. + * Redistribution and use in source and binary forms, with or without
  1337. + * modification, are permitted provided that the following conditions
  1338. + * are met:
  1339. + * 1. Redistributions of source code must retain the copyright
  1340. + * notice, this list of conditions and the following disclaimer.
  1341. + * 2. Redistributions in binary form must reproduce the above copyright
  1342. + * notice, this list of conditions and the following disclaimer in the
  1343. + * documentation and/or other materials provided with the distribution.
  1344. + * 3. All advertising materials mentioning features or use of this software
  1345. + * must display the following acknowledgement:
  1346. + * "This product includes cryptographic software written by
  1347. + * Eric Young (eay@cryptsoft.com)"
  1348. + * The word 'cryptographic' can be left out if the rouines from the library
  1349. + * being used are not cryptographic related :-).
  1350. + * 4. If you include any Windows specific code (or a derivative thereof) from
  1351. + * the apps directory (application code) you must include an acknowledgement:
  1352. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  1353. + *
  1354. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  1355. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1356. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1357. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  1358. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1359. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1360. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1361. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1362. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1363. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1364. + * SUCH DAMAGE.
  1365. + *
  1366. + * The licence and distribution terms for any publically available version or
  1367. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  1368. + * copied and put under another distribution licence
  1369. + * [including the GNU Public Licence.]
  1370. + */
  1371. +
  1372. +static const unsigned char odd_parity[256]={
  1373. + 1, 1, 2, 2, 4, 4, 7, 7, 8, 8, 11, 11, 13, 13, 14, 14,
  1374. + 16, 16, 19, 19, 21, 21, 22, 22, 25, 25, 26, 26, 28, 28, 31, 31,
  1375. + 32, 32, 35, 35, 37, 37, 38, 38, 41, 41, 42, 42, 44, 44, 47, 47,
  1376. + 49, 49, 50, 50, 52, 52, 55, 55, 56, 56, 59, 59, 61, 61, 62, 62,
  1377. + 64, 64, 67, 67, 69, 69, 70, 70, 73, 73, 74, 74, 76, 76, 79, 79,
  1378. + 81, 81, 82, 82, 84, 84, 87, 87, 88, 88, 91, 91, 93, 93, 94, 94,
  1379. + 97, 97, 98, 98,100,100,103,103,104,104,107,107,109,109,110,110,
  1380. +112,112,115,115,117,117,118,118,121,121,122,122,124,124,127,127,
  1381. +128,128,131,131,133,133,134,134,137,137,138,138,140,140,143,143,
  1382. +145,145,146,146,148,148,151,151,152,152,155,155,157,157,158,158,
  1383. +161,161,162,162,164,164,167,167,168,168,171,171,173,173,174,174,
  1384. +176,176,179,179,181,181,182,182,185,185,186,186,188,188,191,191,
  1385. +193,193,194,194,196,196,199,199,200,200,203,203,205,205,206,206,
  1386. +208,208,211,211,213,213,214,214,217,217,218,218,220,220,223,223,
  1387. +224,224,227,227,229,229,230,230,233,233,234,234,236,236,239,239,
  1388. +241,241,242,242,244,244,247,247,248,248,251,251,253,253,254,254};
  1389. --- /dev/null Tue Mar 11 13:02:56 2003
  1390. +++ linux/include/des/sk.h Mon Feb 9 13:51:03 2004
  1391. @@ -0,0 +1,204 @@
  1392. +/* crypto/des/sk.h */
  1393. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  1394. + * All rights reserved.
  1395. + *
  1396. + * This package is an SSL implementation written
  1397. + * by Eric Young (eay@cryptsoft.com).
  1398. + * The implementation was written so as to conform with Netscapes SSL.
  1399. + *
  1400. + * This library is free for commercial and non-commercial use as long as
  1401. + * the following conditions are aheared to. The following conditions
  1402. + * apply to all code found in this distribution, be it the RC4, RSA,
  1403. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  1404. + * included with this distribution is covered by the same copyright terms
  1405. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  1406. + *
  1407. + * Copyright remains Eric Young's, and as such any Copyright notices in
  1408. + * the code are not to be removed.
  1409. + * If this package is used in a product, Eric Young should be given attribution
  1410. + * as the author of the parts of the library used.
  1411. + * This can be in the form of a textual message at program startup or
  1412. + * in documentation (online or textual) provided with the package.
  1413. + *
  1414. + * Redistribution and use in source and binary forms, with or without
  1415. + * modification, are permitted provided that the following conditions
  1416. + * are met:
  1417. + * 1. Redistributions of source code must retain the copyright
  1418. + * notice, this list of conditions and the following disclaimer.
  1419. + * 2. Redistributions in binary form must reproduce the above copyright
  1420. + * notice, this list of conditions and the following disclaimer in the
  1421. + * documentation and/or other materials provided with the distribution.
  1422. + * 3. All advertising materials mentioning features or use of this software
  1423. + * must display the following acknowledgement:
  1424. + * "This product includes cryptographic software written by
  1425. + * Eric Young (eay@cryptsoft.com)"
  1426. + * The word 'cryptographic' can be left out if the rouines from the library
  1427. + * being used are not cryptographic related :-).
  1428. + * 4. If you include any Windows specific code (or a derivative thereof) from
  1429. + * the apps directory (application code) you must include an acknowledgement:
  1430. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  1431. + *
  1432. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  1433. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1434. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1435. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  1436. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1437. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1438. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1439. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1440. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1441. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1442. + * SUCH DAMAGE.
  1443. + *
  1444. + * The licence and distribution terms for any publically available version or
  1445. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  1446. + * copied and put under another distribution licence
  1447. + * [including the GNU Public Licence.]
  1448. + */
  1449. +
  1450. +static const DES_LONG des_skb[8][64]={
  1451. +{
  1452. +/* for C bits (numbered as per FIPS 46) 1 2 3 4 5 6 */
  1453. +0x00000000L,0x00000010L,0x20000000L,0x20000010L,
  1454. +0x00010000L,0x00010010L,0x20010000L,0x20010010L,
  1455. +0x00000800L,0x00000810L,0x20000800L,0x20000810L,
  1456. +0x00010800L,0x00010810L,0x20010800L,0x20010810L,
  1457. +0x00000020L,0x00000030L,0x20000020L,0x20000030L,
  1458. +0x00010020L,0x00010030L,0x20010020L,0x20010030L,
  1459. +0x00000820L,0x00000830L,0x20000820L,0x20000830L,
  1460. +0x00010820L,0x00010830L,0x20010820L,0x20010830L,
  1461. +0x00080000L,0x00080010L,0x20080000L,0x20080010L,
  1462. +0x00090000L,0x00090010L,0x20090000L,0x20090010L,
  1463. +0x00080800L,0x00080810L,0x20080800L,0x20080810L,
  1464. +0x00090800L,0x00090810L,0x20090800L,0x20090810L,
  1465. +0x00080020L,0x00080030L,0x20080020L,0x20080030L,
  1466. +0x00090020L,0x00090030L,0x20090020L,0x20090030L,
  1467. +0x00080820L,0x00080830L,0x20080820L,0x20080830L,
  1468. +0x00090820L,0x00090830L,0x20090820L,0x20090830L,
  1469. +},{
  1470. +/* for C bits (numbered as per FIPS 46) 7 8 10 11 12 13 */
  1471. +0x00000000L,0x02000000L,0x00002000L,0x02002000L,
  1472. +0x00200000L,0x02200000L,0x00202000L,0x02202000L,
  1473. +0x00000004L,0x02000004L,0x00002004L,0x02002004L,
  1474. +0x00200004L,0x02200004L,0x00202004L,0x02202004L,
  1475. +0x00000400L,0x02000400L,0x00002400L,0x02002400L,
  1476. +0x00200400L,0x02200400L,0x00202400L,0x02202400L,
  1477. +0x00000404L,0x02000404L,0x00002404L,0x02002404L,
  1478. +0x00200404L,0x02200404L,0x00202404L,0x02202404L,
  1479. +0x10000000L,0x12000000L,0x10002000L,0x12002000L,
  1480. +0x10200000L,0x12200000L,0x10202000L,0x12202000L,
  1481. +0x10000004L,0x12000004L,0x10002004L,0x12002004L,
  1482. +0x10200004L,0x12200004L,0x10202004L,0x12202004L,
  1483. +0x10000400L,0x12000400L,0x10002400L,0x12002400L,
  1484. +0x10200400L,0x12200400L,0x10202400L,0x12202400L,
  1485. +0x10000404L,0x12000404L,0x10002404L,0x12002404L,
  1486. +0x10200404L,0x12200404L,0x10202404L,0x12202404L,
  1487. +},{
  1488. +/* for C bits (numbered as per FIPS 46) 14 15 16 17 19 20 */
  1489. +0x00000000L,0x00000001L,0x00040000L,0x00040001L,
  1490. +0x01000000L,0x01000001L,0x01040000L,0x01040001L,
  1491. +0x00000002L,0x00000003L,0x00040002L,0x00040003L,
  1492. +0x01000002L,0x01000003L,0x01040002L,0x01040003L,
  1493. +0x00000200L,0x00000201L,0x00040200L,0x00040201L,
  1494. +0x01000200L,0x01000201L,0x01040200L,0x01040201L,
  1495. +0x00000202L,0x00000203L,0x00040202L,0x00040203L,
  1496. +0x01000202L,0x01000203L,0x01040202L,0x01040203L,
  1497. +0x08000000L,0x08000001L,0x08040000L,0x08040001L,
  1498. +0x09000000L,0x09000001L,0x09040000L,0x09040001L,
  1499. +0x08000002L,0x08000003L,0x08040002L,0x08040003L,
  1500. +0x09000002L,0x09000003L,0x09040002L,0x09040003L,
  1501. +0x08000200L,0x08000201L,0x08040200L,0x08040201L,
  1502. +0x09000200L,0x09000201L,0x09040200L,0x09040201L,
  1503. +0x08000202L,0x08000203L,0x08040202L,0x08040203L,
  1504. +0x09000202L,0x09000203L,0x09040202L,0x09040203L,
  1505. +},{
  1506. +/* for C bits (numbered as per FIPS 46) 21 23 24 26 27 28 */
  1507. +0x00000000L,0x00100000L,0x00000100L,0x00100100L,
  1508. +0x00000008L,0x00100008L,0x00000108L,0x00100108L,
  1509. +0x00001000L,0x00101000L,0x00001100L,0x00101100L,
  1510. +0x00001008L,0x00101008L,0x00001108L,0x00101108L,
  1511. +0x04000000L,0x04100000L,0x04000100L,0x04100100L,
  1512. +0x04000008L,0x04100008L,0x04000108L,0x04100108L,
  1513. +0x04001000L,0x04101000L,0x04001100L,0x04101100L,
  1514. +0x04001008L,0x04101008L,0x04001108L,0x04101108L,
  1515. +0x00020000L,0x00120000L,0x00020100L,0x00120100L,
  1516. +0x00020008L,0x00120008L,0x00020108L,0x00120108L,
  1517. +0x00021000L,0x00121000L,0x00021100L,0x00121100L,
  1518. +0x00021008L,0x00121008L,0x00021108L,0x00121108L,
  1519. +0x04020000L,0x04120000L,0x04020100L,0x04120100L,
  1520. +0x04020008L,0x04120008L,0x04020108L,0x04120108L,
  1521. +0x04021000L,0x04121000L,0x04021100L,0x04121100L,
  1522. +0x04021008L,0x04121008L,0x04021108L,0x04121108L,
  1523. +},{
  1524. +/* for D bits (numbered as per FIPS 46) 1 2 3 4 5 6 */
  1525. +0x00000000L,0x10000000L,0x00010000L,0x10010000L,
  1526. +0x00000004L,0x10000004L,0x00010004L,0x10010004L,
  1527. +0x20000000L,0x30000000L,0x20010000L,0x30010000L,
  1528. +0x20000004L,0x30000004L,0x20010004L,0x30010004L,
  1529. +0x00100000L,0x10100000L,0x00110000L,0x10110000L,
  1530. +0x00100004L,0x10100004L,0x00110004L,0x10110004L,
  1531. +0x20100000L,0x30100000L,0x20110000L,0x30110000L,
  1532. +0x20100004L,0x30100004L,0x20110004L,0x30110004L,
  1533. +0x00001000L,0x10001000L,0x00011000L,0x10011000L,
  1534. +0x00001004L,0x10001004L,0x00011004L,0x10011004L,
  1535. +0x20001000L,0x30001000L,0x20011000L,0x30011000L,
  1536. +0x20001004L,0x30001004L,0x20011004L,0x30011004L,
  1537. +0x00101000L,0x10101000L,0x00111000L,0x10111000L,
  1538. +0x00101004L,0x10101004L,0x00111004L,0x10111004L,
  1539. +0x20101000L,0x30101000L,0x20111000L,0x30111000L,
  1540. +0x20101004L,0x30101004L,0x20111004L,0x30111004L,
  1541. +},{
  1542. +/* for D bits (numbered as per FIPS 46) 8 9 11 12 13 14 */
  1543. +0x00000000L,0x08000000L,0x00000008L,0x08000008L,
  1544. +0x00000400L,0x08000400L,0x00000408L,0x08000408L,
  1545. +0x00020000L,0x08020000L,0x00020008L,0x08020008L,
  1546. +0x00020400L,0x08020400L,0x00020408L,0x08020408L,
  1547. +0x00000001L,0x08000001L,0x00000009L,0x08000009L,
  1548. +0x00000401L,0x08000401L,0x00000409L,0x08000409L,
  1549. +0x00020001L,0x08020001L,0x00020009L,0x08020009L,
  1550. +0x00020401L,0x08020401L,0x00020409L,0x08020409L,
  1551. +0x02000000L,0x0A000000L,0x02000008L,0x0A000008L,
  1552. +0x02000400L,0x0A000400L,0x02000408L,0x0A000408L,
  1553. +0x02020000L,0x0A020000L,0x02020008L,0x0A020008L,
  1554. +0x02020400L,0x0A020400L,0x02020408L,0x0A020408L,
  1555. +0x02000001L,0x0A000001L,0x02000009L,0x0A000009L,
  1556. +0x02000401L,0x0A000401L,0x02000409L,0x0A000409L,
  1557. +0x02020001L,0x0A020001L,0x02020009L,0x0A020009L,
  1558. +0x02020401L,0x0A020401L,0x02020409L,0x0A020409L,
  1559. +},{
  1560. +/* for D bits (numbered as per FIPS 46) 16 17 18 19 20 21 */
  1561. +0x00000000L,0x00000100L,0x00080000L,0x00080100L,
  1562. +0x01000000L,0x01000100L,0x01080000L,0x01080100L,
  1563. +0x00000010L,0x00000110L,0x00080010L,0x00080110L,
  1564. +0x01000010L,0x01000110L,0x01080010L,0x01080110L,
  1565. +0x00200000L,0x00200100L,0x00280000L,0x00280100L,
  1566. +0x01200000L,0x01200100L,0x01280000L,0x01280100L,
  1567. +0x00200010L,0x00200110L,0x00280010L,0x00280110L,
  1568. +0x01200010L,0x01200110L,0x01280010L,0x01280110L,
  1569. +0x00000200L,0x00000300L,0x00080200L,0x00080300L,
  1570. +0x01000200L,0x01000300L,0x01080200L,0x01080300L,
  1571. +0x00000210L,0x00000310L,0x00080210L,0x00080310L,
  1572. +0x01000210L,0x01000310L,0x01080210L,0x01080310L,
  1573. +0x00200200L,0x00200300L,0x00280200L,0x00280300L,
  1574. +0x01200200L,0x01200300L,0x01280200L,0x01280300L,
  1575. +0x00200210L,0x00200310L,0x00280210L,0x00280310L,
  1576. +0x01200210L,0x01200310L,0x01280210L,0x01280310L,
  1577. +},{
  1578. +/* for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 */
  1579. +0x00000000L,0x04000000L,0x00040000L,0x04040000L,
  1580. +0x00000002L,0x04000002L,0x00040002L,0x04040002L,
  1581. +0x00002000L,0x04002000L,0x00042000L,0x04042000L,
  1582. +0x00002002L,0x04002002L,0x00042002L,0x04042002L,
  1583. +0x00000020L,0x04000020L,0x00040020L,0x04040020L,
  1584. +0x00000022L,0x04000022L,0x00040022L,0x04040022L,
  1585. +0x00002020L,0x04002020L,0x00042020L,0x04042020L,
  1586. +0x00002022L,0x04002022L,0x00042022L,0x04042022L,
  1587. +0x00000800L,0x04000800L,0x00040800L,0x04040800L,
  1588. +0x00000802L,0x04000802L,0x00040802L,0x04040802L,
  1589. +0x00002800L,0x04002800L,0x00042800L,0x04042800L,
  1590. +0x00002802L,0x04002802L,0x00042802L,0x04042802L,
  1591. +0x00000820L,0x04000820L,0x00040820L,0x04040820L,
  1592. +0x00000822L,0x04000822L,0x00040822L,0x04040822L,
  1593. +0x00002820L,0x04002820L,0x00042820L,0x04042820L,
  1594. +0x00002822L,0x04002822L,0x00042822L,0x04042822L,
  1595. +}};
  1596. --- /dev/null Tue Mar 11 13:02:56 2003
  1597. +++ linux/include/des/spr.h Mon Feb 9 13:51:03 2004
  1598. @@ -0,0 +1,204 @@
  1599. +/* crypto/des/spr.h */
  1600. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  1601. + * All rights reserved.
  1602. + *
  1603. + * This package is an SSL implementation written
  1604. + * by Eric Young (eay@cryptsoft.com).
  1605. + * The implementation was written so as to conform with Netscapes SSL.
  1606. + *
  1607. + * This library is free for commercial and non-commercial use as long as
  1608. + * the following conditions are aheared to. The following conditions
  1609. + * apply to all code found in this distribution, be it the RC4, RSA,
  1610. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  1611. + * included with this distribution is covered by the same copyright terms
  1612. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  1613. + *
  1614. + * Copyright remains Eric Young's, and as such any Copyright notices in
  1615. + * the code are not to be removed.
  1616. + * If this package is used in a product, Eric Young should be given attribution
  1617. + * as the author of the parts of the library used.
  1618. + * This can be in the form of a textual message at program startup or
  1619. + * in documentation (online or textual) provided with the package.
  1620. + *
  1621. + * Redistribution and use in source and binary forms, with or without
  1622. + * modification, are permitted provided that the following conditions
  1623. + * are met:
  1624. + * 1. Redistributions of source code must retain the copyright
  1625. + * notice, this list of conditions and the following disclaimer.
  1626. + * 2. Redistributions in binary form must reproduce the above copyright
  1627. + * notice, this list of conditions and the following disclaimer in the
  1628. + * documentation and/or other materials provided with the distribution.
  1629. + * 3. All advertising materials mentioning features or use of this software
  1630. + * must display the following acknowledgement:
  1631. + * "This product includes cryptographic software written by
  1632. + * Eric Young (eay@cryptsoft.com)"
  1633. + * The word 'cryptographic' can be left out if the rouines from the library
  1634. + * being used are not cryptographic related :-).
  1635. + * 4. If you include any Windows specific code (or a derivative thereof) from
  1636. + * the apps directory (application code) you must include an acknowledgement:
  1637. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  1638. + *
  1639. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  1640. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1641. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1642. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  1643. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1644. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1645. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1646. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1647. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1648. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1649. + * SUCH DAMAGE.
  1650. + *
  1651. + * The licence and distribution terms for any publically available version or
  1652. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  1653. + * copied and put under another distribution licence
  1654. + * [including the GNU Public Licence.]
  1655. + */
  1656. +
  1657. +const DES_LONG des_SPtrans[8][64]={
  1658. +{
  1659. +/* nibble 0 */
  1660. +0x02080800L, 0x00080000L, 0x02000002L, 0x02080802L,
  1661. +0x02000000L, 0x00080802L, 0x00080002L, 0x02000002L,
  1662. +0x00080802L, 0x02080800L, 0x02080000L, 0x00000802L,
  1663. +0x02000802L, 0x02000000L, 0x00000000L, 0x00080002L,
  1664. +0x00080000L, 0x00000002L, 0x02000800L, 0x00080800L,
  1665. +0x02080802L, 0x02080000L, 0x00000802L, 0x02000800L,
  1666. +0x00000002L, 0x00000800L, 0x00080800L, 0x02080002L,
  1667. +0x00000800L, 0x02000802L, 0x02080002L, 0x00000000L,
  1668. +0x00000000L, 0x02080802L, 0x02000800L, 0x00080002L,
  1669. +0x02080800L, 0x00080000L, 0x00000802L, 0x02000800L,
  1670. +0x02080002L, 0x00000800L, 0x00080800L, 0x02000002L,
  1671. +0x00080802L, 0x00000002L, 0x02000002L, 0x02080000L,
  1672. +0x02080802L, 0x00080800L, 0x02080000L, 0x02000802L,
  1673. +0x02000000L, 0x00000802L, 0x00080002L, 0x00000000L,
  1674. +0x00080000L, 0x02000000L, 0x02000802L, 0x02080800L,
  1675. +0x00000002L, 0x02080002L, 0x00000800L, 0x00080802L,
  1676. +},{
  1677. +/* nibble 1 */
  1678. +0x40108010L, 0x00000000L, 0x00108000L, 0x40100000L,
  1679. +0x40000010L, 0x00008010L, 0x40008000L, 0x00108000L,
  1680. +0x00008000L, 0x40100010L, 0x00000010L, 0x40008000L,
  1681. +0x00100010L, 0x40108000L, 0x40100000L, 0x00000010L,
  1682. +0x00100000L, 0x40008010L, 0x40100010L, 0x00008000L,
  1683. +0x00108010L, 0x40000000L, 0x00000000L, 0x00100010L,
  1684. +0x40008010L, 0x00108010L, 0x40108000L, 0x40000010L,
  1685. +0x40000000L, 0x00100000L, 0x00008010L, 0x40108010L,
  1686. +0x00100010L, 0x40108000L, 0x40008000L, 0x00108010L,
  1687. +0x40108010L, 0x00100010L, 0x40000010L, 0x00000000L,
  1688. +0x40000000L, 0x00008010L, 0x00100000L, 0x40100010L,
  1689. +0x00008000L, 0x40000000L, 0x00108010L, 0x40008010L,
  1690. +0x40108000L, 0x00008000L, 0x00000000L, 0x40000010L,
  1691. +0x00000010L, 0x40108010L, 0x00108000L, 0x40100000L,
  1692. +0x40100010L, 0x00100000L, 0x00008010L, 0x40008000L,
  1693. +0x40008010L, 0x00000010L, 0x40100000L, 0x00108000L,
  1694. +},{
  1695. +/* nibble 2 */
  1696. +0x04000001L, 0x04040100L, 0x00000100L, 0x04000101L,
  1697. +0x00040001L, 0x04000000L, 0x04000101L, 0x00040100L,
  1698. +0x04000100L, 0x00040000L, 0x04040000L, 0x00000001L,
  1699. +0x04040101L, 0x00000101L, 0x00000001L, 0x04040001L,
  1700. +0x00000000L, 0x00040001L, 0x04040100L, 0x00000100L,
  1701. +0x00000101L, 0x04040101L, 0x00040000L, 0x04000001L,
  1702. +0x04040001L, 0x04000100L, 0x00040101L, 0x04040000L,
  1703. +0x00040100L, 0x00000000L, 0x04000000L, 0x00040101L,
  1704. +0x04040100L, 0x00000100L, 0x00000001L, 0x00040000L,
  1705. +0x00000101L, 0x00040001L, 0x04040000L, 0x04000101L,
  1706. +0x00000000L, 0x04040100L, 0x00040100L, 0x04040001L,
  1707. +0x00040001L, 0x04000000L, 0x04040101L, 0x00000001L,
  1708. +0x00040101L, 0x04000001L, 0x04000000L, 0x04040101L,
  1709. +0x00040000L, 0x04000100L, 0x04000101L, 0x00040100L,
  1710. +0x04000100L, 0x00000000L, 0x04040001L, 0x00000101L,
  1711. +0x04000001L, 0x00040101L, 0x00000100L, 0x04040000L,
  1712. +},{
  1713. +/* nibble 3 */
  1714. +0x00401008L, 0x10001000L, 0x00000008L, 0x10401008L,
  1715. +0x00000000L, 0x10400000L, 0x10001008L, 0x00400008L,
  1716. +0x10401000L, 0x10000008L, 0x10000000L, 0x00001008L,
  1717. +0x10000008L, 0x00401008L, 0x00400000L, 0x10000000L,
  1718. +0x10400008L, 0x00401000L, 0x00001000L, 0x00000008L,
  1719. +0x00401000L, 0x10001008L, 0x10400000L, 0x00001000L,
  1720. +0x00001008L, 0x00000000L, 0x00400008L, 0x10401000L,
  1721. +0x10001000L, 0x10400008L, 0x10401008L, 0x00400000L,
  1722. +0x10400008L, 0x00001008L, 0x00400000L, 0x10000008L,
  1723. +0x00401000L, 0x10001000L, 0x00000008L, 0x10400000L,
  1724. +0x10001008L, 0x00000000L, 0x00001000L, 0x00400008L,
  1725. +0x00000000L, 0x10400008L, 0x10401000L, 0x00001000L,
  1726. +0x10000000L, 0x10401008L, 0x00401008L, 0x00400000L,
  1727. +0x10401008L, 0x00000008L, 0x10001000L, 0x00401008L,
  1728. +0x00400008L, 0x00401000L, 0x10400000L, 0x10001008L,
  1729. +0x00001008L, 0x10000000L, 0x10000008L, 0x10401000L,
  1730. +},{
  1731. +/* nibble 4 */
  1732. +0x08000000L, 0x00010000L, 0x00000400L, 0x08010420L,
  1733. +0x08010020L, 0x08000400L, 0x00010420L, 0x08010000L,
  1734. +0x00010000L, 0x00000020L, 0x08000020L, 0x00010400L,
  1735. +0x08000420L, 0x08010020L, 0x08010400L, 0x00000000L,
  1736. +0x00010400L, 0x08000000L, 0x00010020L, 0x00000420L,
  1737. +0x08000400L, 0x00010420L, 0x00000000L, 0x08000020L,
  1738. +0x00000020L, 0x08000420L, 0x08010420L, 0x00010020L,
  1739. +0x08010000L, 0x00000400L, 0x00000420L, 0x08010400L,
  1740. +0x08010400L, 0x08000420L, 0x00010020L, 0x08010000L,
  1741. +0x00010000L, 0x00000020L, 0x08000020L, 0x08000400L,
  1742. +0x08000000L, 0x00010400L, 0x08010420L, 0x00000000L,
  1743. +0x00010420L, 0x08000000L, 0x00000400L, 0x00010020L,
  1744. +0x08000420L, 0x00000400L, 0x00000000L, 0x08010420L,
  1745. +0x08010020L, 0x08010400L, 0x00000420L, 0x00010000L,
  1746. +0x00010400L, 0x08010020L, 0x08000400L, 0x00000420L,
  1747. +0x00000020L, 0x00010420L, 0x08010000L, 0x08000020L,
  1748. +},{
  1749. +/* nibble 5 */
  1750. +0x80000040L, 0x00200040L, 0x00000000L, 0x80202000L,
  1751. +0x00200040L, 0x00002000L, 0x80002040L, 0x00200000L,
  1752. +0x00002040L, 0x80202040L, 0x00202000L, 0x80000000L,
  1753. +0x80002000L, 0x80000040L, 0x80200000L, 0x00202040L,
  1754. +0x00200000L, 0x80002040L, 0x80200040L, 0x00000000L,
  1755. +0x00002000L, 0x00000040L, 0x80202000L, 0x80200040L,
  1756. +0x80202040L, 0x80200000L, 0x80000000L, 0x00002040L,
  1757. +0x00000040L, 0x00202000L, 0x00202040L, 0x80002000L,
  1758. +0x00002040L, 0x80000000L, 0x80002000L, 0x00202040L,
  1759. +0x80202000L, 0x00200040L, 0x00000000L, 0x80002000L,
  1760. +0x80000000L, 0x00002000L, 0x80200040L, 0x00200000L,
  1761. +0x00200040L, 0x80202040L, 0x00202000L, 0x00000040L,
  1762. +0x80202040L, 0x00202000L, 0x00200000L, 0x80002040L,
  1763. +0x80000040L, 0x80200000L, 0x00202040L, 0x00000000L,
  1764. +0x00002000L, 0x80000040L, 0x80002040L, 0x80202000L,
  1765. +0x80200000L, 0x00002040L, 0x00000040L, 0x80200040L,
  1766. +},{
  1767. +/* nibble 6 */
  1768. +0x00004000L, 0x00000200L, 0x01000200L, 0x01000004L,
  1769. +0x01004204L, 0x00004004L, 0x00004200L, 0x00000000L,
  1770. +0x01000000L, 0x01000204L, 0x00000204L, 0x01004000L,
  1771. +0x00000004L, 0x01004200L, 0x01004000L, 0x00000204L,
  1772. +0x01000204L, 0x00004000L, 0x00004004L, 0x01004204L,
  1773. +0x00000000L, 0x01000200L, 0x01000004L, 0x00004200L,
  1774. +0x01004004L, 0x00004204L, 0x01004200L, 0x00000004L,
  1775. +0x00004204L, 0x01004004L, 0x00000200L, 0x01000000L,
  1776. +0x00004204L, 0x01004000L, 0x01004004L, 0x00000204L,
  1777. +0x00004000L, 0x00000200L, 0x01000000L, 0x01004004L,
  1778. +0x01000204L, 0x00004204L, 0x00004200L, 0x00000000L,
  1779. +0x00000200L, 0x01000004L, 0x00000004L, 0x01000200L,
  1780. +0x00000000L, 0x01000204L, 0x01000200L, 0x00004200L,
  1781. +0x00000204L, 0x00004000L, 0x01004204L, 0x01000000L,
  1782. +0x01004200L, 0x00000004L, 0x00004004L, 0x01004204L,
  1783. +0x01000004L, 0x01004200L, 0x01004000L, 0x00004004L,
  1784. +},{
  1785. +/* nibble 7 */
  1786. +0x20800080L, 0x20820000L, 0x00020080L, 0x00000000L,
  1787. +0x20020000L, 0x00800080L, 0x20800000L, 0x20820080L,
  1788. +0x00000080L, 0x20000000L, 0x00820000L, 0x00020080L,
  1789. +0x00820080L, 0x20020080L, 0x20000080L, 0x20800000L,
  1790. +0x00020000L, 0x00820080L, 0x00800080L, 0x20020000L,
  1791. +0x20820080L, 0x20000080L, 0x00000000L, 0x00820000L,
  1792. +0x20000000L, 0x00800000L, 0x20020080L, 0x20800080L,
  1793. +0x00800000L, 0x00020000L, 0x20820000L, 0x00000080L,
  1794. +0x00800000L, 0x00020000L, 0x20000080L, 0x20820080L,
  1795. +0x00020080L, 0x20000000L, 0x00000000L, 0x00820000L,
  1796. +0x20800080L, 0x20020080L, 0x20020000L, 0x00800080L,
  1797. +0x20820000L, 0x00000080L, 0x00800080L, 0x20020000L,
  1798. +0x20820080L, 0x00800000L, 0x20800000L, 0x20000080L,
  1799. +0x00820000L, 0x00020080L, 0x20020080L, 0x20800000L,
  1800. +0x00000080L, 0x20820000L, 0x00820080L, 0x00000000L,
  1801. +0x20000000L, 0x20800080L, 0x00020000L, 0x00820080L,
  1802. +}};
  1803. --- /dev/null Tue Mar 11 13:02:56 2003
  1804. +++ linux/include/mast.h Mon Feb 9 13:51:03 2004
  1805. @@ -0,0 +1,33 @@
  1806. +struct mast_callbacks {
  1807. + int (*packet_encap)(struct device *mast, void *context,
  1808. + struct sk_buff *skb, int flowref);
  1809. + int (*link_inquire)(struct device *mast, void *context);
  1810. +};
  1811. +
  1812. +
  1813. +struct device *mast_init (int family,
  1814. + struct mast_callbacks *callbacks,
  1815. + unsigned int flags,
  1816. + unsigned int desired_unit,
  1817. + unsigned int max_flowref,
  1818. + void *context);
  1819. +
  1820. +int mast_destroy(struct device *mast);
  1821. +
  1822. +int mast_recv(struct device *mast, struct sk_buff *skb, int flowref);
  1823. +
  1824. +/* free this skb as being useless, increment failure count. */
  1825. +int mast_toast(struct device *mast, struct sk_buff *skb, int flowref);
  1826. +
  1827. +int mast_linkstat (struct device *mast, int flowref,
  1828. + int status);
  1829. +
  1830. +int mast_setreference (struct device *mast,
  1831. + int defaultSA);
  1832. +
  1833. +int mast_setneighbor (struct device *mast,
  1834. + struct sockaddr *source,
  1835. + struct sockaddr *destination,
  1836. + int flowref);
  1837. +
  1838. +
  1839. --- /dev/null Tue Mar 11 13:02:56 2003
  1840. +++ linux/include/openswan.h Mon Feb 9 13:51:03 2004
  1841. @@ -0,0 +1,518 @@
  1842. +#ifndef _OPENSWAN_H
  1843. +/*
  1844. + * header file for FreeS/WAN library functions
  1845. + * Copyright (C) 1998, 1999, 2000 Henry Spencer.
  1846. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs
  1847. + *
  1848. + * This library is free software; you can redistribute it and/or modify it
  1849. + * under the terms of the GNU Library General Public License as published by
  1850. + * the Free Software Foundation; either version 2 of the License, or (at your
  1851. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  1852. + *
  1853. + * This library is distributed in the hope that it will be useful, but
  1854. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  1855. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  1856. + * License for more details.
  1857. + *
  1858. + * RCSID $Id: openswan.h,v 1.93 2005/04/14 20:21:51 mcr Exp $
  1859. + */
  1860. +#define _OPENSWAN_H /* seen it, no need to see it again */
  1861. +
  1862. +/* you'd think this should be builtin to compiler... */
  1863. +#ifndef TRUE
  1864. +#define TRUE 1
  1865. +#endif
  1866. +
  1867. +#ifndef FALSE
  1868. +#define FALSE 0
  1869. +#endif
  1870. +
  1871. +
  1872. +
  1873. +/*
  1874. + * We've just got to have some datatypes defined... And annoyingly, just
  1875. + * where we get them depends on whether we're in userland or not.
  1876. + */
  1877. +/* things that need to come from one place or the other, depending */
  1878. +#ifdef __KERNEL__
  1879. +#include <linux/types.h>
  1880. +#include <linux/socket.h>
  1881. +#include <linux/in.h>
  1882. +#include <linux/string.h>
  1883. +#include <linux/ctype.h>
  1884. +#define user_assert(foo) /*nothing*/
  1885. +#else
  1886. +#include <sys/types.h>
  1887. +#include <netinet/in.h>
  1888. +#include <string.h>
  1889. +#include <ctype.h>
  1890. +#include <assert.h>
  1891. +#define user_assert(foo) assert(foo)
  1892. +#include <stdio.h>
  1893. +
  1894. +# define uint8_t u_int8_t
  1895. +# define uint16_t u_int16_t
  1896. +# define uint32_t u_int32_t
  1897. +# define uint64_t u_int64_t
  1898. +
  1899. +
  1900. +# define DEBUG_NO_STATIC static
  1901. +
  1902. +#endif
  1903. +
  1904. +#include <openswan/ipsec_param.h>
  1905. +
  1906. +
  1907. +/*
  1908. + * Grab the kernel version to see if we have NET_21, and therefore
  1909. + * IPv6. Some of this is repeated from ipsec_kversions.h. Of course,
  1910. + * we aren't really testing if the kernel has IPv6, but rather if the
  1911. + * the include files do.
  1912. + */
  1913. +#include <linux/version.h>
  1914. +#ifndef KERNEL_VERSION
  1915. +#define KERNEL_VERSION(x,y,z) (((x)<<16)+((y)<<8)+(z))
  1916. +#endif
  1917. +
  1918. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)
  1919. +#define NET_21
  1920. +#endif
  1921. +
  1922. +#ifndef IPPROTO_COMP
  1923. +# define IPPROTO_COMP 108
  1924. +#endif /* !IPPROTO_COMP */
  1925. +
  1926. +#ifndef IPPROTO_INT
  1927. +# define IPPROTO_INT 61
  1928. +#endif /* !IPPROTO_INT */
  1929. +
  1930. +#ifdef CONFIG_KLIPS_DEBUG
  1931. +#ifndef DEBUG_NO_STATIC
  1932. +# define DEBUG_NO_STATIC
  1933. +#endif
  1934. +#else /* CONFIG_KLIPS_DEBUG */
  1935. +#ifndef DEBUG_NO_STATIC
  1936. +# define DEBUG_NO_STATIC static
  1937. +#endif
  1938. +#endif /* CONFIG_KLIPS_DEBUG */
  1939. +
  1940. +#if !defined(ESPINUDP_WITH_NON_IKE)
  1941. +#define ESPINUDP_WITH_NON_IKE 1 /* draft-ietf-ipsec-nat-t-ike-00/01 */
  1942. +#define ESPINUDP_WITH_NON_ESP 2 /* draft-ietf-ipsec-nat-t-ike-02 */
  1943. +#endif
  1944. +
  1945. +/*
  1946. + * Basic data types for the address-handling functions.
  1947. + * ip_address and ip_subnet are supposed to be opaque types; do not
  1948. + * use their definitions directly, they are subject to change!
  1949. + */
  1950. +
  1951. +/* first, some quick fakes in case we're on an old system with no IPv6 */
  1952. +#ifndef s6_addr16
  1953. +struct in6_addr {
  1954. + union
  1955. + {
  1956. + __u8 u6_addr8[16];
  1957. + __u16 u6_addr16[8];
  1958. + __u32 u6_addr32[4];
  1959. + } in6_u;
  1960. +#define s6_addr in6_u.u6_addr8
  1961. +#define s6_addr16 in6_u.u6_addr16
  1962. +#define s6_addr32 in6_u.u6_addr32
  1963. +};
  1964. +struct sockaddr_in6 {
  1965. + unsigned short int sin6_family; /* AF_INET6 */
  1966. + __u16 sin6_port; /* Transport layer port # */
  1967. + __u32 sin6_flowinfo; /* IPv6 flow information */
  1968. + struct in6_addr sin6_addr; /* IPv6 address */
  1969. + __u32 sin6_scope_id; /* scope id (new in RFC2553) */
  1970. +};
  1971. +#endif /* !s6_addr16 */
  1972. +
  1973. +/* then the main types */
  1974. +typedef struct {
  1975. + union {
  1976. + struct sockaddr_in v4;
  1977. + struct sockaddr_in6 v6;
  1978. + } u;
  1979. +} ip_address;
  1980. +typedef struct {
  1981. + ip_address addr;
  1982. + int maskbits;
  1983. +} ip_subnet;
  1984. +
  1985. +/* and the SA ID stuff */
  1986. +#ifdef __KERNEL__
  1987. +typedef __u32 ipsec_spi_t;
  1988. +#else
  1989. +typedef u_int32_t ipsec_spi_t;
  1990. +#endif
  1991. +typedef struct { /* to identify an SA, we need: */
  1992. + ip_address dst; /* A. destination host */
  1993. + ipsec_spi_t spi; /* B. 32-bit SPI, assigned by dest. host */
  1994. +# define SPI_PASS 256 /* magic values... */
  1995. +# define SPI_DROP 257 /* ...for use... */
  1996. +# define SPI_REJECT 258 /* ...with SA_INT */
  1997. +# define SPI_HOLD 259
  1998. +# define SPI_TRAP 260
  1999. +# define SPI_TRAPSUBNET 261
  2000. + int proto; /* C. protocol */
  2001. +# define SA_ESP 50 /* IPPROTO_ESP */
  2002. +# define SA_AH 51 /* IPPROTO_AH */
  2003. +# define SA_IPIP 4 /* IPPROTO_IPIP */
  2004. +# define SA_COMP 108 /* IPPROTO_COMP */
  2005. +# define SA_INT 61 /* IANA reserved for internal use */
  2006. +} ip_said;
  2007. +
  2008. +/* misc */
  2009. +typedef const char *err_t; /* error message, or NULL for success */
  2010. +struct prng { /* pseudo-random-number-generator guts */
  2011. + unsigned char sbox[256];
  2012. + int i, j;
  2013. + unsigned long count;
  2014. +};
  2015. +
  2016. +
  2017. +/*
  2018. + * definitions for user space, taken from freeswan/ipsec_sa.h
  2019. + */
  2020. +typedef uint32_t IPsecSAref_t;
  2021. +
  2022. +#define IPSEC_SA_REF_FIELD_WIDTH (8 * sizeof(IPsecSAref_t))
  2023. +
  2024. +#define IPsecSAref2NFmark(x) ((x) << (IPSEC_SA_REF_FIELD_WIDTH - IPSEC_SA_REF_TABLE_IDX_WIDTH))
  2025. +#define NFmark2IPsecSAref(x) ((x) >> (IPSEC_SA_REF_FIELD_WIDTH - IPSEC_SA_REF_TABLE_IDX_WIDTH))
  2026. +
  2027. +#define IPSEC_SAREF_NULL (~((IPsecSAref_t)0))
  2028. +
  2029. +/* GCC magic for use in function definitions! */
  2030. +#ifdef GCC_LINT
  2031. +# define PRINTF_LIKE(n) __attribute__ ((format(printf, n, n+1)))
  2032. +# define NEVER_RETURNS __attribute__ ((noreturn))
  2033. +# define UNUSED __attribute__ ((unused))
  2034. +# define BLANK_FORMAT " " /* GCC_LINT whines about empty formats */
  2035. +#else
  2036. +# define PRINTF_LIKE(n) /* ignore */
  2037. +# define NEVER_RETURNS /* ignore */
  2038. +# define UNUSED /* ignore */
  2039. +# define BLANK_FORMAT ""
  2040. +#endif
  2041. +
  2042. +
  2043. +
  2044. +
  2045. +
  2046. +/*
  2047. + * new IPv6-compatible functions
  2048. + */
  2049. +
  2050. +/* text conversions */
  2051. +err_t ttoul(const char *src, size_t srclen, int format, unsigned long *dst);
  2052. +size_t ultot(unsigned long src, int format, char *buf, size_t buflen);
  2053. +#define ULTOT_BUF (22+1) /* holds 64 bits in octal */
  2054. +err_t ttoaddr(const char *src, size_t srclen, int af, ip_address *dst);
  2055. +err_t tnatoaddr(const char *src, size_t srclen, int af, ip_address *dst);
  2056. +size_t addrtot(const ip_address *src, int format, char *buf, size_t buflen);
  2057. +/* RFC 1886 old IPv6 reverse-lookup format is the bulkiest */
  2058. +#define ADDRTOT_BUF (32*2 + 3 + 1 + 3 + 1 + 1)
  2059. +err_t ttosubnet(const char *src, size_t srclen, int af, ip_subnet *dst);
  2060. +size_t subnettot(const ip_subnet *src, int format, char *buf, size_t buflen);
  2061. +#define SUBNETTOT_BUF (ADDRTOT_BUF + 1 + 3)
  2062. +size_t subnetporttot(const ip_subnet *src, int format, char *buf, size_t buflen);
  2063. +#define SUBNETPROTOTOT_BUF (SUBNETTOTO_BUF + ULTOT_BUF)
  2064. +err_t ttosa(const char *src, size_t srclen, ip_said *dst);
  2065. +size_t satot(const ip_said *src, int format, char *bufptr, size_t buflen);
  2066. +#define SATOT_BUF (5 + ULTOA_BUF + 1 + ADDRTOT_BUF)
  2067. +err_t ttodata(const char *src, size_t srclen, int base, char *buf,
  2068. + size_t buflen, size_t *needed);
  2069. +err_t ttodatav(const char *src, size_t srclen, int base,
  2070. + char *buf, size_t buflen, size_t *needed,
  2071. + char *errp, size_t errlen, unsigned int flags);
  2072. +#define TTODATAV_BUF 40 /* ttodatav's largest non-literal message */
  2073. +#define TTODATAV_IGNORESPACE (1<<1) /* ignore spaces in base64 encodings*/
  2074. +#define TTODATAV_SPACECOUNTS 0 /* do not ignore spaces in base64 */
  2075. +
  2076. +size_t datatot(const char *src, size_t srclen, int format, char *buf,
  2077. + size_t buflen);
  2078. +size_t keyblobtoid(const unsigned char *src, size_t srclen, char *dst,
  2079. + size_t dstlen);
  2080. +size_t splitkeytoid(const unsigned char *e, size_t elen, const unsigned char *m,
  2081. + size_t mlen, char *dst, size_t dstlen);
  2082. +#define KEYID_BUF 10 /* up to 9 text digits plus NUL */
  2083. +err_t ttoprotoport(char *src, size_t src_len, u_int8_t *proto, u_int16_t *port,
  2084. + int *has_port_wildcard);
  2085. +
  2086. +/* initializations */
  2087. +void initsaid(const ip_address *addr, ipsec_spi_t spi, int proto, ip_said *dst);
  2088. +err_t loopbackaddr(int af, ip_address *dst);
  2089. +err_t unspecaddr(int af, ip_address *dst);
  2090. +err_t anyaddr(int af, ip_address *dst);
  2091. +err_t initaddr(const unsigned char *src, size_t srclen, int af, ip_address *dst);
  2092. +err_t initsubnet(const ip_address *addr, int maskbits, int clash, ip_subnet *dst);
  2093. +err_t addrtosubnet(const ip_address *addr, ip_subnet *dst);
  2094. +
  2095. +/* misc. conversions and related */
  2096. +err_t rangetosubnet(const ip_address *from, const ip_address *to, ip_subnet *dst);
  2097. +int addrtypeof(const ip_address *src);
  2098. +int subnettypeof(const ip_subnet *src);
  2099. +size_t addrlenof(const ip_address *src);
  2100. +size_t addrbytesptr(const ip_address *src, const unsigned char **dst);
  2101. +size_t addrbytesof(const ip_address *src, unsigned char *dst, size_t dstlen);
  2102. +int masktocount(const ip_address *src);
  2103. +void networkof(const ip_subnet *src, ip_address *dst);
  2104. +void maskof(const ip_subnet *src, ip_address *dst);
  2105. +
  2106. +/* tests */
  2107. +int sameaddr(const ip_address *a, const ip_address *b);
  2108. +int addrcmp(const ip_address *a, const ip_address *b);
  2109. +int samesubnet(const ip_subnet *a, const ip_subnet *b);
  2110. +int addrinsubnet(const ip_address *a, const ip_subnet *s);
  2111. +int subnetinsubnet(const ip_subnet *a, const ip_subnet *b);
  2112. +int subnetishost(const ip_subnet *s);
  2113. +int samesaid(const ip_said *a, const ip_said *b);
  2114. +int sameaddrtype(const ip_address *a, const ip_address *b);
  2115. +int samesubnettype(const ip_subnet *a, const ip_subnet *b);
  2116. +int isanyaddr(const ip_address *src);
  2117. +int isunspecaddr(const ip_address *src);
  2118. +int isloopbackaddr(const ip_address *src);
  2119. +
  2120. +/* low-level grot */
  2121. +int portof(const ip_address *src);
  2122. +void setportof(int port, ip_address *dst);
  2123. +struct sockaddr *sockaddrof(ip_address *src);
  2124. +size_t sockaddrlenof(const ip_address *src);
  2125. +
  2126. +/* PRNG */
  2127. +void prng_init(struct prng *prng, const unsigned char *key, size_t keylen);
  2128. +void prng_bytes(struct prng *prng, unsigned char *dst, size_t dstlen);
  2129. +unsigned long prng_count(struct prng *prng);
  2130. +void prng_final(struct prng *prng);
  2131. +
  2132. +/* odds and ends */
  2133. +const char *ipsec_version_code(void);
  2134. +const char *ipsec_version_string(void);
  2135. +const char **ipsec_copyright_notice(void);
  2136. +
  2137. +const char *dns_string_rr(int rr, char *buf, int bufsize);
  2138. +const char *dns_string_datetime(time_t seconds,
  2139. + char *buf,
  2140. + int bufsize);
  2141. +
  2142. +
  2143. +/*
  2144. + * old functions, to be deleted eventually
  2145. + */
  2146. +
  2147. +/* unsigned long */
  2148. +const char * /* NULL for success, else string literal */
  2149. +atoul(
  2150. + const char *src,
  2151. + size_t srclen, /* 0 means strlen(src) */
  2152. + int base, /* 0 means figure it out */
  2153. + unsigned long *resultp
  2154. +);
  2155. +size_t /* space needed for full conversion */
  2156. +ultoa(
  2157. + unsigned long n,
  2158. + int base,
  2159. + char *dst,
  2160. + size_t dstlen
  2161. +);
  2162. +#define ULTOA_BUF 21 /* just large enough for largest result, */
  2163. + /* assuming 64-bit unsigned long! */
  2164. +
  2165. +/* Internet addresses */
  2166. +const char * /* NULL for success, else string literal */
  2167. +atoaddr(
  2168. + const char *src,
  2169. + size_t srclen, /* 0 means strlen(src) */
  2170. + struct in_addr *addr
  2171. +);
  2172. +size_t /* space needed for full conversion */
  2173. +addrtoa(
  2174. + struct in_addr addr,
  2175. + int format, /* character; 0 means default */
  2176. + char *dst,
  2177. + size_t dstlen
  2178. +);
  2179. +#define ADDRTOA_BUF 16 /* just large enough for largest result */
  2180. +
  2181. +/* subnets */
  2182. +const char * /* NULL for success, else string literal */
  2183. +atosubnet(
  2184. + const char *src,
  2185. + size_t srclen, /* 0 means strlen(src) */
  2186. + struct in_addr *addr,
  2187. + struct in_addr *mask
  2188. +);
  2189. +size_t /* space needed for full conversion */
  2190. +subnettoa(
  2191. + struct in_addr addr,
  2192. + struct in_addr mask,
  2193. + int format, /* character; 0 means default */
  2194. + char *dst,
  2195. + size_t dstlen
  2196. +);
  2197. +#define SUBNETTOA_BUF 32 /* large enough for worst case result */
  2198. +
  2199. +/* ranges */
  2200. +const char * /* NULL for success, else string literal */
  2201. +atoasr(
  2202. + const char *src,
  2203. + size_t srclen, /* 0 means strlen(src) */
  2204. + char *type, /* 'a', 's', 'r' */
  2205. + struct in_addr *addrs /* two-element array */
  2206. +);
  2207. +size_t /* space needed for full conversion */
  2208. +rangetoa(
  2209. + struct in_addr *addrs, /* two-element array */
  2210. + int format, /* character; 0 means default */
  2211. + char *dst,
  2212. + size_t dstlen
  2213. +);
  2214. +#define RANGETOA_BUF 34 /* large enough for worst case result */
  2215. +
  2216. +/* data types for SA conversion functions */
  2217. +
  2218. +/* generic data, e.g. keys */
  2219. +const char * /* NULL for success, else string literal */
  2220. +atobytes(
  2221. + const char *src,
  2222. + size_t srclen, /* 0 means strlen(src) */
  2223. + char *dst,
  2224. + size_t dstlen,
  2225. + size_t *lenp /* NULL means don't bother telling me */
  2226. +);
  2227. +size_t /* 0 failure, else true size */
  2228. +bytestoa(
  2229. + const char *src,
  2230. + size_t srclen,
  2231. + int format, /* character; 0 means default */
  2232. + char *dst,
  2233. + size_t dstlen
  2234. +);
  2235. +
  2236. +/* old versions of generic-data functions; deprecated */
  2237. +size_t /* 0 failure, else true size */
  2238. +atodata(
  2239. + const char *src,
  2240. + size_t srclen, /* 0 means strlen(src) */
  2241. + char *dst,
  2242. + size_t dstlen
  2243. +);
  2244. +size_t /* 0 failure, else true size */
  2245. +datatoa(
  2246. + const char *src,
  2247. + size_t srclen,
  2248. + int format, /* character; 0 means default */
  2249. + char *dst,
  2250. + size_t dstlen
  2251. +);
  2252. +
  2253. +/* part extraction and special addresses */
  2254. +struct in_addr
  2255. +subnetof(
  2256. + struct in_addr addr,
  2257. + struct in_addr mask
  2258. +);
  2259. +struct in_addr
  2260. +hostof(
  2261. + struct in_addr addr,
  2262. + struct in_addr mask
  2263. +);
  2264. +struct in_addr
  2265. +broadcastof(
  2266. + struct in_addr addr,
  2267. + struct in_addr mask
  2268. +);
  2269. +
  2270. +/* mask handling */
  2271. +int
  2272. +goodmask(
  2273. + struct in_addr mask
  2274. +);
  2275. +int
  2276. +masktobits(
  2277. + struct in_addr mask
  2278. +);
  2279. +struct in_addr
  2280. +bitstomask(
  2281. + int n
  2282. +);
  2283. +
  2284. +
  2285. +
  2286. +/*
  2287. + * general utilities
  2288. + */
  2289. +
  2290. +#ifndef __KERNEL__
  2291. +/* option pickup from files (userland only because of use of FILE) */
  2292. +const char *optionsfrom(const char *filename, int *argcp, char ***argvp,
  2293. + int optind, FILE *errorreport);
  2294. +
  2295. +/* sanitize a string */
  2296. +extern size_t sanitize_string(char *buf, size_t size);
  2297. +
  2298. +#endif
  2299. +
  2300. +
  2301. +/*
  2302. + * ENUM of klips debugging values. Not currently used in klips.
  2303. + * debug flag is actually 32 -bits, but only one bit is ever used,
  2304. + * so we can actually pack it all into a single 32-bit word.
  2305. + */
  2306. +enum klips_debug_flags {
  2307. + KDF_VERBOSE = 0,
  2308. + KDF_XMIT = 1,
  2309. + KDF_NETLINK = 2, /* obsolete */
  2310. + KDF_XFORM = 3,
  2311. + KDF_EROUTE = 4,
  2312. + KDF_SPI = 5,
  2313. + KDF_RADIJ = 6,
  2314. + KDF_ESP = 7,
  2315. + KDF_AH = 8, /* obsolete */
  2316. + KDF_RCV = 9,
  2317. + KDF_TUNNEL = 10,
  2318. + KDF_PFKEY = 11,
  2319. + KDF_COMP = 12
  2320. +};
  2321. +
  2322. +
  2323. +/*
  2324. + * Debugging levels for pfkey_lib_debug
  2325. + */
  2326. +#define PF_KEY_DEBUG_PARSE_NONE 0
  2327. +#define PF_KEY_DEBUG_PARSE_PROBLEM 1
  2328. +#define PF_KEY_DEBUG_PARSE_STRUCT 2
  2329. +#define PF_KEY_DEBUG_PARSE_FLOW 4
  2330. +#define PF_KEY_DEBUG_BUILD 8
  2331. +#define PF_KEY_DEBUG_PARSE_MAX 15
  2332. +
  2333. +extern unsigned int pfkey_lib_debug; /* bits selecting what to report */
  2334. +
  2335. +/*
  2336. + * pluto and lwdnsq need to know the maximum size of the commands to,
  2337. + * and replies from lwdnsq.
  2338. + */
  2339. +
  2340. +#define LWDNSQ_CMDBUF_LEN 1024
  2341. +#define LWDNSQ_RESULT_LEN_MAX 4096
  2342. +
  2343. +
  2344. +/* syntax for passthrough SA */
  2345. +#ifndef PASSTHROUGHNAME
  2346. +#define PASSTHROUGHNAME "%passthrough"
  2347. +#define PASSTHROUGH4NAME "%passthrough4"
  2348. +#define PASSTHROUGH6NAME "%passthrough6"
  2349. +#define PASSTHROUGHIS "tun0@0.0.0.0"
  2350. +#define PASSTHROUGH4IS "tun0@0.0.0.0"
  2351. +#define PASSTHROUGH6IS "tun0@::"
  2352. +#define PASSTHROUGHTYPE "tun"
  2353. +#define PASSTHROUGHSPI 0
  2354. +#define PASSTHROUGHDST 0
  2355. +#endif
  2356. +
  2357. +
  2358. +
  2359. +#endif /* _OPENSWAN_H */
  2360. --- /dev/null Tue Mar 11 13:02:56 2003
  2361. +++ linux/include/openswan/ipcomp.h Mon Feb 9 13:51:03 2004
  2362. @@ -0,0 +1,61 @@
  2363. +/*
  2364. + * IPCOMP zlib interface code.
  2365. + * Copyright (C) 2000 Svenning Soerensen <svenning@post5.tele.dk>
  2366. + * Copyright (C) 2000, 2001 Richard Guy Briggs <rgb@conscoop.ottawa.on.ca>
  2367. + *
  2368. + * This program is free software; you can redistribute it and/or modify it
  2369. + * under the terms of the GNU General Public License as published by the
  2370. + * Free Software Foundation; either version 2 of the License, or (at your
  2371. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  2372. + *
  2373. + * This program is distributed in the hope that it will be useful, but
  2374. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  2375. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  2376. + * for more details.
  2377. +
  2378. + RCSID $Id: ipcomp.h,v 1.14 2004/07/10 19:08:41 mcr Exp $
  2379. +
  2380. + */
  2381. +
  2382. +/* SSS */
  2383. +
  2384. +#ifndef _IPCOMP_H
  2385. +#define _IPCOMP_H
  2386. +
  2387. +/* Prefix all global deflate symbols with "ipcomp_" to avoid collisions with ppp_deflate & ext2comp */
  2388. +#ifndef IPCOMP_PREFIX
  2389. +#define IPCOMP_PREFIX
  2390. +#endif /* IPCOMP_PREFIX */
  2391. +
  2392. +#ifndef IPPROTO_COMP
  2393. +#define IPPROTO_COMP 108
  2394. +#endif /* IPPROTO_COMP */
  2395. +
  2396. +#ifdef CONFIG_KLIPS_DEBUG
  2397. +extern int sysctl_ipsec_debug_ipcomp;
  2398. +#endif /* CONFIG_KLIPS_DEBUG */
  2399. +
  2400. +struct ipcomphdr { /* IPCOMP header */
  2401. + __u8 ipcomp_nh; /* Next header (protocol) */
  2402. + __u8 ipcomp_flags; /* Reserved, must be 0 */
  2403. + __u16 ipcomp_cpi; /* Compression Parameter Index */
  2404. +};
  2405. +
  2406. +extern struct inet_protocol comp_protocol;
  2407. +extern int sysctl_ipsec_debug_ipcomp;
  2408. +
  2409. +#define IPCOMP_UNCOMPRESSABLE 0x000000001
  2410. +#define IPCOMP_COMPRESSIONERROR 0x000000002
  2411. +#define IPCOMP_PARMERROR 0x000000004
  2412. +#define IPCOMP_DECOMPRESSIONERROR 0x000000008
  2413. +
  2414. +#define IPCOMP_ADAPT_INITIAL_TRIES 8
  2415. +#define IPCOMP_ADAPT_INITIAL_SKIP 4
  2416. +#define IPCOMP_ADAPT_SUBSEQ_TRIES 2
  2417. +#define IPCOMP_ADAPT_SUBSEQ_SKIP 8
  2418. +
  2419. +/* Function prototypes */
  2420. +struct sk_buff *skb_compress(struct sk_buff *skb, struct ipsec_sa *ips, unsigned int *flags);
  2421. +struct sk_buff *skb_decompress(struct sk_buff *skb, struct ipsec_sa *ips, unsigned int *flags);
  2422. +
  2423. +#endif /* _IPCOMP_H */
  2424. --- /dev/null Tue Mar 11 13:02:56 2003
  2425. +++ linux/include/openswan/ipsec_ah.h Mon Feb 9 13:51:03 2004
  2426. @@ -0,0 +1,200 @@
  2427. +/*
  2428. + * Authentication Header declarations
  2429. + * Copyright (C) 1996, 1997 John Ioannidis.
  2430. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  2431. + *
  2432. + * This program is free software; you can redistribute it and/or modify it
  2433. + * under the terms of the GNU General Public License as published by the
  2434. + * Free Software Foundation; either version 2 of the License, or (at your
  2435. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  2436. + *
  2437. + * This program is distributed in the hope that it will be useful, but
  2438. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  2439. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  2440. + * for more details.
  2441. + *
  2442. + * RCSID $Id: ipsec_ah.h,v 1.26 2004/09/13 02:22:10 mcr Exp $
  2443. + */
  2444. +
  2445. +#include "ipsec_md5h.h"
  2446. +#include "ipsec_sha1.h"
  2447. +
  2448. +#ifndef IPPROTO_AH
  2449. +#define IPPROTO_AH 51
  2450. +#endif /* IPPROTO_AH */
  2451. +
  2452. +#include "ipsec_auth.h"
  2453. +
  2454. +#ifdef __KERNEL__
  2455. +
  2456. +extern struct inet_protocol ah_protocol;
  2457. +
  2458. +struct options;
  2459. +
  2460. +struct ahhdr /* Generic AH header */
  2461. +{
  2462. + __u8 ah_nh; /* Next header (protocol) */
  2463. + __u8 ah_hl; /* AH length, in 32-bit words */
  2464. + __u16 ah_rv; /* reserved, must be 0 */
  2465. + __u32 ah_spi; /* Security Parameters Index */
  2466. + __u32 ah_rpl; /* Replay prevention */
  2467. + __u8 ah_data[AHHMAC_HASHLEN];/* Authentication hash */
  2468. +};
  2469. +#define AH_BASIC_LEN 8 /* basic AH header is 8 bytes, nh,hl,rv,spi
  2470. + * and the ah_hl, says how many bytes after that
  2471. + * to cover. */
  2472. +
  2473. +extern struct xform_functions ah_xform_funcs[];
  2474. +
  2475. +#ifdef CONFIG_KLIPS_DEBUG
  2476. +extern int debug_ah;
  2477. +#endif /* CONFIG_KLIPS_DEBUG */
  2478. +#endif /* __KERNEL__ */
  2479. +
  2480. +/*
  2481. + * $Log: ipsec_ah.h,v $
  2482. + * Revision 1.26 2004/09/13 02:22:10 mcr
  2483. + * #define inet_protocol if necessary.
  2484. + *
  2485. + * Revision 1.25 2004/09/06 18:35:41 mcr
  2486. + * 2.6.8.1 gets rid of inet_protocol->net_protocol compatibility,
  2487. + * so adjust for that.
  2488. + *
  2489. + * Revision 1.24 2004/07/10 19:08:41 mcr
  2490. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  2491. + *
  2492. + * Revision 1.23 2004/04/05 19:55:04 mcr
  2493. + * Moved from linux/include/freeswan/ipsec_ah.h,v
  2494. + *
  2495. + * Revision 1.22 2004/04/05 19:41:05 mcr
  2496. + * merged alg-branch code.
  2497. + *
  2498. + * Revision 1.21 2003/12/13 19:10:16 mcr
  2499. + * refactored rcv and xmit code - same as FS 2.05.
  2500. + *
  2501. + * Revision 1.22 2003/12/11 20:14:58 mcr
  2502. + * refactored the xmit code, to move all encapsulation
  2503. + * code into protocol functions. Note that all functions
  2504. + * are essentially done by a single function, which is probably
  2505. + * wrong.
  2506. + * the rcv_functions structures are renamed xform_functions.
  2507. + *
  2508. + * Revision 1.21 2003/12/06 21:21:19 mcr
  2509. + * split up receive path into per-transform files, for
  2510. + * easier later removal.
  2511. + *
  2512. + * Revision 1.20.8.1 2003/12/22 15:25:52 jjo
  2513. + * Merged algo-0.8.1-rc11-test1 into alg-branch
  2514. + *
  2515. + * Revision 1.20 2003/02/06 02:21:34 rgb
  2516. + *
  2517. + * Moved "struct auth_alg" from ipsec_rcv.c to ipsec_ah.h .
  2518. + * Changed "struct ah" to "struct ahhdr" and "struct esp" to "struct esphdr".
  2519. + * Removed "#ifdef INBOUND_POLICY_CHECK_eroute" dead code.
  2520. + *
  2521. + * Revision 1.19 2002/09/16 21:19:13 mcr
  2522. + * fixes for west-ah-icmp-01 - length of AH header must be
  2523. + * calculated properly, and next_header field properly copied.
  2524. + *
  2525. + * Revision 1.18 2002/05/14 02:37:02 rgb
  2526. + * Change reference from _TDB to _IPSA.
  2527. + *
  2528. + * Revision 1.17 2002/04/24 07:36:46 mcr
  2529. + * Moved from ./klips/net/ipsec/ipsec_ah.h,v
  2530. + *
  2531. + * Revision 1.16 2002/02/20 01:27:06 rgb
  2532. + * Ditched a pile of structs only used by the old Netlink interface.
  2533. + *
  2534. + * Revision 1.15 2001/12/11 02:35:57 rgb
  2535. + * Change "struct net_device" to "struct device" for 2.2 compatibility.
  2536. + *
  2537. + * Revision 1.14 2001/11/26 09:23:47 rgb
  2538. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  2539. + *
  2540. + * Revision 1.13.2.1 2001/09/25 02:18:24 mcr
  2541. + * replace "struct device" with "struct netdevice"
  2542. + *
  2543. + * Revision 1.13 2001/06/14 19:35:08 rgb
  2544. + * Update copyright date.
  2545. + *
  2546. + * Revision 1.12 2000/09/12 03:21:20 rgb
  2547. + * Cleared out unused htonq.
  2548. + *
  2549. + * Revision 1.11 2000/09/08 19:12:55 rgb
  2550. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  2551. + *
  2552. + * Revision 1.10 2000/01/21 06:13:10 rgb
  2553. + * Tidied up spacing.
  2554. + * Added macros for HMAC padding magic numbers.(kravietz)
  2555. + *
  2556. + * Revision 1.9 1999/12/07 18:16:23 rgb
  2557. + * Fixed comments at end of #endif lines.
  2558. + *
  2559. + * Revision 1.8 1999/04/11 00:28:56 henry
  2560. + * GPL boilerplate
  2561. + *
  2562. + * Revision 1.7 1999/04/06 04:54:25 rgb
  2563. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  2564. + * patch shell fixes.
  2565. + *
  2566. + * Revision 1.6 1999/01/26 02:06:01 rgb
  2567. + * Removed CONFIG_IPSEC_ALGO_SWITCH macro.
  2568. + *
  2569. + * Revision 1.5 1999/01/22 06:17:49 rgb
  2570. + * Updated macro comments.
  2571. + * Added context types to support algorithm switch code.
  2572. + * 64-bit clean-up -- converting 'u long long' to __u64.
  2573. + *
  2574. + * Revision 1.4 1998/07/14 15:54:56 rgb
  2575. + * Add #ifdef __KERNEL__ to protect kernel-only structures.
  2576. + *
  2577. + * Revision 1.3 1998/06/30 18:05:16 rgb
  2578. + * Comment out references to htonq.
  2579. + *
  2580. + * Revision 1.2 1998/06/25 19:33:46 rgb
  2581. + * Add prototype for protocol receive function.
  2582. + * Rearrange for more logical layout.
  2583. + *
  2584. + * Revision 1.1 1998/06/18 21:27:43 henry
  2585. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  2586. + * kernel-build scripts happier in the presence of symlinks
  2587. + *
  2588. + * Revision 1.4 1998/05/18 22:28:43 rgb
  2589. + * Disable key printing facilities from /proc/net/ipsec_*.
  2590. + *
  2591. + * Revision 1.3 1998/04/21 21:29:07 rgb
  2592. + * Rearrange debug switches to change on the fly debug output from user
  2593. + * space. Only kernel changes checked in at this time. radij.c was also
  2594. + * changed to temporarily remove buggy debugging code in rj_delete causing
  2595. + * an OOPS and hence, netlink device open errors.
  2596. + *
  2597. + * Revision 1.2 1998/04/12 22:03:17 rgb
  2598. + * Updated ESP-3DES-HMAC-MD5-96,
  2599. + * ESP-DES-HMAC-MD5-96,
  2600. + * AH-HMAC-MD5-96,
  2601. + * AH-HMAC-SHA1-96 since Henry started freeswan cvs repository
  2602. + * from old standards (RFC182[5-9] to new (as of March 1998) drafts.
  2603. + *
  2604. + * Fixed eroute references in /proc/net/ipsec*.
  2605. + *
  2606. + * Started to patch module unloading memory leaks in ipsec_netlink and
  2607. + * radij tree unloading.
  2608. + *
  2609. + * Revision 1.1 1998/04/09 03:05:55 henry
  2610. + * sources moved up from linux/net/ipsec
  2611. + *
  2612. + * Revision 1.1.1.1 1998/04/08 05:35:02 henry
  2613. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  2614. + *
  2615. + * Revision 0.4 1997/01/15 01:28:15 ji
  2616. + * Added definitions for new AH transforms.
  2617. + *
  2618. + * Revision 0.3 1996/11/20 14:35:48 ji
  2619. + * Minor Cleanup.
  2620. + * Rationalized debugging code.
  2621. + *
  2622. + * Revision 0.2 1996/11/02 00:18:33 ji
  2623. + * First limited release.
  2624. + *
  2625. + *
  2626. + */
  2627. --- /dev/null Tue Mar 11 13:02:56 2003
  2628. +++ linux/include/openswan/ipsec_alg.h Mon Feb 9 13:51:03 2004
  2629. @@ -0,0 +1,248 @@
  2630. +/*
  2631. + * Modular extensions service and registration functions interface
  2632. + *
  2633. + * Author: JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  2634. + *
  2635. + * ipsec_alg.h,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
  2636. + *
  2637. + */
  2638. +/*
  2639. + * This program is free software; you can redistribute it and/or modify it
  2640. + * under the terms of the GNU General Public License as published by the
  2641. + * Free Software Foundation; either version 2 of the License, or (at your
  2642. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  2643. + *
  2644. + * This program is distributed in the hope that it will be useful, but
  2645. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  2646. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  2647. + * for more details.
  2648. + *
  2649. + */
  2650. +#ifndef IPSEC_ALG_H
  2651. +#define IPSEC_ALG_H
  2652. +
  2653. +/*
  2654. + * gcc >= 3.2 has removed __FUNCTION__, replaced by C99 __func__
  2655. + * *BUT* its a compiler variable.
  2656. + */
  2657. +#if (__GNUC__ >= 3)
  2658. +#ifndef __FUNCTION__
  2659. +#define __FUNCTION__ __func__
  2660. +#endif
  2661. +#endif
  2662. +
  2663. +/* Version 0.8.1-0 */
  2664. +#define IPSEC_ALG_VERSION 0x00080100
  2665. +
  2666. +#include <linux/types.h>
  2667. +#include <linux/list.h>
  2668. +#include <asm/atomic.h>
  2669. +#include <pfkey.h>
  2670. +
  2671. +/*
  2672. + * The following structs are used via pointers in ipsec_alg object to
  2673. + * avoid ipsec_alg.h coupling with freeswan headers, thus simplifying
  2674. + * module development
  2675. + */
  2676. +struct ipsec_sa;
  2677. +struct esp;
  2678. +
  2679. +/**************************************
  2680. + *
  2681. + * Main registration object
  2682. + *
  2683. + *************************************/
  2684. +#define IPSEC_ALG_VERSION_QUAD(v) \
  2685. + (v>>24),((v>>16)&0xff),((v>>8)&0xff),(v&0xff)
  2686. +/*
  2687. + * Main ipsec_alg objects: "OOPrograming wannabe"
  2688. + * Hierachy (carefully handled with _minimal_ cast'ing):
  2689. + *
  2690. + * ipsec_alg+
  2691. + * +->ipsec_alg_enc (ixt_alg_type=SADB_EXT_SUPPORTED_ENCRYPT)
  2692. + * +->ipsec_alg_auth (ixt_alg_type=SADB_EXT_SUPPORTED_AUTH)
  2693. + */
  2694. +
  2695. +/***************************************************************
  2696. + *
  2697. + * INTERFACE object: struct ipsec_alg
  2698. + *
  2699. + ***************************************************************/
  2700. +
  2701. +#define ixt_alg_type ixt_support.ias_exttype
  2702. +#define ixt_alg_id ixt_support.ias_id
  2703. +
  2704. +#define IPSEC_ALG_ST_SUPP 0x01
  2705. +#define IPSEC_ALG_ST_REGISTERED 0x02
  2706. +#define IPSEC_ALG_ST_EXCL 0x04
  2707. +struct ipsec_alg {
  2708. + unsigned ixt_version; /* only allow this version (or 'near')*/ \
  2709. + struct list_head ixt_list; /* dlinked list */ \
  2710. + struct module *ixt_module; /* THIS_MODULE */ \
  2711. + unsigned ixt_state; /* state flags */ \
  2712. + atomic_t ixt_refcnt; /* ref. count when pointed from ipsec_sa */ \
  2713. + char ixt_name[16]; /* descriptive short name, eg. "3des" */ \
  2714. + void *ixt_data; /* private for algo implementation */ \
  2715. + uint8_t ixt_blocksize; /* blocksize in bytes */ \
  2716. +
  2717. + struct ipsec_alg_supported ixt_support;
  2718. +};
  2719. +/*
  2720. + * Note the const in cbc_encrypt IV arg:
  2721. + * some ciphers like to toast passed IV (eg. 3DES): make a local IV copy
  2722. + */
  2723. +struct ipsec_alg_enc {
  2724. + struct ipsec_alg ixt_common;
  2725. + unsigned ixt_e_keylen; /* raw key length in bytes */
  2726. + unsigned ixt_e_ctx_size; /* sa_p->key_e_size */
  2727. + int (*ixt_e_set_key)(struct ipsec_alg_enc *alg, __u8 *key_e, const __u8 *key, size_t keysize);
  2728. + __u8 *(*ixt_e_new_key)(struct ipsec_alg_enc *alg, const __u8 *key, size_t keysize);
  2729. + void (*ixt_e_destroy_key)(struct ipsec_alg_enc *alg, __u8 *key_e);
  2730. + int (*ixt_e_cbc_encrypt)(struct ipsec_alg_enc *alg, __u8 *key_e, __u8 *in, int ilen, const __u8 *iv, int encrypt);
  2731. +};
  2732. +struct ipsec_alg_auth {
  2733. + struct ipsec_alg ixt_common;
  2734. + unsigned ixt_a_keylen; /* raw key length in bytes */
  2735. + unsigned ixt_a_ctx_size; /* sa_p->key_a_size */
  2736. + unsigned ixt_a_authlen; /* 'natural' auth. hash len (bytes) */
  2737. + int (*ixt_a_hmac_set_key)(struct ipsec_alg_auth *alg, __u8 *key_a, const __u8 *key, int keylen);
  2738. + int (*ixt_a_hmac_hash)(struct ipsec_alg_auth *alg, __u8 *key_a, const __u8 *dat, int len, __u8 *hash, int hashlen);
  2739. +};
  2740. +/*
  2741. + * These are _copies_ of SADB_EXT_SUPPORTED_{AUTH,ENCRYPT},
  2742. + * to avoid header coupling for true constants
  2743. + * about headers ... "cp is your friend" --Linus
  2744. + */
  2745. +#define IPSEC_ALG_TYPE_AUTH 14
  2746. +#define IPSEC_ALG_TYPE_ENCRYPT 15
  2747. +
  2748. +/***************************************************************
  2749. + *
  2750. + * INTERFACE for module loading,testing, and unloading
  2751. + *
  2752. + ***************************************************************/
  2753. +/* - registration calls */
  2754. +int register_ipsec_alg(struct ipsec_alg *);
  2755. +int unregister_ipsec_alg(struct ipsec_alg *);
  2756. +/* - optional (simple test) for algos */
  2757. +int ipsec_alg_test(unsigned alg_type, unsigned alg_id, int testparm);
  2758. +/* inline wrappers (usefull for type validation */
  2759. +static inline int register_ipsec_alg_enc(struct ipsec_alg_enc *ixt) {
  2760. + return register_ipsec_alg((struct ipsec_alg*)ixt);
  2761. +}
  2762. +static inline int unregister_ipsec_alg_enc(struct ipsec_alg_enc *ixt) {
  2763. + return unregister_ipsec_alg((struct ipsec_alg*)ixt);
  2764. +}
  2765. +static inline int register_ipsec_alg_auth(struct ipsec_alg_auth *ixt) {
  2766. + return register_ipsec_alg((struct ipsec_alg*)ixt);
  2767. +}
  2768. +static inline int unregister_ipsec_alg_auth(struct ipsec_alg_auth *ixt) {
  2769. + return unregister_ipsec_alg((struct ipsec_alg*)ixt);
  2770. +}
  2771. +
  2772. +/*****************************************************************
  2773. + *
  2774. + * INTERFACE for ENC services: key creation, encrypt function
  2775. + *
  2776. + *****************************************************************/
  2777. +
  2778. +#define IPSEC_ALG_ENCRYPT 1
  2779. +#define IPSEC_ALG_DECRYPT 0
  2780. +
  2781. +/* encryption key context creation function */
  2782. +int ipsec_alg_enc_key_create(struct ipsec_sa *sa_p);
  2783. +/*
  2784. + * ipsec_alg_esp_encrypt(): encrypt ilen bytes in idat returns
  2785. + * 0 or ERR<0
  2786. + */
  2787. +int ipsec_alg_esp_encrypt(struct ipsec_sa *sa_p, __u8 *idat, int ilen, const __u8 *iv, int action);
  2788. +
  2789. +/***************************************************************
  2790. + *
  2791. + * INTERFACE for AUTH services: key creation, hash functions
  2792. + *
  2793. + ***************************************************************/
  2794. +int ipsec_alg_auth_key_create(struct ipsec_sa *sa_p);
  2795. +int ipsec_alg_sa_esp_hash(const struct ipsec_sa *sa_p, const __u8 *espp, int len, __u8 *hash, int hashlen) ;
  2796. +#define ipsec_alg_sa_esp_update(c,k,l) ipsec_alg_sa_esp_hash(c,k,l,NULL,0)
  2797. +
  2798. +/* only called from ipsec_init.c */
  2799. +int ipsec_alg_init(void);
  2800. +
  2801. +/* algo module glue for static algos */
  2802. +void ipsec_alg_static_init(void);
  2803. +typedef int (*ipsec_alg_init_func_t) (void);
  2804. +
  2805. +/**********************************************
  2806. + *
  2807. + * INTERFACE for ipsec_sa init and wipe
  2808. + *
  2809. + **********************************************/
  2810. +
  2811. +/* returns true if ipsec_sa has ipsec_alg obj attached */
  2812. +/*
  2813. + * Initializes ipsec_sa's ipsec_alg object, using already loaded
  2814. + * proto, authalg, encalg.; links ipsec_alg objects (enc, auth)
  2815. + */
  2816. +int ipsec_alg_sa_init(struct ipsec_sa *sa_p);
  2817. +/*
  2818. + * Destroys ipsec_sa's ipsec_alg object
  2819. + * unlinking ipsec_alg objects
  2820. + */
  2821. +int ipsec_alg_sa_wipe(struct ipsec_sa *sa_p);
  2822. +
  2823. +#define IPSEC_ALG_MODULE_INIT_MOD( func_name ) \
  2824. + static int func_name(void); \
  2825. + module_init(func_name); \
  2826. + static int __init func_name(void)
  2827. +#define IPSEC_ALG_MODULE_EXIT_MOD( func_name ) \
  2828. + static void func_name(void); \
  2829. + module_exit(func_name); \
  2830. + static void __exit func_name(void)
  2831. +
  2832. +#define IPSEC_ALG_MODULE_INIT_STATIC( func_name ) \
  2833. + extern int func_name(void); \
  2834. + int func_name(void)
  2835. +#define IPSEC_ALG_MODULE_EXIT_STATIC( func_name ) \
  2836. + extern void func_name(void); \
  2837. + void func_name(void)
  2838. +
  2839. +/**********************************************
  2840. + *
  2841. + * 2.2 backport for some 2.4 useful module stuff
  2842. + *
  2843. + **********************************************/
  2844. +#ifdef MODULE
  2845. +#ifndef THIS_MODULE
  2846. +#define THIS_MODULE (&__this_module)
  2847. +#endif
  2848. +#ifndef module_init
  2849. +typedef int (*__init_module_func_t)(void);
  2850. +typedef void (*__cleanup_module_func_t)(void);
  2851. +
  2852. +#define module_init(x) \
  2853. + int init_module(void) __attribute__((alias(#x))); \
  2854. + static inline __init_module_func_t __init_module_inline(void) \
  2855. + { return x; }
  2856. +#define module_exit(x) \
  2857. + void cleanup_module(void) __attribute__((alias(#x))); \
  2858. + static inline __cleanup_module_func_t __cleanup_module_inline(void) \
  2859. + { return x; }
  2860. +#endif
  2861. +#define IPSEC_ALG_MODULE_INIT( func_name ) IPSEC_ALG_MODULE_INIT_MOD( func_name )
  2862. +#define IPSEC_ALG_MODULE_EXIT( func_name ) IPSEC_ALG_MODULE_EXIT_MOD( func_name )
  2863. +
  2864. +#else /* not MODULE */
  2865. +#ifndef THIS_MODULE
  2866. +#define THIS_MODULE NULL
  2867. +#endif
  2868. +/*
  2869. + * I only want module_init() magic
  2870. + * when algo.c file *is THE MODULE*, in all other
  2871. + * cases, initialization is called explicitely from ipsec_alg_init()
  2872. + */
  2873. +#define IPSEC_ALG_MODULE_INIT( func_name ) IPSEC_ALG_MODULE_INIT_STATIC(func_name)
  2874. +#define IPSEC_ALG_MODULE_EXIT( func_name ) IPSEC_ALG_MODULE_EXIT_STATIC(func_name)
  2875. +#endif
  2876. +
  2877. +#endif /* IPSEC_ALG_H */
  2878. --- /dev/null Tue Mar 11 13:02:56 2003
  2879. +++ linux/include/openswan/ipsec_alg_3des.h Mon Feb 9 13:51:03 2004
  2880. @@ -0,0 +1,12 @@
  2881. +struct TripleDES_context {
  2882. + des_key_schedule s1;
  2883. + des_key_schedule s2;
  2884. + des_key_schedule s3;
  2885. +};
  2886. +typedef struct TripleDES_context TripleDES_context;
  2887. +
  2888. +#define ESP_3DES_KEY_SZ 3*(sizeof(des_cblock))
  2889. +#define ESP_3DES_CBC_BLK_LEN 8
  2890. +
  2891. +
  2892. +
  2893. --- /dev/null Tue Mar 11 13:02:56 2003
  2894. +++ linux/include/openswan/ipsec_auth.h Mon Feb 9 13:51:03 2004
  2895. @@ -0,0 +1,100 @@
  2896. +/*
  2897. + * Authentication Header declarations
  2898. + * Copyright (C) 2003 Michael Richardson <mcr@sandelman.ottawa.on.ca>
  2899. + *
  2900. + * This program is free software; you can redistribute it and/or modify it
  2901. + * under the terms of the GNU General Public License as published by the
  2902. + * Free Software Foundation; either version 2 of the License, or (at your
  2903. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  2904. + *
  2905. + * This program is distributed in the hope that it will be useful, but
  2906. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  2907. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  2908. + * for more details.
  2909. + *
  2910. + * RCSID $Id: ipsec_auth.h,v 1.3 2004/04/06 02:49:08 mcr Exp $
  2911. + */
  2912. +
  2913. +#include "ipsec_md5h.h"
  2914. +#include "ipsec_sha1.h"
  2915. +
  2916. +#ifndef IPSEC_AUTH_H
  2917. +#define IPSEC_AUTH_H
  2918. +
  2919. +#define AH_FLENGTH 12 /* size of fixed part */
  2920. +#define AHMD5_KMAX 64 /* MD5 max 512 bits key */
  2921. +#define AHMD5_AMAX 12 /* MD5 96 bits of authenticator */
  2922. +
  2923. +#define AHMD596_KLEN 16 /* MD5 128 bits key */
  2924. +#define AHSHA196_KLEN 20 /* SHA1 160 bits key */
  2925. +
  2926. +#define AHMD596_ALEN 16 /* MD5 128 bits authentication length */
  2927. +#define AHSHA196_ALEN 20 /* SHA1 160 bits authentication length */
  2928. +
  2929. +#define AHMD596_BLKLEN 64 /* MD5 block length */
  2930. +#define AHSHA196_BLKLEN 64 /* SHA1 block length */
  2931. +#define AHSHA2_256_BLKLEN 64 /* SHA2-256 block length */
  2932. +#define AHSHA2_384_BLKLEN 128 /* SHA2-384 block length (?) */
  2933. +#define AHSHA2_512_BLKLEN 128 /* SHA2-512 block length */
  2934. +
  2935. +#define AH_BLKLEN_MAX 128 /* keep up to date! */
  2936. +
  2937. +
  2938. +#define AH_AMAX AHSHA196_ALEN /* keep up to date! */
  2939. +#define AHHMAC_HASHLEN 12 /* authenticator length of 96bits */
  2940. +#define AHHMAC_RPLLEN 4 /* 32 bit replay counter */
  2941. +
  2942. +#define DB_AH_PKTRX 0x0001
  2943. +#define DB_AH_PKTRX2 0x0002
  2944. +#define DB_AH_DMP 0x0004
  2945. +#define DB_AH_IPSA 0x0010
  2946. +#define DB_AH_XF 0x0020
  2947. +#define DB_AH_INAU 0x0040
  2948. +#define DB_AH_REPLAY 0x0100
  2949. +
  2950. +#ifdef __KERNEL__
  2951. +
  2952. +/* General HMAC algorithm is described in RFC 2104 */
  2953. +
  2954. +#define HMAC_IPAD 0x36
  2955. +#define HMAC_OPAD 0x5C
  2956. +
  2957. +struct md5_ctx {
  2958. + MD5_CTX ictx; /* context after H(K XOR ipad) */
  2959. + MD5_CTX octx; /* context after H(K XOR opad) */
  2960. +};
  2961. +
  2962. +struct sha1_ctx {
  2963. + SHA1_CTX ictx; /* context after H(K XOR ipad) */
  2964. + SHA1_CTX octx; /* context after H(K XOR opad) */
  2965. +};
  2966. +
  2967. +struct auth_alg {
  2968. + void (*init)(void *ctx);
  2969. + void (*update)(void *ctx, unsigned char *bytes, __u32 len);
  2970. + void (*final)(unsigned char *hash, void *ctx);
  2971. + int hashlen;
  2972. +};
  2973. +
  2974. +struct options;
  2975. +
  2976. +#endif /* __KERNEL__ */
  2977. +#endif /* IPSEC_AUTH_H */
  2978. +
  2979. +/*
  2980. + * $Log: ipsec_auth.h,v $
  2981. + * Revision 1.3 2004/04/06 02:49:08 mcr
  2982. + * pullup of algo code from alg-branch.
  2983. + *
  2984. + * Revision 1.2 2004/04/05 19:55:04 mcr
  2985. + * Moved from linux/include/freeswan/ipsec_auth.h,v
  2986. + *
  2987. + * Revision 1.1 2003/12/13 19:10:16 mcr
  2988. + * refactored rcv and xmit code - same as FS 2.05.
  2989. + *
  2990. + * Revision 1.1 2003/12/06 21:21:19 mcr
  2991. + * split up receive path into per-transform files, for
  2992. + * easier later removal.
  2993. + *
  2994. + *
  2995. + */
  2996. --- /dev/null Tue Mar 11 13:02:56 2003
  2997. +++ linux/include/openswan/ipsec_encap.h Mon Feb 9 13:51:03 2004
  2998. @@ -0,0 +1,149 @@
  2999. +/*
  3000. + * declarations relevant to encapsulation-like operations
  3001. + * Copyright (C) 1996, 1997 John Ioannidis.
  3002. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  3003. + *
  3004. + * This program is free software; you can redistribute it and/or modify it
  3005. + * under the terms of the GNU General Public License as published by the
  3006. + * Free Software Foundation; either version 2 of the License, or (at your
  3007. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  3008. + *
  3009. + * This program is distributed in the hope that it will be useful, but
  3010. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  3011. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  3012. + * for more details.
  3013. + *
  3014. + * RCSID $Id: ipsec_encap.h,v 1.19 2004/04/05 19:55:04 mcr Exp $
  3015. + */
  3016. +
  3017. +#ifndef _IPSEC_ENCAP_H_
  3018. +
  3019. +#define SENT_IP4 16 /* data is two struct in_addr + proto + ports*/
  3020. + /* (2 * sizeof(struct in_addr)) */
  3021. + /* sizeof(struct sockaddr_encap)
  3022. + - offsetof(struct sockaddr_encap, Sen.Sip4.Src) */
  3023. +
  3024. +struct sockaddr_encap
  3025. +{
  3026. + __u8 sen_len; /* length */
  3027. + __u8 sen_family; /* AF_ENCAP */
  3028. + __u16 sen_type; /* see SENT_* */
  3029. + union
  3030. + {
  3031. + struct /* SENT_IP4 */
  3032. + {
  3033. + struct in_addr Src;
  3034. + struct in_addr Dst;
  3035. + __u8 Proto;
  3036. + __u16 Sport;
  3037. + __u16 Dport;
  3038. + } Sip4;
  3039. + } Sen;
  3040. +};
  3041. +
  3042. +#define sen_ip_src Sen.Sip4.Src
  3043. +#define sen_ip_dst Sen.Sip4.Dst
  3044. +#define sen_proto Sen.Sip4.Proto
  3045. +#define sen_sport Sen.Sip4.Sport
  3046. +#define sen_dport Sen.Sip4.Dport
  3047. +
  3048. +#ifndef AF_ENCAP
  3049. +#define AF_ENCAP 26
  3050. +#endif /* AF_ENCAP */
  3051. +
  3052. +#define _IPSEC_ENCAP_H_
  3053. +#endif /* _IPSEC_ENCAP_H_ */
  3054. +
  3055. +/*
  3056. + * $Log: ipsec_encap.h,v $
  3057. + * Revision 1.19 2004/04/05 19:55:04 mcr
  3058. + * Moved from linux/include/freeswan/ipsec_encap.h,v
  3059. + *
  3060. + * Revision 1.18 2003/10/31 02:27:05 mcr
  3061. + * pulled up port-selector patches and sa_id elimination.
  3062. + *
  3063. + * Revision 1.17.30.1 2003/09/21 13:59:38 mcr
  3064. + * pre-liminary X.509 patch - does not yet pass tests.
  3065. + *
  3066. + * Revision 1.17 2002/04/24 07:36:46 mcr
  3067. + * Moved from ./klips/net/ipsec/ipsec_encap.h,v
  3068. + *
  3069. + * Revision 1.16 2001/11/26 09:23:47 rgb
  3070. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  3071. + *
  3072. + * Revision 1.15.2.1 2001/09/25 02:18:54 mcr
  3073. + * struct eroute moved to ipsec_eroute.h
  3074. + *
  3075. + * Revision 1.15 2001/09/14 16:58:36 rgb
  3076. + * Added support for storing the first and last packets through a HOLD.
  3077. + *
  3078. + * Revision 1.14 2001/09/08 21:13:31 rgb
  3079. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  3080. + *
  3081. + * Revision 1.13 2001/06/14 19:35:08 rgb
  3082. + * Update copyright date.
  3083. + *
  3084. + * Revision 1.12 2001/05/27 06:12:10 rgb
  3085. + * Added structures for pid, packet count and last access time to eroute.
  3086. + * Added packet count to beginning of /proc/net/ipsec_eroute.
  3087. + *
  3088. + * Revision 1.11 2000/09/08 19:12:56 rgb
  3089. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  3090. + *
  3091. + * Revision 1.10 2000/03/22 16:15:36 rgb
  3092. + * Fixed renaming of dev_get (MB).
  3093. + *
  3094. + * Revision 1.9 2000/01/21 06:13:26 rgb
  3095. + * Added a macro for AF_ENCAP
  3096. + *
  3097. + * Revision 1.8 1999/12/31 14:56:55 rgb
  3098. + * MB fix for 2.3 dev-use-count.
  3099. + *
  3100. + * Revision 1.7 1999/11/18 04:09:18 rgb
  3101. + * Replaced all kernel version macros to shorter, readable form.
  3102. + *
  3103. + * Revision 1.6 1999/09/24 00:34:13 rgb
  3104. + * Add Marc Boucher's support for 2.3.xx+.
  3105. + *
  3106. + * Revision 1.5 1999/04/11 00:28:57 henry
  3107. + * GPL boilerplate
  3108. + *
  3109. + * Revision 1.4 1999/04/06 04:54:25 rgb
  3110. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  3111. + * patch shell fixes.
  3112. + *
  3113. + * Revision 1.3 1998/10/19 14:44:28 rgb
  3114. + * Added inclusion of freeswan.h.
  3115. + * sa_id structure implemented and used: now includes protocol.
  3116. + *
  3117. + * Revision 1.2 1998/07/14 18:19:33 rgb
  3118. + * Added #ifdef __KERNEL__ directives to restrict scope of header.
  3119. + *
  3120. + * Revision 1.1 1998/06/18 21:27:44 henry
  3121. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  3122. + * kernel-build scripts happier in the presence of symlinks
  3123. + *
  3124. + * Revision 1.2 1998/04/21 21:29:10 rgb
  3125. + * Rearrange debug switches to change on the fly debug output from user
  3126. + * space. Only kernel changes checked in at this time. radij.c was also
  3127. + * changed to temporarily remove buggy debugging code in rj_delete causing
  3128. + * an OOPS and hence, netlink device open errors.
  3129. + *
  3130. + * Revision 1.1 1998/04/09 03:05:58 henry
  3131. + * sources moved up from linux/net/ipsec
  3132. + *
  3133. + * Revision 1.1.1.1 1998/04/08 05:35:02 henry
  3134. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  3135. + *
  3136. + * Revision 0.4 1997/01/15 01:28:15 ji
  3137. + * Minor cosmetic changes.
  3138. + *
  3139. + * Revision 0.3 1996/11/20 14:35:48 ji
  3140. + * Minor Cleanup.
  3141. + * Rationalized debugging code.
  3142. + *
  3143. + * Revision 0.2 1996/11/02 00:18:33 ji
  3144. + * First limited release.
  3145. + *
  3146. + *
  3147. + */
  3148. --- /dev/null Tue Mar 11 13:02:56 2003
  3149. +++ linux/include/openswan/ipsec_eroute.h Mon Feb 9 13:51:03 2004
  3150. @@ -0,0 +1,112 @@
  3151. +/*
  3152. + * @(#) declarations of eroute structures
  3153. + *
  3154. + * Copyright (C) 1996, 1997 John Ioannidis.
  3155. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs <rgb@freeswan.org>
  3156. + * Copyright (C) 2001 Michael Richardson <mcr@freeswan.org>
  3157. + *
  3158. + * This program is free software; you can redistribute it and/or modify it
  3159. + * under the terms of the GNU General Public License as published by the
  3160. + * Free Software Foundation; either version 2 of the License, or (at your
  3161. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  3162. + *
  3163. + * This program is distributed in the hope that it will be useful, but
  3164. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  3165. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  3166. + * for more details.
  3167. + *
  3168. + * RCSID $Id: ipsec_eroute.h,v 1.5 2004/04/05 19:55:05 mcr Exp $
  3169. + *
  3170. + * derived from ipsec_encap.h 1.15 on 2001/9/18 by mcr.
  3171. + *
  3172. + */
  3173. +
  3174. +#ifndef _IPSEC_EROUTE_H_
  3175. +
  3176. +#include "radij.h"
  3177. +#include "ipsec_encap.h"
  3178. +#include "ipsec_radij.h"
  3179. +
  3180. +/*
  3181. + * The "type" is really part of the address as far as the routing
  3182. + * system is concerned. By using only one bit in the type field
  3183. + * for each type, we sort-of make sure that different types of
  3184. + * encapsulation addresses won't be matched against the wrong type.
  3185. + */
  3186. +
  3187. +/*
  3188. + * An entry in the radix tree
  3189. + */
  3190. +
  3191. +struct rjtentry
  3192. +{
  3193. + struct radij_node rd_nodes[2]; /* tree glue, and other values */
  3194. +#define rd_key(r) ((struct sockaddr_encap *)((r)->rd_nodes->rj_key))
  3195. +#define rd_mask(r) ((struct sockaddr_encap *)((r)->rd_nodes->rj_mask))
  3196. + short rd_flags;
  3197. + short rd_count;
  3198. +};
  3199. +
  3200. +struct ident
  3201. +{
  3202. + __u16 type; /* identity type */
  3203. + __u64 id; /* identity id */
  3204. + __u8 len; /* identity len */
  3205. + caddr_t data; /* identity data */
  3206. +};
  3207. +
  3208. +/*
  3209. + * An encapsulation route consists of a pointer to a
  3210. + * radix tree entry and a SAID (a destination_address/SPI/protocol triple).
  3211. + */
  3212. +
  3213. +struct eroute
  3214. +{
  3215. + struct rjtentry er_rjt;
  3216. + ip_said er_said;
  3217. + uint32_t er_pid;
  3218. + uint32_t er_count;
  3219. + uint64_t er_lasttime;
  3220. + struct sockaddr_encap er_eaddr; /* MCR get rid of _encap, it is silly*/
  3221. + struct sockaddr_encap er_emask;
  3222. + struct ident er_ident_s;
  3223. + struct ident er_ident_d;
  3224. + struct sk_buff* er_first;
  3225. + struct sk_buff* er_last;
  3226. +};
  3227. +
  3228. +#define er_dst er_said.dst
  3229. +#define er_spi er_said.spi
  3230. +
  3231. +#define _IPSEC_EROUTE_H_
  3232. +#endif /* _IPSEC_EROUTE_H_ */
  3233. +
  3234. +/*
  3235. + * $Log: ipsec_eroute.h,v $
  3236. + * Revision 1.5 2004/04/05 19:55:05 mcr
  3237. + * Moved from linux/include/freeswan/ipsec_eroute.h,v
  3238. + *
  3239. + * Revision 1.4 2003/10/31 02:27:05 mcr
  3240. + * pulled up port-selector patches and sa_id elimination.
  3241. + *
  3242. + * Revision 1.3.30.2 2003/10/29 01:10:19 mcr
  3243. + * elimited "struct sa_id"
  3244. + *
  3245. + * Revision 1.3.30.1 2003/09/21 13:59:38 mcr
  3246. + * pre-liminary X.509 patch - does not yet pass tests.
  3247. + *
  3248. + * Revision 1.3 2002/04/24 07:36:46 mcr
  3249. + * Moved from ./klips/net/ipsec/ipsec_eroute.h,v
  3250. + *
  3251. + * Revision 1.2 2001/11/26 09:16:13 rgb
  3252. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  3253. + *
  3254. + * Revision 1.1.2.1 2001/09/25 02:18:54 mcr
  3255. + * struct eroute moved to ipsec_eroute.h
  3256. + *
  3257. + *
  3258. + * Local variables:
  3259. + * c-file-style: "linux"
  3260. + * End:
  3261. + *
  3262. + */
  3263. --- /dev/null Tue Mar 11 13:02:56 2003
  3264. +++ linux/include/openswan/ipsec_errs.h Mon Feb 9 13:51:03 2004
  3265. @@ -0,0 +1,53 @@
  3266. +/*
  3267. + * @(#) definition of ipsec_errs structure
  3268. + *
  3269. + * Copyright (C) 2001 Richard Guy Briggs <rgb@freeswan.org>
  3270. + * and Michael Richardson <mcr@freeswan.org>
  3271. + *
  3272. + * This program is free software; you can redistribute it and/or modify it
  3273. + * under the terms of the GNU General Public License as published by the
  3274. + * Free Software Foundation; either version 2 of the License, or (at your
  3275. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  3276. + *
  3277. + * This program is distributed in the hope that it will be useful, but
  3278. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  3279. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  3280. + * for more details.
  3281. + *
  3282. + * RCSID $Id: ipsec_errs.h,v 1.4 2004/04/05 19:55:05 mcr Exp $
  3283. + *
  3284. + */
  3285. +
  3286. +/*
  3287. + * This file describes the errors/statistics that FreeSWAN collects.
  3288. + *
  3289. + */
  3290. +
  3291. +struct ipsec_errs {
  3292. + __u32 ips_alg_errs; /* number of algorithm errors */
  3293. + __u32 ips_auth_errs; /* # of authentication errors */
  3294. + __u32 ips_encsize_errs; /* # of encryption size errors*/
  3295. + __u32 ips_encpad_errs; /* # of encryption pad errors*/
  3296. + __u32 ips_replaywin_errs; /* # of pkt sequence errors */
  3297. +};
  3298. +
  3299. +/*
  3300. + * $Log: ipsec_errs.h,v $
  3301. + * Revision 1.4 2004/04/05 19:55:05 mcr
  3302. + * Moved from linux/include/freeswan/ipsec_errs.h,v
  3303. + *
  3304. + * Revision 1.3 2002/04/24 07:36:46 mcr
  3305. + * Moved from ./klips/net/ipsec/ipsec_errs.h,v
  3306. + *
  3307. + * Revision 1.2 2001/11/26 09:16:13 rgb
  3308. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  3309. + *
  3310. + * Revision 1.1.2.1 2001/09/25 02:25:57 mcr
  3311. + * lifetime structure created and common functions created.
  3312. + *
  3313. + *
  3314. + * Local variables:
  3315. + * c-file-style: "linux"
  3316. + * End:
  3317. + *
  3318. + */
  3319. --- /dev/null Tue Mar 11 13:02:56 2003
  3320. +++ linux/include/openswan/ipsec_esp.h Mon Feb 9 13:51:03 2004
  3321. @@ -0,0 +1,157 @@
  3322. +/*
  3323. + * Copyright (C) 1996, 1997 John Ioannidis.
  3324. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  3325. + *
  3326. + * This program is free software; you can redistribute it and/or modify it
  3327. + * under the terms of the GNU General Public License as published by the
  3328. + * Free Software Foundation; either version 2 of the License, or (at your
  3329. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  3330. + *
  3331. + * This program is distributed in the hope that it will be useful, but
  3332. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  3333. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  3334. + * for more details.
  3335. + *
  3336. + * RCSID $Id: ipsec_esp.h,v 1.28 2004/09/13 02:22:10 mcr Exp $
  3337. + */
  3338. +
  3339. +#include "openswan/ipsec_md5h.h"
  3340. +#include "openswan/ipsec_sha1.h"
  3341. +
  3342. +#include "crypto/des.h"
  3343. +
  3344. +#ifndef IPPROTO_ESP
  3345. +#define IPPROTO_ESP 50
  3346. +#endif /* IPPROTO_ESP */
  3347. +
  3348. +#define ESP_HEADER_LEN 8 /* 64 bits header (spi+rpl)*/
  3349. +
  3350. +#define EMT_ESPDESCBC_ULEN 20 /* coming from user mode */
  3351. +#define EMT_ESPDES_KMAX 64 /* 512 bit secret key enough? */
  3352. +#define EMT_ESPDES_KEY_SZ 8 /* 56 bit secret key with parity = 64 bits */
  3353. +#define EMT_ESP3DES_KEY_SZ 24 /* 168 bit secret key with parity = 192 bits */
  3354. +#define EMT_ESPDES_IV_SZ 8 /* IV size */
  3355. +#define ESP_DESCBC_BLKLEN 8 /* DES-CBC block size */
  3356. +
  3357. +#define ESP_IV_MAXSZ 16 /* This is _critical_ */
  3358. +#define ESP_IV_MAXSZ_INT (ESP_IV_MAXSZ/sizeof(int))
  3359. +
  3360. +#define DB_ES_PKTRX 0x0001
  3361. +#define DB_ES_PKTRX2 0x0002
  3362. +#define DB_ES_IPSA 0x0010
  3363. +#define DB_ES_XF 0x0020
  3364. +#define DB_ES_IPAD 0x0040
  3365. +#define DB_ES_INAU 0x0080
  3366. +#define DB_ES_OINFO 0x0100
  3367. +#define DB_ES_OINFO2 0x0200
  3368. +#define DB_ES_OH 0x0400
  3369. +#define DB_ES_REPLAY 0x0800
  3370. +
  3371. +#ifdef __KERNEL__
  3372. +struct des_eks {
  3373. + des_key_schedule ks;
  3374. +};
  3375. +
  3376. +extern struct inet_protocol esp_protocol;
  3377. +
  3378. +struct options;
  3379. +
  3380. +struct esphdr
  3381. +{
  3382. + __u32 esp_spi; /* Security Parameters Index */
  3383. + __u32 esp_rpl; /* Replay counter */
  3384. + __u8 esp_iv[8]; /* iv */
  3385. +};
  3386. +
  3387. +extern struct xform_functions esp_xform_funcs[];
  3388. +
  3389. +#ifdef CONFIG_KLIPS_DEBUG
  3390. +extern int debug_esp;
  3391. +#endif /* CONFIG_KLIPS_DEBUG */
  3392. +#endif /* __KERNEL__ */
  3393. +
  3394. +/*
  3395. + * $Log: ipsec_esp.h,v $
  3396. + * Revision 1.28 2004/09/13 02:22:10 mcr
  3397. + * #define inet_protocol if necessary.
  3398. + *
  3399. + * Revision 1.27 2004/09/06 18:35:41 mcr
  3400. + * 2.6.8.1 gets rid of inet_protocol->net_protocol compatibility,
  3401. + * so adjust for that.
  3402. + *
  3403. + * Revision 1.26 2004/07/10 19:08:41 mcr
  3404. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  3405. + *
  3406. + * Revision 1.25 2004/04/06 02:49:08 mcr
  3407. + * pullup of algo code from alg-branch.
  3408. + *
  3409. + * Revision 1.24 2004/04/05 19:55:05 mcr
  3410. + * Moved from linux/include/freeswan/ipsec_esp.h,v
  3411. + *
  3412. + * Revision 1.23 2004/04/05 19:41:05 mcr
  3413. + * merged alg-branch code.
  3414. + *
  3415. + * Revision 1.22 2003/12/13 19:10:16 mcr
  3416. + * refactored rcv and xmit code - same as FS 2.05.
  3417. + *
  3418. + * Revision 1.23 2003/12/11 20:14:58 mcr
  3419. + * refactored the xmit code, to move all encapsulation
  3420. + * code into protocol functions. Note that all functions
  3421. + * are essentially done by a single function, which is probably
  3422. + * wrong.
  3423. + * the rcv_functions structures are renamed xform_functions.
  3424. + *
  3425. + * Revision 1.22 2003/12/06 21:21:19 mcr
  3426. + * split up receive path into per-transform files, for
  3427. + * easier later removal.
  3428. + *
  3429. + * Revision 1.21.8.1 2003/12/22 15:25:52 jjo
  3430. + * Merged algo-0.8.1-rc11-test1 into alg-branch
  3431. + *
  3432. + * Revision 1.21 2003/02/06 02:21:34 rgb
  3433. + *
  3434. + * Moved "struct auth_alg" from ipsec_rcv.c to ipsec_ah.h .
  3435. + * Changed "struct ah" to "struct ahhdr" and "struct esp" to "struct esphdr".
  3436. + * Removed "#ifdef INBOUND_POLICY_CHECK_eroute" dead code.
  3437. + *
  3438. + * Revision 1.20 2002/05/14 02:37:02 rgb
  3439. + * Change reference from _TDB to _IPSA.
  3440. + *
  3441. + * Revision 1.19 2002/04/24 07:55:32 mcr
  3442. + * #include patches and Makefiles for post-reorg compilation.
  3443. + *
  3444. + * Revision 1.18 2002/04/24 07:36:46 mcr
  3445. + * Moved from ./klips/net/ipsec/ipsec_esp.h,v
  3446. + *
  3447. + * Revision 1.17 2002/02/20 01:27:07 rgb
  3448. + * Ditched a pile of structs only used by the old Netlink interface.
  3449. + *
  3450. + * Revision 1.16 2001/12/11 02:35:57 rgb
  3451. + * Change "struct net_device" to "struct device" for 2.2 compatibility.
  3452. + *
  3453. + * Revision 1.15 2001/11/26 09:23:48 rgb
  3454. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  3455. + *
  3456. + * Revision 1.14.2.3 2001/10/23 04:16:42 mcr
  3457. + * get definition of des_key_schedule from des.h
  3458. + *
  3459. + * Revision 1.14.2.2 2001/10/22 20:33:13 mcr
  3460. + * use "des_key_schedule" structure instead of cooking our own.
  3461. + *
  3462. + * Revision 1.14.2.1 2001/09/25 02:18:25 mcr
  3463. + * replace "struct device" with "struct netdevice"
  3464. + *
  3465. + * Revision 1.14 2001/06/14 19:35:08 rgb
  3466. + * Update copyright date.
  3467. + *
  3468. + * Revision 1.13 2000/09/08 19:12:56 rgb
  3469. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  3470. + *
  3471. + * Revision 1.12 2000/08/01 14:51:50 rgb
  3472. + * Removed _all_ remaining traces of DES.
  3473. + *
  3474. + * Revision 1.11 2000/01/10 16:36:20 rgb
  3475. + * Ditch last of EME option flags, including initiator.
  3476. + *
  3477. + *
  3478. + */
  3479. --- /dev/null Tue Mar 11 13:02:56 2003
  3480. +++ linux/include/openswan/ipsec_ipcomp.h Mon Feb 9 13:51:03 2004
  3481. @@ -0,0 +1,94 @@
  3482. +/*
  3483. + * IP compression header declations
  3484. + *
  3485. + * Copyright (C) 2003 Michael Richardson <mcr@sandelman.ottawa.on.ca>
  3486. + *
  3487. + * This program is free software; you can redistribute it and/or modify it
  3488. + * under the terms of the GNU General Public License as published by the
  3489. + * Free Software Foundation; either version 2 of the License, or (at your
  3490. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  3491. + *
  3492. + * This program is distributed in the hope that it will be useful, but
  3493. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  3494. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  3495. + * for more details.
  3496. + *
  3497. + * RCSID $Id: ipsec_ipcomp.h,v 1.4 2004/07/10 19:08:41 mcr Exp $
  3498. + */
  3499. +
  3500. +#ifndef IPSEC_IPCOMP_H
  3501. +#define IPSEC_IPCOMP_H
  3502. +
  3503. +#include "openswan/ipsec_auth.h"
  3504. +
  3505. +/* Prefix all global deflate symbols with "ipcomp_" to avoid collisions with ppp_deflate & ext2comp */
  3506. +#ifndef IPCOMP_PREFIX
  3507. +#define IPCOMP_PREFIX
  3508. +#endif /* IPCOMP_PREFIX */
  3509. +
  3510. +#ifndef IPPROTO_COMP
  3511. +#define IPPROTO_COMP 108
  3512. +#endif /* IPPROTO_COMP */
  3513. +
  3514. +#ifdef CONFIG_KLIPS_DEBUG
  3515. +extern int sysctl_ipsec_debug_ipcomp;
  3516. +#endif /* CONFIG_KLIPS_DEBUG */
  3517. +
  3518. +struct ipcomphdr { /* IPCOMP header */
  3519. + __u8 ipcomp_nh; /* Next header (protocol) */
  3520. + __u8 ipcomp_flags; /* Reserved, must be 0 */
  3521. + __u16 ipcomp_cpi; /* Compression Parameter Index */
  3522. +};
  3523. +
  3524. +extern struct inet_protocol comp_protocol;
  3525. +extern int sysctl_ipsec_debug_ipcomp;
  3526. +
  3527. +#define IPCOMP_UNCOMPRESSABLE 0x000000001
  3528. +#define IPCOMP_COMPRESSIONERROR 0x000000002
  3529. +#define IPCOMP_PARMERROR 0x000000004
  3530. +#define IPCOMP_DECOMPRESSIONERROR 0x000000008
  3531. +
  3532. +#define IPCOMP_ADAPT_INITIAL_TRIES 8
  3533. +#define IPCOMP_ADAPT_INITIAL_SKIP 4
  3534. +#define IPCOMP_ADAPT_SUBSEQ_TRIES 2
  3535. +#define IPCOMP_ADAPT_SUBSEQ_SKIP 8
  3536. +
  3537. +/* Function prototypes */
  3538. +struct sk_buff *skb_compress(struct sk_buff *skb, struct ipsec_sa *ips, unsigned int *flags);
  3539. +struct sk_buff *skb_decompress(struct sk_buff *skb, struct ipsec_sa *ips, unsigned int *flags);
  3540. +
  3541. +extern struct xform_functions ipcomp_xform_funcs[];
  3542. +
  3543. +#endif /* IPSEC_IPCOMP_H */
  3544. +
  3545. +/*
  3546. + * $Log: ipsec_ipcomp.h,v $
  3547. + * Revision 1.4 2004/07/10 19:08:41 mcr
  3548. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  3549. + *
  3550. + * Revision 1.3 2004/04/06 02:49:08 mcr
  3551. + * pullup of algo code from alg-branch.
  3552. + *
  3553. + * Revision 1.2 2004/04/05 19:55:05 mcr
  3554. + * Moved from linux/include/freeswan/ipsec_ipcomp.h,v
  3555. + *
  3556. + * Revision 1.1 2003/12/13 19:10:16 mcr
  3557. + * refactored rcv and xmit code - same as FS 2.05.
  3558. + *
  3559. + * Revision 1.2 2003/12/11 20:14:58 mcr
  3560. + * refactored the xmit code, to move all encapsulation
  3561. + * code into protocol functions. Note that all functions
  3562. + * are essentially done by a single function, which is probably
  3563. + * wrong.
  3564. + * the rcv_functions structures are renamed xform_functions.
  3565. + *
  3566. + * Revision 1.1 2003/12/06 21:21:19 mcr
  3567. + * split up receive path into per-transform files, for
  3568. + * easier later removal.
  3569. + *
  3570. + *
  3571. + *
  3572. + */
  3573. +
  3574. +
  3575. +
  3576. --- /dev/null Tue Mar 11 13:02:56 2003
  3577. +++ linux/include/openswan/ipsec_ipe4.h Mon Feb 9 13:51:03 2004
  3578. @@ -0,0 +1,68 @@
  3579. +/*
  3580. + * IP-in-IP Header declarations
  3581. + * Copyright (C) 1996, 1997 John Ioannidis.
  3582. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  3583. + *
  3584. + * This program is free software; you can redistribute it and/or modify it
  3585. + * under the terms of the GNU General Public License as published by the
  3586. + * Free Software Foundation; either version 2 of the License, or (at your
  3587. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  3588. + *
  3589. + * This program is distributed in the hope that it will be useful, but
  3590. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  3591. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  3592. + * for more details.
  3593. + *
  3594. + * RCSID $Id: ipsec_ipe4.h,v 1.6 2004/04/05 19:55:05 mcr Exp $
  3595. + */
  3596. +
  3597. +/* The packet header is an IP header! */
  3598. +
  3599. +struct ipe4_xdata /* transform table data */
  3600. +{
  3601. + struct in_addr i4_src;
  3602. + struct in_addr i4_dst;
  3603. +};
  3604. +
  3605. +#define EMT_IPE4_ULEN 8 /* coming from user mode */
  3606. +
  3607. +
  3608. +/*
  3609. + * $Log: ipsec_ipe4.h,v $
  3610. + * Revision 1.6 2004/04/05 19:55:05 mcr
  3611. + * Moved from linux/include/freeswan/ipsec_ipe4.h,v
  3612. + *
  3613. + * Revision 1.5 2002/04/24 07:36:46 mcr
  3614. + * Moved from ./klips/net/ipsec/ipsec_ipe4.h,v
  3615. + *
  3616. + * Revision 1.4 2001/06/14 19:35:08 rgb
  3617. + * Update copyright date.
  3618. + *
  3619. + * Revision 1.3 1999/04/11 00:28:57 henry
  3620. + * GPL boilerplate
  3621. + *
  3622. + * Revision 1.2 1999/04/06 04:54:25 rgb
  3623. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  3624. + * patch shell fixes.
  3625. + *
  3626. + * Revision 1.1 1998/06/18 21:27:47 henry
  3627. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  3628. + * kernel-build scripts happier in the presence of symlinks
  3629. + *
  3630. + * Revision 1.1 1998/04/09 03:06:07 henry
  3631. + * sources moved up from linux/net/ipsec
  3632. + *
  3633. + * Revision 1.1.1.1 1998/04/08 05:35:03 henry
  3634. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  3635. + *
  3636. + * Revision 0.4 1997/01/15 01:28:15 ji
  3637. + * No changes.
  3638. + *
  3639. + * Revision 0.3 1996/11/20 14:48:53 ji
  3640. + * Release update only.
  3641. + *
  3642. + * Revision 0.2 1996/11/02 00:18:33 ji
  3643. + * First limited release.
  3644. + *
  3645. + *
  3646. + */
  3647. --- /dev/null Tue Mar 11 13:02:56 2003
  3648. +++ linux/include/openswan/ipsec_ipip.h Mon Feb 9 13:51:03 2004
  3649. @@ -0,0 +1,45 @@
  3650. +/*
  3651. + * Copyright (C) 2003 Michael Richardson <mcr@sandelman.ottawa.on.ca>
  3652. + *
  3653. + * This program is free software; you can redistribute it and/or modify it
  3654. + * under the terms of the GNU General Public License as published by the
  3655. + * Free Software Foundation; either version 2 of the License, or (at your
  3656. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  3657. + *
  3658. + * This program is distributed in the hope that it will be useful, but
  3659. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  3660. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  3661. + * for more details.
  3662. + *
  3663. + * RCSID $Id: ipsec_ipip.h,v 1.2 2004/04/05 19:55:05 mcr Exp $
  3664. + */
  3665. +
  3666. +#ifndef _IPSEC_IPIP_H_
  3667. +
  3668. +#ifndef IPPROTO_IPIP
  3669. +#define IPPROTO_IPIP 4
  3670. +#endif /* IPPROTO_ESP */
  3671. +
  3672. +extern struct xform_functions ipip_xform_funcs[];
  3673. +
  3674. +#define _IPSEC_IPIP_H_
  3675. +
  3676. +#endif /* _IPSEC_IPIP_H_ */
  3677. +
  3678. +/*
  3679. + * $Log: ipsec_ipip.h,v $
  3680. + * Revision 1.2 2004/04/05 19:55:05 mcr
  3681. + * Moved from linux/include/freeswan/ipsec_ipip.h,v
  3682. + *
  3683. + * Revision 1.1 2003/12/13 19:10:16 mcr
  3684. + * refactored rcv and xmit code - same as FS 2.05.
  3685. + *
  3686. + * Revision 1.1 2003/12/11 20:14:58 mcr
  3687. + * refactored the xmit code, to move all encapsulation
  3688. + * code into protocol functions. Note that all functions
  3689. + * are essentially done by a single function, which is probably
  3690. + * wrong.
  3691. + * the rcv_functions structures are renamed xform_functions.
  3692. + *
  3693. + *
  3694. + */
  3695. --- /dev/null Tue Mar 11 13:02:56 2003
  3696. +++ linux/include/openswan/ipsec_kern24.h Mon Feb 9 13:51:03 2004
  3697. @@ -0,0 +1,61 @@
  3698. +/*
  3699. + * @(#) routines to makes kernel 2.4 compatible with 2.6 usage.
  3700. + *
  3701. + * Copyright (C) 2004 Michael Richardson <mcr@sandelman.ottawa.on.ca>
  3702. + *
  3703. + * This program is free software; you can redistribute it and/or modify it
  3704. + * under the terms of the GNU General Public License as published by the
  3705. + * Free Software Foundation; either version 2 of the License, or (at your
  3706. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  3707. + *
  3708. + * This program is distributed in the hope that it will be useful, but
  3709. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  3710. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  3711. + * for more details.
  3712. + *
  3713. + * RCSID $Id: ipsec_kern24.h,v 1.4 2005/05/20 03:19:18 mcr Exp $
  3714. + */
  3715. +
  3716. +#ifndef _IPSEC_KERN24_H
  3717. +
  3718. +#ifndef NET_26
  3719. +#define sk_receive_queue receive_queue
  3720. +#define sk_destruct destruct
  3721. +#define sk_reuse reuse
  3722. +#define sk_zapped zapped
  3723. +#define sk_family family
  3724. +#define sk_protocol protocol
  3725. +#define sk_protinfo protinfo
  3726. +#define sk_sleep sleep
  3727. +#define sk_state_change state_change
  3728. +#define sk_shutdown shutdown
  3729. +#define sk_err err
  3730. +#define sk_stamp stamp
  3731. +#define sk_socket socket
  3732. +#define sk_sndbuf sndbuf
  3733. +#define sock_flag(sk, flag) sk->dead
  3734. +#define sk_for_each(sk, node, plist) for(sk=*plist; sk!=NULL; sk = sk->next)
  3735. +#endif
  3736. +
  3737. +/* deal with 2.4 vs 2.6 issues with module counts */
  3738. +
  3739. +/* in 2.6, all refcounts are maintained *outside* of the
  3740. + * module to deal with race conditions.
  3741. + */
  3742. +
  3743. +#ifdef NET_26
  3744. +#define KLIPS_INC_USE /* nothing */
  3745. +#define KLIPS_DEC_USE /* nothing */
  3746. +
  3747. +#else
  3748. +#define KLIPS_INC_USE MOD_INC_USE_COUNT
  3749. +#define KLIPS_DEC_USE MOD_DEC_USE_COUNT
  3750. +#endif
  3751. +
  3752. +extern int printk_ratelimit(void);
  3753. +
  3754. +
  3755. +#define _IPSEC_KERN24_H 1
  3756. +
  3757. +#endif /* _IPSEC_KERN24_H */
  3758. +
  3759. --- /dev/null Tue Mar 11 13:02:56 2003
  3760. +++ linux/include/openswan/ipsec_kversion.h Mon Feb 9 13:51:03 2004
  3761. @@ -0,0 +1,352 @@
  3762. +#ifndef _OPENSWAN_KVERSIONS_H
  3763. +/*
  3764. + * header file for FreeS/WAN library functions
  3765. + * Copyright (C) 1998, 1999, 2000 Henry Spencer.
  3766. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs
  3767. + *
  3768. + * This library is free software; you can redistribute it and/or modify it
  3769. + * under the terms of the GNU Library General Public License as published by
  3770. + * the Free Software Foundation; either version 2 of the License, or (at your
  3771. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  3772. + *
  3773. + * This library is distributed in the hope that it will be useful, but
  3774. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  3775. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  3776. + * License for more details.
  3777. + *
  3778. + * RCSID $Id: ipsec_kversion.h,v 1.15.2.11 2007/02/20 03:53:16 paul Exp $
  3779. + */
  3780. +#define _OPENSWAN_KVERSIONS_H /* seen it, no need to see it again */
  3781. +
  3782. +/*
  3783. + * this file contains a series of atomic defines that depend upon
  3784. + * kernel version numbers. The kernel versions are arranged
  3785. + * in version-order number (which is often not chronological)
  3786. + * and each clause enables or disables a feature.
  3787. + */
  3788. +
  3789. +/*
  3790. + * First, assorted kernel-version-dependent trickery.
  3791. + */
  3792. +#include <linux/version.h>
  3793. +#ifndef KERNEL_VERSION
  3794. +#define KERNEL_VERSION(x,y,z) (((x)<<16)+((y)<<8)+(z))
  3795. +#endif
  3796. +
  3797. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,0)
  3798. +#define HEADER_CACHE_BIND_21
  3799. +#error "KLIPS is no longer supported on Linux 2.0. Sorry"
  3800. +#endif
  3801. +
  3802. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)
  3803. +#define SPINLOCK
  3804. +#define PROC_FS_21
  3805. +#define NETLINK_SOCK
  3806. +#define NET_21
  3807. +#endif
  3808. +
  3809. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,1,19)
  3810. +#define net_device_stats enet_statistics
  3811. +#endif
  3812. +
  3813. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
  3814. +#define SPINLOCK_23
  3815. +#define NETDEV_23
  3816. +# ifndef CONFIG_IP_ALIAS
  3817. +# define CONFIG_IP_ALIAS
  3818. +# endif
  3819. +#include <linux/socket.h>
  3820. +#include <linux/skbuff.h>
  3821. +#include <linux/netlink.h>
  3822. +# ifdef NETLINK_XFRM
  3823. +# define NETDEV_25
  3824. +# endif
  3825. +#endif
  3826. +
  3827. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,25)
  3828. +#define PROC_FS_2325
  3829. +#undef PROC_FS_21
  3830. +#endif
  3831. +
  3832. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,30)
  3833. +#define PROC_NO_DUMMY
  3834. +#endif
  3835. +
  3836. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,35)
  3837. +#define SKB_COPY_EXPAND
  3838. +#endif
  3839. +
  3840. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,37)
  3841. +#define IP_SELECT_IDENT
  3842. +#endif
  3843. +
  3844. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,50)) && defined(CONFIG_NETFILTER)
  3845. +#define SKB_RESET_NFCT
  3846. +#endif
  3847. +
  3848. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,2)
  3849. +#define IP_SELECT_IDENT_NEW
  3850. +#endif
  3851. +
  3852. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,4)
  3853. +#define IPH_is_SKB_PULLED
  3854. +#define SKB_COW_NEW
  3855. +#define PROTO_HANDLER_SINGLE_PARM
  3856. +#define IP_FRAGMENT_LINEARIZE 1
  3857. +#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,4) */
  3858. +# ifdef REDHAT_BOGOSITY
  3859. +# define IP_SELECT_IDENT_NEW
  3860. +# define IPH_is_SKB_PULLED
  3861. +# define SKB_COW_NEW
  3862. +# define PROTO_HANDLER_SINGLE_PARM
  3863. +# endif /* REDHAT_BOGOSITY */
  3864. +#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,4) */
  3865. +
  3866. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,9)
  3867. +#define MALLOC_SLAB
  3868. +#define LINUX_KERNEL_HAS_SNPRINTF
  3869. +#endif
  3870. +
  3871. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
  3872. +#define HAVE_NETDEV_PRINTK 1
  3873. +#define NET_26
  3874. +#endif
  3875. +
  3876. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,8)
  3877. +#define NEED_INET_PROTOCOL
  3878. +#endif
  3879. +
  3880. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
  3881. +#define HAVE_SOCK_ZAPPED
  3882. +#define NET_26_12_SKALLOC
  3883. +#endif
  3884. +
  3885. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,13)
  3886. +#define HAVE_SOCK_SECURITY
  3887. +/* skb->nf_debug disappared completely in 2.6.13 */
  3888. +#define HAVE_SKB_NF_DEBUG
  3889. +#endif
  3890. +
  3891. +#define SYSCTL_IPSEC_DEFAULT_TTL sysctl_ip_default_ttl
  3892. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
  3893. +/* skb->stamp changed to skb->tstamp in 2.6.14 */
  3894. +#define HAVE_TSTAMP
  3895. +#define HAVE_INET_SK_SPORT
  3896. +#undef SYSCTL_IPSEC_DEFAULT_TTL
  3897. +#define SYSCTL_IPSEC_DEFAULT_TTL IPSEC_DEFAULT_TTL
  3898. +#else
  3899. +#define HAVE_SKB_LIST
  3900. +#endif
  3901. +
  3902. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,18)
  3903. +#define HAVE_NEW_SKB_LINEARIZE
  3904. +#endif
  3905. +
  3906. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  3907. +/* skb->nfmark changed to skb->mark in 2.6.20 */
  3908. +#define nfmark mark
  3909. +#endif
  3910. +
  3911. +#ifdef NET_21
  3912. +# include <linux/in6.h>
  3913. +#else
  3914. + /* old kernel in.h has some IPv6 stuff, but not quite enough */
  3915. +# define s6_addr16 s6_addr
  3916. +# define AF_INET6 10
  3917. +# define uint8_t __u8
  3918. +# define uint16_t __u16
  3919. +# define uint32_t __u32
  3920. +# define uint64_t __u64
  3921. +#endif
  3922. +
  3923. +#ifdef NET_21
  3924. +# define ipsec_kfree_skb(a) kfree_skb(a)
  3925. +#else /* NET_21 */
  3926. +# define ipsec_kfree_skb(a) kfree_skb(a, FREE_WRITE)
  3927. +#endif /* NET_21 */
  3928. +
  3929. +#ifdef NETDEV_23
  3930. +#if 0
  3931. +#ifndef NETDEV_25
  3932. +#define device net_device
  3933. +#endif
  3934. +#endif
  3935. +# define ipsec_dev_get dev_get_by_name
  3936. +# define __ipsec_dev_get __dev_get_by_name
  3937. +# define ipsec_dev_put(x) dev_put(x)
  3938. +# define __ipsec_dev_put(x) __dev_put(x)
  3939. +# define ipsec_dev_hold(x) dev_hold(x)
  3940. +#else /* NETDEV_23 */
  3941. +# define ipsec_dev_get dev_get
  3942. +# define __ipsec_dev_put(x)
  3943. +# define ipsec_dev_put(x)
  3944. +# define ipsec_dev_hold(x)
  3945. +#endif /* NETDEV_23 */
  3946. +
  3947. +#ifndef SPINLOCK
  3948. +# include <linux/bios32.h>
  3949. + /* simulate spin locks and read/write locks */
  3950. + typedef struct {
  3951. + volatile char lock;
  3952. + } spinlock_t;
  3953. +
  3954. + typedef struct {
  3955. + volatile unsigned int lock;
  3956. + } rwlock_t;
  3957. +
  3958. +# define spin_lock_init(x) { (x)->lock = 0;}
  3959. +# define rw_lock_init(x) { (x)->lock = 0; }
  3960. +
  3961. +# define spin_lock(x) { while ((x)->lock) barrier(); (x)->lock=1;}
  3962. +# define spin_lock_irq(x) { cli(); spin_lock(x);}
  3963. +# define spin_lock_irqsave(x,flags) { save_flags(flags); spin_lock_irq(x);}
  3964. +
  3965. +# define spin_unlock(x) { (x)->lock=0;}
  3966. +# define spin_unlock_irq(x) { spin_unlock(x); sti();}
  3967. +# define spin_unlock_irqrestore(x,flags) { spin_unlock(x); restore_flags(flags);}
  3968. +
  3969. +# define read_lock(x) spin_lock(x)
  3970. +# define read_lock_irq(x) spin_lock_irq(x)
  3971. +# define read_lock_irqsave(x,flags) spin_lock_irqsave(x,flags)
  3972. +
  3973. +# define read_unlock(x) spin_unlock(x)
  3974. +# define read_unlock_irq(x) spin_unlock_irq(x)
  3975. +# define read_unlock_irqrestore(x,flags) spin_unlock_irqrestore(x,flags)
  3976. +
  3977. +# define write_lock(x) spin_lock(x)
  3978. +# define write_lock_irq(x) spin_lock_irq(x)
  3979. +# define write_lock_irqsave(x,flags) spin_lock_irqsave(x,flags)
  3980. +
  3981. +# define write_unlock(x) spin_unlock(x)
  3982. +# define write_unlock_irq(x) spin_unlock_irq(x)
  3983. +# define write_unlock_irqrestore(x,flags) spin_unlock_irqrestore(x,flags)
  3984. +#endif /* !SPINLOCK */
  3985. +
  3986. +#ifndef SPINLOCK_23
  3987. +# define spin_lock_bh(x) spin_lock_irq(x)
  3988. +# define spin_unlock_bh(x) spin_unlock_irq(x)
  3989. +
  3990. +# define read_lock_bh(x) read_lock_irq(x)
  3991. +# define read_unlock_bh(x) read_unlock_irq(x)
  3992. +
  3993. +# define write_lock_bh(x) write_lock_irq(x)
  3994. +# define write_unlock_bh(x) write_unlock_irq(x)
  3995. +#endif /* !SPINLOCK_23 */
  3996. +
  3997. +#ifndef HAVE_NETDEV_PRINTK
  3998. +#define netdev_printk(sevlevel, netdev, msglevel, format, arg...) \
  3999. + printk(sevlevel "%s: " format , netdev->name , ## arg)
  4000. +#endif
  4001. +
  4002. +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0)
  4003. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
  4004. +#include "openswan/ipsec_kern24.h"
  4005. +#else
  4006. +#error "kernels before 2.4 are not supported at this time"
  4007. +#endif
  4008. +#endif
  4009. +
  4010. +
  4011. +#endif /* _OPENSWAN_KVERSIONS_H */
  4012. +
  4013. +/*
  4014. + * $Log: ipsec_kversion.h,v $
  4015. + * Revision 1.15.2.11 2007/02/20 03:53:16 paul
  4016. + * Added comment, made layout consistent with other checks.
  4017. + *
  4018. + * Revision 1.15.2.10 2007/02/16 19:08:12 paul
  4019. + * Fix for compiling on 2.6.20 (nfmark is now called mark in sk_buff)
  4020. + *
  4021. + * Revision 1.15.2.9 2006/07/29 05:00:40 paul
  4022. + * Added HAVE_NEW_SKB_LINEARIZE for 2.6.18+ kernels where skb_linearize
  4023. + * only takes 1 argument.
  4024. + *
  4025. + * Revision 1.15.2.8 2006/05/01 14:31:52 mcr
  4026. + * FREESWAN->OPENSWAN in #ifdef.
  4027. + *
  4028. + * Revision 1.15.2.7 2006/01/11 02:02:59 mcr
  4029. + * updated patches and DEFAULT_TTL code to work
  4030. + *
  4031. + * Revision 1.15.2.6 2006/01/03 19:25:02 ken
  4032. + * Remove duplicated #ifdef for TTL fix - bad patch
  4033. + *
  4034. + * Revision 1.15.2.5 2006/01/03 18:06:33 ken
  4035. + * Fix for missing sysctl default ttl
  4036. + *
  4037. + * Revision 1.15.2.4 2005/11/27 21:40:14 paul
  4038. + * Pull down TTL fixes from head. this fixes "Unknown symbol sysctl_ip_default_ttl"
  4039. + * in for klips as module.
  4040. + *
  4041. + * Revision 1.15.2.3 2005/11/22 04:11:52 ken
  4042. + * Backport fixes for 2.6.14 kernels from HEAD
  4043. + *
  4044. + * Revision 1.15.2.2 2005/09/01 01:57:19 paul
  4045. + * michael's fixes for 2.6.13 from head
  4046. + *
  4047. + * Revision 1.15.2.1 2005/08/27 23:13:48 paul
  4048. + * Fix for:
  4049. + * 7 weeks ago: [NET]: Remove unused security member in sk_buff
  4050. + * changeset 4280: 328ea53f5fee
  4051. + * parent 4279: beb0afb0e3f8
  4052. + * author: Thomas Graf <tgraf@suug.ch>
  4053. + * date: Tue Jul 5 21:12:44 2005
  4054. + * files: include/linux/skbuff.h include/linux/tc_ematch/tc_em_meta.h net/core/skbuff.c net/ipv4/ip_output.c net/ipv6/ip6_output.c net/sched/em_meta.c
  4055. + *
  4056. + * This should fix compilation on 2.6.13(rc) kernels
  4057. + *
  4058. + * Revision 1.15 2005/07/19 20:02:15 mcr
  4059. + * sk_alloc() interface change.
  4060. + *
  4061. + * Revision 1.14 2005/07/08 16:20:05 mcr
  4062. + * fix for 2.6.12 disapperance of sk_zapped field -> sock_flags.
  4063. + *
  4064. + * Revision 1.13 2005/05/20 03:19:18 mcr
  4065. + * modifications for use on 2.4.30 kernel, with backported
  4066. + * printk_ratelimit(). all warnings removed.
  4067. + *
  4068. + * Revision 1.12 2005/04/13 22:46:21 mcr
  4069. + * note that KLIPS does not work on Linux 2.0.
  4070. + *
  4071. + * Revision 1.11 2004/09/13 02:22:26 mcr
  4072. + * #define inet_protocol if necessary.
  4073. + *
  4074. + * Revision 1.10 2004/08/03 18:17:15 mcr
  4075. + * in 2.6, use "net_device" instead of #define device->net_device.
  4076. + * this probably breaks 2.0 compiles.
  4077. + *
  4078. + * Revision 1.9 2004/04/05 19:55:05 mcr
  4079. + * Moved from linux/include/freeswan/ipsec_kversion.h,v
  4080. + *
  4081. + * Revision 1.8 2003/12/13 19:10:16 mcr
  4082. + * refactored rcv and xmit code - same as FS 2.05.
  4083. + *
  4084. + * Revision 1.7 2003/07/31 22:48:08 mcr
  4085. + * derive NET25-ness from presence of NETLINK_XFRM macro.
  4086. + *
  4087. + * Revision 1.6 2003/06/24 20:22:32 mcr
  4088. + * added new global: ipsecdevices[] so that we can keep track of
  4089. + * the ipsecX devices. They will be referenced with dev_hold(),
  4090. + * so 2.2 may need this as well.
  4091. + *
  4092. + * Revision 1.5 2003/04/03 17:38:09 rgb
  4093. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  4094. + *
  4095. + * Revision 1.4 2002/04/24 07:36:46 mcr
  4096. + * Moved from ./klips/net/ipsec/ipsec_kversion.h,v
  4097. + *
  4098. + * Revision 1.3 2002/04/12 03:21:17 mcr
  4099. + * three parameter version of ip_select_ident appears first
  4100. + * in 2.4.2 (RH7.1) not 2.4.4.
  4101. + *
  4102. + * Revision 1.2 2002/03/08 21:35:22 rgb
  4103. + * Defined LINUX_KERNEL_HAS_SNPRINTF to shut up compiler warnings after
  4104. + * 2.4.9. (Andreas Piesk).
  4105. + *
  4106. + * Revision 1.1 2002/01/29 02:11:42 mcr
  4107. + * removal of kversions.h - sources that needed it now use ipsec_param.h.
  4108. + * updating of IPv6 structures to match latest in6.h version.
  4109. + * removed dead code from freeswan.h that also duplicated kversions.h
  4110. + * code.
  4111. + *
  4112. + *
  4113. + */
  4114. --- /dev/null Tue Mar 11 13:02:56 2003
  4115. +++ linux/include/openswan/ipsec_life.h Mon Feb 9 13:51:03 2004
  4116. @@ -0,0 +1,112 @@
  4117. +/*
  4118. + * Definitions relevant to IPSEC lifetimes
  4119. + * Copyright (C) 2001 Richard Guy Briggs <rgb@freeswan.org>
  4120. + * and Michael Richardson <mcr@freeswan.org>
  4121. + *
  4122. + * This program is free software; you can redistribute it and/or modify it
  4123. + * under the terms of the GNU General Public License as published by the
  4124. + * Free Software Foundation; either version 2 of the License, or (at your
  4125. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  4126. + *
  4127. + * This program is distributed in the hope that it will be useful, but
  4128. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  4129. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  4130. + * for more details.
  4131. + *
  4132. + * RCSID $Id: ipsec_life.h,v 1.4 2004/04/05 19:55:05 mcr Exp $
  4133. + *
  4134. + * This file derived from ipsec_xform.h on 2001/9/18 by mcr.
  4135. + *
  4136. + */
  4137. +
  4138. +/*
  4139. + * This file describes the book keeping fields for the
  4140. + * IPsec Security Association Structure. ("ipsec_sa")
  4141. + *
  4142. + * This structure is never allocated directly by kernel code,
  4143. + * (it is always a static/auto or is part of a structure)
  4144. + * so it does not have a reference count.
  4145. + *
  4146. + */
  4147. +
  4148. +#ifndef _IPSEC_LIFE_H_
  4149. +
  4150. +/*
  4151. + * _count is total count.
  4152. + * _hard is hard limit (kill SA after this number)
  4153. + * _soft is soft limit (try to renew SA after this number)
  4154. + * _last is used in some special cases.
  4155. + *
  4156. + */
  4157. +
  4158. +struct ipsec_lifetime64
  4159. +{
  4160. + __u64 ipl_count;
  4161. + __u64 ipl_soft;
  4162. + __u64 ipl_hard;
  4163. + __u64 ipl_last;
  4164. +};
  4165. +
  4166. +struct ipsec_lifetimes
  4167. +{
  4168. + /* number of bytes processed */
  4169. + struct ipsec_lifetime64 ipl_bytes;
  4170. +
  4171. + /* number of packets processed */
  4172. + struct ipsec_lifetime64 ipl_packets;
  4173. +
  4174. + /* time since SA was added */
  4175. + struct ipsec_lifetime64 ipl_addtime;
  4176. +
  4177. + /* time since SA was first used */
  4178. + struct ipsec_lifetime64 ipl_usetime;
  4179. +
  4180. + /* from rfc2367:
  4181. + * For CURRENT, the number of different connections,
  4182. + * endpoints, or flows that the association has been
  4183. + * allocated towards. For HARD and SOFT, the number of
  4184. + * these the association may be allocated towards
  4185. + * before it expires. The concept of a connection,
  4186. + * flow, or endpoint is system specific.
  4187. + *
  4188. + * mcr(2001-9-18) it is unclear what purpose these serve for FreeSWAN.
  4189. + * They are maintained for PF_KEY compatibility.
  4190. + */
  4191. + struct ipsec_lifetime64 ipl_allocations;
  4192. +};
  4193. +
  4194. +enum ipsec_life_alive {
  4195. + ipsec_life_harddied = -1,
  4196. + ipsec_life_softdied = 0,
  4197. + ipsec_life_okay = 1
  4198. +};
  4199. +
  4200. +enum ipsec_life_type {
  4201. + ipsec_life_timebased = 1,
  4202. + ipsec_life_countbased= 0
  4203. +};
  4204. +
  4205. +#define _IPSEC_LIFE_H_
  4206. +#endif /* _IPSEC_LIFE_H_ */
  4207. +
  4208. +
  4209. +/*
  4210. + * $Log: ipsec_life.h,v $
  4211. + * Revision 1.4 2004/04/05 19:55:05 mcr
  4212. + * Moved from linux/include/freeswan/ipsec_life.h,v
  4213. + *
  4214. + * Revision 1.3 2002/04/24 07:36:46 mcr
  4215. + * Moved from ./klips/net/ipsec/ipsec_life.h,v
  4216. + *
  4217. + * Revision 1.2 2001/11/26 09:16:14 rgb
  4218. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  4219. + *
  4220. + * Revision 1.1.2.1 2001/09/25 02:25:58 mcr
  4221. + * lifetime structure created and common functions created.
  4222. + *
  4223. + *
  4224. + * Local variables:
  4225. + * c-file-style: "linux"
  4226. + * End:
  4227. + *
  4228. + */
  4229. --- /dev/null Tue Mar 11 13:02:56 2003
  4230. +++ linux/include/openswan/ipsec_md5h.h Mon Feb 9 13:51:03 2004
  4231. @@ -0,0 +1,143 @@
  4232. +/*
  4233. + * RCSID $Id: ipsec_md5h.h,v 1.10 2004/09/08 17:21:35 ken Exp $
  4234. + */
  4235. +
  4236. +/*
  4237. + * The rest of this file is Copyright RSA DSI. See the following comments
  4238. + * for the full Copyright notice.
  4239. + */
  4240. +
  4241. +#ifndef _IPSEC_MD5H_H_
  4242. +#define _IPSEC_MD5H_H_
  4243. +
  4244. +/* GLOBAL.H - RSAREF types and constants
  4245. + */
  4246. +
  4247. +/* PROTOTYPES should be set to one if and only if the compiler supports
  4248. + function argument prototyping.
  4249. + The following makes PROTOTYPES default to 0 if it has not already
  4250. + been defined with C compiler flags.
  4251. + */
  4252. +#ifndef PROTOTYPES
  4253. +#define PROTOTYPES 1
  4254. +#endif /* !PROTOTYPES */
  4255. +
  4256. +/* POINTER defines a generic pointer type */
  4257. +typedef __u8 *POINTER;
  4258. +
  4259. +/* UINT2 defines a two byte word */
  4260. +typedef __u16 UINT2;
  4261. +
  4262. +/* UINT4 defines a four byte word */
  4263. +typedef __u32 UINT4;
  4264. +
  4265. +/* PROTO_LIST is defined depending on how PROTOTYPES is defined above.
  4266. + If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it
  4267. + returns an empty list.
  4268. + */
  4269. +
  4270. +#if PROTOTYPES
  4271. +#define PROTO_LIST(list) list
  4272. +#else /* PROTOTYPES */
  4273. +#define PROTO_LIST(list) ()
  4274. +#endif /* PROTOTYPES */
  4275. +
  4276. +
  4277. +/* MD5.H - header file for MD5C.C
  4278. + */
  4279. +
  4280. +/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  4281. +rights reserved.
  4282. +
  4283. +License to copy and use this software is granted provided that it
  4284. +is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  4285. +Algorithm" in all material mentioning or referencing this software
  4286. +or this function.
  4287. +
  4288. +License is also granted to make and use derivative works provided
  4289. +that such works are identified as "derived from the RSA Data
  4290. +Security, Inc. MD5 Message-Digest Algorithm" in all material
  4291. +mentioning or referencing the derived work.
  4292. +
  4293. +RSA Data Security, Inc. makes no representations concerning either
  4294. +the merchantability of this software or the suitability of this
  4295. +software for any particular purpose. It is provided "as is"
  4296. +without express or implied warranty of any kind.
  4297. +
  4298. +These notices must be retained in any copies of any part of this
  4299. +documentation and/or software.
  4300. + */
  4301. +
  4302. +/* MD5 context. */
  4303. +typedef struct {
  4304. + UINT4 state[4]; /* state (ABCD) */
  4305. + UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
  4306. + unsigned char buffer[64]; /* input buffer */
  4307. +} MD5_CTX;
  4308. +
  4309. +void osMD5Init PROTO_LIST ((void *));
  4310. +void osMD5Update PROTO_LIST
  4311. + ((void *, unsigned char *, __u32));
  4312. +void osMD5Final PROTO_LIST ((unsigned char [16], void *));
  4313. +
  4314. +#endif /* _IPSEC_MD5H_H_ */
  4315. +
  4316. +/*
  4317. + * $Log: ipsec_md5h.h,v $
  4318. + * Revision 1.10 2004/09/08 17:21:35 ken
  4319. + * Rename MD5* -> osMD5 functions to prevent clashes with other symbols exported by kernel modules (CIFS in 2.6 initiated this)
  4320. + *
  4321. + * Revision 1.9 2004/04/05 19:55:05 mcr
  4322. + * Moved from linux/include/freeswan/ipsec_md5h.h,v
  4323. + *
  4324. + * Revision 1.8 2002/09/10 01:45:09 mcr
  4325. + * changed type of MD5_CTX and SHA1_CTX to void * so that
  4326. + * the function prototypes would match, and could be placed
  4327. + * into a pointer to a function.
  4328. + *
  4329. + * Revision 1.7 2002/04/24 07:36:46 mcr
  4330. + * Moved from ./klips/net/ipsec/ipsec_md5h.h,v
  4331. + *
  4332. + * Revision 1.6 1999/12/13 13:59:13 rgb
  4333. + * Quick fix to argument size to Update bugs.
  4334. + *
  4335. + * Revision 1.5 1999/12/07 18:16:23 rgb
  4336. + * Fixed comments at end of #endif lines.
  4337. + *
  4338. + * Revision 1.4 1999/04/06 04:54:26 rgb
  4339. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  4340. + * patch shell fixes.
  4341. + *
  4342. + * Revision 1.3 1999/01/22 06:19:58 rgb
  4343. + * 64-bit clean-up.
  4344. + *
  4345. + * Revision 1.2 1998/11/30 13:22:54 rgb
  4346. + * Rationalised all the klips kernel file headers. They are much shorter
  4347. + * now and won't conflict under RH5.2.
  4348. + *
  4349. + * Revision 1.1 1998/06/18 21:27:48 henry
  4350. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  4351. + * kernel-build scripts happier in the presence of symlinks
  4352. + *
  4353. + * Revision 1.2 1998/04/23 20:54:03 rgb
  4354. + * Fixed md5 and sha1 include file nesting issues, to be cleaned up when
  4355. + * verified.
  4356. + *
  4357. + * Revision 1.1 1998/04/09 03:04:21 henry
  4358. + * sources moved up from linux/net/ipsec
  4359. + * these two include files modified not to include others except in kernel
  4360. + *
  4361. + * Revision 1.1.1.1 1998/04/08 05:35:03 henry
  4362. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  4363. + *
  4364. + * Revision 0.4 1997/01/15 01:28:15 ji
  4365. + * No changes.
  4366. + *
  4367. + * Revision 0.3 1996/11/20 14:48:53 ji
  4368. + * Release update only.
  4369. + *
  4370. + * Revision 0.2 1996/11/02 00:18:33 ji
  4371. + * First limited release.
  4372. + *
  4373. + *
  4374. + */
  4375. --- /dev/null Tue Mar 11 13:02:56 2003
  4376. +++ linux/include/openswan/ipsec_param.h Mon Feb 9 13:51:03 2004
  4377. @@ -0,0 +1,387 @@
  4378. +/*
  4379. + * @(#) Openswan tunable paramaters
  4380. + *
  4381. + * Copyright (C) 2001 Richard Guy Briggs <rgb@freeswan.org>
  4382. + * and Michael Richardson <mcr@freeswan.org>
  4383. + * Copyright (C) 2004 Michael Richardson <mcr@xelerance.com>
  4384. + *
  4385. + * This program is free software; you can redistribute it and/or modify it
  4386. + * under the terms of the GNU General Public License as published by the
  4387. + * Free Software Foundation; either version 2 of the License, or (at your
  4388. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  4389. + *
  4390. + * This program is distributed in the hope that it will be useful, but
  4391. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  4392. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  4393. + * for more details.
  4394. + *
  4395. + * RCSID $Id: ipsec_param.h,v 1.29.6.3 2006/05/01 14:32:31 mcr Exp $
  4396. + *
  4397. + */
  4398. +
  4399. +/*
  4400. + * This file provides a set of #define's which may be tuned by various
  4401. + * people/configurations. It keeps all compile-time tunables in one place.
  4402. + *
  4403. + * This file should be included before all other IPsec kernel-only files.
  4404. + *
  4405. + */
  4406. +
  4407. +#ifndef _IPSEC_PARAM_H_
  4408. +
  4409. +#ifdef __KERNEL__
  4410. +#include "ipsec_kversion.h"
  4411. +#include <linux/ip.h> /* struct iphdr */
  4412. +/* Set number of ipsecX virtual devices here. */
  4413. +/* This must be < exp(field width of IPSEC_DEV_FORMAT) */
  4414. +/* It must also be reasonable so as not to overload the memory and CPU */
  4415. +/* constraints of the host. */
  4416. +#define IPSEC_NUM_IF 4
  4417. +/* The field width must be < IF_NAM_SIZ - strlen("ipsec") - 1. */
  4418. +/* With "ipsec" being 5 characters, that means 10 is the max field width */
  4419. +/* but machine memory and CPU constraints are not likely to tollerate */
  4420. +/* more than 3 digits. The default is one digit. */
  4421. +/* Update: userland scripts get upset if they can't find "ipsec0", so */
  4422. +/* for now, no "0"-padding should be used (which would have been helpful */
  4423. +/* to make text-searches work */
  4424. +#define IPSEC_DEV_FORMAT "ipsec%d"
  4425. +/* For, say, 500 virtual ipsec devices, I would recommend: */
  4426. +/* #define IPSEC_NUM_IF 500 */
  4427. +/* #define IPSEC_DEV_FORMAT "ipsec%03d" */
  4428. +/* Note that the "interfaces=" line in /etc/ipsec.conf would be, um, challenging. */
  4429. +
  4430. +/* use dynamic ipsecX device allocation */
  4431. +#ifndef CONFIG_KLIPS_DYNDEV
  4432. +#define CONFIG_KLIPS_DYNDEV 1
  4433. +#endif /* CONFIG_KLIPS_DYNDEV */
  4434. +
  4435. +
  4436. +#ifdef CONFIG_KLIPS_BIGGATE
  4437. +# define SADB_HASHMOD 8069
  4438. +#else /* CONFIG_KLIPS_BIGGATE */
  4439. +# define SADB_HASHMOD 257
  4440. +#endif /* CONFIG_KLIPS_BIGGATE */
  4441. +#endif /* __KERNEL__ */
  4442. +
  4443. +/*
  4444. + * This is for the SA reference table. This number is related to the
  4445. + * maximum number of SAs that KLIPS can concurrently deal with, plus enough
  4446. + * space for keeping expired SAs around.
  4447. + *
  4448. + * TABLE_MAX_WIDTH is the number of bits that we will use.
  4449. + * MAIN_TABLE_WIDTH is the number of bits used for the primary index table.
  4450. + *
  4451. + */
  4452. +#ifndef IPSEC_SA_REF_TABLE_IDX_WIDTH
  4453. +# define IPSEC_SA_REF_TABLE_IDX_WIDTH 16
  4454. +#endif
  4455. +
  4456. +#ifndef IPSEC_SA_REF_MAINTABLE_IDX_WIDTH
  4457. +# define IPSEC_SA_REF_MAINTABLE_IDX_WIDTH 4
  4458. +#endif
  4459. +
  4460. +#ifndef IPSEC_SA_REF_FREELIST_NUM_ENTRIES
  4461. +# define IPSEC_SA_REF_FREELIST_NUM_ENTRIES 256
  4462. +#endif
  4463. +
  4464. +#ifndef IPSEC_SA_REF_CODE
  4465. +# define IPSEC_SA_REF_CODE 1
  4466. +#endif
  4467. +
  4468. +#ifdef __KERNEL__
  4469. +/* This is defined for 2.4, but not 2.2.... */
  4470. +#ifndef ARPHRD_VOID
  4471. +# define ARPHRD_VOID 0xFFFF
  4472. +#endif
  4473. +
  4474. +/* always turn on IPIP mode */
  4475. +#ifndef CONFIG_KLIPS_IPIP
  4476. +#define CONFIG_KLIPS_IPIP 1
  4477. +#endif
  4478. +
  4479. +/*
  4480. + * Worry about PROC_FS stuff
  4481. + */
  4482. +#if defined(PROC_FS_2325)
  4483. +/* kernel 2.4 */
  4484. +# define IPSEC_PROC_LAST_ARG ,int *eof,void *data
  4485. +# define IPSEC_PROCFS_DEBUG_NO_STATIC
  4486. +# define IPSEC_PROC_SUBDIRS
  4487. +#else
  4488. +/* kernel <2.4 */
  4489. +# define IPSEC_PROCFS_DEBUG_NO_STATIC DEBUG_NO_STATIC
  4490. +
  4491. +# ifndef PROC_NO_DUMMY
  4492. +# define IPSEC_PROC_LAST_ARG , int dummy
  4493. +# else
  4494. +# define IPSEC_PROC_LAST_ARG
  4495. +# endif /* !PROC_NO_DUMMY */
  4496. +#endif /* PROC_FS_2325 */
  4497. +
  4498. +#if !defined(LINUX_KERNEL_HAS_SNPRINTF)
  4499. +/* GNU CPP specific! */
  4500. +# define snprintf(buf, len, fmt...) sprintf(buf, ##fmt)
  4501. +#endif /* !LINUX_KERNEL_HAS_SNPRINTF */
  4502. +
  4503. +#ifdef SPINLOCK
  4504. +# ifdef SPINLOCK_23
  4505. +# include <linux/spinlock.h> /* *lock* */
  4506. +# else /* SPINLOCK_23 */
  4507. +# include <asm/spinlock.h> /* *lock* */
  4508. +# endif /* SPINLOCK_23 */
  4509. +#endif /* SPINLOCK */
  4510. +
  4511. +#ifndef KLIPS_FIXES_DES_PARITY
  4512. +# define KLIPS_FIXES_DES_PARITY 1
  4513. +#endif /* !KLIPS_FIXES_DES_PARITY */
  4514. +
  4515. +/* we don't really want to print these unless there are really big problems */
  4516. +#ifndef KLIPS_DIVULGE_CYPHER_KEY
  4517. +# define KLIPS_DIVULGE_CYPHER_KEY 0
  4518. +#endif /* !KLIPS_DIVULGE_CYPHER_KEY */
  4519. +
  4520. +#ifndef KLIPS_DIVULGE_HMAC_KEY
  4521. +# define KLIPS_DIVULGE_HMAC_KEY 0
  4522. +#endif /* !KLIPS_DIVULGE_HMAC_KEY */
  4523. +
  4524. +#ifndef IPSEC_DISALLOW_IPOPTIONS
  4525. +# define IPSEC_DISALLOW_IPOPTIONS 1
  4526. +#endif /* !KLIPS_DIVULGE_HMAC_KEY */
  4527. +
  4528. +/* extra toggles for regression testing */
  4529. +#ifdef CONFIG_KLIPS_REGRESS
  4530. +
  4531. +/*
  4532. + * should pfkey_acquire() become 100% lossy?
  4533. + *
  4534. + */
  4535. +extern int sysctl_ipsec_regress_pfkey_lossage;
  4536. +#ifndef KLIPS_PFKEY_ACQUIRE_LOSSAGE
  4537. +# ifdef CONFIG_KLIPS_PFKEY_ACQUIRE_LOSSAGE
  4538. +# define KLIPS_PFKEY_ACQUIRE_LOSSAGE 100
  4539. +# endif /* CONFIG_KLIPS_PFKEY_ACQUIRE_LOSSAGE */
  4540. +#else
  4541. +#define KLIPS_PFKEY_ACQUIRE_LOSSAGE 0
  4542. +#endif /* KLIPS_PFKEY_ACQUIRE_LOSSAGE */
  4543. +
  4544. +#else /* CONFIG_KLIPS_REGRESS */
  4545. +#define KLIPS_PFKEY_ACQUIRE_LOSSAGE 0
  4546. +
  4547. +#endif /* CONFIG_KLIPS_REGRESS */
  4548. +
  4549. +
  4550. +/*
  4551. + * debugging routines.
  4552. + */
  4553. +#define KLIPS_ERROR(flag, format, args...) if(printk_ratelimit() || flag) printk(KERN_ERR "KLIPS " format, ## args)
  4554. +#ifdef CONFIG_KLIPS_DEBUG
  4555. +extern void ipsec_print_ip(struct iphdr *ip);
  4556. +
  4557. +# define KLIPS_PRINT(flag, format, args...) \
  4558. + ((flag) ? printk(KERN_INFO format , ## args) : 0)
  4559. +# define KLIPS_PRINTMORE(flag, format, args...) \
  4560. + ((flag) ? printk(format , ## args) : 0)
  4561. +# define KLIPS_IP_PRINT(flag, ip) \
  4562. + ((flag) ? ipsec_print_ip(ip) : 0)
  4563. +#else /* CONFIG_KLIPS_DEBUG */
  4564. +# define KLIPS_PRINT(flag, format, args...) do ; while(0)
  4565. +# define KLIPS_PRINTMORE(flag, format, args...) do ; while(0)
  4566. +# define KLIPS_IP_PRINT(flag, ip) do ; while(0)
  4567. +#endif /* CONFIG_KLIPS_DEBUG */
  4568. +
  4569. +
  4570. +/*
  4571. + * Stupid kernel API differences in APIs. Not only do some
  4572. + * kernels not have ip_select_ident, but some have differing APIs,
  4573. + * and SuSE has one with one parameter, but no way of checking to
  4574. + * see what is really what.
  4575. + */
  4576. +
  4577. +#ifdef SUSE_LINUX_2_4_19_IS_STUPID
  4578. +#define KLIPS_IP_SELECT_IDENT(iph, skb) ip_select_ident(iph)
  4579. +#else
  4580. +
  4581. +/* simplest case, nothing */
  4582. +#if !defined(IP_SELECT_IDENT)
  4583. +#define KLIPS_IP_SELECT_IDENT(iph, skb) do { iph->id = htons(ip_id_count++); } while(0)
  4584. +#endif
  4585. +
  4586. +/* kernels > 2.3.37-ish */
  4587. +#if defined(IP_SELECT_IDENT) && !defined(IP_SELECT_IDENT_NEW)
  4588. +#define KLIPS_IP_SELECT_IDENT(iph, skb) ip_select_ident(iph, skb->dst)
  4589. +#endif
  4590. +
  4591. +/* kernels > 2.4.2 */
  4592. +#if defined(IP_SELECT_IDENT) && defined(IP_SELECT_IDENT_NEW)
  4593. +#define KLIPS_IP_SELECT_IDENT(iph, skb) ip_select_ident(iph, skb->dst, NULL)
  4594. +#endif
  4595. +
  4596. +#endif /* SUSE_LINUX_2_4_19_IS_STUPID */
  4597. +
  4598. +/*
  4599. + * make klips fail test:east-espiv-01.
  4600. + * exploit is at testing/attacks/espiv
  4601. + *
  4602. + */
  4603. +#define KLIPS_IMPAIRMENT_ESPIV_CBC_ATTACK 0
  4604. +
  4605. +
  4606. +/* IP_FRAGMENT_LINEARIZE is set in freeswan.h if Kernel > 2.4.4 */
  4607. +#ifndef IP_FRAGMENT_LINEARIZE
  4608. +# define IP_FRAGMENT_LINEARIZE 0
  4609. +#endif /* IP_FRAGMENT_LINEARIZE */
  4610. +#endif /* __KERNEL__ */
  4611. +
  4612. +#ifdef NEED_INET_PROTOCOL
  4613. +#define inet_protocol net_protocol
  4614. +#endif
  4615. +
  4616. +#if defined(CONFIG_IPSEC_NAT_TRAVERSAL) && CONFIG_IPSEC_NAT_TRAVERSAL
  4617. +#define NAT_TRAVERSAL 1
  4618. +#else
  4619. +/* let people either #undef, or #define = 0 it */
  4620. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  4621. +#undef CONFIG_IPSEC_NAT_TRAVERSAL
  4622. +#endif
  4623. +#endif
  4624. +
  4625. +#ifndef IPSEC_DEFAULT_TTL
  4626. +#define IPSEC_DEFAULT_TTL 64
  4627. +#endif
  4628. +
  4629. +#define _IPSEC_PARAM_H_
  4630. +#endif /* _IPSEC_PARAM_H_ */
  4631. +
  4632. +/*
  4633. + * $Log: ipsec_param.h,v $
  4634. + * Revision 1.29.6.3 2006/05/01 14:32:31 mcr
  4635. + * added KLIPS_ERROR and make sure that things work without CONFIG_KLIPS_REGRESS.
  4636. + *
  4637. + * Revision 1.29.6.2 2005/11/27 21:40:14 paul
  4638. + * Pull down TTL fixes from head. this fixes "Unknown symbol sysctl_ip_default_ttl"
  4639. + * in for klips as module.
  4640. + *
  4641. + * Revision 1.29.6.1 2005/08/12 16:24:18 ken
  4642. + * Pull in NAT-T compile logic from HEAD
  4643. + *
  4644. + * Revision 1.29 2005/01/26 00:50:35 mcr
  4645. + * adjustment of confusion of CONFIG_IPSEC_NAT vs CONFIG_KLIPS_NAT,
  4646. + * and make sure that NAT_TRAVERSAL is set as well to match
  4647. + * userspace compiles of code.
  4648. + *
  4649. + * Revision 1.28 2004/09/13 15:50:15 mcr
  4650. + * spell NEED_INET properly, not NET_INET.
  4651. + *
  4652. + * Revision 1.27 2004/09/13 02:21:45 mcr
  4653. + * always turn on IPIP mode.
  4654. + * #define inet_protocol if necessary.
  4655. + *
  4656. + * Revision 1.26 2004/08/17 03:25:43 mcr
  4657. + * freeswan->openswan.
  4658. + *
  4659. + * Revision 1.25 2004/07/10 19:08:41 mcr
  4660. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  4661. + *
  4662. + * Revision 1.24 2004/04/05 19:55:06 mcr
  4663. + * Moved from linux/include/freeswan/ipsec_param.h,v
  4664. + *
  4665. + * Revision 1.23 2003/12/13 19:10:16 mcr
  4666. + * refactored rcv and xmit code - same as FS 2.05.
  4667. + *
  4668. + * Revision 1.22 2003/10/31 02:27:05 mcr
  4669. + * pulled up port-selector patches and sa_id elimination.
  4670. + *
  4671. + * Revision 1.21.4.1 2003/10/29 01:10:19 mcr
  4672. + * elimited "struct sa_id"
  4673. + *
  4674. + * Revision 1.21 2003/04/03 17:38:18 rgb
  4675. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  4676. + * Change indentation for readability.
  4677. + *
  4678. + * Revision 1.20 2003/03/14 08:09:26 rgb
  4679. + * Fixed up CONFIG_IPSEC_DYNDEV definitions.
  4680. + *
  4681. + * Revision 1.19 2003/01/30 02:31:43 rgb
  4682. + *
  4683. + * Rename SAref table macro names for clarity.
  4684. + *
  4685. + * Revision 1.18 2002/09/30 19:06:26 rgb
  4686. + * Reduce default table to 16 bits width.
  4687. + *
  4688. + * Revision 1.17 2002/09/20 15:40:29 rgb
  4689. + * Define switch to activate new SAref code.
  4690. + * Prefix macros with "IPSEC_".
  4691. + * Rework saref freelist.
  4692. + * Restrict some bits to kernel context for use to klips utils.
  4693. + *
  4694. + * Revision 1.16 2002/09/20 05:00:31 rgb
  4695. + * Define switch to divulge hmac keys for debugging.
  4696. + * Added IPOPTIONS switch.
  4697. + *
  4698. + * Revision 1.15 2002/09/19 02:34:24 mcr
  4699. + * define IPSEC_PROC_SUBDIRS if we are 2.4, and use that in ipsec_proc.c
  4700. + * to decide if we are to create /proc/net/ipsec/.
  4701. + *
  4702. + * Revision 1.14 2002/08/30 01:20:54 mcr
  4703. + * reorganized 2.0/2.2/2.4 procfs support macro so match
  4704. + * 2.4 values/typedefs.
  4705. + *
  4706. + * Revision 1.13 2002/07/28 22:03:28 mcr
  4707. + * added some documentation to SA_REF_*
  4708. + * turned on fix for ESPIV attack, now that we have the attack code.
  4709. + *
  4710. + * Revision 1.12 2002/07/26 08:48:31 rgb
  4711. + * Added SA ref table code.
  4712. + *
  4713. + * Revision 1.11 2002/07/23 02:57:45 rgb
  4714. + * Define ARPHRD_VOID for < 2.4 kernels.
  4715. + *
  4716. + * Revision 1.10 2002/05/27 21:37:28 rgb
  4717. + * Set the defaults sanely for those adventurous enough to try more than 1
  4718. + * digit of ipsec devices.
  4719. + *
  4720. + * Revision 1.9 2002/05/27 18:56:07 rgb
  4721. + * Convert to dynamic ipsec device allocation.
  4722. + *
  4723. + * Revision 1.8 2002/04/24 07:36:47 mcr
  4724. + * Moved from ./klips/net/ipsec/ipsec_param.h,v
  4725. + *
  4726. + * Revision 1.7 2002/04/20 00:12:25 rgb
  4727. + * Added esp IV CBC attack fix, disabled.
  4728. + *
  4729. + * Revision 1.6 2002/01/29 02:11:42 mcr
  4730. + * removal of kversions.h - sources that needed it now use ipsec_param.h.
  4731. + * updating of IPv6 structures to match latest in6.h version.
  4732. + * removed dead code from freeswan.h that also duplicated kversions.h
  4733. + * code.
  4734. + *
  4735. + * Revision 1.5 2002/01/28 19:22:01 mcr
  4736. + * by default, turn off LINEARIZE option
  4737. + * (let kversions.h turn it on)
  4738. + *
  4739. + * Revision 1.4 2002/01/20 20:19:36 mcr
  4740. + * renamed option to IP_FRAGMENT_LINEARIZE.
  4741. + *
  4742. + * Revision 1.3 2002/01/12 02:57:25 mcr
  4743. + * first regression test causes acquire messages to be lost
  4744. + * 100% of the time. This is to help testing of pluto.
  4745. + *
  4746. + * Revision 1.2 2001/11/26 09:16:14 rgb
  4747. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  4748. + *
  4749. + * Revision 1.1.2.3 2001/10/23 04:40:16 mcr
  4750. + * added #define for DIVULGING session keys in debug output.
  4751. + *
  4752. + * Revision 1.1.2.2 2001/10/22 20:53:25 mcr
  4753. + * added a define to control forcing of DES parity.
  4754. + *
  4755. + * Revision 1.1.2.1 2001/09/25 02:20:19 mcr
  4756. + * many common kernel configuration questions centralized.
  4757. + * more things remain that should be moved from freeswan.h.
  4758. + *
  4759. + *
  4760. + * Local variables:
  4761. + * c-file-style: "linux"
  4762. + * End:
  4763. + *
  4764. + */
  4765. --- /dev/null Tue Mar 11 13:02:56 2003
  4766. +++ linux/include/openswan/ipsec_policy.h Mon Feb 9 13:51:03 2004
  4767. @@ -0,0 +1,217 @@
  4768. +#ifndef _IPSEC_POLICY_H
  4769. +/*
  4770. + * policy interface file between pluto and applications
  4771. + * Copyright (C) 2003 Michael Richardson <mcr@freeswan.org>
  4772. + *
  4773. + * This library is free software; you can redistribute it and/or modify it
  4774. + * under the terms of the GNU Library General Public License as published by
  4775. + * the Free Software Foundation; either version 2 of the License, or (at your
  4776. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  4777. + *
  4778. + * This library is distributed in the hope that it will be useful, but
  4779. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  4780. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  4781. + * License for more details.
  4782. + *
  4783. + * RCSID $Id: ipsec_policy.h,v 1.7.6.1 2005/07/26 01:53:07 ken Exp $
  4784. + */
  4785. +#define _IPSEC_POLICY_H /* seen it, no need to see it again */
  4786. +
  4787. +
  4788. +/*
  4789. + * this file defines an interface between an application (or rather an
  4790. + * application library) and a key/policy daemon. It provides for inquiries
  4791. + * as to the current state of a connected socket, as well as for general
  4792. + * questions.
  4793. + *
  4794. + * In general, the interface is defined as a series of functional interfaces,
  4795. + * and the policy messages should be internal. However, because this is in
  4796. + * fact an ABI between pieces of the system that may get compiled and revised
  4797. + * seperately, this ABI must be public and revision controlled.
  4798. + *
  4799. + * It is expected that the daemon will always support previous versions.
  4800. + */
  4801. +
  4802. +#define IPSEC_POLICY_MSG_REVISION (unsigned)200305061
  4803. +
  4804. +enum ipsec_policy_command {
  4805. + IPSEC_CMD_QUERY_FD = 1,
  4806. + IPSEC_CMD_QUERY_HOSTPAIR = 2,
  4807. + IPSEC_CMD_QUERY_DSTONLY = 3,
  4808. +};
  4809. +
  4810. +struct ipsec_policy_msg_head {
  4811. + u_int32_t ipm_version;
  4812. + u_int32_t ipm_msg_len;
  4813. + u_int32_t ipm_msg_type;
  4814. + u_int32_t ipm_msg_seq;
  4815. +};
  4816. +
  4817. +enum ipsec_privacy_quality {
  4818. + IPSEC_PRIVACY_NONE = 0,
  4819. + IPSEC_PRIVACY_INTEGRAL = 4, /* not private at all. AH-like */
  4820. + IPSEC_PRIVACY_UNKNOWN = 8, /* something is claimed, but details unavail */
  4821. + IPSEC_PRIVACY_ROT13 = 12, /* trivially breakable, i.e. 1DES */
  4822. + IPSEC_PRIVACY_GAK = 16, /* known eavesdroppers */
  4823. + IPSEC_PRIVACY_PRIVATE = 32, /* secure for at least a decade */
  4824. + IPSEC_PRIVACY_STRONG = 64, /* ridiculously secure */
  4825. + IPSEC_PRIVACY_TORTOISE = 192, /* even stronger, but very slow */
  4826. + IPSEC_PRIVACY_OTP = 224, /* some kind of *true* one time pad */
  4827. +};
  4828. +
  4829. +enum ipsec_bandwidth_quality {
  4830. + IPSEC_QOS_UNKNOWN = 0, /* unknown bandwidth */
  4831. + IPSEC_QOS_INTERACTIVE = 16, /* reasonably moderate jitter, moderate fast.
  4832. + Good enough for telnet/ssh. */
  4833. + IPSEC_QOS_VOIP = 32, /* faster crypto, predicable jitter */
  4834. + IPSEC_QOS_FTP = 64, /* higher throughput crypto, perhaps hardware
  4835. + offloaded, but latency/jitter may be bad */
  4836. + IPSEC_QOS_WIRESPEED = 128, /* expect to be able to fill your pipe */
  4837. +};
  4838. +
  4839. +/* moved from programs/pluto/constants.h */
  4840. +/* IPsec AH transform values
  4841. + * RFC2407 The Internet IP security Domain of Interpretation for ISAKMP 4.4.3
  4842. + * and in http://www.iana.org/assignments/isakmp-registry
  4843. + */
  4844. +enum ipsec_authentication_algo {
  4845. + AH_MD5=2,
  4846. + AH_SHA=3,
  4847. + AH_DES=4,
  4848. + AH_SHA2_256=5,
  4849. + AH_SHA2_384=6,
  4850. + AH_SHA2_512=7
  4851. +};
  4852. +
  4853. +/* IPsec ESP transform values
  4854. + * RFC2407 The Internet IP security Domain of Interpretation for ISAKMP 4.4.4
  4855. + * and from http://www.iana.org/assignments/isakmp-registry
  4856. + */
  4857. +
  4858. +enum ipsec_cipher_algo {
  4859. + ESP_reserved=0,
  4860. + ESP_DES_IV64=1,
  4861. + ESP_DES=2,
  4862. + ESP_3DES=3,
  4863. + ESP_RC5=4,
  4864. + ESP_IDEA=5,
  4865. + ESP_CAST=6,
  4866. + ESP_BLOWFISH=7,
  4867. + ESP_3IDEA=8,
  4868. + ESP_DES_IV32=9,
  4869. + ESP_RC4=10,
  4870. + ESP_NULL=11,
  4871. + ESP_AES=12, /* 128 bit AES */
  4872. +};
  4873. +
  4874. +/* IPCOMP transform values
  4875. + * RFC2407 The Internet IP security Domain of Interpretation for ISAKMP 4.4.5
  4876. + */
  4877. +
  4878. +enum ipsec_comp_algo {
  4879. + IPCOMP_OUI= 1,
  4880. + IPCOMP_DEFLATE= 2,
  4881. + IPCOMP_LZS= 3,
  4882. + IPCOMP_V42BIS= 4
  4883. +};
  4884. +
  4885. +/* Identification type values
  4886. + * RFC 2407 The Internet IP security Domain of Interpretation for ISAKMP 4.6.2.1
  4887. + */
  4888. +
  4889. +enum ipsec_id_type {
  4890. + ID_IMPOSSIBLE= (-2), /* private to Pluto */
  4891. + ID_MYID= (-1), /* private to Pluto */
  4892. + ID_NONE= 0, /* private to Pluto */
  4893. + ID_IPV4_ADDR= 1,
  4894. + ID_FQDN= 2,
  4895. + ID_USER_FQDN= 3,
  4896. + ID_IPV4_ADDR_SUBNET= 4,
  4897. + ID_IPV6_ADDR= 5,
  4898. + ID_IPV6_ADDR_SUBNET= 6,
  4899. + ID_IPV4_ADDR_RANGE= 7,
  4900. + ID_IPV6_ADDR_RANGE= 8,
  4901. + ID_DER_ASN1_DN= 9,
  4902. + ID_DER_ASN1_GN= 10,
  4903. + ID_KEY_ID= 11
  4904. +};
  4905. +
  4906. +/* Certificate type values
  4907. + * RFC 2408 ISAKMP, chapter 3.9
  4908. + */
  4909. +enum ipsec_cert_type {
  4910. + CERT_NONE= 0, /* none, or guess from file contents */
  4911. + CERT_PKCS7_WRAPPED_X509= 1, /* self-signed certificate from disk */
  4912. + CERT_PGP= 2,
  4913. + CERT_DNS_SIGNED_KEY= 3, /* KEY RR from DNS */
  4914. + CERT_X509_SIGNATURE= 4,
  4915. + CERT_X509_KEY_EXCHANGE= 5,
  4916. + CERT_KERBEROS_TOKENS= 6,
  4917. + CERT_CRL= 7,
  4918. + CERT_ARL= 8,
  4919. + CERT_SPKI= 9,
  4920. + CERT_X509_ATTRIBUTE= 10,
  4921. + CERT_RAW_RSA= 11, /* raw RSA from config file */
  4922. +};
  4923. +
  4924. +/* a SIG record in ASCII */
  4925. +struct ipsec_dns_sig {
  4926. + char fqdn[256];
  4927. + char dns_sig[768]; /* empty string if not signed */
  4928. +};
  4929. +
  4930. +struct ipsec_raw_key {
  4931. + char id_name[256];
  4932. + char fs_keyid[8];
  4933. +};
  4934. +
  4935. +struct ipsec_identity {
  4936. + enum ipsec_id_type ii_type;
  4937. + enum ipsec_cert_type ii_format;
  4938. + union {
  4939. + struct ipsec_dns_sig ipsec_dns_signed;
  4940. + /* some thing for PGP */
  4941. + /* some thing for PKIX */
  4942. + struct ipsec_raw_key ipsec_raw_key;
  4943. + } ii_credential;
  4944. +};
  4945. +
  4946. +#define IPSEC_MAX_CREDENTIALS 32
  4947. +
  4948. +struct ipsec_policy_cmd_query {
  4949. + struct ipsec_policy_msg_head head;
  4950. +
  4951. + /* Query section */
  4952. + ip_address query_local; /* us */
  4953. + ip_address query_remote; /* them */
  4954. + u_int8_t proto; /* TCP, ICMP, etc. */
  4955. + u_short src_port, dst_port;
  4956. +
  4957. + /* Answer section */
  4958. + enum ipsec_privacy_quality strength;
  4959. + enum ipsec_bandwidth_quality bandwidth;
  4960. + enum ipsec_authentication_algo auth_detail;
  4961. + enum ipsec_cipher_algo esp_detail;
  4962. + enum ipsec_comp_algo comp_detail;
  4963. +
  4964. + int credential_count;
  4965. +
  4966. + struct ipsec_identity credentials[IPSEC_MAX_CREDENTIALS];
  4967. +};
  4968. +
  4969. +#define IPSEC_POLICY_SOCKET "/var/run/pluto/pluto.info"
  4970. +
  4971. +/* prototypes */
  4972. +extern err_t ipsec_policy_lookup(int fd, struct ipsec_policy_cmd_query *result);
  4973. +extern err_t ipsec_policy_init(void);
  4974. +extern err_t ipsec_policy_final(void);
  4975. +extern err_t ipsec_policy_readmsg(int policysock,
  4976. + unsigned char *buf, size_t buflen);
  4977. +extern err_t ipsec_policy_sendrecv(unsigned char *buf, size_t buflen);
  4978. +extern err_t ipsec_policy_cgilookup(struct ipsec_policy_cmd_query *result);
  4979. +
  4980. +
  4981. +extern const char *ipsec_policy_version_code(void);
  4982. +extern const char *ipsec_policy_version_string(void);
  4983. +
  4984. +#endif /* _IPSEC_POLICY_H */
  4985. --- /dev/null Tue Mar 11 13:02:56 2003
  4986. +++ linux/include/openswan/ipsec_proto.h Mon Feb 9 13:51:03 2004
  4987. @@ -0,0 +1,199 @@
  4988. +/*
  4989. + * @(#) prototypes for FreeSWAN functions
  4990. + *
  4991. + * Copyright (C) 2001 Richard Guy Briggs <rgb@freeswan.org>
  4992. + * and Michael Richardson <mcr@freeswan.org>
  4993. + *
  4994. + * This program is free software; you can redistribute it and/or modify it
  4995. + * under the terms of the GNU General Public License as published by the
  4996. + * Free Software Foundation; either version 2 of the License, or (at your
  4997. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  4998. + *
  4999. + * This program is distributed in the hope that it will be useful, but
  5000. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  5001. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  5002. + * for more details.
  5003. + *
  5004. + * RCSID $Id: ipsec_proto.h,v 1.14 2005/04/29 04:50:03 mcr Exp $
  5005. + *
  5006. + */
  5007. +
  5008. +#ifndef _IPSEC_PROTO_H_
  5009. +
  5010. +#include "ipsec_param.h"
  5011. +
  5012. +/*
  5013. + * This file is a kernel only file that declares prototypes for
  5014. + * all intra-module function calls and global data structures.
  5015. + *
  5016. + * Include this file last.
  5017. + *
  5018. + */
  5019. +
  5020. +/* forward references */
  5021. +enum ipsec_direction;
  5022. +enum ipsec_life_type;
  5023. +struct ipsec_lifetime64;
  5024. +struct ident;
  5025. +struct sockaddr_encap;
  5026. +struct ipsec_sa;
  5027. +
  5028. +/* ipsec_init.c */
  5029. +extern struct prng ipsec_prng;
  5030. +
  5031. +/* ipsec_sa.c */
  5032. +extern struct ipsec_sa *ipsec_sadb_hash[SADB_HASHMOD];
  5033. +extern spinlock_t tdb_lock;
  5034. +extern int ipsec_sadb_init(void);
  5035. +extern int ipsec_sadb_cleanup(__u8);
  5036. +
  5037. +extern struct ipsec_sa *ipsec_sa_alloc(int*error);
  5038. +
  5039. +
  5040. +extern struct ipsec_sa *ipsec_sa_getbyid(ip_said *);
  5041. +extern int ipsec_sa_put(struct ipsec_sa *);
  5042. +extern /* void */ int ipsec_sa_del(struct ipsec_sa *);
  5043. +extern /* void */ int ipsec_sa_delchain(struct ipsec_sa *);
  5044. +extern /* void */ int ipsec_sa_add(struct ipsec_sa *);
  5045. +
  5046. +extern int ipsec_sa_init(struct ipsec_sa *ipsp);
  5047. +extern int ipsec_sa_wipe(struct ipsec_sa *ipsp);
  5048. +
  5049. +/* debug declarations */
  5050. +
  5051. +/* ipsec_proc.c */
  5052. +extern int ipsec_proc_init(void);
  5053. +extern void ipsec_proc_cleanup(void);
  5054. +
  5055. +/* ipsec_rcv.c */
  5056. +extern int ipsec_rcv(struct sk_buff *skb);
  5057. +extern int klips26_rcv_encap(struct sk_buff *skb, __u16 encap_type);
  5058. +
  5059. +/* ipsec_xmit.c */
  5060. +struct ipsec_xmit_state;
  5061. +extern enum ipsec_xmit_value ipsec_xmit_sanity_check_dev(struct ipsec_xmit_state *ixs);
  5062. +extern enum ipsec_xmit_value ipsec_xmit_sanity_check_skb(struct ipsec_xmit_state *ixs);
  5063. +extern void ipsec_print_ip(struct iphdr *ip);
  5064. +
  5065. +
  5066. +
  5067. +/* ipsec_radij.c */
  5068. +extern int ipsec_makeroute(struct sockaddr_encap *ea,
  5069. + struct sockaddr_encap *em,
  5070. + ip_said said,
  5071. + uint32_t pid,
  5072. + struct sk_buff *skb,
  5073. + struct ident *ident_s,
  5074. + struct ident *ident_d);
  5075. +
  5076. +extern int ipsec_breakroute(struct sockaddr_encap *ea,
  5077. + struct sockaddr_encap *em,
  5078. + struct sk_buff **first,
  5079. + struct sk_buff **last);
  5080. +
  5081. +int ipsec_radijinit(void);
  5082. +int ipsec_cleareroutes(void);
  5083. +int ipsec_radijcleanup(void);
  5084. +
  5085. +/* ipsec_life.c */
  5086. +extern enum ipsec_life_alive ipsec_lifetime_check(struct ipsec_lifetime64 *il64,
  5087. + const char *lifename,
  5088. + const char *saname,
  5089. + enum ipsec_life_type ilt,
  5090. + enum ipsec_direction idir,
  5091. + struct ipsec_sa *ips);
  5092. +
  5093. +
  5094. +extern int ipsec_lifetime_format(char *buffer,
  5095. + int buflen,
  5096. + char *lifename,
  5097. + enum ipsec_life_type timebaselife,
  5098. + struct ipsec_lifetime64 *lifetime);
  5099. +
  5100. +extern void ipsec_lifetime_update_hard(struct ipsec_lifetime64 *lifetime,
  5101. + __u64 newvalue);
  5102. +
  5103. +extern void ipsec_lifetime_update_soft(struct ipsec_lifetime64 *lifetime,
  5104. + __u64 newvalue);
  5105. +
  5106. +/* ipsec_snprintf.c */
  5107. +extern int ipsec_snprintf(char * buf, ssize_t size, const char *fmt, ...);
  5108. +extern void ipsec_dmp_block(char *s, caddr_t bb, int len);
  5109. +
  5110. +
  5111. +/* ipsec_alg.c */
  5112. +extern int ipsec_alg_init(void);
  5113. +
  5114. +
  5115. +#ifdef CONFIG_KLIPS_DEBUG
  5116. +
  5117. +extern int debug_xform;
  5118. +extern int debug_eroute;
  5119. +extern int debug_spi;
  5120. +extern int debug_netlink;
  5121. +
  5122. +#endif /* CONFIG_KLIPS_DEBUG */
  5123. +
  5124. +
  5125. +
  5126. +
  5127. +#define _IPSEC_PROTO_H
  5128. +#endif /* _IPSEC_PROTO_H_ */
  5129. +
  5130. +/*
  5131. + * $Log: ipsec_proto.h,v $
  5132. + * Revision 1.14 2005/04/29 04:50:03 mcr
  5133. + * prototypes for xmit and alg code.
  5134. + *
  5135. + * Revision 1.13 2005/04/17 03:46:07 mcr
  5136. + * added prototypes for ipsec_rcv() routines.
  5137. + *
  5138. + * Revision 1.12 2005/04/14 20:28:37 mcr
  5139. + * added additional prototypes.
  5140. + *
  5141. + * Revision 1.11 2005/04/14 01:16:28 mcr
  5142. + * add prototypes for snprintf.
  5143. + *
  5144. + * Revision 1.10 2005/04/13 22:47:28 mcr
  5145. + * make sure that forward references are available.
  5146. + *
  5147. + * Revision 1.9 2004/07/10 19:08:41 mcr
  5148. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  5149. + *
  5150. + * Revision 1.8 2004/04/05 19:55:06 mcr
  5151. + * Moved from linux/include/freeswan/ipsec_proto.h,v
  5152. + *
  5153. + * Revision 1.7 2003/10/31 02:27:05 mcr
  5154. + * pulled up port-selector patches and sa_id elimination.
  5155. + *
  5156. + * Revision 1.6.30.1 2003/10/29 01:10:19 mcr
  5157. + * elimited "struct sa_id"
  5158. + *
  5159. + * Revision 1.6 2002/05/23 07:13:48 rgb
  5160. + * Added ipsec_sa_put() for releasing an ipsec_sa refcount.
  5161. + *
  5162. + * Revision 1.5 2002/05/14 02:36:40 rgb
  5163. + * Converted reference from ipsec_sa_put to ipsec_sa_add to avoid confusion
  5164. + * with "put" usage in the kernel.
  5165. + *
  5166. + * Revision 1.4 2002/04/24 07:36:47 mcr
  5167. + * Moved from ./klips/net/ipsec/ipsec_proto.h,v
  5168. + *
  5169. + * Revision 1.3 2002/04/20 00:12:25 rgb
  5170. + * Added esp IV CBC attack fix, disabled.
  5171. + *
  5172. + * Revision 1.2 2001/11/26 09:16:15 rgb
  5173. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  5174. + *
  5175. + * Revision 1.1.2.1 2001/09/25 02:21:01 mcr
  5176. + * ipsec_proto.h created to keep prototypes rather than deal with
  5177. + * cyclic dependancies of structures and prototypes in .h files.
  5178. + *
  5179. + *
  5180. + *
  5181. + * Local variables:
  5182. + * c-file-style: "linux"
  5183. + * End:
  5184. + *
  5185. + */
  5186. +
  5187. --- /dev/null Tue Mar 11 13:02:56 2003
  5188. +++ linux/include/openswan/ipsec_radij.h Mon Feb 9 13:51:03 2004
  5189. @@ -0,0 +1,179 @@
  5190. +/*
  5191. + * @(#) Definitions relevant to the IPSEC <> radij tree interfacing
  5192. + * Copyright (C) 1996, 1997 John Ioannidis.
  5193. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  5194. + *
  5195. + * This program is free software; you can redistribute it and/or modify it
  5196. + * under the terms of the GNU General Public License as published by the
  5197. + * Free Software Foundation; either version 2 of the License, or (at your
  5198. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  5199. + *
  5200. + * This program is distributed in the hope that it will be useful, but
  5201. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  5202. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  5203. + * for more details.
  5204. + *
  5205. + * RCSID $Id: ipsec_radij.h,v 1.22 2004/07/10 19:08:41 mcr Exp $
  5206. + */
  5207. +
  5208. +#ifndef _IPSEC_RADIJ_H
  5209. +
  5210. +#include <openswan.h>
  5211. +
  5212. +int ipsec_walk(char *);
  5213. +
  5214. +int ipsec_rj_walker_procprint(struct radij_node *, void *);
  5215. +int ipsec_rj_walker_delete(struct radij_node *, void *);
  5216. +
  5217. +/* This structure is used to pass information between
  5218. + * ipsec_eroute_get_info and ipsec_rj_walker_procprint
  5219. + * (through rj_walktree) and between calls of ipsec_rj_walker_procprint.
  5220. + */
  5221. +struct wsbuf
  5222. +{
  5223. + /* from caller of ipsec_eroute_get_info: */
  5224. + char *const buffer; /* start of buffer provided */
  5225. + const int length; /* length of buffer provided */
  5226. + const off_t offset; /* file position of first character of interest */
  5227. + /* accumulated by ipsec_rj_walker_procprint: */
  5228. + int len; /* number of character filled into buffer */
  5229. + off_t begin; /* file position contained in buffer[0] (<=offset) */
  5230. +};
  5231. +
  5232. +extern struct radij_node_head *rnh;
  5233. +extern spinlock_t eroute_lock;
  5234. +
  5235. +struct eroute * ipsec_findroute(struct sockaddr_encap *);
  5236. +
  5237. +#define O1(x) (int)(((x)>>24)&0xff)
  5238. +#define O2(x) (int)(((x)>>16)&0xff)
  5239. +#define O3(x) (int)(((x)>>8)&0xff)
  5240. +#define O4(x) (int)(((x))&0xff)
  5241. +
  5242. +#ifdef CONFIG_KLIPS_DEBUG
  5243. +extern int debug_radij;
  5244. +void rj_dumptrees(void);
  5245. +
  5246. +#define DB_RJ_DUMPTREES 0x0001
  5247. +#define DB_RJ_FINDROUTE 0x0002
  5248. +#endif /* CONFIG_KLIPS_DEBUG */
  5249. +
  5250. +#define _IPSEC_RADIJ_H
  5251. +#endif
  5252. +
  5253. +/*
  5254. + * $Log: ipsec_radij.h,v $
  5255. + * Revision 1.22 2004/07/10 19:08:41 mcr
  5256. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  5257. + *
  5258. + * Revision 1.21 2004/04/29 11:06:42 ken
  5259. + * Last bits from 2.06 procfs updates
  5260. + *
  5261. + * Revision 1.20 2004/04/06 02:49:08 mcr
  5262. + * pullup of algo code from alg-branch.
  5263. + *
  5264. + * Revision 1.19 2004/04/05 19:55:06 mcr
  5265. + * Moved from linux/include/freeswan/ipsec_radij.h,v
  5266. + *
  5267. + * Revision 1.18 2002/04/24 07:36:47 mcr
  5268. + * Moved from ./klips/net/ipsec/ipsec_radij.h,v
  5269. + *
  5270. + * Revision 1.17 2001/11/26 09:23:49 rgb
  5271. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  5272. + *
  5273. + * Revision 1.16.2.1 2001/09/25 02:21:17 mcr
  5274. + * ipsec_proto.h created to keep prototypes rather than deal with
  5275. + * cyclic dependancies of structures and prototypes in .h files.
  5276. + *
  5277. + * Revision 1.16 2001/09/15 16:24:04 rgb
  5278. + * Re-inject first and last HOLD packet when an eroute REPLACE is done.
  5279. + *
  5280. + * Revision 1.15 2001/09/14 16:58:37 rgb
  5281. + * Added support for storing the first and last packets through a HOLD.
  5282. + *
  5283. + * Revision 1.14 2001/09/08 21:13:32 rgb
  5284. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  5285. + *
  5286. + * Revision 1.13 2001/06/14 19:35:09 rgb
  5287. + * Update copyright date.
  5288. + *
  5289. + * Revision 1.12 2001/05/27 06:12:11 rgb
  5290. + * Added structures for pid, packet count and last access time to eroute.
  5291. + * Added packet count to beginning of /proc/net/ipsec_eroute.
  5292. + *
  5293. + * Revision 1.11 2000/09/08 19:12:56 rgb
  5294. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  5295. + *
  5296. + * Revision 1.10 1999/11/17 15:53:39 rgb
  5297. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  5298. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  5299. + * klips/net/ipsec/Makefile.
  5300. + *
  5301. + * Revision 1.9 1999/10/01 00:01:23 rgb
  5302. + * Added eroute structure locking.
  5303. + *
  5304. + * Revision 1.8 1999/04/11 00:28:59 henry
  5305. + * GPL boilerplate
  5306. + *
  5307. + * Revision 1.7 1999/04/06 04:54:26 rgb
  5308. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  5309. + * patch shell fixes.
  5310. + *
  5311. + * Revision 1.6 1999/01/22 06:23:26 rgb
  5312. + * Cruft clean-out.
  5313. + *
  5314. + * Revision 1.5 1998/10/25 02:42:08 rgb
  5315. + * Change return type on ipsec_breakroute and ipsec_makeroute and add an
  5316. + * argument to be able to transmit more infomation about errors.
  5317. + *
  5318. + * Revision 1.4 1998/10/19 14:44:29 rgb
  5319. + * Added inclusion of freeswan.h.
  5320. + * sa_id structure implemented and used: now includes protocol.
  5321. + *
  5322. + * Revision 1.3 1998/07/28 00:03:31 rgb
  5323. + * Comment out temporary inet_nto4u() kluge.
  5324. + *
  5325. + * Revision 1.2 1998/07/14 18:22:00 rgb
  5326. + * Add function to clear the eroute table.
  5327. + *
  5328. + * Revision 1.1 1998/06/18 21:27:49 henry
  5329. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  5330. + * kernel-build scripts happier in the presence of symlinks
  5331. + *
  5332. + * Revision 1.5 1998/05/25 20:30:38 rgb
  5333. + * Remove temporary ipsec_walk, rj_deltree and rj_delnodes functions.
  5334. + *
  5335. + * Rename ipsec_rj_walker (ipsec_walk) to ipsec_rj_walker_procprint and
  5336. + * add ipsec_rj_walker_delete.
  5337. + *
  5338. + * Revision 1.4 1998/05/21 13:02:56 rgb
  5339. + * Imported definitions from ipsec_radij.c and radij.c to support /proc 3k
  5340. + * limit fix.
  5341. + *
  5342. + * Revision 1.3 1998/04/21 21:29:09 rgb
  5343. + * Rearrange debug switches to change on the fly debug output from user
  5344. + * space. Only kernel changes checked in at this time. radij.c was also
  5345. + * changed to temporarily remove buggy debugging code in rj_delete causing
  5346. + * an OOPS and hence, netlink device open errors.
  5347. + *
  5348. + * Revision 1.2 1998/04/14 17:30:39 rgb
  5349. + * Fix up compiling errors for radij tree memory reclamation.
  5350. + *
  5351. + * Revision 1.1 1998/04/09 03:06:10 henry
  5352. + * sources moved up from linux/net/ipsec
  5353. + *
  5354. + * Revision 1.1.1.1 1998/04/08 05:35:04 henry
  5355. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  5356. + *
  5357. + * Revision 0.4 1997/01/15 01:28:15 ji
  5358. + * No changes.
  5359. + *
  5360. + * Revision 0.3 1996/11/20 14:39:04 ji
  5361. + * Minor cleanups.
  5362. + * Rationalized debugging code.
  5363. + *
  5364. + * Revision 0.2 1996/11/02 00:18:33 ji
  5365. + * First limited release.
  5366. + *
  5367. + *
  5368. + */
  5369. --- /dev/null Tue Mar 11 13:02:56 2003
  5370. +++ linux/include/openswan/ipsec_rcv.h Mon Feb 9 13:51:03 2004
  5371. @@ -0,0 +1,199 @@
  5372. +/*
  5373. + *
  5374. + * Copyright (C) 1996, 1997 John Ioannidis.
  5375. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  5376. + *
  5377. + * This program is free software; you can redistribute it and/or modify it
  5378. + * under the terms of the GNU General Public License as published by the
  5379. + * Free Software Foundation; either version 2 of the License, or (at your
  5380. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  5381. + *
  5382. + * This program is distributed in the hope that it will be useful, but
  5383. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  5384. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  5385. + * for more details.
  5386. + *
  5387. + * RCSID $Id: ipsec_rcv.h,v 1.28.2.2 2006/10/06 21:39:26 paul Exp $
  5388. + */
  5389. +
  5390. +#ifndef IPSEC_RCV_H
  5391. +#define IPSEC_RCV_H
  5392. +
  5393. +#include "openswan/ipsec_auth.h"
  5394. +
  5395. +#define DB_RX_PKTRX 0x0001
  5396. +#define DB_RX_PKTRX2 0x0002
  5397. +#define DB_RX_DMP 0x0004
  5398. +#define DB_RX_IPSA 0x0010
  5399. +#define DB_RX_XF 0x0020
  5400. +#define DB_RX_IPAD 0x0040
  5401. +#define DB_RX_INAU 0x0080
  5402. +#define DB_RX_OINFO 0x0100
  5403. +#define DB_RX_OINFO2 0x0200
  5404. +#define DB_RX_OH 0x0400
  5405. +#define DB_RX_REPLAY 0x0800
  5406. +
  5407. +#ifdef __KERNEL__
  5408. +/* struct options; */
  5409. +
  5410. +#define __NO_VERSION__
  5411. +#ifndef AUTOCONF_INCLUDED
  5412. +#include <linux/config.h> /* for CONFIG_IP_FORWARD */
  5413. +#endif
  5414. +#ifdef CONFIG_MODULES
  5415. +#include <linux/module.h>
  5416. +#endif
  5417. +#include <linux/version.h>
  5418. +#include <openswan.h>
  5419. +
  5420. +#define IPSEC_BIRTH_TEMPLATE_MAXLEN 256
  5421. +
  5422. +struct ipsec_birth_reply {
  5423. + int packet_template_len;
  5424. + unsigned char packet_template[IPSEC_BIRTH_TEMPLATE_MAXLEN];
  5425. +};
  5426. +
  5427. +extern struct ipsec_birth_reply ipsec_ipv4_birth_packet;
  5428. +extern struct ipsec_birth_reply ipsec_ipv6_birth_packet;
  5429. +
  5430. +enum ipsec_rcv_value {
  5431. + IPSEC_RCV_LASTPROTO=1,
  5432. + IPSEC_RCV_OK=0,
  5433. + IPSEC_RCV_BADPROTO=-1,
  5434. + IPSEC_RCV_BADLEN=-2,
  5435. + IPSEC_RCV_ESP_BADALG=-3,
  5436. + IPSEC_RCV_3DES_BADBLOCKING=-4,
  5437. + IPSEC_RCV_ESP_DECAPFAIL=-5,
  5438. + IPSEC_RCV_DECAPFAIL=-6,
  5439. + IPSEC_RCV_SAIDNOTFOUND=-7,
  5440. + IPSEC_RCV_IPCOMPALONE=-8,
  5441. + IPSEC_RCV_IPCOMPFAILED=-10,
  5442. + IPSEC_RCV_SAIDNOTLIVE=-11,
  5443. + IPSEC_RCV_FAILEDINBOUND=-12,
  5444. + IPSEC_RCV_LIFETIMEFAILED=-13,
  5445. + IPSEC_RCV_BADAUTH=-14,
  5446. + IPSEC_RCV_REPLAYFAILED=-15,
  5447. + IPSEC_RCV_AUTHFAILED=-16,
  5448. + IPSEC_RCV_REPLAYROLLED=-17,
  5449. + IPSEC_RCV_BAD_DECRYPT=-18
  5450. +};
  5451. +
  5452. +struct ipsec_rcv_state {
  5453. + struct sk_buff *skb;
  5454. + struct net_device_stats *stats;
  5455. + struct iphdr *ipp; /* the IP header */
  5456. + struct ipsec_sa *ipsp; /* current SA being processed */
  5457. + int len; /* length of packet */
  5458. + int ilen; /* length of inner payload (-authlen) */
  5459. + int authlen; /* how big is the auth data at end */
  5460. + int hard_header_len; /* layer 2 size */
  5461. + int iphlen; /* how big is IP header */
  5462. + struct auth_alg *authfuncs;
  5463. + ip_said said;
  5464. + char sa[SATOT_BUF];
  5465. + size_t sa_len;
  5466. + __u8 next_header;
  5467. + __u8 hash[AH_AMAX];
  5468. + char ipsaddr_txt[ADDRTOA_BUF];
  5469. + char ipdaddr_txt[ADDRTOA_BUF];
  5470. + __u8 *octx;
  5471. + __u8 *ictx;
  5472. + int ictx_len;
  5473. + int octx_len;
  5474. + union {
  5475. + struct {
  5476. + struct esphdr *espp;
  5477. + } espstuff;
  5478. + struct {
  5479. + struct ahhdr *ahp;
  5480. + } ahstuff;
  5481. + struct {
  5482. + struct ipcomphdr *compp;
  5483. + } ipcompstuff;
  5484. + } protostuff;
  5485. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  5486. + __u8 natt_type;
  5487. + __u16 natt_sport;
  5488. + __u16 natt_dport;
  5489. + int natt_len;
  5490. +#endif
  5491. +};
  5492. +
  5493. +extern int
  5494. +#ifdef PROTO_HANDLER_SINGLE_PARM
  5495. +ipsec_rcv(struct sk_buff *skb);
  5496. +#else /* PROTO_HANDLER_SINGLE_PARM */
  5497. +ipsec_rcv(struct sk_buff *skb,
  5498. + unsigned short xlen);
  5499. +#endif /* PROTO_HANDLER_SINGLE_PARM */
  5500. +
  5501. +#ifdef CONFIG_KLIPS_DEBUG
  5502. +extern int debug_rcv;
  5503. +#define ipsec_rcv_dmp(_x,_y, _z) if (debug_rcv && sysctl_ipsec_debug_verbose) ipsec_dmp_block(_x,_y,_z)
  5504. +#else
  5505. +#define ipsec_rcv_dmp(_x,_y, _z) do {} while(0)
  5506. +#endif /* CONFIG_KLIPS_DEBUG */
  5507. +
  5508. +extern int sysctl_ipsec_inbound_policy_check;
  5509. +#endif /* __KERNEL__ */
  5510. +
  5511. +extern int klips26_rcv_encap(struct sk_buff *skb, __u16 encap_type);
  5512. +
  5513. +
  5514. +#endif /* IPSEC_RCV_H */
  5515. +
  5516. +/*
  5517. + * $Log: ipsec_rcv.h,v $
  5518. + * Revision 1.28.2.2 2006/10/06 21:39:26 paul
  5519. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  5520. + * set. This is defined through autoconf.h which is included through the
  5521. + * linux kernel build macros.
  5522. + *
  5523. + * Revision 1.28.2.1 2006/07/10 15:52:20 paul
  5524. + * Fix for bug #642 by Bart Trojanowski
  5525. + *
  5526. + * Revision 1.28 2005/05/11 00:59:45 mcr
  5527. + * do not call debug routines if !defined KLIPS_DEBUG.
  5528. + *
  5529. + * Revision 1.27 2005/04/29 04:59:46 mcr
  5530. + * use ipsec_dmp_block.
  5531. + *
  5532. + * Revision 1.26 2005/04/13 22:48:35 mcr
  5533. + * added comments, and removed some log.
  5534. + * removed Linux 2.0 support.
  5535. + *
  5536. + * Revision 1.25 2005/04/08 18:25:37 mcr
  5537. + * prototype klips26 encap receive function
  5538. + *
  5539. + * Revision 1.24 2004/08/20 21:45:37 mcr
  5540. + * CONFIG_KLIPS_NAT_TRAVERSAL is not used in an attempt to
  5541. + * be 26sec compatible. But, some defines where changed.
  5542. + *
  5543. + * Revision 1.23 2004/08/03 18:17:40 mcr
  5544. + * in 2.6, use "net_device" instead of #define device->net_device.
  5545. + * this probably breaks 2.0 compiles.
  5546. + *
  5547. + * Revision 1.22 2004/07/10 19:08:41 mcr
  5548. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  5549. + *
  5550. + * Revision 1.21 2004/04/06 02:49:08 mcr
  5551. + * pullup of algo code from alg-branch.
  5552. + *
  5553. + * Revision 1.20 2004/04/05 19:55:06 mcr
  5554. + * Moved from linux/include/freeswan/ipsec_rcv.h,v
  5555. + *
  5556. + * Revision 1.19 2003/12/15 18:13:09 mcr
  5557. + * when compiling with NAT traversal, don't assume that the
  5558. + * kernel has been patched, unless CONFIG_IPSEC_NAT_NON_ESP
  5559. + * is set.
  5560. + *
  5561. + * history elided 2005-04-12.
  5562. + *
  5563. + * Local Variables:
  5564. + * c-basic-offset:8
  5565. + * c-style:linux
  5566. + * End:
  5567. + *
  5568. + */
  5569. +
  5570. +
  5571. --- /dev/null Tue Mar 11 13:02:56 2003
  5572. +++ linux/include/openswan/ipsec_sa.h Mon Feb 9 13:51:03 2004
  5573. @@ -0,0 +1,355 @@
  5574. +/*
  5575. + * @(#) Definitions of IPsec Security Association (ipsec_sa)
  5576. + *
  5577. + * Copyright (C) 2001, 2002, 2003
  5578. + * Richard Guy Briggs <rgb@freeswan.org>
  5579. + * and Michael Richardson <mcr@freeswan.org>
  5580. + *
  5581. + * This program is free software; you can redistribute it and/or modify it
  5582. + * under the terms of the GNU General Public License as published by the
  5583. + * Free Software Foundation; either version 2 of the License, or (at your
  5584. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  5585. + *
  5586. + * This program is distributed in the hope that it will be useful, but
  5587. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  5588. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  5589. + * for more details.
  5590. + *
  5591. + * RCSID $Id: ipsec_sa.h,v 1.23 2005/05/11 01:18:59 mcr Exp $
  5592. + *
  5593. + * This file derived from ipsec_xform.h on 2001/9/18 by mcr.
  5594. + *
  5595. + */
  5596. +
  5597. +/*
  5598. + * This file describes the IPsec Security Association Structure.
  5599. + *
  5600. + * This structure keeps track of a single transform that may be done
  5601. + * to a set of packets. It can describe applying the transform or
  5602. + * apply the reverse. (e.g. compression vs expansion). However, it
  5603. + * only describes one at a time. To describe both, two structures would
  5604. + * be used, but since the sides of the transform are performed
  5605. + * on different machines typically it is usual to have only one side
  5606. + * of each association.
  5607. + *
  5608. + */
  5609. +
  5610. +#ifndef _IPSEC_SA_H_
  5611. +
  5612. +#ifdef __KERNEL__
  5613. +#include "openswan/ipsec_stats.h"
  5614. +#include "openswan/ipsec_life.h"
  5615. +#include "openswan/ipsec_eroute.h"
  5616. +#endif /* __KERNEL__ */
  5617. +#include "openswan/ipsec_param.h"
  5618. +
  5619. +#include "pfkeyv2.h"
  5620. +
  5621. +
  5622. +/* SAs are held in a table.
  5623. + * Entries in this table are referenced by IPsecSAref_t values.
  5624. + * IPsecSAref_t values are conceptually subscripts. Because
  5625. + * we want to allocate the table piece-meal, the subscripting
  5626. + * is implemented with two levels, a bit like paged virtual memory.
  5627. + * This representation mechanism is known as an Iliffe Vector.
  5628. + *
  5629. + * The Main table (AKA the refTable) consists of 2^IPSEC_SA_REF_MAINTABLE_IDX_WIDTH
  5630. + * pointers to subtables.
  5631. + * Each subtable has 2^IPSEC_SA_REF_SUBTABLE_IDX_WIDTH entries, each of which
  5632. + * is a pointer to an SA.
  5633. + *
  5634. + * An IPsecSAref_t contains either an exceptional value (signified by the
  5635. + * high-order bit being on) or a reference to a table entry. A table entry
  5636. + * reference has the subtable subscript in the low-order
  5637. + * IPSEC_SA_REF_SUBTABLE_IDX_WIDTH bits and the Main table subscript
  5638. + * in the next lowest IPSEC_SA_REF_MAINTABLE_IDX_WIDTH bits.
  5639. + *
  5640. + * The Maintable entry for an IPsecSAref_t x, a pointer to its subtable, is
  5641. + * IPsecSAref2table(x). It is of type struct IPsecSArefSubTable *.
  5642. + *
  5643. + * The pointer to the SA for x is IPsecSAref2SA(x). It is of type
  5644. + * struct ipsec_sa*. The macro definition clearly shows the two-level
  5645. + * access needed to find the SA pointer.
  5646. + *
  5647. + * The Maintable is allocated when IPsec is initialized.
  5648. + * Each subtable is allocated when needed, but the first is allocated
  5649. + * when IPsec is initialized.
  5650. + *
  5651. + * IPsecSAref_t is designed to be smaller than an NFmark so that
  5652. + * they can be stored in NFmarks and still leave a few bits for other
  5653. + * purposes. The spare bits are in the low order of the NFmark
  5654. + * but in the high order of the IPsecSAref_t, so conversion is required.
  5655. + * We pick the upper bits of NFmark on the theory that they are less likely to
  5656. + * interfere with more pedestrian uses of nfmark.
  5657. + */
  5658. +
  5659. +
  5660. +typedef unsigned short int IPsecRefTableUnusedCount;
  5661. +
  5662. +#define IPSEC_SA_REF_TABLE_NUM_ENTRIES (1 << IPSEC_SA_REF_TABLE_IDX_WIDTH)
  5663. +
  5664. +#ifdef __KERNEL__
  5665. +#if ((IPSEC_SA_REF_TABLE_IDX_WIDTH - (1 + IPSEC_SA_REF_MAINTABLE_IDX_WIDTH)) < 0)
  5666. +#error "IPSEC_SA_REF_TABLE_IDX_WIDTH("IPSEC_SA_REF_TABLE_IDX_WIDTH") MUST be < 1 + IPSEC_SA_REF_MAINTABLE_IDX_WIDTH("IPSEC_SA_REF_MAINTABLE_IDX_WIDTH")"
  5667. +#endif
  5668. +
  5669. +#define IPSEC_SA_REF_SUBTABLE_IDX_WIDTH (IPSEC_SA_REF_TABLE_IDX_WIDTH - IPSEC_SA_REF_MAINTABLE_IDX_WIDTH)
  5670. +
  5671. +#define IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES (1 << IPSEC_SA_REF_MAINTABLE_IDX_WIDTH)
  5672. +#define IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES (1 << IPSEC_SA_REF_SUBTABLE_IDX_WIDTH)
  5673. +
  5674. +#ifdef CONFIG_NETFILTER
  5675. +#define IPSEC_SA_REF_HOST_FIELD(x) ((struct sk_buff*)(x))->nfmark
  5676. +#define IPSEC_SA_REF_HOST_FIELD_TYPE typeof(IPSEC_SA_REF_HOST_FIELD(NULL))
  5677. +#else /* CONFIG_NETFILTER */
  5678. +/* just make it work for now, it doesn't matter, since there is no nfmark */
  5679. +#define IPSEC_SA_REF_HOST_FIELD_TYPE unsigned long
  5680. +#endif /* CONFIG_NETFILTER */
  5681. +#define IPSEC_SA_REF_HOST_FIELD_WIDTH (8 * sizeof(IPSEC_SA_REF_HOST_FIELD_TYPE))
  5682. +#define IPSEC_SA_REF_FIELD_WIDTH (8 * sizeof(IPsecSAref_t))
  5683. +
  5684. +#define IPSEC_SA_REF_MASK (IPSEC_SAREF_NULL >> (IPSEC_SA_REF_FIELD_WIDTH - IPSEC_SA_REF_TABLE_IDX_WIDTH))
  5685. +#define IPSEC_SA_REF_TABLE_MASK ((IPSEC_SAREF_NULL >> (IPSEC_SA_REF_FIELD_WIDTH - IPSEC_SA_REF_MAINTABLE_IDX_WIDTH)) << IPSEC_SA_REF_SUBTABLE_IDX_WIDTH)
  5686. +#define IPSEC_SA_REF_ENTRY_MASK (IPSEC_SAREF_NULL >> (IPSEC_SA_REF_FIELD_WIDTH - IPSEC_SA_REF_SUBTABLE_IDX_WIDTH))
  5687. +
  5688. +#define IPsecSAref2table(x) (((x) & IPSEC_SA_REF_TABLE_MASK) >> IPSEC_SA_REF_SUBTABLE_IDX_WIDTH)
  5689. +#define IPsecSAref2entry(x) ((x) & IPSEC_SA_REF_ENTRY_MASK)
  5690. +#define IPsecSArefBuild(x,y) (((x) << IPSEC_SA_REF_SUBTABLE_IDX_WIDTH) + (y))
  5691. +
  5692. +#define IPsecSAref2SA(x) (ipsec_sadb.refTable[IPsecSAref2table(x)]->entry[IPsecSAref2entry(x)])
  5693. +#define IPsecSA2SAref(x) ((x)->ips_ref)
  5694. +
  5695. +#define EMT_INBOUND 0x01 /* SA direction, 1=inbound */
  5696. +
  5697. +/* 'struct ipsec_sa' should be 64bit aligned when allocated. */
  5698. +struct ipsec_sa
  5699. +{
  5700. + IPsecSAref_t ips_ref; /* reference table entry number */
  5701. + atomic_t ips_refcount; /* reference count for this struct */
  5702. + struct ipsec_sa *ips_hnext; /* next in hash chain */
  5703. + struct ipsec_sa *ips_inext; /* pointer to next xform */
  5704. + struct ipsec_sa *ips_onext; /* pointer to prev xform */
  5705. +
  5706. + struct ifnet *ips_rcvif; /* related rcv encap interface */
  5707. +
  5708. + ip_said ips_said; /* SA ID */
  5709. +
  5710. + __u32 ips_seq; /* seq num of msg that initiated this SA */
  5711. + __u32 ips_pid; /* PID of process that initiated this SA */
  5712. + __u8 ips_authalg; /* auth algorithm for this SA */
  5713. + __u8 ips_encalg; /* enc algorithm for this SA */
  5714. +
  5715. + struct ipsec_stats ips_errs;
  5716. +
  5717. + __u8 ips_replaywin; /* replay window size */
  5718. + enum sadb_sastate ips_state; /* state of SA */
  5719. + __u32 ips_replaywin_lastseq; /* last pkt sequence num */
  5720. + __u64 ips_replaywin_bitmap; /* bitmap of received pkts */
  5721. + __u32 ips_replaywin_maxdiff; /* max pkt sequence difference */
  5722. +
  5723. + __u32 ips_flags; /* generic xform flags */
  5724. +
  5725. +
  5726. + struct ipsec_lifetimes ips_life; /* lifetime records */
  5727. +
  5728. + /* selector information */
  5729. + __u8 ips_transport_protocol; /* protocol for this SA, if ports are involved */
  5730. + struct sockaddr*ips_addr_s; /* src sockaddr */
  5731. + struct sockaddr*ips_addr_d; /* dst sockaddr */
  5732. + struct sockaddr*ips_addr_p; /* proxy sockaddr */
  5733. + __u16 ips_addr_s_size;
  5734. + __u16 ips_addr_d_size;
  5735. + __u16 ips_addr_p_size;
  5736. + ip_address ips_flow_s;
  5737. + ip_address ips_flow_d;
  5738. + ip_address ips_mask_s;
  5739. + ip_address ips_mask_d;
  5740. +
  5741. + __u16 ips_key_bits_a; /* size of authkey in bits */
  5742. + __u16 ips_auth_bits; /* size of authenticator in bits */
  5743. + __u16 ips_key_bits_e; /* size of enckey in bits */
  5744. + __u16 ips_iv_bits; /* size of IV in bits */
  5745. + __u8 ips_iv_size;
  5746. + __u16 ips_key_a_size;
  5747. + __u16 ips_key_e_size;
  5748. +
  5749. + caddr_t ips_key_a; /* authentication key */
  5750. + caddr_t ips_key_e; /* encryption key */
  5751. + caddr_t ips_iv; /* Initialisation Vector */
  5752. +
  5753. + struct ident ips_ident_s; /* identity src */
  5754. + struct ident ips_ident_d; /* identity dst */
  5755. +
  5756. + /* these are included even if CONFIG_KLIPS_IPCOMP is off */
  5757. + __u16 ips_comp_adapt_tries; /* ipcomp self-adaption tries */
  5758. + __u16 ips_comp_adapt_skip; /* ipcomp self-adaption to-skip */
  5759. + __u64 ips_comp_ratio_cbytes; /* compressed bytes */
  5760. + __u64 ips_comp_ratio_dbytes; /* decompressed (or uncompressed) bytes */
  5761. +
  5762. + /* these are included even if CONFIG_IPSEC_NAT_TRAVERSAL is off */
  5763. + __u8 ips_natt_type;
  5764. + __u8 ips_natt_reserved[3];
  5765. + __u16 ips_natt_sport;
  5766. + __u16 ips_natt_dport;
  5767. +
  5768. + struct sockaddr *ips_natt_oa;
  5769. + __u16 ips_natt_oa_size;
  5770. + __u16 ips_natt_reserved2;
  5771. +
  5772. +#if 0
  5773. + __u32 ips_sens_dpd;
  5774. + __u8 ips_sens_sens_level;
  5775. + __u8 ips_sens_sens_len;
  5776. + __u64* ips_sens_sens_bitmap;
  5777. + __u8 ips_sens_integ_level;
  5778. + __u8 ips_sens_integ_len;
  5779. + __u64* ips_sens_integ_bitmap;
  5780. +#endif
  5781. + struct ipsec_alg_enc *ips_alg_enc;
  5782. + struct ipsec_alg_auth *ips_alg_auth;
  5783. + IPsecSAref_t ips_ref_rel;
  5784. +};
  5785. +
  5786. +struct IPsecSArefSubTable
  5787. +{
  5788. + struct ipsec_sa* entry[IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES];
  5789. +};
  5790. +
  5791. +struct ipsec_sadb {
  5792. + struct IPsecSArefSubTable* refTable[IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES];
  5793. + IPsecSAref_t refFreeList[IPSEC_SA_REF_FREELIST_NUM_ENTRIES];
  5794. + int refFreeListHead;
  5795. + int refFreeListTail;
  5796. + IPsecSAref_t refFreeListCont;
  5797. + IPsecSAref_t said_hash[SADB_HASHMOD];
  5798. + spinlock_t sadb_lock;
  5799. +};
  5800. +
  5801. +extern struct ipsec_sadb ipsec_sadb;
  5802. +
  5803. +extern int ipsec_SAref_recycle(void);
  5804. +extern int ipsec_SArefSubTable_alloc(unsigned table);
  5805. +extern int ipsec_saref_freelist_init(void);
  5806. +extern int ipsec_sadb_init(void);
  5807. +extern struct ipsec_sa *ipsec_sa_alloc(int*error); /* pass in error var by pointer */
  5808. +extern IPsecSAref_t ipsec_SAref_alloc(int*erorr); /* pass in error var by pointer */
  5809. +extern int ipsec_sa_free(struct ipsec_sa* ips);
  5810. +extern int ipsec_sa_put(struct ipsec_sa *ips);
  5811. +extern int ipsec_sa_add(struct ipsec_sa *ips);
  5812. +extern int ipsec_sa_del(struct ipsec_sa *ips);
  5813. +extern int ipsec_sa_delchain(struct ipsec_sa *ips);
  5814. +extern int ipsec_sadb_cleanup(__u8 proto);
  5815. +extern int ipsec_sadb_free(void);
  5816. +extern int ipsec_sa_wipe(struct ipsec_sa *ips);
  5817. +#endif /* __KERNEL__ */
  5818. +
  5819. +enum ipsec_direction {
  5820. + ipsec_incoming = 1,
  5821. + ipsec_outgoing = 2
  5822. +};
  5823. +
  5824. +#define _IPSEC_SA_H_
  5825. +#endif /* _IPSEC_SA_H_ */
  5826. +
  5827. +/*
  5828. + * $Log: ipsec_sa.h,v $
  5829. + * Revision 1.23 2005/05/11 01:18:59 mcr
  5830. + * do not change structure based upon options, to avoid
  5831. + * too many #ifdef.
  5832. + *
  5833. + * Revision 1.22 2005/04/14 01:17:09 mcr
  5834. + * change sadb_state to an enum.
  5835. + *
  5836. + * Revision 1.21 2004/08/20 21:45:37 mcr
  5837. + * CONFIG_KLIPS_NAT_TRAVERSAL is not used in an attempt to
  5838. + * be 26sec compatible. But, some defines where changed.
  5839. + *
  5840. + * Revision 1.20 2004/07/10 19:08:41 mcr
  5841. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  5842. + *
  5843. + * Revision 1.19 2004/04/05 19:55:06 mcr
  5844. + * Moved from linux/include/freeswan/ipsec_sa.h,v
  5845. + *
  5846. + * Revision 1.18 2004/04/05 19:41:05 mcr
  5847. + * merged alg-branch code.
  5848. + *
  5849. + * Revision 1.17.2.1 2003/12/22 15:25:52 jjo
  5850. + * . Merged algo-0.8.1-rc11-test1 into alg-branch
  5851. + *
  5852. + * Revision 1.17 2003/12/10 01:20:06 mcr
  5853. + * NAT-traversal patches to KLIPS.
  5854. + *
  5855. + * Revision 1.16 2003/10/31 02:27:05 mcr
  5856. + * pulled up port-selector patches and sa_id elimination.
  5857. + *
  5858. + * Revision 1.15.4.1 2003/10/29 01:10:19 mcr
  5859. + * elimited "struct sa_id"
  5860. + *
  5861. + * Revision 1.15 2003/05/11 00:53:09 mcr
  5862. + * IPsecSAref_t and macros were moved to freeswan.h.
  5863. + *
  5864. + * Revision 1.14 2003/02/12 19:31:55 rgb
  5865. + * Fixed bug in "file seen" machinery.
  5866. + * Updated copyright year.
  5867. + *
  5868. + * Revision 1.13 2003/01/30 02:31:52 rgb
  5869. + *
  5870. + * Re-wrote comments describing SAref system for accuracy.
  5871. + * Rename SAref table macro names for clarity.
  5872. + * Convert IPsecSAref_t from signed to unsigned to fix apparent SAref exhaustion bug.
  5873. + * Transmit error code through to caller from callee for better diagnosis of problems.
  5874. + * Enclose all macro arguments in parens to avoid any possible obscrure bugs.
  5875. + *
  5876. + * Revision 1.12 2002/10/07 18:31:19 rgb
  5877. + * Change comment to reflect the flexible nature of the main and sub-table widths.
  5878. + * Added a counter for the number of unused entries in each subtable.
  5879. + * Further break up host field type macro to host field.
  5880. + * Move field width sanity checks to ipsec_sa.c
  5881. + * Define a mask for an entire saref.
  5882. + *
  5883. + * Revision 1.11 2002/09/20 15:40:33 rgb
  5884. + * Re-write most of the SAref macros and types to eliminate any pointer references to Entrys.
  5885. + * Fixed SAref/nfmark macros.
  5886. + * Rework saref freeslist.
  5887. + * Place all ipsec sadb globals into one struct.
  5888. + * Restrict some bits to kernel context for use to klips utils.
  5889. + *
  5890. + * Revision 1.10 2002/09/20 05:00:34 rgb
  5891. + * Update copyright date.
  5892. + *
  5893. + * Revision 1.9 2002/09/17 17:19:29 mcr
  5894. + * make it compile even if there is no netfilter - we lost
  5895. + * functionality, but it works, especially on 2.2.
  5896. + *
  5897. + * Revision 1.8 2002/07/28 22:59:53 mcr
  5898. + * clarified/expanded one comment.
  5899. + *
  5900. + * Revision 1.7 2002/07/26 08:48:31 rgb
  5901. + * Added SA ref table code.
  5902. + *
  5903. + * Revision 1.6 2002/05/31 17:27:48 rgb
  5904. + * Comment fix.
  5905. + *
  5906. + * Revision 1.5 2002/05/27 18:55:03 rgb
  5907. + * Remove final vistiges of tdb references via IPSEC_KLIPS1_COMPAT.
  5908. + *
  5909. + * Revision 1.4 2002/05/23 07:13:36 rgb
  5910. + * Convert "usecount" to "refcount" to remove ambiguity.
  5911. + *
  5912. + * Revision 1.3 2002/04/24 07:36:47 mcr
  5913. + * Moved from ./klips/net/ipsec/ipsec_sa.h,v
  5914. + *
  5915. + * Revision 1.2 2001/11/26 09:16:15 rgb
  5916. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  5917. + *
  5918. + * Revision 1.1.2.1 2001/09/25 02:24:58 mcr
  5919. + * struct tdb -> struct ipsec_sa.
  5920. + * sa(tdb) manipulation functions renamed and moved to ipsec_sa.c
  5921. + * ipsec_xform.c removed. header file still contains useful things.
  5922. + *
  5923. + *
  5924. + * Local variables:
  5925. + * c-file-style: "linux"
  5926. + * End:
  5927. + *
  5928. + */
  5929. --- /dev/null Tue Mar 11 13:02:56 2003
  5930. +++ linux/include/openswan/ipsec_sha1.h Mon Feb 9 13:51:03 2004
  5931. @@ -0,0 +1,79 @@
  5932. +/*
  5933. + * RCSID $Id: ipsec_sha1.h,v 1.8 2004/04/05 19:55:07 mcr Exp $
  5934. + */
  5935. +
  5936. +/*
  5937. + * Here is the original comment from the distribution:
  5938. +
  5939. +SHA-1 in C
  5940. +By Steve Reid <steve@edmweb.com>
  5941. +100% Public Domain
  5942. +
  5943. + * Adapted for use by the IPSEC code by John Ioannidis
  5944. + */
  5945. +
  5946. +
  5947. +#ifndef _IPSEC_SHA1_H_
  5948. +#define _IPSEC_SHA1_H_
  5949. +
  5950. +typedef struct
  5951. +{
  5952. + __u32 state[5];
  5953. + __u32 count[2];
  5954. + __u8 buffer[64];
  5955. +} SHA1_CTX;
  5956. +
  5957. +void SHA1Transform(__u32 state[5], __u8 buffer[64]);
  5958. +void SHA1Init(void *context);
  5959. +void SHA1Update(void *context, unsigned char *data, __u32 len);
  5960. +void SHA1Final(unsigned char digest[20], void *context);
  5961. +
  5962. +
  5963. +#endif /* _IPSEC_SHA1_H_ */
  5964. +
  5965. +/*
  5966. + * $Log: ipsec_sha1.h,v $
  5967. + * Revision 1.8 2004/04/05 19:55:07 mcr
  5968. + * Moved from linux/include/freeswan/ipsec_sha1.h,v
  5969. + *
  5970. + * Revision 1.7 2002/09/10 01:45:09 mcr
  5971. + * changed type of MD5_CTX and SHA1_CTX to void * so that
  5972. + * the function prototypes would match, and could be placed
  5973. + * into a pointer to a function.
  5974. + *
  5975. + * Revision 1.6 2002/04/24 07:36:47 mcr
  5976. + * Moved from ./klips/net/ipsec/ipsec_sha1.h,v
  5977. + *
  5978. + * Revision 1.5 1999/12/13 13:59:13 rgb
  5979. + * Quick fix to argument size to Update bugs.
  5980. + *
  5981. + * Revision 1.4 1999/12/07 18:16:23 rgb
  5982. + * Fixed comments at end of #endif lines.
  5983. + *
  5984. + * Revision 1.3 1999/04/06 04:54:27 rgb
  5985. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  5986. + * patch shell fixes.
  5987. + *
  5988. + * Revision 1.2 1998/11/30 13:22:54 rgb
  5989. + * Rationalised all the klips kernel file headers. They are much shorter
  5990. + * now and won't conflict under RH5.2.
  5991. + *
  5992. + * Revision 1.1 1998/06/18 21:27:50 henry
  5993. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  5994. + * kernel-build scripts happier in the presence of symlinks
  5995. + *
  5996. + * Revision 1.2 1998/04/23 20:54:05 rgb
  5997. + * Fixed md5 and sha1 include file nesting issues, to be cleaned up when
  5998. + * verified.
  5999. + *
  6000. + * Revision 1.1 1998/04/09 03:04:21 henry
  6001. + * sources moved up from linux/net/ipsec
  6002. + * these two include files modified not to include others except in kernel
  6003. + *
  6004. + * Revision 1.1.1.1 1998/04/08 05:35:04 henry
  6005. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  6006. + *
  6007. + * Revision 0.4 1997/01/15 01:28:15 ji
  6008. + * New transform
  6009. + *
  6010. + */
  6011. --- /dev/null Tue Mar 11 13:02:56 2003
  6012. +++ linux/include/openswan/ipsec_stats.h Mon Feb 9 13:51:03 2004
  6013. @@ -0,0 +1,76 @@
  6014. +/*
  6015. + * @(#) definition of ipsec_stats structure
  6016. + *
  6017. + * Copyright (C) 2001 Richard Guy Briggs <rgb@freeswan.org>
  6018. + * and Michael Richardson <mcr@freeswan.org>
  6019. + *
  6020. + * This program is free software; you can redistribute it and/or modify it
  6021. + * under the terms of the GNU General Public License as published by the
  6022. + * Free Software Foundation; either version 2 of the License, or (at your
  6023. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  6024. + *
  6025. + * This program is distributed in the hope that it will be useful, but
  6026. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  6027. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  6028. + * for more details.
  6029. + *
  6030. + * RCSID $Id: ipsec_stats.h,v 1.7 2005/04/14 01:17:45 mcr Exp $
  6031. + *
  6032. + */
  6033. +
  6034. +/*
  6035. + * This file describes the errors/statistics that FreeSWAN collects.
  6036. + */
  6037. +
  6038. +#ifndef _IPSEC_STATS_H_
  6039. +
  6040. +struct ipsec_stats {
  6041. + __u32 ips_alg_errs; /* number of algorithm errors */
  6042. + __u32 ips_auth_errs; /* # of authentication errors */
  6043. + __u32 ips_encsize_errs; /* # of encryption size errors*/
  6044. + __u32 ips_encpad_errs; /* # of encryption pad errors*/
  6045. + __u32 ips_replaywin_errs; /* # of pkt sequence errors */
  6046. +};
  6047. +
  6048. +#define _IPSEC_STATS_H_
  6049. +#endif /* _IPSEC_STATS_H_ */
  6050. +
  6051. +/*
  6052. + * $Log: ipsec_stats.h,v $
  6053. + * Revision 1.7 2005/04/14 01:17:45 mcr
  6054. + * add prototypes for snprintf.
  6055. + *
  6056. + * Revision 1.6 2004/04/05 19:55:07 mcr
  6057. + * Moved from linux/include/freeswan/ipsec_stats.h,v
  6058. + *
  6059. + * Revision 1.5 2004/04/05 19:41:05 mcr
  6060. + * merged alg-branch code.
  6061. + *
  6062. + * Revision 1.4 2004/03/28 20:27:19 paul
  6063. + * Included tested and confirmed fixes mcr made and dhr verified for
  6064. + * snprint statements. Changed one other snprintf to use ipsec_snprintf
  6065. + * so it wouldnt break compatibility with 2.0/2.2 kernels. Verified with
  6066. + * dhr. (thanks dhr!)
  6067. + *
  6068. + * Revision 1.4 2004/03/24 01:58:31 mcr
  6069. + * sprintf->snprintf for formatting into proc buffer.
  6070. + *
  6071. + * Revision 1.3.34.1 2004/04/05 04:30:46 mcr
  6072. + * patches for alg-branch to compile/work with 2.x openswan
  6073. + *
  6074. + * Revision 1.3 2002/04/24 07:36:47 mcr
  6075. + * Moved from ./klips/net/ipsec/ipsec_stats.h,v
  6076. + *
  6077. + * Revision 1.2 2001/11/26 09:16:16 rgb
  6078. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  6079. + *
  6080. + * Revision 1.1.2.1 2001/09/25 02:27:00 mcr
  6081. + * statistics moved to seperate structure.
  6082. + *
  6083. + *
  6084. + *
  6085. + * Local variables:
  6086. + * c-file-style: "linux"
  6087. + * End:
  6088. + *
  6089. + */
  6090. --- /dev/null Tue Mar 11 13:02:56 2003
  6091. +++ linux/include/openswan/ipsec_tunnel.h Mon Feb 9 13:51:03 2004
  6092. @@ -0,0 +1,280 @@
  6093. +/*
  6094. + * IPSEC tunneling code
  6095. + * Copyright (C) 1996, 1997 John Ioannidis.
  6096. + * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Richard Guy Briggs.
  6097. + *
  6098. + * This program is free software; you can redistribute it and/or modify it
  6099. + * under the terms of the GNU General Public License as published by the
  6100. + * Free Software Foundation; either version 2 of the License, or (at your
  6101. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  6102. + *
  6103. + * This program is distributed in the hope that it will be useful, but
  6104. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  6105. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  6106. + * for more details.
  6107. + *
  6108. + * RCSID $Id: ipsec_tunnel.h,v 1.33 2005/06/04 16:06:05 mcr Exp $
  6109. + */
  6110. +
  6111. +
  6112. +#ifdef NET_21
  6113. +# define DEV_QUEUE_XMIT(skb, device, pri) {\
  6114. + skb->dev = device; \
  6115. + neigh_compat_output(skb); \
  6116. + /* skb->dst->output(skb); */ \
  6117. + }
  6118. +# define ICMP_SEND(skb_in, type, code, info, dev) \
  6119. + icmp_send(skb_in, type, code, htonl(info))
  6120. +# define IP_SEND(skb, dev) \
  6121. + ip_send(skb);
  6122. +#else /* NET_21 */
  6123. +# define DEV_QUEUE_XMIT(skb, device, pri) {\
  6124. + dev_queue_xmit(skb, device, pri); \
  6125. + }
  6126. +# define ICMP_SEND(skb_in, type, code, info, dev) \
  6127. + icmp_send(skb_in, type, code, info, dev)
  6128. +# define IP_SEND(skb, dev) \
  6129. + if(ntohs(iph->tot_len) > physmtu) { \
  6130. + ip_fragment(NULL, skb, dev, 0); \
  6131. + ipsec_kfree_skb(skb); \
  6132. + } else { \
  6133. + dev_queue_xmit(skb, dev, SOPRI_NORMAL); \
  6134. + }
  6135. +#endif /* NET_21 */
  6136. +
  6137. +
  6138. +/*
  6139. + * Heavily based on drivers/net/new_tunnel.c. Lots
  6140. + * of ideas also taken from the 2.1.x version of drivers/net/shaper.c
  6141. + */
  6142. +
  6143. +struct ipsectunnelconf
  6144. +{
  6145. + __u32 cf_cmd;
  6146. + union
  6147. + {
  6148. + char cfu_name[12];
  6149. + } cf_u;
  6150. +#define cf_name cf_u.cfu_name
  6151. +};
  6152. +
  6153. +#define IPSEC_SET_DEV (SIOCDEVPRIVATE)
  6154. +#define IPSEC_DEL_DEV (SIOCDEVPRIVATE + 1)
  6155. +#define IPSEC_CLR_DEV (SIOCDEVPRIVATE + 2)
  6156. +
  6157. +#ifdef __KERNEL__
  6158. +#include <linux/version.h>
  6159. +#ifndef KERNEL_VERSION
  6160. +# define KERNEL_VERSION(x,y,z) (((x)<<16)+((y)<<8)+(z))
  6161. +#endif
  6162. +struct ipsecpriv
  6163. +{
  6164. + struct sk_buff_head sendq;
  6165. + struct net_device *dev;
  6166. + struct wait_queue *wait_queue;
  6167. + char locked;
  6168. + int (*hard_start_xmit) (struct sk_buff *skb,
  6169. + struct net_device *dev);
  6170. + int (*hard_header) (struct sk_buff *skb,
  6171. + struct net_device *dev,
  6172. + unsigned short type,
  6173. + void *daddr,
  6174. + void *saddr,
  6175. + unsigned len);
  6176. +#ifdef NET_21
  6177. + int (*rebuild_header)(struct sk_buff *skb);
  6178. +#else /* NET_21 */
  6179. + int (*rebuild_header)(void *buff, struct net_device *dev,
  6180. + unsigned long raddr, struct sk_buff *skb);
  6181. +#endif /* NET_21 */
  6182. + int (*set_mac_address)(struct net_device *dev, void *addr);
  6183. +#ifndef NET_21
  6184. + void (*header_cache_bind)(struct hh_cache **hhp, struct net_device *dev,
  6185. + unsigned short htype, __u32 daddr);
  6186. +#endif /* !NET_21 */
  6187. + void (*header_cache_update)(struct hh_cache *hh, struct net_device *dev, unsigned char * haddr);
  6188. + struct net_device_stats *(*get_stats)(struct net_device *dev);
  6189. + struct net_device_stats mystats;
  6190. + int mtu; /* What is the desired MTU? */
  6191. +};
  6192. +
  6193. +extern char ipsec_tunnel_c_version[];
  6194. +
  6195. +extern struct net_device *ipsecdevices[IPSEC_NUM_IF];
  6196. +
  6197. +int ipsec_tunnel_init_devices(void);
  6198. +
  6199. +/* void */ int ipsec_tunnel_cleanup_devices(void);
  6200. +
  6201. +extern /* void */ int ipsec_init(void);
  6202. +
  6203. +extern int ipsec_tunnel_start_xmit(struct sk_buff *skb, struct net_device *dev);
  6204. +extern struct net_device *ipsec_get_device(int inst);
  6205. +
  6206. +#ifdef CONFIG_KLIPS_DEBUG
  6207. +extern int debug_tunnel;
  6208. +extern int sysctl_ipsec_debug_verbose;
  6209. +#endif /* CONFIG_KLIPS_DEBUG */
  6210. +#endif /* __KERNEL__ */
  6211. +
  6212. +#ifdef CONFIG_KLIPS_DEBUG
  6213. +#define DB_TN_INIT 0x0001
  6214. +#define DB_TN_PROCFS 0x0002
  6215. +#define DB_TN_XMIT 0x0010
  6216. +#define DB_TN_OHDR 0x0020
  6217. +#define DB_TN_CROUT 0x0040
  6218. +#define DB_TN_OXFS 0x0080
  6219. +#define DB_TN_REVEC 0x0100
  6220. +#define DB_TN_ENCAP 0x0200
  6221. +#endif /* CONFIG_KLIPS_DEBUG */
  6222. +
  6223. +/*
  6224. + * $Log: ipsec_tunnel.h,v $
  6225. + * Revision 1.33 2005/06/04 16:06:05 mcr
  6226. + * better patch for nat-t rcv-device code.
  6227. + *
  6228. + * Revision 1.32 2005/05/21 03:18:35 mcr
  6229. + * added additional debug flag tunnelling.
  6230. + *
  6231. + * Revision 1.31 2004/08/03 18:18:02 mcr
  6232. + * in 2.6, use "net_device" instead of #define device->net_device.
  6233. + * this probably breaks 2.0 compiles.
  6234. + *
  6235. + * Revision 1.30 2004/07/10 19:08:41 mcr
  6236. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  6237. + *
  6238. + * Revision 1.29 2004/04/05 19:55:07 mcr
  6239. + * Moved from linux/include/freeswan/ipsec_tunnel.h,v
  6240. + *
  6241. + * Revision 1.28 2003/06/24 20:22:32 mcr
  6242. + * added new global: ipsecdevices[] so that we can keep track of
  6243. + * the ipsecX devices. They will be referenced with dev_hold(),
  6244. + * so 2.2 may need this as well.
  6245. + *
  6246. + * Revision 1.27 2003/04/03 17:38:09 rgb
  6247. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  6248. + *
  6249. + * Revision 1.26 2003/02/12 19:32:20 rgb
  6250. + * Updated copyright year.
  6251. + *
  6252. + * Revision 1.25 2002/05/27 18:56:07 rgb
  6253. + * Convert to dynamic ipsec device allocation.
  6254. + *
  6255. + * Revision 1.24 2002/04/24 07:36:48 mcr
  6256. + * Moved from ./klips/net/ipsec/ipsec_tunnel.h,v
  6257. + *
  6258. + * Revision 1.23 2001/11/06 19:50:44 rgb
  6259. + * Moved IP_SEND, ICMP_SEND, DEV_QUEUE_XMIT macros to ipsec_tunnel.h for
  6260. + * use also by pfkey_v2_parser.c
  6261. + *
  6262. + * Revision 1.22 2001/09/15 16:24:05 rgb
  6263. + * Re-inject first and last HOLD packet when an eroute REPLACE is done.
  6264. + *
  6265. + * Revision 1.21 2001/06/14 19:35:10 rgb
  6266. + * Update copyright date.
  6267. + *
  6268. + * Revision 1.20 2000/09/15 11:37:02 rgb
  6269. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  6270. + * IPCOMP zlib deflate code.
  6271. + *
  6272. + * Revision 1.19 2000/09/08 19:12:56 rgb
  6273. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  6274. + *
  6275. + * Revision 1.18 2000/07/28 13:50:54 rgb
  6276. + * Changed enet_statistics to net_device_stats and added back compatibility
  6277. + * for pre-2.1.19.
  6278. + *
  6279. + * Revision 1.17 1999/11/19 01:12:15 rgb
  6280. + * Purge unneeded proc_info prototypes, now that static linking uses
  6281. + * dynamic proc_info registration.
  6282. + *
  6283. + * Revision 1.16 1999/11/18 18:51:00 rgb
  6284. + * Changed all device registrations for static linking to
  6285. + * dynamic to reduce the number and size of patches.
  6286. + *
  6287. + * Revision 1.15 1999/11/18 04:14:21 rgb
  6288. + * Replaced all kernel version macros to shorter, readable form.
  6289. + * Added CONFIG_PROC_FS compiler directives in case it is shut off.
  6290. + * Added Marc Boucher's 2.3.25 proc patches.
  6291. + *
  6292. + * Revision 1.14 1999/05/25 02:50:10 rgb
  6293. + * Fix kernel version macros for 2.0.x static linking.
  6294. + *
  6295. + * Revision 1.13 1999/05/25 02:41:06 rgb
  6296. + * Add ipsec_klipsdebug support for static linking.
  6297. + *
  6298. + * Revision 1.12 1999/05/05 22:02:32 rgb
  6299. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  6300. + *
  6301. + * Revision 1.11 1999/04/29 15:19:50 rgb
  6302. + * Add return values to init and cleanup functions.
  6303. + *
  6304. + * Revision 1.10 1999/04/16 16:02:39 rgb
  6305. + * Bump up macro to 4 ipsec I/Fs.
  6306. + *
  6307. + * Revision 1.9 1999/04/15 15:37:25 rgb
  6308. + * Forward check changes from POST1_00 branch.
  6309. + *
  6310. + * Revision 1.5.2.1 1999/04/02 04:26:14 rgb
  6311. + * Backcheck from HEAD, pre1.0.
  6312. + *
  6313. + * Revision 1.8 1999/04/11 00:29:01 henry
  6314. + * GPL boilerplate
  6315. + *
  6316. + * Revision 1.7 1999/04/06 04:54:28 rgb
  6317. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  6318. + * patch shell fixes.
  6319. + *
  6320. + * Revision 1.6 1999/03/31 05:44:48 rgb
  6321. + * Keep PMTU reduction private.
  6322. + *
  6323. + * Revision 1.5 1999/02/10 22:31:20 rgb
  6324. + * Change rebuild_header member to reflect generality of link layer.
  6325. + *
  6326. + * Revision 1.4 1998/12/01 13:22:04 rgb
  6327. + * Added support for debug printing of version info.
  6328. + *
  6329. + * Revision 1.3 1998/07/29 20:42:46 rgb
  6330. + * Add a macro for clearing all tunnel devices.
  6331. + * Rearrange structures and declarations for sharing with userspace.
  6332. + *
  6333. + * Revision 1.2 1998/06/25 20:01:45 rgb
  6334. + * Make prototypes available for ipsec_init and ipsec proc_dir_entries
  6335. + * for static linking.
  6336. + *
  6337. + * Revision 1.1 1998/06/18 21:27:50 henry
  6338. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  6339. + * kernel-build scripts happier in the presence of symlinks
  6340. + *
  6341. + * Revision 1.3 1998/05/18 21:51:50 rgb
  6342. + * Added macros for num of I/F's and a procfs debug switch.
  6343. + *
  6344. + * Revision 1.2 1998/04/21 21:29:09 rgb
  6345. + * Rearrange debug switches to change on the fly debug output from user
  6346. + * space. Only kernel changes checked in at this time. radij.c was also
  6347. + * changed to temporarily remove buggy debugging code in rj_delete causing
  6348. + * an OOPS and hence, netlink device open errors.
  6349. + *
  6350. + * Revision 1.1 1998/04/09 03:06:13 henry
  6351. + * sources moved up from linux/net/ipsec
  6352. + *
  6353. + * Revision 1.1.1.1 1998/04/08 05:35:05 henry
  6354. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  6355. + *
  6356. + * Revision 0.5 1997/06/03 04:24:48 ji
  6357. + * Added transport mode.
  6358. + * Changed the way routing is done.
  6359. + * Lots of bug fixes.
  6360. + *
  6361. + * Revision 0.4 1997/01/15 01:28:15 ji
  6362. + * No changes.
  6363. + *
  6364. + * Revision 0.3 1996/11/20 14:39:04 ji
  6365. + * Minor cleanups.
  6366. + * Rationalized debugging code.
  6367. + *
  6368. + * Revision 0.2 1996/11/02 00:18:33 ji
  6369. + * First limited release.
  6370. + *
  6371. + *
  6372. + */
  6373. --- /dev/null Tue Mar 11 13:02:56 2003
  6374. +++ linux/include/openswan/ipsec_xform.h Mon Feb 9 13:51:03 2004
  6375. @@ -0,0 +1,257 @@
  6376. +/*
  6377. + * Definitions relevant to IPSEC transformations
  6378. + * Copyright (C) 1996, 1997 John Ioannidis.
  6379. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  6380. + * COpyright (C) 2003 Michael Richardson <mcr@sandelman.ottawa.on.ca>
  6381. + *
  6382. + * This program is free software; you can redistribute it and/or modify it
  6383. + * under the terms of the GNU General Public License as published by the
  6384. + * Free Software Foundation; either version 2 of the License, or (at your
  6385. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  6386. + *
  6387. + * This program is distributed in the hope that it will be useful, but
  6388. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  6389. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  6390. + * for more details.
  6391. + *
  6392. + * RCSID $Id: ipsec_xform.h,v 1.41 2004/07/10 19:08:41 mcr Exp $
  6393. + */
  6394. +
  6395. +#ifndef _IPSEC_XFORM_H_
  6396. +
  6397. +#include <openswan.h>
  6398. +
  6399. +#define XF_NONE 0 /* No transform set */
  6400. +#define XF_IP4 1 /* IPv4 inside IPv4 */
  6401. +#define XF_AHMD5 2 /* AH MD5 */
  6402. +#define XF_AHSHA 3 /* AH SHA */
  6403. +#define XF_ESP3DES 5 /* ESP DES3-CBC */
  6404. +#define XF_AHHMACMD5 6 /* AH-HMAC-MD5 with opt replay prot */
  6405. +#define XF_AHHMACSHA1 7 /* AH-HMAC-SHA1 with opt replay prot */
  6406. +#define XF_ESP3DESMD5 9 /* triple DES, HMAC-MD-5, 128-bits of authentication */
  6407. +#define XF_ESP3DESMD596 10 /* triple DES, HMAC-MD-5, 96-bits of authentication */
  6408. +#define XF_ESPNULLMD596 12 /* NULL, HMAC-MD-5 with 96-bits of authentication */
  6409. +#define XF_ESPNULLSHA196 13 /* NULL, HMAC-SHA-1 with 96-bits of authentication */
  6410. +#define XF_ESP3DESSHA196 14 /* triple DES, HMAC-SHA-1, 96-bits of authentication */
  6411. +#define XF_IP6 15 /* IPv6 inside IPv6 */
  6412. +#define XF_COMPDEFLATE 16 /* IPCOMP deflate */
  6413. +
  6414. +#define XF_CLR 126 /* Clear SA table */
  6415. +#define XF_DEL 127 /* Delete SA */
  6416. +
  6417. +/* IPsec AH transform values
  6418. + * RFC 2407
  6419. + * draft-ietf-ipsec-doi-tc-mib-02.txt
  6420. + */
  6421. +
  6422. +#define AH_NONE 0
  6423. +#define AH_MD5 2
  6424. +#define AH_SHA 3
  6425. +/* draft-ietf-ipsec-ciph-aes-cbc-03.txt */
  6426. +#define AH_SHA2_256 5
  6427. +#define AH_SHA2_384 6
  6428. +#define AH_SHA2_512 7
  6429. +#define AH_RIPEMD 8
  6430. +#define AH_MAX 15
  6431. +
  6432. +/* IPsec ESP transform values */
  6433. +
  6434. +#define ESP_NONE 0
  6435. +#define ESP_DES 2
  6436. +#define ESP_3DES 3
  6437. +#define ESP_RC5 4
  6438. +#define ESP_IDEA 5
  6439. +#define ESP_CAST 6
  6440. +#define ESP_BLOWFISH 7
  6441. +#define ESP_3IDEA 8
  6442. +#define ESP_RC4 10
  6443. +#define ESP_NULL 11
  6444. +#define ESP_AES 12
  6445. +
  6446. +/* as draft-ietf-ipsec-ciph-aes-cbc-02.txt */
  6447. +#define ESP_MARS 249
  6448. +#define ESP_RC6 250
  6449. +#define ESP_SERPENT 252
  6450. +#define ESP_TWOFISH 253
  6451. +
  6452. +/* IPCOMP transform values */
  6453. +
  6454. +#define IPCOMP_NONE 0
  6455. +#define IPCOMP_OUI 1
  6456. +#define IPCOMP_DEFLAT 2
  6457. +#define IPCOMP_LZS 3
  6458. +#define IPCOMP_V42BIS 4
  6459. +
  6460. +#define XFT_AUTH 0x0001
  6461. +#define XFT_CONF 0x0100
  6462. +
  6463. +/* available if CONFIG_KLIPS_DEBUG is defined */
  6464. +#define DB_XF_INIT 0x0001
  6465. +
  6466. +#define PROTO2TXT(x) \
  6467. + (x) == IPPROTO_AH ? "AH" : \
  6468. + (x) == IPPROTO_ESP ? "ESP" : \
  6469. + (x) == IPPROTO_IPIP ? "IPIP" : \
  6470. + (x) == IPPROTO_COMP ? "COMP" : \
  6471. + "UNKNOWN_proto"
  6472. +static inline const char *enc_name_id (unsigned id) {
  6473. + static char buf[16];
  6474. + snprintf(buf, sizeof(buf), "_ID%d", id);
  6475. + return buf;
  6476. +}
  6477. +static inline const char *auth_name_id (unsigned id) {
  6478. + static char buf[16];
  6479. + snprintf(buf, sizeof(buf), "_ID%d", id);
  6480. + return buf;
  6481. +}
  6482. +#define IPS_XFORM_NAME(x) \
  6483. + PROTO2TXT((x)->ips_said.proto), \
  6484. + (x)->ips_said.proto == IPPROTO_COMP ? \
  6485. + ((x)->ips_encalg == SADB_X_CALG_DEFLATE ? \
  6486. + "_DEFLATE" : "_UNKNOWN_comp") : \
  6487. + (x)->ips_encalg == ESP_NONE ? "" : \
  6488. + (x)->ips_encalg == ESP_3DES ? "_3DES" : \
  6489. + (x)->ips_encalg == ESP_AES ? "_AES" : \
  6490. + (x)->ips_encalg == ESP_SERPENT ? "_SERPENT" : \
  6491. + (x)->ips_encalg == ESP_TWOFISH ? "_TWOFISH" : \
  6492. + enc_name_id(x->ips_encalg)/* "_UNKNOWN_encr" */, \
  6493. + (x)->ips_authalg == AH_NONE ? "" : \
  6494. + (x)->ips_authalg == AH_MD5 ? "_HMAC_MD5" : \
  6495. + (x)->ips_authalg == AH_SHA ? "_HMAC_SHA1" : \
  6496. + (x)->ips_authalg == AH_SHA2_256 ? "_HMAC_SHA2_256" : \
  6497. + (x)->ips_authalg == AH_SHA2_384 ? "_HMAC_SHA2_384" : \
  6498. + (x)->ips_authalg == AH_SHA2_512 ? "_HMAC_SHA2_512" : \
  6499. + auth_name_id(x->ips_authalg) /* "_UNKNOWN_auth" */ \
  6500. +
  6501. +#ifdef __KERNEL__
  6502. +struct ipsec_rcv_state;
  6503. +struct ipsec_xmit_state;
  6504. +
  6505. +struct xform_functions {
  6506. + enum ipsec_rcv_value (*rcv_checks)(struct ipsec_rcv_state *irs,
  6507. + struct sk_buff *skb);
  6508. + enum ipsec_rcv_value (*rcv_decrypt)(struct ipsec_rcv_state *irs);
  6509. +
  6510. + enum ipsec_rcv_value (*rcv_setup_auth)(struct ipsec_rcv_state *irs,
  6511. + struct sk_buff *skb,
  6512. + __u32 *replay,
  6513. + unsigned char **authenticator);
  6514. + enum ipsec_rcv_value (*rcv_calc_auth)(struct ipsec_rcv_state *irs,
  6515. + struct sk_buff *skb);
  6516. +
  6517. + enum ipsec_xmit_value (*xmit_setup)(struct ipsec_xmit_state *ixs);
  6518. + enum ipsec_xmit_value (*xmit_encrypt)(struct ipsec_xmit_state *ixs);
  6519. +
  6520. + enum ipsec_xmit_value (*xmit_setup_auth)(struct ipsec_xmit_state *ixs,
  6521. + struct sk_buff *skb,
  6522. + __u32 *replay,
  6523. + unsigned char **authenticator);
  6524. + enum ipsec_xmit_value (*xmit_calc_auth)(struct ipsec_xmit_state *ixs,
  6525. + struct sk_buff *skb);
  6526. + int xmit_headroom;
  6527. + int xmit_needtailroom;
  6528. +};
  6529. +
  6530. +#endif /* __KERNEL__ */
  6531. +
  6532. +#ifdef CONFIG_KLIPS_DEBUG
  6533. +extern void ipsec_dmp(char *s, caddr_t bb, int len);
  6534. +#else /* CONFIG_KLIPS_DEBUG */
  6535. +#define ipsec_dmp(_x, _y, _z)
  6536. +#endif /* CONFIG_KLIPS_DEBUG */
  6537. +
  6538. +
  6539. +#define _IPSEC_XFORM_H_
  6540. +#endif /* _IPSEC_XFORM_H_ */
  6541. +
  6542. +/*
  6543. + * $Log: ipsec_xform.h,v $
  6544. + * Revision 1.41 2004/07/10 19:08:41 mcr
  6545. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  6546. + *
  6547. + * Revision 1.40 2004/04/06 02:49:08 mcr
  6548. + * pullup of algo code from alg-branch.
  6549. + *
  6550. + * Revision 1.39 2004/04/05 19:55:07 mcr
  6551. + * Moved from linux/include/freeswan/ipsec_xform.h,v
  6552. + *
  6553. + * Revision 1.38 2004/04/05 19:41:05 mcr
  6554. + * merged alg-branch code.
  6555. + *
  6556. + * Revision 1.37 2003/12/13 19:10:16 mcr
  6557. + * refactored rcv and xmit code - same as FS 2.05.
  6558. + *
  6559. + * Revision 1.36.34.1 2003/12/22 15:25:52 jjo
  6560. + * Merged algo-0.8.1-rc11-test1 into alg-branch
  6561. + *
  6562. + * Revision 1.36 2002/04/24 07:36:48 mcr
  6563. + * Moved from ./klips/net/ipsec/ipsec_xform.h,v
  6564. + *
  6565. + * Revision 1.35 2001/11/26 09:23:51 rgb
  6566. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  6567. + *
  6568. + * Revision 1.33.2.1 2001/09/25 02:24:58 mcr
  6569. + * struct tdb -> struct ipsec_sa.
  6570. + * sa(tdb) manipulation functions renamed and moved to ipsec_sa.c
  6571. + * ipsec_xform.c removed. header file still contains useful things.
  6572. + *
  6573. + * Revision 1.34 2001/11/06 19:47:17 rgb
  6574. + * Changed lifetime_packets to uint32 from uint64.
  6575. + *
  6576. + * Revision 1.33 2001/09/08 21:13:34 rgb
  6577. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  6578. + *
  6579. + * Revision 1.32 2001/07/06 07:40:01 rgb
  6580. + * Reformatted for readability.
  6581. + * Added inbound policy checking fields for use with IPIP SAs.
  6582. + *
  6583. + * Revision 1.31 2001/06/14 19:35:11 rgb
  6584. + * Update copyright date.
  6585. + *
  6586. + * Revision 1.30 2001/05/30 08:14:03 rgb
  6587. + * Removed vestiges of esp-null transforms.
  6588. + *
  6589. + * Revision 1.29 2001/01/30 23:42:47 rgb
  6590. + * Allow pfkey msgs from pid other than user context required for ACQUIRE
  6591. + * and subsequent ADD or UDATE.
  6592. + *
  6593. + * Revision 1.28 2000/11/06 04:30:40 rgb
  6594. + * Add Svenning's adaptive content compression.
  6595. + *
  6596. + * Revision 1.27 2000/09/19 00:38:25 rgb
  6597. + * Fixed algorithm name bugs introduced for ipcomp.
  6598. + *
  6599. + * Revision 1.26 2000/09/17 21:36:48 rgb
  6600. + * Added proto2txt macro.
  6601. + *
  6602. + * Revision 1.25 2000/09/17 18:56:47 rgb
  6603. + * Added IPCOMP support.
  6604. + *
  6605. + * Revision 1.24 2000/09/12 19:34:12 rgb
  6606. + * Defined XF_IP6 from Gerhard for ipv6 tunnel support.
  6607. + *
  6608. + * Revision 1.23 2000/09/12 03:23:14 rgb
  6609. + * Cleaned out now unused tdb_xform and tdb_xdata members of struct tdb.
  6610. + *
  6611. + * Revision 1.22 2000/09/08 19:12:56 rgb
  6612. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  6613. + *
  6614. + * Revision 1.21 2000/09/01 18:32:43 rgb
  6615. + * Added (disabled) sensitivity members to tdb struct.
  6616. + *
  6617. + * Revision 1.20 2000/08/30 05:31:01 rgb
  6618. + * Removed all the rest of the references to tdb_spi, tdb_proto, tdb_dst.
  6619. + * Kill remainder of tdb_xform, tdb_xdata, xformsw.
  6620. + *
  6621. + * Revision 1.19 2000/08/01 14:51:52 rgb
  6622. + * Removed _all_ remaining traces of DES.
  6623. + *
  6624. + * Revision 1.18 2000/01/21 06:17:45 rgb
  6625. + * Tidied up spacing.
  6626. + *
  6627. + *
  6628. + * Local variables:
  6629. + * c-file-style: "linux"
  6630. + * End:
  6631. + *
  6632. + */
  6633. --- /dev/null Tue Mar 11 13:02:56 2003
  6634. +++ linux/include/openswan/ipsec_xmit.h Mon Feb 9 13:51:03 2004
  6635. @@ -0,0 +1,198 @@
  6636. +/*
  6637. + * IPSEC tunneling code
  6638. + * Copyright (C) 1996, 1997 John Ioannidis.
  6639. + * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Richard Guy Briggs.
  6640. + *
  6641. + * This program is free software; you can redistribute it and/or modify it
  6642. + * under the terms of the GNU General Public License as published by the
  6643. + * Free Software Foundation; either version 2 of the License, or (at your
  6644. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  6645. + *
  6646. + * This program is distributed in the hope that it will be useful, but
  6647. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  6648. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  6649. + * for more details.
  6650. + *
  6651. + * RCSID $Id: ipsec_xmit.h,v 1.14 2005/05/11 01:00:26 mcr Exp $
  6652. + */
  6653. +
  6654. +#include "openswan/ipsec_sa.h"
  6655. +
  6656. +enum ipsec_xmit_value
  6657. +{
  6658. + IPSEC_XMIT_STOLEN=2,
  6659. + IPSEC_XMIT_PASS=1,
  6660. + IPSEC_XMIT_OK=0,
  6661. + IPSEC_XMIT_ERRMEMALLOC=-1,
  6662. + IPSEC_XMIT_ESP_BADALG=-2,
  6663. + IPSEC_XMIT_BADPROTO=-3,
  6664. + IPSEC_XMIT_ESP_PUSHPULLERR=-4,
  6665. + IPSEC_XMIT_BADLEN=-5,
  6666. + IPSEC_XMIT_AH_BADALG=-6,
  6667. + IPSEC_XMIT_SAIDNOTFOUND=-7,
  6668. + IPSEC_XMIT_SAIDNOTLIVE=-8,
  6669. + IPSEC_XMIT_REPLAYROLLED=-9,
  6670. + IPSEC_XMIT_LIFETIMEFAILED=-10,
  6671. + IPSEC_XMIT_CANNOTFRAG=-11,
  6672. + IPSEC_XMIT_MSSERR=-12,
  6673. + IPSEC_XMIT_ERRSKBALLOC=-13,
  6674. + IPSEC_XMIT_ENCAPFAIL=-14,
  6675. + IPSEC_XMIT_NODEV=-15,
  6676. + IPSEC_XMIT_NOPRIVDEV=-16,
  6677. + IPSEC_XMIT_NOPHYSDEV=-17,
  6678. + IPSEC_XMIT_NOSKB=-18,
  6679. + IPSEC_XMIT_NOIPV6=-19,
  6680. + IPSEC_XMIT_NOIPOPTIONS=-20,
  6681. + IPSEC_XMIT_TTLEXPIRED=-21,
  6682. + IPSEC_XMIT_BADHHLEN=-22,
  6683. + IPSEC_XMIT_PUSHPULLERR=-23,
  6684. + IPSEC_XMIT_ROUTEERR=-24,
  6685. + IPSEC_XMIT_RECURSDETECT=-25,
  6686. + IPSEC_XMIT_IPSENDFAILURE=-26,
  6687. + IPSEC_XMIT_ESPUDP=-27,
  6688. + IPSEC_XMIT_ESPUDP_BADTYPE=-28,
  6689. +};
  6690. +
  6691. +struct ipsec_xmit_state
  6692. +{
  6693. + struct sk_buff *skb; /* working skb pointer */
  6694. + struct net_device *dev; /* working dev pointer */
  6695. + struct ipsecpriv *prv; /* Our device' private space */
  6696. + struct sk_buff *oskb; /* Original skb pointer */
  6697. + struct net_device_stats *stats; /* This device's statistics */
  6698. + struct iphdr *iph; /* Our new IP header */
  6699. + __u32 newdst; /* The other SG's IP address */
  6700. + __u32 orgdst; /* Original IP destination address */
  6701. + __u32 orgedst; /* 1st SG's IP address */
  6702. + __u32 newsrc; /* The new source SG's IP address */
  6703. + __u32 orgsrc; /* Original IP source address */
  6704. + __u32 innersrc; /* Innermost IP source address */
  6705. + int iphlen; /* IP header length */
  6706. + int pyldsz; /* upper protocol payload size */
  6707. + int headroom;
  6708. + int tailroom;
  6709. + int authlen;
  6710. + int max_headroom; /* The extra header space needed */
  6711. + int max_tailroom; /* The extra stuffing needed */
  6712. + int ll_headroom; /* The extra link layer hard_header space needed */
  6713. + int tot_headroom; /* The total header space needed */
  6714. + int tot_tailroom; /* The totalstuffing needed */
  6715. + __u8 *saved_header; /* saved copy of the hard header */
  6716. + unsigned short sport, dport;
  6717. +
  6718. + struct sockaddr_encap matcher; /* eroute search key */
  6719. + struct eroute *eroute;
  6720. + struct ipsec_sa *ipsp, *ipsq; /* ipsec_sa pointers */
  6721. + char sa_txt[SATOT_BUF];
  6722. + size_t sa_len;
  6723. + int hard_header_stripped; /* has the hard header been removed yet? */
  6724. + int hard_header_len;
  6725. + struct net_device *physdev;
  6726. +/* struct device *virtdev; */
  6727. + short physmtu;
  6728. + short cur_mtu; /* copy of prv->mtu, cause prv may == NULL */
  6729. + short mtudiff;
  6730. +#ifdef NET_21
  6731. + struct rtable *route;
  6732. +#endif /* NET_21 */
  6733. + ip_said outgoing_said;
  6734. +#ifdef NET_21
  6735. + int pass;
  6736. +#endif /* NET_21 */
  6737. + int error;
  6738. + uint32_t eroute_pid;
  6739. + struct ipsec_sa ips;
  6740. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  6741. + uint8_t natt_type;
  6742. + uint8_t natt_head;
  6743. + uint16_t natt_sport;
  6744. + uint16_t natt_dport;
  6745. +#endif
  6746. +};
  6747. +
  6748. +enum ipsec_xmit_value
  6749. +ipsec_xmit_sanity_check_dev(struct ipsec_xmit_state *ixs);
  6750. +
  6751. +enum ipsec_xmit_value
  6752. +ipsec_xmit_sanity_check_skb(struct ipsec_xmit_state *ixs);
  6753. +
  6754. +enum ipsec_xmit_value
  6755. +ipsec_xmit_encap_bundle(struct ipsec_xmit_state *ixs);
  6756. +
  6757. +extern void ipsec_extract_ports(struct iphdr * iph, struct sockaddr_encap * er);
  6758. +
  6759. +
  6760. +extern int ipsec_xmit_trap_count;
  6761. +extern int ipsec_xmit_trap_sendcount;
  6762. +
  6763. +#ifdef CONFIG_KLIPS_DEBUG
  6764. +extern int debug_tunnel;
  6765. +
  6766. +#define debug_xmit debug_tunnel
  6767. +
  6768. +#define ipsec_xmit_dmp(_x,_y, _z) if (debug_xmit && sysctl_ipsec_debug_verbose) ipsec_dmp_block(_x,_y,_z)
  6769. +#else
  6770. +#define ipsec_xmit_dmp(_x,_y, _z) do {} while(0)
  6771. +
  6772. +#endif /* CONFIG_KLIPS_DEBUG */
  6773. +
  6774. +extern int sysctl_ipsec_debug_verbose;
  6775. +extern int sysctl_ipsec_icmp;
  6776. +extern int sysctl_ipsec_tos;
  6777. +
  6778. +
  6779. +/*
  6780. + * $Log: ipsec_xmit.h,v $
  6781. + * Revision 1.14 2005/05/11 01:00:26 mcr
  6782. + * do not call debug routines if !defined KLIPS_DEBUG.
  6783. + *
  6784. + * Revision 1.13 2005/04/29 05:01:38 mcr
  6785. + * use ipsec_dmp_block.
  6786. + * added cur_mtu to ixs instead of using ixs->dev.
  6787. + *
  6788. + * Revision 1.12 2004/08/20 21:45:37 mcr
  6789. + * CONFIG_KLIPS_NAT_TRAVERSAL is not used in an attempt to
  6790. + * be 26sec compatible. But, some defines where changed.
  6791. + *
  6792. + * Revision 1.11 2004/08/03 18:18:21 mcr
  6793. + * in 2.6, use "net_device" instead of #define device->net_device.
  6794. + * this probably breaks 2.0 compiles.
  6795. + *
  6796. + * Revision 1.10 2004/07/10 19:08:41 mcr
  6797. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  6798. + *
  6799. + * Revision 1.9 2004/04/06 02:49:08 mcr
  6800. + * pullup of algo code from alg-branch.
  6801. + *
  6802. + * Revision 1.8 2004/04/05 19:55:07 mcr
  6803. + * Moved from linux/include/freeswan/ipsec_xmit.h,v
  6804. + *
  6805. + * Revision 1.7 2004/02/03 03:11:40 mcr
  6806. + * new xmit type if the UDP encapsulation is wrong.
  6807. + *
  6808. + * Revision 1.6 2003/12/13 19:10:16 mcr
  6809. + * refactored rcv and xmit code - same as FS 2.05.
  6810. + *
  6811. + * Revision 1.5 2003/12/10 01:20:06 mcr
  6812. + * NAT-traversal patches to KLIPS.
  6813. + *
  6814. + * Revision 1.4 2003/12/06 16:37:04 mcr
  6815. + * 1.4.7a X.509 patch applied.
  6816. + *
  6817. + * Revision 1.3 2003/10/31 02:27:05 mcr
  6818. + * pulled up port-selector patches and sa_id elimination.
  6819. + *
  6820. + * Revision 1.2.4.2 2003/10/29 01:10:19 mcr
  6821. + * elimited "struct sa_id"
  6822. + *
  6823. + * Revision 1.2.4.1 2003/09/21 13:59:38 mcr
  6824. + * pre-liminary X.509 patch - does not yet pass tests.
  6825. + *
  6826. + * Revision 1.2 2003/06/20 01:42:13 mcr
  6827. + * added counters to measure how many ACQUIREs we send to pluto,
  6828. + * and how many are successfully sent.
  6829. + *
  6830. + * Revision 1.1 2003/02/12 19:31:03 rgb
  6831. + * Refactored from ipsec_tunnel.c
  6832. + *
  6833. + */
  6834. --- /dev/null Tue Mar 11 13:02:56 2003
  6835. +++ linux/include/openswan/passert.h Mon Feb 9 13:51:03 2004
  6836. @@ -0,0 +1,75 @@
  6837. +/*
  6838. + * sanitize a string into a printable format.
  6839. + *
  6840. + * Copyright (C) 1998-2002 D. Hugh Redelmeier.
  6841. + * Copyright (C) 2003 Michael Richardson <mcr@freeswan.org>
  6842. + *
  6843. + * This library is free software; you can redistribute it and/or modify it
  6844. + * under the terms of the GNU Library General Public License as published by
  6845. + * the Free Software Foundation; either version 2 of the License, or (at your
  6846. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  6847. + *
  6848. + * This library is distributed in the hope that it will be useful, but
  6849. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  6850. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  6851. + * License for more details.
  6852. + *
  6853. + * RCSID $Id: passert.h,v 1.7 2004/10/21 18:44:42 mcr Exp $
  6854. + */
  6855. +
  6856. +#include "openswan.h"
  6857. +
  6858. +#ifndef _OPENSWAN_PASSERT_H
  6859. +#define _OPENSWAN_PASSERT_H
  6860. +/* our versions of assert: log result */
  6861. +
  6862. +#ifdef DEBUG
  6863. +
  6864. +typedef void (*openswan_passert_fail_t)(const char *pred_str,
  6865. + const char *file_str,
  6866. + unsigned long line_no) NEVER_RETURNS;
  6867. +
  6868. +openswan_passert_fail_t openswan_passert_fail;
  6869. +
  6870. +extern void pexpect_log(const char *pred_str
  6871. + , const char *file_str, unsigned long line_no);
  6872. +
  6873. +# define impossible() do { \
  6874. + if(openswan_passert_fail) { \
  6875. + (*openswan_passert_fail)("impossible", __FILE__, __LINE__); \
  6876. + }} while(0)
  6877. +
  6878. +extern void switch_fail(int n
  6879. + , const char *file_str, unsigned long line_no) NEVER_RETURNS;
  6880. +
  6881. +# define bad_case(n) switch_fail((int) n, __FILE__, __LINE__)
  6882. +
  6883. +# define passert(pred) do { \
  6884. + if (!(pred)) \
  6885. + if(openswan_passert_fail) { \
  6886. + (*openswan_passert_fail)(#pred, __FILE__, __LINE__); \
  6887. + } \
  6888. + } while(0)
  6889. +
  6890. +# define pexpect(pred) do { \
  6891. + if (!(pred)) \
  6892. + pexpect_log(#pred, __FILE__, __LINE__); \
  6893. + } while(0)
  6894. +
  6895. +/* assert that an err_t is NULL; evaluate exactly once */
  6896. +# define happy(x) { \
  6897. + err_t ugh = x; \
  6898. + if (ugh != NULL) \
  6899. + if(openswan_passert_fail) { (*openswan_passert_fail)(ugh, __FILE__, __LINE__); } \
  6900. + }
  6901. +
  6902. +#else /*!DEBUG*/
  6903. +
  6904. +# define impossible() abort()
  6905. +# define bad_case(n) abort()
  6906. +# define passert(pred) { } /* do nothing */
  6907. +# define happy(x) { (void) x; } /* evaluate non-judgementally */
  6908. +
  6909. +#endif /*!DEBUG*/
  6910. +
  6911. +#endif /* _OPENSWAN_PASSERT_H */
  6912. --- /dev/null Tue Mar 11 13:02:56 2003
  6913. +++ linux/include/openswan/pfkey_debug.h Mon Feb 9 13:51:03 2004
  6914. @@ -0,0 +1,54 @@
  6915. +/*
  6916. + * sanitize a string into a printable format.
  6917. + *
  6918. + * Copyright (C) 1998-2002 D. Hugh Redelmeier.
  6919. + * Copyright (C) 2003 Michael Richardson <mcr@freeswan.org>
  6920. + *
  6921. + * This library is free software; you can redistribute it and/or modify it
  6922. + * under the terms of the GNU Library General Public License as published by
  6923. + * the Free Software Foundation; either version 2 of the License, or (at your
  6924. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  6925. + *
  6926. + * This library is distributed in the hope that it will be useful, but
  6927. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  6928. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  6929. + * License for more details.
  6930. + *
  6931. + * RCSID $Id: pfkey_debug.h,v 1.3 2004/04/05 19:55:07 mcr Exp $
  6932. + */
  6933. +
  6934. +#ifndef _FREESWAN_PFKEY_DEBUG_H
  6935. +#define _FREESWAN_PFKEY_DEBUG_H
  6936. +
  6937. +#ifdef __KERNEL__
  6938. +
  6939. +/* note, kernel version ignores pfkey levels */
  6940. +# define DEBUGGING(level,args...) \
  6941. + KLIPS_PRINT(debug_pfkey, "klips_debug:" args)
  6942. +
  6943. +# define ERROR(args...) printk(KERN_ERR "klips:" args)
  6944. +
  6945. +#else
  6946. +
  6947. +extern unsigned int pfkey_lib_debug;
  6948. +
  6949. +extern void (*pfkey_debug_func)(const char *message, ...) PRINTF_LIKE(1);
  6950. +extern void (*pfkey_error_func)(const char *message, ...) PRINTF_LIKE(1);
  6951. +
  6952. +#define DEBUGGING(level,args...) if(pfkey_lib_debug & level) { \
  6953. + if(pfkey_debug_func != NULL) { \
  6954. + (*pfkey_debug_func)("pfkey_lib_debug:" args); \
  6955. + } else { \
  6956. + printf("pfkey_lib_debug:" args); \
  6957. + } }
  6958. +
  6959. +#define ERROR(args...) if(pfkey_error_func != NULL) { \
  6960. + (*pfkey_error_func)("pfkey_lib_debug:" args); \
  6961. + }
  6962. +
  6963. +# define MALLOC(size) malloc(size)
  6964. +# define FREE(obj) free(obj)
  6965. +
  6966. +#endif
  6967. +
  6968. +#endif
  6969. --- /dev/null Tue Mar 11 13:02:56 2003
  6970. +++ linux/include/openswan/radij.h Mon Feb 9 13:51:03 2004
  6971. @@ -0,0 +1,280 @@
  6972. +/*
  6973. + * RCSID $Id: radij.h,v 1.13 2004/04/05 19:55:08 mcr Exp $
  6974. + */
  6975. +
  6976. +/*
  6977. + * This file is defived from ${SRC}/sys/net/radix.h of BSD 4.4lite
  6978. + *
  6979. + * Variable and procedure names have been modified so that they don't
  6980. + * conflict with the original BSD code, as a small number of modifications
  6981. + * have been introduced and we may want to reuse this code in BSD.
  6982. + *
  6983. + * The `j' in `radij' is pronounced as a voiceless guttural (like a Greek
  6984. + * chi or a German ch sound (as `doch', not as in `milch'), or even a
  6985. + * spanish j as in Juan. It is not as far back in the throat like
  6986. + * the corresponding Hebrew sound, nor is it a soft breath like the English h.
  6987. + * It has nothing to do with the Dutch ij sound.
  6988. + *
  6989. + * Here is the appropriate copyright notice:
  6990. + */
  6991. +
  6992. +/*
  6993. + * Copyright (c) 1988, 1989, 1993
  6994. + * The Regents of the University of California. All rights reserved.
  6995. + *
  6996. + * Redistribution and use in source and binary forms, with or without
  6997. + * modification, are permitted provided that the following conditions
  6998. + * are met:
  6999. + * 1. Redistributions of source code must retain the above copyright
  7000. + * notice, this list of conditions and the following disclaimer.
  7001. + * 2. Redistributions in binary form must reproduce the above copyright
  7002. + * notice, this list of conditions and the following disclaimer in the
  7003. + * documentation and/or other materials provided with the distribution.
  7004. + * 3. All advertising materials mentioning features or use of this software
  7005. + * must display the following acknowledgement:
  7006. + * This product includes software developed by the University of
  7007. + * California, Berkeley and its contributors.
  7008. + * 4. Neither the name of the University nor the names of its contributors
  7009. + * may be used to endorse or promote products derived from this software
  7010. + * without specific prior written permission.
  7011. + *
  7012. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  7013. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  7014. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  7015. + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  7016. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  7017. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  7018. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  7019. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  7020. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  7021. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  7022. + * SUCH DAMAGE.
  7023. + *
  7024. + * @(#)radix.h 8.1 (Berkeley) 6/10/93
  7025. + */
  7026. +
  7027. +#ifndef _RADIJ_H_
  7028. +#define _RADIJ_H_
  7029. +
  7030. +/*
  7031. +#define RJ_DEBUG
  7032. +*/
  7033. +
  7034. +#ifdef __KERNEL__
  7035. +
  7036. +#ifndef __P
  7037. +#ifdef __STDC__
  7038. +#define __P(x) x
  7039. +#else
  7040. +#define __P(x) ()
  7041. +#endif
  7042. +#endif
  7043. +
  7044. +/*
  7045. + * Radix search tree node layout.
  7046. + */
  7047. +
  7048. +struct radij_node
  7049. +{
  7050. + struct radij_mask *rj_mklist; /* list of masks contained in subtree */
  7051. + struct radij_node *rj_p; /* parent */
  7052. + short rj_b; /* bit offset; -1-index(netmask) */
  7053. + char rj_bmask; /* node: mask for bit test*/
  7054. + u_char rj_flags; /* enumerated next */
  7055. +#define RJF_NORMAL 1 /* leaf contains normal route */
  7056. +#define RJF_ROOT 2 /* leaf is root leaf for tree */
  7057. +#define RJF_ACTIVE 4 /* This node is alive (for rtfree) */
  7058. + union {
  7059. + struct { /* leaf only data: */
  7060. + caddr_t rj_Key; /* object of search */
  7061. + caddr_t rj_Mask; /* netmask, if present */
  7062. + struct radij_node *rj_Dupedkey;
  7063. + } rj_leaf;
  7064. + struct { /* node only data: */
  7065. + int rj_Off; /* where to start compare */
  7066. + struct radij_node *rj_L;/* progeny */
  7067. + struct radij_node *rj_R;/* progeny */
  7068. + }rj_node;
  7069. + } rj_u;
  7070. +#ifdef RJ_DEBUG
  7071. + int rj_info;
  7072. + struct radij_node *rj_twin;
  7073. + struct radij_node *rj_ybro;
  7074. +#endif
  7075. +};
  7076. +
  7077. +#define rj_dupedkey rj_u.rj_leaf.rj_Dupedkey
  7078. +#define rj_key rj_u.rj_leaf.rj_Key
  7079. +#define rj_mask rj_u.rj_leaf.rj_Mask
  7080. +#define rj_off rj_u.rj_node.rj_Off
  7081. +#define rj_l rj_u.rj_node.rj_L
  7082. +#define rj_r rj_u.rj_node.rj_R
  7083. +
  7084. +/*
  7085. + * Annotations to tree concerning potential routes applying to subtrees.
  7086. + */
  7087. +
  7088. +extern struct radij_mask {
  7089. + short rm_b; /* bit offset; -1-index(netmask) */
  7090. + char rm_unused; /* cf. rj_bmask */
  7091. + u_char rm_flags; /* cf. rj_flags */
  7092. + struct radij_mask *rm_mklist; /* more masks to try */
  7093. + caddr_t rm_mask; /* the mask */
  7094. + int rm_refs; /* # of references to this struct */
  7095. +} *rj_mkfreelist;
  7096. +
  7097. +#define MKGet(m) {\
  7098. + if (rj_mkfreelist) {\
  7099. + m = rj_mkfreelist; \
  7100. + rj_mkfreelist = (m)->rm_mklist; \
  7101. + } else \
  7102. + R_Malloc(m, struct radij_mask *, sizeof (*(m))); }\
  7103. +
  7104. +#define MKFree(m) { (m)->rm_mklist = rj_mkfreelist; rj_mkfreelist = (m);}
  7105. +
  7106. +struct radij_node_head {
  7107. + struct radij_node *rnh_treetop;
  7108. + int rnh_addrsize; /* permit, but not require fixed keys */
  7109. + int rnh_pktsize; /* permit, but not require fixed keys */
  7110. +#if 0
  7111. + struct radij_node *(*rnh_addaddr) /* add based on sockaddr */
  7112. + __P((void *v, void *mask,
  7113. + struct radij_node_head *head, struct radij_node nodes[]));
  7114. +#endif
  7115. + int (*rnh_addaddr) /* add based on sockaddr */
  7116. + __P((void *v, void *mask,
  7117. + struct radij_node_head *head, struct radij_node nodes[]));
  7118. + struct radij_node *(*rnh_addpkt) /* add based on packet hdr */
  7119. + __P((void *v, void *mask,
  7120. + struct radij_node_head *head, struct radij_node nodes[]));
  7121. +#if 0
  7122. + struct radij_node *(*rnh_deladdr) /* remove based on sockaddr */
  7123. + __P((void *v, void *mask, struct radij_node_head *head));
  7124. +#endif
  7125. + int (*rnh_deladdr) /* remove based on sockaddr */
  7126. + __P((void *v, void *mask, struct radij_node_head *head, struct radij_node **node));
  7127. + struct radij_node *(*rnh_delpkt) /* remove based on packet hdr */
  7128. + __P((void *v, void *mask, struct radij_node_head *head));
  7129. + struct radij_node *(*rnh_matchaddr) /* locate based on sockaddr */
  7130. + __P((void *v, struct radij_node_head *head));
  7131. + struct radij_node *(*rnh_matchpkt) /* locate based on packet hdr */
  7132. + __P((void *v, struct radij_node_head *head));
  7133. + int (*rnh_walktree) /* traverse tree */
  7134. + __P((struct radij_node_head *head, int (*f)(struct radij_node *rn, void *w), void *w));
  7135. + struct radij_node rnh_nodes[3]; /* empty tree for common case */
  7136. +};
  7137. +
  7138. +
  7139. +#define Bcmp(a, b, n) memcmp(((caddr_t)(b)), ((caddr_t)(a)), (unsigned)(n))
  7140. +#define Bcopy(a, b, n) memmove(((caddr_t)(b)), ((caddr_t)(a)), (unsigned)(n))
  7141. +#define Bzero(p, n) memset((caddr_t)(p), 0, (unsigned)(n))
  7142. +#define R_Malloc(p, t, n) ((p = (t) kmalloc((size_t)(n), GFP_ATOMIC)), Bzero((p),(n)))
  7143. +#define Free(p) kfree((caddr_t)p);
  7144. +
  7145. +void rj_init __P((void));
  7146. +int rj_inithead __P((void **, int));
  7147. +int rj_refines __P((void *, void *));
  7148. +int rj_walktree __P((struct radij_node_head *head, int (*f)(struct radij_node *rn, void *w), void *w));
  7149. +struct radij_node
  7150. + *rj_addmask __P((void *, int, int)) /* , rgb */ ;
  7151. +int /* * */ rj_addroute __P((void *, void *, struct radij_node_head *,
  7152. + struct radij_node [2])) /* , rgb */ ;
  7153. +int /* * */ rj_delete __P((void *, void *, struct radij_node_head *, struct radij_node **)) /* , rgb */ ;
  7154. +struct radij_node /* rgb */
  7155. + *rj_insert __P((void *, struct radij_node_head *, int *,
  7156. + struct radij_node [2])),
  7157. + *rj_match __P((void *, struct radij_node_head *)),
  7158. + *rj_newpair __P((void *, int, struct radij_node[2])),
  7159. + *rj_search __P((void *, struct radij_node *)),
  7160. + *rj_search_m __P((void *, struct radij_node *, void *));
  7161. +
  7162. +void rj_deltree(struct radij_node_head *);
  7163. +void rj_delnodes(struct radij_node *);
  7164. +void rj_free_mkfreelist(void);
  7165. +int radijcleartree(void);
  7166. +int radijcleanup(void);
  7167. +
  7168. +extern struct radij_node_head *mask_rjhead;
  7169. +extern int maj_keylen;
  7170. +#endif /* __KERNEL__ */
  7171. +
  7172. +#endif /* _RADIJ_H_ */
  7173. +
  7174. +
  7175. +/*
  7176. + * $Log: radij.h,v $
  7177. + * Revision 1.13 2004/04/05 19:55:08 mcr
  7178. + * Moved from linux/include/freeswan/radij.h,v
  7179. + *
  7180. + * Revision 1.12 2002/04/24 07:36:48 mcr
  7181. + * Moved from ./klips/net/ipsec/radij.h,v
  7182. + *
  7183. + * Revision 1.11 2001/09/20 15:33:00 rgb
  7184. + * Min/max cleanup.
  7185. + *
  7186. + * Revision 1.10 1999/11/18 04:09:20 rgb
  7187. + * Replaced all kernel version macros to shorter, readable form.
  7188. + *
  7189. + * Revision 1.9 1999/05/05 22:02:33 rgb
  7190. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  7191. + *
  7192. + * Revision 1.8 1999/04/29 15:24:58 rgb
  7193. + * Add check for existence of macros min/max.
  7194. + *
  7195. + * Revision 1.7 1999/04/11 00:29:02 henry
  7196. + * GPL boilerplate
  7197. + *
  7198. + * Revision 1.6 1999/04/06 04:54:29 rgb
  7199. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  7200. + * patch shell fixes.
  7201. + *
  7202. + * Revision 1.5 1999/01/22 06:30:32 rgb
  7203. + * 64-bit clean-up.
  7204. + *
  7205. + * Revision 1.4 1998/11/30 13:22:55 rgb
  7206. + * Rationalised all the klips kernel file headers. They are much shorter
  7207. + * now and won't conflict under RH5.2.
  7208. + *
  7209. + * Revision 1.3 1998/10/25 02:43:27 rgb
  7210. + * Change return type on rj_addroute and rj_delete and add and argument
  7211. + * to the latter to be able to transmit more infomation about errors.
  7212. + *
  7213. + * Revision 1.2 1998/07/14 18:09:51 rgb
  7214. + * Add a routine to clear eroute table.
  7215. + * Added #ifdef __KERNEL__ directives to restrict scope of header.
  7216. + *
  7217. + * Revision 1.1 1998/06/18 21:30:22 henry
  7218. + * move sources from klips/src to klips/net/ipsec to keep stupid kernel
  7219. + * build scripts happier about symlinks
  7220. + *
  7221. + * Revision 1.4 1998/05/25 20:34:16 rgb
  7222. + * Remove temporary ipsec_walk, rj_deltree and rj_delnodes functions.
  7223. + *
  7224. + * Rename ipsec_rj_walker (ipsec_walk) to ipsec_rj_walker_procprint and
  7225. + * add ipsec_rj_walker_delete.
  7226. + *
  7227. + * Recover memory for eroute table on unload of module.
  7228. + *
  7229. + * Revision 1.3 1998/04/22 16:51:37 rgb
  7230. + * Tidy up radij debug code from recent rash of modifications to debug code.
  7231. + *
  7232. + * Revision 1.2 1998/04/14 17:30:38 rgb
  7233. + * Fix up compiling errors for radij tree memory reclamation.
  7234. + *
  7235. + * Revision 1.1 1998/04/09 03:06:16 henry
  7236. + * sources moved up from linux/net/ipsec
  7237. + *
  7238. + * Revision 1.1.1.1 1998/04/08 05:35:04 henry
  7239. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  7240. + *
  7241. + * Revision 0.4 1997/01/15 01:28:15 ji
  7242. + * No changes.
  7243. + *
  7244. + * Revision 0.3 1996/11/20 14:44:45 ji
  7245. + * Release update only.
  7246. + *
  7247. + * Revision 0.2 1996/11/02 00:18:33 ji
  7248. + * First limited release.
  7249. + *
  7250. + *
  7251. + */
  7252. --- /dev/null Tue Mar 11 13:02:56 2003
  7253. +++ linux/include/pfkey.h Mon Feb 9 13:51:03 2004
  7254. @@ -0,0 +1,529 @@
  7255. +/*
  7256. + * FreeS/WAN specific PF_KEY headers
  7257. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs.
  7258. + *
  7259. + * This program is free software; you can redistribute it and/or modify it
  7260. + * under the terms of the GNU General Public License as published by the
  7261. + * Free Software Foundation; either version 2 of the License, or (at your
  7262. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  7263. + *
  7264. + * This program is distributed in the hope that it will be useful, but
  7265. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  7266. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  7267. + * for more details.
  7268. + *
  7269. + * RCSID $Id: pfkey.h,v 1.49 2005/05/11 00:57:29 mcr Exp $
  7270. + */
  7271. +
  7272. +#ifndef __NET_IPSEC_PF_KEY_H
  7273. +#define __NET_IPSEC_PF_KEY_H
  7274. +#ifdef __KERNEL__
  7275. +extern struct proto_ops pfkey_proto_ops;
  7276. +typedef struct sock pfkey_sock;
  7277. +extern int debug_pfkey;
  7278. +
  7279. +extern /* void */ int pfkey_init(void);
  7280. +extern /* void */ int pfkey_cleanup(void);
  7281. +
  7282. +struct socket_list
  7283. +{
  7284. + struct socket *socketp;
  7285. + struct socket_list *next;
  7286. +};
  7287. +extern int pfkey_list_insert_socket(struct socket*, struct socket_list**);
  7288. +extern int pfkey_list_remove_socket(struct socket*, struct socket_list**);
  7289. +extern struct socket_list *pfkey_open_sockets;
  7290. +extern struct socket_list *pfkey_registered_sockets[];
  7291. +
  7292. +struct ipsec_alg_supported
  7293. +{
  7294. + uint16_t ias_exttype;
  7295. + uint8_t ias_id;
  7296. + uint8_t ias_ivlen;
  7297. + uint16_t ias_keyminbits;
  7298. + uint16_t ias_keymaxbits;
  7299. + char *ias_name;
  7300. +};
  7301. +
  7302. +extern struct supported_list *pfkey_supported_list[];
  7303. +struct supported_list
  7304. +{
  7305. + struct ipsec_alg_supported *supportedp;
  7306. + struct supported_list *next;
  7307. +};
  7308. +extern int pfkey_list_insert_supported(struct ipsec_alg_supported*, struct supported_list**);
  7309. +extern int pfkey_list_remove_supported(struct ipsec_alg_supported*, struct supported_list**);
  7310. +
  7311. +struct sockaddr_key
  7312. +{
  7313. + uint16_t key_family; /* PF_KEY */
  7314. + uint16_t key_pad; /* not used */
  7315. + uint32_t key_pid; /* process ID */
  7316. +};
  7317. +
  7318. +struct pfkey_extracted_data
  7319. +{
  7320. + struct ipsec_sa* ips;
  7321. + struct ipsec_sa* ips2;
  7322. + struct eroute *eroute;
  7323. +};
  7324. +
  7325. +/* forward reference */
  7326. +struct sadb_ext;
  7327. +struct sadb_msg;
  7328. +struct sockaddr;
  7329. +struct sadb_comb;
  7330. +struct sadb_sadb;
  7331. +struct sadb_alg;
  7332. +
  7333. +extern int
  7334. +pfkey_alloc_eroute(struct eroute** eroute);
  7335. +
  7336. +extern int
  7337. +pfkey_sa_process(struct sadb_ext *pfkey_ext,
  7338. + struct pfkey_extracted_data* extr);
  7339. +
  7340. +extern int
  7341. +pfkey_lifetime_process(struct sadb_ext *pfkey_ext,
  7342. + struct pfkey_extracted_data* extr);
  7343. +
  7344. +extern int
  7345. +pfkey_address_process(struct sadb_ext *pfkey_ext,
  7346. + struct pfkey_extracted_data* extr);
  7347. +
  7348. +extern int
  7349. +pfkey_key_process(struct sadb_ext *pfkey_ext,
  7350. + struct pfkey_extracted_data* extr);
  7351. +
  7352. +extern int
  7353. +pfkey_ident_process(struct sadb_ext *pfkey_ext,
  7354. + struct pfkey_extracted_data* extr);
  7355. +
  7356. +extern int
  7357. +pfkey_sens_process(struct sadb_ext *pfkey_ext,
  7358. + struct pfkey_extracted_data* extr);
  7359. +
  7360. +extern int
  7361. +pfkey_prop_process(struct sadb_ext *pfkey_ext,
  7362. + struct pfkey_extracted_data* extr);
  7363. +
  7364. +extern int
  7365. +pfkey_supported_process(struct sadb_ext *pfkey_ext,
  7366. + struct pfkey_extracted_data* extr);
  7367. +
  7368. +extern int
  7369. +pfkey_spirange_process(struct sadb_ext *pfkey_ext,
  7370. + struct pfkey_extracted_data* extr);
  7371. +
  7372. +extern int
  7373. +pfkey_x_kmprivate_process(struct sadb_ext *pfkey_ext,
  7374. + struct pfkey_extracted_data* extr);
  7375. +
  7376. +extern int
  7377. +pfkey_x_satype_process(struct sadb_ext *pfkey_ext,
  7378. + struct pfkey_extracted_data* extr);
  7379. +
  7380. +extern int
  7381. +pfkey_x_debug_process(struct sadb_ext *pfkey_ext,
  7382. + struct pfkey_extracted_data* extr);
  7383. +
  7384. +extern int pfkey_upmsg(struct socket *, struct sadb_msg *);
  7385. +extern int pfkey_expire(struct ipsec_sa *, int);
  7386. +extern int pfkey_acquire(struct ipsec_sa *);
  7387. +#else /* ! __KERNEL__ */
  7388. +
  7389. +extern void (*pfkey_debug_func)(const char *message, ...);
  7390. +extern void (*pfkey_error_func)(const char *message, ...);
  7391. +extern void pfkey_print(struct sadb_msg *msg, FILE *out);
  7392. +
  7393. +
  7394. +#endif /* __KERNEL__ */
  7395. +
  7396. +extern uint8_t satype2proto(uint8_t satype);
  7397. +extern uint8_t proto2satype(uint8_t proto);
  7398. +extern char* satype2name(uint8_t satype);
  7399. +extern char* proto2name(uint8_t proto);
  7400. +
  7401. +struct key_opt
  7402. +{
  7403. + uint32_t key_pid; /* process ID */
  7404. + struct sock *sk;
  7405. +};
  7406. +
  7407. +#define key_pid(sk) ((struct key_opt*)&((sk)->sk_protinfo))->key_pid
  7408. +
  7409. +/* XXX-mcr this is not an alignment, this is because the count is in 64-bit
  7410. + * words.
  7411. + */
  7412. +#define IPSEC_PFKEYv2_ALIGN (sizeof(uint64_t)/sizeof(uint8_t))
  7413. +#define BITS_PER_OCTET 8
  7414. +#define OCTETBITS 8
  7415. +#define PFKEYBITS 64
  7416. +#define DIVUP(x,y) ((x + y -1) / y) /* divide, rounding upwards */
  7417. +#define ALIGN_N(x,y) (DIVUP(x,y) * y) /* align on y boundary */
  7418. +
  7419. +#define IPSEC_PFKEYv2_LEN(x) ((x) * IPSEC_PFKEYv2_ALIGN)
  7420. +#define IPSEC_PFKEYv2_WORDS(x) ((x) / IPSEC_PFKEYv2_ALIGN)
  7421. +
  7422. +
  7423. +#define PFKEYv2_MAX_MSGSIZE 4096
  7424. +
  7425. +/*
  7426. + * PF_KEYv2 permitted and required extensions in and out bitmaps
  7427. + */
  7428. +struct pf_key_ext_parsers_def {
  7429. + int (*parser)(struct sadb_ext*);
  7430. + char *parser_name;
  7431. +};
  7432. +
  7433. +
  7434. +#define SADB_EXTENSIONS_MAX 31
  7435. +extern unsigned int extensions_bitmaps[2/*in/out*/][2/*perm/req*/][SADB_EXTENSIONS_MAX];
  7436. +#define EXT_BITS_IN 0
  7437. +#define EXT_BITS_OUT 1
  7438. +#define EXT_BITS_PERM 0
  7439. +#define EXT_BITS_REQ 1
  7440. +
  7441. +extern void pfkey_extensions_init(struct sadb_ext *extensions[]);
  7442. +extern void pfkey_extensions_free(struct sadb_ext *extensions[]);
  7443. +extern void pfkey_msg_free(struct sadb_msg **pfkey_msg);
  7444. +
  7445. +extern int pfkey_msg_parse(struct sadb_msg *pfkey_msg,
  7446. + struct pf_key_ext_parsers_def *ext_parsers[],
  7447. + struct sadb_ext **extensions,
  7448. + int dir);
  7449. +
  7450. +extern int pfkey_register_reply(int satype, struct sadb_msg *sadb_msg);
  7451. +
  7452. +/*
  7453. + * PF_KEYv2 build function prototypes
  7454. + */
  7455. +
  7456. +int
  7457. +pfkey_msg_hdr_build(struct sadb_ext** pfkey_ext,
  7458. + uint8_t msg_type,
  7459. + uint8_t satype,
  7460. + uint8_t msg_errno,
  7461. + uint32_t seq,
  7462. + uint32_t pid);
  7463. +
  7464. +int
  7465. +pfkey_sa_ref_build(struct sadb_ext ** pfkey_ext,
  7466. + uint16_t exttype,
  7467. + uint32_t spi, /* in network order */
  7468. + uint8_t replay_window,
  7469. + uint8_t sa_state,
  7470. + uint8_t auth,
  7471. + uint8_t encrypt,
  7472. + uint32_t flags,
  7473. + uint32_t/*IPsecSAref_t*/ ref);
  7474. +
  7475. +int
  7476. +pfkey_sa_build(struct sadb_ext ** pfkey_ext,
  7477. + uint16_t exttype,
  7478. + uint32_t spi, /* in network order */
  7479. + uint8_t replay_window,
  7480. + uint8_t sa_state,
  7481. + uint8_t auth,
  7482. + uint8_t encrypt,
  7483. + uint32_t flags);
  7484. +
  7485. +int
  7486. +pfkey_lifetime_build(struct sadb_ext ** pfkey_ext,
  7487. + uint16_t exttype,
  7488. + uint32_t allocations,
  7489. + uint64_t bytes,
  7490. + uint64_t addtime,
  7491. + uint64_t usetime,
  7492. + uint32_t packets);
  7493. +
  7494. +int
  7495. +pfkey_address_build(struct sadb_ext** pfkey_ext,
  7496. + uint16_t exttype,
  7497. + uint8_t proto,
  7498. + uint8_t prefixlen,
  7499. + struct sockaddr* address);
  7500. +
  7501. +int
  7502. +pfkey_key_build(struct sadb_ext** pfkey_ext,
  7503. + uint16_t exttype,
  7504. + uint16_t key_bits,
  7505. + char* key);
  7506. +
  7507. +int
  7508. +pfkey_ident_build(struct sadb_ext** pfkey_ext,
  7509. + uint16_t exttype,
  7510. + uint16_t ident_type,
  7511. + uint64_t ident_id,
  7512. + uint8_t ident_len,
  7513. + char* ident_string);
  7514. +
  7515. +#ifdef __KERNEL__
  7516. +extern int pfkey_nat_t_new_mapping(struct ipsec_sa *, struct sockaddr *, __u16);
  7517. +extern int pfkey_x_nat_t_type_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr);
  7518. +extern int pfkey_x_nat_t_port_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr);
  7519. +#endif /* __KERNEL__ */
  7520. +int
  7521. +pfkey_x_nat_t_type_build(struct sadb_ext** pfkey_ext,
  7522. + uint8_t type);
  7523. +int
  7524. +pfkey_x_nat_t_port_build(struct sadb_ext** pfkey_ext,
  7525. + uint16_t exttype,
  7526. + uint16_t port);
  7527. +
  7528. +int
  7529. +pfkey_sens_build(struct sadb_ext** pfkey_ext,
  7530. + uint32_t dpd,
  7531. + uint8_t sens_level,
  7532. + uint8_t sens_len,
  7533. + uint64_t* sens_bitmap,
  7534. + uint8_t integ_level,
  7535. + uint8_t integ_len,
  7536. + uint64_t* integ_bitmap);
  7537. +
  7538. +int pfkey_x_protocol_build(struct sadb_ext **, uint8_t);
  7539. +
  7540. +
  7541. +int
  7542. +pfkey_prop_build(struct sadb_ext** pfkey_ext,
  7543. + uint8_t replay,
  7544. + unsigned int comb_num,
  7545. + struct sadb_comb* comb);
  7546. +
  7547. +int
  7548. +pfkey_supported_build(struct sadb_ext** pfkey_ext,
  7549. + uint16_t exttype,
  7550. + unsigned int alg_num,
  7551. + struct sadb_alg* alg);
  7552. +
  7553. +int
  7554. +pfkey_spirange_build(struct sadb_ext** pfkey_ext,
  7555. + uint16_t exttype,
  7556. + uint32_t min,
  7557. + uint32_t max);
  7558. +
  7559. +int
  7560. +pfkey_x_kmprivate_build(struct sadb_ext** pfkey_ext);
  7561. +
  7562. +int
  7563. +pfkey_x_satype_build(struct sadb_ext** pfkey_ext,
  7564. + uint8_t satype);
  7565. +
  7566. +int
  7567. +pfkey_x_debug_build(struct sadb_ext** pfkey_ext,
  7568. + uint32_t tunnel,
  7569. + uint32_t netlink,
  7570. + uint32_t xform,
  7571. + uint32_t eroute,
  7572. + uint32_t spi,
  7573. + uint32_t radij,
  7574. + uint32_t esp,
  7575. + uint32_t ah,
  7576. + uint32_t rcv,
  7577. + uint32_t pfkey,
  7578. + uint32_t ipcomp,
  7579. + uint32_t verbose);
  7580. +
  7581. +int
  7582. +pfkey_msg_build(struct sadb_msg** pfkey_msg,
  7583. + struct sadb_ext* extensions[],
  7584. + int dir);
  7585. +
  7586. +/* in pfkey_v2_debug.c - routines to decode numbers -> strings */
  7587. +const char *
  7588. +pfkey_v2_sadb_ext_string(int extnum);
  7589. +
  7590. +const char *
  7591. +pfkey_v2_sadb_type_string(int sadb_type);
  7592. +
  7593. +
  7594. +#endif /* __NET_IPSEC_PF_KEY_H */
  7595. +
  7596. +/*
  7597. + * $Log: pfkey.h,v $
  7598. + * Revision 1.49 2005/05/11 00:57:29 mcr
  7599. + * rename struct supported -> struct ipsec_alg_supported.
  7600. + * make pfkey.h more standalone.
  7601. + *
  7602. + * Revision 1.48 2005/05/01 03:12:50 mcr
  7603. + * include name of algorithm in datastructure.
  7604. + *
  7605. + * Revision 1.47 2004/08/21 00:44:14 mcr
  7606. + * simplify definition of nat_t related prototypes.
  7607. + *
  7608. + * Revision 1.46 2004/08/04 16:27:22 mcr
  7609. + * 2.6 sk_ options.
  7610. + *
  7611. + * Revision 1.45 2004/04/06 02:49:00 mcr
  7612. + * pullup of algo code from alg-branch.
  7613. + *
  7614. + * Revision 1.44 2003/12/10 01:20:01 mcr
  7615. + * NAT-traversal patches to KLIPS.
  7616. + *
  7617. + * Revision 1.43 2003/10/31 02:26:44 mcr
  7618. + * pulled up port-selector patches.
  7619. + *
  7620. + * Revision 1.42.2.2 2003/10/29 01:09:32 mcr
  7621. + * added debugging for pfkey library.
  7622. + *
  7623. + * Revision 1.42.2.1 2003/09/21 13:59:34 mcr
  7624. + * pre-liminary X.509 patch - does not yet pass tests.
  7625. + *
  7626. + * Revision 1.42 2003/08/25 22:08:19 mcr
  7627. + * removed pfkey_proto_init() from pfkey.h for 2.6 support.
  7628. + *
  7629. + * Revision 1.41 2003/05/07 17:28:57 mcr
  7630. + * new function pfkey_debug_func added for us in debugging from
  7631. +
  7632. + * pfkey library.
  7633. + *
  7634. + * Revision 1.40 2003/01/30 02:31:34 rgb
  7635. + *
  7636. + * Convert IPsecSAref_t from signed to unsigned to fix apparent SAref exhaustion bug.
  7637. + *
  7638. + * Revision 1.39 2002/09/20 15:40:21 rgb
  7639. + * Switch from pfkey_alloc_ipsec_sa() to ipsec_sa_alloc().
  7640. + * Added ref parameter to pfkey_sa_build().
  7641. + * Cleaned out unused cruft.
  7642. + *
  7643. + * Revision 1.38 2002/05/14 02:37:24 rgb
  7644. + * Change all references to tdb, TDB or Tunnel Descriptor Block to ips,
  7645. + * ipsec_sa or ipsec_sa.
  7646. + * Added function prototypes for the functions moved to
  7647. + * pfkey_v2_ext_process.c.
  7648. + *
  7649. + * Revision 1.37 2002/04/24 07:36:49 mcr
  7650. + * Moved from ./lib/pfkey.h,v
  7651. + *
  7652. + * Revision 1.36 2002/01/20 20:34:49 mcr
  7653. + * added pfkey_v2_sadb_type_string to decode sadb_type to string.
  7654. + *
  7655. + * Revision 1.35 2001/11/27 05:27:47 mcr
  7656. + * pfkey parses are now maintained by a structure
  7657. + * that includes their name for debug purposes.
  7658. + *
  7659. + * Revision 1.34 2001/11/26 09:23:53 rgb
  7660. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  7661. + *
  7662. + * Revision 1.33 2001/11/06 19:47:47 rgb
  7663. + * Added packet parameter to lifetime and comb structures.
  7664. + *
  7665. + * Revision 1.32 2001/09/08 21:13:34 rgb
  7666. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  7667. + *
  7668. + * Revision 1.31 2001/06/14 19:35:16 rgb
  7669. + * Update copyright date.
  7670. + *
  7671. + * Revision 1.30 2001/02/27 07:04:52 rgb
  7672. + * Added satype2name prototype.
  7673. + *
  7674. + * Revision 1.29 2001/02/26 19:59:33 rgb
  7675. + * Ditch unused sadb_satype2proto[], replaced by satype2proto().
  7676. + *
  7677. + * Revision 1.28 2000/10/10 20:10:19 rgb
  7678. + * Added support for debug_ipcomp and debug_verbose to klipsdebug.
  7679. + *
  7680. + * Revision 1.27 2000/09/21 04:20:45 rgb
  7681. + * Fixed array size off-by-one error. (Thanks Svenning!)
  7682. + *
  7683. + * Revision 1.26 2000/09/12 03:26:05 rgb
  7684. + * Added pfkey_acquire prototype.
  7685. + *
  7686. + * Revision 1.25 2000/09/08 19:21:28 rgb
  7687. + * Fix pfkey_prop_build() parameter to be only single indirection.
  7688. + *
  7689. + * Revision 1.24 2000/09/01 18:46:42 rgb
  7690. + * Added a supported algorithms array lists, one per satype and registered
  7691. + * existing algorithms.
  7692. + * Fixed pfkey_list_{insert,remove}_{socket,support}() to allow change to
  7693. + * list.
  7694. + *
  7695. + * Revision 1.23 2000/08/27 01:55:26 rgb
  7696. + * Define OCTETBITS and PFKEYBITS to avoid using 'magic' numbers in code.
  7697. + *
  7698. + * Revision 1.22 2000/08/20 21:39:23 rgb
  7699. + * Added kernel prototypes for kernel funcitions pfkey_upmsg() and
  7700. + * pfkey_expire().
  7701. + *
  7702. + * Revision 1.21 2000/08/15 17:29:23 rgb
  7703. + * Fixes from SZI to untested pfkey_prop_build().
  7704. + *
  7705. + * Revision 1.20 2000/05/10 20:14:19 rgb
  7706. + * Fleshed out sensitivity, proposal and supported extensions.
  7707. + *
  7708. + * Revision 1.19 2000/03/16 14:07:23 rgb
  7709. + * Renamed ALIGN macro to avoid fighting with others in kernel.
  7710. + *
  7711. + * Revision 1.18 2000/01/22 23:24:06 rgb
  7712. + * Added prototypes for proto2satype(), satype2proto() and proto2name().
  7713. + *
  7714. + * Revision 1.17 2000/01/21 06:26:59 rgb
  7715. + * Converted from double tdb arguments to one structure (extr)
  7716. + * containing pointers to all temporary information structures.
  7717. + * Added klipsdebug switching capability.
  7718. + * Dropped unused argument to pfkey_x_satype_build().
  7719. + *
  7720. + * Revision 1.16 1999/12/29 21:17:41 rgb
  7721. + * Changed pfkey_msg_build() I/F to include a struct sadb_msg**
  7722. + * parameter for cleaner manipulation of extensions[] and to guard
  7723. + * against potential memory leaks.
  7724. + * Changed the I/F to pfkey_msg_free() for the same reason.
  7725. + *
  7726. + * Revision 1.15 1999/12/09 23:12:54 rgb
  7727. + * Added macro for BITS_PER_OCTET.
  7728. + * Added argument to pfkey_sa_build() to do eroutes.
  7729. + *
  7730. + * Revision 1.14 1999/12/08 20:33:25 rgb
  7731. + * Changed sa_family_t to uint16_t for 2.0.xx compatibility.
  7732. + *
  7733. + * Revision 1.13 1999/12/07 19:53:40 rgb
  7734. + * Removed unused first argument from extension parsers.
  7735. + * Changed __u* types to uint* to avoid use of asm/types.h and
  7736. + * sys/types.h in userspace code.
  7737. + * Added function prototypes for pfkey message and extensions
  7738. + * initialisation and cleanup.
  7739. + *
  7740. + * Revision 1.12 1999/12/01 22:19:38 rgb
  7741. + * Change pfkey_sa_build to accept an SPI in network byte order.
  7742. + *
  7743. + * Revision 1.11 1999/11/27 11:55:26 rgb
  7744. + * Added extern sadb_satype2proto to enable moving protocol lookup table
  7745. + * to lib/pfkey_v2_parse.c.
  7746. + * Delete unused, moved typedefs.
  7747. + * Add argument to pfkey_msg_parse() for direction.
  7748. + * Consolidated the 4 1-d extension bitmap arrays into one 4-d array.
  7749. + *
  7750. + * Revision 1.10 1999/11/23 22:29:21 rgb
  7751. + * This file has been moved in the distribution from klips/net/ipsec to
  7752. + * lib.
  7753. + * Add macros for dealing with alignment and rounding up more opaquely.
  7754. + * The uint<n>_t type defines have been moved to freeswan.h to avoid
  7755. + * chicken-and-egg problems.
  7756. + * Add macros for dealing with alignment and rounding up more opaque.
  7757. + * Added prototypes for using extention header bitmaps.
  7758. + * Added prototypes of all the build functions.
  7759. + *
  7760. + * Revision 1.9 1999/11/20 21:59:48 rgb
  7761. + * Moved socketlist type declarations and prototypes for shared use.
  7762. + * Slightly modified scope of sockaddr_key declaration.
  7763. + *
  7764. + * Revision 1.8 1999/11/17 14:34:25 rgb
  7765. + * Protect sa_family_t from being used in userspace with GLIBC<2.
  7766. + *
  7767. + * Revision 1.7 1999/10/27 19:40:35 rgb
  7768. + * Add a maximum PFKEY packet size macro.
  7769. + *
  7770. + * Revision 1.6 1999/10/26 16:58:58 rgb
  7771. + * Created a sockaddr_key and key_opt socket extension structures.
  7772. + *
  7773. + * Revision 1.5 1999/06/10 05:24:41 rgb
  7774. + * Renamed variables to reduce confusion.
  7775. + *
  7776. + * Revision 1.4 1999/04/29 15:21:11 rgb
  7777. + * Add pfkey support to debugging.
  7778. + * Add return values to init and cleanup functions.
  7779. + *
  7780. + * Revision 1.3 1999/04/15 17:58:07 rgb
  7781. + * Add RCSID labels.
  7782. + *
  7783. + */
  7784. --- /dev/null Tue Mar 11 13:02:56 2003
  7785. +++ linux/include/pfkeyv2.h Mon Feb 9 13:51:03 2004
  7786. @@ -0,0 +1,472 @@
  7787. +/*
  7788. + * RCSID $Id: pfkeyv2.h,v 1.31 2005/04/14 01:14:54 mcr Exp $
  7789. + */
  7790. +
  7791. +/*
  7792. +RFC 2367 PF_KEY Key Management API July 1998
  7793. +
  7794. +
  7795. +Appendix D: Sample Header File
  7796. +
  7797. +This file defines structures and symbols for the PF_KEY Version 2
  7798. +key management interface. It was written at the U.S. Naval Research
  7799. +Laboratory. This file is in the public domain. The authors ask that
  7800. +you leave this credit intact on any copies of this file.
  7801. +*/
  7802. +#ifndef __PFKEY_V2_H
  7803. +#define __PFKEY_V2_H 1
  7804. +
  7805. +#define PF_KEY_V2 2
  7806. +#define PFKEYV2_REVISION 199806L
  7807. +
  7808. +#define SADB_RESERVED 0
  7809. +#define SADB_GETSPI 1
  7810. +#define SADB_UPDATE 2
  7811. +#define SADB_ADD 3
  7812. +#define SADB_DELETE 4
  7813. +#define SADB_GET 5
  7814. +#define SADB_ACQUIRE 6
  7815. +#define SADB_REGISTER 7
  7816. +#define SADB_EXPIRE 8
  7817. +#define SADB_FLUSH 9
  7818. +#define SADB_DUMP 10
  7819. +#define SADB_X_PROMISC 11
  7820. +#define SADB_X_PCHANGE 12
  7821. +#define SADB_X_GRPSA 13
  7822. +#define SADB_X_ADDFLOW 14
  7823. +#define SADB_X_DELFLOW 15
  7824. +#define SADB_X_DEBUG 16
  7825. +#define SADB_X_NAT_T_NEW_MAPPING 17
  7826. +#define SADB_MAX 17
  7827. +
  7828. +struct sadb_msg {
  7829. + uint8_t sadb_msg_version;
  7830. + uint8_t sadb_msg_type;
  7831. + uint8_t sadb_msg_errno;
  7832. + uint8_t sadb_msg_satype;
  7833. + uint16_t sadb_msg_len;
  7834. + uint16_t sadb_msg_reserved;
  7835. + uint32_t sadb_msg_seq;
  7836. + uint32_t sadb_msg_pid;
  7837. +};
  7838. +
  7839. +struct sadb_ext {
  7840. + uint16_t sadb_ext_len;
  7841. + uint16_t sadb_ext_type;
  7842. +};
  7843. +
  7844. +struct sadb_sa {
  7845. + uint16_t sadb_sa_len;
  7846. + uint16_t sadb_sa_exttype;
  7847. + uint32_t sadb_sa_spi;
  7848. + uint8_t sadb_sa_replay;
  7849. + uint8_t sadb_sa_state;
  7850. + uint8_t sadb_sa_auth;
  7851. + uint8_t sadb_sa_encrypt;
  7852. + uint32_t sadb_sa_flags;
  7853. + uint32_t /*IPsecSAref_t*/ sadb_x_sa_ref; /* 32 bits */
  7854. + uint8_t sadb_x_reserved[4];
  7855. +};
  7856. +
  7857. +struct sadb_sa_v1 {
  7858. + uint16_t sadb_sa_len;
  7859. + uint16_t sadb_sa_exttype;
  7860. + uint32_t sadb_sa_spi;
  7861. + uint8_t sadb_sa_replay;
  7862. + uint8_t sadb_sa_state;
  7863. + uint8_t sadb_sa_auth;
  7864. + uint8_t sadb_sa_encrypt;
  7865. + uint32_t sadb_sa_flags;
  7866. +};
  7867. +
  7868. +struct sadb_lifetime {
  7869. + uint16_t sadb_lifetime_len;
  7870. + uint16_t sadb_lifetime_exttype;
  7871. + uint32_t sadb_lifetime_allocations;
  7872. + uint64_t sadb_lifetime_bytes;
  7873. + uint64_t sadb_lifetime_addtime;
  7874. + uint64_t sadb_lifetime_usetime;
  7875. + uint32_t sadb_x_lifetime_packets;
  7876. + uint32_t sadb_x_lifetime_reserved;
  7877. +};
  7878. +
  7879. +struct sadb_address {
  7880. + uint16_t sadb_address_len;
  7881. + uint16_t sadb_address_exttype;
  7882. + uint8_t sadb_address_proto;
  7883. + uint8_t sadb_address_prefixlen;
  7884. + uint16_t sadb_address_reserved;
  7885. +};
  7886. +
  7887. +struct sadb_key {
  7888. + uint16_t sadb_key_len;
  7889. + uint16_t sadb_key_exttype;
  7890. + uint16_t sadb_key_bits;
  7891. + uint16_t sadb_key_reserved;
  7892. +};
  7893. +
  7894. +struct sadb_ident {
  7895. + uint16_t sadb_ident_len;
  7896. + uint16_t sadb_ident_exttype;
  7897. + uint16_t sadb_ident_type;
  7898. + uint16_t sadb_ident_reserved;
  7899. + uint64_t sadb_ident_id;
  7900. +};
  7901. +
  7902. +struct sadb_sens {
  7903. + uint16_t sadb_sens_len;
  7904. + uint16_t sadb_sens_exttype;
  7905. + uint32_t sadb_sens_dpd;
  7906. + uint8_t sadb_sens_sens_level;
  7907. + uint8_t sadb_sens_sens_len;
  7908. + uint8_t sadb_sens_integ_level;
  7909. + uint8_t sadb_sens_integ_len;
  7910. + uint32_t sadb_sens_reserved;
  7911. +};
  7912. +
  7913. +struct sadb_prop {
  7914. + uint16_t sadb_prop_len;
  7915. + uint16_t sadb_prop_exttype;
  7916. + uint8_t sadb_prop_replay;
  7917. + uint8_t sadb_prop_reserved[3];
  7918. +};
  7919. +
  7920. +struct sadb_comb {
  7921. + uint8_t sadb_comb_auth;
  7922. + uint8_t sadb_comb_encrypt;
  7923. + uint16_t sadb_comb_flags;
  7924. + uint16_t sadb_comb_auth_minbits;
  7925. + uint16_t sadb_comb_auth_maxbits;
  7926. + uint16_t sadb_comb_encrypt_minbits;
  7927. + uint16_t sadb_comb_encrypt_maxbits;
  7928. + uint32_t sadb_comb_reserved;
  7929. + uint32_t sadb_comb_soft_allocations;
  7930. + uint32_t sadb_comb_hard_allocations;
  7931. + uint64_t sadb_comb_soft_bytes;
  7932. + uint64_t sadb_comb_hard_bytes;
  7933. + uint64_t sadb_comb_soft_addtime;
  7934. + uint64_t sadb_comb_hard_addtime;
  7935. + uint64_t sadb_comb_soft_usetime;
  7936. + uint64_t sadb_comb_hard_usetime;
  7937. + uint32_t sadb_x_comb_soft_packets;
  7938. + uint32_t sadb_x_comb_hard_packets;
  7939. +};
  7940. +
  7941. +struct sadb_supported {
  7942. + uint16_t sadb_supported_len;
  7943. + uint16_t sadb_supported_exttype;
  7944. + uint32_t sadb_supported_reserved;
  7945. +};
  7946. +
  7947. +struct sadb_alg {
  7948. + uint8_t sadb_alg_id;
  7949. + uint8_t sadb_alg_ivlen;
  7950. + uint16_t sadb_alg_minbits;
  7951. + uint16_t sadb_alg_maxbits;
  7952. + uint16_t sadb_alg_reserved;
  7953. +};
  7954. +
  7955. +struct sadb_spirange {
  7956. + uint16_t sadb_spirange_len;
  7957. + uint16_t sadb_spirange_exttype;
  7958. + uint32_t sadb_spirange_min;
  7959. + uint32_t sadb_spirange_max;
  7960. + uint32_t sadb_spirange_reserved;
  7961. +};
  7962. +
  7963. +struct sadb_x_kmprivate {
  7964. + uint16_t sadb_x_kmprivate_len;
  7965. + uint16_t sadb_x_kmprivate_exttype;
  7966. + uint32_t sadb_x_kmprivate_reserved;
  7967. +};
  7968. +
  7969. +struct sadb_x_satype {
  7970. + uint16_t sadb_x_satype_len;
  7971. + uint16_t sadb_x_satype_exttype;
  7972. + uint8_t sadb_x_satype_satype;
  7973. + uint8_t sadb_x_satype_reserved[3];
  7974. +};
  7975. +
  7976. +struct sadb_x_policy {
  7977. + uint16_t sadb_x_policy_len;
  7978. + uint16_t sadb_x_policy_exttype;
  7979. + uint16_t sadb_x_policy_type;
  7980. + uint8_t sadb_x_policy_dir;
  7981. + uint8_t sadb_x_policy_reserved;
  7982. + uint32_t sadb_x_policy_id;
  7983. + uint32_t sadb_x_policy_reserved2;
  7984. +};
  7985. +
  7986. +struct sadb_x_debug {
  7987. + uint16_t sadb_x_debug_len;
  7988. + uint16_t sadb_x_debug_exttype;
  7989. + uint32_t sadb_x_debug_tunnel;
  7990. + uint32_t sadb_x_debug_netlink;
  7991. + uint32_t sadb_x_debug_xform;
  7992. + uint32_t sadb_x_debug_eroute;
  7993. + uint32_t sadb_x_debug_spi;
  7994. + uint32_t sadb_x_debug_radij;
  7995. + uint32_t sadb_x_debug_esp;
  7996. + uint32_t sadb_x_debug_ah;
  7997. + uint32_t sadb_x_debug_rcv;
  7998. + uint32_t sadb_x_debug_pfkey;
  7999. + uint32_t sadb_x_debug_ipcomp;
  8000. + uint32_t sadb_x_debug_verbose;
  8001. + uint8_t sadb_x_debug_reserved[4];
  8002. +};
  8003. +
  8004. +struct sadb_x_nat_t_type {
  8005. + uint16_t sadb_x_nat_t_type_len;
  8006. + uint16_t sadb_x_nat_t_type_exttype;
  8007. + uint8_t sadb_x_nat_t_type_type;
  8008. + uint8_t sadb_x_nat_t_type_reserved[3];
  8009. +};
  8010. +struct sadb_x_nat_t_port {
  8011. + uint16_t sadb_x_nat_t_port_len;
  8012. + uint16_t sadb_x_nat_t_port_exttype;
  8013. + uint16_t sadb_x_nat_t_port_port;
  8014. + uint16_t sadb_x_nat_t_port_reserved;
  8015. +};
  8016. +
  8017. +/*
  8018. + * A protocol structure for passing through the transport level
  8019. + * protocol. It contains more fields than are actually used/needed
  8020. + * but it is this way to be compatible with the structure used in
  8021. + * OpenBSD (http://www.openbsd.org/cgi-bin/cvsweb/src/sys/net/pfkeyv2.h)
  8022. + */
  8023. +struct sadb_protocol {
  8024. + uint16_t sadb_protocol_len;
  8025. + uint16_t sadb_protocol_exttype;
  8026. + uint8_t sadb_protocol_proto;
  8027. + uint8_t sadb_protocol_direction;
  8028. + uint8_t sadb_protocol_flags;
  8029. + uint8_t sadb_protocol_reserved2;
  8030. +};
  8031. +
  8032. +#define SADB_EXT_RESERVED 0
  8033. +#define SADB_EXT_SA 1
  8034. +#define SADB_EXT_LIFETIME_CURRENT 2
  8035. +#define SADB_EXT_LIFETIME_HARD 3
  8036. +#define SADB_EXT_LIFETIME_SOFT 4
  8037. +#define SADB_EXT_ADDRESS_SRC 5
  8038. +#define SADB_EXT_ADDRESS_DST 6
  8039. +#define SADB_EXT_ADDRESS_PROXY 7
  8040. +#define SADB_EXT_KEY_AUTH 8
  8041. +#define SADB_EXT_KEY_ENCRYPT 9
  8042. +#define SADB_EXT_IDENTITY_SRC 10
  8043. +#define SADB_EXT_IDENTITY_DST 11
  8044. +#define SADB_EXT_SENSITIVITY 12
  8045. +#define SADB_EXT_PROPOSAL 13
  8046. +#define SADB_EXT_SUPPORTED_AUTH 14
  8047. +#define SADB_EXT_SUPPORTED_ENCRYPT 15
  8048. +#define SADB_EXT_SPIRANGE 16
  8049. +#define SADB_X_EXT_KMPRIVATE 17
  8050. +#define SADB_X_EXT_SATYPE2 18
  8051. +#ifdef KERNEL26_HAS_KAME_DUPLICATES
  8052. +#define SADB_X_EXT_POLICY 18
  8053. +#endif
  8054. +#define SADB_X_EXT_SA2 19
  8055. +#define SADB_X_EXT_ADDRESS_DST2 20
  8056. +#define SADB_X_EXT_ADDRESS_SRC_FLOW 21
  8057. +#define SADB_X_EXT_ADDRESS_DST_FLOW 22
  8058. +#define SADB_X_EXT_ADDRESS_SRC_MASK 23
  8059. +#define SADB_X_EXT_ADDRESS_DST_MASK 24
  8060. +#define SADB_X_EXT_DEBUG 25
  8061. +#define SADB_X_EXT_PROTOCOL 26
  8062. +#define SADB_X_EXT_NAT_T_TYPE 27
  8063. +#define SADB_X_EXT_NAT_T_SPORT 28
  8064. +#define SADB_X_EXT_NAT_T_DPORT 29
  8065. +#define SADB_X_EXT_NAT_T_OA 30
  8066. +#define SADB_EXT_MAX 30
  8067. +
  8068. +/* SADB_X_DELFLOW required over and above SADB_X_SAFLAGS_CLEARFLOW */
  8069. +#define SADB_X_EXT_ADDRESS_DELFLOW \
  8070. + ( (1<<SADB_X_EXT_ADDRESS_SRC_FLOW) \
  8071. + | (1<<SADB_X_EXT_ADDRESS_DST_FLOW) \
  8072. + | (1<<SADB_X_EXT_ADDRESS_SRC_MASK) \
  8073. + | (1<<SADB_X_EXT_ADDRESS_DST_MASK))
  8074. +
  8075. +#define SADB_SATYPE_UNSPEC 0
  8076. +#define SADB_SATYPE_AH 2
  8077. +#define SADB_SATYPE_ESP 3
  8078. +#define SADB_SATYPE_RSVP 5
  8079. +#define SADB_SATYPE_OSPFV2 6
  8080. +#define SADB_SATYPE_RIPV2 7
  8081. +#define SADB_SATYPE_MIP 8
  8082. +#define SADB_X_SATYPE_IPIP 9
  8083. +#ifdef KERNEL26_HAS_KAME_DUPLICATES
  8084. +#define SADB_X_SATYPE_IPCOMP 9 /* ICK! */
  8085. +#endif
  8086. +#define SADB_X_SATYPE_COMP 10
  8087. +#define SADB_X_SATYPE_INT 11
  8088. +#define SADB_SATYPE_MAX 11
  8089. +
  8090. +enum sadb_sastate {
  8091. + SADB_SASTATE_LARVAL=0,
  8092. + SADB_SASTATE_MATURE=1,
  8093. + SADB_SASTATE_DYING=2,
  8094. + SADB_SASTATE_DEAD=3
  8095. +};
  8096. +#define SADB_SASTATE_MAX 3
  8097. +
  8098. +#define SADB_SAFLAGS_PFS 1
  8099. +#define SADB_X_SAFLAGS_REPLACEFLOW 2
  8100. +#define SADB_X_SAFLAGS_CLEARFLOW 4
  8101. +#define SADB_X_SAFLAGS_INFLOW 8
  8102. +
  8103. +/* not obvious, but these are the same values as used in isakmp,
  8104. + * and in freeswan/ipsec_policy.h. If you need to add any, they
  8105. + * should be added as according to
  8106. + * http://www.iana.org/assignments/isakmp-registry
  8107. + *
  8108. + * and if not, then please try to use a private-use value, and
  8109. + * consider asking IANA to assign a value.
  8110. + */
  8111. +#define SADB_AALG_NONE 0
  8112. +#define SADB_AALG_MD5HMAC 2
  8113. +#define SADB_AALG_SHA1HMAC 3
  8114. +#define SADB_X_AALG_SHA2_256HMAC 5
  8115. +#define SADB_X_AALG_SHA2_384HMAC 6
  8116. +#define SADB_X_AALG_SHA2_512HMAC 7
  8117. +#define SADB_X_AALG_RIPEMD160HMAC 8
  8118. +#define SADB_X_AALG_NULL 251 /* kame */
  8119. +#define SADB_AALG_MAX 251
  8120. +
  8121. +#define SADB_EALG_NONE 0
  8122. +#define SADB_EALG_DESCBC 2
  8123. +#define SADB_EALG_3DESCBC 3
  8124. +#define SADB_X_EALG_CASTCBC 6
  8125. +#define SADB_X_EALG_BLOWFISHCBC 7
  8126. +#define SADB_EALG_NULL 11
  8127. +#define SADB_X_EALG_AESCBC 12
  8128. +#define SADB_EALG_MAX 255
  8129. +
  8130. +#define SADB_X_CALG_NONE 0
  8131. +#define SADB_X_CALG_OUI 1
  8132. +#define SADB_X_CALG_DEFLATE 2
  8133. +#define SADB_X_CALG_LZS 3
  8134. +#define SADB_X_CALG_V42BIS 4
  8135. +#ifdef KERNEL26_HAS_KAME_DUPLICATES
  8136. +#define SADB_X_CALG_LZJH 4
  8137. +#endif
  8138. +#define SADB_X_CALG_MAX 4
  8139. +
  8140. +#define SADB_X_TALG_NONE 0
  8141. +#define SADB_X_TALG_IPv4_in_IPv4 1
  8142. +#define SADB_X_TALG_IPv6_in_IPv4 2
  8143. +#define SADB_X_TALG_IPv4_in_IPv6 3
  8144. +#define SADB_X_TALG_IPv6_in_IPv6 4
  8145. +#define SADB_X_TALG_MAX 4
  8146. +
  8147. +
  8148. +#define SADB_IDENTTYPE_RESERVED 0
  8149. +#define SADB_IDENTTYPE_PREFIX 1
  8150. +#define SADB_IDENTTYPE_FQDN 2
  8151. +#define SADB_IDENTTYPE_USERFQDN 3
  8152. +#define SADB_X_IDENTTYPE_CONNECTION 4
  8153. +#define SADB_IDENTTYPE_MAX 4
  8154. +
  8155. +#define SADB_KEY_FLAGS_MAX 0
  8156. +#endif /* __PFKEY_V2_H */
  8157. +
  8158. +/*
  8159. + * $Log: pfkeyv2.h,v $
  8160. + * Revision 1.31 2005/04/14 01:14:54 mcr
  8161. + * change sadb_state to an enum.
  8162. + *
  8163. + * Revision 1.30 2004/04/06 02:49:00 mcr
  8164. + * pullup of algo code from alg-branch.
  8165. + *
  8166. + * Revision 1.29 2003/12/22 21:35:58 mcr
  8167. + * new patches from Dr{Who}.
  8168. + *
  8169. + * Revision 1.28 2003/12/22 19:33:15 mcr
  8170. + * added 0.6c NAT-T patch.
  8171. + *
  8172. + * Revision 1.27 2003/12/10 01:20:01 mcr
  8173. + * NAT-traversal patches to KLIPS.
  8174. + *
  8175. + * Revision 1.26 2003/10/31 02:26:44 mcr
  8176. + * pulled up port-selector patches.
  8177. + *
  8178. + * Revision 1.25.4.1 2003/09/21 13:59:34 mcr
  8179. + * pre-liminary X.509 patch - does not yet pass tests.
  8180. + *
  8181. + * Revision 1.25 2003/07/31 23:59:17 mcr
  8182. + * re-introduce kernel 2.6 duplicate values for now.
  8183. + * hope to get them changed!
  8184. + *
  8185. + * Revision 1.24 2003/07/31 22:55:27 mcr
  8186. + * added some definitions to keep pfkeyv2.h files in sync.
  8187. + *
  8188. + * Revision 1.23 2003/05/11 00:43:48 mcr
  8189. + * added comment about origin of values used
  8190. + *
  8191. + * Revision 1.22 2003/01/30 02:31:34 rgb
  8192. + *
  8193. + * Convert IPsecSAref_t from signed to unsigned to fix apparent SAref exhaustion bug.
  8194. + *
  8195. + * Revision 1.21 2002/12/16 19:26:49 mcr
  8196. + * added definition of FS 1.xx sadb structure
  8197. + *
  8198. + * Revision 1.20 2002/09/20 15:40:25 rgb
  8199. + * Added sadb_x_sa_ref to struct sadb_sa.
  8200. + *
  8201. + * Revision 1.19 2002/04/24 07:36:49 mcr
  8202. + * Moved from ./lib/pfkeyv2.h,v
  8203. + *
  8204. + * Revision 1.18 2001/11/06 19:47:47 rgb
  8205. + * Added packet parameter to lifetime and comb structures.
  8206. + *
  8207. + * Revision 1.17 2001/09/08 21:13:35 rgb
  8208. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  8209. + *
  8210. + * Revision 1.16 2001/07/06 19:49:46 rgb
  8211. + * Added SADB_X_SAFLAGS_INFLOW for supporting incoming policy checks.
  8212. + *
  8213. + * Revision 1.15 2001/02/26 20:00:43 rgb
  8214. + * Added internal IP protocol 61 for magic SAs.
  8215. + *
  8216. + * Revision 1.14 2001/02/08 18:51:05 rgb
  8217. + * Include RFC document title and appendix subsection title.
  8218. + *
  8219. + * Revision 1.13 2000/10/10 20:10:20 rgb
  8220. + * Added support for debug_ipcomp and debug_verbose to klipsdebug.
  8221. + *
  8222. + * Revision 1.12 2000/09/15 06:41:50 rgb
  8223. + * Added V42BIS constant.
  8224. + *
  8225. + * Revision 1.11 2000/09/12 22:35:37 rgb
  8226. + * Restructured to remove unused extensions from CLEARFLOW messages.
  8227. + *
  8228. + * Revision 1.10 2000/09/12 18:50:09 rgb
  8229. + * Added IPIP tunnel types as algo support.
  8230. + *
  8231. + * Revision 1.9 2000/08/21 16:47:19 rgb
  8232. + * Added SADB_X_CALG_* macros for IPCOMP.
  8233. + *
  8234. + * Revision 1.8 2000/08/09 20:43:34 rgb
  8235. + * Fixed bitmask value for SADB_X_SAFLAGS_CLEAREROUTE.
  8236. + *
  8237. + * Revision 1.7 2000/01/21 06:28:37 rgb
  8238. + * Added flow add/delete message type macros.
  8239. + * Added flow address extension type macros.
  8240. + * Tidied up spacing.
  8241. + * Added klipsdebug switching capability.
  8242. + *
  8243. + * Revision 1.6 1999/11/27 11:56:08 rgb
  8244. + * Add SADB_X_SATYPE_COMP for compression, eventually.
  8245. + *
  8246. + * Revision 1.5 1999/11/23 22:23:16 rgb
  8247. + * This file has been moved in the distribution from klips/net/ipsec to
  8248. + * lib.
  8249. + *
  8250. + * Revision 1.4 1999/04/29 15:23:29 rgb
  8251. + * Add GRPSA support.
  8252. + * Add support for a second SATYPE, SA and DST_ADDRESS.
  8253. + * Add IPPROTO_IPIP support.
  8254. + *
  8255. + * Revision 1.3 1999/04/15 17:58:08 rgb
  8256. + * Add RCSID labels.
  8257. + *
  8258. + */
  8259. --- /dev/null Tue Mar 11 13:02:56 2003
  8260. +++ linux/include/zlib/zconf.h Mon Feb 9 13:51:03 2004
  8261. @@ -0,0 +1,309 @@
  8262. +/* zconf.h -- configuration of the zlib compression library
  8263. + * Copyright (C) 1995-2002 Jean-loup Gailly.
  8264. + * For conditions of distribution and use, see copyright notice in zlib.h
  8265. + */
  8266. +
  8267. +/* @(#) $Id: zconf.h,v 1.4 2004/07/10 07:48:40 mcr Exp $ */
  8268. +
  8269. +#ifndef _ZCONF_H
  8270. +#define _ZCONF_H
  8271. +
  8272. +/*
  8273. + * If you *really* need a unique prefix for all types and library functions,
  8274. + * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
  8275. + */
  8276. +#ifdef IPCOMP_PREFIX
  8277. +# define deflateInit_ ipcomp_deflateInit_
  8278. +# define deflate ipcomp_deflate
  8279. +# define deflateEnd ipcomp_deflateEnd
  8280. +# define inflateInit_ ipcomp_inflateInit_
  8281. +# define inflate ipcomp_inflate
  8282. +# define inflateEnd ipcomp_inflateEnd
  8283. +# define deflateInit2_ ipcomp_deflateInit2_
  8284. +# define deflateSetDictionary ipcomp_deflateSetDictionary
  8285. +# define deflateCopy ipcomp_deflateCopy
  8286. +# define deflateReset ipcomp_deflateReset
  8287. +# define deflateParams ipcomp_deflateParams
  8288. +# define inflateInit2_ ipcomp_inflateInit2_
  8289. +# define inflateSetDictionary ipcomp_inflateSetDictionary
  8290. +# define inflateSync ipcomp_inflateSync
  8291. +# define inflateSyncPoint ipcomp_inflateSyncPoint
  8292. +# define inflateReset ipcomp_inflateReset
  8293. +# define compress ipcomp_compress
  8294. +# define compress2 ipcomp_compress2
  8295. +# define uncompress ipcomp_uncompress
  8296. +# define adler32 ipcomp_adler32
  8297. +# define crc32 ipcomp_crc32
  8298. +# define get_crc_table ipcomp_get_crc_table
  8299. +/* SSS: these also need to be prefixed to avoid clash with ppp_deflate and ext2compression */
  8300. +# define inflate_blocks ipcomp_deflate_blocks
  8301. +# define inflate_blocks_free ipcomp_deflate_blocks_free
  8302. +# define inflate_blocks_new ipcomp_inflate_blocks_new
  8303. +# define inflate_blocks_reset ipcomp_inflate_blocks_reset
  8304. +# define inflate_blocks_sync_point ipcomp_inflate_blocks_sync_point
  8305. +# define inflate_set_dictionary ipcomp_inflate_set_dictionary
  8306. +# define inflate_codes ipcomp_inflate_codes
  8307. +# define inflate_codes_free ipcomp_inflate_codes_free
  8308. +# define inflate_codes_new ipcomp_inflate_codes_new
  8309. +# define inflate_fast ipcomp_inflate_fast
  8310. +# define inflate_trees_bits ipcomp_inflate_trees_bits
  8311. +# define inflate_trees_dynamic ipcomp_inflate_trees_dynamic
  8312. +# define inflate_trees_fixed ipcomp_inflate_trees_fixed
  8313. +# define inflate_flush ipcomp_inflate_flush
  8314. +# define inflate_mask ipcomp_inflate_mask
  8315. +# define _dist_code _ipcomp_dist_code
  8316. +# define _length_code _ipcomp_length_code
  8317. +# define _tr_align _ipcomp_tr_align
  8318. +# define _tr_flush_block _ipcomp_tr_flush_block
  8319. +# define _tr_init _ipcomp_tr_init
  8320. +# define _tr_stored_block _ipcomp_tr_stored_block
  8321. +# define _tr_tally _ipcomp_tr_tally
  8322. +# define zError ipcomp_zError
  8323. +# define z_errmsg ipcomp_z_errmsg
  8324. +# define zlibVersion ipcomp_zlibVersion
  8325. +# define match_init ipcomp_match_init
  8326. +# define longest_match ipcomp_longest_match
  8327. +#endif
  8328. +
  8329. +#ifdef Z_PREFIX
  8330. +# define Byte z_Byte
  8331. +# define uInt z_uInt
  8332. +# define uLong z_uLong
  8333. +# define Bytef z_Bytef
  8334. +# define charf z_charf
  8335. +# define intf z_intf
  8336. +# define uIntf z_uIntf
  8337. +# define uLongf z_uLongf
  8338. +# define voidpf z_voidpf
  8339. +# define voidp z_voidp
  8340. +#endif
  8341. +
  8342. +#if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
  8343. +# define WIN32
  8344. +#endif
  8345. +#if defined(__GNUC__) || defined(WIN32) || defined(__386__) || defined(i386)
  8346. +# ifndef __32BIT__
  8347. +# define __32BIT__
  8348. +# endif
  8349. +#endif
  8350. +#if defined(__MSDOS__) && !defined(MSDOS)
  8351. +# define MSDOS
  8352. +#endif
  8353. +
  8354. +/*
  8355. + * Compile with -DMAXSEG_64K if the alloc function cannot allocate more
  8356. + * than 64k bytes at a time (needed on systems with 16-bit int).
  8357. + */
  8358. +#if defined(MSDOS) && !defined(__32BIT__)
  8359. +# define MAXSEG_64K
  8360. +#endif
  8361. +#ifdef MSDOS
  8362. +# define UNALIGNED_OK
  8363. +#endif
  8364. +
  8365. +#if (defined(MSDOS) || defined(_WINDOWS) || defined(WIN32)) && !defined(STDC)
  8366. +# define STDC
  8367. +#endif
  8368. +#if defined(__STDC__) || defined(__cplusplus) || defined(__OS2__)
  8369. +# ifndef STDC
  8370. +# define STDC
  8371. +# endif
  8372. +#endif
  8373. +
  8374. +#ifndef STDC
  8375. +# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */
  8376. +# define const
  8377. +# endif
  8378. +#endif
  8379. +
  8380. +/* Some Mac compilers merge all .h files incorrectly: */
  8381. +#if defined(__MWERKS__) || defined(applec) ||defined(THINK_C) ||defined(__SC__)
  8382. +# define NO_DUMMY_DECL
  8383. +#endif
  8384. +
  8385. +/* Old Borland C incorrectly complains about missing returns: */
  8386. +#if defined(__BORLANDC__) && (__BORLANDC__ < 0x500)
  8387. +# define NEED_DUMMY_RETURN
  8388. +#endif
  8389. +
  8390. +
  8391. +/* Maximum value for memLevel in deflateInit2 */
  8392. +#ifndef MAX_MEM_LEVEL
  8393. +# ifdef MAXSEG_64K
  8394. +# define MAX_MEM_LEVEL 8
  8395. +# else
  8396. +# define MAX_MEM_LEVEL 9
  8397. +# endif
  8398. +#endif
  8399. +
  8400. +/* Maximum value for windowBits in deflateInit2 and inflateInit2.
  8401. + * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
  8402. + * created by gzip. (Files created by minigzip can still be extracted by
  8403. + * gzip.)
  8404. + */
  8405. +#ifndef MAX_WBITS
  8406. +# define MAX_WBITS 15 /* 32K LZ77 window */
  8407. +#endif
  8408. +
  8409. +/* The memory requirements for deflate are (in bytes):
  8410. + (1 << (windowBits+2)) + (1 << (memLevel+9))
  8411. + that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
  8412. + plus a few kilobytes for small objects. For example, if you want to reduce
  8413. + the default memory requirements from 256K to 128K, compile with
  8414. + make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
  8415. + Of course this will generally degrade compression (there's no free lunch).
  8416. +
  8417. + The memory requirements for inflate are (in bytes) 1 << windowBits
  8418. + that is, 32K for windowBits=15 (default value) plus a few kilobytes
  8419. + for small objects.
  8420. +*/
  8421. +
  8422. + /* Type declarations */
  8423. +
  8424. +#ifndef OF /* function prototypes */
  8425. +# ifdef STDC
  8426. +# define OF(args) args
  8427. +# else
  8428. +# define OF(args) ()
  8429. +# endif
  8430. +#endif
  8431. +
  8432. +/* The following definitions for FAR are needed only for MSDOS mixed
  8433. + * model programming (small or medium model with some far allocations).
  8434. + * This was tested only with MSC; for other MSDOS compilers you may have
  8435. + * to define NO_MEMCPY in zutil.h. If you don't need the mixed model,
  8436. + * just define FAR to be empty.
  8437. + */
  8438. +#if (defined(M_I86SM) || defined(M_I86MM)) && !defined(__32BIT__)
  8439. + /* MSC small or medium model */
  8440. +# define SMALL_MEDIUM
  8441. +# ifdef _MSC_VER
  8442. +# define FAR _far
  8443. +# else
  8444. +# define FAR far
  8445. +# endif
  8446. +#endif
  8447. +#if defined(__BORLANDC__) && (defined(__SMALL__) || defined(__MEDIUM__))
  8448. +# ifndef __32BIT__
  8449. +# define SMALL_MEDIUM
  8450. +# define FAR _far
  8451. +# endif
  8452. +#endif
  8453. +
  8454. +/* Compile with -DZLIB_DLL for Windows DLL support */
  8455. +#if defined(ZLIB_DLL)
  8456. +# if defined(_WINDOWS) || defined(WINDOWS)
  8457. +# ifdef FAR
  8458. +# undef FAR
  8459. +# endif
  8460. +# include <windows.h>
  8461. +# define ZEXPORT WINAPI
  8462. +# ifdef WIN32
  8463. +# define ZEXPORTVA WINAPIV
  8464. +# else
  8465. +# define ZEXPORTVA FAR _cdecl _export
  8466. +# endif
  8467. +# endif
  8468. +# if defined (__BORLANDC__)
  8469. +# if (__BORLANDC__ >= 0x0500) && defined (WIN32)
  8470. +# include <windows.h>
  8471. +# define ZEXPORT __declspec(dllexport) WINAPI
  8472. +# define ZEXPORTRVA __declspec(dllexport) WINAPIV
  8473. +# else
  8474. +# if defined (_Windows) && defined (__DLL__)
  8475. +# define ZEXPORT _export
  8476. +# define ZEXPORTVA _export
  8477. +# endif
  8478. +# endif
  8479. +# endif
  8480. +#endif
  8481. +
  8482. +#if defined (__BEOS__)
  8483. +# if defined (ZLIB_DLL)
  8484. +# define ZEXTERN extern __declspec(dllexport)
  8485. +# else
  8486. +# define ZEXTERN extern __declspec(dllimport)
  8487. +# endif
  8488. +#endif
  8489. +
  8490. +#ifndef ZEXPORT
  8491. +# define ZEXPORT
  8492. +#endif
  8493. +#ifndef ZEXPORTVA
  8494. +# define ZEXPORTVA
  8495. +#endif
  8496. +#ifndef ZEXTERN
  8497. +# define ZEXTERN extern
  8498. +#endif
  8499. +
  8500. +#ifndef FAR
  8501. +# define FAR
  8502. +#endif
  8503. +
  8504. +#if !defined(MACOS) && !defined(TARGET_OS_MAC)
  8505. +typedef unsigned char Byte; /* 8 bits */
  8506. +#endif
  8507. +typedef unsigned int uInt; /* 16 bits or more */
  8508. +typedef unsigned long uLong; /* 32 bits or more */
  8509. +
  8510. +#ifdef SMALL_MEDIUM
  8511. + /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */
  8512. +# define Bytef Byte FAR
  8513. +#else
  8514. + typedef Byte FAR Bytef;
  8515. +#endif
  8516. +typedef char FAR charf;
  8517. +typedef int FAR intf;
  8518. +typedef uInt FAR uIntf;
  8519. +typedef uLong FAR uLongf;
  8520. +
  8521. +#ifdef STDC
  8522. + typedef void FAR *voidpf;
  8523. + typedef void *voidp;
  8524. +#else
  8525. + typedef Byte FAR *voidpf;
  8526. + typedef Byte *voidp;
  8527. +#endif
  8528. +
  8529. +#ifdef HAVE_UNISTD_H
  8530. +# include <sys/types.h> /* for off_t */
  8531. +# include <unistd.h> /* for SEEK_* and off_t */
  8532. +# define z_off_t off_t
  8533. +#endif
  8534. +#ifndef SEEK_SET
  8535. +# define SEEK_SET 0 /* Seek from beginning of file. */
  8536. +# define SEEK_CUR 1 /* Seek from current position. */
  8537. +# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
  8538. +#endif
  8539. +#ifndef z_off_t
  8540. +# define z_off_t long
  8541. +#endif
  8542. +
  8543. +/* MVS linker does not support external names larger than 8 bytes */
  8544. +#if defined(__MVS__)
  8545. +# pragma map(deflateInit_,"DEIN")
  8546. +# pragma map(deflateInit2_,"DEIN2")
  8547. +# pragma map(deflateEnd,"DEEND")
  8548. +# pragma map(inflateInit_,"ININ")
  8549. +# pragma map(inflateInit2_,"ININ2")
  8550. +# pragma map(inflateEnd,"INEND")
  8551. +# pragma map(inflateSync,"INSY")
  8552. +# pragma map(inflateSetDictionary,"INSEDI")
  8553. +# pragma map(inflate_blocks,"INBL")
  8554. +# pragma map(inflate_blocks_new,"INBLNE")
  8555. +# pragma map(inflate_blocks_free,"INBLFR")
  8556. +# pragma map(inflate_blocks_reset,"INBLRE")
  8557. +# pragma map(inflate_codes_free,"INCOFR")
  8558. +# pragma map(inflate_codes,"INCO")
  8559. +# pragma map(inflate_fast,"INFA")
  8560. +# pragma map(inflate_flush,"INFLU")
  8561. +# pragma map(inflate_mask,"INMA")
  8562. +# pragma map(inflate_set_dictionary,"INSEDI2")
  8563. +# pragma map(ipcomp_inflate_copyright,"INCOPY")
  8564. +# pragma map(inflate_trees_bits,"INTRBI")
  8565. +# pragma map(inflate_trees_dynamic,"INTRDY")
  8566. +# pragma map(inflate_trees_fixed,"INTRFI")
  8567. +# pragma map(inflate_trees_free,"INTRFR")
  8568. +#endif
  8569. +
  8570. +#endif /* _ZCONF_H */
  8571. --- /dev/null Tue Mar 11 13:02:56 2003
  8572. +++ linux/include/zlib/zlib.h Mon Feb 9 13:51:03 2004
  8573. @@ -0,0 +1,893 @@
  8574. +/* zlib.h -- interface of the 'zlib' general purpose compression library
  8575. + version 1.1.4, March 11th, 2002
  8576. +
  8577. + Copyright (C) 1995-2002 Jean-loup Gailly and Mark Adler
  8578. +
  8579. + This software is provided 'as-is', without any express or implied
  8580. + warranty. In no event will the authors be held liable for any damages
  8581. + arising from the use of this software.
  8582. +
  8583. + Permission is granted to anyone to use this software for any purpose,
  8584. + including commercial applications, and to alter it and redistribute it
  8585. + freely, subject to the following restrictions:
  8586. +
  8587. + 1. The origin of this software must not be misrepresented; you must not
  8588. + claim that you wrote the original software. If you use this software
  8589. + in a product, an acknowledgment in the product documentation would be
  8590. + appreciated but is not required.
  8591. + 2. Altered source versions must be plainly marked as such, and must not be
  8592. + misrepresented as being the original software.
  8593. + 3. This notice may not be removed or altered from any source distribution.
  8594. +
  8595. + Jean-loup Gailly Mark Adler
  8596. + jloup@gzip.org madler@alumni.caltech.edu
  8597. +
  8598. +
  8599. + The data format used by the zlib library is described by RFCs (Request for
  8600. + Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
  8601. + (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
  8602. +*/
  8603. +
  8604. +#ifndef _ZLIB_H
  8605. +#define _ZLIB_H
  8606. +
  8607. +#include "zconf.h"
  8608. +
  8609. +#ifdef __cplusplus
  8610. +extern "C" {
  8611. +#endif
  8612. +
  8613. +#define ZLIB_VERSION "1.1.4"
  8614. +
  8615. +/*
  8616. + The 'zlib' compression library provides in-memory compression and
  8617. + decompression functions, including integrity checks of the uncompressed
  8618. + data. This version of the library supports only one compression method
  8619. + (deflation) but other algorithms will be added later and will have the same
  8620. + stream interface.
  8621. +
  8622. + Compression can be done in a single step if the buffers are large
  8623. + enough (for example if an input file is mmap'ed), or can be done by
  8624. + repeated calls of the compression function. In the latter case, the
  8625. + application must provide more input and/or consume the output
  8626. + (providing more output space) before each call.
  8627. +
  8628. + The library also supports reading and writing files in gzip (.gz) format
  8629. + with an interface similar to that of stdio.
  8630. +
  8631. + The library does not install any signal handler. The decoder checks
  8632. + the consistency of the compressed data, so the library should never
  8633. + crash even in case of corrupted input.
  8634. +*/
  8635. +
  8636. +typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
  8637. +typedef void (*free_func) OF((voidpf opaque, voidpf address));
  8638. +
  8639. +struct internal_state;
  8640. +
  8641. +typedef struct z_stream_s {
  8642. + Bytef *next_in; /* next input byte */
  8643. + uInt avail_in; /* number of bytes available at next_in */
  8644. + uLong total_in; /* total nb of input bytes read so far */
  8645. +
  8646. + Bytef *next_out; /* next output byte should be put there */
  8647. + uInt avail_out; /* remaining free space at next_out */
  8648. + uLong total_out; /* total nb of bytes output so far */
  8649. +
  8650. + const char *msg; /* last error message, NULL if no error */
  8651. + struct internal_state FAR *state; /* not visible by applications */
  8652. +
  8653. + alloc_func zalloc; /* used to allocate the internal state */
  8654. + free_func zfree; /* used to free the internal state */
  8655. + voidpf opaque; /* private data object passed to zalloc and zfree */
  8656. +
  8657. + int data_type; /* best guess about the data type: ascii or binary */
  8658. + uLong adler; /* adler32 value of the uncompressed data */
  8659. + uLong reserved; /* reserved for future use */
  8660. +} z_stream;
  8661. +
  8662. +typedef z_stream FAR *z_streamp;
  8663. +
  8664. +/*
  8665. + The application must update next_in and avail_in when avail_in has
  8666. + dropped to zero. It must update next_out and avail_out when avail_out
  8667. + has dropped to zero. The application must initialize zalloc, zfree and
  8668. + opaque before calling the init function. All other fields are set by the
  8669. + compression library and must not be updated by the application.
  8670. +
  8671. + The opaque value provided by the application will be passed as the first
  8672. + parameter for calls of zalloc and zfree. This can be useful for custom
  8673. + memory management. The compression library attaches no meaning to the
  8674. + opaque value.
  8675. +
  8676. + zalloc must return Z_NULL if there is not enough memory for the object.
  8677. + If zlib is used in a multi-threaded application, zalloc and zfree must be
  8678. + thread safe.
  8679. +
  8680. + On 16-bit systems, the functions zalloc and zfree must be able to allocate
  8681. + exactly 65536 bytes, but will not be required to allocate more than this
  8682. + if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
  8683. + pointers returned by zalloc for objects of exactly 65536 bytes *must*
  8684. + have their offset normalized to zero. The default allocation function
  8685. + provided by this library ensures this (see zutil.c). To reduce memory
  8686. + requirements and avoid any allocation of 64K objects, at the expense of
  8687. + compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
  8688. +
  8689. + The fields total_in and total_out can be used for statistics or
  8690. + progress reports. After compression, total_in holds the total size of
  8691. + the uncompressed data and may be saved for use in the decompressor
  8692. + (particularly if the decompressor wants to decompress everything in
  8693. + a single step).
  8694. +*/
  8695. +
  8696. + /* constants */
  8697. +
  8698. +#define Z_NO_FLUSH 0
  8699. +#define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
  8700. +#define Z_SYNC_FLUSH 2
  8701. +#define Z_FULL_FLUSH 3
  8702. +#define Z_FINISH 4
  8703. +/* Allowed flush values; see deflate() below for details */
  8704. +
  8705. +#define Z_OK 0
  8706. +#define Z_STREAM_END 1
  8707. +#define Z_NEED_DICT 2
  8708. +#define Z_ERRNO (-1)
  8709. +#define Z_STREAM_ERROR (-2)
  8710. +#define Z_DATA_ERROR (-3)
  8711. +#define Z_MEM_ERROR (-4)
  8712. +#define Z_BUF_ERROR (-5)
  8713. +#define Z_VERSION_ERROR (-6)
  8714. +/* Return codes for the compression/decompression functions. Negative
  8715. + * values are errors, positive values are used for special but normal events.
  8716. + */
  8717. +
  8718. +#define Z_NO_COMPRESSION 0
  8719. +#define Z_BEST_SPEED 1
  8720. +#define Z_BEST_COMPRESSION 9
  8721. +#define Z_DEFAULT_COMPRESSION (-1)
  8722. +/* compression levels */
  8723. +
  8724. +#define Z_FILTERED 1
  8725. +#define Z_HUFFMAN_ONLY 2
  8726. +#define Z_DEFAULT_STRATEGY 0
  8727. +/* compression strategy; see deflateInit2() below for details */
  8728. +
  8729. +#define Z_BINARY 0
  8730. +#define Z_ASCII 1
  8731. +#define Z_UNKNOWN 2
  8732. +/* Possible values of the data_type field */
  8733. +
  8734. +#define Z_DEFLATED 8
  8735. +/* The deflate compression method (the only one supported in this version) */
  8736. +
  8737. +#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */
  8738. +
  8739. +#define zlib_version zlibVersion()
  8740. +/* for compatibility with versions < 1.0.2 */
  8741. +
  8742. + /* basic functions */
  8743. +
  8744. +ZEXTERN const char * ZEXPORT zlibVersion OF((void));
  8745. +/* The application can compare zlibVersion and ZLIB_VERSION for consistency.
  8746. + If the first character differs, the library code actually used is
  8747. + not compatible with the zlib.h header file used by the application.
  8748. + This check is automatically made by deflateInit and inflateInit.
  8749. + */
  8750. +
  8751. +/*
  8752. +ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level));
  8753. +
  8754. + Initializes the internal stream state for compression. The fields
  8755. + zalloc, zfree and opaque must be initialized before by the caller.
  8756. + If zalloc and zfree are set to Z_NULL, deflateInit updates them to
  8757. + use default allocation functions.
  8758. +
  8759. + The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
  8760. + 1 gives best speed, 9 gives best compression, 0 gives no compression at
  8761. + all (the input data is simply copied a block at a time).
  8762. + Z_DEFAULT_COMPRESSION requests a default compromise between speed and
  8763. + compression (currently equivalent to level 6).
  8764. +
  8765. + deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
  8766. + enough memory, Z_STREAM_ERROR if level is not a valid compression level,
  8767. + Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
  8768. + with the version assumed by the caller (ZLIB_VERSION).
  8769. + msg is set to null if there is no error message. deflateInit does not
  8770. + perform any compression: this will be done by deflate().
  8771. +*/
  8772. +
  8773. +
  8774. +ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
  8775. +/*
  8776. + deflate compresses as much data as possible, and stops when the input
  8777. + buffer becomes empty or the output buffer becomes full. It may introduce some
  8778. + output latency (reading input without producing any output) except when
  8779. + forced to flush.
  8780. +
  8781. + The detailed semantics are as follows. deflate performs one or both of the
  8782. + following actions:
  8783. +
  8784. + - Compress more input starting at next_in and update next_in and avail_in
  8785. + accordingly. If not all input can be processed (because there is not
  8786. + enough room in the output buffer), next_in and avail_in are updated and
  8787. + processing will resume at this point for the next call of deflate().
  8788. +
  8789. + - Provide more output starting at next_out and update next_out and avail_out
  8790. + accordingly. This action is forced if the parameter flush is non zero.
  8791. + Forcing flush frequently degrades the compression ratio, so this parameter
  8792. + should be set only when necessary (in interactive applications).
  8793. + Some output may be provided even if flush is not set.
  8794. +
  8795. + Before the call of deflate(), the application should ensure that at least
  8796. + one of the actions is possible, by providing more input and/or consuming
  8797. + more output, and updating avail_in or avail_out accordingly; avail_out
  8798. + should never be zero before the call. The application can consume the
  8799. + compressed output when it wants, for example when the output buffer is full
  8800. + (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
  8801. + and with zero avail_out, it must be called again after making room in the
  8802. + output buffer because there might be more output pending.
  8803. +
  8804. + If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
  8805. + flushed to the output buffer and the output is aligned on a byte boundary, so
  8806. + that the decompressor can get all input data available so far. (In particular
  8807. + avail_in is zero after the call if enough output space has been provided
  8808. + before the call.) Flushing may degrade compression for some compression
  8809. + algorithms and so it should be used only when necessary.
  8810. +
  8811. + If flush is set to Z_FULL_FLUSH, all output is flushed as with
  8812. + Z_SYNC_FLUSH, and the compression state is reset so that decompression can
  8813. + restart from this point if previous compressed data has been damaged or if
  8814. + random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
  8815. + the compression.
  8816. +
  8817. + If deflate returns with avail_out == 0, this function must be called again
  8818. + with the same value of the flush parameter and more output space (updated
  8819. + avail_out), until the flush is complete (deflate returns with non-zero
  8820. + avail_out).
  8821. +
  8822. + If the parameter flush is set to Z_FINISH, pending input is processed,
  8823. + pending output is flushed and deflate returns with Z_STREAM_END if there
  8824. + was enough output space; if deflate returns with Z_OK, this function must be
  8825. + called again with Z_FINISH and more output space (updated avail_out) but no
  8826. + more input data, until it returns with Z_STREAM_END or an error. After
  8827. + deflate has returned Z_STREAM_END, the only possible operations on the
  8828. + stream are deflateReset or deflateEnd.
  8829. +
  8830. + Z_FINISH can be used immediately after deflateInit if all the compression
  8831. + is to be done in a single step. In this case, avail_out must be at least
  8832. + 0.1% larger than avail_in plus 12 bytes. If deflate does not return
  8833. + Z_STREAM_END, then it must be called again as described above.
  8834. +
  8835. + deflate() sets strm->adler to the adler32 checksum of all input read
  8836. + so far (that is, total_in bytes).
  8837. +
  8838. + deflate() may update data_type if it can make a good guess about
  8839. + the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered
  8840. + binary. This field is only for information purposes and does not affect
  8841. + the compression algorithm in any manner.
  8842. +
  8843. + deflate() returns Z_OK if some progress has been made (more input
  8844. + processed or more output produced), Z_STREAM_END if all input has been
  8845. + consumed and all output has been produced (only when flush is set to
  8846. + Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
  8847. + if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible
  8848. + (for example avail_in or avail_out was zero).
  8849. +*/
  8850. +
  8851. +
  8852. +ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));
  8853. +/*
  8854. + All dynamically allocated data structures for this stream are freed.
  8855. + This function discards any unprocessed input and does not flush any
  8856. + pending output.
  8857. +
  8858. + deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
  8859. + stream state was inconsistent, Z_DATA_ERROR if the stream was freed
  8860. + prematurely (some input or output was discarded). In the error case,
  8861. + msg may be set but then points to a static string (which must not be
  8862. + deallocated).
  8863. +*/
  8864. +
  8865. +
  8866. +/*
  8867. +ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm));
  8868. +
  8869. + Initializes the internal stream state for decompression. The fields
  8870. + next_in, avail_in, zalloc, zfree and opaque must be initialized before by
  8871. + the caller. If next_in is not Z_NULL and avail_in is large enough (the exact
  8872. + value depends on the compression method), inflateInit determines the
  8873. + compression method from the zlib header and allocates all data structures
  8874. + accordingly; otherwise the allocation will be deferred to the first call of
  8875. + inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to
  8876. + use default allocation functions.
  8877. +
  8878. + inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
  8879. + memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
  8880. + version assumed by the caller. msg is set to null if there is no error
  8881. + message. inflateInit does not perform any decompression apart from reading
  8882. + the zlib header if present: this will be done by inflate(). (So next_in and
  8883. + avail_in may be modified, but next_out and avail_out are unchanged.)
  8884. +*/
  8885. +
  8886. +
  8887. +ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
  8888. +/*
  8889. + inflate decompresses as much data as possible, and stops when the input
  8890. + buffer becomes empty or the output buffer becomes full. It may some
  8891. + introduce some output latency (reading input without producing any output)
  8892. + except when forced to flush.
  8893. +
  8894. + The detailed semantics are as follows. inflate performs one or both of the
  8895. + following actions:
  8896. +
  8897. + - Decompress more input starting at next_in and update next_in and avail_in
  8898. + accordingly. If not all input can be processed (because there is not
  8899. + enough room in the output buffer), next_in is updated and processing
  8900. + will resume at this point for the next call of inflate().
  8901. +
  8902. + - Provide more output starting at next_out and update next_out and avail_out
  8903. + accordingly. inflate() provides as much output as possible, until there
  8904. + is no more input data or no more space in the output buffer (see below
  8905. + about the flush parameter).
  8906. +
  8907. + Before the call of inflate(), the application should ensure that at least
  8908. + one of the actions is possible, by providing more input and/or consuming
  8909. + more output, and updating the next_* and avail_* values accordingly.
  8910. + The application can consume the uncompressed output when it wants, for
  8911. + example when the output buffer is full (avail_out == 0), or after each
  8912. + call of inflate(). If inflate returns Z_OK and with zero avail_out, it
  8913. + must be called again after making room in the output buffer because there
  8914. + might be more output pending.
  8915. +
  8916. + If the parameter flush is set to Z_SYNC_FLUSH, inflate flushes as much
  8917. + output as possible to the output buffer. The flushing behavior of inflate is
  8918. + not specified for values of the flush parameter other than Z_SYNC_FLUSH
  8919. + and Z_FINISH, but the current implementation actually flushes as much output
  8920. + as possible anyway.
  8921. +
  8922. + inflate() should normally be called until it returns Z_STREAM_END or an
  8923. + error. However if all decompression is to be performed in a single step
  8924. + (a single call of inflate), the parameter flush should be set to
  8925. + Z_FINISH. In this case all pending input is processed and all pending
  8926. + output is flushed; avail_out must be large enough to hold all the
  8927. + uncompressed data. (The size of the uncompressed data may have been saved
  8928. + by the compressor for this purpose.) The next operation on this stream must
  8929. + be inflateEnd to deallocate the decompression state. The use of Z_FINISH
  8930. + is never required, but can be used to inform inflate that a faster routine
  8931. + may be used for the single inflate() call.
  8932. +
  8933. + If a preset dictionary is needed at this point (see inflateSetDictionary
  8934. + below), inflate sets strm-adler to the adler32 checksum of the
  8935. + dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise
  8936. + it sets strm->adler to the adler32 checksum of all output produced
  8937. + so far (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or
  8938. + an error code as described below. At the end of the stream, inflate()
  8939. + checks that its computed adler32 checksum is equal to that saved by the
  8940. + compressor and returns Z_STREAM_END only if the checksum is correct.
  8941. +
  8942. + inflate() returns Z_OK if some progress has been made (more input processed
  8943. + or more output produced), Z_STREAM_END if the end of the compressed data has
  8944. + been reached and all uncompressed output has been produced, Z_NEED_DICT if a
  8945. + preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
  8946. + corrupted (input stream not conforming to the zlib format or incorrect
  8947. + adler32 checksum), Z_STREAM_ERROR if the stream structure was inconsistent
  8948. + (for example if next_in or next_out was NULL), Z_MEM_ERROR if there was not
  8949. + enough memory, Z_BUF_ERROR if no progress is possible or if there was not
  8950. + enough room in the output buffer when Z_FINISH is used. In the Z_DATA_ERROR
  8951. + case, the application may then call inflateSync to look for a good
  8952. + compression block.
  8953. +*/
  8954. +
  8955. +
  8956. +ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm));
  8957. +/*
  8958. + All dynamically allocated data structures for this stream are freed.
  8959. + This function discards any unprocessed input and does not flush any
  8960. + pending output.
  8961. +
  8962. + inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
  8963. + was inconsistent. In the error case, msg may be set but then points to a
  8964. + static string (which must not be deallocated).
  8965. +*/
  8966. +
  8967. + /* Advanced functions */
  8968. +
  8969. +/*
  8970. + The following functions are needed only in some special applications.
  8971. +*/
  8972. +
  8973. +/*
  8974. +ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm,
  8975. + int level,
  8976. + int method,
  8977. + int windowBits,
  8978. + int memLevel,
  8979. + int strategy));
  8980. +
  8981. + This is another version of deflateInit with more compression options. The
  8982. + fields next_in, zalloc, zfree and opaque must be initialized before by
  8983. + the caller.
  8984. +
  8985. + The method parameter is the compression method. It must be Z_DEFLATED in
  8986. + this version of the library.
  8987. +
  8988. + The windowBits parameter is the base two logarithm of the window size
  8989. + (the size of the history buffer). It should be in the range 8..15 for this
  8990. + version of the library. Larger values of this parameter result in better
  8991. + compression at the expense of memory usage. The default value is 15 if
  8992. + deflateInit is used instead.
  8993. +
  8994. + The memLevel parameter specifies how much memory should be allocated
  8995. + for the internal compression state. memLevel=1 uses minimum memory but
  8996. + is slow and reduces compression ratio; memLevel=9 uses maximum memory
  8997. + for optimal speed. The default value is 8. See zconf.h for total memory
  8998. + usage as a function of windowBits and memLevel.
  8999. +
  9000. + The strategy parameter is used to tune the compression algorithm. Use the
  9001. + value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
  9002. + filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no
  9003. + string match). Filtered data consists mostly of small values with a
  9004. + somewhat random distribution. In this case, the compression algorithm is
  9005. + tuned to compress them better. The effect of Z_FILTERED is to force more
  9006. + Huffman coding and less string matching; it is somewhat intermediate
  9007. + between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects
  9008. + the compression ratio but not the correctness of the compressed output even
  9009. + if it is not set appropriately.
  9010. +
  9011. + deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
  9012. + memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid
  9013. + method). msg is set to null if there is no error message. deflateInit2 does
  9014. + not perform any compression: this will be done by deflate().
  9015. +*/
  9016. +
  9017. +ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
  9018. + const Bytef *dictionary,
  9019. + uInt dictLength));
  9020. +/*
  9021. + Initializes the compression dictionary from the given byte sequence
  9022. + without producing any compressed output. This function must be called
  9023. + immediately after deflateInit, deflateInit2 or deflateReset, before any
  9024. + call of deflate. The compressor and decompressor must use exactly the same
  9025. + dictionary (see inflateSetDictionary).
  9026. +
  9027. + The dictionary should consist of strings (byte sequences) that are likely
  9028. + to be encountered later in the data to be compressed, with the most commonly
  9029. + used strings preferably put towards the end of the dictionary. Using a
  9030. + dictionary is most useful when the data to be compressed is short and can be
  9031. + predicted with good accuracy; the data can then be compressed better than
  9032. + with the default empty dictionary.
  9033. +
  9034. + Depending on the size of the compression data structures selected by
  9035. + deflateInit or deflateInit2, a part of the dictionary may in effect be
  9036. + discarded, for example if the dictionary is larger than the window size in
  9037. + deflate or deflate2. Thus the strings most likely to be useful should be
  9038. + put at the end of the dictionary, not at the front.
  9039. +
  9040. + Upon return of this function, strm->adler is set to the Adler32 value
  9041. + of the dictionary; the decompressor may later use this value to determine
  9042. + which dictionary has been used by the compressor. (The Adler32 value
  9043. + applies to the whole dictionary even if only a subset of the dictionary is
  9044. + actually used by the compressor.)
  9045. +
  9046. + deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
  9047. + parameter is invalid (such as NULL dictionary) or the stream state is
  9048. + inconsistent (for example if deflate has already been called for this stream
  9049. + or if the compression method is bsort). deflateSetDictionary does not
  9050. + perform any compression: this will be done by deflate().
  9051. +*/
  9052. +
  9053. +ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest,
  9054. + z_streamp source));
  9055. +/*
  9056. + Sets the destination stream as a complete copy of the source stream.
  9057. +
  9058. + This function can be useful when several compression strategies will be
  9059. + tried, for example when there are several ways of pre-processing the input
  9060. + data with a filter. The streams that will be discarded should then be freed
  9061. + by calling deflateEnd. Note that deflateCopy duplicates the internal
  9062. + compression state which can be quite large, so this strategy is slow and
  9063. + can consume lots of memory.
  9064. +
  9065. + deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
  9066. + enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
  9067. + (such as zalloc being NULL). msg is left unchanged in both source and
  9068. + destination.
  9069. +*/
  9070. +
  9071. +ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm));
  9072. +/*
  9073. + This function is equivalent to deflateEnd followed by deflateInit,
  9074. + but does not free and reallocate all the internal compression state.
  9075. + The stream will keep the same compression level and any other attributes
  9076. + that may have been set by deflateInit2.
  9077. +
  9078. + deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
  9079. + stream state was inconsistent (such as zalloc or state being NULL).
  9080. +*/
  9081. +
  9082. +ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
  9083. + int level,
  9084. + int strategy));
  9085. +/*
  9086. + Dynamically update the compression level and compression strategy. The
  9087. + interpretation of level and strategy is as in deflateInit2. This can be
  9088. + used to switch between compression and straight copy of the input data, or
  9089. + to switch to a different kind of input data requiring a different
  9090. + strategy. If the compression level is changed, the input available so far
  9091. + is compressed with the old level (and may be flushed); the new level will
  9092. + take effect only at the next call of deflate().
  9093. +
  9094. + Before the call of deflateParams, the stream state must be set as for
  9095. + a call of deflate(), since the currently available input may have to
  9096. + be compressed and flushed. In particular, strm->avail_out must be non-zero.
  9097. +
  9098. + deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
  9099. + stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
  9100. + if strm->avail_out was zero.
  9101. +*/
  9102. +
  9103. +/*
  9104. +ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm,
  9105. + int windowBits));
  9106. +
  9107. + This is another version of inflateInit with an extra parameter. The
  9108. + fields next_in, avail_in, zalloc, zfree and opaque must be initialized
  9109. + before by the caller.
  9110. +
  9111. + The windowBits parameter is the base two logarithm of the maximum window
  9112. + size (the size of the history buffer). It should be in the range 8..15 for
  9113. + this version of the library. The default value is 15 if inflateInit is used
  9114. + instead. If a compressed stream with a larger window size is given as
  9115. + input, inflate() will return with the error code Z_DATA_ERROR instead of
  9116. + trying to allocate a larger window.
  9117. +
  9118. + inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
  9119. + memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative
  9120. + memLevel). msg is set to null if there is no error message. inflateInit2
  9121. + does not perform any decompression apart from reading the zlib header if
  9122. + present: this will be done by inflate(). (So next_in and avail_in may be
  9123. + modified, but next_out and avail_out are unchanged.)
  9124. +*/
  9125. +
  9126. +ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
  9127. + const Bytef *dictionary,
  9128. + uInt dictLength));
  9129. +/*
  9130. + Initializes the decompression dictionary from the given uncompressed byte
  9131. + sequence. This function must be called immediately after a call of inflate
  9132. + if this call returned Z_NEED_DICT. The dictionary chosen by the compressor
  9133. + can be determined from the Adler32 value returned by this call of
  9134. + inflate. The compressor and decompressor must use exactly the same
  9135. + dictionary (see deflateSetDictionary).
  9136. +
  9137. + inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
  9138. + parameter is invalid (such as NULL dictionary) or the stream state is
  9139. + inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
  9140. + expected one (incorrect Adler32 value). inflateSetDictionary does not
  9141. + perform any decompression: this will be done by subsequent calls of
  9142. + inflate().
  9143. +*/
  9144. +
  9145. +ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm));
  9146. +/*
  9147. + Skips invalid compressed data until a full flush point (see above the
  9148. + description of deflate with Z_FULL_FLUSH) can be found, or until all
  9149. + available input is skipped. No output is provided.
  9150. +
  9151. + inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
  9152. + if no more input was provided, Z_DATA_ERROR if no flush point has been found,
  9153. + or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
  9154. + case, the application may save the current current value of total_in which
  9155. + indicates where valid compressed data was found. In the error case, the
  9156. + application may repeatedly call inflateSync, providing more input each time,
  9157. + until success or end of the input data.
  9158. +*/
  9159. +
  9160. +ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm));
  9161. +/*
  9162. + This function is equivalent to inflateEnd followed by inflateInit,
  9163. + but does not free and reallocate all the internal decompression state.
  9164. + The stream will keep attributes that may have been set by inflateInit2.
  9165. +
  9166. + inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
  9167. + stream state was inconsistent (such as zalloc or state being NULL).
  9168. +*/
  9169. +
  9170. +
  9171. + /* utility functions */
  9172. +
  9173. +/*
  9174. + The following utility functions are implemented on top of the
  9175. + basic stream-oriented functions. To simplify the interface, some
  9176. + default options are assumed (compression level and memory usage,
  9177. + standard memory allocation functions). The source code of these
  9178. + utility functions can easily be modified if you need special options.
  9179. +*/
  9180. +
  9181. +ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
  9182. + const Bytef *source, uLong sourceLen));
  9183. +/*
  9184. + Compresses the source buffer into the destination buffer. sourceLen is
  9185. + the byte length of the source buffer. Upon entry, destLen is the total
  9186. + size of the destination buffer, which must be at least 0.1% larger than
  9187. + sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the
  9188. + compressed buffer.
  9189. + This function can be used to compress a whole file at once if the
  9190. + input file is mmap'ed.
  9191. + compress returns Z_OK if success, Z_MEM_ERROR if there was not
  9192. + enough memory, Z_BUF_ERROR if there was not enough room in the output
  9193. + buffer.
  9194. +*/
  9195. +
  9196. +ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen,
  9197. + const Bytef *source, uLong sourceLen,
  9198. + int level));
  9199. +/*
  9200. + Compresses the source buffer into the destination buffer. The level
  9201. + parameter has the same meaning as in deflateInit. sourceLen is the byte
  9202. + length of the source buffer. Upon entry, destLen is the total size of the
  9203. + destination buffer, which must be at least 0.1% larger than sourceLen plus
  9204. + 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
  9205. +
  9206. + compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
  9207. + memory, Z_BUF_ERROR if there was not enough room in the output buffer,
  9208. + Z_STREAM_ERROR if the level parameter is invalid.
  9209. +*/
  9210. +
  9211. +ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
  9212. + const Bytef *source, uLong sourceLen));
  9213. +/*
  9214. + Decompresses the source buffer into the destination buffer. sourceLen is
  9215. + the byte length of the source buffer. Upon entry, destLen is the total
  9216. + size of the destination buffer, which must be large enough to hold the
  9217. + entire uncompressed data. (The size of the uncompressed data must have
  9218. + been saved previously by the compressor and transmitted to the decompressor
  9219. + by some mechanism outside the scope of this compression library.)
  9220. + Upon exit, destLen is the actual size of the compressed buffer.
  9221. + This function can be used to decompress a whole file at once if the
  9222. + input file is mmap'ed.
  9223. +
  9224. + uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
  9225. + enough memory, Z_BUF_ERROR if there was not enough room in the output
  9226. + buffer, or Z_DATA_ERROR if the input data was corrupted.
  9227. +*/
  9228. +
  9229. +
  9230. +typedef voidp gzFile;
  9231. +
  9232. +ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode));
  9233. +/*
  9234. + Opens a gzip (.gz) file for reading or writing. The mode parameter
  9235. + is as in fopen ("rb" or "wb") but can also include a compression level
  9236. + ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
  9237. + Huffman only compression as in "wb1h". (See the description
  9238. + of deflateInit2 for more information about the strategy parameter.)
  9239. +
  9240. + gzopen can be used to read a file which is not in gzip format; in this
  9241. + case gzread will directly read from the file without decompression.
  9242. +
  9243. + gzopen returns NULL if the file could not be opened or if there was
  9244. + insufficient memory to allocate the (de)compression state; errno
  9245. + can be checked to distinguish the two cases (if errno is zero, the
  9246. + zlib error is Z_MEM_ERROR). */
  9247. +
  9248. +ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode));
  9249. +/*
  9250. + gzdopen() associates a gzFile with the file descriptor fd. File
  9251. + descriptors are obtained from calls like open, dup, creat, pipe or
  9252. + fileno (in the file has been previously opened with fopen).
  9253. + The mode parameter is as in gzopen.
  9254. + The next call of gzclose on the returned gzFile will also close the
  9255. + file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
  9256. + descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
  9257. + gzdopen returns NULL if there was insufficient memory to allocate
  9258. + the (de)compression state.
  9259. +*/
  9260. +
  9261. +ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
  9262. +/*
  9263. + Dynamically update the compression level or strategy. See the description
  9264. + of deflateInit2 for the meaning of these parameters.
  9265. + gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
  9266. + opened for writing.
  9267. +*/
  9268. +
  9269. +ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len));
  9270. +/*
  9271. + Reads the given number of uncompressed bytes from the compressed file.
  9272. + If the input file was not in gzip format, gzread copies the given number
  9273. + of bytes into the buffer.
  9274. + gzread returns the number of uncompressed bytes actually read (0 for
  9275. + end of file, -1 for error). */
  9276. +
  9277. +ZEXTERN int ZEXPORT gzwrite OF((gzFile file,
  9278. + const voidp buf, unsigned len));
  9279. +/*
  9280. + Writes the given number of uncompressed bytes into the compressed file.
  9281. + gzwrite returns the number of uncompressed bytes actually written
  9282. + (0 in case of error).
  9283. +*/
  9284. +
  9285. +ZEXTERN int ZEXPORTVA gzprintf OF((gzFile file, const char *format, ...));
  9286. +/*
  9287. + Converts, formats, and writes the args to the compressed file under
  9288. + control of the format string, as in fprintf. gzprintf returns the number of
  9289. + uncompressed bytes actually written (0 in case of error).
  9290. +*/
  9291. +
  9292. +ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));
  9293. +/*
  9294. + Writes the given null-terminated string to the compressed file, excluding
  9295. + the terminating null character.
  9296. + gzputs returns the number of characters written, or -1 in case of error.
  9297. +*/
  9298. +
  9299. +ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len));
  9300. +/*
  9301. + Reads bytes from the compressed file until len-1 characters are read, or
  9302. + a newline character is read and transferred to buf, or an end-of-file
  9303. + condition is encountered. The string is then terminated with a null
  9304. + character.
  9305. + gzgets returns buf, or Z_NULL in case of error.
  9306. +*/
  9307. +
  9308. +ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c));
  9309. +/*
  9310. + Writes c, converted to an unsigned char, into the compressed file.
  9311. + gzputc returns the value that was written, or -1 in case of error.
  9312. +*/
  9313. +
  9314. +ZEXTERN int ZEXPORT gzgetc OF((gzFile file));
  9315. +/*
  9316. + Reads one byte from the compressed file. gzgetc returns this byte
  9317. + or -1 in case of end of file or error.
  9318. +*/
  9319. +
  9320. +ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush));
  9321. +/*
  9322. + Flushes all pending output into the compressed file. The parameter
  9323. + flush is as in the deflate() function. The return value is the zlib
  9324. + error number (see function gzerror below). gzflush returns Z_OK if
  9325. + the flush parameter is Z_FINISH and all output could be flushed.
  9326. + gzflush should be called only when strictly necessary because it can
  9327. + degrade compression.
  9328. +*/
  9329. +
  9330. +ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file,
  9331. + z_off_t offset, int whence));
  9332. +/*
  9333. + Sets the starting position for the next gzread or gzwrite on the
  9334. + given compressed file. The offset represents a number of bytes in the
  9335. + uncompressed data stream. The whence parameter is defined as in lseek(2);
  9336. + the value SEEK_END is not supported.
  9337. + If the file is opened for reading, this function is emulated but can be
  9338. + extremely slow. If the file is opened for writing, only forward seeks are
  9339. + supported; gzseek then compresses a sequence of zeroes up to the new
  9340. + starting position.
  9341. +
  9342. + gzseek returns the resulting offset location as measured in bytes from
  9343. + the beginning of the uncompressed stream, or -1 in case of error, in
  9344. + particular if the file is opened for writing and the new starting position
  9345. + would be before the current position.
  9346. +*/
  9347. +
  9348. +ZEXTERN int ZEXPORT gzrewind OF((gzFile file));
  9349. +/*
  9350. + Rewinds the given file. This function is supported only for reading.
  9351. +
  9352. + gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
  9353. +*/
  9354. +
  9355. +ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file));
  9356. +/*
  9357. + Returns the starting position for the next gzread or gzwrite on the
  9358. + given compressed file. This position represents a number of bytes in the
  9359. + uncompressed data stream.
  9360. +
  9361. + gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
  9362. +*/
  9363. +
  9364. +ZEXTERN int ZEXPORT gzeof OF((gzFile file));
  9365. +/*
  9366. + Returns 1 when EOF has previously been detected reading the given
  9367. + input stream, otherwise zero.
  9368. +*/
  9369. +
  9370. +ZEXTERN int ZEXPORT gzclose OF((gzFile file));
  9371. +/*
  9372. + Flushes all pending output if necessary, closes the compressed file
  9373. + and deallocates all the (de)compression state. The return value is the zlib
  9374. + error number (see function gzerror below).
  9375. +*/
  9376. +
  9377. +ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum));
  9378. +/*
  9379. + Returns the error message for the last error which occurred on the
  9380. + given compressed file. errnum is set to zlib error number. If an
  9381. + error occurred in the file system and not in the compression library,
  9382. + errnum is set to Z_ERRNO and the application may consult errno
  9383. + to get the exact error code.
  9384. +*/
  9385. +
  9386. + /* checksum functions */
  9387. +
  9388. +/*
  9389. + These functions are not related to compression but are exported
  9390. + anyway because they might be useful in applications using the
  9391. + compression library.
  9392. +*/
  9393. +
  9394. +ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
  9395. +
  9396. +/*
  9397. + Update a running Adler-32 checksum with the bytes buf[0..len-1] and
  9398. + return the updated checksum. If buf is NULL, this function returns
  9399. + the required initial value for the checksum.
  9400. + An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
  9401. + much faster. Usage example:
  9402. +
  9403. + uLong adler = adler32(0L, Z_NULL, 0);
  9404. +
  9405. + while (read_buffer(buffer, length) != EOF) {
  9406. + adler = adler32(adler, buffer, length);
  9407. + }
  9408. + if (adler != original_adler) error();
  9409. +*/
  9410. +
  9411. +ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len));
  9412. +/*
  9413. + Update a running crc with the bytes buf[0..len-1] and return the updated
  9414. + crc. If buf is NULL, this function returns the required initial value
  9415. + for the crc. Pre- and post-conditioning (one's complement) is performed
  9416. + within this function so it shouldn't be done by the application.
  9417. + Usage example:
  9418. +
  9419. + uLong crc = crc32(0L, Z_NULL, 0);
  9420. +
  9421. + while (read_buffer(buffer, length) != EOF) {
  9422. + crc = crc32(crc, buffer, length);
  9423. + }
  9424. + if (crc != original_crc) error();
  9425. +*/
  9426. +
  9427. +
  9428. + /* various hacks, don't look :) */
  9429. +
  9430. +/* deflateInit and inflateInit are macros to allow checking the zlib version
  9431. + * and the compiler's view of z_stream:
  9432. + */
  9433. +ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level,
  9434. + const char *version, int stream_size));
  9435. +ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm,
  9436. + const char *version, int stream_size));
  9437. +ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method,
  9438. + int windowBits, int memLevel,
  9439. + int strategy, const char *version,
  9440. + int stream_size));
  9441. +ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits,
  9442. + const char *version, int stream_size));
  9443. +#define deflateInit(strm, level) \
  9444. + deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream))
  9445. +#define inflateInit(strm) \
  9446. + inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream))
  9447. +#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
  9448. + deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
  9449. + (strategy), ZLIB_VERSION, sizeof(z_stream))
  9450. +#define inflateInit2(strm, windowBits) \
  9451. + inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
  9452. +
  9453. +
  9454. +#if !defined(_Z_UTIL_H) && !defined(NO_DUMMY_DECL)
  9455. + struct internal_state {int dummy;}; /* hack for buggy compilers */
  9456. +#endif
  9457. +
  9458. +ZEXTERN const char * ZEXPORT zError OF((int err));
  9459. +ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp z));
  9460. +ZEXTERN const uLongf * ZEXPORT get_crc_table OF((void));
  9461. +
  9462. +#ifdef __cplusplus
  9463. +}
  9464. +#endif
  9465. +
  9466. +#endif /* _ZLIB_H */
  9467. --- /dev/null Tue Mar 11 13:02:56 2003
  9468. +++ linux/include/zlib/zutil.h Mon Feb 9 13:51:03 2004
  9469. @@ -0,0 +1,225 @@
  9470. +/* zutil.h -- internal interface and configuration of the compression library
  9471. + * Copyright (C) 1995-2002 Jean-loup Gailly.
  9472. + * For conditions of distribution and use, see copyright notice in zlib.h
  9473. + */
  9474. +
  9475. +/* WARNING: this file should *not* be used by applications. It is
  9476. + part of the implementation of the compression library and is
  9477. + subject to change. Applications should only use zlib.h.
  9478. + */
  9479. +
  9480. +/* @(#) $Id: zutil.h,v 1.4 2002/04/24 07:36:48 mcr Exp $ */
  9481. +
  9482. +#ifndef _Z_UTIL_H
  9483. +#define _Z_UTIL_H
  9484. +
  9485. +#include "zlib.h"
  9486. +
  9487. +#include <linux/string.h>
  9488. +#define HAVE_MEMCPY
  9489. +
  9490. +#if 0 // #ifdef STDC
  9491. +# include <stddef.h>
  9492. +# include <string.h>
  9493. +# include <stdlib.h>
  9494. +#endif
  9495. +#ifndef __KERNEL__
  9496. +#ifdef NO_ERRNO_H
  9497. + extern int errno;
  9498. +#else
  9499. +# include <errno.h>
  9500. +#endif
  9501. +#endif
  9502. +
  9503. +#ifndef local
  9504. +# define local static
  9505. +#endif
  9506. +/* compile with -Dlocal if your debugger can't find static symbols */
  9507. +
  9508. +typedef unsigned char uch;
  9509. +typedef uch FAR uchf;
  9510. +typedef unsigned short ush;
  9511. +typedef ush FAR ushf;
  9512. +typedef unsigned long ulg;
  9513. +
  9514. +extern const char *z_errmsg[10]; /* indexed by 2-zlib_error */
  9515. +/* (size given to avoid silly warnings with Visual C++) */
  9516. +
  9517. +#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
  9518. +
  9519. +#define ERR_RETURN(strm,err) \
  9520. + return (strm->msg = ERR_MSG(err), (err))
  9521. +/* To be used only when the state is known to be valid */
  9522. +
  9523. + /* common constants */
  9524. +
  9525. +#ifndef DEF_WBITS
  9526. +# define DEF_WBITS MAX_WBITS
  9527. +#endif
  9528. +/* default windowBits for decompression. MAX_WBITS is for compression only */
  9529. +
  9530. +#if MAX_MEM_LEVEL >= 8
  9531. +# define DEF_MEM_LEVEL 8
  9532. +#else
  9533. +# define DEF_MEM_LEVEL MAX_MEM_LEVEL
  9534. +#endif
  9535. +/* default memLevel */
  9536. +
  9537. +#define STORED_BLOCK 0
  9538. +#define STATIC_TREES 1
  9539. +#define DYN_TREES 2
  9540. +/* The three kinds of block type */
  9541. +
  9542. +#define MIN_MATCH 3
  9543. +#define MAX_MATCH 258
  9544. +/* The minimum and maximum match lengths */
  9545. +
  9546. +#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
  9547. +
  9548. + /* target dependencies */
  9549. +
  9550. +#ifdef MSDOS
  9551. +# define OS_CODE 0x00
  9552. +# if defined(__TURBOC__) || defined(__BORLANDC__)
  9553. +# if(__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__))
  9554. + /* Allow compilation with ANSI keywords only enabled */
  9555. + void _Cdecl farfree( void *block );
  9556. + void *_Cdecl farmalloc( unsigned long nbytes );
  9557. +# else
  9558. +# include <alloc.h>
  9559. +# endif
  9560. +# else /* MSC or DJGPP */
  9561. +# include <malloc.h>
  9562. +# endif
  9563. +#endif
  9564. +
  9565. +#ifdef OS2
  9566. +# define OS_CODE 0x06
  9567. +#endif
  9568. +
  9569. +#ifdef WIN32 /* Window 95 & Windows NT */
  9570. +# define OS_CODE 0x0b
  9571. +#endif
  9572. +
  9573. +#if defined(VAXC) || defined(VMS)
  9574. +# define OS_CODE 0x02
  9575. +# define F_OPEN(name, mode) \
  9576. + fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
  9577. +#endif
  9578. +
  9579. +#ifdef AMIGA
  9580. +# define OS_CODE 0x01
  9581. +#endif
  9582. +
  9583. +#if defined(ATARI) || defined(atarist)
  9584. +# define OS_CODE 0x05
  9585. +#endif
  9586. +
  9587. +#if defined(MACOS) || defined(TARGET_OS_MAC)
  9588. +# define OS_CODE 0x07
  9589. +# if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os
  9590. +# include <unix.h> /* for fdopen */
  9591. +# else
  9592. +# ifndef fdopen
  9593. +# define fdopen(fd,mode) NULL /* No fdopen() */
  9594. +# endif
  9595. +# endif
  9596. +#endif
  9597. +
  9598. +#ifdef __50SERIES /* Prime/PRIMOS */
  9599. +# define OS_CODE 0x0F
  9600. +#endif
  9601. +
  9602. +#ifdef TOPS20
  9603. +# define OS_CODE 0x0a
  9604. +#endif
  9605. +
  9606. +#if defined(_BEOS_) || defined(RISCOS)
  9607. +# define fdopen(fd,mode) NULL /* No fdopen() */
  9608. +#endif
  9609. +
  9610. +#if (defined(_MSC_VER) && (_MSC_VER > 600))
  9611. +# define fdopen(fd,type) _fdopen(fd,type)
  9612. +#endif
  9613. +
  9614. +
  9615. + /* Common defaults */
  9616. +
  9617. +#ifndef OS_CODE
  9618. +# define OS_CODE 0x03 /* assume Unix */
  9619. +#endif
  9620. +
  9621. +#ifndef F_OPEN
  9622. +# define F_OPEN(name, mode) fopen((name), (mode))
  9623. +#endif
  9624. +
  9625. + /* functions */
  9626. +
  9627. +#ifdef HAVE_STRERROR
  9628. + extern char *strerror OF((int));
  9629. +# define zstrerror(errnum) strerror(errnum)
  9630. +#else
  9631. +# define zstrerror(errnum) ""
  9632. +#endif
  9633. +
  9634. +#if defined(pyr)
  9635. +# define NO_MEMCPY
  9636. +#endif
  9637. +#if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__)
  9638. + /* Use our own functions for small and medium model with MSC <= 5.0.
  9639. + * You may have to use the same strategy for Borland C (untested).
  9640. + * The __SC__ check is for Symantec.
  9641. + */
  9642. +# define NO_MEMCPY
  9643. +#endif
  9644. +#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
  9645. +# define HAVE_MEMCPY
  9646. +#endif
  9647. +#ifdef HAVE_MEMCPY
  9648. +# ifdef SMALL_MEDIUM /* MSDOS small or medium model */
  9649. +# define zmemcpy _fmemcpy
  9650. +# define zmemcmp _fmemcmp
  9651. +# define zmemzero(dest, len) _fmemset(dest, 0, len)
  9652. +# else
  9653. +# define zmemcpy memcpy
  9654. +# define zmemcmp memcmp
  9655. +# define zmemzero(dest, len) memset(dest, 0, len)
  9656. +# endif
  9657. +#else
  9658. + extern void zmemcpy OF((Bytef* dest, const Bytef* source, uInt len));
  9659. + extern int zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len));
  9660. + extern void zmemzero OF((Bytef* dest, uInt len));
  9661. +#endif
  9662. +
  9663. +/* Diagnostic functions */
  9664. +#ifdef DEBUG
  9665. +# include <stdio.h>
  9666. + extern int z_verbose;
  9667. + extern void z_error OF((char *m));
  9668. +# define Assert(cond,msg) {if(!(cond)) z_error(msg);}
  9669. +# define Trace(x) {if (z_verbose>=0) fprintf x ;}
  9670. +# define Tracev(x) {if (z_verbose>0) fprintf x ;}
  9671. +# define Tracevv(x) {if (z_verbose>1) fprintf x ;}
  9672. +# define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
  9673. +# define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
  9674. +#else
  9675. +# define Assert(cond,msg)
  9676. +# define Trace(x)
  9677. +# define Tracev(x)
  9678. +# define Tracevv(x)
  9679. +# define Tracec(c,x)
  9680. +# define Tracecv(c,x)
  9681. +#endif
  9682. +
  9683. +
  9684. +typedef uLong (ZEXPORT *check_func) OF((uLong check, const Bytef *buf,
  9685. + uInt len));
  9686. +voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size));
  9687. +void zcfree OF((voidpf opaque, voidpf ptr));
  9688. +
  9689. +#define ZALLOC(strm, items, size) \
  9690. + (*((strm)->zalloc))((strm)->opaque, (items), (size))
  9691. +#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
  9692. +#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
  9693. +
  9694. +#endif /* _Z_UTIL_H */
  9695. --- /dev/null Tue Mar 11 13:02:56 2003
  9696. +++ linux/lib/libfreeswan/Makefile.objs Mon Feb 9 13:51:03 2004
  9697. @@ -0,0 +1,21 @@
  9698. +obj-y += satot.o
  9699. +obj-y += addrtot.o
  9700. +obj-y += ultot.o
  9701. +obj-y += addrtypeof.o
  9702. +obj-y += anyaddr.o
  9703. +obj-y += initaddr.o
  9704. +obj-y += ultoa.o
  9705. +obj-y += addrtoa.o
  9706. +obj-y += subnettoa.o
  9707. +obj-y += subnetof.o
  9708. +obj-y += goodmask.o
  9709. +obj-y += datatot.o
  9710. +obj-y += rangetoa.o
  9711. +obj-y += prng.o
  9712. +obj-y += pfkey_v2_parse.o
  9713. +obj-y += pfkey_v2_build.o
  9714. +obj-y += pfkey_v2_debug.o
  9715. +obj-y += pfkey_v2_ext_bits.o
  9716. +
  9717. +#version.c: ${LIBFREESWANDIR}/version.in.c ${OPENSWANSRCDIR}/Makefile.ver
  9718. +# sed '/"/s/xxx/$(IPSECVERSION)/' ${LIBFREESWANDIR}/version.in.c >$@
  9719. --- /dev/null Tue Mar 11 13:02:56 2003
  9720. +++ linux/lib/zlib/Makefile Mon Feb 9 13:51:03 2004
  9721. @@ -0,0 +1,118 @@
  9722. +# (kernel) Makefile for IPCOMP zlib deflate code
  9723. +# Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  9724. +# Copyright (C) 2000 Svenning Soerensen
  9725. +#
  9726. +# This program is free software; you can redistribute it and/or modify it
  9727. +# under the terms of the GNU General Public License as published by the
  9728. +# Free Software Foundation; either version 2 of the License, or (at your
  9729. +# option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9730. +#
  9731. +# This program is distributed in the hope that it will be useful, but
  9732. +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  9733. +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  9734. +# for more details.
  9735. +#
  9736. +# RCSID $Id: Makefile,v 1.9 2002/04/24 07:55:32 mcr Exp $
  9737. +#
  9738. +
  9739. +
  9740. +
  9741. +include ../Makefile.inc
  9742. +
  9743. +
  9744. +
  9745. +ifndef TOPDIR
  9746. +TOPDIR := /usr/src/linux
  9747. +endif
  9748. +
  9749. +
  9750. +L_TARGET := zlib.a
  9751. +
  9752. +obj-y :=
  9753. +
  9754. +include Makefile.objs
  9755. +
  9756. +EXTRA_CFLAGS += $(KLIPSCOMPILE)
  9757. +
  9758. +EXTRA_CFLAGS += -Wall
  9759. +#EXTRA_CFLAGS += -Wconversion
  9760. +#EXTRA_CFLAGS += -Wmissing-prototypes
  9761. +EXTRA_CFLAGS += -Wpointer-arith
  9762. +#EXTRA_CFLAGS += -Wcast-qual
  9763. +#EXTRA_CFLAGS += -Wmissing-declarations
  9764. +EXTRA_CFLAGS += -Wstrict-prototypes
  9765. +#EXTRA_CFLAGS += -pedantic
  9766. +#EXTRA_CFLAGS += -W
  9767. +#EXTRA_CFLAGS += -Wwrite-strings
  9768. +EXTRA_CFLAGS += -Wbad-function-cast
  9769. +EXTRA_CFLAGS += -DIPCOMP_PREFIX
  9770. +
  9771. +.S.o:
  9772. + $(CC) -D__ASSEMBLY__ -DNO_UNDERLINE -traditional -c $< -o $*.o
  9773. +
  9774. +asm-obj-$(CONFIG_M586) += match586.o
  9775. +asm-obj-$(CONFIG_M586TSC) += match586.o
  9776. +asm-obj-$(CONFIG_M586MMX) += match586.o
  9777. +asm-obj-$(CONFIG_M686) += match686.o
  9778. +asm-obj-$(CONFIG_MPENTIUMIII) += match686.o
  9779. +asm-obj-$(CONFIG_MPENTIUM4) += match686.o
  9780. +asm-obj-$(CONFIG_MK6) += match586.o
  9781. +asm-obj-$(CONFIG_MK7) += match686.o
  9782. +asm-obj-$(CONFIG_MCRUSOE) += match586.o
  9783. +asm-obj-$(CONFIG_MWINCHIPC6) += match586.o
  9784. +asm-obj-$(CONFIG_MWINCHIP2) += match686.o
  9785. +asm-obj-$(CONFIG_MWINCHIP3D) += match686.o
  9786. +
  9787. +obj-y += $(asm-obj-y)
  9788. +ifneq ($(strip $(asm-obj-y)),)
  9789. + EXTRA_CFLAGS += -DASMV
  9790. +endif
  9791. +
  9792. +active-objs := $(sort $(obj-y) $(obj-m))
  9793. +L_OBJS := $(obj-y)
  9794. +M_OBJS := $(obj-m)
  9795. +MIX_OBJS := $(filter $(export-objs), $(active-objs))
  9796. +
  9797. +include $(TOPDIR)/Rules.make
  9798. +
  9799. +$(obj-y) : $(TOPDIR)/include/linux/config.h $(TOPDIR)/include/linux/autoconf.h
  9800. +
  9801. +
  9802. +clean:
  9803. + -rm -f *.o *.a
  9804. +
  9805. +checkprograms:
  9806. +programs: $(L_TARGET)
  9807. +
  9808. +#
  9809. +# $Log: Makefile,v $
  9810. +# Revision 1.9 2002/04/24 07:55:32 mcr
  9811. +# #include patches and Makefiles for post-reorg compilation.
  9812. +#
  9813. +# Revision 1.8 2002/04/24 07:36:44 mcr
  9814. +# Moved from ./zlib/Makefile,v
  9815. +#
  9816. +# Revision 1.7 2002/03/27 23:34:35 mcr
  9817. +# added programs: target
  9818. +#
  9819. +# Revision 1.6 2001/12/05 20:19:08 henry
  9820. +# use new compile-control variable
  9821. +#
  9822. +# Revision 1.5 2001/11/27 16:38:08 mcr
  9823. +# added new "checkprograms" target to deal with programs that
  9824. +# are required for "make check", but that may not be ready to
  9825. +# build for every user due to external dependancies.
  9826. +#
  9827. +# Revision 1.4 2001/10/24 14:46:24 henry
  9828. +# Makefile.inc
  9829. +#
  9830. +# Revision 1.3 2001/04/21 23:05:24 rgb
  9831. +# Update asm directives for 2.4 style makefiles.
  9832. +#
  9833. +# Revision 1.2 2001/01/29 22:22:00 rgb
  9834. +# Convert to 2.4 new style with back compat.
  9835. +#
  9836. +# Revision 1.1.1.1 2000/09/29 18:51:33 rgb
  9837. +# zlib_beginnings
  9838. +#
  9839. +#
  9840. --- /dev/null Tue Mar 11 13:02:56 2003
  9841. +++ linux/lib/zlib/Makefile.objs Mon Feb 9 13:51:03 2004
  9842. @@ -0,0 +1,27 @@
  9843. +obj-$(CONFIG_IPSEC_IPCOMP) += adler32.o
  9844. +obj-$(CONFIG_IPSEC_IPCOMP) += deflate.o
  9845. +obj-$(CONFIG_IPSEC_IPCOMP) += infblock.o
  9846. +obj-$(CONFIG_IPSEC_IPCOMP) += infcodes.o
  9847. +obj-$(CONFIG_IPSEC_IPCOMP) += inffast.o
  9848. +obj-$(CONFIG_IPSEC_IPCOMP) += inflate.o
  9849. +obj-$(CONFIG_IPSEC_IPCOMP) += inftrees.o
  9850. +obj-$(CONFIG_IPSEC_IPCOMP) += infutil.o
  9851. +obj-$(CONFIG_IPSEC_IPCOMP) += trees.o
  9852. +obj-$(CONFIG_IPSEC_IPCOMP) += zutil.o
  9853. +
  9854. +asm-obj-$(CONFIG_M586) += ${LIBZLIBSRCDIR}/match586.o
  9855. +asm-obj-$(CONFIG_M586TSC) += ${LIBZLIBSRCDIR}/match586.o
  9856. +asm-obj-$(CONFIG_M586MMX) += ${LIBZLIBSRCDIR}/match586.o
  9857. +asm-obj-$(CONFIG_M686) += ${LIBZLIBSRCDIR}/match686.o
  9858. +asm-obj-$(CONFIG_MPENTIUMIII) += ${LIBZLIBSRCDIR}/match686.o
  9859. +asm-obj-$(CONFIG_MPENTIUM4) += ${LIBZLIBSRCDIR}/match686.o
  9860. +asm-obj-$(CONFIG_MK6) += ${LIBZLIBSRCDIR}/match586.o
  9861. +asm-obj-$(CONFIG_MK7) += ${LIBZLIBSRCDIR}/match686.o
  9862. +asm-obj-$(CONFIG_MCRUSOE) += ${LIBZLIBSRCDIR}/match586.o
  9863. +asm-obj-$(CONFIG_MWINCHIPC6) += ${LIBZLIBSRCDIR}/match586.o
  9864. +asm-obj-$(CONFIG_MWINCHIP2) += ${LIBZLIBSRCDIR}/match686.o
  9865. +asm-obj-$(CONFIG_MWINCHIP3D) += ${LIBZLIBSRCDIR}/match686.o
  9866. +
  9867. +EXTRA_CFLAGS += -DIPCOMP_PREFIX
  9868. +
  9869. +
  9870. --- swan26/net/Kconfig.preipsec 2005-09-01 18:15:19.000000000 -0400
  9871. +++ swan26/net/Kconfig 2005-09-03 16:51:17.000000000 -0400
  9872. @@ -215,2 +215,6 @@
  9873. +if INET
  9874. +source "net/ipsec/Kconfig"
  9875. +endif # if INET
  9876. +
  9877. endif # if NET
  9878. --- /distros/kernel/linux-2.6.3-rc4/net/Makefile Mon Feb 16 21:22:12 2004
  9879. +++ ref26/net/Makefile Thu Feb 19 21:02:25 2004
  9880. @@ -42,3 +42,6 @@
  9881. ifeq ($(CONFIG_NET),y)
  9882. obj-$(CONFIG_SYSCTL) += sysctl_net.o
  9883. endif
  9884. +
  9885. +obj-$(CONFIG_KLIPS) += ipsec/
  9886. +
  9887. --- /dev/null Tue Mar 11 13:02:56 2003
  9888. +++ linux/net/ipsec/Kconfig Mon Feb 9 13:51:03 2004
  9889. @@ -0,0 +1,161 @@
  9890. +#
  9891. +# IPSEC configuration
  9892. +# Copyright (C) 2004 Michael Richardson <mcr@freeswan.org>
  9893. +#
  9894. +# This program is free software; you can redistribute it and/or modify it
  9895. +# under the terms of the GNU General Public License as published by the
  9896. +# Free Software Foundation; either version 2 of the License, or (at your
  9897. +# option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9898. +#
  9899. +# This program is distributed in the hope that it will be useful, but
  9900. +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  9901. +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  9902. +# for more details.
  9903. +#
  9904. +# RCSID $Id: Kconfig,v 1.6.2.2 2006/10/11 18:14:33 paul Exp $
  9905. +
  9906. +config KLIPS
  9907. + tristate "Openswan IPsec (KLIPS26)"
  9908. + default n
  9909. + help
  9910. + KLIPS is the Openswan (www.openswan.org) Kernel Level IP Security
  9911. + system. It is extensively tested, and has interoperated with
  9912. + many other systems.
  9913. + It provides "ipsecX" devices on which one can do firewalling.
  9914. + The userland, is compatible with both KLIPS and 26sec.
  9915. +
  9916. +menu "KLIPS options"
  9917. + depends on KLIPS
  9918. +
  9919. +config KLIPS_ESP
  9920. + bool 'Encapsulating Security Payload - ESP ("VPN")'
  9921. + default y
  9922. + help
  9923. + This option provides support for the IPSEC Encapsulation Security
  9924. + Payload (IP protocol 50) which provides packet layer content
  9925. + hiding, and content authentication.
  9926. + It is recommended to enable this. RFC2406
  9927. +
  9928. +config KLIPS_AH
  9929. + bool 'Authentication Header - AH'
  9930. + default n
  9931. + help
  9932. + This option provides support for the IPSEC Authentication Header
  9933. + (IP protocol 51) which provides packet layer sender and content
  9934. + authentication. It does not provide for confidentiality.
  9935. + It is not recommended to enable this. RFC2402
  9936. +
  9937. +config KLIPS_AUTH_HMAC_MD5
  9938. + bool 'HMAC-MD5 authentication algorithm'
  9939. + default y
  9940. + help
  9941. + The HMAC-MD5 algorithm is used by ESP (and AH) to guarantee packet
  9942. + integrity. There is little reason not to include it.
  9943. +
  9944. +config KLIPS_AUTH_HMAC_SHA1
  9945. + bool 'HMAC-SHA1 authentication algorithm'
  9946. + default y
  9947. + help
  9948. + The HMAC-SHA1 algorithm is used by ESP (and AH) to guarantee packet
  9949. + integrity. SHA1 is a little slower than MD5, but is said to be
  9950. + a bit more secure. There is little reason not to include it.
  9951. +
  9952. +config KLIPS_ENC_CRYPTOAPI
  9953. + bool 'CryptoAPI algorithm interface'
  9954. + default n
  9955. + help
  9956. + Enable the algorithm interface to make all CryptoAPI 1.0 algorithms
  9957. + available to KLIPS.
  9958. +
  9959. +config KLIPS_ENC_1DES
  9960. + bool 'Include 1DES with CryptoAPI'
  9961. + default n
  9962. + depends on KLIPS_ENC_CRYPTOAPI
  9963. + help
  9964. + The CryptoAPI interface does not include support for every algorithm
  9965. + yet, and one that it doesn't support by default is the VERY WEAK
  9966. + 1DES. Select this if you are terminally stupid.
  9967. +
  9968. +config KLIPS_ENC_3DES
  9969. + bool '3DES encryption algorithm'
  9970. + default y
  9971. + help
  9972. + The 3DES algorithm is used by ESP to provide for packet privacy.
  9973. + 3DES is 3-repeats of the DES algorithm. 3DES is widely supported,
  9974. + and analyzed and is considered very secure. 1DES is not supported.
  9975. +
  9976. +config KLIPS_ENC_AES
  9977. + bool 'AES encryption algorithm'
  9978. + default y
  9979. + help
  9980. + The AES algorithm is used by ESP to provide for packet privacy.
  9981. + AES the NIST replacement for DES. AES is being widely analyzed,
  9982. + and is very fast.
  9983. +
  9984. +config KLIPS_ENC_NULL
  9985. + bool 'NULL NON-encryption algorithm'
  9986. + default n
  9987. + help
  9988. + NON encryption algo , maybe useful for ESP auth only scenarios
  9989. + (eg: with NAT-T), see RFC 2410.
  9990. +
  9991. +config KLIPS_IPCOMP
  9992. + bool 'IP compression'
  9993. + default y
  9994. + help
  9995. + The IPcomp protocol is used prior to ESP to make the packet
  9996. + smaller. Once encrypted, compression will fail, so any link
  9997. + layer efforts (e.g. PPP) will not work.
  9998. +
  9999. +config KLIPS_DEBUG
  10000. + bool 'IPsec debugging'
  10001. + default y
  10002. + help
  10003. + KLIPS includes a lot of debugging code. Unless there is a real
  10004. + tangible benefit to removing this code, it should be left in place.
  10005. + Debugging connections without access to kernel level debugging is
  10006. + essentially impossible. Leave this on.
  10007. +
  10008. +endmenu
  10009. +
  10010. +#
  10011. +#
  10012. +# $Log: Kconfig,v $
  10013. +# Revision 1.6.2.2 2006/10/11 18:14:33 paul
  10014. +# Add JuanJo Ciarlante's ESP_NULL patches for KLIPS, but leave it disabled
  10015. +# per default.
  10016. +#
  10017. +# Revision 1.6.2.1 2006/04/20 16:33:06 mcr
  10018. +# remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  10019. +# Fix in-kernel module compilation. Sub-makefiles do not work.
  10020. +#
  10021. +# Revision 1.6 2005/05/18 20:55:27 mcr
  10022. +# default cryptoapi to n.
  10023. +#
  10024. +# Revision 1.5 2005/05/11 01:23:25 mcr
  10025. +# added 1DES option to cryptoapi.
  10026. +#
  10027. +# Revision 1.4 2005/04/29 05:29:54 mcr
  10028. +# add option to include cryptoapi algorithms.
  10029. +#
  10030. +# Revision 1.3 2004/08/17 03:27:23 mcr
  10031. +# klips 2.6 edits.
  10032. +#
  10033. +# Revision 1.2 2004/08/14 03:27:39 mcr
  10034. +# 2.6 kernel build/configuration files.
  10035. +#
  10036. +# Revision 1.1 2004/08/14 02:47:55 mcr
  10037. +# kernel build/config patches
  10038. +#
  10039. +# Revision 1.3 2004/02/24 17:17:04 mcr
  10040. +# s/CONFIG_IPSEC/CONFIG_KLIPS/ as 26sec uses "CONFIG_IPSEC" to
  10041. +# turn it on/off as well.
  10042. +#
  10043. +# Revision 1.2 2004/02/22 06:50:42 mcr
  10044. +# kernel 2.6 port - merged with 2.4 code.
  10045. +#
  10046. +# Revision 1.1.2.1 2004/02/20 02:07:53 mcr
  10047. +# module configuration for KLIPS 2.6
  10048. +#
  10049. +#
  10050. +
  10051. --- /dev/null Tue Mar 11 13:02:56 2003
  10052. +++ linux/net/ipsec/Makefile Mon Feb 9 13:51:03 2004
  10053. @@ -0,0 +1,195 @@
  10054. +# Makefile for KLIPS kernel code as a module for 2.6 kernels
  10055. +#
  10056. +# Makefile for KLIPS kernel code as a module
  10057. +# Copyright (C) 1998, 1999, 2000,2001 Richard Guy Briggs.
  10058. +# Copyright (C) 2002-2004 Michael Richardson <mcr@freeswan.org>
  10059. +#
  10060. +# This program is free software; you can redistribute it and/or modify it
  10061. +# under the terms of the GNU General Public License as published by the
  10062. +# Free Software Foundation; either version 2 of the License, or (at your
  10063. +# option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  10064. +#
  10065. +# This program is distributed in the hope that it will be useful, but
  10066. +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10067. +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  10068. +# for more details.
  10069. +#
  10070. +# RCSID $Id: Makefile.fs2_6,v 1.8.2.2 2006/10/11 18:14:33 paul Exp $
  10071. +#
  10072. +# Note! Dependencies are done automagically by 'make dep', which also
  10073. +# removes any old dependencies. DON'T put your own dependencies here
  10074. +# unless it's something special (ie not a .c file).
  10075. +#
  10076. +
  10077. +OPENSWANSRCDIR?=.
  10078. +KLIPS_TOP?=.
  10079. +
  10080. +-include ${OPENSWANSRCDIR}/Makefile.ver
  10081. +
  10082. +base-klips-objs :=
  10083. +
  10084. +base-klips-objs+= ipsec_init.o ipsec_sa.o ipsec_radij.o radij.o
  10085. +base-klips-objs+= ipsec_life.o ipsec_proc.o
  10086. +base-klips-objs+= ipsec_tunnel.o ipsec_xmit.o ipsec_rcv.o ipsec_ipip.o
  10087. +base-klips-objs+= ipsec_snprintf.o
  10088. +base-klips-objs+= sysctl_net_ipsec.o
  10089. +base-klips-objs+= pfkey_v2.o pfkey_v2_parser.o pfkey_v2_ext_process.o
  10090. +base-klips-objs+= version.o
  10091. +
  10092. +base-klips-objs+= satot.o
  10093. +base-klips-objs+= addrtot.o
  10094. +base-klips-objs+= ultot.o
  10095. +base-klips-objs+= addrtypeof.o
  10096. +base-klips-objs+= anyaddr.o
  10097. +base-klips-objs+= initaddr.o
  10098. +base-klips-objs+= ultoa.o
  10099. +base-klips-objs+= addrtoa.o
  10100. +base-klips-objs+= subnettoa.o
  10101. +base-klips-objs+= subnetof.o
  10102. +base-klips-objs+= goodmask.o
  10103. +base-klips-objs+= datatot.o
  10104. +base-klips-objs+= rangetoa.o
  10105. +base-klips-objs+= prng.o
  10106. +base-klips-objs+= pfkey_v2_parse.o
  10107. +base-klips-objs+= pfkey_v2_build.o
  10108. +base-klips-objs+= pfkey_v2_debug.o
  10109. +base-klips-objs+= pfkey_v2_ext_bits.o
  10110. +base-klips-objs+= version.o
  10111. +
  10112. +obj-${CONFIG_KLIPS} += ipsec.o
  10113. +
  10114. +ipsec-objs += ${base-klips-objs}
  10115. +
  10116. +ipsec-$(CONFIG_KLIPS_ESP) += ipsec_esp.o
  10117. +ipsec-$(CONFIG_KLIPS_IPCOMP) += ipsec_ipcomp.o
  10118. +ipsec-$(CONFIG_KLIPS_AUTH_HMAC_MD5) += ipsec_md5c.o
  10119. +ipsec-$(CONFIG_KLIPS_AUTH_HMAC_SHA1) += ipsec_sha1.o
  10120. +
  10121. +# AH, if you really think you need it.
  10122. +ipsec-$(CONFIG_KLIPS_AH) += ipsec_ah.o
  10123. +
  10124. +ipsec-y += ipsec_alg.o
  10125. +
  10126. +# include code from DES subdir
  10127. +crypto-$(CONFIG_KLIPS_ENC_3DES) += des/ipsec_alg_3des.o
  10128. +crypto-$(CONFIG_KLIPS_ENC_3DES) += des/cbc_enc.o
  10129. +crypto-$(CONFIG_KLIPS_ENC_3DES) += des/ecb_enc.o
  10130. +crypto-$(CONFIG_KLIPS_ENC_3DES) += des/set_key.o
  10131. +
  10132. +ifeq ($(strip ${SUBARCH}),)
  10133. +SUBARCH:=${ARCH}
  10134. +endif
  10135. +
  10136. +# the assembly version expects frame pointers, which are
  10137. +# optional in many kernel builds. If you want speed, you should
  10138. +# probably use cryptoapi code instead.
  10139. +USEASSEMBLY=${SUBARCH}${CONFIG_FRAME_POINTER}
  10140. +ifeq (${USEASSEMBLY},i386y)
  10141. +crypto-$(CONFIG_KLIPS_ENC_3DES) += des/dx86unix.o
  10142. +else
  10143. +crypto-$(CONFIG_KLIPS_ENC_3DES) += des/des_enc.o
  10144. +endif
  10145. +
  10146. +# include code from AES subdir
  10147. +crypto-$(CONFIG_KLIPS_ENC_AES) += aes/ipsec_alg_aes.o
  10148. +crypto-$(CONFIG_KLIPS_ENC_AES) += aes/aes_xcbc_mac.o
  10149. +crypto-$(CONFIG_KLIPS_ENC_AES) += aes/aes_cbc.o
  10150. +
  10151. +ifeq ($(strip ${SUBARCH}),)
  10152. +SUBARCH:=${ARCH}
  10153. +endif
  10154. +
  10155. +USEASSEMBLY=${SUBARCH}${CONFIG_FRAME_POINTER}
  10156. +ifeq (${USEASSEMBLY},i386y)
  10157. +crypto-$(CONFIG_KLIPS_ENC_AES) += aes/aes-i586.o
  10158. +else
  10159. +crypto-$(CONFIG_KLIPS_ENC_AES) += aes/aes.o
  10160. +endif
  10161. +
  10162. +crypto-$(CONFIG_KLIPS_ENC_NULL) += null/ipsec_alg_null.o
  10163. +
  10164. +ipsec-y += ${crypto-y}
  10165. +
  10166. +ipsec-$(CONFIG_KLIPS_ENC_CRYPTOAPI) += ipsec_alg_cryptoapi.o
  10167. +
  10168. +# IPcomp stuff
  10169. +base-ipcomp-objs := ipcomp.o
  10170. +base-ipcomp-objs += adler32.o
  10171. +base-ipcomp-objs += deflate.o
  10172. +base-ipcomp-objs += infblock.o
  10173. +base-ipcomp-objs += infcodes.o
  10174. +base-ipcomp-objs += inffast.o
  10175. +base-ipcomp-objs += inflate.o
  10176. +base-ipcomp-objs += inftrees.o
  10177. +base-ipcomp-objs += infutil.o
  10178. +base-ipcomp-objs += trees.o
  10179. +base-ipcomp-objs += zutil.o
  10180. +asm-ipcomp-obj-$(CONFIG_M586) += match586.o
  10181. +asm-ipcomp-obj-$(CONFIG_M586TSC) += match586.o
  10182. +asm-ipcomp-obj-$(CONFIG_M586MMX) += match586.o
  10183. +asm-ipcomp-obj-$(CONFIG_M686) += match686.o
  10184. +asm-ipcomp-obj-$(CONFIG_MPENTIUMIII) += match686.o
  10185. +asm-ipcomp-obj-$(CONFIG_MPENTIUM4) += match686.o
  10186. +asm-ipcomp-obj-$(CONFIG_MK6) += match586.o
  10187. +asm-ipcomp-obj-$(CONFIG_MK7) += match686.o
  10188. +asm-ipcomp-obj-$(CONFIG_MCRUSOE) += match586.o
  10189. +asm-ipcomp-obj-$(CONFIG_MWINCHIPC6) += match586.o
  10190. +asm-ipcomp-obj-$(CONFIG_MWINCHIP2) += match686.o
  10191. +asm-ipcomp-obj-$(CONFIG_MWINCHIP3D) += match686.o
  10192. +base-ipcomp-objs += ${asm-ipcomp-obj-y}
  10193. +
  10194. +ipsec-$(CONFIG_KLIPS_IPCOMP) += ${base-ipcomp-objs}
  10195. +
  10196. +EXTRA_CFLAGS += -DIPCOMP_PREFIX
  10197. +
  10198. +#
  10199. +# $Log: Makefile.fs2_6,v $
  10200. +# Revision 1.8.2.2 2006/10/11 18:14:33 paul
  10201. +# Add JuanJo Ciarlante's ESP_NULL patches for KLIPS, but leave it disabled
  10202. +# per default.
  10203. +#
  10204. +# Revision 1.8.2.1 2006/04/20 16:33:06 mcr
  10205. +# remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  10206. +# Fix in-kernel module compilation. Sub-makefiles do not work.
  10207. +#
  10208. +# Revision 1.8 2005/05/11 03:15:42 mcr
  10209. +# adjusted makefiles to sanely build modules properly.
  10210. +#
  10211. +# Revision 1.7 2005/04/13 22:52:12 mcr
  10212. +# moved KLIPS specific snprintf() wrapper to seperate file.
  10213. +#
  10214. +# Revision 1.6 2004/08/22 05:02:03 mcr
  10215. +# organized symbols such that it is easier to build modules.
  10216. +#
  10217. +# Revision 1.5 2004/08/18 01:43:56 mcr
  10218. +# adjusted makefile enumation so that it can be used by module
  10219. +# wrapper.
  10220. +#
  10221. +# Revision 1.4 2004/08/17 03:27:23 mcr
  10222. +# klips 2.6 edits.
  10223. +#
  10224. +# Revision 1.3 2004/08/04 16:50:13 mcr
  10225. +# removed duplicate definition of dx86unix.o
  10226. +#
  10227. +# Revision 1.2 2004/08/03 18:21:09 mcr
  10228. +# only set KLIPS_TOP and OPENSWANSRCDIR if not already set.
  10229. +#
  10230. +# Revision 1.1 2004/07/26 15:02:22 mcr
  10231. +# makefile for KLIPS module for 2.6.
  10232. +#
  10233. +# Revision 1.3 2004/02/24 17:17:04 mcr
  10234. +# s/CONFIG_IPSEC/CONFIG_KLIPS/ as 26sec uses "CONFIG_IPSEC" to
  10235. +# turn it on/off as well.
  10236. +#
  10237. +# Revision 1.2 2004/02/22 06:50:42 mcr
  10238. +# kernel 2.6 port - merged with 2.4 code.
  10239. +#
  10240. +# Revision 1.1.2.1 2004/02/20 02:07:53 mcr
  10241. +# module configuration for KLIPS 2.6
  10242. +#
  10243. +#
  10244. +# Local Variables:
  10245. +# compile-command: "(cd ../../.. && source umlsetup.sh && make -C ${POOLSPACE} module/ipsec.o)"
  10246. +# End Variables:
  10247. +#
  10248. +
  10249. --- /dev/null Tue Mar 11 13:02:56 2003
  10250. +++ linux/net/ipsec/README-zlib Mon Feb 9 13:51:03 2004
  10251. @@ -0,0 +1,147 @@
  10252. +zlib 1.1.4 is a general purpose data compression library. All the code
  10253. +is thread safe. The data format used by the zlib library
  10254. +is described by RFCs (Request for Comments) 1950 to 1952 in the files
  10255. +http://www.ietf.org/rfc/rfc1950.txt (zlib format), rfc1951.txt (deflate
  10256. +format) and rfc1952.txt (gzip format). These documents are also available in
  10257. +other formats from ftp://ftp.uu.net/graphics/png/documents/zlib/zdoc-index.html
  10258. +
  10259. +All functions of the compression library are documented in the file zlib.h
  10260. +(volunteer to write man pages welcome, contact jloup@gzip.org). A usage
  10261. +example of the library is given in the file example.c which also tests that
  10262. +the library is working correctly. Another example is given in the file
  10263. +minigzip.c. The compression library itself is composed of all source files
  10264. +except example.c and minigzip.c.
  10265. +
  10266. +To compile all files and run the test program, follow the instructions
  10267. +given at the top of Makefile. In short "make test; make install"
  10268. +should work for most machines. For Unix: "./configure; make test; make install"
  10269. +For MSDOS, use one of the special makefiles such as Makefile.msc.
  10270. +For VMS, use Make_vms.com or descrip.mms.
  10271. +
  10272. +Questions about zlib should be sent to <zlib@gzip.org>, or to
  10273. +Gilles Vollant <info@winimage.com> for the Windows DLL version.
  10274. +The zlib home page is http://www.zlib.org or http://www.gzip.org/zlib/
  10275. +Before reporting a problem, please check this site to verify that
  10276. +you have the latest version of zlib; otherwise get the latest version and
  10277. +check whether the problem still exists or not.
  10278. +
  10279. +PLEASE read the zlib FAQ http://www.gzip.org/zlib/zlib_faq.html
  10280. +before asking for help.
  10281. +
  10282. +Mark Nelson <markn@ieee.org> wrote an article about zlib for the Jan. 1997
  10283. +issue of Dr. Dobb's Journal; a copy of the article is available in
  10284. +http://dogma.net/markn/articles/zlibtool/zlibtool.htm
  10285. +
  10286. +The changes made in version 1.1.4 are documented in the file ChangeLog.
  10287. +The only changes made since 1.1.3 are bug corrections:
  10288. +
  10289. +- ZFREE was repeated on same allocation on some error conditions.
  10290. + This creates a security problem described in
  10291. + http://www.zlib.org/advisory-2002-03-11.txt
  10292. +- Returned incorrect error (Z_MEM_ERROR) on some invalid data
  10293. +- Avoid accesses before window for invalid distances with inflate window
  10294. + less than 32K.
  10295. +- force windowBits > 8 to avoid a bug in the encoder for a window size
  10296. + of 256 bytes. (A complete fix will be available in 1.1.5).
  10297. +
  10298. +The beta version 1.1.5beta includes many more changes. A new official
  10299. +version 1.1.5 will be released as soon as extensive testing has been
  10300. +completed on it.
  10301. +
  10302. +
  10303. +Unsupported third party contributions are provided in directory "contrib".
  10304. +
  10305. +A Java implementation of zlib is available in the Java Development Kit
  10306. +http://www.javasoft.com/products/JDK/1.1/docs/api/Package-java.util.zip.html
  10307. +See the zlib home page http://www.zlib.org for details.
  10308. +
  10309. +A Perl interface to zlib written by Paul Marquess <pmarquess@bfsec.bt.co.uk>
  10310. +is in the CPAN (Comprehensive Perl Archive Network) sites
  10311. +http://www.cpan.org/modules/by-module/Compress/
  10312. +
  10313. +A Python interface to zlib written by A.M. Kuchling <amk@magnet.com>
  10314. +is available in Python 1.5 and later versions, see
  10315. +http://www.python.org/doc/lib/module-zlib.html
  10316. +
  10317. +A zlib binding for TCL written by Andreas Kupries <a.kupries@westend.com>
  10318. +is availlable at http://www.westend.com/~kupries/doc/trf/man/man.html
  10319. +
  10320. +An experimental package to read and write files in .zip format,
  10321. +written on top of zlib by Gilles Vollant <info@winimage.com>, is
  10322. +available at http://www.winimage.com/zLibDll/unzip.html
  10323. +and also in the contrib/minizip directory of zlib.
  10324. +
  10325. +
  10326. +Notes for some targets:
  10327. +
  10328. +- To build a Windows DLL version, include in a DLL project zlib.def, zlib.rc
  10329. + and all .c files except example.c and minigzip.c; compile with -DZLIB_DLL
  10330. + The zlib DLL support was initially done by Alessandro Iacopetti and is
  10331. + now maintained by Gilles Vollant <info@winimage.com>. Check the zlib DLL
  10332. + home page at http://www.winimage.com/zLibDll
  10333. +
  10334. + From Visual Basic, you can call the DLL functions which do not take
  10335. + a structure as argument: compress, uncompress and all gz* functions.
  10336. + See contrib/visual-basic.txt for more information, or get
  10337. + http://www.tcfb.com/dowseware/cmp-z-it.zip
  10338. +
  10339. +- For 64-bit Irix, deflate.c must be compiled without any optimization.
  10340. + With -O, one libpng test fails. The test works in 32 bit mode (with
  10341. + the -n32 compiler flag). The compiler bug has been reported to SGI.
  10342. +
  10343. +- zlib doesn't work with gcc 2.6.3 on a DEC 3000/300LX under OSF/1 2.1
  10344. + it works when compiled with cc.
  10345. +
  10346. +- on Digital Unix 4.0D (formely OSF/1) on AlphaServer, the cc option -std1
  10347. + is necessary to get gzprintf working correctly. This is done by configure.
  10348. +
  10349. +- zlib doesn't work on HP-UX 9.05 with some versions of /bin/cc. It works
  10350. + with other compilers. Use "make test" to check your compiler.
  10351. +
  10352. +- gzdopen is not supported on RISCOS, BEOS and by some Mac compilers.
  10353. +
  10354. +- For Turbo C the small model is supported only with reduced performance to
  10355. + avoid any far allocation; it was tested with -DMAX_WBITS=11 -DMAX_MEM_LEVEL=3
  10356. +
  10357. +- For PalmOs, see http://www.cs.uit.no/~perm/PASTA/pilot/software.html
  10358. + Per Harald Myrvang <perm@stud.cs.uit.no>
  10359. +
  10360. +
  10361. +Acknowledgments:
  10362. +
  10363. + The deflate format used by zlib was defined by Phil Katz. The deflate
  10364. + and zlib specifications were written by L. Peter Deutsch. Thanks to all the
  10365. + people who reported problems and suggested various improvements in zlib;
  10366. + they are too numerous to cite here.
  10367. +
  10368. +Copyright notice:
  10369. +
  10370. + (C) 1995-2002 Jean-loup Gailly and Mark Adler
  10371. +
  10372. + This software is provided 'as-is', without any express or implied
  10373. + warranty. In no event will the authors be held liable for any damages
  10374. + arising from the use of this software.
  10375. +
  10376. + Permission is granted to anyone to use this software for any purpose,
  10377. + including commercial applications, and to alter it and redistribute it
  10378. + freely, subject to the following restrictions:
  10379. +
  10380. + 1. The origin of this software must not be misrepresented; you must not
  10381. + claim that you wrote the original software. If you use this software
  10382. + in a product, an acknowledgment in the product documentation would be
  10383. + appreciated but is not required.
  10384. + 2. Altered source versions must be plainly marked as such, and must not be
  10385. + misrepresented as being the original software.
  10386. + 3. This notice may not be removed or altered from any source distribution.
  10387. +
  10388. + Jean-loup Gailly Mark Adler
  10389. + jloup@gzip.org madler@alumni.caltech.edu
  10390. +
  10391. +If you use the zlib library in a product, we would appreciate *not*
  10392. +receiving lengthy legal documents to sign. The sources are provided
  10393. +for free but without warranty of any kind. The library has been
  10394. +entirely written by Jean-loup Gailly and Mark Adler; it does not
  10395. +include third-party code.
  10396. +
  10397. +If you redistribute modified sources, we would appreciate that you include
  10398. +in the file ChangeLog history information documenting your changes.
  10399. --- /dev/null Tue Mar 11 13:02:56 2003
  10400. +++ linux/net/ipsec/README-zlib.freeswan Mon Feb 9 13:51:03 2004
  10401. @@ -0,0 +1,13 @@
  10402. +The only changes made to these files for use in FreeS/WAN are:
  10403. +
  10404. + - In zconf.h, macros are defined to prefix global symbols with "ipcomp_"
  10405. + (or "_ipcomp"), when compiled with -DIPCOMP_PREFIX.
  10406. + - The copyright strings are defined local (static)
  10407. +
  10408. + The above changes are made to avoid name collisions with ppp_deflate
  10409. + and ext2compr.
  10410. +
  10411. + - Files not needed for FreeS/WAN have been removed
  10412. +
  10413. + See the "README" file for information about where to obtain the complete
  10414. + zlib package.
  10415. --- /dev/null Tue Mar 11 13:02:56 2003
  10416. +++ linux/net/ipsec/addrtoa.c Mon Feb 9 13:51:03 2004
  10417. @@ -0,0 +1,67 @@
  10418. +/*
  10419. + * addresses to ASCII
  10420. + * Copyright (C) 1998, 1999 Henry Spencer.
  10421. + *
  10422. + * This library is free software; you can redistribute it and/or modify it
  10423. + * under the terms of the GNU Library General Public License as published by
  10424. + * the Free Software Foundation; either version 2 of the License, or (at your
  10425. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  10426. + *
  10427. + * This library is distributed in the hope that it will be useful, but
  10428. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10429. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  10430. + * License for more details.
  10431. + *
  10432. + * RCSID $Id: addrtoa.c,v 1.10 2004/07/10 07:43:47 mcr Exp $
  10433. + */
  10434. +#include "openswan.h"
  10435. +
  10436. +#define NBYTES 4 /* bytes in an address */
  10437. +#define PERBYTE 4 /* three digits plus a dot or NUL */
  10438. +#define BUFLEN (NBYTES*PERBYTE)
  10439. +
  10440. +#if BUFLEN != ADDRTOA_BUF
  10441. +#error "ADDRTOA_BUF in openswan.h inconsistent with addrtoa() code"
  10442. +#endif
  10443. +
  10444. +/*
  10445. + - addrtoa - convert binary address to ASCII dotted decimal
  10446. + */
  10447. +size_t /* space needed for full conversion */
  10448. +addrtoa(addr, format, dst, dstlen)
  10449. +struct in_addr addr;
  10450. +int format; /* character */
  10451. +char *dst; /* need not be valid if dstlen is 0 */
  10452. +size_t dstlen;
  10453. +{
  10454. + unsigned long a = ntohl(addr.s_addr);
  10455. + int i;
  10456. + size_t n;
  10457. + unsigned long byte;
  10458. + char buf[BUFLEN];
  10459. + char *p;
  10460. +
  10461. + switch (format) {
  10462. + case 0:
  10463. + break;
  10464. + default:
  10465. + return 0;
  10466. + break;
  10467. + }
  10468. +
  10469. + p = buf;
  10470. + for (i = NBYTES-1; i >= 0; i--) {
  10471. + byte = (a >> (i*8)) & 0xff;
  10472. + p += ultoa(byte, 10, p, PERBYTE);
  10473. + if (i != 0)
  10474. + *(p-1) = '.';
  10475. + }
  10476. + n = p - buf;
  10477. +
  10478. + if (dstlen > 0) {
  10479. + if (n > dstlen)
  10480. + buf[dstlen - 1] = '\0';
  10481. + strcpy(dst, buf);
  10482. + }
  10483. + return n;
  10484. +}
  10485. --- /dev/null Tue Mar 11 13:02:56 2003
  10486. +++ linux/net/ipsec/addrtot.c Mon Feb 9 13:51:03 2004
  10487. @@ -0,0 +1,423 @@
  10488. +/*
  10489. + * addresses to text
  10490. + * Copyright (C) 2000 Henry Spencer.
  10491. + *
  10492. + * This library is free software; you can redistribute it and/or modify it
  10493. + * under the terms of the GNU Library General Public License as published by
  10494. + * the Free Software Foundation; either version 2 of the License, or (at your
  10495. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  10496. + *
  10497. + * This library is distributed in the hope that it will be useful, but
  10498. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10499. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  10500. + * License for more details.
  10501. + *
  10502. + * RCSID $Id: addrtot.c,v 1.22.2.1 2005/11/17 22:30:49 paul Exp $
  10503. + */
  10504. +
  10505. +#if defined(__KERNEL__) && defined(__HAVE_ARCH_STRSTR)
  10506. +#include <linux/string.h>
  10507. +#endif
  10508. +
  10509. +#include "openswan.h"
  10510. +
  10511. +#define IP4BYTES 4 /* bytes in an IPv4 address */
  10512. +#define PERBYTE 4 /* three digits plus a dot or NUL */
  10513. +#define IP6BYTES 16 /* bytes in an IPv6 address */
  10514. +
  10515. +/* forwards */
  10516. +static size_t normal4(const unsigned char *s, size_t len, char *b, char **dp);
  10517. +static size_t normal6(const unsigned char *s, size_t len, char *b, char **dp, int squish);
  10518. +static size_t reverse4(const unsigned char *s, size_t len, char *b, char **dp);
  10519. +static size_t reverse6(const unsigned char *s, size_t len, char *b, char **dp);
  10520. +
  10521. +#if defined(__KERNEL__) && !defined(__HAVE_ARCH_STRSTR)
  10522. +#define strstr ipsec_strstr
  10523. +/*
  10524. + * Find the first occurrence of find in s.
  10525. + * (from NetBSD 1.6's /src/lib/libc/string/strstr.c)
  10526. + */
  10527. +static char *
  10528. +strstr(s, find)
  10529. + const char *s, *find;
  10530. +{
  10531. + char c, sc;
  10532. + size_t len;
  10533. +
  10534. + if ((c = *find++) != 0) {
  10535. + len = strlen(find);
  10536. + do {
  10537. + do {
  10538. + if ((sc = *s++) == 0)
  10539. + return (NULL);
  10540. + } while (sc != c);
  10541. + } while (strncmp(s, find, len) != 0);
  10542. + s--;
  10543. + }
  10544. + /* LINTED interface specification */
  10545. + return ((char *)s);
  10546. +}
  10547. +#endif
  10548. +
  10549. +/*
  10550. + - addrtot - convert binary address to text (dotted decimal or IPv6 string)
  10551. + */
  10552. +size_t /* space needed for full conversion */
  10553. +addrtot(src, format, dst, dstlen)
  10554. +const ip_address *src;
  10555. +int format; /* character */
  10556. +char *dst; /* need not be valid if dstlen is 0 */
  10557. +size_t dstlen;
  10558. +{
  10559. + const unsigned char *b;
  10560. + size_t n;
  10561. + char buf[1+ADDRTOT_BUF+1]; /* :address: */
  10562. + char *p;
  10563. + int t = addrtypeof(src);
  10564. +# define TF(t, f) (((t)<<8) | (f))
  10565. +
  10566. + n = addrbytesptr(src, &b);
  10567. + if (n == 0) {
  10568. + bad:
  10569. + dst[0]='\0';
  10570. + strncat(dst, "<invalid>", dstlen);
  10571. + return sizeof("<invalid>");
  10572. + }
  10573. +
  10574. + switch (TF(t, format)) {
  10575. + case TF(AF_INET, 0):
  10576. + n = normal4(b, n, buf, &p);
  10577. + break;
  10578. + case TF(AF_INET6, 0):
  10579. + n = normal6(b, n, buf, &p, 1);
  10580. + break;
  10581. + case TF(AF_INET, 'Q'):
  10582. + n = normal4(b, n, buf, &p);
  10583. + break;
  10584. + case TF(AF_INET6, 'Q'):
  10585. + n = normal6(b, n, buf, &p, 0);
  10586. + break;
  10587. + case TF(AF_INET, 'r'):
  10588. + n = reverse4(b, n, buf, &p);
  10589. + break;
  10590. + case TF(AF_INET6, 'r'):
  10591. + n = reverse6(b, n, buf, &p);
  10592. + break;
  10593. + default: /* including (AF_INET, 'R') */
  10594. + goto bad;
  10595. + break;
  10596. + }
  10597. +
  10598. + if (dstlen > 0) {
  10599. + if (dstlen < n)
  10600. + p[dstlen - 1] = '\0';
  10601. + strcpy(dst, p);
  10602. + }
  10603. + return n;
  10604. +}
  10605. +
  10606. +/*
  10607. + - normal4 - normal IPv4 address-text conversion
  10608. + */
  10609. +static size_t /* size of text, including NUL */
  10610. +normal4(srcp, srclen, buf, dstp)
  10611. +const unsigned char *srcp;
  10612. +size_t srclen;
  10613. +char *buf; /* guaranteed large enough */
  10614. +char **dstp; /* where to put result pointer */
  10615. +{
  10616. + int i;
  10617. + char *p;
  10618. +
  10619. + if (srclen != IP4BYTES) /* "can't happen" */
  10620. + return 0;
  10621. + p = buf;
  10622. + for (i = 0; i < IP4BYTES; i++) {
  10623. + p += ultot(srcp[i], 10, p, PERBYTE);
  10624. + if (i != IP4BYTES - 1)
  10625. + *(p-1) = '.'; /* overwrites the NUL */
  10626. + }
  10627. + *dstp = buf;
  10628. + return p - buf;
  10629. +}
  10630. +
  10631. +/*
  10632. + - normal6 - normal IPv6 address-text conversion
  10633. + */
  10634. +static size_t /* size of text, including NUL */
  10635. +normal6(srcp, srclen, buf, dstp, squish)
  10636. +const unsigned char *srcp;
  10637. +size_t srclen;
  10638. +char *buf; /* guaranteed large enough, plus 2 */
  10639. +char **dstp; /* where to put result pointer */
  10640. +int squish; /* whether to squish out 0:0 */
  10641. +{
  10642. + int i;
  10643. + unsigned long piece;
  10644. + char *p;
  10645. + char *q;
  10646. +
  10647. + if (srclen != IP6BYTES) /* "can't happen" */
  10648. + return 0;
  10649. + p = buf;
  10650. + *p++ = ':';
  10651. + for (i = 0; i < IP6BYTES/2; i++) {
  10652. + piece = (srcp[2*i] << 8) + srcp[2*i + 1];
  10653. + p += ultot(piece, 16, p, 5); /* 5 = abcd + NUL */
  10654. + *(p-1) = ':'; /* overwrites the NUL */
  10655. + }
  10656. + *p = '\0';
  10657. + q = strstr(buf, ":0:0:");
  10658. + if (squish && q != NULL) { /* zero squishing is possible */
  10659. + p = q + 1;
  10660. + while (*p == '0' && *(p+1) == ':')
  10661. + p += 2;
  10662. + q++;
  10663. + *q++ = ':'; /* overwrite first 0 */
  10664. + while (*p != '\0')
  10665. + *q++ = *p++;
  10666. + *q = '\0';
  10667. + if (!(*(q-1) == ':' && *(q-2) == ':'))
  10668. + *--q = '\0'; /* strip final : unless :: */
  10669. + p = buf;
  10670. + if (!(*p == ':' && *(p+1) == ':'))
  10671. + p++; /* skip initial : unless :: */
  10672. + } else {
  10673. + q = p;
  10674. + *--q = '\0'; /* strip final : */
  10675. + p = buf + 1; /* skip initial : */
  10676. + }
  10677. + *dstp = p;
  10678. + return q - p + 1;
  10679. +}
  10680. +
  10681. +/*
  10682. + - reverse4 - IPv4 reverse-lookup conversion
  10683. + */
  10684. +static size_t /* size of text, including NUL */
  10685. +reverse4(srcp, srclen, buf, dstp)
  10686. +const unsigned char *srcp;
  10687. +size_t srclen;
  10688. +char *buf; /* guaranteed large enough */
  10689. +char **dstp; /* where to put result pointer */
  10690. +{
  10691. + int i;
  10692. + char *p;
  10693. +
  10694. + if (srclen != IP4BYTES) /* "can't happen" */
  10695. + return 0;
  10696. + p = buf;
  10697. + for (i = IP4BYTES-1; i >= 0; i--) {
  10698. + p += ultot(srcp[i], 10, p, PERBYTE);
  10699. + *(p-1) = '.'; /* overwrites the NUL */
  10700. + }
  10701. + strcpy(p, "IN-ADDR.ARPA.");
  10702. + *dstp = buf;
  10703. + return strlen(buf) + 1;
  10704. +}
  10705. +
  10706. +/*
  10707. + - reverse6 - IPv6 reverse-lookup conversion (RFC 1886)
  10708. + * A trifle inefficient, really shouldn't use ultot...
  10709. + */
  10710. +static size_t /* size of text, including NUL */
  10711. +reverse6(srcp, srclen, buf, dstp)
  10712. +const unsigned char *srcp;
  10713. +size_t srclen;
  10714. +char *buf; /* guaranteed large enough */
  10715. +char **dstp; /* where to put result pointer */
  10716. +{
  10717. + int i;
  10718. + unsigned long piece;
  10719. + char *p;
  10720. +
  10721. + if (srclen != IP6BYTES) /* "can't happen" */
  10722. + return 0;
  10723. + p = buf;
  10724. + for (i = IP6BYTES-1; i >= 0; i--) {
  10725. + piece = srcp[i];
  10726. + p += ultot(piece&0xf, 16, p, 2);
  10727. + *(p-1) = '.';
  10728. + p += ultot(piece>>4, 16, p, 2);
  10729. + *(p-1) = '.';
  10730. + }
  10731. + strcpy(p, "IP6.ARPA.");
  10732. + *dstp = buf;
  10733. + return strlen(buf) + 1;
  10734. +}
  10735. +
  10736. +/*
  10737. + - reverse6 - modern IPv6 reverse-lookup conversion (RFC 2874)
  10738. + * this version removed as it was obsoleted in the end.
  10739. + */
  10740. +
  10741. +#ifdef ADDRTOT_MAIN
  10742. +
  10743. +#include <stdio.h>
  10744. +#include <sys/socket.h>
  10745. +#include <netinet/in.h>
  10746. +#include <arpa/inet.h>
  10747. +
  10748. +void regress(void);
  10749. +
  10750. +int
  10751. +main(int argc, char *argv[])
  10752. +{
  10753. + if (argc < 2) {
  10754. + fprintf(stderr, "Usage: %s {addr|net/mask|begin...end|-r}\n",
  10755. + argv[0]);
  10756. + exit(2);
  10757. + }
  10758. +
  10759. + if (strcmp(argv[1], "-r") == 0) {
  10760. + regress();
  10761. + fprintf(stderr, "regress() returned?!?\n");
  10762. + exit(1);
  10763. + }
  10764. + exit(0);
  10765. +}
  10766. +
  10767. +struct rtab {
  10768. + char *input;
  10769. + char format;
  10770. + char *output; /* NULL means error expected */
  10771. +} rtab[] = {
  10772. + {"1.2.3.0", 0, "1.2.3.0"},
  10773. + {"1:2::3:4", 0, "1:2::3:4"},
  10774. + {"1:2::3:4", 'Q', "1:2:0:0:0:0:3:4"},
  10775. + {"1:2:0:0:3:4:0:0", 0, "1:2::3:4:0:0"},
  10776. + {"1.2.3.4", 'r' , "4.3.2.1.IN-ADDR.ARPA."},
  10777. + /* 0 1 2 3 4 5 6 7 8 9 a b c d e f 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  10778. + {"1:2::3:4", 'r', "4.0.0.0.3.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0.1.0.0.0.IP6.ARPA."},
  10779. + {NULL, 0, NULL}
  10780. +};
  10781. +
  10782. +void
  10783. +regress()
  10784. +{
  10785. + struct rtab *r;
  10786. + int status = 0;
  10787. + ip_address a;
  10788. + char in[100];
  10789. + char buf[100];
  10790. + const char *oops;
  10791. + size_t n;
  10792. +
  10793. + for (r = rtab; r->input != NULL; r++) {
  10794. + strcpy(in, r->input);
  10795. +
  10796. + /* convert it *to* internal format */
  10797. + oops = ttoaddr(in, strlen(in), 0, &a);
  10798. +
  10799. + /* now convert it back */
  10800. +
  10801. + n = addrtot(&a, r->format, buf, sizeof(buf));
  10802. +
  10803. + if (n == 0 && r->output == NULL)
  10804. + {} /* okay, error expected */
  10805. +
  10806. + else if (n == 0) {
  10807. + printf("`%s' atoasr failed\n", r->input);
  10808. + status = 1;
  10809. +
  10810. + } else if (r->output == NULL) {
  10811. + printf("`%s' atoasr succeeded unexpectedly '%c'\n",
  10812. + r->input, r->format);
  10813. + status = 1;
  10814. + } else {
  10815. + if (strcasecmp(r->output, buf) != 0) {
  10816. + printf("`%s' '%c' gave `%s', expected `%s'\n",
  10817. + r->input, r->format, buf, r->output);
  10818. + status = 1;
  10819. + }
  10820. + }
  10821. + }
  10822. + exit(status);
  10823. +}
  10824. +
  10825. +#endif /* ADDRTOT_MAIN */
  10826. +
  10827. +/*
  10828. + * $Log: addrtot.c,v $
  10829. + * Revision 1.22.2.1 2005/11/17 22:30:49 paul
  10830. + * pull up strstr fix from head.
  10831. + *
  10832. + * Revision 1.22 2005/05/20 16:47:40 mcr
  10833. + * make strstr static if we need it.
  10834. + *
  10835. + * Revision 1.21 2005/03/21 00:35:12 mcr
  10836. + * test for strstr properly
  10837. + *
  10838. + * Revision 1.20 2004/11/09 22:52:20 mcr
  10839. + * until we figure out which kernels have strsep and which
  10840. + * do not (UML does not under certain circumstances), then
  10841. + * let's just provide our own.
  10842. + *
  10843. + * Revision 1.19 2004/10/08 16:30:33 mcr
  10844. + * pull-up of initial crypto-offload work.
  10845. + *
  10846. + * Revision 1.18 2004/09/18 19:33:08 mcr
  10847. + * use an appropriate kernel happy ifdef for strstr.
  10848. + *
  10849. + * Revision 1.17 2004/09/15 21:49:02 mcr
  10850. + * use local copy of strstr() if this is going in the kernel.
  10851. + * Not clear why this worked before, or why this shows up
  10852. + * for modules only.
  10853. + *
  10854. + * Revision 1.16 2004/07/10 07:43:47 mcr
  10855. + * Moved from linux/lib/libfreeswan/addrtot.c,v
  10856. + *
  10857. + * Revision 1.15 2004/04/11 17:39:25 mcr
  10858. + * removed internal.h requirements.
  10859. + *
  10860. + * Revision 1.14 2004/03/08 01:59:08 ken
  10861. + * freeswan.h -> openswan.h
  10862. + *
  10863. + * Revision 1.13 2004/01/05 23:21:05 mcr
  10864. + * if the address type is invalid, then return length of <invalid>
  10865. + * string!
  10866. + *
  10867. + * Revision 1.12 2003/12/30 06:42:48 mcr
  10868. + * added $Log: addrtot.c,v $
  10869. + * added Revision 1.22.2.1 2005/11/17 22:30:49 paul
  10870. + * added pull up strstr fix from head.
  10871. + * added
  10872. + * added Revision 1.22 2005/05/20 16:47:40 mcr
  10873. + * added make strstr static if we need it.
  10874. + * added
  10875. + * added Revision 1.21 2005/03/21 00:35:12 mcr
  10876. + * added test for strstr properly
  10877. + * added
  10878. + * added Revision 1.20 2004/11/09 22:52:20 mcr
  10879. + * added until we figure out which kernels have strsep and which
  10880. + * added do not (UML does not under certain circumstances), then
  10881. + * added let's just provide our own.
  10882. + * added
  10883. + * added Revision 1.19 2004/10/08 16:30:33 mcr
  10884. + * added pull-up of initial crypto-offload work.
  10885. + * added
  10886. + * added Revision 1.18 2004/09/18 19:33:08 mcr
  10887. + * added use an appropriate kernel happy ifdef for strstr.
  10888. + * added
  10889. + * added Revision 1.17 2004/09/15 21:49:02 mcr
  10890. + * added use local copy of strstr() if this is going in the kernel.
  10891. + * added Not clear why this worked before, or why this shows up
  10892. + * added for modules only.
  10893. + * added
  10894. + * added Revision 1.16 2004/07/10 07:43:47 mcr
  10895. + * added Moved from linux/lib/libfreeswan/addrtot.c,v
  10896. + * added
  10897. + * added Revision 1.15 2004/04/11 17:39:25 mcr
  10898. + * added removed internal.h requirements.
  10899. + * added
  10900. + * added Revision 1.14 2004/03/08 01:59:08 ken
  10901. + * added freeswan.h -> openswan.h
  10902. + * added
  10903. + * added Revision 1.13 2004/01/05 23:21:05 mcr
  10904. + * added if the address type is invalid, then return length of <invalid>
  10905. + * added string!
  10906. + * added
  10907. + *
  10908. + *
  10909. + */
  10910. +
  10911. --- /dev/null Tue Mar 11 13:02:56 2003
  10912. +++ linux/net/ipsec/addrtypeof.c Mon Feb 9 13:51:03 2004
  10913. @@ -0,0 +1,93 @@
  10914. +/*
  10915. + * extract parts of an ip_address
  10916. + * Copyright (C) 2000 Henry Spencer.
  10917. + *
  10918. + * This library is free software; you can redistribute it and/or modify it
  10919. + * under the terms of the GNU Library General Public License as published by
  10920. + * the Free Software Foundation; either version 2 of the License, or (at your
  10921. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  10922. + *
  10923. + * This library is distributed in the hope that it will be useful, but
  10924. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10925. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  10926. + * License for more details.
  10927. + *
  10928. + * RCSID $Id: addrtypeof.c,v 1.10 2004/07/10 07:43:47 mcr Exp $
  10929. + */
  10930. +#include "openswan.h"
  10931. +
  10932. +/*
  10933. + - addrtypeof - get the type of an ip_address
  10934. + */
  10935. +int
  10936. +addrtypeof(src)
  10937. +const ip_address *src;
  10938. +{
  10939. + return src->u.v4.sin_family;
  10940. +}
  10941. +
  10942. +/*
  10943. + - addrbytesptr - get pointer to the address bytes of an ip_address
  10944. + */
  10945. +size_t /* 0 for error */
  10946. +addrbytesptr(src, dstp)
  10947. +const ip_address *src;
  10948. +const unsigned char **dstp; /* NULL means just a size query */
  10949. +{
  10950. + const unsigned char *p;
  10951. + size_t n;
  10952. +
  10953. + switch (src->u.v4.sin_family) {
  10954. + case AF_INET:
  10955. + p = (const unsigned char *)&src->u.v4.sin_addr.s_addr;
  10956. + n = 4;
  10957. + break;
  10958. + case AF_INET6:
  10959. + p = (const unsigned char *)&src->u.v6.sin6_addr;
  10960. + n = 16;
  10961. + break;
  10962. + default:
  10963. + return 0;
  10964. + break;
  10965. + }
  10966. +
  10967. + if (dstp != NULL)
  10968. + *dstp = p;
  10969. + return n;
  10970. +}
  10971. +
  10972. +/*
  10973. + - addrlenof - get length of the address bytes of an ip_address
  10974. + */
  10975. +size_t /* 0 for error */
  10976. +addrlenof(src)
  10977. +const ip_address *src;
  10978. +{
  10979. + return addrbytesptr(src, NULL);
  10980. +}
  10981. +
  10982. +/*
  10983. + - addrbytesof - get the address bytes of an ip_address
  10984. + */
  10985. +size_t /* 0 for error */
  10986. +addrbytesof(src, dst, dstlen)
  10987. +const ip_address *src;
  10988. +unsigned char *dst;
  10989. +size_t dstlen;
  10990. +{
  10991. + const unsigned char *p;
  10992. + size_t n;
  10993. + size_t ncopy;
  10994. +
  10995. + n = addrbytesptr(src, &p);
  10996. + if (n == 0)
  10997. + return 0;
  10998. +
  10999. + if (dstlen > 0) {
  11000. + ncopy = n;
  11001. + if (ncopy > dstlen)
  11002. + ncopy = dstlen;
  11003. + memcpy(dst, p, ncopy);
  11004. + }
  11005. + return n;
  11006. +}
  11007. --- /dev/null Tue Mar 11 13:02:56 2003
  11008. +++ linux/net/ipsec/adler32.c Mon Feb 9 13:51:03 2004
  11009. @@ -0,0 +1,49 @@
  11010. +/* adler32.c -- compute the Adler-32 checksum of a data stream
  11011. + * Copyright (C) 1995-2002 Mark Adler
  11012. + * For conditions of distribution and use, see copyright notice in zlib.h
  11013. + */
  11014. +
  11015. +/* @(#) $Id: adler32.c,v 1.6 2004/07/10 19:11:18 mcr Exp $ */
  11016. +
  11017. +#include <zlib/zlib.h>
  11018. +#include <zlib/zconf.h>
  11019. +
  11020. +#define BASE 65521L /* largest prime smaller than 65536 */
  11021. +#define NMAX 5552
  11022. +/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
  11023. +
  11024. +#define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
  11025. +#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
  11026. +#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
  11027. +#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
  11028. +#define DO16(buf) DO8(buf,0); DO8(buf,8);
  11029. +
  11030. +/* ========================================================================= */
  11031. +uLong ZEXPORT adler32(adler, buf, len)
  11032. + uLong adler;
  11033. + const Bytef *buf;
  11034. + uInt len;
  11035. +{
  11036. + unsigned long s1 = adler & 0xffff;
  11037. + unsigned long s2 = (adler >> 16) & 0xffff;
  11038. + int k;
  11039. +
  11040. + if (buf == Z_NULL) return 1L;
  11041. +
  11042. + while (len > 0) {
  11043. + k = len < NMAX ? len : NMAX;
  11044. + len -= k;
  11045. + while (k >= 16) {
  11046. + DO16(buf);
  11047. + buf += 16;
  11048. + k -= 16;
  11049. + }
  11050. + if (k != 0) do {
  11051. + s1 += *buf++;
  11052. + s2 += s1;
  11053. + } while (--k);
  11054. + s1 %= BASE;
  11055. + s2 %= BASE;
  11056. + }
  11057. + return (s2 << 16) | s1;
  11058. +}
  11059. --- /dev/null Tue Mar 11 13:02:56 2003
  11060. +++ linux/net/ipsec/aes/Makefile Mon Feb 9 13:51:03 2004
  11061. @@ -0,0 +1,59 @@
  11062. +# Makefile for KLIPS 3DES kernel code as a module for 2.6 kernels
  11063. +#
  11064. +# Makefile for KLIPS kernel code as a module
  11065. +# Copyright (C) 2002-2004 Michael Richardson <mcr@xelerance.com>
  11066. +#
  11067. +# This program is free software; you can redistribute it and/or modify it
  11068. +# under the terms of the GNU General Public License as published by the
  11069. +# Free Software Foundation; either version 2 of the License, or (at your
  11070. +# option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  11071. +#
  11072. +# This program is distributed in the hope that it will be useful, but
  11073. +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11074. +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11075. +# for more details.
  11076. +#
  11077. +# RCSID $Id: Makefile.fs2_6,v 1.1.10.1 2005/08/12 16:10:05 ken Exp $
  11078. +#
  11079. +# Note! Dependencies are done automagically by 'make dep', which also
  11080. +# removes any old dependencies. DON'T put your own dependencies here
  11081. +# unless it's something special (ie not a .c file).
  11082. +#
  11083. +
  11084. +obj-$(CONFIG_KLIPS_ENC_AES) += ipsec_alg_aes.o
  11085. +obj-$(CONFIG_KLIPS_ENC_AES) += aes_xcbc_mac.o
  11086. +obj-$(CONFIG_KLIPS_ENC_AES) += aes_cbc.o
  11087. +
  11088. +ifeq ($(strip ${SUBARCH}),)
  11089. +SUBARCH:=${ARCH}
  11090. +endif
  11091. +
  11092. +# the assembly version expects frame pointers, which are
  11093. +# optional in many kernel builds. If you want speed, you should
  11094. +# probably use cryptoapi code instead.
  11095. +USEASSEMBLY=${SUBARCH}${CONFIG_FRAME_POINTER}
  11096. +ifeq (${USEASSEMBLY},i386y)
  11097. +obj-$(CONFIG_KLIPS_ENC_AES) += aes-i586.o
  11098. +else
  11099. +obj-$(CONFIG_KLIPS_ENC_AES) += aes.o
  11100. +endif
  11101. +
  11102. +
  11103. +#
  11104. +# $Log: Makefile.fs2_6,v $
  11105. +# Revision 1.1.10.1 2005/08/12 16:10:05 ken
  11106. +# do not use assembly code with there are no frame pointers
  11107. +#
  11108. +# Revision 1.2 2005/08/12 14:13:58 mcr
  11109. +# do not use assembly code with there are no frame pointers,
  11110. +# as it does not have the right linkages.
  11111. +#
  11112. +# Revision 1.1 2004/08/17 03:31:34 mcr
  11113. +# klips 2.6 edits.
  11114. +#
  11115. +#
  11116. +# Local Variables:
  11117. +# compile-command: "(cd ../../.. && source umlsetup.sh && make -C ${POOLSPACE} module/ipsec.o)"
  11118. +# End Variables:
  11119. +#
  11120. +
  11121. --- /dev/null Tue Mar 11 13:02:56 2003
  11122. +++ linux/net/ipsec/aes/aes-i586.S Mon Feb 9 13:51:03 2004
  11123. @@ -0,0 +1,892 @@
  11124. +//
  11125. +// Copyright (c) 2001, Dr Brian Gladman <brg@gladman.uk.net>, Worcester, UK.
  11126. +// All rights reserved.
  11127. +//
  11128. +// TERMS
  11129. +//
  11130. +// Redistribution and use in source and binary forms, with or without
  11131. +// modification, are permitted subject to the following conditions:
  11132. +//
  11133. +// 1. Redistributions of source code must retain the above copyright
  11134. +// notice, this list of conditions and the following disclaimer.
  11135. +//
  11136. +// 2. Redistributions in binary form must reproduce the above copyright
  11137. +// notice, this list of conditions and the following disclaimer in the
  11138. +// documentation and/or other materials provided with the distribution.
  11139. +//
  11140. +// 3. The copyright holder's name must not be used to endorse or promote
  11141. +// any products derived from this software without his specific prior
  11142. +// written permission.
  11143. +//
  11144. +// This software is provided 'as is' with no express or implied warranties
  11145. +// of correctness or fitness for purpose.
  11146. +
  11147. +// Modified by Jari Ruusu, December 24 2001
  11148. +// - Converted syntax to GNU CPP/assembler syntax
  11149. +// - C programming interface converted back to "old" API
  11150. +// - Minor portability cleanups and speed optimizations
  11151. +
  11152. +// An AES (Rijndael) implementation for the Pentium. This version only
  11153. +// implements the standard AES block length (128 bits, 16 bytes). This code
  11154. +// does not preserve the eax, ecx or edx registers or the artihmetic status
  11155. +// flags. However, the ebx, esi, edi, and ebp registers are preserved across
  11156. +// calls.
  11157. +
  11158. +// void aes_set_key(aes_context *cx, const unsigned char key[], const int key_len, const int f)
  11159. +// void aes_encrypt(const aes_context *cx, const unsigned char in_blk[], unsigned char out_blk[])
  11160. +// void aes_decrypt(const aes_context *cx, const unsigned char in_blk[], unsigned char out_blk[])
  11161. +
  11162. +#if defined(USE_UNDERLINE)
  11163. +# define aes_set_key _aes_set_key
  11164. +# define aes_encrypt _aes_encrypt
  11165. +# define aes_decrypt _aes_decrypt
  11166. +#endif
  11167. +#if !defined(ALIGN32BYTES)
  11168. +# define ALIGN32BYTES 32
  11169. +#endif
  11170. +
  11171. + .file "aes-i586.S"
  11172. + .globl aes_set_key
  11173. + .globl aes_encrypt
  11174. + .globl aes_decrypt
  11175. +
  11176. +#define tlen 1024 // length of each of 4 'xor' arrays (256 32-bit words)
  11177. +
  11178. +// offsets to parameters with one register pushed onto stack
  11179. +
  11180. +#define ctx 8 // AES context structure
  11181. +#define in_blk 12 // input byte array address parameter
  11182. +#define out_blk 16 // output byte array address parameter
  11183. +
  11184. +// offsets in context structure
  11185. +
  11186. +#define nkey 0 // key length, size 4
  11187. +#define nrnd 4 // number of rounds, size 4
  11188. +#define ekey 8 // encryption key schedule base address, size 256
  11189. +#define dkey 264 // decryption key schedule base address, size 256
  11190. +
  11191. +// This macro performs a forward encryption cycle. It is entered with
  11192. +// the first previous round column values in %eax, %ebx, %esi and %edi and
  11193. +// exits with the final values in the same registers.
  11194. +
  11195. +#define fwd_rnd(p1,p2) \
  11196. + mov %ebx,(%esp) ;\
  11197. + movzbl %al,%edx ;\
  11198. + mov %eax,%ecx ;\
  11199. + mov p2(%ebp),%eax ;\
  11200. + mov %edi,4(%esp) ;\
  11201. + mov p2+12(%ebp),%edi ;\
  11202. + xor p1(,%edx,4),%eax ;\
  11203. + movzbl %ch,%edx ;\
  11204. + shr $16,%ecx ;\
  11205. + mov p2+4(%ebp),%ebx ;\
  11206. + xor p1+tlen(,%edx,4),%edi ;\
  11207. + movzbl %cl,%edx ;\
  11208. + movzbl %ch,%ecx ;\
  11209. + xor p1+3*tlen(,%ecx,4),%ebx ;\
  11210. + mov %esi,%ecx ;\
  11211. + mov p1+2*tlen(,%edx,4),%esi ;\
  11212. + movzbl %cl,%edx ;\
  11213. + xor p1(,%edx,4),%esi ;\
  11214. + movzbl %ch,%edx ;\
  11215. + shr $16,%ecx ;\
  11216. + xor p1+tlen(,%edx,4),%ebx ;\
  11217. + movzbl %cl,%edx ;\
  11218. + movzbl %ch,%ecx ;\
  11219. + xor p1+2*tlen(,%edx,4),%eax ;\
  11220. + mov (%esp),%edx ;\
  11221. + xor p1+3*tlen(,%ecx,4),%edi ;\
  11222. + movzbl %dl,%ecx ;\
  11223. + xor p2+8(%ebp),%esi ;\
  11224. + xor p1(,%ecx,4),%ebx ;\
  11225. + movzbl %dh,%ecx ;\
  11226. + shr $16,%edx ;\
  11227. + xor p1+tlen(,%ecx,4),%eax ;\
  11228. + movzbl %dl,%ecx ;\
  11229. + movzbl %dh,%edx ;\
  11230. + xor p1+2*tlen(,%ecx,4),%edi ;\
  11231. + mov 4(%esp),%ecx ;\
  11232. + xor p1+3*tlen(,%edx,4),%esi ;\
  11233. + movzbl %cl,%edx ;\
  11234. + xor p1(,%edx,4),%edi ;\
  11235. + movzbl %ch,%edx ;\
  11236. + shr $16,%ecx ;\
  11237. + xor p1+tlen(,%edx,4),%esi ;\
  11238. + movzbl %cl,%edx ;\
  11239. + movzbl %ch,%ecx ;\
  11240. + xor p1+2*tlen(,%edx,4),%ebx ;\
  11241. + xor p1+3*tlen(,%ecx,4),%eax
  11242. +
  11243. +// This macro performs an inverse encryption cycle. It is entered with
  11244. +// the first previous round column values in %eax, %ebx, %esi and %edi and
  11245. +// exits with the final values in the same registers.
  11246. +
  11247. +#define inv_rnd(p1,p2) \
  11248. + movzbl %al,%edx ;\
  11249. + mov %ebx,(%esp) ;\
  11250. + mov %eax,%ecx ;\
  11251. + mov p2(%ebp),%eax ;\
  11252. + mov %edi,4(%esp) ;\
  11253. + mov p2+4(%ebp),%ebx ;\
  11254. + xor p1(,%edx,4),%eax ;\
  11255. + movzbl %ch,%edx ;\
  11256. + shr $16,%ecx ;\
  11257. + mov p2+12(%ebp),%edi ;\
  11258. + xor p1+tlen(,%edx,4),%ebx ;\
  11259. + movzbl %cl,%edx ;\
  11260. + movzbl %ch,%ecx ;\
  11261. + xor p1+3*tlen(,%ecx,4),%edi ;\
  11262. + mov %esi,%ecx ;\
  11263. + mov p1+2*tlen(,%edx,4),%esi ;\
  11264. + movzbl %cl,%edx ;\
  11265. + xor p1(,%edx,4),%esi ;\
  11266. + movzbl %ch,%edx ;\
  11267. + shr $16,%ecx ;\
  11268. + xor p1+tlen(,%edx,4),%edi ;\
  11269. + movzbl %cl,%edx ;\
  11270. + movzbl %ch,%ecx ;\
  11271. + xor p1+2*tlen(,%edx,4),%eax ;\
  11272. + mov (%esp),%edx ;\
  11273. + xor p1+3*tlen(,%ecx,4),%ebx ;\
  11274. + movzbl %dl,%ecx ;\
  11275. + xor p2+8(%ebp),%esi ;\
  11276. + xor p1(,%ecx,4),%ebx ;\
  11277. + movzbl %dh,%ecx ;\
  11278. + shr $16,%edx ;\
  11279. + xor p1+tlen(,%ecx,4),%esi ;\
  11280. + movzbl %dl,%ecx ;\
  11281. + movzbl %dh,%edx ;\
  11282. + xor p1+2*tlen(,%ecx,4),%edi ;\
  11283. + mov 4(%esp),%ecx ;\
  11284. + xor p1+3*tlen(,%edx,4),%eax ;\
  11285. + movzbl %cl,%edx ;\
  11286. + xor p1(,%edx,4),%edi ;\
  11287. + movzbl %ch,%edx ;\
  11288. + shr $16,%ecx ;\
  11289. + xor p1+tlen(,%edx,4),%eax ;\
  11290. + movzbl %cl,%edx ;\
  11291. + movzbl %ch,%ecx ;\
  11292. + xor p1+2*tlen(,%edx,4),%ebx ;\
  11293. + xor p1+3*tlen(,%ecx,4),%esi
  11294. +
  11295. +// AES (Rijndael) Encryption Subroutine
  11296. +
  11297. + .text
  11298. + .align ALIGN32BYTES
  11299. +aes_encrypt:
  11300. + push %ebp
  11301. + mov ctx(%esp),%ebp // pointer to context
  11302. + mov in_blk(%esp),%ecx
  11303. + push %ebx
  11304. + push %esi
  11305. + push %edi
  11306. + mov nrnd(%ebp),%edx // number of rounds
  11307. + lea ekey+16(%ebp),%ebp // key pointer
  11308. +
  11309. +// input four columns and xor in first round key
  11310. +
  11311. + mov (%ecx),%eax
  11312. + mov 4(%ecx),%ebx
  11313. + mov 8(%ecx),%esi
  11314. + mov 12(%ecx),%edi
  11315. + xor -16(%ebp),%eax
  11316. + xor -12(%ebp),%ebx
  11317. + xor -8(%ebp),%esi
  11318. + xor -4(%ebp),%edi
  11319. +
  11320. + sub $8,%esp // space for register saves on stack
  11321. +
  11322. + sub $10,%edx
  11323. + je aes_15
  11324. + add $32,%ebp
  11325. + sub $2,%edx
  11326. + je aes_13
  11327. + add $32,%ebp
  11328. +
  11329. + fwd_rnd(aes_ft_tab,-64) // 14 rounds for 256-bit key
  11330. + fwd_rnd(aes_ft_tab,-48)
  11331. +aes_13: fwd_rnd(aes_ft_tab,-32) // 12 rounds for 192-bit key
  11332. + fwd_rnd(aes_ft_tab,-16)
  11333. +aes_15: fwd_rnd(aes_ft_tab,0) // 10 rounds for 128-bit key
  11334. + fwd_rnd(aes_ft_tab,16)
  11335. + fwd_rnd(aes_ft_tab,32)
  11336. + fwd_rnd(aes_ft_tab,48)
  11337. + fwd_rnd(aes_ft_tab,64)
  11338. + fwd_rnd(aes_ft_tab,80)
  11339. + fwd_rnd(aes_ft_tab,96)
  11340. + fwd_rnd(aes_ft_tab,112)
  11341. + fwd_rnd(aes_ft_tab,128)
  11342. + fwd_rnd(aes_fl_tab,144) // last round uses a different table
  11343. +
  11344. +// move final values to the output array.
  11345. +
  11346. + mov out_blk+20(%esp),%ebp
  11347. + add $8,%esp
  11348. + mov %eax,(%ebp)
  11349. + mov %ebx,4(%ebp)
  11350. + mov %esi,8(%ebp)
  11351. + mov %edi,12(%ebp)
  11352. + pop %edi
  11353. + pop %esi
  11354. + pop %ebx
  11355. + pop %ebp
  11356. + ret
  11357. +
  11358. +
  11359. +// AES (Rijndael) Decryption Subroutine
  11360. +
  11361. + .align ALIGN32BYTES
  11362. +aes_decrypt:
  11363. + push %ebp
  11364. + mov ctx(%esp),%ebp // pointer to context
  11365. + mov in_blk(%esp),%ecx
  11366. + push %ebx
  11367. + push %esi
  11368. + push %edi
  11369. + mov nrnd(%ebp),%edx // number of rounds
  11370. + lea dkey+16(%ebp),%ebp // key pointer
  11371. +
  11372. +// input four columns and xor in first round key
  11373. +
  11374. + mov (%ecx),%eax
  11375. + mov 4(%ecx),%ebx
  11376. + mov 8(%ecx),%esi
  11377. + mov 12(%ecx),%edi
  11378. + xor -16(%ebp),%eax
  11379. + xor -12(%ebp),%ebx
  11380. + xor -8(%ebp),%esi
  11381. + xor -4(%ebp),%edi
  11382. +
  11383. + sub $8,%esp // space for register saves on stack
  11384. +
  11385. + sub $10,%edx
  11386. + je aes_25
  11387. + add $32,%ebp
  11388. + sub $2,%edx
  11389. + je aes_23
  11390. + add $32,%ebp
  11391. +
  11392. + inv_rnd(aes_it_tab,-64) // 14 rounds for 256-bit key
  11393. + inv_rnd(aes_it_tab,-48)
  11394. +aes_23: inv_rnd(aes_it_tab,-32) // 12 rounds for 192-bit key
  11395. + inv_rnd(aes_it_tab,-16)
  11396. +aes_25: inv_rnd(aes_it_tab,0) // 10 rounds for 128-bit key
  11397. + inv_rnd(aes_it_tab,16)
  11398. + inv_rnd(aes_it_tab,32)
  11399. + inv_rnd(aes_it_tab,48)
  11400. + inv_rnd(aes_it_tab,64)
  11401. + inv_rnd(aes_it_tab,80)
  11402. + inv_rnd(aes_it_tab,96)
  11403. + inv_rnd(aes_it_tab,112)
  11404. + inv_rnd(aes_it_tab,128)
  11405. + inv_rnd(aes_il_tab,144) // last round uses a different table
  11406. +
  11407. +// move final values to the output array.
  11408. +
  11409. + mov out_blk+20(%esp),%ebp
  11410. + add $8,%esp
  11411. + mov %eax,(%ebp)
  11412. + mov %ebx,4(%ebp)
  11413. + mov %esi,8(%ebp)
  11414. + mov %edi,12(%ebp)
  11415. + pop %edi
  11416. + pop %esi
  11417. + pop %ebx
  11418. + pop %ebp
  11419. + ret
  11420. +
  11421. +// AES (Rijndael) Key Schedule Subroutine
  11422. +
  11423. +// input/output parameters
  11424. +
  11425. +#define aes_cx 12 // AES context
  11426. +#define in_key 16 // key input array address
  11427. +#define key_ln 20 // key length, bytes (16,24,32) or bits (128,192,256)
  11428. +#define ed_flg 24 // 0=create both encr/decr keys, 1=create encr key only
  11429. +
  11430. +// offsets for locals
  11431. +
  11432. +#define cnt -4
  11433. +#define kpf -8
  11434. +#define slen 8
  11435. +
  11436. +// This macro performs a column mixing operation on an input 32-bit
  11437. +// word to give a 32-bit result. It uses each of the 4 bytes in the
  11438. +// the input column to index 4 different tables of 256 32-bit words
  11439. +// that are xored together to form the output value.
  11440. +
  11441. +#define mix_col(p1) \
  11442. + movzbl %bl,%ecx ;\
  11443. + mov p1(,%ecx,4),%eax ;\
  11444. + movzbl %bh,%ecx ;\
  11445. + ror $16,%ebx ;\
  11446. + xor p1+tlen(,%ecx,4),%eax ;\
  11447. + movzbl %bl,%ecx ;\
  11448. + xor p1+2*tlen(,%ecx,4),%eax ;\
  11449. + movzbl %bh,%ecx ;\
  11450. + xor p1+3*tlen(,%ecx,4),%eax
  11451. +
  11452. +// Key Schedule Macros
  11453. +
  11454. +#define ksc4(p1) \
  11455. + rol $24,%ebx ;\
  11456. + mix_col(aes_fl_tab) ;\
  11457. + ror $8,%ebx ;\
  11458. + xor 4*p1+aes_rcon_tab,%eax ;\
  11459. + xor %eax,%esi ;\
  11460. + xor %esi,%ebp ;\
  11461. + mov %esi,16*p1(%edi) ;\
  11462. + mov %ebp,16*p1+4(%edi) ;\
  11463. + xor %ebp,%edx ;\
  11464. + xor %edx,%ebx ;\
  11465. + mov %edx,16*p1+8(%edi) ;\
  11466. + mov %ebx,16*p1+12(%edi)
  11467. +
  11468. +#define ksc6(p1) \
  11469. + rol $24,%ebx ;\
  11470. + mix_col(aes_fl_tab) ;\
  11471. + ror $8,%ebx ;\
  11472. + xor 4*p1+aes_rcon_tab,%eax ;\
  11473. + xor 24*p1-24(%edi),%eax ;\
  11474. + mov %eax,24*p1(%edi) ;\
  11475. + xor 24*p1-20(%edi),%eax ;\
  11476. + mov %eax,24*p1+4(%edi) ;\
  11477. + xor %eax,%esi ;\
  11478. + xor %esi,%ebp ;\
  11479. + mov %esi,24*p1+8(%edi) ;\
  11480. + mov %ebp,24*p1+12(%edi) ;\
  11481. + xor %ebp,%edx ;\
  11482. + xor %edx,%ebx ;\
  11483. + mov %edx,24*p1+16(%edi) ;\
  11484. + mov %ebx,24*p1+20(%edi)
  11485. +
  11486. +#define ksc8(p1) \
  11487. + rol $24,%ebx ;\
  11488. + mix_col(aes_fl_tab) ;\
  11489. + ror $8,%ebx ;\
  11490. + xor 4*p1+aes_rcon_tab,%eax ;\
  11491. + xor 32*p1-32(%edi),%eax ;\
  11492. + mov %eax,32*p1(%edi) ;\
  11493. + xor 32*p1-28(%edi),%eax ;\
  11494. + mov %eax,32*p1+4(%edi) ;\
  11495. + xor 32*p1-24(%edi),%eax ;\
  11496. + mov %eax,32*p1+8(%edi) ;\
  11497. + xor 32*p1-20(%edi),%eax ;\
  11498. + mov %eax,32*p1+12(%edi) ;\
  11499. + push %ebx ;\
  11500. + mov %eax,%ebx ;\
  11501. + mix_col(aes_fl_tab) ;\
  11502. + pop %ebx ;\
  11503. + xor %eax,%esi ;\
  11504. + xor %esi,%ebp ;\
  11505. + mov %esi,32*p1+16(%edi) ;\
  11506. + mov %ebp,32*p1+20(%edi) ;\
  11507. + xor %ebp,%edx ;\
  11508. + xor %edx,%ebx ;\
  11509. + mov %edx,32*p1+24(%edi) ;\
  11510. + mov %ebx,32*p1+28(%edi)
  11511. +
  11512. + .align ALIGN32BYTES
  11513. +aes_set_key:
  11514. + pushfl
  11515. + push %ebp
  11516. + mov %esp,%ebp
  11517. + sub $slen,%esp
  11518. + push %ebx
  11519. + push %esi
  11520. + push %edi
  11521. +
  11522. + mov aes_cx(%ebp),%edx // edx -> AES context
  11523. +
  11524. + mov key_ln(%ebp),%ecx // key length
  11525. + cmpl $128,%ecx
  11526. + jb aes_30
  11527. + shr $3,%ecx
  11528. +aes_30: cmpl $32,%ecx
  11529. + je aes_32
  11530. + cmpl $24,%ecx
  11531. + je aes_32
  11532. + mov $16,%ecx
  11533. +aes_32: shr $2,%ecx
  11534. + mov %ecx,nkey(%edx)
  11535. +
  11536. + lea 6(%ecx),%eax // 10/12/14 for 4/6/8 32-bit key length
  11537. + mov %eax,nrnd(%edx)
  11538. +
  11539. + mov in_key(%ebp),%esi // key input array
  11540. + lea ekey(%edx),%edi // key position in AES context
  11541. + cld
  11542. + push %ebp
  11543. + mov %ecx,%eax // save key length in eax
  11544. + rep ; movsl // words in the key schedule
  11545. + mov -4(%esi),%ebx // put some values in registers
  11546. + mov -8(%esi),%edx // to allow faster code
  11547. + mov -12(%esi),%ebp
  11548. + mov -16(%esi),%esi
  11549. +
  11550. + cmpl $4,%eax // jump on key size
  11551. + je aes_36
  11552. + cmpl $6,%eax
  11553. + je aes_35
  11554. +
  11555. + ksc8(0)
  11556. + ksc8(1)
  11557. + ksc8(2)
  11558. + ksc8(3)
  11559. + ksc8(4)
  11560. + ksc8(5)
  11561. + ksc8(6)
  11562. + jmp aes_37
  11563. +aes_35: ksc6(0)
  11564. + ksc6(1)
  11565. + ksc6(2)
  11566. + ksc6(3)
  11567. + ksc6(4)
  11568. + ksc6(5)
  11569. + ksc6(6)
  11570. + ksc6(7)
  11571. + jmp aes_37
  11572. +aes_36: ksc4(0)
  11573. + ksc4(1)
  11574. + ksc4(2)
  11575. + ksc4(3)
  11576. + ksc4(4)
  11577. + ksc4(5)
  11578. + ksc4(6)
  11579. + ksc4(7)
  11580. + ksc4(8)
  11581. + ksc4(9)
  11582. +aes_37: pop %ebp
  11583. + mov aes_cx(%ebp),%edx // edx -> AES context
  11584. + cmpl $0,ed_flg(%ebp)
  11585. + jne aes_39
  11586. +
  11587. +// compile decryption key schedule from encryption schedule - reverse
  11588. +// order and do mix_column operation on round keys except first and last
  11589. +
  11590. + mov nrnd(%edx),%eax // kt = cx->d_key + nc * cx->Nrnd
  11591. + shl $2,%eax
  11592. + lea dkey(%edx,%eax,4),%edi
  11593. + lea ekey(%edx),%esi // kf = cx->e_key
  11594. +
  11595. + movsl // copy first round key (unmodified)
  11596. + movsl
  11597. + movsl
  11598. + movsl
  11599. + sub $32,%edi
  11600. + movl $1,cnt(%ebp)
  11601. +aes_38: // do mix column on each column of
  11602. + lodsl // each round key
  11603. + mov %eax,%ebx
  11604. + mix_col(aes_im_tab)
  11605. + stosl
  11606. + lodsl
  11607. + mov %eax,%ebx
  11608. + mix_col(aes_im_tab)
  11609. + stosl
  11610. + lodsl
  11611. + mov %eax,%ebx
  11612. + mix_col(aes_im_tab)
  11613. + stosl
  11614. + lodsl
  11615. + mov %eax,%ebx
  11616. + mix_col(aes_im_tab)
  11617. + stosl
  11618. + sub $32,%edi
  11619. +
  11620. + incl cnt(%ebp)
  11621. + mov cnt(%ebp),%eax
  11622. + cmp nrnd(%edx),%eax
  11623. + jb aes_38
  11624. +
  11625. + movsl // copy last round key (unmodified)
  11626. + movsl
  11627. + movsl
  11628. + movsl
  11629. +aes_39: pop %edi
  11630. + pop %esi
  11631. + pop %ebx
  11632. + mov %ebp,%esp
  11633. + pop %ebp
  11634. + popfl
  11635. + ret
  11636. +
  11637. +
  11638. +// finite field multiplies by {02}, {04} and {08}
  11639. +
  11640. +#define f2(x) ((x<<1)^(((x>>7)&1)*0x11b))
  11641. +#define f4(x) ((x<<2)^(((x>>6)&1)*0x11b)^(((x>>6)&2)*0x11b))
  11642. +#define f8(x) ((x<<3)^(((x>>5)&1)*0x11b)^(((x>>5)&2)*0x11b)^(((x>>5)&4)*0x11b))
  11643. +
  11644. +// finite field multiplies required in table generation
  11645. +
  11646. +#define f3(x) (f2(x) ^ x)
  11647. +#define f9(x) (f8(x) ^ x)
  11648. +#define fb(x) (f8(x) ^ f2(x) ^ x)
  11649. +#define fd(x) (f8(x) ^ f4(x) ^ x)
  11650. +#define fe(x) (f8(x) ^ f4(x) ^ f2(x))
  11651. +
  11652. +// These defines generate the forward table entries
  11653. +
  11654. +#define u0(x) ((f3(x) << 24) | (x << 16) | (x << 8) | f2(x))
  11655. +#define u1(x) ((x << 24) | (x << 16) | (f2(x) << 8) | f3(x))
  11656. +#define u2(x) ((x << 24) | (f2(x) << 16) | (f3(x) << 8) | x)
  11657. +#define u3(x) ((f2(x) << 24) | (f3(x) << 16) | (x << 8) | x)
  11658. +
  11659. +// These defines generate the inverse table entries
  11660. +
  11661. +#define v0(x) ((fb(x) << 24) | (fd(x) << 16) | (f9(x) << 8) | fe(x))
  11662. +#define v1(x) ((fd(x) << 24) | (f9(x) << 16) | (fe(x) << 8) | fb(x))
  11663. +#define v2(x) ((f9(x) << 24) | (fe(x) << 16) | (fb(x) << 8) | fd(x))
  11664. +#define v3(x) ((fe(x) << 24) | (fb(x) << 16) | (fd(x) << 8) | f9(x))
  11665. +
  11666. +// These defines generate entries for the last round tables
  11667. +
  11668. +#define w0(x) (x)
  11669. +#define w1(x) (x << 8)
  11670. +#define w2(x) (x << 16)
  11671. +#define w3(x) (x << 24)
  11672. +
  11673. +// macro to generate inverse mix column tables (needed for the key schedule)
  11674. +
  11675. +#define im_data0(p1) \
  11676. + .long p1(0x00),p1(0x01),p1(0x02),p1(0x03),p1(0x04),p1(0x05),p1(0x06),p1(0x07) ;\
  11677. + .long p1(0x08),p1(0x09),p1(0x0a),p1(0x0b),p1(0x0c),p1(0x0d),p1(0x0e),p1(0x0f) ;\
  11678. + .long p1(0x10),p1(0x11),p1(0x12),p1(0x13),p1(0x14),p1(0x15),p1(0x16),p1(0x17) ;\
  11679. + .long p1(0x18),p1(0x19),p1(0x1a),p1(0x1b),p1(0x1c),p1(0x1d),p1(0x1e),p1(0x1f)
  11680. +#define im_data1(p1) \
  11681. + .long p1(0x20),p1(0x21),p1(0x22),p1(0x23),p1(0x24),p1(0x25),p1(0x26),p1(0x27) ;\
  11682. + .long p1(0x28),p1(0x29),p1(0x2a),p1(0x2b),p1(0x2c),p1(0x2d),p1(0x2e),p1(0x2f) ;\
  11683. + .long p1(0x30),p1(0x31),p1(0x32),p1(0x33),p1(0x34),p1(0x35),p1(0x36),p1(0x37) ;\
  11684. + .long p1(0x38),p1(0x39),p1(0x3a),p1(0x3b),p1(0x3c),p1(0x3d),p1(0x3e),p1(0x3f)
  11685. +#define im_data2(p1) \
  11686. + .long p1(0x40),p1(0x41),p1(0x42),p1(0x43),p1(0x44),p1(0x45),p1(0x46),p1(0x47) ;\
  11687. + .long p1(0x48),p1(0x49),p1(0x4a),p1(0x4b),p1(0x4c),p1(0x4d),p1(0x4e),p1(0x4f) ;\
  11688. + .long p1(0x50),p1(0x51),p1(0x52),p1(0x53),p1(0x54),p1(0x55),p1(0x56),p1(0x57) ;\
  11689. + .long p1(0x58),p1(0x59),p1(0x5a),p1(0x5b),p1(0x5c),p1(0x5d),p1(0x5e),p1(0x5f)
  11690. +#define im_data3(p1) \
  11691. + .long p1(0x60),p1(0x61),p1(0x62),p1(0x63),p1(0x64),p1(0x65),p1(0x66),p1(0x67) ;\
  11692. + .long p1(0x68),p1(0x69),p1(0x6a),p1(0x6b),p1(0x6c),p1(0x6d),p1(0x6e),p1(0x6f) ;\
  11693. + .long p1(0x70),p1(0x71),p1(0x72),p1(0x73),p1(0x74),p1(0x75),p1(0x76),p1(0x77) ;\
  11694. + .long p1(0x78),p1(0x79),p1(0x7a),p1(0x7b),p1(0x7c),p1(0x7d),p1(0x7e),p1(0x7f)
  11695. +#define im_data4(p1) \
  11696. + .long p1(0x80),p1(0x81),p1(0x82),p1(0x83),p1(0x84),p1(0x85),p1(0x86),p1(0x87) ;\
  11697. + .long p1(0x88),p1(0x89),p1(0x8a),p1(0x8b),p1(0x8c),p1(0x8d),p1(0x8e),p1(0x8f) ;\
  11698. + .long p1(0x90),p1(0x91),p1(0x92),p1(0x93),p1(0x94),p1(0x95),p1(0x96),p1(0x97) ;\
  11699. + .long p1(0x98),p1(0x99),p1(0x9a),p1(0x9b),p1(0x9c),p1(0x9d),p1(0x9e),p1(0x9f)
  11700. +#define im_data5(p1) \
  11701. + .long p1(0xa0),p1(0xa1),p1(0xa2),p1(0xa3),p1(0xa4),p1(0xa5),p1(0xa6),p1(0xa7) ;\
  11702. + .long p1(0xa8),p1(0xa9),p1(0xaa),p1(0xab),p1(0xac),p1(0xad),p1(0xae),p1(0xaf) ;\
  11703. + .long p1(0xb0),p1(0xb1),p1(0xb2),p1(0xb3),p1(0xb4),p1(0xb5),p1(0xb6),p1(0xb7) ;\
  11704. + .long p1(0xb8),p1(0xb9),p1(0xba),p1(0xbb),p1(0xbc),p1(0xbd),p1(0xbe),p1(0xbf)
  11705. +#define im_data6(p1) \
  11706. + .long p1(0xc0),p1(0xc1),p1(0xc2),p1(0xc3),p1(0xc4),p1(0xc5),p1(0xc6),p1(0xc7) ;\
  11707. + .long p1(0xc8),p1(0xc9),p1(0xca),p1(0xcb),p1(0xcc),p1(0xcd),p1(0xce),p1(0xcf) ;\
  11708. + .long p1(0xd0),p1(0xd1),p1(0xd2),p1(0xd3),p1(0xd4),p1(0xd5),p1(0xd6),p1(0xd7) ;\
  11709. + .long p1(0xd8),p1(0xd9),p1(0xda),p1(0xdb),p1(0xdc),p1(0xdd),p1(0xde),p1(0xdf)
  11710. +#define im_data7(p1) \
  11711. + .long p1(0xe0),p1(0xe1),p1(0xe2),p1(0xe3),p1(0xe4),p1(0xe5),p1(0xe6),p1(0xe7) ;\
  11712. + .long p1(0xe8),p1(0xe9),p1(0xea),p1(0xeb),p1(0xec),p1(0xed),p1(0xee),p1(0xef) ;\
  11713. + .long p1(0xf0),p1(0xf1),p1(0xf2),p1(0xf3),p1(0xf4),p1(0xf5),p1(0xf6),p1(0xf7) ;\
  11714. + .long p1(0xf8),p1(0xf9),p1(0xfa),p1(0xfb),p1(0xfc),p1(0xfd),p1(0xfe),p1(0xff)
  11715. +
  11716. +// S-box data - 256 entries
  11717. +
  11718. +#define sb_data0(p1) \
  11719. + .long p1(0x63),p1(0x7c),p1(0x77),p1(0x7b),p1(0xf2),p1(0x6b),p1(0x6f),p1(0xc5) ;\
  11720. + .long p1(0x30),p1(0x01),p1(0x67),p1(0x2b),p1(0xfe),p1(0xd7),p1(0xab),p1(0x76) ;\
  11721. + .long p1(0xca),p1(0x82),p1(0xc9),p1(0x7d),p1(0xfa),p1(0x59),p1(0x47),p1(0xf0) ;\
  11722. + .long p1(0xad),p1(0xd4),p1(0xa2),p1(0xaf),p1(0x9c),p1(0xa4),p1(0x72),p1(0xc0)
  11723. +#define sb_data1(p1) \
  11724. + .long p1(0xb7),p1(0xfd),p1(0x93),p1(0x26),p1(0x36),p1(0x3f),p1(0xf7),p1(0xcc) ;\
  11725. + .long p1(0x34),p1(0xa5),p1(0xe5),p1(0xf1),p1(0x71),p1(0xd8),p1(0x31),p1(0x15) ;\
  11726. + .long p1(0x04),p1(0xc7),p1(0x23),p1(0xc3),p1(0x18),p1(0x96),p1(0x05),p1(0x9a) ;\
  11727. + .long p1(0x07),p1(0x12),p1(0x80),p1(0xe2),p1(0xeb),p1(0x27),p1(0xb2),p1(0x75)
  11728. +#define sb_data2(p1) \
  11729. + .long p1(0x09),p1(0x83),p1(0x2c),p1(0x1a),p1(0x1b),p1(0x6e),p1(0x5a),p1(0xa0) ;\
  11730. + .long p1(0x52),p1(0x3b),p1(0xd6),p1(0xb3),p1(0x29),p1(0xe3),p1(0x2f),p1(0x84) ;\
  11731. + .long p1(0x53),p1(0xd1),p1(0x00),p1(0xed),p1(0x20),p1(0xfc),p1(0xb1),p1(0x5b) ;\
  11732. + .long p1(0x6a),p1(0xcb),p1(0xbe),p1(0x39),p1(0x4a),p1(0x4c),p1(0x58),p1(0xcf)
  11733. +#define sb_data3(p1) \
  11734. + .long p1(0xd0),p1(0xef),p1(0xaa),p1(0xfb),p1(0x43),p1(0x4d),p1(0x33),p1(0x85) ;\
  11735. + .long p1(0x45),p1(0xf9),p1(0x02),p1(0x7f),p1(0x50),p1(0x3c),p1(0x9f),p1(0xa8) ;\
  11736. + .long p1(0x51),p1(0xa3),p1(0x40),p1(0x8f),p1(0x92),p1(0x9d),p1(0x38),p1(0xf5) ;\
  11737. + .long p1(0xbc),p1(0xb6),p1(0xda),p1(0x21),p1(0x10),p1(0xff),p1(0xf3),p1(0xd2)
  11738. +#define sb_data4(p1) \
  11739. + .long p1(0xcd),p1(0x0c),p1(0x13),p1(0xec),p1(0x5f),p1(0x97),p1(0x44),p1(0x17) ;\
  11740. + .long p1(0xc4),p1(0xa7),p1(0x7e),p1(0x3d),p1(0x64),p1(0x5d),p1(0x19),p1(0x73) ;\
  11741. + .long p1(0x60),p1(0x81),p1(0x4f),p1(0xdc),p1(0x22),p1(0x2a),p1(0x90),p1(0x88) ;\
  11742. + .long p1(0x46),p1(0xee),p1(0xb8),p1(0x14),p1(0xde),p1(0x5e),p1(0x0b),p1(0xdb)
  11743. +#define sb_data5(p1) \
  11744. + .long p1(0xe0),p1(0x32),p1(0x3a),p1(0x0a),p1(0x49),p1(0x06),p1(0x24),p1(0x5c) ;\
  11745. + .long p1(0xc2),p1(0xd3),p1(0xac),p1(0x62),p1(0x91),p1(0x95),p1(0xe4),p1(0x79) ;\
  11746. + .long p1(0xe7),p1(0xc8),p1(0x37),p1(0x6d),p1(0x8d),p1(0xd5),p1(0x4e),p1(0xa9) ;\
  11747. + .long p1(0x6c),p1(0x56),p1(0xf4),p1(0xea),p1(0x65),p1(0x7a),p1(0xae),p1(0x08)
  11748. +#define sb_data6(p1) \
  11749. + .long p1(0xba),p1(0x78),p1(0x25),p1(0x2e),p1(0x1c),p1(0xa6),p1(0xb4),p1(0xc6) ;\
  11750. + .long p1(0xe8),p1(0xdd),p1(0x74),p1(0x1f),p1(0x4b),p1(0xbd),p1(0x8b),p1(0x8a) ;\
  11751. + .long p1(0x70),p1(0x3e),p1(0xb5),p1(0x66),p1(0x48),p1(0x03),p1(0xf6),p1(0x0e) ;\
  11752. + .long p1(0x61),p1(0x35),p1(0x57),p1(0xb9),p1(0x86),p1(0xc1),p1(0x1d),p1(0x9e)
  11753. +#define sb_data7(p1) \
  11754. + .long p1(0xe1),p1(0xf8),p1(0x98),p1(0x11),p1(0x69),p1(0xd9),p1(0x8e),p1(0x94) ;\
  11755. + .long p1(0x9b),p1(0x1e),p1(0x87),p1(0xe9),p1(0xce),p1(0x55),p1(0x28),p1(0xdf) ;\
  11756. + .long p1(0x8c),p1(0xa1),p1(0x89),p1(0x0d),p1(0xbf),p1(0xe6),p1(0x42),p1(0x68) ;\
  11757. + .long p1(0x41),p1(0x99),p1(0x2d),p1(0x0f),p1(0xb0),p1(0x54),p1(0xbb),p1(0x16)
  11758. +
  11759. +// Inverse S-box data - 256 entries
  11760. +
  11761. +#define ib_data0(p1) \
  11762. + .long p1(0x52),p1(0x09),p1(0x6a),p1(0xd5),p1(0x30),p1(0x36),p1(0xa5),p1(0x38) ;\
  11763. + .long p1(0xbf),p1(0x40),p1(0xa3),p1(0x9e),p1(0x81),p1(0xf3),p1(0xd7),p1(0xfb) ;\
  11764. + .long p1(0x7c),p1(0xe3),p1(0x39),p1(0x82),p1(0x9b),p1(0x2f),p1(0xff),p1(0x87) ;\
  11765. + .long p1(0x34),p1(0x8e),p1(0x43),p1(0x44),p1(0xc4),p1(0xde),p1(0xe9),p1(0xcb)
  11766. +#define ib_data1(p1) \
  11767. + .long p1(0x54),p1(0x7b),p1(0x94),p1(0x32),p1(0xa6),p1(0xc2),p1(0x23),p1(0x3d) ;\
  11768. + .long p1(0xee),p1(0x4c),p1(0x95),p1(0x0b),p1(0x42),p1(0xfa),p1(0xc3),p1(0x4e) ;\
  11769. + .long p1(0x08),p1(0x2e),p1(0xa1),p1(0x66),p1(0x28),p1(0xd9),p1(0x24),p1(0xb2) ;\
  11770. + .long p1(0x76),p1(0x5b),p1(0xa2),p1(0x49),p1(0x6d),p1(0x8b),p1(0xd1),p1(0x25)
  11771. +#define ib_data2(p1) \
  11772. + .long p1(0x72),p1(0xf8),p1(0xf6),p1(0x64),p1(0x86),p1(0x68),p1(0x98),p1(0x16) ;\
  11773. + .long p1(0xd4),p1(0xa4),p1(0x5c),p1(0xcc),p1(0x5d),p1(0x65),p1(0xb6),p1(0x92) ;\
  11774. + .long p1(0x6c),p1(0x70),p1(0x48),p1(0x50),p1(0xfd),p1(0xed),p1(0xb9),p1(0xda) ;\
  11775. + .long p1(0x5e),p1(0x15),p1(0x46),p1(0x57),p1(0xa7),p1(0x8d),p1(0x9d),p1(0x84)
  11776. +#define ib_data3(p1) \
  11777. + .long p1(0x90),p1(0xd8),p1(0xab),p1(0x00),p1(0x8c),p1(0xbc),p1(0xd3),p1(0x0a) ;\
  11778. + .long p1(0xf7),p1(0xe4),p1(0x58),p1(0x05),p1(0xb8),p1(0xb3),p1(0x45),p1(0x06) ;\
  11779. + .long p1(0xd0),p1(0x2c),p1(0x1e),p1(0x8f),p1(0xca),p1(0x3f),p1(0x0f),p1(0x02) ;\
  11780. + .long p1(0xc1),p1(0xaf),p1(0xbd),p1(0x03),p1(0x01),p1(0x13),p1(0x8a),p1(0x6b)
  11781. +#define ib_data4(p1) \
  11782. + .long p1(0x3a),p1(0x91),p1(0x11),p1(0x41),p1(0x4f),p1(0x67),p1(0xdc),p1(0xea) ;\
  11783. + .long p1(0x97),p1(0xf2),p1(0xcf),p1(0xce),p1(0xf0),p1(0xb4),p1(0xe6),p1(0x73) ;\
  11784. + .long p1(0x96),p1(0xac),p1(0x74),p1(0x22),p1(0xe7),p1(0xad),p1(0x35),p1(0x85) ;\
  11785. + .long p1(0xe2),p1(0xf9),p1(0x37),p1(0xe8),p1(0x1c),p1(0x75),p1(0xdf),p1(0x6e)
  11786. +#define ib_data5(p1) \
  11787. + .long p1(0x47),p1(0xf1),p1(0x1a),p1(0x71),p1(0x1d),p1(0x29),p1(0xc5),p1(0x89) ;\
  11788. + .long p1(0x6f),p1(0xb7),p1(0x62),p1(0x0e),p1(0xaa),p1(0x18),p1(0xbe),p1(0x1b) ;\
  11789. + .long p1(0xfc),p1(0x56),p1(0x3e),p1(0x4b),p1(0xc6),p1(0xd2),p1(0x79),p1(0x20) ;\
  11790. + .long p1(0x9a),p1(0xdb),p1(0xc0),p1(0xfe),p1(0x78),p1(0xcd),p1(0x5a),p1(0xf4)
  11791. +#define ib_data6(p1) \
  11792. + .long p1(0x1f),p1(0xdd),p1(0xa8),p1(0x33),p1(0x88),p1(0x07),p1(0xc7),p1(0x31) ;\
  11793. + .long p1(0xb1),p1(0x12),p1(0x10),p1(0x59),p1(0x27),p1(0x80),p1(0xec),p1(0x5f) ;\
  11794. + .long p1(0x60),p1(0x51),p1(0x7f),p1(0xa9),p1(0x19),p1(0xb5),p1(0x4a),p1(0x0d) ;\
  11795. + .long p1(0x2d),p1(0xe5),p1(0x7a),p1(0x9f),p1(0x93),p1(0xc9),p1(0x9c),p1(0xef)
  11796. +#define ib_data7(p1) \
  11797. + .long p1(0xa0),p1(0xe0),p1(0x3b),p1(0x4d),p1(0xae),p1(0x2a),p1(0xf5),p1(0xb0) ;\
  11798. + .long p1(0xc8),p1(0xeb),p1(0xbb),p1(0x3c),p1(0x83),p1(0x53),p1(0x99),p1(0x61) ;\
  11799. + .long p1(0x17),p1(0x2b),p1(0x04),p1(0x7e),p1(0xba),p1(0x77),p1(0xd6),p1(0x26) ;\
  11800. + .long p1(0xe1),p1(0x69),p1(0x14),p1(0x63),p1(0x55),p1(0x21),p1(0x0c),p1(0x7d)
  11801. +
  11802. +// The rcon_table (needed for the key schedule)
  11803. +//
  11804. +// Here is original Dr Brian Gladman's source code:
  11805. +// _rcon_tab:
  11806. +// %assign x 1
  11807. +// %rep 29
  11808. +// dd x
  11809. +// %assign x f2(x)
  11810. +// %endrep
  11811. +//
  11812. +// Here is precomputed output (it's more portable this way):
  11813. +
  11814. + .align ALIGN32BYTES
  11815. +aes_rcon_tab:
  11816. + .long 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80
  11817. + .long 0x1b,0x36,0x6c,0xd8,0xab,0x4d,0x9a,0x2f
  11818. + .long 0x5e,0xbc,0x63,0xc6,0x97,0x35,0x6a,0xd4
  11819. + .long 0xb3,0x7d,0xfa,0xef,0xc5
  11820. +
  11821. +// The forward xor tables
  11822. +
  11823. + .align ALIGN32BYTES
  11824. +aes_ft_tab:
  11825. + sb_data0(u0)
  11826. + sb_data1(u0)
  11827. + sb_data2(u0)
  11828. + sb_data3(u0)
  11829. + sb_data4(u0)
  11830. + sb_data5(u0)
  11831. + sb_data6(u0)
  11832. + sb_data7(u0)
  11833. +
  11834. + sb_data0(u1)
  11835. + sb_data1(u1)
  11836. + sb_data2(u1)
  11837. + sb_data3(u1)
  11838. + sb_data4(u1)
  11839. + sb_data5(u1)
  11840. + sb_data6(u1)
  11841. + sb_data7(u1)
  11842. +
  11843. + sb_data0(u2)
  11844. + sb_data1(u2)
  11845. + sb_data2(u2)
  11846. + sb_data3(u2)
  11847. + sb_data4(u2)
  11848. + sb_data5(u2)
  11849. + sb_data6(u2)
  11850. + sb_data7(u2)
  11851. +
  11852. + sb_data0(u3)
  11853. + sb_data1(u3)
  11854. + sb_data2(u3)
  11855. + sb_data3(u3)
  11856. + sb_data4(u3)
  11857. + sb_data5(u3)
  11858. + sb_data6(u3)
  11859. + sb_data7(u3)
  11860. +
  11861. + .align ALIGN32BYTES
  11862. +aes_fl_tab:
  11863. + sb_data0(w0)
  11864. + sb_data1(w0)
  11865. + sb_data2(w0)
  11866. + sb_data3(w0)
  11867. + sb_data4(w0)
  11868. + sb_data5(w0)
  11869. + sb_data6(w0)
  11870. + sb_data7(w0)
  11871. +
  11872. + sb_data0(w1)
  11873. + sb_data1(w1)
  11874. + sb_data2(w1)
  11875. + sb_data3(w1)
  11876. + sb_data4(w1)
  11877. + sb_data5(w1)
  11878. + sb_data6(w1)
  11879. + sb_data7(w1)
  11880. +
  11881. + sb_data0(w2)
  11882. + sb_data1(w2)
  11883. + sb_data2(w2)
  11884. + sb_data3(w2)
  11885. + sb_data4(w2)
  11886. + sb_data5(w2)
  11887. + sb_data6(w2)
  11888. + sb_data7(w2)
  11889. +
  11890. + sb_data0(w3)
  11891. + sb_data1(w3)
  11892. + sb_data2(w3)
  11893. + sb_data3(w3)
  11894. + sb_data4(w3)
  11895. + sb_data5(w3)
  11896. + sb_data6(w3)
  11897. + sb_data7(w3)
  11898. +
  11899. +// The inverse xor tables
  11900. +
  11901. + .align ALIGN32BYTES
  11902. +aes_it_tab:
  11903. + ib_data0(v0)
  11904. + ib_data1(v0)
  11905. + ib_data2(v0)
  11906. + ib_data3(v0)
  11907. + ib_data4(v0)
  11908. + ib_data5(v0)
  11909. + ib_data6(v0)
  11910. + ib_data7(v0)
  11911. +
  11912. + ib_data0(v1)
  11913. + ib_data1(v1)
  11914. + ib_data2(v1)
  11915. + ib_data3(v1)
  11916. + ib_data4(v1)
  11917. + ib_data5(v1)
  11918. + ib_data6(v1)
  11919. + ib_data7(v1)
  11920. +
  11921. + ib_data0(v2)
  11922. + ib_data1(v2)
  11923. + ib_data2(v2)
  11924. + ib_data3(v2)
  11925. + ib_data4(v2)
  11926. + ib_data5(v2)
  11927. + ib_data6(v2)
  11928. + ib_data7(v2)
  11929. +
  11930. + ib_data0(v3)
  11931. + ib_data1(v3)
  11932. + ib_data2(v3)
  11933. + ib_data3(v3)
  11934. + ib_data4(v3)
  11935. + ib_data5(v3)
  11936. + ib_data6(v3)
  11937. + ib_data7(v3)
  11938. +
  11939. + .align ALIGN32BYTES
  11940. +aes_il_tab:
  11941. + ib_data0(w0)
  11942. + ib_data1(w0)
  11943. + ib_data2(w0)
  11944. + ib_data3(w0)
  11945. + ib_data4(w0)
  11946. + ib_data5(w0)
  11947. + ib_data6(w0)
  11948. + ib_data7(w0)
  11949. +
  11950. + ib_data0(w1)
  11951. + ib_data1(w1)
  11952. + ib_data2(w1)
  11953. + ib_data3(w1)
  11954. + ib_data4(w1)
  11955. + ib_data5(w1)
  11956. + ib_data6(w1)
  11957. + ib_data7(w1)
  11958. +
  11959. + ib_data0(w2)
  11960. + ib_data1(w2)
  11961. + ib_data2(w2)
  11962. + ib_data3(w2)
  11963. + ib_data4(w2)
  11964. + ib_data5(w2)
  11965. + ib_data6(w2)
  11966. + ib_data7(w2)
  11967. +
  11968. + ib_data0(w3)
  11969. + ib_data1(w3)
  11970. + ib_data2(w3)
  11971. + ib_data3(w3)
  11972. + ib_data4(w3)
  11973. + ib_data5(w3)
  11974. + ib_data6(w3)
  11975. + ib_data7(w3)
  11976. +
  11977. +// The inverse mix column tables
  11978. +
  11979. + .align ALIGN32BYTES
  11980. +aes_im_tab:
  11981. + im_data0(v0)
  11982. + im_data1(v0)
  11983. + im_data2(v0)
  11984. + im_data3(v0)
  11985. + im_data4(v0)
  11986. + im_data5(v0)
  11987. + im_data6(v0)
  11988. + im_data7(v0)
  11989. +
  11990. + im_data0(v1)
  11991. + im_data1(v1)
  11992. + im_data2(v1)
  11993. + im_data3(v1)
  11994. + im_data4(v1)
  11995. + im_data5(v1)
  11996. + im_data6(v1)
  11997. + im_data7(v1)
  11998. +
  11999. + im_data0(v2)
  12000. + im_data1(v2)
  12001. + im_data2(v2)
  12002. + im_data3(v2)
  12003. + im_data4(v2)
  12004. + im_data5(v2)
  12005. + im_data6(v2)
  12006. + im_data7(v2)
  12007. +
  12008. + im_data0(v3)
  12009. + im_data1(v3)
  12010. + im_data2(v3)
  12011. + im_data3(v3)
  12012. + im_data4(v3)
  12013. + im_data5(v3)
  12014. + im_data6(v3)
  12015. + im_data7(v3)
  12016. --- /dev/null Tue Mar 11 13:02:56 2003
  12017. +++ linux/net/ipsec/aes/aes.c Mon Feb 9 13:51:03 2004
  12018. @@ -0,0 +1,1415 @@
  12019. +// I retain copyright in this code but I encourage its free use provided
  12020. +// that I don't carry any responsibility for the results. I am especially
  12021. +// happy to see it used in free and open source software. If you do use
  12022. +// it I would appreciate an acknowledgement of its origin in the code or
  12023. +// the product that results and I would also appreciate knowing a little
  12024. +// about the use to which it is being put. I am grateful to Frank Yellin
  12025. +// for some ideas that are used in this implementation.
  12026. +//
  12027. +// Dr B. R. Gladman <brg@gladman.uk.net> 6th April 2001.
  12028. +//
  12029. +// This is an implementation of the AES encryption algorithm (Rijndael)
  12030. +// designed by Joan Daemen and Vincent Rijmen. This version is designed
  12031. +// to provide both fixed and dynamic block and key lengths and can also
  12032. +// run with either big or little endian internal byte order (see aes.h).
  12033. +// It inputs block and key lengths in bytes with the legal values being
  12034. +// 16, 24 and 32.
  12035. +
  12036. +/*
  12037. + * Modified by Jari Ruusu, May 1 2001
  12038. + * - Fixed some compile warnings, code was ok but gcc warned anyway.
  12039. + * - Changed basic types: byte -> unsigned char, word -> u_int32_t
  12040. + * - Major name space cleanup: Names visible to outside now begin
  12041. + * with "aes_" or "AES_". A lot of stuff moved from aes.h to aes.c
  12042. + * - Removed C++ and DLL support as part of name space cleanup.
  12043. + * - Eliminated unnecessary recomputation of tables. (actual bug fix)
  12044. + * - Merged precomputed constant tables to aes.c file.
  12045. + * - Removed data alignment restrictions for portability reasons.
  12046. + * - Made block and key lengths accept bit count (128/192/256)
  12047. + * as well byte count (16/24/32).
  12048. + * - Removed all error checks. This change also eliminated the need
  12049. + * to preinitialize the context struct to zero.
  12050. + * - Removed some totally unused constants.
  12051. + */
  12052. +
  12053. +#include "crypto/aes.h"
  12054. +
  12055. +// CONFIGURATION OPTIONS (see also aes.h)
  12056. +//
  12057. +// 1. Define UNROLL for full loop unrolling in encryption and decryption.
  12058. +// 2. Define PARTIAL_UNROLL to unroll two loops in encryption and decryption.
  12059. +// 3. Define FIXED_TABLES for compiled rather than dynamic tables.
  12060. +// 4. Define FF_TABLES to use tables for field multiplies and inverses.
  12061. +// Do not enable this without understanding stack space requirements.
  12062. +// 5. Define ARRAYS to use arrays to hold the local state block. If this
  12063. +// is not defined, individually declared 32-bit words are used.
  12064. +// 6. Define FAST_VARIABLE if a high speed variable block implementation
  12065. +// is needed (essentially three separate fixed block size code sequences)
  12066. +// 7. Define either ONE_TABLE or FOUR_TABLES for a fast table driven
  12067. +// version using 1 table (2 kbytes of table space) or 4 tables (8
  12068. +// kbytes of table space) for higher speed.
  12069. +// 8. Define either ONE_LR_TABLE or FOUR_LR_TABLES for a further speed
  12070. +// increase by using tables for the last rounds but with more table
  12071. +// space (2 or 8 kbytes extra).
  12072. +// 9. If neither ONE_TABLE nor FOUR_TABLES is defined, a compact but
  12073. +// slower version is provided.
  12074. +// 10. If fast decryption key scheduling is needed define ONE_IM_TABLE
  12075. +// or FOUR_IM_TABLES for higher speed (2 or 8 kbytes extra).
  12076. +
  12077. +#define UNROLL
  12078. +//#define PARTIAL_UNROLL
  12079. +
  12080. +#define FIXED_TABLES
  12081. +//#define FF_TABLES
  12082. +//#define ARRAYS
  12083. +#define FAST_VARIABLE
  12084. +
  12085. +//#define ONE_TABLE
  12086. +#define FOUR_TABLES
  12087. +
  12088. +//#define ONE_LR_TABLE
  12089. +#define FOUR_LR_TABLES
  12090. +
  12091. +//#define ONE_IM_TABLE
  12092. +#define FOUR_IM_TABLES
  12093. +
  12094. +#if defined(UNROLL) && defined (PARTIAL_UNROLL)
  12095. +#error both UNROLL and PARTIAL_UNROLL are defined
  12096. +#endif
  12097. +
  12098. +#if defined(ONE_TABLE) && defined (FOUR_TABLES)
  12099. +#error both ONE_TABLE and FOUR_TABLES are defined
  12100. +#endif
  12101. +
  12102. +#if defined(ONE_LR_TABLE) && defined (FOUR_LR_TABLES)
  12103. +#error both ONE_LR_TABLE and FOUR_LR_TABLES are defined
  12104. +#endif
  12105. +
  12106. +#if defined(ONE_IM_TABLE) && defined (FOUR_IM_TABLES)
  12107. +#error both ONE_IM_TABLE and FOUR_IM_TABLES are defined
  12108. +#endif
  12109. +
  12110. +#if defined(AES_BLOCK_SIZE) && AES_BLOCK_SIZE != 16 && AES_BLOCK_SIZE != 24 && AES_BLOCK_SIZE != 32
  12111. +#error an illegal block size has been specified
  12112. +#endif
  12113. +
  12114. +// upr(x,n): rotates bytes within words by n positions, moving bytes
  12115. +// to higher index positions with wrap around into low positions
  12116. +// ups(x,n): moves bytes by n positions to higher index positions in
  12117. +// words but without wrap around
  12118. +// bval(x,n): extracts a byte from a word
  12119. +
  12120. +#define upr(x,n) (((x) << 8 * (n)) | ((x) >> (32 - 8 * (n))))
  12121. +#define ups(x,n) ((x) << 8 * (n))
  12122. +#define bval(x,n) ((unsigned char)((x) >> 8 * (n)))
  12123. +#define bytes2word(b0, b1, b2, b3) \
  12124. + ((u_int32_t)(b3) << 24 | (u_int32_t)(b2) << 16 | (u_int32_t)(b1) << 8 | (b0))
  12125. +
  12126. +
  12127. +/* little endian processor without data alignment restrictions: AES_LE_OK */
  12128. +/* original code: i386 */
  12129. +#if defined(i386) || defined(_I386) || defined(__i386__) || defined(__i386)
  12130. +#define AES_LE_OK 1
  12131. +/* added (tested): alpha --jjo */
  12132. +#elif defined(__alpha__)|| defined (__alpha)
  12133. +#define AES_LE_OK 1
  12134. +/* added (tested): ia64 --jjo */
  12135. +#elif defined(__ia64__)|| defined (__ia64)
  12136. +#define AES_LE_OK 1
  12137. +#endif
  12138. +
  12139. +#ifdef AES_LE_OK
  12140. +/* little endian processor without data alignment restrictions */
  12141. +#define word_in(x) *(u_int32_t*)(x)
  12142. +#define const_word_in(x) *(const u_int32_t*)(x)
  12143. +#define word_out(x,v) *(u_int32_t*)(x) = (v)
  12144. +#define const_word_out(x,v) *(const u_int32_t*)(x) = (v)
  12145. +#else
  12146. +/* slower but generic big endian or with data alignment restrictions */
  12147. +/* some additional "const" touches to stop "gcc -Wcast-qual" complains --jjo */
  12148. +#define word_in(x) ((u_int32_t)(((unsigned char *)(x))[0])|((u_int32_t)(((unsigned char *)(x))[1])<<8)|((u_int32_t)(((unsigned char *)(x))[2])<<16)|((u_int32_t)(((unsigned char *)(x))[3])<<24))
  12149. +#define const_word_in(x) ((const u_int32_t)(((const unsigned char *)(x))[0])|((const u_int32_t)(((const unsigned char *)(x))[1])<<8)|((const u_int32_t)(((const unsigned char *)(x))[2])<<16)|((const u_int32_t)(((const unsigned char *)(x))[3])<<24))
  12150. +#define word_out(x,v) ((unsigned char *)(x))[0]=(v),((unsigned char *)(x))[1]=((v)>>8),((unsigned char *)(x))[2]=((v)>>16),((unsigned char *)(x))[3]=((v)>>24)
  12151. +#define const_word_out(x,v) ((const unsigned char *)(x))[0]=(v),((const unsigned char *)(x))[1]=((v)>>8),((const unsigned char *)(x))[2]=((v)>>16),((const unsigned char *)(x))[3]=((v)>>24)
  12152. +#endif
  12153. +
  12154. +// Disable at least some poor combinations of options
  12155. +
  12156. +#if !defined(ONE_TABLE) && !defined(FOUR_TABLES)
  12157. +#define FIXED_TABLES
  12158. +#undef UNROLL
  12159. +#undef ONE_LR_TABLE
  12160. +#undef FOUR_LR_TABLES
  12161. +#undef ONE_IM_TABLE
  12162. +#undef FOUR_IM_TABLES
  12163. +#elif !defined(FOUR_TABLES)
  12164. +#ifdef FOUR_LR_TABLES
  12165. +#undef FOUR_LR_TABLES
  12166. +#define ONE_LR_TABLE
  12167. +#endif
  12168. +#ifdef FOUR_IM_TABLES
  12169. +#undef FOUR_IM_TABLES
  12170. +#define ONE_IM_TABLE
  12171. +#endif
  12172. +#elif !defined(AES_BLOCK_SIZE)
  12173. +#if defined(UNROLL)
  12174. +#define PARTIAL_UNROLL
  12175. +#undef UNROLL
  12176. +#endif
  12177. +#endif
  12178. +
  12179. +// the finite field modular polynomial and elements
  12180. +
  12181. +#define ff_poly 0x011b
  12182. +#define ff_hi 0x80
  12183. +
  12184. +// multiply four bytes in GF(2^8) by 'x' {02} in parallel
  12185. +
  12186. +#define m1 0x80808080
  12187. +#define m2 0x7f7f7f7f
  12188. +#define m3 0x0000001b
  12189. +#define FFmulX(x) ((((x) & m2) << 1) ^ ((((x) & m1) >> 7) * m3))
  12190. +
  12191. +// The following defines provide alternative definitions of FFmulX that might
  12192. +// give improved performance if a fast 32-bit multiply is not available. Note
  12193. +// that a temporary variable u needs to be defined where FFmulX is used.
  12194. +
  12195. +// #define FFmulX(x) (u = (x) & m1, u |= (u >> 1), ((x) & m2) << 1) ^ ((u >> 3) | (u >> 6))
  12196. +// #define m4 0x1b1b1b1b
  12197. +// #define FFmulX(x) (u = (x) & m1, ((x) & m2) << 1) ^ ((u - (u >> 7)) & m4)
  12198. +
  12199. +// perform column mix operation on four bytes in parallel
  12200. +
  12201. +#define fwd_mcol(x) (f2 = FFmulX(x), f2 ^ upr(x ^ f2,3) ^ upr(x,2) ^ upr(x,1))
  12202. +
  12203. +#if defined(FIXED_TABLES)
  12204. +
  12205. +// the S-Box table
  12206. +
  12207. +static const unsigned char s_box[256] =
  12208. +{
  12209. + 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,
  12210. + 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
  12211. + 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
  12212. + 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
  12213. + 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc,
  12214. + 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
  12215. + 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a,
  12216. + 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
  12217. + 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
  12218. + 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
  12219. + 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b,
  12220. + 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
  12221. + 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85,
  12222. + 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
  12223. + 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
  12224. + 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
  12225. + 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17,
  12226. + 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
  12227. + 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88,
  12228. + 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
  12229. + 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c,
  12230. + 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
  12231. + 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9,
  12232. + 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
  12233. + 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6,
  12234. + 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
  12235. + 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
  12236. + 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
  12237. + 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94,
  12238. + 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
  12239. + 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68,
  12240. + 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
  12241. +};
  12242. +
  12243. +// the inverse S-Box table
  12244. +
  12245. +static const unsigned char inv_s_box[256] =
  12246. +{
  12247. + 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38,
  12248. + 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
  12249. + 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87,
  12250. + 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
  12251. + 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d,
  12252. + 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
  12253. + 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2,
  12254. + 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
  12255. + 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16,
  12256. + 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
  12257. + 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda,
  12258. + 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
  12259. + 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a,
  12260. + 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
  12261. + 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02,
  12262. + 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
  12263. + 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea,
  12264. + 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
  12265. + 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85,
  12266. + 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
  12267. + 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89,
  12268. + 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
  12269. + 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20,
  12270. + 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
  12271. + 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31,
  12272. + 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
  12273. + 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d,
  12274. + 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
  12275. + 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0,
  12276. + 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
  12277. + 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26,
  12278. + 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
  12279. +};
  12280. +
  12281. +#define w0(p) 0x000000##p
  12282. +
  12283. +// Number of elements required in this table for different
  12284. +// block and key lengths is:
  12285. +//
  12286. +// Nk = 4 6 8
  12287. +// ----------
  12288. +// Nb = 4 | 10 8 7
  12289. +// 6 | 19 12 11
  12290. +// 8 | 29 19 14
  12291. +//
  12292. +// this table can be a table of bytes if the key schedule
  12293. +// code is adjusted accordingly
  12294. +
  12295. +static const u_int32_t rcon_tab[29] =
  12296. +{
  12297. + w0(01), w0(02), w0(04), w0(08),
  12298. + w0(10), w0(20), w0(40), w0(80),
  12299. + w0(1b), w0(36), w0(6c), w0(d8),
  12300. + w0(ab), w0(4d), w0(9a), w0(2f),
  12301. + w0(5e), w0(bc), w0(63), w0(c6),
  12302. + w0(97), w0(35), w0(6a), w0(d4),
  12303. + w0(b3), w0(7d), w0(fa), w0(ef),
  12304. + w0(c5)
  12305. +};
  12306. +
  12307. +#undef w0
  12308. +
  12309. +#define r0(p,q,r,s) 0x##p##q##r##s
  12310. +#define r1(p,q,r,s) 0x##q##r##s##p
  12311. +#define r2(p,q,r,s) 0x##r##s##p##q
  12312. +#define r3(p,q,r,s) 0x##s##p##q##r
  12313. +#define w0(p) 0x000000##p
  12314. +#define w1(p) 0x0000##p##00
  12315. +#define w2(p) 0x00##p##0000
  12316. +#define w3(p) 0x##p##000000
  12317. +
  12318. +#if defined(FIXED_TABLES) && (defined(ONE_TABLE) || defined(FOUR_TABLES))
  12319. +
  12320. +// data for forward tables (other than last round)
  12321. +
  12322. +#define f_table \
  12323. + r(a5,63,63,c6), r(84,7c,7c,f8), r(99,77,77,ee), r(8d,7b,7b,f6),\
  12324. + r(0d,f2,f2,ff), r(bd,6b,6b,d6), r(b1,6f,6f,de), r(54,c5,c5,91),\
  12325. + r(50,30,30,60), r(03,01,01,02), r(a9,67,67,ce), r(7d,2b,2b,56),\
  12326. + r(19,fe,fe,e7), r(62,d7,d7,b5), r(e6,ab,ab,4d), r(9a,76,76,ec),\
  12327. + r(45,ca,ca,8f), r(9d,82,82,1f), r(40,c9,c9,89), r(87,7d,7d,fa),\
  12328. + r(15,fa,fa,ef), r(eb,59,59,b2), r(c9,47,47,8e), r(0b,f0,f0,fb),\
  12329. + r(ec,ad,ad,41), r(67,d4,d4,b3), r(fd,a2,a2,5f), r(ea,af,af,45),\
  12330. + r(bf,9c,9c,23), r(f7,a4,a4,53), r(96,72,72,e4), r(5b,c0,c0,9b),\
  12331. + r(c2,b7,b7,75), r(1c,fd,fd,e1), r(ae,93,93,3d), r(6a,26,26,4c),\
  12332. + r(5a,36,36,6c), r(41,3f,3f,7e), r(02,f7,f7,f5), r(4f,cc,cc,83),\
  12333. + r(5c,34,34,68), r(f4,a5,a5,51), r(34,e5,e5,d1), r(08,f1,f1,f9),\
  12334. + r(93,71,71,e2), r(73,d8,d8,ab), r(53,31,31,62), r(3f,15,15,2a),\
  12335. + r(0c,04,04,08), r(52,c7,c7,95), r(65,23,23,46), r(5e,c3,c3,9d),\
  12336. + r(28,18,18,30), r(a1,96,96,37), r(0f,05,05,0a), r(b5,9a,9a,2f),\
  12337. + r(09,07,07,0e), r(36,12,12,24), r(9b,80,80,1b), r(3d,e2,e2,df),\
  12338. + r(26,eb,eb,cd), r(69,27,27,4e), r(cd,b2,b2,7f), r(9f,75,75,ea),\
  12339. + r(1b,09,09,12), r(9e,83,83,1d), r(74,2c,2c,58), r(2e,1a,1a,34),\
  12340. + r(2d,1b,1b,36), r(b2,6e,6e,dc), r(ee,5a,5a,b4), r(fb,a0,a0,5b),\
  12341. + r(f6,52,52,a4), r(4d,3b,3b,76), r(61,d6,d6,b7), r(ce,b3,b3,7d),\
  12342. + r(7b,29,29,52), r(3e,e3,e3,dd), r(71,2f,2f,5e), r(97,84,84,13),\
  12343. + r(f5,53,53,a6), r(68,d1,d1,b9), r(00,00,00,00), r(2c,ed,ed,c1),\
  12344. + r(60,20,20,40), r(1f,fc,fc,e3), r(c8,b1,b1,79), r(ed,5b,5b,b6),\
  12345. + r(be,6a,6a,d4), r(46,cb,cb,8d), r(d9,be,be,67), r(4b,39,39,72),\
  12346. + r(de,4a,4a,94), r(d4,4c,4c,98), r(e8,58,58,b0), r(4a,cf,cf,85),\
  12347. + r(6b,d0,d0,bb), r(2a,ef,ef,c5), r(e5,aa,aa,4f), r(16,fb,fb,ed),\
  12348. + r(c5,43,43,86), r(d7,4d,4d,9a), r(55,33,33,66), r(94,85,85,11),\
  12349. + r(cf,45,45,8a), r(10,f9,f9,e9), r(06,02,02,04), r(81,7f,7f,fe),\
  12350. + r(f0,50,50,a0), r(44,3c,3c,78), r(ba,9f,9f,25), r(e3,a8,a8,4b),\
  12351. + r(f3,51,51,a2), r(fe,a3,a3,5d), r(c0,40,40,80), r(8a,8f,8f,05),\
  12352. + r(ad,92,92,3f), r(bc,9d,9d,21), r(48,38,38,70), r(04,f5,f5,f1),\
  12353. + r(df,bc,bc,63), r(c1,b6,b6,77), r(75,da,da,af), r(63,21,21,42),\
  12354. + r(30,10,10,20), r(1a,ff,ff,e5), r(0e,f3,f3,fd), r(6d,d2,d2,bf),\
  12355. + r(4c,cd,cd,81), r(14,0c,0c,18), r(35,13,13,26), r(2f,ec,ec,c3),\
  12356. + r(e1,5f,5f,be), r(a2,97,97,35), r(cc,44,44,88), r(39,17,17,2e),\
  12357. + r(57,c4,c4,93), r(f2,a7,a7,55), r(82,7e,7e,fc), r(47,3d,3d,7a),\
  12358. + r(ac,64,64,c8), r(e7,5d,5d,ba), r(2b,19,19,32), r(95,73,73,e6),\
  12359. + r(a0,60,60,c0), r(98,81,81,19), r(d1,4f,4f,9e), r(7f,dc,dc,a3),\
  12360. + r(66,22,22,44), r(7e,2a,2a,54), r(ab,90,90,3b), r(83,88,88,0b),\
  12361. + r(ca,46,46,8c), r(29,ee,ee,c7), r(d3,b8,b8,6b), r(3c,14,14,28),\
  12362. + r(79,de,de,a7), r(e2,5e,5e,bc), r(1d,0b,0b,16), r(76,db,db,ad),\
  12363. + r(3b,e0,e0,db), r(56,32,32,64), r(4e,3a,3a,74), r(1e,0a,0a,14),\
  12364. + r(db,49,49,92), r(0a,06,06,0c), r(6c,24,24,48), r(e4,5c,5c,b8),\
  12365. + r(5d,c2,c2,9f), r(6e,d3,d3,bd), r(ef,ac,ac,43), r(a6,62,62,c4),\
  12366. + r(a8,91,91,39), r(a4,95,95,31), r(37,e4,e4,d3), r(8b,79,79,f2),\
  12367. + r(32,e7,e7,d5), r(43,c8,c8,8b), r(59,37,37,6e), r(b7,6d,6d,da),\
  12368. + r(8c,8d,8d,01), r(64,d5,d5,b1), r(d2,4e,4e,9c), r(e0,a9,a9,49),\
  12369. + r(b4,6c,6c,d8), r(fa,56,56,ac), r(07,f4,f4,f3), r(25,ea,ea,cf),\
  12370. + r(af,65,65,ca), r(8e,7a,7a,f4), r(e9,ae,ae,47), r(18,08,08,10),\
  12371. + r(d5,ba,ba,6f), r(88,78,78,f0), r(6f,25,25,4a), r(72,2e,2e,5c),\
  12372. + r(24,1c,1c,38), r(f1,a6,a6,57), r(c7,b4,b4,73), r(51,c6,c6,97),\
  12373. + r(23,e8,e8,cb), r(7c,dd,dd,a1), r(9c,74,74,e8), r(21,1f,1f,3e),\
  12374. + r(dd,4b,4b,96), r(dc,bd,bd,61), r(86,8b,8b,0d), r(85,8a,8a,0f),\
  12375. + r(90,70,70,e0), r(42,3e,3e,7c), r(c4,b5,b5,71), r(aa,66,66,cc),\
  12376. + r(d8,48,48,90), r(05,03,03,06), r(01,f6,f6,f7), r(12,0e,0e,1c),\
  12377. + r(a3,61,61,c2), r(5f,35,35,6a), r(f9,57,57,ae), r(d0,b9,b9,69),\
  12378. + r(91,86,86,17), r(58,c1,c1,99), r(27,1d,1d,3a), r(b9,9e,9e,27),\
  12379. + r(38,e1,e1,d9), r(13,f8,f8,eb), r(b3,98,98,2b), r(33,11,11,22),\
  12380. + r(bb,69,69,d2), r(70,d9,d9,a9), r(89,8e,8e,07), r(a7,94,94,33),\
  12381. + r(b6,9b,9b,2d), r(22,1e,1e,3c), r(92,87,87,15), r(20,e9,e9,c9),\
  12382. + r(49,ce,ce,87), r(ff,55,55,aa), r(78,28,28,50), r(7a,df,df,a5),\
  12383. + r(8f,8c,8c,03), r(f8,a1,a1,59), r(80,89,89,09), r(17,0d,0d,1a),\
  12384. + r(da,bf,bf,65), r(31,e6,e6,d7), r(c6,42,42,84), r(b8,68,68,d0),\
  12385. + r(c3,41,41,82), r(b0,99,99,29), r(77,2d,2d,5a), r(11,0f,0f,1e),\
  12386. + r(cb,b0,b0,7b), r(fc,54,54,a8), r(d6,bb,bb,6d), r(3a,16,16,2c)
  12387. +
  12388. +// data for inverse tables (other than last round)
  12389. +
  12390. +#define i_table \
  12391. + r(50,a7,f4,51), r(53,65,41,7e), r(c3,a4,17,1a), r(96,5e,27,3a),\
  12392. + r(cb,6b,ab,3b), r(f1,45,9d,1f), r(ab,58,fa,ac), r(93,03,e3,4b),\
  12393. + r(55,fa,30,20), r(f6,6d,76,ad), r(91,76,cc,88), r(25,4c,02,f5),\
  12394. + r(fc,d7,e5,4f), r(d7,cb,2a,c5), r(80,44,35,26), r(8f,a3,62,b5),\
  12395. + r(49,5a,b1,de), r(67,1b,ba,25), r(98,0e,ea,45), r(e1,c0,fe,5d),\
  12396. + r(02,75,2f,c3), r(12,f0,4c,81), r(a3,97,46,8d), r(c6,f9,d3,6b),\
  12397. + r(e7,5f,8f,03), r(95,9c,92,15), r(eb,7a,6d,bf), r(da,59,52,95),\
  12398. + r(2d,83,be,d4), r(d3,21,74,58), r(29,69,e0,49), r(44,c8,c9,8e),\
  12399. + r(6a,89,c2,75), r(78,79,8e,f4), r(6b,3e,58,99), r(dd,71,b9,27),\
  12400. + r(b6,4f,e1,be), r(17,ad,88,f0), r(66,ac,20,c9), r(b4,3a,ce,7d),\
  12401. + r(18,4a,df,63), r(82,31,1a,e5), r(60,33,51,97), r(45,7f,53,62),\
  12402. + r(e0,77,64,b1), r(84,ae,6b,bb), r(1c,a0,81,fe), r(94,2b,08,f9),\
  12403. + r(58,68,48,70), r(19,fd,45,8f), r(87,6c,de,94), r(b7,f8,7b,52),\
  12404. + r(23,d3,73,ab), r(e2,02,4b,72), r(57,8f,1f,e3), r(2a,ab,55,66),\
  12405. + r(07,28,eb,b2), r(03,c2,b5,2f), r(9a,7b,c5,86), r(a5,08,37,d3),\
  12406. + r(f2,87,28,30), r(b2,a5,bf,23), r(ba,6a,03,02), r(5c,82,16,ed),\
  12407. + r(2b,1c,cf,8a), r(92,b4,79,a7), r(f0,f2,07,f3), r(a1,e2,69,4e),\
  12408. + r(cd,f4,da,65), r(d5,be,05,06), r(1f,62,34,d1), r(8a,fe,a6,c4),\
  12409. + r(9d,53,2e,34), r(a0,55,f3,a2), r(32,e1,8a,05), r(75,eb,f6,a4),\
  12410. + r(39,ec,83,0b), r(aa,ef,60,40), r(06,9f,71,5e), r(51,10,6e,bd),\
  12411. + r(f9,8a,21,3e), r(3d,06,dd,96), r(ae,05,3e,dd), r(46,bd,e6,4d),\
  12412. + r(b5,8d,54,91), r(05,5d,c4,71), r(6f,d4,06,04), r(ff,15,50,60),\
  12413. + r(24,fb,98,19), r(97,e9,bd,d6), r(cc,43,40,89), r(77,9e,d9,67),\
  12414. + r(bd,42,e8,b0), r(88,8b,89,07), r(38,5b,19,e7), r(db,ee,c8,79),\
  12415. + r(47,0a,7c,a1), r(e9,0f,42,7c), r(c9,1e,84,f8), r(00,00,00,00),\
  12416. + r(83,86,80,09), r(48,ed,2b,32), r(ac,70,11,1e), r(4e,72,5a,6c),\
  12417. + r(fb,ff,0e,fd), r(56,38,85,0f), r(1e,d5,ae,3d), r(27,39,2d,36),\
  12418. + r(64,d9,0f,0a), r(21,a6,5c,68), r(d1,54,5b,9b), r(3a,2e,36,24),\
  12419. + r(b1,67,0a,0c), r(0f,e7,57,93), r(d2,96,ee,b4), r(9e,91,9b,1b),\
  12420. + r(4f,c5,c0,80), r(a2,20,dc,61), r(69,4b,77,5a), r(16,1a,12,1c),\
  12421. + r(0a,ba,93,e2), r(e5,2a,a0,c0), r(43,e0,22,3c), r(1d,17,1b,12),\
  12422. + r(0b,0d,09,0e), r(ad,c7,8b,f2), r(b9,a8,b6,2d), r(c8,a9,1e,14),\
  12423. + r(85,19,f1,57), r(4c,07,75,af), r(bb,dd,99,ee), r(fd,60,7f,a3),\
  12424. + r(9f,26,01,f7), r(bc,f5,72,5c), r(c5,3b,66,44), r(34,7e,fb,5b),\
  12425. + r(76,29,43,8b), r(dc,c6,23,cb), r(68,fc,ed,b6), r(63,f1,e4,b8),\
  12426. + r(ca,dc,31,d7), r(10,85,63,42), r(40,22,97,13), r(20,11,c6,84),\
  12427. + r(7d,24,4a,85), r(f8,3d,bb,d2), r(11,32,f9,ae), r(6d,a1,29,c7),\
  12428. + r(4b,2f,9e,1d), r(f3,30,b2,dc), r(ec,52,86,0d), r(d0,e3,c1,77),\
  12429. + r(6c,16,b3,2b), r(99,b9,70,a9), r(fa,48,94,11), r(22,64,e9,47),\
  12430. + r(c4,8c,fc,a8), r(1a,3f,f0,a0), r(d8,2c,7d,56), r(ef,90,33,22),\
  12431. + r(c7,4e,49,87), r(c1,d1,38,d9), r(fe,a2,ca,8c), r(36,0b,d4,98),\
  12432. + r(cf,81,f5,a6), r(28,de,7a,a5), r(26,8e,b7,da), r(a4,bf,ad,3f),\
  12433. + r(e4,9d,3a,2c), r(0d,92,78,50), r(9b,cc,5f,6a), r(62,46,7e,54),\
  12434. + r(c2,13,8d,f6), r(e8,b8,d8,90), r(5e,f7,39,2e), r(f5,af,c3,82),\
  12435. + r(be,80,5d,9f), r(7c,93,d0,69), r(a9,2d,d5,6f), r(b3,12,25,cf),\
  12436. + r(3b,99,ac,c8), r(a7,7d,18,10), r(6e,63,9c,e8), r(7b,bb,3b,db),\
  12437. + r(09,78,26,cd), r(f4,18,59,6e), r(01,b7,9a,ec), r(a8,9a,4f,83),\
  12438. + r(65,6e,95,e6), r(7e,e6,ff,aa), r(08,cf,bc,21), r(e6,e8,15,ef),\
  12439. + r(d9,9b,e7,ba), r(ce,36,6f,4a), r(d4,09,9f,ea), r(d6,7c,b0,29),\
  12440. + r(af,b2,a4,31), r(31,23,3f,2a), r(30,94,a5,c6), r(c0,66,a2,35),\
  12441. + r(37,bc,4e,74), r(a6,ca,82,fc), r(b0,d0,90,e0), r(15,d8,a7,33),\
  12442. + r(4a,98,04,f1), r(f7,da,ec,41), r(0e,50,cd,7f), r(2f,f6,91,17),\
  12443. + r(8d,d6,4d,76), r(4d,b0,ef,43), r(54,4d,aa,cc), r(df,04,96,e4),\
  12444. + r(e3,b5,d1,9e), r(1b,88,6a,4c), r(b8,1f,2c,c1), r(7f,51,65,46),\
  12445. + r(04,ea,5e,9d), r(5d,35,8c,01), r(73,74,87,fa), r(2e,41,0b,fb),\
  12446. + r(5a,1d,67,b3), r(52,d2,db,92), r(33,56,10,e9), r(13,47,d6,6d),\
  12447. + r(8c,61,d7,9a), r(7a,0c,a1,37), r(8e,14,f8,59), r(89,3c,13,eb),\
  12448. + r(ee,27,a9,ce), r(35,c9,61,b7), r(ed,e5,1c,e1), r(3c,b1,47,7a),\
  12449. + r(59,df,d2,9c), r(3f,73,f2,55), r(79,ce,14,18), r(bf,37,c7,73),\
  12450. + r(ea,cd,f7,53), r(5b,aa,fd,5f), r(14,6f,3d,df), r(86,db,44,78),\
  12451. + r(81,f3,af,ca), r(3e,c4,68,b9), r(2c,34,24,38), r(5f,40,a3,c2),\
  12452. + r(72,c3,1d,16), r(0c,25,e2,bc), r(8b,49,3c,28), r(41,95,0d,ff),\
  12453. + r(71,01,a8,39), r(de,b3,0c,08), r(9c,e4,b4,d8), r(90,c1,56,64),\
  12454. + r(61,84,cb,7b), r(70,b6,32,d5), r(74,5c,6c,48), r(42,57,b8,d0)
  12455. +
  12456. +// generate the required tables in the desired endian format
  12457. +
  12458. +#undef r
  12459. +#define r r0
  12460. +
  12461. +#if defined(ONE_TABLE)
  12462. +static const u_int32_t ft_tab[256] =
  12463. + { f_table };
  12464. +#elif defined(FOUR_TABLES)
  12465. +static const u_int32_t ft_tab[4][256] =
  12466. +{ { f_table },
  12467. +#undef r
  12468. +#define r r1
  12469. + { f_table },
  12470. +#undef r
  12471. +#define r r2
  12472. + { f_table },
  12473. +#undef r
  12474. +#define r r3
  12475. + { f_table }
  12476. +};
  12477. +#endif
  12478. +
  12479. +#undef r
  12480. +#define r r0
  12481. +#if defined(ONE_TABLE)
  12482. +static const u_int32_t it_tab[256] =
  12483. + { i_table };
  12484. +#elif defined(FOUR_TABLES)
  12485. +static const u_int32_t it_tab[4][256] =
  12486. +{ { i_table },
  12487. +#undef r
  12488. +#define r r1
  12489. + { i_table },
  12490. +#undef r
  12491. +#define r r2
  12492. + { i_table },
  12493. +#undef r
  12494. +#define r r3
  12495. + { i_table }
  12496. +};
  12497. +#endif
  12498. +
  12499. +#endif
  12500. +
  12501. +#if defined(FIXED_TABLES) && (defined(ONE_LR_TABLE) || defined(FOUR_LR_TABLES))
  12502. +
  12503. +// data for inverse tables (last round)
  12504. +
  12505. +#define li_table \
  12506. + w(52), w(09), w(6a), w(d5), w(30), w(36), w(a5), w(38),\
  12507. + w(bf), w(40), w(a3), w(9e), w(81), w(f3), w(d7), w(fb),\
  12508. + w(7c), w(e3), w(39), w(82), w(9b), w(2f), w(ff), w(87),\
  12509. + w(34), w(8e), w(43), w(44), w(c4), w(de), w(e9), w(cb),\
  12510. + w(54), w(7b), w(94), w(32), w(a6), w(c2), w(23), w(3d),\
  12511. + w(ee), w(4c), w(95), w(0b), w(42), w(fa), w(c3), w(4e),\
  12512. + w(08), w(2e), w(a1), w(66), w(28), w(d9), w(24), w(b2),\
  12513. + w(76), w(5b), w(a2), w(49), w(6d), w(8b), w(d1), w(25),\
  12514. + w(72), w(f8), w(f6), w(64), w(86), w(68), w(98), w(16),\
  12515. + w(d4), w(a4), w(5c), w(cc), w(5d), w(65), w(b6), w(92),\
  12516. + w(6c), w(70), w(48), w(50), w(fd), w(ed), w(b9), w(da),\
  12517. + w(5e), w(15), w(46), w(57), w(a7), w(8d), w(9d), w(84),\
  12518. + w(90), w(d8), w(ab), w(00), w(8c), w(bc), w(d3), w(0a),\
  12519. + w(f7), w(e4), w(58), w(05), w(b8), w(b3), w(45), w(06),\
  12520. + w(d0), w(2c), w(1e), w(8f), w(ca), w(3f), w(0f), w(02),\
  12521. + w(c1), w(af), w(bd), w(03), w(01), w(13), w(8a), w(6b),\
  12522. + w(3a), w(91), w(11), w(41), w(4f), w(67), w(dc), w(ea),\
  12523. + w(97), w(f2), w(cf), w(ce), w(f0), w(b4), w(e6), w(73),\
  12524. + w(96), w(ac), w(74), w(22), w(e7), w(ad), w(35), w(85),\
  12525. + w(e2), w(f9), w(37), w(e8), w(1c), w(75), w(df), w(6e),\
  12526. + w(47), w(f1), w(1a), w(71), w(1d), w(29), w(c5), w(89),\
  12527. + w(6f), w(b7), w(62), w(0e), w(aa), w(18), w(be), w(1b),\
  12528. + w(fc), w(56), w(3e), w(4b), w(c6), w(d2), w(79), w(20),\
  12529. + w(9a), w(db), w(c0), w(fe), w(78), w(cd), w(5a), w(f4),\
  12530. + w(1f), w(dd), w(a8), w(33), w(88), w(07), w(c7), w(31),\
  12531. + w(b1), w(12), w(10), w(59), w(27), w(80), w(ec), w(5f),\
  12532. + w(60), w(51), w(7f), w(a9), w(19), w(b5), w(4a), w(0d),\
  12533. + w(2d), w(e5), w(7a), w(9f), w(93), w(c9), w(9c), w(ef),\
  12534. + w(a0), w(e0), w(3b), w(4d), w(ae), w(2a), w(f5), w(b0),\
  12535. + w(c8), w(eb), w(bb), w(3c), w(83), w(53), w(99), w(61),\
  12536. + w(17), w(2b), w(04), w(7e), w(ba), w(77), w(d6), w(26),\
  12537. + w(e1), w(69), w(14), w(63), w(55), w(21), w(0c), w(7d),
  12538. +
  12539. +// generate the required tables in the desired endian format
  12540. +
  12541. +#undef r
  12542. +#define r(p,q,r,s) w0(q)
  12543. +#if defined(ONE_LR_TABLE)
  12544. +static const u_int32_t fl_tab[256] =
  12545. + { f_table };
  12546. +#elif defined(FOUR_LR_TABLES)
  12547. +static const u_int32_t fl_tab[4][256] =
  12548. +{ { f_table },
  12549. +#undef r
  12550. +#define r(p,q,r,s) w1(q)
  12551. + { f_table },
  12552. +#undef r
  12553. +#define r(p,q,r,s) w2(q)
  12554. + { f_table },
  12555. +#undef r
  12556. +#define r(p,q,r,s) w3(q)
  12557. + { f_table }
  12558. +};
  12559. +#endif
  12560. +
  12561. +#undef w
  12562. +#define w w0
  12563. +#if defined(ONE_LR_TABLE)
  12564. +static const u_int32_t il_tab[256] =
  12565. + { li_table };
  12566. +#elif defined(FOUR_LR_TABLES)
  12567. +static const u_int32_t il_tab[4][256] =
  12568. +{ { li_table },
  12569. +#undef w
  12570. +#define w w1
  12571. + { li_table },
  12572. +#undef w
  12573. +#define w w2
  12574. + { li_table },
  12575. +#undef w
  12576. +#define w w3
  12577. + { li_table }
  12578. +};
  12579. +#endif
  12580. +
  12581. +#endif
  12582. +
  12583. +#if defined(FIXED_TABLES) && (defined(ONE_IM_TABLE) || defined(FOUR_IM_TABLES))
  12584. +
  12585. +#define m_table \
  12586. + r(00,00,00,00), r(0b,0d,09,0e), r(16,1a,12,1c), r(1d,17,1b,12),\
  12587. + r(2c,34,24,38), r(27,39,2d,36), r(3a,2e,36,24), r(31,23,3f,2a),\
  12588. + r(58,68,48,70), r(53,65,41,7e), r(4e,72,5a,6c), r(45,7f,53,62),\
  12589. + r(74,5c,6c,48), r(7f,51,65,46), r(62,46,7e,54), r(69,4b,77,5a),\
  12590. + r(b0,d0,90,e0), r(bb,dd,99,ee), r(a6,ca,82,fc), r(ad,c7,8b,f2),\
  12591. + r(9c,e4,b4,d8), r(97,e9,bd,d6), r(8a,fe,a6,c4), r(81,f3,af,ca),\
  12592. + r(e8,b8,d8,90), r(e3,b5,d1,9e), r(fe,a2,ca,8c), r(f5,af,c3,82),\
  12593. + r(c4,8c,fc,a8), r(cf,81,f5,a6), r(d2,96,ee,b4), r(d9,9b,e7,ba),\
  12594. + r(7b,bb,3b,db), r(70,b6,32,d5), r(6d,a1,29,c7), r(66,ac,20,c9),\
  12595. + r(57,8f,1f,e3), r(5c,82,16,ed), r(41,95,0d,ff), r(4a,98,04,f1),\
  12596. + r(23,d3,73,ab), r(28,de,7a,a5), r(35,c9,61,b7), r(3e,c4,68,b9),\
  12597. + r(0f,e7,57,93), r(04,ea,5e,9d), r(19,fd,45,8f), r(12,f0,4c,81),\
  12598. + r(cb,6b,ab,3b), r(c0,66,a2,35), r(dd,71,b9,27), r(d6,7c,b0,29),\
  12599. + r(e7,5f,8f,03), r(ec,52,86,0d), r(f1,45,9d,1f), r(fa,48,94,11),\
  12600. + r(93,03,e3,4b), r(98,0e,ea,45), r(85,19,f1,57), r(8e,14,f8,59),\
  12601. + r(bf,37,c7,73), r(b4,3a,ce,7d), r(a9,2d,d5,6f), r(a2,20,dc,61),\
  12602. + r(f6,6d,76,ad), r(fd,60,7f,a3), r(e0,77,64,b1), r(eb,7a,6d,bf),\
  12603. + r(da,59,52,95), r(d1,54,5b,9b), r(cc,43,40,89), r(c7,4e,49,87),\
  12604. + r(ae,05,3e,dd), r(a5,08,37,d3), r(b8,1f,2c,c1), r(b3,12,25,cf),\
  12605. + r(82,31,1a,e5), r(89,3c,13,eb), r(94,2b,08,f9), r(9f,26,01,f7),\
  12606. + r(46,bd,e6,4d), r(4d,b0,ef,43), r(50,a7,f4,51), r(5b,aa,fd,5f),\
  12607. + r(6a,89,c2,75), r(61,84,cb,7b), r(7c,93,d0,69), r(77,9e,d9,67),\
  12608. + r(1e,d5,ae,3d), r(15,d8,a7,33), r(08,cf,bc,21), r(03,c2,b5,2f),\
  12609. + r(32,e1,8a,05), r(39,ec,83,0b), r(24,fb,98,19), r(2f,f6,91,17),\
  12610. + r(8d,d6,4d,76), r(86,db,44,78), r(9b,cc,5f,6a), r(90,c1,56,64),\
  12611. + r(a1,e2,69,4e), r(aa,ef,60,40), r(b7,f8,7b,52), r(bc,f5,72,5c),\
  12612. + r(d5,be,05,06), r(de,b3,0c,08), r(c3,a4,17,1a), r(c8,a9,1e,14),\
  12613. + r(f9,8a,21,3e), r(f2,87,28,30), r(ef,90,33,22), r(e4,9d,3a,2c),\
  12614. + r(3d,06,dd,96), r(36,0b,d4,98), r(2b,1c,cf,8a), r(20,11,c6,84),\
  12615. + r(11,32,f9,ae), r(1a,3f,f0,a0), r(07,28,eb,b2), r(0c,25,e2,bc),\
  12616. + r(65,6e,95,e6), r(6e,63,9c,e8), r(73,74,87,fa), r(78,79,8e,f4),\
  12617. + r(49,5a,b1,de), r(42,57,b8,d0), r(5f,40,a3,c2), r(54,4d,aa,cc),\
  12618. + r(f7,da,ec,41), r(fc,d7,e5,4f), r(e1,c0,fe,5d), r(ea,cd,f7,53),\
  12619. + r(db,ee,c8,79), r(d0,e3,c1,77), r(cd,f4,da,65), r(c6,f9,d3,6b),\
  12620. + r(af,b2,a4,31), r(a4,bf,ad,3f), r(b9,a8,b6,2d), r(b2,a5,bf,23),\
  12621. + r(83,86,80,09), r(88,8b,89,07), r(95,9c,92,15), r(9e,91,9b,1b),\
  12622. + r(47,0a,7c,a1), r(4c,07,75,af), r(51,10,6e,bd), r(5a,1d,67,b3),\
  12623. + r(6b,3e,58,99), r(60,33,51,97), r(7d,24,4a,85), r(76,29,43,8b),\
  12624. + r(1f,62,34,d1), r(14,6f,3d,df), r(09,78,26,cd), r(02,75,2f,c3),\
  12625. + r(33,56,10,e9), r(38,5b,19,e7), r(25,4c,02,f5), r(2e,41,0b,fb),\
  12626. + r(8c,61,d7,9a), r(87,6c,de,94), r(9a,7b,c5,86), r(91,76,cc,88),\
  12627. + r(a0,55,f3,a2), r(ab,58,fa,ac), r(b6,4f,e1,be), r(bd,42,e8,b0),\
  12628. + r(d4,09,9f,ea), r(df,04,96,e4), r(c2,13,8d,f6), r(c9,1e,84,f8),\
  12629. + r(f8,3d,bb,d2), r(f3,30,b2,dc), r(ee,27,a9,ce), r(e5,2a,a0,c0),\
  12630. + r(3c,b1,47,7a), r(37,bc,4e,74), r(2a,ab,55,66), r(21,a6,5c,68),\
  12631. + r(10,85,63,42), r(1b,88,6a,4c), r(06,9f,71,5e), r(0d,92,78,50),\
  12632. + r(64,d9,0f,0a), r(6f,d4,06,04), r(72,c3,1d,16), r(79,ce,14,18),\
  12633. + r(48,ed,2b,32), r(43,e0,22,3c), r(5e,f7,39,2e), r(55,fa,30,20),\
  12634. + r(01,b7,9a,ec), r(0a,ba,93,e2), r(17,ad,88,f0), r(1c,a0,81,fe),\
  12635. + r(2d,83,be,d4), r(26,8e,b7,da), r(3b,99,ac,c8), r(30,94,a5,c6),\
  12636. + r(59,df,d2,9c), r(52,d2,db,92), r(4f,c5,c0,80), r(44,c8,c9,8e),\
  12637. + r(75,eb,f6,a4), r(7e,e6,ff,aa), r(63,f1,e4,b8), r(68,fc,ed,b6),\
  12638. + r(b1,67,0a,0c), r(ba,6a,03,02), r(a7,7d,18,10), r(ac,70,11,1e),\
  12639. + r(9d,53,2e,34), r(96,5e,27,3a), r(8b,49,3c,28), r(80,44,35,26),\
  12640. + r(e9,0f,42,7c), r(e2,02,4b,72), r(ff,15,50,60), r(f4,18,59,6e),\
  12641. + r(c5,3b,66,44), r(ce,36,6f,4a), r(d3,21,74,58), r(d8,2c,7d,56),\
  12642. + r(7a,0c,a1,37), r(71,01,a8,39), r(6c,16,b3,2b), r(67,1b,ba,25),\
  12643. + r(56,38,85,0f), r(5d,35,8c,01), r(40,22,97,13), r(4b,2f,9e,1d),\
  12644. + r(22,64,e9,47), r(29,69,e0,49), r(34,7e,fb,5b), r(3f,73,f2,55),\
  12645. + r(0e,50,cd,7f), r(05,5d,c4,71), r(18,4a,df,63), r(13,47,d6,6d),\
  12646. + r(ca,dc,31,d7), r(c1,d1,38,d9), r(dc,c6,23,cb), r(d7,cb,2a,c5),\
  12647. + r(e6,e8,15,ef), r(ed,e5,1c,e1), r(f0,f2,07,f3), r(fb,ff,0e,fd),\
  12648. + r(92,b4,79,a7), r(99,b9,70,a9), r(84,ae,6b,bb), r(8f,a3,62,b5),\
  12649. + r(be,80,5d,9f), r(b5,8d,54,91), r(a8,9a,4f,83), r(a3,97,46,8d)
  12650. +
  12651. +#undef r
  12652. +#define r r0
  12653. +
  12654. +#if defined(ONE_IM_TABLE)
  12655. +static const u_int32_t im_tab[256] =
  12656. + { m_table };
  12657. +#elif defined(FOUR_IM_TABLES)
  12658. +static const u_int32_t im_tab[4][256] =
  12659. +{ { m_table },
  12660. +#undef r
  12661. +#define r r1
  12662. + { m_table },
  12663. +#undef r
  12664. +#define r r2
  12665. + { m_table },
  12666. +#undef r
  12667. +#define r r3
  12668. + { m_table }
  12669. +};
  12670. +#endif
  12671. +
  12672. +#endif
  12673. +
  12674. +#else
  12675. +
  12676. +static int tab_gen = 0;
  12677. +
  12678. +static unsigned char s_box[256]; // the S box
  12679. +static unsigned char inv_s_box[256]; // the inverse S box
  12680. +static u_int32_t rcon_tab[AES_RC_LENGTH]; // table of round constants
  12681. +
  12682. +#if defined(ONE_TABLE)
  12683. +static u_int32_t ft_tab[256];
  12684. +static u_int32_t it_tab[256];
  12685. +#elif defined(FOUR_TABLES)
  12686. +static u_int32_t ft_tab[4][256];
  12687. +static u_int32_t it_tab[4][256];
  12688. +#endif
  12689. +
  12690. +#if defined(ONE_LR_TABLE)
  12691. +static u_int32_t fl_tab[256];
  12692. +static u_int32_t il_tab[256];
  12693. +#elif defined(FOUR_LR_TABLES)
  12694. +static u_int32_t fl_tab[4][256];
  12695. +static u_int32_t il_tab[4][256];
  12696. +#endif
  12697. +
  12698. +#if defined(ONE_IM_TABLE)
  12699. +static u_int32_t im_tab[256];
  12700. +#elif defined(FOUR_IM_TABLES)
  12701. +static u_int32_t im_tab[4][256];
  12702. +#endif
  12703. +
  12704. +// Generate the tables for the dynamic table option
  12705. +
  12706. +#if !defined(FF_TABLES)
  12707. +
  12708. +// It will generally be sensible to use tables to compute finite
  12709. +// field multiplies and inverses but where memory is scarse this
  12710. +// code might sometimes be better.
  12711. +
  12712. +// return 2 ^ (n - 1) where n is the bit number of the highest bit
  12713. +// set in x with x in the range 1 < x < 0x00000200. This form is
  12714. +// used so that locals within FFinv can be bytes rather than words
  12715. +
  12716. +static unsigned char hibit(const u_int32_t x)
  12717. +{ unsigned char r = (unsigned char)((x >> 1) | (x >> 2));
  12718. +
  12719. + r |= (r >> 2);
  12720. + r |= (r >> 4);
  12721. + return (r + 1) >> 1;
  12722. +}
  12723. +
  12724. +// return the inverse of the finite field element x
  12725. +
  12726. +static unsigned char FFinv(const unsigned char x)
  12727. +{ unsigned char p1 = x, p2 = 0x1b, n1 = hibit(x), n2 = 0x80, v1 = 1, v2 = 0;
  12728. +
  12729. + if(x < 2) return x;
  12730. +
  12731. + for(;;)
  12732. + {
  12733. + if(!n1) return v1;
  12734. +
  12735. + while(n2 >= n1)
  12736. + {
  12737. + n2 /= n1; p2 ^= p1 * n2; v2 ^= v1 * n2; n2 = hibit(p2);
  12738. + }
  12739. +
  12740. + if(!n2) return v2;
  12741. +
  12742. + while(n1 >= n2)
  12743. + {
  12744. + n1 /= n2; p1 ^= p2 * n1; v1 ^= v2 * n1; n1 = hibit(p1);
  12745. + }
  12746. + }
  12747. +}
  12748. +
  12749. +// define the finite field multiplies required for Rijndael
  12750. +
  12751. +#define FFmul02(x) ((((x) & 0x7f) << 1) ^ ((x) & 0x80 ? 0x1b : 0))
  12752. +#define FFmul03(x) ((x) ^ FFmul02(x))
  12753. +#define FFmul09(x) ((x) ^ FFmul02(FFmul02(FFmul02(x))))
  12754. +#define FFmul0b(x) ((x) ^ FFmul02((x) ^ FFmul02(FFmul02(x))))
  12755. +#define FFmul0d(x) ((x) ^ FFmul02(FFmul02((x) ^ FFmul02(x))))
  12756. +#define FFmul0e(x) FFmul02((x) ^ FFmul02((x) ^ FFmul02(x)))
  12757. +
  12758. +#else
  12759. +
  12760. +#define FFinv(x) ((x) ? pow[255 - log[x]]: 0)
  12761. +
  12762. +#define FFmul02(x) (x ? pow[log[x] + 0x19] : 0)
  12763. +#define FFmul03(x) (x ? pow[log[x] + 0x01] : 0)
  12764. +#define FFmul09(x) (x ? pow[log[x] + 0xc7] : 0)
  12765. +#define FFmul0b(x) (x ? pow[log[x] + 0x68] : 0)
  12766. +#define FFmul0d(x) (x ? pow[log[x] + 0xee] : 0)
  12767. +#define FFmul0e(x) (x ? pow[log[x] + 0xdf] : 0)
  12768. +
  12769. +#endif
  12770. +
  12771. +// The forward and inverse affine transformations used in the S-box
  12772. +
  12773. +#define fwd_affine(x) \
  12774. + (w = (u_int32_t)x, w ^= (w<<1)^(w<<2)^(w<<3)^(w<<4), 0x63^(unsigned char)(w^(w>>8)))
  12775. +
  12776. +#define inv_affine(x) \
  12777. + (w = (u_int32_t)x, w = (w<<1)^(w<<3)^(w<<6), 0x05^(unsigned char)(w^(w>>8)))
  12778. +
  12779. +static void gen_tabs(void)
  12780. +{ u_int32_t i, w;
  12781. +
  12782. +#if defined(FF_TABLES)
  12783. +
  12784. + unsigned char pow[512], log[256];
  12785. +
  12786. + // log and power tables for GF(2^8) finite field with
  12787. + // 0x011b as modular polynomial - the simplest primitive
  12788. + // root is 0x03, used here to generate the tables
  12789. +
  12790. + i = 0; w = 1;
  12791. + do
  12792. + {
  12793. + pow[i] = (unsigned char)w;
  12794. + pow[i + 255] = (unsigned char)w;
  12795. + log[w] = (unsigned char)i++;
  12796. + w ^= (w << 1) ^ (w & ff_hi ? ff_poly : 0);
  12797. + }
  12798. + while (w != 1);
  12799. +
  12800. +#endif
  12801. +
  12802. + for(i = 0, w = 1; i < AES_RC_LENGTH; ++i)
  12803. + {
  12804. + rcon_tab[i] = bytes2word(w, 0, 0, 0);
  12805. + w = (w << 1) ^ (w & ff_hi ? ff_poly : 0);
  12806. + }
  12807. +
  12808. + for(i = 0; i < 256; ++i)
  12809. + { unsigned char b;
  12810. +
  12811. + s_box[i] = b = fwd_affine(FFinv((unsigned char)i));
  12812. +
  12813. + w = bytes2word(b, 0, 0, 0);
  12814. +#if defined(ONE_LR_TABLE)
  12815. + fl_tab[i] = w;
  12816. +#elif defined(FOUR_LR_TABLES)
  12817. + fl_tab[0][i] = w;
  12818. + fl_tab[1][i] = upr(w,1);
  12819. + fl_tab[2][i] = upr(w,2);
  12820. + fl_tab[3][i] = upr(w,3);
  12821. +#endif
  12822. + w = bytes2word(FFmul02(b), b, b, FFmul03(b));
  12823. +#if defined(ONE_TABLE)
  12824. + ft_tab[i] = w;
  12825. +#elif defined(FOUR_TABLES)
  12826. + ft_tab[0][i] = w;
  12827. + ft_tab[1][i] = upr(w,1);
  12828. + ft_tab[2][i] = upr(w,2);
  12829. + ft_tab[3][i] = upr(w,3);
  12830. +#endif
  12831. + inv_s_box[i] = b = FFinv(inv_affine((unsigned char)i));
  12832. +
  12833. + w = bytes2word(b, 0, 0, 0);
  12834. +#if defined(ONE_LR_TABLE)
  12835. + il_tab[i] = w;
  12836. +#elif defined(FOUR_LR_TABLES)
  12837. + il_tab[0][i] = w;
  12838. + il_tab[1][i] = upr(w,1);
  12839. + il_tab[2][i] = upr(w,2);
  12840. + il_tab[3][i] = upr(w,3);
  12841. +#endif
  12842. + w = bytes2word(FFmul0e(b), FFmul09(b), FFmul0d(b), FFmul0b(b));
  12843. +#if defined(ONE_TABLE)
  12844. + it_tab[i] = w;
  12845. +#elif defined(FOUR_TABLES)
  12846. + it_tab[0][i] = w;
  12847. + it_tab[1][i] = upr(w,1);
  12848. + it_tab[2][i] = upr(w,2);
  12849. + it_tab[3][i] = upr(w,3);
  12850. +#endif
  12851. +#if defined(ONE_IM_TABLE)
  12852. + im_tab[b] = w;
  12853. +#elif defined(FOUR_IM_TABLES)
  12854. + im_tab[0][b] = w;
  12855. + im_tab[1][b] = upr(w,1);
  12856. + im_tab[2][b] = upr(w,2);
  12857. + im_tab[3][b] = upr(w,3);
  12858. +#endif
  12859. +
  12860. + }
  12861. +}
  12862. +
  12863. +#endif
  12864. +
  12865. +#define no_table(x,box,vf,rf,c) bytes2word( \
  12866. + box[bval(vf(x,0,c),rf(0,c))], \
  12867. + box[bval(vf(x,1,c),rf(1,c))], \
  12868. + box[bval(vf(x,2,c),rf(2,c))], \
  12869. + box[bval(vf(x,3,c),rf(3,c))])
  12870. +
  12871. +#define one_table(x,op,tab,vf,rf,c) \
  12872. + ( tab[bval(vf(x,0,c),rf(0,c))] \
  12873. + ^ op(tab[bval(vf(x,1,c),rf(1,c))],1) \
  12874. + ^ op(tab[bval(vf(x,2,c),rf(2,c))],2) \
  12875. + ^ op(tab[bval(vf(x,3,c),rf(3,c))],3))
  12876. +
  12877. +#define four_tables(x,tab,vf,rf,c) \
  12878. + ( tab[0][bval(vf(x,0,c),rf(0,c))] \
  12879. + ^ tab[1][bval(vf(x,1,c),rf(1,c))] \
  12880. + ^ tab[2][bval(vf(x,2,c),rf(2,c))] \
  12881. + ^ tab[3][bval(vf(x,3,c),rf(3,c))])
  12882. +
  12883. +#define vf1(x,r,c) (x)
  12884. +#define rf1(r,c) (r)
  12885. +#define rf2(r,c) ((r-c)&3)
  12886. +
  12887. +#if defined(FOUR_LR_TABLES)
  12888. +#define ls_box(x,c) four_tables(x,fl_tab,vf1,rf2,c)
  12889. +#elif defined(ONE_LR_TABLE)
  12890. +#define ls_box(x,c) one_table(x,upr,fl_tab,vf1,rf2,c)
  12891. +#else
  12892. +#define ls_box(x,c) no_table(x,s_box,vf1,rf2,c)
  12893. +#endif
  12894. +
  12895. +#if defined(FOUR_IM_TABLES)
  12896. +#define inv_mcol(x) four_tables(x,im_tab,vf1,rf1,0)
  12897. +#elif defined(ONE_IM_TABLE)
  12898. +#define inv_mcol(x) one_table(x,upr,im_tab,vf1,rf1,0)
  12899. +#else
  12900. +#define inv_mcol(x) \
  12901. + (f9 = (x),f2 = FFmulX(f9), f4 = FFmulX(f2), f8 = FFmulX(f4), f9 ^= f8, \
  12902. + f2 ^= f4 ^ f8 ^ upr(f2 ^ f9,3) ^ upr(f4 ^ f9,2) ^ upr(f9,1))
  12903. +#endif
  12904. +
  12905. +// Subroutine to set the block size (if variable) in bytes, legal
  12906. +// values being 16, 24 and 32.
  12907. +
  12908. +#if defined(AES_BLOCK_SIZE)
  12909. +#define nc (AES_BLOCK_SIZE / 4)
  12910. +#else
  12911. +#define nc (cx->aes_Ncol)
  12912. +
  12913. +void aes_set_blk(aes_context *cx, int n_bytes)
  12914. +{
  12915. +#if !defined(FIXED_TABLES)
  12916. + if(!tab_gen) { gen_tabs(); tab_gen = 1; }
  12917. +#endif
  12918. +
  12919. + switch(n_bytes) {
  12920. + case 32: /* bytes */
  12921. + case 256: /* bits */
  12922. + nc = 8;
  12923. + break;
  12924. + case 24: /* bytes */
  12925. + case 192: /* bits */
  12926. + nc = 6;
  12927. + break;
  12928. + case 16: /* bytes */
  12929. + case 128: /* bits */
  12930. + default:
  12931. + nc = 4;
  12932. + break;
  12933. + }
  12934. +}
  12935. +
  12936. +#endif
  12937. +
  12938. +// Initialise the key schedule from the user supplied key. The key
  12939. +// length is now specified in bytes - 16, 24 or 32 as appropriate.
  12940. +// This corresponds to bit lengths of 128, 192 and 256 bits, and
  12941. +// to Nk values of 4, 6 and 8 respectively.
  12942. +
  12943. +#define mx(t,f) (*t++ = inv_mcol(*f),f++)
  12944. +#define cp(t,f) *t++ = *f++
  12945. +
  12946. +#if AES_BLOCK_SIZE == 16
  12947. +#define cpy(d,s) cp(d,s); cp(d,s); cp(d,s); cp(d,s)
  12948. +#define mix(d,s) mx(d,s); mx(d,s); mx(d,s); mx(d,s)
  12949. +#elif AES_BLOCK_SIZE == 24
  12950. +#define cpy(d,s) cp(d,s); cp(d,s); cp(d,s); cp(d,s); \
  12951. + cp(d,s); cp(d,s)
  12952. +#define mix(d,s) mx(d,s); mx(d,s); mx(d,s); mx(d,s); \
  12953. + mx(d,s); mx(d,s)
  12954. +#elif AES_BLOCK_SIZE == 32
  12955. +#define cpy(d,s) cp(d,s); cp(d,s); cp(d,s); cp(d,s); \
  12956. + cp(d,s); cp(d,s); cp(d,s); cp(d,s)
  12957. +#define mix(d,s) mx(d,s); mx(d,s); mx(d,s); mx(d,s); \
  12958. + mx(d,s); mx(d,s); mx(d,s); mx(d,s)
  12959. +#else
  12960. +
  12961. +#define cpy(d,s) \
  12962. +switch(nc) \
  12963. +{ case 8: cp(d,s); cp(d,s); \
  12964. + case 6: cp(d,s); cp(d,s); \
  12965. + case 4: cp(d,s); cp(d,s); \
  12966. + cp(d,s); cp(d,s); \
  12967. +}
  12968. +
  12969. +#define mix(d,s) \
  12970. +switch(nc) \
  12971. +{ case 8: mx(d,s); mx(d,s); \
  12972. + case 6: mx(d,s); mx(d,s); \
  12973. + case 4: mx(d,s); mx(d,s); \
  12974. + mx(d,s); mx(d,s); \
  12975. +}
  12976. +
  12977. +#endif
  12978. +
  12979. +void aes_set_key(aes_context *cx, const unsigned char in_key[], int n_bytes, const int f)
  12980. +{ u_int32_t *kf, *kt, rci;
  12981. +
  12982. +#if !defined(FIXED_TABLES)
  12983. + if(!tab_gen) { gen_tabs(); tab_gen = 1; }
  12984. +#endif
  12985. +
  12986. + switch(n_bytes) {
  12987. + case 32: /* bytes */
  12988. + case 256: /* bits */
  12989. + cx->aes_Nkey = 8;
  12990. + break;
  12991. + case 24: /* bytes */
  12992. + case 192: /* bits */
  12993. + cx->aes_Nkey = 6;
  12994. + break;
  12995. + case 16: /* bytes */
  12996. + case 128: /* bits */
  12997. + default:
  12998. + cx->aes_Nkey = 4;
  12999. + break;
  13000. + }
  13001. +
  13002. + cx->aes_Nrnd = (cx->aes_Nkey > nc ? cx->aes_Nkey : nc) + 6;
  13003. +
  13004. + cx->aes_e_key[0] = const_word_in(in_key );
  13005. + cx->aes_e_key[1] = const_word_in(in_key + 4);
  13006. + cx->aes_e_key[2] = const_word_in(in_key + 8);
  13007. + cx->aes_e_key[3] = const_word_in(in_key + 12);
  13008. +
  13009. + kf = cx->aes_e_key;
  13010. + kt = kf + nc * (cx->aes_Nrnd + 1) - cx->aes_Nkey;
  13011. + rci = 0;
  13012. +
  13013. + switch(cx->aes_Nkey)
  13014. + {
  13015. + case 4: do
  13016. + { kf[4] = kf[0] ^ ls_box(kf[3],3) ^ rcon_tab[rci++];
  13017. + kf[5] = kf[1] ^ kf[4];
  13018. + kf[6] = kf[2] ^ kf[5];
  13019. + kf[7] = kf[3] ^ kf[6];
  13020. + kf += 4;
  13021. + }
  13022. + while(kf < kt);
  13023. + break;
  13024. +
  13025. + case 6: cx->aes_e_key[4] = const_word_in(in_key + 16);
  13026. + cx->aes_e_key[5] = const_word_in(in_key + 20);
  13027. + do
  13028. + { kf[ 6] = kf[0] ^ ls_box(kf[5],3) ^ rcon_tab[rci++];
  13029. + kf[ 7] = kf[1] ^ kf[ 6];
  13030. + kf[ 8] = kf[2] ^ kf[ 7];
  13031. + kf[ 9] = kf[3] ^ kf[ 8];
  13032. + kf[10] = kf[4] ^ kf[ 9];
  13033. + kf[11] = kf[5] ^ kf[10];
  13034. + kf += 6;
  13035. + }
  13036. + while(kf < kt);
  13037. + break;
  13038. +
  13039. + case 8: cx->aes_e_key[4] = const_word_in(in_key + 16);
  13040. + cx->aes_e_key[5] = const_word_in(in_key + 20);
  13041. + cx->aes_e_key[6] = const_word_in(in_key + 24);
  13042. + cx->aes_e_key[7] = const_word_in(in_key + 28);
  13043. + do
  13044. + { kf[ 8] = kf[0] ^ ls_box(kf[7],3) ^ rcon_tab[rci++];
  13045. + kf[ 9] = kf[1] ^ kf[ 8];
  13046. + kf[10] = kf[2] ^ kf[ 9];
  13047. + kf[11] = kf[3] ^ kf[10];
  13048. + kf[12] = kf[4] ^ ls_box(kf[11],0);
  13049. + kf[13] = kf[5] ^ kf[12];
  13050. + kf[14] = kf[6] ^ kf[13];
  13051. + kf[15] = kf[7] ^ kf[14];
  13052. + kf += 8;
  13053. + }
  13054. + while (kf < kt);
  13055. + break;
  13056. + }
  13057. +
  13058. + if(!f)
  13059. + { u_int32_t i;
  13060. +
  13061. + kt = cx->aes_d_key + nc * cx->aes_Nrnd;
  13062. + kf = cx->aes_e_key;
  13063. +
  13064. + cpy(kt, kf); kt -= 2 * nc;
  13065. +
  13066. + for(i = 1; i < cx->aes_Nrnd; ++i)
  13067. + {
  13068. +#if defined(ONE_TABLE) || defined(FOUR_TABLES)
  13069. +#if !defined(ONE_IM_TABLE) && !defined(FOUR_IM_TABLES)
  13070. + u_int32_t f2, f4, f8, f9;
  13071. +#endif
  13072. + mix(kt, kf);
  13073. +#else
  13074. + cpy(kt, kf);
  13075. +#endif
  13076. + kt -= 2 * nc;
  13077. + }
  13078. +
  13079. + cpy(kt, kf);
  13080. + }
  13081. +}
  13082. +
  13083. +// y = output word, x = input word, r = row, c = column
  13084. +// for r = 0, 1, 2 and 3 = column accessed for row r
  13085. +
  13086. +#if defined(ARRAYS)
  13087. +#define s(x,c) x[c]
  13088. +#else
  13089. +#define s(x,c) x##c
  13090. +#endif
  13091. +
  13092. +// I am grateful to Frank Yellin for the following constructions
  13093. +// which, given the column (c) of the output state variable that
  13094. +// is being computed, return the input state variables which are
  13095. +// needed for each row (r) of the state
  13096. +
  13097. +// For the fixed block size options, compilers reduce these two
  13098. +// expressions to fixed variable references. For variable block
  13099. +// size code conditional clauses will sometimes be returned
  13100. +
  13101. +#define unused 77 // Sunset Strip
  13102. +
  13103. +#define fwd_var(x,r,c) \
  13104. + ( r==0 ? \
  13105. + ( c==0 ? s(x,0) \
  13106. + : c==1 ? s(x,1) \
  13107. + : c==2 ? s(x,2) \
  13108. + : c==3 ? s(x,3) \
  13109. + : c==4 ? s(x,4) \
  13110. + : c==5 ? s(x,5) \
  13111. + : c==6 ? s(x,6) \
  13112. + : s(x,7)) \
  13113. + : r==1 ? \
  13114. + ( c==0 ? s(x,1) \
  13115. + : c==1 ? s(x,2) \
  13116. + : c==2 ? s(x,3) \
  13117. + : c==3 ? nc==4 ? s(x,0) : s(x,4) \
  13118. + : c==4 ? s(x,5) \
  13119. + : c==5 ? nc==8 ? s(x,6) : s(x,0) \
  13120. + : c==6 ? s(x,7) \
  13121. + : s(x,0)) \
  13122. + : r==2 ? \
  13123. + ( c==0 ? nc==8 ? s(x,3) : s(x,2) \
  13124. + : c==1 ? nc==8 ? s(x,4) : s(x,3) \
  13125. + : c==2 ? nc==4 ? s(x,0) : nc==8 ? s(x,5) : s(x,4) \
  13126. + : c==3 ? nc==4 ? s(x,1) : nc==8 ? s(x,6) : s(x,5) \
  13127. + : c==4 ? nc==8 ? s(x,7) : s(x,0) \
  13128. + : c==5 ? nc==8 ? s(x,0) : s(x,1) \
  13129. + : c==6 ? s(x,1) \
  13130. + : s(x,2)) \
  13131. + : \
  13132. + ( c==0 ? nc==8 ? s(x,4) : s(x,3) \
  13133. + : c==1 ? nc==4 ? s(x,0) : nc==8 ? s(x,5) : s(x,4) \
  13134. + : c==2 ? nc==4 ? s(x,1) : nc==8 ? s(x,6) : s(x,5) \
  13135. + : c==3 ? nc==4 ? s(x,2) : nc==8 ? s(x,7) : s(x,0) \
  13136. + : c==4 ? nc==8 ? s(x,0) : s(x,1) \
  13137. + : c==5 ? nc==8 ? s(x,1) : s(x,2) \
  13138. + : c==6 ? s(x,2) \
  13139. + : s(x,3)))
  13140. +
  13141. +#define inv_var(x,r,c) \
  13142. + ( r==0 ? \
  13143. + ( c==0 ? s(x,0) \
  13144. + : c==1 ? s(x,1) \
  13145. + : c==2 ? s(x,2) \
  13146. + : c==3 ? s(x,3) \
  13147. + : c==4 ? s(x,4) \
  13148. + : c==5 ? s(x,5) \
  13149. + : c==6 ? s(x,6) \
  13150. + : s(x,7)) \
  13151. + : r==1 ? \
  13152. + ( c==0 ? nc==4 ? s(x,3) : nc==8 ? s(x,7) : s(x,5) \
  13153. + : c==1 ? s(x,0) \
  13154. + : c==2 ? s(x,1) \
  13155. + : c==3 ? s(x,2) \
  13156. + : c==4 ? s(x,3) \
  13157. + : c==5 ? s(x,4) \
  13158. + : c==6 ? s(x,5) \
  13159. + : s(x,6)) \
  13160. + : r==2 ? \
  13161. + ( c==0 ? nc==4 ? s(x,2) : nc==8 ? s(x,5) : s(x,4) \
  13162. + : c==1 ? nc==4 ? s(x,3) : nc==8 ? s(x,6) : s(x,5) \
  13163. + : c==2 ? nc==8 ? s(x,7) : s(x,0) \
  13164. + : c==3 ? nc==8 ? s(x,0) : s(x,1) \
  13165. + : c==4 ? nc==8 ? s(x,1) : s(x,2) \
  13166. + : c==5 ? nc==8 ? s(x,2) : s(x,3) \
  13167. + : c==6 ? s(x,3) \
  13168. + : s(x,4)) \
  13169. + : \
  13170. + ( c==0 ? nc==4 ? s(x,1) : nc==8 ? s(x,4) : s(x,3) \
  13171. + : c==1 ? nc==4 ? s(x,2) : nc==8 ? s(x,5) : s(x,4) \
  13172. + : c==2 ? nc==4 ? s(x,3) : nc==8 ? s(x,6) : s(x,5) \
  13173. + : c==3 ? nc==8 ? s(x,7) : s(x,0) \
  13174. + : c==4 ? nc==8 ? s(x,0) : s(x,1) \
  13175. + : c==5 ? nc==8 ? s(x,1) : s(x,2) \
  13176. + : c==6 ? s(x,2) \
  13177. + : s(x,3)))
  13178. +
  13179. +#define si(y,x,k,c) s(y,c) = const_word_in(x + 4 * c) ^ k[c]
  13180. +#define so(y,x,c) word_out(y + 4 * c, s(x,c))
  13181. +
  13182. +#if defined(FOUR_TABLES)
  13183. +#define fwd_rnd(y,x,k,c) s(y,c)= (k)[c] ^ four_tables(x,ft_tab,fwd_var,rf1,c)
  13184. +#define inv_rnd(y,x,k,c) s(y,c)= (k)[c] ^ four_tables(x,it_tab,inv_var,rf1,c)
  13185. +#elif defined(ONE_TABLE)
  13186. +#define fwd_rnd(y,x,k,c) s(y,c)= (k)[c] ^ one_table(x,upr,ft_tab,fwd_var,rf1,c)
  13187. +#define inv_rnd(y,x,k,c) s(y,c)= (k)[c] ^ one_table(x,upr,it_tab,inv_var,rf1,c)
  13188. +#else
  13189. +#define fwd_rnd(y,x,k,c) s(y,c) = fwd_mcol(no_table(x,s_box,fwd_var,rf1,c)) ^ (k)[c]
  13190. +#define inv_rnd(y,x,k,c) s(y,c) = inv_mcol(no_table(x,inv_s_box,inv_var,rf1,c) ^ (k)[c])
  13191. +#endif
  13192. +
  13193. +#if defined(FOUR_LR_TABLES)
  13194. +#define fwd_lrnd(y,x,k,c) s(y,c)= (k)[c] ^ four_tables(x,fl_tab,fwd_var,rf1,c)
  13195. +#define inv_lrnd(y,x,k,c) s(y,c)= (k)[c] ^ four_tables(x,il_tab,inv_var,rf1,c)
  13196. +#elif defined(ONE_LR_TABLE)
  13197. +#define fwd_lrnd(y,x,k,c) s(y,c)= (k)[c] ^ one_table(x,ups,fl_tab,fwd_var,rf1,c)
  13198. +#define inv_lrnd(y,x,k,c) s(y,c)= (k)[c] ^ one_table(x,ups,il_tab,inv_var,rf1,c)
  13199. +#else
  13200. +#define fwd_lrnd(y,x,k,c) s(y,c) = no_table(x,s_box,fwd_var,rf1,c) ^ (k)[c]
  13201. +#define inv_lrnd(y,x,k,c) s(y,c) = no_table(x,inv_s_box,inv_var,rf1,c) ^ (k)[c]
  13202. +#endif
  13203. +
  13204. +#if AES_BLOCK_SIZE == 16
  13205. +
  13206. +#if defined(ARRAYS)
  13207. +#define locals(y,x) x[4],y[4]
  13208. +#else
  13209. +#define locals(y,x) x##0,x##1,x##2,x##3,y##0,y##1,y##2,y##3
  13210. +// the following defines prevent the compiler requiring the declaration
  13211. +// of generated but unused variables in the fwd_var and inv_var macros
  13212. +#define b04 unused
  13213. +#define b05 unused
  13214. +#define b06 unused
  13215. +#define b07 unused
  13216. +#define b14 unused
  13217. +#define b15 unused
  13218. +#define b16 unused
  13219. +#define b17 unused
  13220. +#endif
  13221. +#define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \
  13222. + s(y,2) = s(x,2); s(y,3) = s(x,3);
  13223. +#define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); si(y,x,k,3)
  13224. +#define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3)
  13225. +#define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); rm(y,x,k,3)
  13226. +
  13227. +#elif AES_BLOCK_SIZE == 24
  13228. +
  13229. +#if defined(ARRAYS)
  13230. +#define locals(y,x) x[6],y[6]
  13231. +#else
  13232. +#define locals(y,x) x##0,x##1,x##2,x##3,x##4,x##5, \
  13233. + y##0,y##1,y##2,y##3,y##4,y##5
  13234. +#define b06 unused
  13235. +#define b07 unused
  13236. +#define b16 unused
  13237. +#define b17 unused
  13238. +#endif
  13239. +#define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \
  13240. + s(y,2) = s(x,2); s(y,3) = s(x,3); \
  13241. + s(y,4) = s(x,4); s(y,5) = s(x,5);
  13242. +#define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); \
  13243. + si(y,x,k,3); si(y,x,k,4); si(y,x,k,5)
  13244. +#define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); \
  13245. + so(y,x,3); so(y,x,4); so(y,x,5)
  13246. +#define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); \
  13247. + rm(y,x,k,3); rm(y,x,k,4); rm(y,x,k,5)
  13248. +#else
  13249. +
  13250. +#if defined(ARRAYS)
  13251. +#define locals(y,x) x[8],y[8]
  13252. +#else
  13253. +#define locals(y,x) x##0,x##1,x##2,x##3,x##4,x##5,x##6,x##7, \
  13254. + y##0,y##1,y##2,y##3,y##4,y##5,y##6,y##7
  13255. +#endif
  13256. +#define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \
  13257. + s(y,2) = s(x,2); s(y,3) = s(x,3); \
  13258. + s(y,4) = s(x,4); s(y,5) = s(x,5); \
  13259. + s(y,6) = s(x,6); s(y,7) = s(x,7);
  13260. +
  13261. +#if AES_BLOCK_SIZE == 32
  13262. +
  13263. +#define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); si(y,x,k,3); \
  13264. + si(y,x,k,4); si(y,x,k,5); si(y,x,k,6); si(y,x,k,7)
  13265. +#define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3); \
  13266. + so(y,x,4); so(y,x,5); so(y,x,6); so(y,x,7)
  13267. +#define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); rm(y,x,k,3); \
  13268. + rm(y,x,k,4); rm(y,x,k,5); rm(y,x,k,6); rm(y,x,k,7)
  13269. +#else
  13270. +
  13271. +#define state_in(y,x,k) \
  13272. +switch(nc) \
  13273. +{ case 8: si(y,x,k,7); si(y,x,k,6); \
  13274. + case 6: si(y,x,k,5); si(y,x,k,4); \
  13275. + case 4: si(y,x,k,3); si(y,x,k,2); \
  13276. + si(y,x,k,1); si(y,x,k,0); \
  13277. +}
  13278. +
  13279. +#define state_out(y,x) \
  13280. +switch(nc) \
  13281. +{ case 8: so(y,x,7); so(y,x,6); \
  13282. + case 6: so(y,x,5); so(y,x,4); \
  13283. + case 4: so(y,x,3); so(y,x,2); \
  13284. + so(y,x,1); so(y,x,0); \
  13285. +}
  13286. +
  13287. +#if defined(FAST_VARIABLE)
  13288. +
  13289. +#define round(rm,y,x,k) \
  13290. +switch(nc) \
  13291. +{ case 8: rm(y,x,k,7); rm(y,x,k,6); \
  13292. + rm(y,x,k,5); rm(y,x,k,4); \
  13293. + rm(y,x,k,3); rm(y,x,k,2); \
  13294. + rm(y,x,k,1); rm(y,x,k,0); \
  13295. + break; \
  13296. + case 6: rm(y,x,k,5); rm(y,x,k,4); \
  13297. + rm(y,x,k,3); rm(y,x,k,2); \
  13298. + rm(y,x,k,1); rm(y,x,k,0); \
  13299. + break; \
  13300. + case 4: rm(y,x,k,3); rm(y,x,k,2); \
  13301. + rm(y,x,k,1); rm(y,x,k,0); \
  13302. + break; \
  13303. +}
  13304. +#else
  13305. +
  13306. +#define round(rm,y,x,k) \
  13307. +switch(nc) \
  13308. +{ case 8: rm(y,x,k,7); rm(y,x,k,6); \
  13309. + case 6: rm(y,x,k,5); rm(y,x,k,4); \
  13310. + case 4: rm(y,x,k,3); rm(y,x,k,2); \
  13311. + rm(y,x,k,1); rm(y,x,k,0); \
  13312. +}
  13313. +
  13314. +#endif
  13315. +
  13316. +#endif
  13317. +#endif
  13318. +
  13319. +void aes_encrypt(const aes_context *cx, const unsigned char in_blk[], unsigned char out_blk[])
  13320. +{ u_int32_t locals(b0, b1);
  13321. + const u_int32_t *kp = cx->aes_e_key;
  13322. +
  13323. +#if !defined(ONE_TABLE) && !defined(FOUR_TABLES)
  13324. + u_int32_t f2;
  13325. +#endif
  13326. +
  13327. + state_in(b0, in_blk, kp); kp += nc;
  13328. +
  13329. +#if defined(UNROLL)
  13330. +
  13331. + switch(cx->aes_Nrnd)
  13332. + {
  13333. + case 14: round(fwd_rnd, b1, b0, kp );
  13334. + round(fwd_rnd, b0, b1, kp + nc ); kp += 2 * nc;
  13335. + case 12: round(fwd_rnd, b1, b0, kp );
  13336. + round(fwd_rnd, b0, b1, kp + nc ); kp += 2 * nc;
  13337. + case 10: round(fwd_rnd, b1, b0, kp );
  13338. + round(fwd_rnd, b0, b1, kp + nc);
  13339. + round(fwd_rnd, b1, b0, kp + 2 * nc);
  13340. + round(fwd_rnd, b0, b1, kp + 3 * nc);
  13341. + round(fwd_rnd, b1, b0, kp + 4 * nc);
  13342. + round(fwd_rnd, b0, b1, kp + 5 * nc);
  13343. + round(fwd_rnd, b1, b0, kp + 6 * nc);
  13344. + round(fwd_rnd, b0, b1, kp + 7 * nc);
  13345. + round(fwd_rnd, b1, b0, kp + 8 * nc);
  13346. + round(fwd_lrnd, b0, b1, kp + 9 * nc);
  13347. + }
  13348. +
  13349. +#elif defined(PARTIAL_UNROLL)
  13350. + { u_int32_t rnd;
  13351. +
  13352. + for(rnd = 0; rnd < (cx->aes_Nrnd >> 1) - 1; ++rnd)
  13353. + {
  13354. + round(fwd_rnd, b1, b0, kp);
  13355. + round(fwd_rnd, b0, b1, kp + nc); kp += 2 * nc;
  13356. + }
  13357. +
  13358. + round(fwd_rnd, b1, b0, kp);
  13359. + round(fwd_lrnd, b0, b1, kp + nc);
  13360. + }
  13361. +#else
  13362. + { u_int32_t rnd;
  13363. +
  13364. + for(rnd = 0; rnd < cx->aes_Nrnd - 1; ++rnd)
  13365. + {
  13366. + round(fwd_rnd, b1, b0, kp);
  13367. + l_copy(b0, b1); kp += nc;
  13368. + }
  13369. +
  13370. + round(fwd_lrnd, b0, b1, kp);
  13371. + }
  13372. +#endif
  13373. +
  13374. + state_out(out_blk, b0);
  13375. +}
  13376. +
  13377. +void aes_decrypt(const aes_context *cx, const unsigned char in_blk[], unsigned char out_blk[])
  13378. +{ u_int32_t locals(b0, b1);
  13379. + const u_int32_t *kp = cx->aes_d_key;
  13380. +
  13381. +#if !defined(ONE_TABLE) && !defined(FOUR_TABLES)
  13382. + u_int32_t f2, f4, f8, f9;
  13383. +#endif
  13384. +
  13385. + state_in(b0, in_blk, kp); kp += nc;
  13386. +
  13387. +#if defined(UNROLL)
  13388. +
  13389. + switch(cx->aes_Nrnd)
  13390. + {
  13391. + case 14: round(inv_rnd, b1, b0, kp );
  13392. + round(inv_rnd, b0, b1, kp + nc ); kp += 2 * nc;
  13393. + case 12: round(inv_rnd, b1, b0, kp );
  13394. + round(inv_rnd, b0, b1, kp + nc ); kp += 2 * nc;
  13395. + case 10: round(inv_rnd, b1, b0, kp );
  13396. + round(inv_rnd, b0, b1, kp + nc);
  13397. + round(inv_rnd, b1, b0, kp + 2 * nc);
  13398. + round(inv_rnd, b0, b1, kp + 3 * nc);
  13399. + round(inv_rnd, b1, b0, kp + 4 * nc);
  13400. + round(inv_rnd, b0, b1, kp + 5 * nc);
  13401. + round(inv_rnd, b1, b0, kp + 6 * nc);
  13402. + round(inv_rnd, b0, b1, kp + 7 * nc);
  13403. + round(inv_rnd, b1, b0, kp + 8 * nc);
  13404. + round(inv_lrnd, b0, b1, kp + 9 * nc);
  13405. + }
  13406. +
  13407. +#elif defined(PARTIAL_UNROLL)
  13408. + { u_int32_t rnd;
  13409. +
  13410. + for(rnd = 0; rnd < (cx->aes_Nrnd >> 1) - 1; ++rnd)
  13411. + {
  13412. + round(inv_rnd, b1, b0, kp);
  13413. + round(inv_rnd, b0, b1, kp + nc); kp += 2 * nc;
  13414. + }
  13415. +
  13416. + round(inv_rnd, b1, b0, kp);
  13417. + round(inv_lrnd, b0, b1, kp + nc);
  13418. + }
  13419. +#else
  13420. + { u_int32_t rnd;
  13421. +
  13422. + for(rnd = 0; rnd < cx->aes_Nrnd - 1; ++rnd)
  13423. + {
  13424. + round(inv_rnd, b1, b0, kp);
  13425. + l_copy(b0, b1); kp += nc;
  13426. + }
  13427. +
  13428. + round(inv_lrnd, b0, b1, kp);
  13429. + }
  13430. +#endif
  13431. +
  13432. + state_out(out_blk, b0);
  13433. +}
  13434. --- /dev/null Tue Mar 11 13:02:56 2003
  13435. +++ linux/net/ipsec/aes/aes_cbc.c Mon Feb 9 13:51:03 2004
  13436. @@ -0,0 +1,46 @@
  13437. +/*
  13438. +// I retain copyright in this code but I encourage its free use provided
  13439. +// that I don't carry any responsibility for the results. I am especially
  13440. +// happy to see it used in free and open source software. If you do use
  13441. +// it I would appreciate an acknowledgement of its origin in the code or
  13442. +// the product that results and I would also appreciate knowing a little
  13443. +// about the use to which it is being put. I am grateful to Frank Yellin
  13444. +// for some ideas that are used in this implementation.
  13445. +//
  13446. +// Dr B. R. Gladman <brg@gladman.uk.net> 6th April 2001.
  13447. +//
  13448. +// This is an implementation of the AES encryption algorithm (Rijndael)
  13449. +// designed by Joan Daemen and Vincent Rijmen. This version is designed
  13450. +// to provide both fixed and dynamic block and key lengths and can also
  13451. +// run with either big or little endian internal byte order (see aes.h).
  13452. +// It inputs block and key lengths in bytes with the legal values being
  13453. +// 16, 24 and 32.
  13454. +*
  13455. +*/
  13456. +
  13457. +#ifdef __KERNEL__
  13458. +#include <linux/types.h>
  13459. +#else
  13460. +#include <sys/types.h>
  13461. +#endif
  13462. +#include "crypto/aes_cbc.h"
  13463. +#include "crypto/cbc_generic.h"
  13464. +
  13465. +/* returns bool success */
  13466. +int AES_set_key(aes_context *aes_ctx, const u_int8_t *key, int keysize) {
  13467. + aes_set_key(aes_ctx, key, keysize, 0);
  13468. + return 1;
  13469. +}
  13470. +CBC_IMPL_BLK16(AES_cbc_encrypt, aes_context, u_int8_t *, aes_encrypt, aes_decrypt);
  13471. +
  13472. +
  13473. +/*
  13474. + * $Log: aes_cbc.c,v $
  13475. + * Revision 1.2 2004/07/10 07:48:40 mcr
  13476. + * Moved from linux/crypto/ciphers/aes/aes_cbc.c,v
  13477. + *
  13478. + * Revision 1.1 2004/04/06 02:48:12 mcr
  13479. + * pullup of AES cipher from alg-branch.
  13480. + *
  13481. + *
  13482. + */
  13483. --- /dev/null Tue Mar 11 13:02:56 2003
  13484. +++ linux/net/ipsec/aes/aes_xcbc_mac.c Mon Feb 9 13:51:03 2004
  13485. @@ -0,0 +1,67 @@
  13486. +#ifdef __KERNEL__
  13487. +#include <linux/types.h>
  13488. +#include <linux/kernel.h>
  13489. +#define DEBUG(x)
  13490. +#else
  13491. +#include <stdio.h>
  13492. +#include <sys/types.h>
  13493. +#define DEBUG(x) x
  13494. +#endif
  13495. +
  13496. +#include "crypto/aes.h"
  13497. +#include "crypto/aes_xcbc_mac.h"
  13498. +
  13499. +int AES_xcbc_mac_set_key(aes_context_mac *ctxm, const u_int8_t *key, int keylen)
  13500. +{
  13501. + int ret=1;
  13502. + aes_block kn[3] = {
  13503. + { 0x01010101, 0x01010101, 0x01010101, 0x01010101 },
  13504. + { 0x02020202, 0x02020202, 0x02020202, 0x02020202 },
  13505. + { 0x03030303, 0x03030303, 0x03030303, 0x03030303 },
  13506. + };
  13507. + aes_set_key(&ctxm->ctx_k1, key, keylen, 0);
  13508. + aes_encrypt(&ctxm->ctx_k1, (u_int8_t *) kn[0], (u_int8_t *) kn[0]);
  13509. + aes_encrypt(&ctxm->ctx_k1, (u_int8_t *) kn[1], (u_int8_t *) ctxm->k2);
  13510. + aes_encrypt(&ctxm->ctx_k1, (u_int8_t *) kn[2], (u_int8_t *) ctxm->k3);
  13511. + aes_set_key(&ctxm->ctx_k1, (u_int8_t *) kn[0], 16, 0);
  13512. + return ret;
  13513. +}
  13514. +static void do_pad_xor(u_int8_t *out, const u_int8_t *in, int len) {
  13515. + int pos=0;
  13516. + for (pos=1; pos <= 16; pos++, in++, out++) {
  13517. + if (pos <= len)
  13518. + *out ^= *in;
  13519. + if (pos > len) {
  13520. + DEBUG(printf("put 0x80 at pos=%d\n", pos));
  13521. + *out ^= 0x80;
  13522. + break;
  13523. + }
  13524. + }
  13525. +}
  13526. +static void xor_block(aes_block res, const aes_block op) {
  13527. + res[0] ^= op[0];
  13528. + res[1] ^= op[1];
  13529. + res[2] ^= op[2];
  13530. + res[3] ^= op[3];
  13531. +}
  13532. +int AES_xcbc_mac_hash(const aes_context_mac *ctxm, const u_int8_t * in, int ilen, u_int8_t hash[16]) {
  13533. + int ret=ilen;
  13534. + u_int32_t out[4] = { 0, 0, 0, 0 };
  13535. + for (; ilen > 16 ; ilen-=16) {
  13536. + xor_block(out, (const u_int32_t*) &in[0]);
  13537. + aes_encrypt(&ctxm->ctx_k1, in, (u_int8_t *)&out[0]);
  13538. + in+=16;
  13539. + }
  13540. + do_pad_xor((u_int8_t *)&out, in, ilen);
  13541. + if (ilen==16) {
  13542. + DEBUG(printf("using k3\n"));
  13543. + xor_block(out, ctxm->k3);
  13544. + }
  13545. + else
  13546. + {
  13547. + DEBUG(printf("using k2\n"));
  13548. + xor_block(out, ctxm->k2);
  13549. + }
  13550. + aes_encrypt(&ctxm->ctx_k1, (u_int8_t *)out, hash);
  13551. + return ret;
  13552. +}
  13553. --- /dev/null Tue Mar 11 13:02:56 2003
  13554. +++ linux/net/ipsec/aes/ipsec_alg_aes.c Mon Feb 9 13:51:03 2004
  13555. @@ -0,0 +1,296 @@
  13556. +/*
  13557. + * ipsec_alg AES cipher stubs
  13558. + *
  13559. + * Author: JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  13560. + *
  13561. + * ipsec_alg_aes.c,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
  13562. + *
  13563. + * This program is free software; you can redistribute it and/or modify it
  13564. + * under the terms of the GNU General Public License as published by the
  13565. + * Free Software Foundation; either version 2 of the License, or (at your
  13566. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  13567. + *
  13568. + * This program is distributed in the hope that it will be useful, but
  13569. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13570. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13571. + * for more details.
  13572. + *
  13573. + * Fixes by:
  13574. + * PK: Pawel Krawczyk <kravietz@aba.krakow.pl>
  13575. + * Fixes list:
  13576. + * PK: make XCBC comply with latest draft (keylength)
  13577. + *
  13578. + */
  13579. +#ifndef AUTOCONF_INCLUDED
  13580. +#include <linux/config.h>
  13581. +#endif
  13582. +#include <linux/version.h>
  13583. +
  13584. +/*
  13585. + * special case: ipsec core modular with this static algo inside:
  13586. + * must avoid MODULE magic for this file
  13587. + */
  13588. +#if defined(CONFIG_KLIPS_MODULE) && defined(CONFIG_KLIPS_ENC_AES)
  13589. +#undef MODULE
  13590. +#endif
  13591. +
  13592. +#include <linux/module.h>
  13593. +#include <linux/init.h>
  13594. +
  13595. +#include <linux/kernel.h> /* printk() */
  13596. +#include <linux/errno.h> /* error codes */
  13597. +#include <linux/types.h> /* size_t */
  13598. +#include <linux/string.h>
  13599. +
  13600. +/* Check if __exit is defined, if not null it */
  13601. +#ifndef __exit
  13602. +#define __exit
  13603. +#endif
  13604. +
  13605. +/* Low freeswan header coupling */
  13606. +#include "openswan/ipsec_alg.h"
  13607. +#include "crypto/aes_cbc.h"
  13608. +
  13609. +#define CONFIG_KLIPS_ENC_AES_MAC 1
  13610. +
  13611. +#define AES_CONTEXT_T aes_context
  13612. +static int debug_aes=0;
  13613. +static int test_aes=0;
  13614. +static int excl_aes=0;
  13615. +static int keyminbits=0;
  13616. +static int keymaxbits=0;
  13617. +#if defined(CONFIG_KLIPS_ENC_AES_MODULE)
  13618. +MODULE_AUTHOR("JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>");
  13619. +#ifdef module_param
  13620. +module_param(debug_aes,int,0600)
  13621. +module_param(test_aes,int,0600)
  13622. +module_param(excl_aes,int,0600)
  13623. +module_param(keyminbits,int,0600)
  13624. +module_param(keymaxbits,int,0600)
  13625. +#else
  13626. +MODULE_PARM(debug_aes, "i");
  13627. +MODULE_PARM(test_aes, "i");
  13628. +MODULE_PARM(excl_aes, "i");
  13629. +MODULE_PARM(keyminbits, "i");
  13630. +MODULE_PARM(keymaxbits, "i");
  13631. +#endif
  13632. +#endif
  13633. +
  13634. +#if CONFIG_KLIPS_ENC_AES_MAC
  13635. +#include "crypto/aes_xcbc_mac.h"
  13636. +
  13637. +/*
  13638. + * Not IANA number yet (draft-ietf-ipsec-ciph-aes-xcbc-mac-00.txt).
  13639. + * We use 9 for non-modular algorithm and none for modular, thus
  13640. + * forcing user to specify one on module load. -kravietz
  13641. + */
  13642. +#ifdef MODULE
  13643. +static int auth_id=0;
  13644. +#else
  13645. +static int auth_id=9;
  13646. +#endif
  13647. +#ifdef module_param
  13648. +module_param(auth_id, int, 0600);
  13649. +#else
  13650. +MODULE_PARM(auth_id, "i");
  13651. +#endif
  13652. +#endif
  13653. +
  13654. +#define ESP_AES 12 /* truely _constant_ :) */
  13655. +
  13656. +/* 128, 192 or 256 */
  13657. +#define ESP_AES_KEY_SZ_MIN 16 /* 128 bit secret key */
  13658. +#define ESP_AES_KEY_SZ_MAX 32 /* 256 bit secret key */
  13659. +#define ESP_AES_CBC_BLK_LEN 16 /* AES-CBC block size */
  13660. +
  13661. +/* Values according to draft-ietf-ipsec-ciph-aes-xcbc-mac-02.txt
  13662. + * -kravietz
  13663. + */
  13664. +#define ESP_AES_MAC_KEY_SZ 16 /* 128 bit MAC key */
  13665. +#define ESP_AES_MAC_BLK_LEN 16 /* 128 bit block */
  13666. +
  13667. +static int _aes_set_key(struct ipsec_alg_enc *alg,
  13668. + __u8 * key_e, const __u8 * key,
  13669. + size_t keysize)
  13670. +{
  13671. + int ret;
  13672. + AES_CONTEXT_T *ctx=(AES_CONTEXT_T*)key_e;
  13673. + ret=AES_set_key(ctx, key, keysize)!=0? 0: -EINVAL;
  13674. + if (debug_aes > 0)
  13675. + printk(KERN_DEBUG "klips_debug:_aes_set_key:"
  13676. + "ret=%d key_e=%p key=%p keysize=%ld\n",
  13677. + ret, key_e, key, (unsigned long int) keysize);
  13678. + return ret;
  13679. +}
  13680. +
  13681. +static int _aes_cbc_encrypt(struct ipsec_alg_enc *alg, __u8 * key_e,
  13682. + __u8 * in, int ilen, const __u8 * iv,
  13683. + int encrypt)
  13684. +{
  13685. + AES_CONTEXT_T *ctx=(AES_CONTEXT_T*)key_e;
  13686. + if (debug_aes > 0)
  13687. + printk(KERN_DEBUG "klips_debug:_aes_cbc_encrypt:"
  13688. + "key_e=%p in=%p ilen=%d iv=%p encrypt=%d\n",
  13689. + key_e, in, ilen, iv, encrypt);
  13690. + return AES_cbc_encrypt(ctx, in, in, ilen, iv, encrypt);
  13691. +}
  13692. +#if CONFIG_KLIPS_ENC_AES_MAC
  13693. +static int _aes_mac_set_key(struct ipsec_alg_auth *alg, __u8 * key_a, const __u8 * key, int keylen) {
  13694. + aes_context_mac *ctxm=(aes_context_mac *)key_a;
  13695. + return AES_xcbc_mac_set_key(ctxm, key, keylen)? 0 : -EINVAL;
  13696. +}
  13697. +static int _aes_mac_hash(struct ipsec_alg_auth *alg, __u8 * key_a, const __u8 * dat, int len, __u8 * hash, int hashlen) {
  13698. + int ret;
  13699. + char hash_buf[16];
  13700. + aes_context_mac *ctxm=(aes_context_mac *)key_a;
  13701. + ret=AES_xcbc_mac_hash(ctxm, dat, len, hash_buf);
  13702. + memcpy(hash, hash_buf, hashlen);
  13703. + return ret;
  13704. +}
  13705. +static struct ipsec_alg_auth ipsec_alg_AES_MAC = {
  13706. + ixt_common: { ixt_version: IPSEC_ALG_VERSION,
  13707. + ixt_refcnt: ATOMIC_INIT(0),
  13708. + ixt_name: "aes_mac",
  13709. + ixt_blocksize: ESP_AES_MAC_BLK_LEN,
  13710. + ixt_support: {
  13711. + ias_exttype: IPSEC_ALG_TYPE_AUTH,
  13712. + ias_id: 0,
  13713. + ias_keyminbits: ESP_AES_MAC_KEY_SZ*8,
  13714. + ias_keymaxbits: ESP_AES_MAC_KEY_SZ*8,
  13715. + },
  13716. + },
  13717. +#if defined(CONFIG_KLIPS_ENC_AES_MODULE)
  13718. + ixt_module: THIS_MODULE,
  13719. +#endif
  13720. + ixt_a_keylen: ESP_AES_MAC_KEY_SZ,
  13721. + ixt_a_ctx_size: sizeof(aes_context_mac),
  13722. + ixt_a_hmac_set_key: _aes_mac_set_key,
  13723. + ixt_a_hmac_hash:_aes_mac_hash,
  13724. +};
  13725. +#endif /* CONFIG_KLIPS_ENC_AES_MAC */
  13726. +static struct ipsec_alg_enc ipsec_alg_AES = {
  13727. + ixt_common: { ixt_version: IPSEC_ALG_VERSION,
  13728. + ixt_refcnt: ATOMIC_INIT(0),
  13729. + ixt_name: "aes",
  13730. + ixt_blocksize: ESP_AES_CBC_BLK_LEN,
  13731. + ixt_support: {
  13732. + ias_exttype: IPSEC_ALG_TYPE_ENCRYPT,
  13733. + ias_id: ESP_AES,
  13734. + ias_keyminbits: ESP_AES_KEY_SZ_MIN*8,
  13735. + ias_keymaxbits: ESP_AES_KEY_SZ_MAX*8,
  13736. + },
  13737. + },
  13738. +#if defined(CONFIG_KLIPS_ENC_AES_MODULE)
  13739. + ixt_module: THIS_MODULE,
  13740. +#endif
  13741. + ixt_e_keylen: ESP_AES_KEY_SZ_MAX,
  13742. + ixt_e_ctx_size: sizeof(AES_CONTEXT_T),
  13743. + ixt_e_set_key: _aes_set_key,
  13744. + ixt_e_cbc_encrypt:_aes_cbc_encrypt,
  13745. +};
  13746. +
  13747. +#if defined(CONFIG_KLIPS_ENC_AES_MODULE)
  13748. +IPSEC_ALG_MODULE_INIT_MOD( ipsec_aes_init )
  13749. +#else
  13750. +IPSEC_ALG_MODULE_INIT_STATIC( ipsec_aes_init )
  13751. +#endif
  13752. +{
  13753. + int ret, test_ret;
  13754. +
  13755. + if (keyminbits)
  13756. + ipsec_alg_AES.ixt_common.ixt_support.ias_keyminbits=keyminbits;
  13757. + if (keymaxbits) {
  13758. + ipsec_alg_AES.ixt_common.ixt_support.ias_keymaxbits=keymaxbits;
  13759. + if (keymaxbits*8>ipsec_alg_AES.ixt_common.ixt_support.ias_keymaxbits)
  13760. + ipsec_alg_AES.ixt_e_keylen=keymaxbits*8;
  13761. + }
  13762. + if (excl_aes) ipsec_alg_AES.ixt_common.ixt_state |= IPSEC_ALG_ST_EXCL;
  13763. + ret=register_ipsec_alg_enc(&ipsec_alg_AES);
  13764. + printk("ipsec_aes_init(alg_type=%d alg_id=%d name=%s): ret=%d\n",
  13765. + ipsec_alg_AES.ixt_common.ixt_support.ias_exttype,
  13766. + ipsec_alg_AES.ixt_common.ixt_support.ias_id,
  13767. + ipsec_alg_AES.ixt_common.ixt_name,
  13768. + ret);
  13769. + if (ret==0 && test_aes) {
  13770. + test_ret=ipsec_alg_test(
  13771. + ipsec_alg_AES.ixt_common.ixt_support.ias_exttype ,
  13772. + ipsec_alg_AES.ixt_common.ixt_support.ias_id,
  13773. + test_aes);
  13774. + printk("ipsec_aes_init(alg_type=%d alg_id=%d): test_ret=%d\n",
  13775. + ipsec_alg_AES.ixt_common.ixt_support.ias_exttype ,
  13776. + ipsec_alg_AES.ixt_common.ixt_support.ias_id,
  13777. + test_ret);
  13778. + }
  13779. +#if CONFIG_KLIPS_ENC_AES_MAC
  13780. + if (auth_id!=0){
  13781. + int ret;
  13782. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_id=auth_id;
  13783. + ret=register_ipsec_alg_auth(&ipsec_alg_AES_MAC);
  13784. + printk("ipsec_aes_init(alg_type=%d alg_id=%d name=%s): ret=%d\n",
  13785. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_exttype,
  13786. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_id,
  13787. + ipsec_alg_AES_MAC.ixt_common.ixt_name,
  13788. + ret);
  13789. + if (ret==0 && test_aes) {
  13790. + test_ret=ipsec_alg_test(
  13791. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_exttype,
  13792. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_id,
  13793. + test_aes);
  13794. + printk("ipsec_aes_init(alg_type=%d alg_id=%d): test_ret=%d\n",
  13795. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_exttype,
  13796. + ipsec_alg_AES_MAC.ixt_common.ixt_support.ias_id,
  13797. + test_ret);
  13798. + }
  13799. + } else {
  13800. + printk(KERN_DEBUG "klips_debug: experimental ipsec_alg_AES_MAC not registered [Ok] (auth_id=%d)\n", auth_id);
  13801. + }
  13802. +#endif /* CONFIG_KLIPS_ENC_AES_MAC */
  13803. + return ret;
  13804. +}
  13805. +
  13806. +#if defined(CONFIG_KLIPS_ENC_AES_MODULE)
  13807. +IPSEC_ALG_MODULE_EXIT_MOD( ipsec_aes_fini )
  13808. +#else
  13809. +IPSEC_ALG_MODULE_EXIT_STATIC( ipsec_aes_fini )
  13810. +#endif
  13811. +{
  13812. +#if CONFIG_KLIPS_ENC_AES_MAC
  13813. + if (auth_id) unregister_ipsec_alg_auth(&ipsec_alg_AES_MAC);
  13814. +#endif /* CONFIG_KLIPS_ENC_AES_MAC */
  13815. + unregister_ipsec_alg_enc(&ipsec_alg_AES);
  13816. + return;
  13817. +}
  13818. +#ifdef MODULE_LICENSE
  13819. +MODULE_LICENSE("GPL");
  13820. +#endif
  13821. +
  13822. +#if 0 /* +NOT_YET */
  13823. +#ifndef MODULE
  13824. +/*
  13825. + * This is intended for static module setups, currently
  13826. + * doesn't work for modular ipsec.o with static algos inside
  13827. + */
  13828. +static int setup_keybits(const char *str)
  13829. +{
  13830. + unsigned aux;
  13831. + char *end;
  13832. +
  13833. + aux = simple_strtoul(str,&end,0);
  13834. + if (aux != 128 && aux != 192 && aux != 256)
  13835. + return 0;
  13836. + keyminbits = aux;
  13837. +
  13838. + if (*end == 0 || *end != ',')
  13839. + return 1;
  13840. + str=end+1;
  13841. + aux = simple_strtoul(str, NULL, 0);
  13842. + if (aux != 128 && aux != 192 && aux != 256)
  13843. + return 0;
  13844. + if (aux >= keyminbits)
  13845. + keymaxbits = aux;
  13846. + return 1;
  13847. +}
  13848. +__setup("ipsec_aes_keybits=", setup_keybits);
  13849. +#endif
  13850. +#endif
  13851. +
  13852. --- /dev/null Tue Mar 11 13:02:56 2003
  13853. +++ linux/net/ipsec/alg/Config.alg_aes.in Mon Feb 9 13:51:03 2004
  13854. @@ -0,0 +1,3 @@
  13855. +if [ "$CONFIG_IPSEC_ALG" = "y" ]; then
  13856. + tristate ' AES encryption algorithm' CONFIG_IPSEC_ENC_AES
  13857. +fi
  13858. --- /dev/null Tue Mar 11 13:02:56 2003
  13859. +++ linux/net/ipsec/alg/Config.alg_cryptoapi.in Mon Feb 9 13:51:03 2004
  13860. @@ -0,0 +1,6 @@
  13861. +if [ "$CONFIG_IPSEC_ALG" = "y" ]; then
  13862. + dep_tristate ' CRYPTOAPI ciphers support (needs cryptoapi patch)' CONFIG_IPSEC_ALG_CRYPTOAPI $CONFIG_CRYPTO
  13863. + if [ "$CONFIG_IPSEC_ALG_CRYPTOAPI" != "n" ]; then
  13864. + bool ' CRYPTOAPI proprietary ciphers ' CONFIG_IPSEC_ALG_NON_LIBRE
  13865. + fi
  13866. +fi
  13867. --- /dev/null Tue Mar 11 13:02:56 2003
  13868. +++ linux/net/ipsec/alg/Config.in Mon Feb 9 13:51:03 2004
  13869. @@ -0,0 +1,3 @@
  13870. +#Placeholder
  13871. +source net/ipsec/alg/Config.alg_aes.in
  13872. +source net/ipsec/alg/Config.alg_cryptoapi.in
  13873. --- /dev/null Tue Mar 11 13:02:56 2003
  13874. +++ linux/net/ipsec/alg/Makefile Mon Feb 9 13:51:03 2004
  13875. @@ -0,0 +1,112 @@
  13876. +# Makefile,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
  13877. +ifeq ($(strip $(KLIPSMODULE)),)
  13878. +FREESWANSRCDIR=.
  13879. +else
  13880. +FREESWANSRCDIR=../../../..
  13881. +endif
  13882. +ifeq ($(strip $(KLIPS_TOP)),)
  13883. +KLIPS_TOP=../../..
  13884. +override EXTRA_CFLAGS += -I$(KLIPS_TOP)/include
  13885. +endif
  13886. +
  13887. +ifeq ($(CONFIG_IPSEC_DEBUG),y)
  13888. +override EXTRA_CFLAGS += -g
  13889. +endif
  13890. +
  13891. +# LIBCRYPTO normally comes as an argument from "parent" Makefile
  13892. +# (this applies both to FS' "make module" and eg. Linux' "make modules"
  13893. +# But make dep doest follow same evaluations, so we need this default:
  13894. +LIBCRYPTO=$(TOPDIR)/lib/libcrypto
  13895. +
  13896. +override EXTRA_CFLAGS += -I$(LIBCRYPTO)/include
  13897. +override EXTRA_CFLAGS += -Wall -Wpointer-arith -Wstrict-prototypes
  13898. +
  13899. +MOD_LIST_NAME := NET_MISC_MODULES
  13900. +
  13901. +#O_TARGET := static_init.o
  13902. +
  13903. +subdir- :=
  13904. +subdir-n :=
  13905. +subdir-y :=
  13906. +subdir-m :=
  13907. +
  13908. +obj-y := static_init.o
  13909. +
  13910. +ARCH_ASM-y :=
  13911. +ARCH_ASM-$(CONFIG_M586) := i586
  13912. +ARCH_ASM-$(CONFIG_M586TSC) := i586
  13913. +ARCH_ASM-$(CONFIG_M586MMX) := i586
  13914. +ARCH_ASM-$(CONFIG_MK6) := i586
  13915. +ARCH_ASM-$(CONFIG_M686) := i686
  13916. +ARCH_ASM-$(CONFIG_MPENTIUMIII) := i686
  13917. +ARCH_ASM-$(CONFIG_MPENTIUM4) := i686
  13918. +ARCH_ASM-$(CONFIG_MK7) := i686
  13919. +ARCH_ASM-$(CONFIG_MCRUSOE) := i586
  13920. +ARCH_ASM-$(CONFIG_MWINCHIPC6) := i586
  13921. +ARCH_ASM-$(CONFIG_MWINCHIP2) := i586
  13922. +ARCH_ASM-$(CONFIG_MWINCHIP3D) := i586
  13923. +ARCH_ASM-$(CONFIG_USERMODE) := i586
  13924. +
  13925. +ARCH_ASM :=$(ARCH_ASM-y)
  13926. +ifdef NO_ASM
  13927. +ARCH_ASM :=
  13928. +endif
  13929. +
  13930. +# The algorithm makefiles may put dependences, short-circuit them
  13931. +null:
  13932. +
  13933. +makefiles=$(filter-out %.preipsec, $(wildcard Makefile.alg_*))
  13934. +ifneq ($(makefiles),)
  13935. +#include Makefile.alg_aes
  13936. +#include Makefile.alg_aes-opt
  13937. +include $(makefiles)
  13938. +endif
  13939. +
  13940. +# These rules translate from new to old makefile rules
  13941. +# Translate to Rules.make lists.
  13942. +multi-used := $(filter $(list-multi), $(obj-y) $(obj-m))
  13943. +multi-objs := $(foreach m, $(multi-used), $($(basename $(m))-objs))
  13944. +active-objs := $(sort $(multi-objs) $(obj-y) $(obj-m))
  13945. +O_OBJS := $(obj-y)
  13946. +M_OBJS := $(obj-m)
  13947. +MIX_OBJS := $(filter $(export-objs), $(active-objs))
  13948. +#OX_OBJS := $(export-objs)
  13949. +SUB_DIRS := $(subdir-y)
  13950. +ALL_SUB_DIRS := $(subdir-y) $(subdir-m)
  13951. +MOD_SUB_DIRS := $(subdir-m)
  13952. +
  13953. +
  13954. +static_init_mod.o: $(obj-y)
  13955. + rm -f $@
  13956. + $(LD) $(LD_EXTRAFLAGS) $(obj-y) -r -o $@
  13957. +
  13958. +perlasm: ../../../crypto/ciphers/des/asm/perlasm
  13959. + ln -sf $? $@
  13960. +
  13961. +$(obj-y) $(obj-m): $(TOPDIR)/include/linux/config.h $(TOPDIR)/include/linux/autoconf.h $(KLIPS_TOP)/include/freeswan/ipsec_alg.h
  13962. +$(alg_obj-y) $(alg_obj-m): perlasm $(TOPDIR)/include/linux/config.h $(TOPDIR)/include/linux/autoconf.h $(KLIPS_TOP)/include/freeswan/ipsec_alg.h
  13963. +
  13964. +
  13965. +all_alg_modules: perlasm $(ALG_MODULES)
  13966. + @echo "ALG_MODULES=$(ALG_MODULES)"
  13967. +
  13968. +
  13969. +#
  13970. +# Construct alg. init. function: call ipsec_ALGO_init() for every static algo
  13971. +# Needed when there are static algos (with static or modular ipsec.o)
  13972. +#
  13973. +static_init.c: $(TOPDIR)/include/linux/autoconf.h Makefile $(makefiles) scripts/mk-static_init.c.sh
  13974. + @echo "Re-creating $@"
  13975. + $(SHELL) scripts/mk-static_init.c.sh $(static_init-func-y) > $@
  13976. +
  13977. +clean:
  13978. + @for i in $(ALG_SUBDIRS);do test -d $$i && make -C $$i clean;done;exit 0
  13979. + @find . -type l -exec rm -f {} \;
  13980. + -rm -f perlasm
  13981. + -rm -rf $(ALG_SUBDIRS)
  13982. + -rm -f *.o static_init.c
  13983. +
  13984. +ifdef TOPDIR
  13985. +include $(TOPDIR)/Rules.make
  13986. +endif
  13987. +
  13988. --- /dev/null Tue Mar 11 13:02:56 2003
  13989. +++ linux/net/ipsec/alg/Makefile.alg_aes Mon Feb 9 13:51:03 2004
  13990. @@ -0,0 +1,18 @@
  13991. +MOD_AES := ipsec_aes.o
  13992. +
  13993. +ALG_MODULES += $(MOD_AES)
  13994. +ALG_SUBDIRS += libaes
  13995. +
  13996. +obj-$(CONFIG_IPSEC_ALG_AES) += $(MOD_AES)
  13997. +static_init-func-$(CONFIG_IPSEC_ALG_AES)+= ipsec_aes_init
  13998. +alg_obj-$(CONFIG_IPSEC_ALG_AES) += ipsec_alg_aes.o
  13999. +
  14000. +AES_OBJS := ipsec_alg_aes.o $(LIBCRYPTO)/libaes/libaes.a
  14001. +
  14002. +
  14003. +$(MOD_AES): $(AES_OBJS)
  14004. + $(LD) $(EXTRA_LDFLAGS) -r $(AES_OBJS) -o $@
  14005. +
  14006. +$(LIBCRYPTO)/libaes/libaes.a:
  14007. + $(MAKE) -C $(LIBCRYPTO)/libaes CC='$(CC)' 'ARCH_ASM=$(ARCH_ASM)' CFLAGS='$(CFLAGS) $(EXTRA_CFLAGS)' libaes.a
  14008. +
  14009. --- /dev/null Tue Mar 11 13:02:56 2003
  14010. +++ linux/net/ipsec/alg/Makefile.alg_cryptoapi Mon Feb 9 13:51:03 2004
  14011. @@ -0,0 +1,14 @@
  14012. +MOD_CRYPTOAPI := ipsec_cryptoapi.o
  14013. +
  14014. +ifneq ($(wildcard $(TOPDIR)/include/linux/crypto.h),)
  14015. +ALG_MODULES += $(MOD_CRYPTOAPI)
  14016. +obj-$(CONFIG_IPSEC_ALG_CRYPTOAPI) += $(MOD_CRYPTOAPI)
  14017. +static_init-func-$(CONFIG_IPSEC_ALG_CRYPTOAPI)+= ipsec_cryptoapi_init
  14018. +alg_obj-$(CONFIG_IPSEC_ALG_CRYPTOAPI) += ipsec_alg_cryptoapi.o
  14019. +else
  14020. +$(warning "Linux CryptoAPI (2.4.22+ or 2.6.x) not found, not building ipsec_cryptoapi.o")
  14021. +endif
  14022. +
  14023. +CRYPTOAPI_OBJS := ipsec_alg_cryptoapi.o
  14024. +$(MOD_CRYPTOAPI): $(CRYPTOAPI_OBJS)
  14025. + $(LD) -r $(CRYPTOAPI_OBJS) -o $@
  14026. --- /dev/null Tue Mar 11 13:02:56 2003
  14027. +++ linux/net/ipsec/alg/ipsec_alg_cryptoapi.c Mon Feb 9 13:51:03 2004
  14028. @@ -0,0 +1,442 @@
  14029. +/*
  14030. + * ipsec_alg to linux cryptoapi GLUE
  14031. + *
  14032. + * Authors: CODE.ar TEAM
  14033. + * Harpo MAxx <harpo@linuxmendoza.org.ar>
  14034. + * JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  14035. + * Luciano Ruete <docemeses@softhome.net>
  14036. + *
  14037. + * ipsec_alg_cryptoapi.c,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
  14038. + *
  14039. + * This program is free software; you can redistribute it and/or modify it
  14040. + * under the terms of the GNU General Public License as published by the
  14041. + * Free Software Foundation; either version 2 of the License, or (at your
  14042. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  14043. + *
  14044. + * This program is distributed in the hope that it will be useful, but
  14045. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14046. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14047. + * for more details.
  14048. + *
  14049. + * Example usage:
  14050. + * modinfo -p ipsec_cryptoapi (quite useful info, including supported algos)
  14051. + * modprobe ipsec_cryptoapi
  14052. + * modprobe ipsec_cryptoapi test=1
  14053. + * modprobe ipsec_cryptoapi excl=1 (exclusive cipher/algo)
  14054. + * modprobe ipsec_cryptoapi noauto=1 aes=1 twofish=1 (only these ciphers)
  14055. + * modprobe ipsec_cryptoapi aes=128,128 (force these keylens)
  14056. + * modprobe ipsec_cryptoapi des_ede3=0 (everything but 3DES)
  14057. + */
  14058. +#ifndef AUTOCONF_INCLUDED
  14059. +#include <linux/config.h>
  14060. +#endif
  14061. +#include <linux/version.h>
  14062. +
  14063. +/*
  14064. + * special case: ipsec core modular with this static algo inside:
  14065. + * must avoid MODULE magic for this file
  14066. + */
  14067. +#if CONFIG_IPSEC_MODULE && CONFIG_IPSEC_ALG_CRYPTOAPI
  14068. +#undef MODULE
  14069. +#endif
  14070. +
  14071. +#include <linux/module.h>
  14072. +#include <linux/init.h>
  14073. +
  14074. +#include <linux/kernel.h> /* printk() */
  14075. +#include <linux/errno.h> /* error codes */
  14076. +#include <linux/types.h> /* size_t */
  14077. +#include <linux/string.h>
  14078. +
  14079. +/* Check if __exit is defined, if not null it */
  14080. +#ifndef __exit
  14081. +#define __exit
  14082. +#endif
  14083. +
  14084. +/* warn the innocent */
  14085. +#if !defined (CONFIG_CRYPTO) && !defined (CONFIG_CRYPTO_MODULE)
  14086. +#warning "No linux CryptoAPI found, install 2.4.22+ or 2.6.x"
  14087. +#define NO_CRYPTOAPI_SUPPORT
  14088. +#endif
  14089. +/* Low freeswan header coupling */
  14090. +#include "openswan/ipsec_alg.h"
  14091. +
  14092. +#include <linux/crypto.h>
  14093. +#ifdef CRYPTO_API_VERSION_CODE
  14094. +#warning "Old CryptoAPI is not supported. Only linux-2.4.22+ or linux-2.6.x are supported"
  14095. +#define NO_CRYPTOAPI_SUPPORT
  14096. +#endif
  14097. +
  14098. +#ifdef NO_CRYPTOAPI_SUPPORT
  14099. +#warning "Building an unusable module :P"
  14100. +/* Catch old CryptoAPI by not allowing module to load */
  14101. +IPSEC_ALG_MODULE_INIT( ipsec_cryptoapi_init )
  14102. +{
  14103. + printk(KERN_WARNING "ipsec_cryptoapi.o was not built on stock Linux CryptoAPI (2.4.22+ or 2.6.x), not loading.\n");
  14104. + return -EINVAL;
  14105. +}
  14106. +#else
  14107. +#include <asm/scatterlist.h>
  14108. +#include <asm/pgtable.h>
  14109. +#include <linux/mm.h>
  14110. +
  14111. +#define CIPHERNAME_AES "aes"
  14112. +#define CIPHERNAME_3DES "des3_ede"
  14113. +#define CIPHERNAME_BLOWFISH "blowfish"
  14114. +#define CIPHERNAME_CAST "cast5"
  14115. +#define CIPHERNAME_SERPENT "serpent"
  14116. +#define CIPHERNAME_TWOFISH "twofish"
  14117. +
  14118. +#define ESP_3DES 3
  14119. +#define ESP_AES 12
  14120. +#define ESP_BLOWFISH 7 /* truely _constant_ :) */
  14121. +#define ESP_CAST 6 /* quite constant :) */
  14122. +#define ESP_SERPENT 252 /* from ipsec drafts */
  14123. +#define ESP_TWOFISH 253 /* from ipsec drafts */
  14124. +
  14125. +#define AH_MD5 2
  14126. +#define AH_SHA 3
  14127. +#define DIGESTNAME_MD5 "md5"
  14128. +#define DIGESTNAME_SHA1 "sha1"
  14129. +
  14130. +MODULE_AUTHOR("Juanjo Ciarlante, Harpo MAxx, Luciano Ruete");
  14131. +static int debug=0;
  14132. +static int test=0;
  14133. +static int excl=0;
  14134. +static int noauto = 0;
  14135. +
  14136. +static int des_ede3[] = {-1, -1};
  14137. +static int aes[] = {-1, -1};
  14138. +static int blowfish[] = {-1, -1};
  14139. +static int cast[] = {-1, -1};
  14140. +static int serpent[] = {-1, -1};
  14141. +static int twofish[] = {-1, -1};
  14142. +
  14143. +#ifdef module_param
  14144. +module_param(debug,int,0600);
  14145. +module_param(test,int,0600);
  14146. +module_param(ebug,int,0600);
  14147. +
  14148. +module_param(noauto,int,0600);
  14149. +module_param(ebug,int,0600);
  14150. +
  14151. +module_param_array(des_ede3,int,NULL,0);
  14152. +module_param(aes,int,NULL,0);
  14153. +module_param(blowfish,int,NULL,0);
  14154. +module_param(cast,int,NULL,0);
  14155. +module_param(serpent,int,NULL,0);
  14156. +module_param(twofish,int,NULL,0);
  14157. +#else
  14158. +MODULE_PARM(debug, "i");
  14159. +MODULE_PARM(test, "i");
  14160. +MODULE_PARM(excl, "i");
  14161. +
  14162. +MODULE_PARM(noauto,"i");
  14163. +
  14164. +MODULE_PARM(des_ede3,"1-2i");
  14165. +MODULE_PARM(aes,"1-2i");
  14166. +MODULE_PARM(blowfish,"1-2i");
  14167. +MODULE_PARM(cast,"1-2i");
  14168. +MODULE_PARM(serpent,"1-2i");
  14169. +MODULE_PARM(twofish,"1-2i");
  14170. +#endif
  14171. +
  14172. +MODULE_PARM_DESC(noauto, "Dont try all known algos, just setup enabled ones");
  14173. +
  14174. +MODULE_PARM_DESC(des_ede3, "0: disable | 1: force_enable | min,max: dontuse");
  14175. +MODULE_PARM_DESC(aes, "0: disable | 1: force_enable | min,max: keybitlens");
  14176. +MODULE_PARM_DESC(blowfish, "0: disable | 1: force_enable | min,max: keybitlens");
  14177. +MODULE_PARM_DESC(cast, "0: disable | 1: force_enable | min,max: keybitlens");
  14178. +MODULE_PARM_DESC(serpent, "0: disable | 1: force_enable | min,max: keybitlens");
  14179. +MODULE_PARM_DESC(twofish, "0: disable | 1: force_enable | min,max: keybitlens");
  14180. +
  14181. +struct ipsec_alg_capi_cipher {
  14182. + const char *ciphername; /* cryptoapi's ciphername */
  14183. + unsigned blocksize;
  14184. + unsigned short minbits;
  14185. + unsigned short maxbits;
  14186. + int *parm; /* lkm param for this cipher */
  14187. + struct ipsec_alg_enc alg; /* note it's not a pointer */
  14188. +};
  14189. +static struct ipsec_alg_capi_cipher alg_capi_carray[] = {
  14190. + { CIPHERNAME_AES , 16, 128, 256, aes , { ixt_alg_id: ESP_AES, }},
  14191. + { CIPHERNAME_TWOFISH , 16, 128, 256, twofish, { ixt_alg_id: ESP_TWOFISH, }},
  14192. + { CIPHERNAME_SERPENT , 16, 128, 256, serpent, { ixt_alg_id: ESP_SERPENT, }},
  14193. + { CIPHERNAME_CAST , 8, 128, 128, cast , { ixt_alg_id: ESP_CAST, }},
  14194. + { CIPHERNAME_BLOWFISH , 8, 96, 448, blowfish,{ ixt_alg_id: ESP_BLOWFISH, }},
  14195. + { CIPHERNAME_3DES , 8, 192, 192, des_ede3,{ ixt_alg_id: ESP_3DES, }},
  14196. + { NULL, 0, 0, 0, NULL, {} }
  14197. +};
  14198. +#ifdef NOT_YET
  14199. +struct ipsec_alg_capi_digest {
  14200. + const char *digestname; /* cryptoapi's digestname */
  14201. + struct digest_implementation *di;
  14202. + struct ipsec_alg_auth alg; /* note it's not a pointer */
  14203. +};
  14204. +static struct ipsec_alg_capi_cipher alg_capi_darray[] = {
  14205. + { DIGESTNAME_MD5, NULL, { ixt_alg_id: AH_MD5, }},
  14206. + { DIGESTNAME_SHA1, NULL, { ixt_alg_id: AH_SHA, }},
  14207. + { NULL, NULL, {} }
  14208. +};
  14209. +#endif
  14210. +/*
  14211. + * "generic" linux cryptoapi setup_cipher() function
  14212. + */
  14213. +int setup_cipher(const char *ciphername)
  14214. +{
  14215. + return crypto_alg_available(ciphername, 0);
  14216. +}
  14217. +
  14218. +/*
  14219. + * setups ipsec_alg_capi_cipher "hyper" struct components, calling
  14220. + * register_ipsec_alg for cointaned ipsec_alg object
  14221. + */
  14222. +static void _capi_destroy_key (struct ipsec_alg_enc *alg, __u8 *key_e);
  14223. +static __u8 * _capi_new_key (struct ipsec_alg_enc *alg, const __u8 *key, size_t keylen);
  14224. +static int _capi_cbc_encrypt(struct ipsec_alg_enc *alg, __u8 * key_e, __u8 * in, int ilen, const __u8 * iv, int encrypt);
  14225. +
  14226. +static int
  14227. +setup_ipsec_alg_capi_cipher(struct ipsec_alg_capi_cipher *cptr)
  14228. +{
  14229. + int ret;
  14230. + cptr->alg.ixt_version = IPSEC_ALG_VERSION;
  14231. + cptr->alg.ixt_module = THIS_MODULE;
  14232. + atomic_set (& cptr->alg.ixt_refcnt, 0);
  14233. + strncpy (cptr->alg.ixt_name , cptr->ciphername, sizeof (cptr->alg.ixt_name));
  14234. +
  14235. + cptr->alg.ixt_blocksize=cptr->blocksize;
  14236. + cptr->alg.ixt_keyminbits=cptr->minbits;
  14237. + cptr->alg.ixt_keymaxbits=cptr->maxbits;
  14238. + cptr->alg.ixt_state = 0;
  14239. + if (excl) cptr->alg.ixt_state |= IPSEC_ALG_ST_EXCL;
  14240. + cptr->alg.ixt_e_keylen=cptr->alg.ixt_keymaxbits/8;
  14241. + cptr->alg.ixt_e_ctx_size = 0;
  14242. + cptr->alg.ixt_alg_type = IPSEC_ALG_TYPE_ENCRYPT;
  14243. + cptr->alg.ixt_e_new_key = _capi_new_key;
  14244. + cptr->alg.ixt_e_destroy_key = _capi_destroy_key;
  14245. + cptr->alg.ixt_e_cbc_encrypt = _capi_cbc_encrypt;
  14246. + cptr->alg.ixt_data = cptr;
  14247. +
  14248. + ret=register_ipsec_alg_enc(&cptr->alg);
  14249. + printk("setup_ipsec_alg_capi_cipher(): "
  14250. + "alg_type=%d alg_id=%d name=%s "
  14251. + "keyminbits=%d keymaxbits=%d, ret=%d\n",
  14252. + cptr->alg.ixt_alg_type,
  14253. + cptr->alg.ixt_alg_id,
  14254. + cptr->alg.ixt_name,
  14255. + cptr->alg.ixt_keyminbits,
  14256. + cptr->alg.ixt_keymaxbits,
  14257. + ret);
  14258. + return ret;
  14259. +}
  14260. +/*
  14261. + * called in ipsec_sa_wipe() time, will destroy key contexts
  14262. + * and do 1 unbind()
  14263. + */
  14264. +static void
  14265. +_capi_destroy_key (struct ipsec_alg_enc *alg, __u8 *key_e)
  14266. +{
  14267. + struct crypto_tfm *tfm=(struct crypto_tfm*)key_e;
  14268. +
  14269. + if (debug > 0)
  14270. + printk(KERN_DEBUG "klips_debug: _capi_destroy_key:"
  14271. + "name=%s key_e=%p \n",
  14272. + alg->ixt_name, key_e);
  14273. + if (!key_e) {
  14274. + printk(KERN_ERR "klips_debug: _capi_destroy_key:"
  14275. + "name=%s NULL key_e!\n",
  14276. + alg->ixt_name);
  14277. + return;
  14278. + }
  14279. + crypto_free_tfm(tfm);
  14280. +}
  14281. +
  14282. +/*
  14283. + * create new key context, need alg->ixt_data to know which
  14284. + * (of many) cipher inside this module is the target
  14285. + */
  14286. +static __u8 *
  14287. +_capi_new_key (struct ipsec_alg_enc *alg, const __u8 *key, size_t keylen)
  14288. +{
  14289. + struct ipsec_alg_capi_cipher *cptr;
  14290. + struct crypto_tfm *tfm=NULL;
  14291. +
  14292. + cptr = alg->ixt_data;
  14293. + if (!cptr) {
  14294. + printk(KERN_ERR "_capi_new_key(): "
  14295. + "NULL ixt_data (?!) for \"%s\" algo\n"
  14296. + , alg->ixt_name);
  14297. + goto err;
  14298. + }
  14299. + if (debug > 0)
  14300. + printk(KERN_DEBUG "klips_debug:_capi_new_key:"
  14301. + "name=%s cptr=%p key=%p keysize=%d\n",
  14302. + alg->ixt_name, cptr, key, keylen);
  14303. +
  14304. + /*
  14305. + * alloc tfm
  14306. + */
  14307. + tfm = crypto_alloc_tfm(cptr->ciphername, CRYPTO_TFM_MODE_CBC);
  14308. + if (!tfm) {
  14309. + printk(KERN_ERR "_capi_new_key(): "
  14310. + "NULL tfm for \"%s\" cryptoapi (\"%s\") algo\n"
  14311. + , alg->ixt_name, cptr->ciphername);
  14312. + goto err;
  14313. + }
  14314. + if (crypto_cipher_setkey(tfm, key, keylen) < 0) {
  14315. + printk(KERN_ERR "_capi_new_key(): "
  14316. + "failed new_key() for \"%s\" cryptoapi algo (keylen=%d)\n"
  14317. + , alg->ixt_name, keylen);
  14318. + crypto_free_tfm(tfm);
  14319. + tfm=NULL;
  14320. + }
  14321. +err:
  14322. + if (debug > 0)
  14323. + printk(KERN_DEBUG "klips_debug:_capi_new_key:"
  14324. + "name=%s key=%p keylen=%d tfm=%p\n",
  14325. + alg->ixt_name, key, keylen, tfm);
  14326. + return (__u8 *) tfm;
  14327. +}
  14328. +/*
  14329. + * core encryption function: will use cx->ci to call actual cipher's
  14330. + * cbc function
  14331. + */
  14332. +static int
  14333. +_capi_cbc_encrypt(struct ipsec_alg_enc *alg, __u8 * key_e, __u8 * in, int ilen, const __u8 * iv, int encrypt) {
  14334. + int error =0;
  14335. + struct crypto_tfm *tfm=(struct crypto_tfm *)key_e;
  14336. + struct scatterlist sg = {
  14337. + .page = virt_to_page(in),
  14338. + .offset = (unsigned long)(in) % PAGE_SIZE,
  14339. + .length=ilen,
  14340. + };
  14341. + if (debug > 1)
  14342. + printk(KERN_DEBUG "klips_debug:_capi_cbc_encrypt:"
  14343. + "key_e=%p "
  14344. + "in=%p out=%p ilen=%d iv=%p encrypt=%d\n"
  14345. + , key_e
  14346. + , in, in, ilen, iv, encrypt);
  14347. + crypto_cipher_set_iv(tfm, iv, crypto_tfm_alg_ivsize(tfm));
  14348. + if (encrypt)
  14349. + error = crypto_cipher_encrypt (tfm, &sg, &sg, ilen);
  14350. + else
  14351. + error = crypto_cipher_decrypt (tfm, &sg, &sg, ilen);
  14352. + if (debug > 1)
  14353. + printk(KERN_DEBUG "klips_debug:_capi_cbc_encrypt:"
  14354. + "error=%d\n"
  14355. + , error);
  14356. + return (error<0)? error : ilen;
  14357. +}
  14358. +/*
  14359. + * main initialization loop: for each cipher in list, do
  14360. + * 1) setup cryptoapi cipher else continue
  14361. + * 2) register ipsec_alg object
  14362. + */
  14363. +static int
  14364. +setup_cipher_list (struct ipsec_alg_capi_cipher* clist)
  14365. +{
  14366. + struct ipsec_alg_capi_cipher *cptr;
  14367. + /* foreach cipher in list ... */
  14368. + for (cptr=clist;cptr->ciphername;cptr++) {
  14369. + /*
  14370. + * see if cipher has been disabled (0) or
  14371. + * if noauto set and not enabled (1)
  14372. + */
  14373. + if (cptr->parm[0] == 0 || (noauto && cptr->parm[0] < 0)) {
  14374. + if (debug>0)
  14375. + printk(KERN_INFO "setup_cipher_list(): "
  14376. + "ciphername=%s skipped at user request: "
  14377. + "noauto=%d parm[0]=%d parm[1]=%d\n"
  14378. + , cptr->ciphername
  14379. + , noauto
  14380. + , cptr->parm[0]
  14381. + , cptr->parm[1]);
  14382. + continue;
  14383. + }
  14384. + /*
  14385. + * use a local ci to avoid touching cptr->ci,
  14386. + * if register ipsec_alg success then bind cipher
  14387. + */
  14388. + if( setup_cipher(cptr->ciphername) ) {
  14389. + if (debug > 0)
  14390. + printk(KERN_DEBUG "klips_debug:"
  14391. + "setup_cipher_list():"
  14392. + "ciphername=%s found\n"
  14393. + , cptr->ciphername);
  14394. + if (setup_ipsec_alg_capi_cipher(cptr) == 0) {
  14395. +
  14396. +
  14397. + } else {
  14398. + printk(KERN_ERR "klips_debug:"
  14399. + "setup_cipher_list():"
  14400. + "ciphername=%s failed ipsec_alg_register\n"
  14401. + , cptr->ciphername);
  14402. + }
  14403. + } else {
  14404. + if (debug>0)
  14405. + printk(KERN_INFO "setup_cipher_list(): lookup for ciphername=%s: not found \n",
  14406. + cptr->ciphername);
  14407. + }
  14408. + }
  14409. + return 0;
  14410. +}
  14411. +/*
  14412. + * deregister ipsec_alg objects and unbind ciphers
  14413. + */
  14414. +static int
  14415. +unsetup_cipher_list (struct ipsec_alg_capi_cipher* clist)
  14416. +{
  14417. + struct ipsec_alg_capi_cipher *cptr;
  14418. + /* foreach cipher in list ... */
  14419. + for (cptr=clist;cptr->ciphername;cptr++) {
  14420. + if (cptr->alg.ixt_state & IPSEC_ALG_ST_REGISTERED) {
  14421. + unregister_ipsec_alg_enc(&cptr->alg);
  14422. + }
  14423. + }
  14424. + return 0;
  14425. +}
  14426. +/*
  14427. + * test loop for registered algos
  14428. + */
  14429. +static int
  14430. +test_cipher_list (struct ipsec_alg_capi_cipher* clist)
  14431. +{
  14432. + int test_ret;
  14433. + struct ipsec_alg_capi_cipher *cptr;
  14434. + /* foreach cipher in list ... */
  14435. + for (cptr=clist;cptr->ciphername;cptr++) {
  14436. + if (cptr->alg.ixt_state & IPSEC_ALG_ST_REGISTERED) {
  14437. + test_ret=ipsec_alg_test(
  14438. + cptr->alg.ixt_alg_type,
  14439. + cptr->alg.ixt_alg_id,
  14440. + test);
  14441. + printk("test_cipher_list(alg_type=%d alg_id=%d): test_ret=%d\n",
  14442. + cptr->alg.ixt_alg_type,
  14443. + cptr->alg.ixt_alg_id,
  14444. + test_ret);
  14445. + }
  14446. + }
  14447. + return 0;
  14448. +}
  14449. +
  14450. +IPSEC_ALG_MODULE_INIT( ipsec_cryptoapi_init )
  14451. +{
  14452. + int ret, test_ret;
  14453. + if ((ret=setup_cipher_list(alg_capi_carray)) < 0)
  14454. + return -EPROTONOSUPPORT;
  14455. + if (ret==0 && test) {
  14456. + test_ret=test_cipher_list(alg_capi_carray);
  14457. + }
  14458. + return ret;
  14459. +}
  14460. +IPSEC_ALG_MODULE_EXIT( ipsec_cryptoapi_fini )
  14461. +{
  14462. + unsetup_cipher_list(alg_capi_carray);
  14463. + return;
  14464. +}
  14465. +#ifdef MODULE_LICENSE
  14466. +MODULE_LICENSE("GPL");
  14467. +#endif
  14468. +
  14469. +EXPORT_NO_SYMBOLS;
  14470. +#endif /* NO_CRYPTOAPI_SUPPORT */
  14471. --- /dev/null Tue Mar 11 13:02:56 2003
  14472. +++ linux/net/ipsec/alg/scripts/mk-static_init.c.sh Mon Feb 9 13:51:03 2004
  14473. @@ -0,0 +1,18 @@
  14474. +#!/bin/sh
  14475. +cat << EOF
  14476. +#include <linux/kernel.h>
  14477. +#include <linux/list.h>
  14478. +#include "freeswan/ipsec_alg.h"
  14479. +$(for i in $*; do
  14480. + test -z "$i" && continue
  14481. + echo "extern int $i(void);"
  14482. +done)
  14483. +void ipsec_alg_static_init(void){
  14484. + int __attribute__ ((unused)) err=0;
  14485. +$(for i in $*; do
  14486. + test -z "$i" && continue
  14487. + echo " if ((err=$i()) < 0)"
  14488. + echo " printk(KERN_WARNING \"$i() returned %d\", err);"
  14489. +done)
  14490. +}
  14491. +EOF
  14492. --- /dev/null Tue Mar 11 13:02:56 2003
  14493. +++ linux/net/ipsec/anyaddr.c Mon Feb 9 13:51:03 2004
  14494. @@ -0,0 +1,148 @@
  14495. +/*
  14496. + * special addresses
  14497. + * Copyright (C) 2000 Henry Spencer.
  14498. + *
  14499. + * This library is free software; you can redistribute it and/or modify it
  14500. + * under the terms of the GNU Library General Public License as published by
  14501. + * the Free Software Foundation; either version 2 of the License, or (at your
  14502. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  14503. + *
  14504. + * This library is distributed in the hope that it will be useful, but
  14505. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14506. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  14507. + * License for more details.
  14508. + *
  14509. + * RCSID $Id: anyaddr.c,v 1.10.10.1 2006/11/24 05:55:46 paul Exp $
  14510. + */
  14511. +#include "openswan.h"
  14512. +
  14513. +/* these are mostly fallbacks for the no-IPv6-support-in-library case */
  14514. +#ifndef IN6ADDR_ANY_INIT
  14515. +#define IN6ADDR_ANY_INIT {{{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }}}
  14516. +#endif
  14517. +#ifndef IN6ADDR_LOOPBACK_INIT
  14518. +#define IN6ADDR_LOOPBACK_INIT {{{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }}}
  14519. +#endif
  14520. +
  14521. +static struct in6_addr v6any = IN6ADDR_ANY_INIT;
  14522. +static struct in6_addr v6loop = IN6ADDR_LOOPBACK_INIT;
  14523. +
  14524. +/*
  14525. + - anyaddr - initialize to the any-address value
  14526. + */
  14527. +err_t /* NULL for success, else string literal */
  14528. +anyaddr(af, dst)
  14529. +int af; /* address family */
  14530. +ip_address *dst;
  14531. +{
  14532. + uint32_t v4any = htonl(INADDR_ANY);
  14533. +
  14534. + switch (af) {
  14535. + case AF_INET:
  14536. + return initaddr((unsigned char *)&v4any, sizeof(v4any), af, dst);
  14537. + break;
  14538. + case AF_INET6:
  14539. + return initaddr((unsigned char *)&v6any, sizeof(v6any), af, dst);
  14540. + break;
  14541. + default:
  14542. + return "unknown address family in anyaddr/unspecaddr";
  14543. + break;
  14544. + }
  14545. +}
  14546. +
  14547. +/*
  14548. + - unspecaddr - initialize to the unspecified-address value
  14549. + */
  14550. +err_t /* NULL for success, else string literal */
  14551. +unspecaddr(af, dst)
  14552. +int af; /* address family */
  14553. +ip_address *dst;
  14554. +{
  14555. + return anyaddr(af, dst);
  14556. +}
  14557. +
  14558. +/*
  14559. + - loopbackaddr - initialize to the loopback-address value
  14560. + */
  14561. +err_t /* NULL for success, else string literal */
  14562. +loopbackaddr(af, dst)
  14563. +int af; /* address family */
  14564. +ip_address *dst;
  14565. +{
  14566. + uint32_t v4loop = htonl(INADDR_LOOPBACK);
  14567. +
  14568. + switch (af) {
  14569. + case AF_INET:
  14570. + return initaddr((unsigned char *)&v4loop, sizeof(v4loop), af, dst);
  14571. + break;
  14572. + case AF_INET6:
  14573. + return initaddr((unsigned char *)&v6loop, sizeof(v6loop), af, dst);
  14574. + break;
  14575. + default:
  14576. + return "unknown address family in loopbackaddr";
  14577. + break;
  14578. + }
  14579. +}
  14580. +
  14581. +/*
  14582. + - isanyaddr - test for the any-address value
  14583. + */
  14584. +int
  14585. +isanyaddr(src)
  14586. +const ip_address *src;
  14587. +{
  14588. + uint32_t v4any = htonl(INADDR_ANY);
  14589. + int cmp;
  14590. +
  14591. + switch (src->u.v4.sin_family) {
  14592. + case AF_INET:
  14593. + cmp = memcmp(&src->u.v4.sin_addr.s_addr, &v4any, sizeof(v4any));
  14594. + break;
  14595. + case AF_INET6:
  14596. + cmp = memcmp(&src->u.v6.sin6_addr, &v6any, sizeof(v6any));
  14597. + break;
  14598. + case 0:
  14599. + /* a zeroed structure is considered any address */
  14600. + return 1;
  14601. + default:
  14602. + return 0;
  14603. + break;
  14604. + }
  14605. +
  14606. + return (cmp == 0) ? 1 : 0;
  14607. +}
  14608. +
  14609. +/*
  14610. + - isunspecaddr - test for the unspecified-address value
  14611. + */
  14612. +int
  14613. +isunspecaddr(src)
  14614. +const ip_address *src;
  14615. +{
  14616. + return isanyaddr(src);
  14617. +}
  14618. +
  14619. +/*
  14620. + - isloopbackaddr - test for the loopback-address value
  14621. + */
  14622. +int
  14623. +isloopbackaddr(src)
  14624. +const ip_address *src;
  14625. +{
  14626. + uint32_t v4loop = htonl(INADDR_LOOPBACK);
  14627. + int cmp;
  14628. +
  14629. + switch (src->u.v4.sin_family) {
  14630. + case AF_INET:
  14631. + cmp = memcmp(&src->u.v4.sin_addr.s_addr, &v4loop, sizeof(v4loop));
  14632. + break;
  14633. + case AF_INET6:
  14634. + cmp = memcmp(&src->u.v6.sin6_addr, &v6loop, sizeof(v6loop));
  14635. + break;
  14636. + default:
  14637. + return 0;
  14638. + break;
  14639. + }
  14640. +
  14641. + return (cmp == 0) ? 1 : 0;
  14642. +}
  14643. --- /dev/null Tue Mar 11 13:02:56 2003
  14644. +++ linux/net/ipsec/datatot.c Mon Feb 9 13:51:03 2004
  14645. @@ -0,0 +1,234 @@
  14646. +/*
  14647. + * convert from binary data (e.g. key) to text form
  14648. + * Copyright (C) 2000 Henry Spencer.
  14649. + *
  14650. + * This library is free software; you can redistribute it and/or modify it
  14651. + * under the terms of the GNU Library General Public License as published by
  14652. + * the Free Software Foundation; either version 2 of the License, or (at your
  14653. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  14654. + *
  14655. + * This library is distributed in the hope that it will be useful, but
  14656. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14657. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  14658. + * License for more details.
  14659. + *
  14660. + * RCSID $Id: datatot.c,v 1.7 2005/04/14 20:48:43 mcr Exp $
  14661. + */
  14662. +#include "openswan.h"
  14663. +
  14664. +static void convert(const char *src, size_t nreal, int format, char *out);
  14665. +
  14666. +/*
  14667. + - datatot - convert data bytes to text
  14668. + */
  14669. +size_t /* true length (with NUL) for success */
  14670. +datatot(src, srclen, format, dst, dstlen)
  14671. +const char *src;
  14672. +size_t srclen;
  14673. +int format; /* character indicating what format */
  14674. +char *dst; /* need not be valid if dstlen is 0 */
  14675. +size_t dstlen;
  14676. +{
  14677. + size_t inblocksize; /* process this many bytes at a time */
  14678. + size_t outblocksize; /* producing this many */
  14679. + size_t breakevery; /* add a _ every this many (0 means don't) */
  14680. + size_t sincebreak; /* output bytes since last _ */
  14681. + char breakchar; /* character used to break between groups */
  14682. + char inblock[10]; /* enough for any format */
  14683. + char outblock[10]; /* enough for any format */
  14684. + char fake[1]; /* fake output area for dstlen == 0 */
  14685. + size_t needed; /* return value */
  14686. + char *stop; /* where the terminating NUL will go */
  14687. + size_t ntodo; /* remaining input */
  14688. + size_t nreal;
  14689. + char *out;
  14690. + char *prefix;
  14691. +
  14692. + breakevery = 0;
  14693. + breakchar = '_';
  14694. +
  14695. + switch (format) {
  14696. + case 0:
  14697. + case 'h':
  14698. + format = 'x';
  14699. + breakevery = 8;
  14700. + /* FALLTHROUGH */
  14701. + case 'x':
  14702. + inblocksize = 1;
  14703. + outblocksize = 2;
  14704. + prefix = "0x";
  14705. + break;
  14706. + case ':':
  14707. + format = 'x';
  14708. + breakevery = 2;
  14709. + breakchar = ':';
  14710. + /* FALLTHROUGH */
  14711. + case 16:
  14712. + inblocksize = 1;
  14713. + outblocksize = 2;
  14714. + prefix = "";
  14715. + format = 'x';
  14716. + break;
  14717. + case 's':
  14718. + inblocksize = 3;
  14719. + outblocksize = 4;
  14720. + prefix = "0s";
  14721. + break;
  14722. + case 64: /* beware, equals ' ' */
  14723. + inblocksize = 3;
  14724. + outblocksize = 4;
  14725. + prefix = "";
  14726. + format = 's';
  14727. + break;
  14728. + default:
  14729. + return 0;
  14730. + break;
  14731. + }
  14732. +
  14733. + user_assert(inblocksize < sizeof(inblock));
  14734. + user_assert(outblocksize < sizeof(outblock));
  14735. + user_assert(breakevery % outblocksize == 0);
  14736. +
  14737. + if (srclen == 0)
  14738. + return 0;
  14739. + ntodo = srclen;
  14740. +
  14741. + if (dstlen == 0) { /* dispose of awkward special case */
  14742. + dst = fake;
  14743. + dstlen = 1;
  14744. + }
  14745. + stop = dst + dstlen - 1;
  14746. +
  14747. + nreal = strlen(prefix);
  14748. + needed = nreal; /* for starters */
  14749. + if (dstlen <= nreal) { /* prefix won't fit */
  14750. + strncpy(dst, prefix, dstlen - 1);
  14751. + dst += dstlen - 1;
  14752. + } else {
  14753. + strcpy(dst, prefix);
  14754. + dst += nreal;
  14755. + }
  14756. +
  14757. + user_assert(dst <= stop);
  14758. + sincebreak = 0;
  14759. +
  14760. + while (ntodo > 0) {
  14761. + if (ntodo < inblocksize) { /* incomplete input */
  14762. + memset(inblock, 0, sizeof(inblock));
  14763. + memcpy(inblock, src, ntodo);
  14764. + src = inblock;
  14765. + nreal = ntodo;
  14766. + ntodo = inblocksize;
  14767. + } else
  14768. + nreal = inblocksize;
  14769. + out = (outblocksize > stop - dst) ? outblock : dst;
  14770. +
  14771. + convert(src, nreal, format, out);
  14772. + needed += outblocksize;
  14773. + sincebreak += outblocksize;
  14774. + if (dst < stop) {
  14775. + if (out != dst) {
  14776. + user_assert(outblocksize > stop - dst);
  14777. + memcpy(dst, out, stop - dst);
  14778. + dst = stop;
  14779. + } else
  14780. + dst += outblocksize;
  14781. + }
  14782. +
  14783. + src += inblocksize;
  14784. + ntodo -= inblocksize;
  14785. + if (breakevery != 0 && sincebreak >= breakevery && ntodo > 0) {
  14786. + if (dst < stop)
  14787. + *dst++ = breakchar;
  14788. + needed++;
  14789. + sincebreak = 0;
  14790. + }
  14791. + }
  14792. +
  14793. + user_assert(dst <= stop);
  14794. + *dst++ = '\0';
  14795. + needed++;
  14796. +
  14797. + return needed;
  14798. +}
  14799. +
  14800. +/*
  14801. + - convert - convert one input block to one output block
  14802. + */
  14803. +static void
  14804. +convert(src, nreal, format, out)
  14805. +const char *src;
  14806. +size_t nreal; /* how much of the input block is real */
  14807. +int format;
  14808. +char *out;
  14809. +{
  14810. + static char hex[] = "0123456789abcdef";
  14811. + static char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  14812. + "abcdefghijklmnopqrstuvwxyz"
  14813. + "0123456789+/";
  14814. + unsigned char c;
  14815. + unsigned char c1, c2, c3;
  14816. +
  14817. + user_assert(nreal > 0);
  14818. + switch (format) {
  14819. + case 'x':
  14820. + user_assert(nreal == 1);
  14821. + c = (unsigned char)*src;
  14822. + *out++ = hex[c >> 4];
  14823. + *out++ = hex[c & 0xf];
  14824. + break;
  14825. + case 's':
  14826. + c1 = (unsigned char)*src++;
  14827. + c2 = (unsigned char)*src++;
  14828. + c3 = (unsigned char)*src++;
  14829. + *out++ = base64[c1 >> 2]; /* top 6 bits of c1 */
  14830. + c = (c1 & 0x3) << 4; /* bottom 2 of c1... */
  14831. + c |= c2 >> 4; /* ...top 4 of c2 */
  14832. + *out++ = base64[c];
  14833. + if (nreal == 1)
  14834. + *out++ = '=';
  14835. + else {
  14836. + c = (c2 & 0xf) << 2; /* bottom 4 of c2... */
  14837. + c |= c3 >> 6; /* ...top 2 of c3 */
  14838. + *out++ = base64[c];
  14839. + }
  14840. + if (nreal <= 2)
  14841. + *out++ = '=';
  14842. + else
  14843. + *out++ = base64[c3 & 0x3f]; /* bottom 6 of c3 */
  14844. + break;
  14845. + default:
  14846. + user_assert(nreal == 0); /* unknown format */
  14847. + break;
  14848. + }
  14849. +}
  14850. +
  14851. +/*
  14852. + - datatoa - convert data to ASCII
  14853. + * backward-compatibility synonym for datatot
  14854. + */
  14855. +size_t /* true length (with NUL) for success */
  14856. +datatoa(src, srclen, format, dst, dstlen)
  14857. +const char *src;
  14858. +size_t srclen;
  14859. +int format; /* character indicating what format */
  14860. +char *dst; /* need not be valid if dstlen is 0 */
  14861. +size_t dstlen;
  14862. +{
  14863. + return datatot(src, srclen, format, dst, dstlen);
  14864. +}
  14865. +
  14866. +/*
  14867. + - bytestoa - convert data bytes to ASCII
  14868. + * backward-compatibility synonym for datatot
  14869. + */
  14870. +size_t /* true length (with NUL) for success */
  14871. +bytestoa(src, srclen, format, dst, dstlen)
  14872. +const char *src;
  14873. +size_t srclen;
  14874. +int format; /* character indicating what format */
  14875. +char *dst; /* need not be valid if dstlen is 0 */
  14876. +size_t dstlen;
  14877. +{
  14878. + return datatot(src, srclen, format, dst, dstlen);
  14879. +}
  14880. --- /dev/null Tue Mar 11 13:02:56 2003
  14881. +++ linux/net/ipsec/defconfig Mon Feb 9 13:51:03 2004
  14882. @@ -0,0 +1,148 @@
  14883. +
  14884. +#
  14885. +# RCSID $Id: defconfig,v 1.28.2.1 2006/10/11 18:14:33 paul Exp $
  14886. +#
  14887. +
  14888. +#
  14889. +# FreeS/WAN IPSec implementation, KLIPS kernel config defaults
  14890. +#
  14891. +
  14892. +#
  14893. +# First, lets override stuff already set or not in the kernel config.
  14894. +#
  14895. +# We can't even think about leaving this off...
  14896. +CONFIG_INET=y
  14897. +
  14898. +#
  14899. +# This must be on for subnet protection.
  14900. +CONFIG_IP_FORWARD=y
  14901. +
  14902. +# Shut off IPSEC masquerading if it has been enabled, since it will
  14903. +# break the compile. IPPROTO_ESP and IPPROTO_AH were included in
  14904. +# net/ipv4/ip_masq.c when they should have gone into include/linux/in.h.
  14905. +CONFIG_IP_MASQUERADE_IPSEC=n
  14906. +
  14907. +#
  14908. +# Next, lets set the recommended FreeS/WAN configuration.
  14909. +#
  14910. +
  14911. +# To config as static (preferred), 'y'. To config as module, 'm'.
  14912. +CONFIG_KLIPS=m
  14913. +
  14914. +# To do tunnel mode IPSec, this must be enabled.
  14915. +CONFIG_KLIPS_IPIP=y
  14916. +
  14917. +# To enable authentication, say 'y'. (Highly recommended)
  14918. +CONFIG_KLIPS_AH=y
  14919. +
  14920. +# Authentication algorithm(s):
  14921. +CONFIG_KLIPS_AUTH_HMAC_MD5=y
  14922. +CONFIG_KLIPS_AUTH_HMAC_SHA1=y
  14923. +
  14924. +# To enable encryption, say 'y'. (Highly recommended)
  14925. +CONFIG_KLIPS_ESP=y
  14926. +
  14927. +# modular algo extensions (and new ALGOs)
  14928. +CONFIG_KLIPS_ALG=y
  14929. +
  14930. +# Encryption algorithm(s):
  14931. +CONFIG_KLIPS_ENC_3DES=y
  14932. +CONFIG_KLIPS_ENC_AES=y
  14933. +# CONFIG_KLIPS_ENC_NULL=y
  14934. +
  14935. +# Use CryptoAPI for ALG? - by default, no.
  14936. +CONFIG_KLIPS_ENC_CRYPTOAPI=n
  14937. +
  14938. +# IP Compression: new, probably still has minor bugs.
  14939. +CONFIG_KLIPS_IPCOMP=y
  14940. +
  14941. +# To enable userspace-switchable KLIPS debugging, say 'y'.
  14942. +CONFIG_KLIPS_DEBUG=y
  14943. +
  14944. +# NAT Traversal
  14945. +CONFIG_IPSEC_NAT_TRAVERSAL=y
  14946. +
  14947. +#
  14948. +#
  14949. +# $Log: defconfig,v $
  14950. +# Revision 1.28.2.1 2006/10/11 18:14:33 paul
  14951. +# Add JuanJo Ciarlante's ESP_NULL patches for KLIPS, but leave it disabled
  14952. +# per default.
  14953. +#
  14954. +# Revision 1.28 2005/05/11 03:15:42 mcr
  14955. +# adjusted makefiles to sanely build modules properly.
  14956. +#
  14957. +# Revision 1.27 2005/03/20 03:00:05 mcr
  14958. +# default configuration should enable NAT_TRAVERSAL.
  14959. +#
  14960. +# Revision 1.26 2004/07/10 19:11:18 mcr
  14961. +# CONFIG_IPSEC -> CONFIG_KLIPS.
  14962. +#
  14963. +# Revision 1.25 2004/07/05 01:03:53 mcr
  14964. +# fix for adding cryptoapi code.
  14965. +# keep it off for now, since UMLs do not have it yet.
  14966. +#
  14967. +# Revision 1.24 2004/04/06 02:49:25 mcr
  14968. +# pullup of algo code from alg-branch.
  14969. +#
  14970. +# Revision 1.23.2.2 2004/04/05 04:30:46 mcr
  14971. +# patches for alg-branch to compile/work with 2.x openswan
  14972. +#
  14973. +# Revision 1.23.2.1 2003/12/22 15:25:52 jjo
  14974. +# . Merged algo-0.8.1-rc11-test1 into alg-branch
  14975. +#
  14976. +# Revision 1.23 2003/12/10 01:14:27 mcr
  14977. +# NAT-traversal patches to KLIPS.
  14978. +#
  14979. +# Revision 1.22 2003/02/24 19:37:27 mcr
  14980. +# changed default compilation mode to static.
  14981. +#
  14982. +# Revision 1.21 2002/04/24 07:36:27 mcr
  14983. +# Moved from ./klips/net/ipsec/defconfig,v
  14984. +#
  14985. +# Revision 1.20 2002/04/02 04:07:40 mcr
  14986. +# default build is now 'm'odule for KLIPS
  14987. +#
  14988. +# Revision 1.19 2002/03/08 18:57:17 rgb
  14989. +# Added a blank line at the beginning of the file to make it easier for
  14990. +# other projects to patch ./arch/i386/defconfig, for example
  14991. +# LIDS+grSecurity requested by Jason Pattie.
  14992. +#
  14993. +# Revision 1.18 2000/11/30 17:26:56 rgb
  14994. +# Cleaned out unused options and enabled ipcomp by default.
  14995. +#
  14996. +# Revision 1.17 2000/09/15 11:37:01 rgb
  14997. +# Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  14998. +# IPCOMP zlib deflate code.
  14999. +#
  15000. +# Revision 1.16 2000/09/08 19:12:55 rgb
  15001. +# Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  15002. +#
  15003. +# Revision 1.15 2000/05/24 19:37:13 rgb
  15004. +# *** empty log message ***
  15005. +#
  15006. +# Revision 1.14 2000/05/11 21:14:57 henry
  15007. +# just commenting the FOOBAR=y lines out is not enough
  15008. +#
  15009. +# Revision 1.13 2000/05/10 20:17:58 rgb
  15010. +# Comment out netlink defaults, which are no longer needed.
  15011. +#
  15012. +# Revision 1.12 2000/05/10 19:13:38 rgb
  15013. +# Added configure option to shut off no eroute passthrough.
  15014. +#
  15015. +# Revision 1.11 2000/03/16 07:09:46 rgb
  15016. +# Hardcode PF_KEYv2 support.
  15017. +# Disable IPSEC_ICMP by default.
  15018. +# Remove DES config option from defaults file.
  15019. +#
  15020. +# Revision 1.10 2000/01/11 03:09:42 rgb
  15021. +# Added a default of 'y' to PF_KEYv2 keying I/F.
  15022. +#
  15023. +# Revision 1.9 1999/05/08 21:23:12 rgb
  15024. +# Added support for 2.2.x kernels.
  15025. +#
  15026. +# Revision 1.8 1999/04/06 04:54:25 rgb
  15027. +# Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  15028. +# patch shell fixes.
  15029. +#
  15030. +#
  15031. --- /dev/null Tue Mar 11 13:02:56 2003
  15032. +++ linux/net/ipsec/deflate.c Mon Feb 9 13:51:03 2004
  15033. @@ -0,0 +1,1351 @@
  15034. +/* deflate.c -- compress data using the deflation algorithm
  15035. + * Copyright (C) 1995-2002 Jean-loup Gailly.
  15036. + * For conditions of distribution and use, see copyright notice in zlib.h
  15037. + */
  15038. +
  15039. +/*
  15040. + * ALGORITHM
  15041. + *
  15042. + * The "deflation" process depends on being able to identify portions
  15043. + * of the input text which are identical to earlier input (within a
  15044. + * sliding window trailing behind the input currently being processed).
  15045. + *
  15046. + * The most straightforward technique turns out to be the fastest for
  15047. + * most input files: try all possible matches and select the longest.
  15048. + * The key feature of this algorithm is that insertions into the string
  15049. + * dictionary are very simple and thus fast, and deletions are avoided
  15050. + * completely. Insertions are performed at each input character, whereas
  15051. + * string matches are performed only when the previous match ends. So it
  15052. + * is preferable to spend more time in matches to allow very fast string
  15053. + * insertions and avoid deletions. The matching algorithm for small
  15054. + * strings is inspired from that of Rabin & Karp. A brute force approach
  15055. + * is used to find longer strings when a small match has been found.
  15056. + * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
  15057. + * (by Leonid Broukhis).
  15058. + * A previous version of this file used a more sophisticated algorithm
  15059. + * (by Fiala and Greene) which is guaranteed to run in linear amortized
  15060. + * time, but has a larger average cost, uses more memory and is patented.
  15061. + * However the F&G algorithm may be faster for some highly redundant
  15062. + * files if the parameter max_chain_length (described below) is too large.
  15063. + *
  15064. + * ACKNOWLEDGEMENTS
  15065. + *
  15066. + * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
  15067. + * I found it in 'freeze' written by Leonid Broukhis.
  15068. + * Thanks to many people for bug reports and testing.
  15069. + *
  15070. + * REFERENCES
  15071. + *
  15072. + * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
  15073. + * Available in ftp://ds.internic.net/rfc/rfc1951.txt
  15074. + *
  15075. + * A description of the Rabin and Karp algorithm is given in the book
  15076. + * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
  15077. + *
  15078. + * Fiala,E.R., and Greene,D.H.
  15079. + * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
  15080. + *
  15081. + */
  15082. +
  15083. +/* @(#) $Id: deflate.c,v 1.4 2004/07/10 07:48:37 mcr Exp $ */
  15084. +
  15085. +#include "deflate.h"
  15086. +
  15087. +local const char deflate_copyright[] =
  15088. + " deflate 1.1.4 Copyright 1995-2002 Jean-loup Gailly ";
  15089. +/*
  15090. + If you use the zlib library in a product, an acknowledgment is welcome
  15091. + in the documentation of your product. If for some reason you cannot
  15092. + include such an acknowledgment, I would appreciate that you keep this
  15093. + copyright string in the executable of your product.
  15094. + */
  15095. +
  15096. +/* ===========================================================================
  15097. + * Function prototypes.
  15098. + */
  15099. +typedef enum {
  15100. + need_more, /* block not completed, need more input or more output */
  15101. + block_done, /* block flush performed */
  15102. + finish_started, /* finish started, need only more output at next deflate */
  15103. + finish_done /* finish done, accept no more input or output */
  15104. +} block_state;
  15105. +
  15106. +typedef block_state (*compress_func) OF((deflate_state *s, int flush));
  15107. +/* Compression function. Returns the block state after the call. */
  15108. +
  15109. +local void fill_window OF((deflate_state *s));
  15110. +local block_state deflate_stored OF((deflate_state *s, int flush));
  15111. +local block_state deflate_fast OF((deflate_state *s, int flush));
  15112. +local block_state deflate_slow OF((deflate_state *s, int flush));
  15113. +local void lm_init OF((deflate_state *s));
  15114. +local void putShortMSB OF((deflate_state *s, uInt b));
  15115. +local void flush_pending OF((z_streamp strm));
  15116. +local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
  15117. +#ifdef ASMV
  15118. + void match_init OF((void)); /* asm code initialization */
  15119. + uInt longest_match OF((deflate_state *s, IPos cur_match));
  15120. +#else
  15121. +local uInt longest_match OF((deflate_state *s, IPos cur_match));
  15122. +#endif
  15123. +
  15124. +#ifdef DEBUG
  15125. +local void check_match OF((deflate_state *s, IPos start, IPos match,
  15126. + int length));
  15127. +#endif
  15128. +
  15129. +/* ===========================================================================
  15130. + * Local data
  15131. + */
  15132. +
  15133. +#define NIL 0
  15134. +/* Tail of hash chains */
  15135. +
  15136. +#ifndef TOO_FAR
  15137. +# define TOO_FAR 4096
  15138. +#endif
  15139. +/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
  15140. +
  15141. +#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
  15142. +/* Minimum amount of lookahead, except at the end of the input file.
  15143. + * See deflate.c for comments about the MIN_MATCH+1.
  15144. + */
  15145. +
  15146. +/* Values for max_lazy_match, good_match and max_chain_length, depending on
  15147. + * the desired pack level (0..9). The values given below have been tuned to
  15148. + * exclude worst case performance for pathological files. Better values may be
  15149. + * found for specific files.
  15150. + */
  15151. +typedef struct config_s {
  15152. + ush good_length; /* reduce lazy search above this match length */
  15153. + ush max_lazy; /* do not perform lazy search above this match length */
  15154. + ush nice_length; /* quit search above this match length */
  15155. + ush max_chain;
  15156. + compress_func func;
  15157. +} config;
  15158. +
  15159. +local const config configuration_table[10] = {
  15160. +/* good lazy nice chain */
  15161. +/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
  15162. +/* 1 */ {4, 4, 8, 4, deflate_fast}, /* maximum speed, no lazy matches */
  15163. +/* 2 */ {4, 5, 16, 8, deflate_fast},
  15164. +/* 3 */ {4, 6, 32, 32, deflate_fast},
  15165. +
  15166. +/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
  15167. +/* 5 */ {8, 16, 32, 32, deflate_slow},
  15168. +/* 6 */ {8, 16, 128, 128, deflate_slow},
  15169. +/* 7 */ {8, 32, 128, 256, deflate_slow},
  15170. +/* 8 */ {32, 128, 258, 1024, deflate_slow},
  15171. +/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* maximum compression */
  15172. +
  15173. +/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
  15174. + * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
  15175. + * meaning.
  15176. + */
  15177. +
  15178. +#define EQUAL 0
  15179. +/* result of memcmp for equal strings */
  15180. +
  15181. +struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
  15182. +
  15183. +/* ===========================================================================
  15184. + * Update a hash value with the given input byte
  15185. + * IN assertion: all calls to to UPDATE_HASH are made with consecutive
  15186. + * input characters, so that a running hash key can be computed from the
  15187. + * previous key instead of complete recalculation each time.
  15188. + */
  15189. +#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
  15190. +
  15191. +
  15192. +/* ===========================================================================
  15193. + * Insert string str in the dictionary and set match_head to the previous head
  15194. + * of the hash chain (the most recent string with same hash key). Return
  15195. + * the previous length of the hash chain.
  15196. + * If this file is compiled with -DFASTEST, the compression level is forced
  15197. + * to 1, and no hash chains are maintained.
  15198. + * IN assertion: all calls to to INSERT_STRING are made with consecutive
  15199. + * input characters and the first MIN_MATCH bytes of str are valid
  15200. + * (except for the last MIN_MATCH-1 bytes of the input file).
  15201. + */
  15202. +#ifdef FASTEST
  15203. +#define INSERT_STRING(s, str, match_head) \
  15204. + (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
  15205. + match_head = s->head[s->ins_h], \
  15206. + s->head[s->ins_h] = (Pos)(str))
  15207. +#else
  15208. +#define INSERT_STRING(s, str, match_head) \
  15209. + (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
  15210. + s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
  15211. + s->head[s->ins_h] = (Pos)(str))
  15212. +#endif
  15213. +
  15214. +/* ===========================================================================
  15215. + * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
  15216. + * prev[] will be initialized on the fly.
  15217. + */
  15218. +#define CLEAR_HASH(s) \
  15219. + s->head[s->hash_size-1] = NIL; \
  15220. + zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
  15221. +
  15222. +/* ========================================================================= */
  15223. +int ZEXPORT deflateInit_(strm, level, version, stream_size)
  15224. + z_streamp strm;
  15225. + int level;
  15226. + const char *version;
  15227. + int stream_size;
  15228. +{
  15229. + return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
  15230. + Z_DEFAULT_STRATEGY, version, stream_size);
  15231. + /* To do: ignore strm->next_in if we use it as window */
  15232. +}
  15233. +
  15234. +/* ========================================================================= */
  15235. +int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
  15236. + version, stream_size)
  15237. + z_streamp strm;
  15238. + int level;
  15239. + int method;
  15240. + int windowBits;
  15241. + int memLevel;
  15242. + int strategy;
  15243. + const char *version;
  15244. + int stream_size;
  15245. +{
  15246. + deflate_state *s;
  15247. + int noheader = 0;
  15248. + static const char* my_version = ZLIB_VERSION;
  15249. +
  15250. + ushf *overlay;
  15251. + /* We overlay pending_buf and d_buf+l_buf. This works since the average
  15252. + * output size for (length,distance) codes is <= 24 bits.
  15253. + */
  15254. +
  15255. + if (version == Z_NULL || version[0] != my_version[0] ||
  15256. + stream_size != sizeof(z_stream)) {
  15257. + return Z_VERSION_ERROR;
  15258. + }
  15259. + if (strm == Z_NULL) return Z_STREAM_ERROR;
  15260. +
  15261. + strm->msg = Z_NULL;
  15262. + if (strm->zalloc == Z_NULL) {
  15263. + return Z_STREAM_ERROR;
  15264. +/* strm->zalloc = zcalloc;
  15265. + strm->opaque = (voidpf)0;*/
  15266. + }
  15267. + if (strm->zfree == Z_NULL) return Z_STREAM_ERROR; /* strm->zfree = zcfree; */
  15268. +
  15269. + if (level == Z_DEFAULT_COMPRESSION) level = 6;
  15270. +#ifdef FASTEST
  15271. + level = 1;
  15272. +#endif
  15273. +
  15274. + if (windowBits < 0) { /* undocumented feature: suppress zlib header */
  15275. + noheader = 1;
  15276. + windowBits = -windowBits;
  15277. + }
  15278. + if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
  15279. + windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
  15280. + strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
  15281. + return Z_STREAM_ERROR;
  15282. + }
  15283. + s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
  15284. + if (s == Z_NULL) return Z_MEM_ERROR;
  15285. + strm->state = (struct internal_state FAR *)s;
  15286. + s->strm = strm;
  15287. +
  15288. + s->noheader = noheader;
  15289. + s->w_bits = windowBits;
  15290. + s->w_size = 1 << s->w_bits;
  15291. + s->w_mask = s->w_size - 1;
  15292. +
  15293. + s->hash_bits = memLevel + 7;
  15294. + s->hash_size = 1 << s->hash_bits;
  15295. + s->hash_mask = s->hash_size - 1;
  15296. + s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
  15297. +
  15298. + s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
  15299. + s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
  15300. + s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
  15301. +
  15302. + s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
  15303. +
  15304. + overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
  15305. + s->pending_buf = (uchf *) overlay;
  15306. + s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
  15307. +
  15308. + if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
  15309. + s->pending_buf == Z_NULL) {
  15310. + strm->msg = ERR_MSG(Z_MEM_ERROR);
  15311. + deflateEnd (strm);
  15312. + return Z_MEM_ERROR;
  15313. + }
  15314. + s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
  15315. + s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
  15316. +
  15317. + s->level = level;
  15318. + s->strategy = strategy;
  15319. + s->method = (Byte)method;
  15320. +
  15321. + return deflateReset(strm);
  15322. +}
  15323. +
  15324. +/* ========================================================================= */
  15325. +int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
  15326. + z_streamp strm;
  15327. + const Bytef *dictionary;
  15328. + uInt dictLength;
  15329. +{
  15330. + deflate_state *s;
  15331. + uInt length = dictLength;
  15332. + uInt n;
  15333. + IPos hash_head = 0;
  15334. +
  15335. + if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
  15336. + strm->state->status != INIT_STATE) return Z_STREAM_ERROR;
  15337. +
  15338. + s = strm->state;
  15339. + strm->adler = adler32(strm->adler, dictionary, dictLength);
  15340. +
  15341. + if (length < MIN_MATCH) return Z_OK;
  15342. + if (length > MAX_DIST(s)) {
  15343. + length = MAX_DIST(s);
  15344. +#ifndef USE_DICT_HEAD
  15345. + dictionary += dictLength - length; /* use the tail of the dictionary */
  15346. +#endif
  15347. + }
  15348. + zmemcpy(s->window, dictionary, length);
  15349. + s->strstart = length;
  15350. + s->block_start = (long)length;
  15351. +
  15352. + /* Insert all strings in the hash table (except for the last two bytes).
  15353. + * s->lookahead stays null, so s->ins_h will be recomputed at the next
  15354. + * call of fill_window.
  15355. + */
  15356. + s->ins_h = s->window[0];
  15357. + UPDATE_HASH(s, s->ins_h, s->window[1]);
  15358. + for (n = 0; n <= length - MIN_MATCH; n++) {
  15359. + INSERT_STRING(s, n, hash_head);
  15360. + }
  15361. + if (hash_head) hash_head = 0; /* to make compiler happy */
  15362. + return Z_OK;
  15363. +}
  15364. +
  15365. +/* ========================================================================= */
  15366. +int ZEXPORT deflateReset (strm)
  15367. + z_streamp strm;
  15368. +{
  15369. + deflate_state *s;
  15370. +
  15371. + if (strm == Z_NULL || strm->state == Z_NULL ||
  15372. + strm->zalloc == Z_NULL || strm->zfree == Z_NULL) return Z_STREAM_ERROR;
  15373. +
  15374. + strm->total_in = strm->total_out = 0;
  15375. + strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
  15376. + strm->data_type = Z_UNKNOWN;
  15377. +
  15378. + s = (deflate_state *)strm->state;
  15379. + s->pending = 0;
  15380. + s->pending_out = s->pending_buf;
  15381. +
  15382. + if (s->noheader < 0) {
  15383. + s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */
  15384. + }
  15385. + s->status = s->noheader ? BUSY_STATE : INIT_STATE;
  15386. + strm->adler = 1;
  15387. + s->last_flush = Z_NO_FLUSH;
  15388. +
  15389. + _tr_init(s);
  15390. + lm_init(s);
  15391. +
  15392. + return Z_OK;
  15393. +}
  15394. +
  15395. +/* ========================================================================= */
  15396. +int ZEXPORT deflateParams(strm, level, strategy)
  15397. + z_streamp strm;
  15398. + int level;
  15399. + int strategy;
  15400. +{
  15401. + deflate_state *s;
  15402. + compress_func func;
  15403. + int err = Z_OK;
  15404. +
  15405. + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  15406. + s = strm->state;
  15407. +
  15408. + if (level == Z_DEFAULT_COMPRESSION) {
  15409. + level = 6;
  15410. + }
  15411. + if (level < 0 || level > 9 || strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
  15412. + return Z_STREAM_ERROR;
  15413. + }
  15414. + func = configuration_table[s->level].func;
  15415. +
  15416. + if (func != configuration_table[level].func && strm->total_in != 0) {
  15417. + /* Flush the last buffer: */
  15418. + err = deflate(strm, Z_PARTIAL_FLUSH);
  15419. + }
  15420. + if (s->level != level) {
  15421. + s->level = level;
  15422. + s->max_lazy_match = configuration_table[level].max_lazy;
  15423. + s->good_match = configuration_table[level].good_length;
  15424. + s->nice_match = configuration_table[level].nice_length;
  15425. + s->max_chain_length = configuration_table[level].max_chain;
  15426. + }
  15427. + s->strategy = strategy;
  15428. + return err;
  15429. +}
  15430. +
  15431. +/* =========================================================================
  15432. + * Put a short in the pending buffer. The 16-bit value is put in MSB order.
  15433. + * IN assertion: the stream state is correct and there is enough room in
  15434. + * pending_buf.
  15435. + */
  15436. +local void putShortMSB (s, b)
  15437. + deflate_state *s;
  15438. + uInt b;
  15439. +{
  15440. + put_byte(s, (Byte)(b >> 8));
  15441. + put_byte(s, (Byte)(b & 0xff));
  15442. +}
  15443. +
  15444. +/* =========================================================================
  15445. + * Flush as much pending output as possible. All deflate() output goes
  15446. + * through this function so some applications may wish to modify it
  15447. + * to avoid allocating a large strm->next_out buffer and copying into it.
  15448. + * (See also read_buf()).
  15449. + */
  15450. +local void flush_pending(strm)
  15451. + z_streamp strm;
  15452. +{
  15453. + unsigned len = strm->state->pending;
  15454. +
  15455. + if (len > strm->avail_out) len = strm->avail_out;
  15456. + if (len == 0) return;
  15457. +
  15458. + zmemcpy(strm->next_out, strm->state->pending_out, len);
  15459. + strm->next_out += len;
  15460. + strm->state->pending_out += len;
  15461. + strm->total_out += len;
  15462. + strm->avail_out -= len;
  15463. + strm->state->pending -= len;
  15464. + if (strm->state->pending == 0) {
  15465. + strm->state->pending_out = strm->state->pending_buf;
  15466. + }
  15467. +}
  15468. +
  15469. +/* ========================================================================= */
  15470. +int ZEXPORT deflate (strm, flush)
  15471. + z_streamp strm;
  15472. + int flush;
  15473. +{
  15474. + int old_flush; /* value of flush param for previous deflate call */
  15475. + deflate_state *s;
  15476. +
  15477. + if (strm == Z_NULL || strm->state == Z_NULL ||
  15478. + flush > Z_FINISH || flush < 0) {
  15479. + return Z_STREAM_ERROR;
  15480. + }
  15481. + s = strm->state;
  15482. +
  15483. + if (strm->next_out == Z_NULL ||
  15484. + (strm->next_in == Z_NULL && strm->avail_in != 0) ||
  15485. + (s->status == FINISH_STATE && flush != Z_FINISH)) {
  15486. + ERR_RETURN(strm, Z_STREAM_ERROR);
  15487. + }
  15488. + if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
  15489. +
  15490. + s->strm = strm; /* just in case */
  15491. + old_flush = s->last_flush;
  15492. + s->last_flush = flush;
  15493. +
  15494. + /* Write the zlib header */
  15495. + if (s->status == INIT_STATE) {
  15496. +
  15497. + uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
  15498. + uInt level_flags = (s->level-1) >> 1;
  15499. +
  15500. + if (level_flags > 3) level_flags = 3;
  15501. + header |= (level_flags << 6);
  15502. + if (s->strstart != 0) header |= PRESET_DICT;
  15503. + header += 31 - (header % 31);
  15504. +
  15505. + s->status = BUSY_STATE;
  15506. + putShortMSB(s, header);
  15507. +
  15508. + /* Save the adler32 of the preset dictionary: */
  15509. + if (s->strstart != 0) {
  15510. + putShortMSB(s, (uInt)(strm->adler >> 16));
  15511. + putShortMSB(s, (uInt)(strm->adler & 0xffff));
  15512. + }
  15513. + strm->adler = 1L;
  15514. + }
  15515. +
  15516. + /* Flush as much pending output as possible */
  15517. + if (s->pending != 0) {
  15518. + flush_pending(strm);
  15519. + if (strm->avail_out == 0) {
  15520. + /* Since avail_out is 0, deflate will be called again with
  15521. + * more output space, but possibly with both pending and
  15522. + * avail_in equal to zero. There won't be anything to do,
  15523. + * but this is not an error situation so make sure we
  15524. + * return OK instead of BUF_ERROR at next call of deflate:
  15525. + */
  15526. + s->last_flush = -1;
  15527. + return Z_OK;
  15528. + }
  15529. +
  15530. + /* Make sure there is something to do and avoid duplicate consecutive
  15531. + * flushes. For repeated and useless calls with Z_FINISH, we keep
  15532. + * returning Z_STREAM_END instead of Z_BUFF_ERROR.
  15533. + */
  15534. + } else if (strm->avail_in == 0 && flush <= old_flush &&
  15535. + flush != Z_FINISH) {
  15536. + ERR_RETURN(strm, Z_BUF_ERROR);
  15537. + }
  15538. +
  15539. + /* User must not provide more input after the first FINISH: */
  15540. + if (s->status == FINISH_STATE && strm->avail_in != 0) {
  15541. + ERR_RETURN(strm, Z_BUF_ERROR);
  15542. + }
  15543. +
  15544. + /* Start a new block or continue the current one.
  15545. + */
  15546. + if (strm->avail_in != 0 || s->lookahead != 0 ||
  15547. + (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
  15548. + block_state bstate;
  15549. +
  15550. + bstate = (*(configuration_table[s->level].func))(s, flush);
  15551. +
  15552. + if (bstate == finish_started || bstate == finish_done) {
  15553. + s->status = FINISH_STATE;
  15554. + }
  15555. + if (bstate == need_more || bstate == finish_started) {
  15556. + if (strm->avail_out == 0) {
  15557. + s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
  15558. + }
  15559. + return Z_OK;
  15560. + /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
  15561. + * of deflate should use the same flush parameter to make sure
  15562. + * that the flush is complete. So we don't have to output an
  15563. + * empty block here, this will be done at next call. This also
  15564. + * ensures that for a very small output buffer, we emit at most
  15565. + * one empty block.
  15566. + */
  15567. + }
  15568. + if (bstate == block_done) {
  15569. + if (flush == Z_PARTIAL_FLUSH) {
  15570. + _tr_align(s);
  15571. + } else { /* FULL_FLUSH or SYNC_FLUSH */
  15572. + _tr_stored_block(s, (char*)0, 0L, 0);
  15573. + /* For a full flush, this empty block will be recognized
  15574. + * as a special marker by inflate_sync().
  15575. + */
  15576. + if (flush == Z_FULL_FLUSH) {
  15577. + CLEAR_HASH(s); /* forget history */
  15578. + }
  15579. + }
  15580. + flush_pending(strm);
  15581. + if (strm->avail_out == 0) {
  15582. + s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
  15583. + return Z_OK;
  15584. + }
  15585. + }
  15586. + }
  15587. + Assert(strm->avail_out > 0, "bug2");
  15588. +
  15589. + if (flush != Z_FINISH) return Z_OK;
  15590. + if (s->noheader) return Z_STREAM_END;
  15591. +
  15592. + /* Write the zlib trailer (adler32) */
  15593. + putShortMSB(s, (uInt)(strm->adler >> 16));
  15594. + putShortMSB(s, (uInt)(strm->adler & 0xffff));
  15595. + flush_pending(strm);
  15596. + /* If avail_out is zero, the application will call deflate again
  15597. + * to flush the rest.
  15598. + */
  15599. + s->noheader = -1; /* write the trailer only once! */
  15600. + return s->pending != 0 ? Z_OK : Z_STREAM_END;
  15601. +}
  15602. +
  15603. +/* ========================================================================= */
  15604. +int ZEXPORT deflateEnd (strm)
  15605. + z_streamp strm;
  15606. +{
  15607. + int status;
  15608. +
  15609. + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  15610. +
  15611. + status = strm->state->status;
  15612. + if (status != INIT_STATE && status != BUSY_STATE &&
  15613. + status != FINISH_STATE) {
  15614. + return Z_STREAM_ERROR;
  15615. + }
  15616. +
  15617. + /* Deallocate in reverse order of allocations: */
  15618. + TRY_FREE(strm, strm->state->pending_buf);
  15619. + TRY_FREE(strm, strm->state->head);
  15620. + TRY_FREE(strm, strm->state->prev);
  15621. + TRY_FREE(strm, strm->state->window);
  15622. +
  15623. + ZFREE(strm, strm->state);
  15624. + strm->state = Z_NULL;
  15625. +
  15626. + return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
  15627. +}
  15628. +
  15629. +/* =========================================================================
  15630. + * Copy the source state to the destination state.
  15631. + * To simplify the source, this is not supported for 16-bit MSDOS (which
  15632. + * doesn't have enough memory anyway to duplicate compression states).
  15633. + */
  15634. +int ZEXPORT deflateCopy (dest, source)
  15635. + z_streamp dest;
  15636. + z_streamp source;
  15637. +{
  15638. +#ifdef MAXSEG_64K
  15639. + return Z_STREAM_ERROR;
  15640. +#else
  15641. + deflate_state *ds;
  15642. + deflate_state *ss;
  15643. + ushf *overlay;
  15644. +
  15645. +
  15646. + if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
  15647. + return Z_STREAM_ERROR;
  15648. + }
  15649. +
  15650. + ss = source->state;
  15651. +
  15652. + *dest = *source;
  15653. +
  15654. + ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
  15655. + if (ds == Z_NULL) return Z_MEM_ERROR;
  15656. + dest->state = (struct internal_state FAR *) ds;
  15657. + *ds = *ss;
  15658. + ds->strm = dest;
  15659. +
  15660. + ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
  15661. + ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
  15662. + ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
  15663. + overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
  15664. + ds->pending_buf = (uchf *) overlay;
  15665. +
  15666. + if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
  15667. + ds->pending_buf == Z_NULL) {
  15668. + deflateEnd (dest);
  15669. + return Z_MEM_ERROR;
  15670. + }
  15671. + /* following zmemcpy do not work for 16-bit MSDOS */
  15672. + zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
  15673. + zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
  15674. + zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
  15675. + zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
  15676. +
  15677. + ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
  15678. + ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
  15679. + ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
  15680. +
  15681. + ds->l_desc.dyn_tree = ds->dyn_ltree;
  15682. + ds->d_desc.dyn_tree = ds->dyn_dtree;
  15683. + ds->bl_desc.dyn_tree = ds->bl_tree;
  15684. +
  15685. + return Z_OK;
  15686. +#endif
  15687. +}
  15688. +
  15689. +/* ===========================================================================
  15690. + * Read a new buffer from the current input stream, update the adler32
  15691. + * and total number of bytes read. All deflate() input goes through
  15692. + * this function so some applications may wish to modify it to avoid
  15693. + * allocating a large strm->next_in buffer and copying from it.
  15694. + * (See also flush_pending()).
  15695. + */
  15696. +local int read_buf(strm, buf, size)
  15697. + z_streamp strm;
  15698. + Bytef *buf;
  15699. + unsigned size;
  15700. +{
  15701. + unsigned len = strm->avail_in;
  15702. +
  15703. + if (len > size) len = size;
  15704. + if (len == 0) return 0;
  15705. +
  15706. + strm->avail_in -= len;
  15707. +
  15708. + if (!strm->state->noheader) {
  15709. + strm->adler = adler32(strm->adler, strm->next_in, len);
  15710. + }
  15711. + zmemcpy(buf, strm->next_in, len);
  15712. + strm->next_in += len;
  15713. + strm->total_in += len;
  15714. +
  15715. + return (int)len;
  15716. +}
  15717. +
  15718. +/* ===========================================================================
  15719. + * Initialize the "longest match" routines for a new zlib stream
  15720. + */
  15721. +local void lm_init (s)
  15722. + deflate_state *s;
  15723. +{
  15724. + s->window_size = (ulg)2L*s->w_size;
  15725. +
  15726. + CLEAR_HASH(s);
  15727. +
  15728. + /* Set the default configuration parameters:
  15729. + */
  15730. + s->max_lazy_match = configuration_table[s->level].max_lazy;
  15731. + s->good_match = configuration_table[s->level].good_length;
  15732. + s->nice_match = configuration_table[s->level].nice_length;
  15733. + s->max_chain_length = configuration_table[s->level].max_chain;
  15734. +
  15735. + s->strstart = 0;
  15736. + s->block_start = 0L;
  15737. + s->lookahead = 0;
  15738. + s->match_length = s->prev_length = MIN_MATCH-1;
  15739. + s->match_available = 0;
  15740. + s->ins_h = 0;
  15741. +#ifdef ASMV
  15742. + match_init(); /* initialize the asm code */
  15743. +#endif
  15744. +}
  15745. +
  15746. +/* ===========================================================================
  15747. + * Set match_start to the longest match starting at the given string and
  15748. + * return its length. Matches shorter or equal to prev_length are discarded,
  15749. + * in which case the result is equal to prev_length and match_start is
  15750. + * garbage.
  15751. + * IN assertions: cur_match is the head of the hash chain for the current
  15752. + * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  15753. + * OUT assertion: the match length is not greater than s->lookahead.
  15754. + */
  15755. +#ifndef ASMV
  15756. +/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
  15757. + * match.S. The code will be functionally equivalent.
  15758. + */
  15759. +#ifndef FASTEST
  15760. +local uInt longest_match(s, cur_match)
  15761. + deflate_state *s;
  15762. + IPos cur_match; /* current match */
  15763. +{
  15764. + unsigned chain_length = s->max_chain_length;/* max hash chain length */
  15765. + register Bytef *scan = s->window + s->strstart; /* current string */
  15766. + register Bytef *match; /* matched string */
  15767. + register int len; /* length of current match */
  15768. + int best_len = s->prev_length; /* best match length so far */
  15769. + int nice_match = s->nice_match; /* stop if match long enough */
  15770. + IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
  15771. + s->strstart - (IPos)MAX_DIST(s) : NIL;
  15772. + /* Stop when cur_match becomes <= limit. To simplify the code,
  15773. + * we prevent matches with the string of window index 0.
  15774. + */
  15775. + Posf *prev = s->prev;
  15776. + uInt wmask = s->w_mask;
  15777. +
  15778. +#ifdef UNALIGNED_OK
  15779. + /* Compare two bytes at a time. Note: this is not always beneficial.
  15780. + * Try with and without -DUNALIGNED_OK to check.
  15781. + */
  15782. + register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
  15783. + register ush scan_start = *(ushf*)scan;
  15784. + register ush scan_end = *(ushf*)(scan+best_len-1);
  15785. +#else
  15786. + register Bytef *strend = s->window + s->strstart + MAX_MATCH;
  15787. + register Byte scan_end1 = scan[best_len-1];
  15788. + register Byte scan_end = scan[best_len];
  15789. +#endif
  15790. +
  15791. + /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  15792. + * It is easy to get rid of this optimization if necessary.
  15793. + */
  15794. + Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  15795. +
  15796. + /* Do not waste too much time if we already have a good match: */
  15797. + if (s->prev_length >= s->good_match) {
  15798. + chain_length >>= 2;
  15799. + }
  15800. + /* Do not look for matches beyond the end of the input. This is necessary
  15801. + * to make deflate deterministic.
  15802. + */
  15803. + if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
  15804. +
  15805. + Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  15806. +
  15807. + do {
  15808. + Assert(cur_match < s->strstart, "no future");
  15809. + match = s->window + cur_match;
  15810. +
  15811. + /* Skip to next match if the match length cannot increase
  15812. + * or if the match length is less than 2:
  15813. + */
  15814. +#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
  15815. + /* This code assumes sizeof(unsigned short) == 2. Do not use
  15816. + * UNALIGNED_OK if your compiler uses a different size.
  15817. + */
  15818. + if (*(ushf*)(match+best_len-1) != scan_end ||
  15819. + *(ushf*)match != scan_start) continue;
  15820. +
  15821. + /* It is not necessary to compare scan[2] and match[2] since they are
  15822. + * always equal when the other bytes match, given that the hash keys
  15823. + * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
  15824. + * strstart+3, +5, ... up to strstart+257. We check for insufficient
  15825. + * lookahead only every 4th comparison; the 128th check will be made
  15826. + * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
  15827. + * necessary to put more guard bytes at the end of the window, or
  15828. + * to check more often for insufficient lookahead.
  15829. + */
  15830. + Assert(scan[2] == match[2], "scan[2]?");
  15831. + scan++, match++;
  15832. + do {
  15833. + } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  15834. + *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  15835. + *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  15836. + *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  15837. + scan < strend);
  15838. + /* The funny "do {}" generates better code on most compilers */
  15839. +
  15840. + /* Here, scan <= window+strstart+257 */
  15841. + Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  15842. + if (*scan == *match) scan++;
  15843. +
  15844. + len = (MAX_MATCH - 1) - (int)(strend-scan);
  15845. + scan = strend - (MAX_MATCH-1);
  15846. +
  15847. +#else /* UNALIGNED_OK */
  15848. +
  15849. + if (match[best_len] != scan_end ||
  15850. + match[best_len-1] != scan_end1 ||
  15851. + *match != *scan ||
  15852. + *++match != scan[1]) continue;
  15853. +
  15854. + /* The check at best_len-1 can be removed because it will be made
  15855. + * again later. (This heuristic is not always a win.)
  15856. + * It is not necessary to compare scan[2] and match[2] since they
  15857. + * are always equal when the other bytes match, given that
  15858. + * the hash keys are equal and that HASH_BITS >= 8.
  15859. + */
  15860. + scan += 2, match++;
  15861. + Assert(*scan == *match, "match[2]?");
  15862. +
  15863. + /* We check for insufficient lookahead only every 8th comparison;
  15864. + * the 256th check will be made at strstart+258.
  15865. + */
  15866. + do {
  15867. + } while (*++scan == *++match && *++scan == *++match &&
  15868. + *++scan == *++match && *++scan == *++match &&
  15869. + *++scan == *++match && *++scan == *++match &&
  15870. + *++scan == *++match && *++scan == *++match &&
  15871. + scan < strend);
  15872. +
  15873. + Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  15874. +
  15875. + len = MAX_MATCH - (int)(strend - scan);
  15876. + scan = strend - MAX_MATCH;
  15877. +
  15878. +#endif /* UNALIGNED_OK */
  15879. +
  15880. + if (len > best_len) {
  15881. + s->match_start = cur_match;
  15882. + best_len = len;
  15883. + if (len >= nice_match) break;
  15884. +#ifdef UNALIGNED_OK
  15885. + scan_end = *(ushf*)(scan+best_len-1);
  15886. +#else
  15887. + scan_end1 = scan[best_len-1];
  15888. + scan_end = scan[best_len];
  15889. +#endif
  15890. + }
  15891. + } while ((cur_match = prev[cur_match & wmask]) > limit
  15892. + && --chain_length != 0);
  15893. +
  15894. + if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
  15895. + return s->lookahead;
  15896. +}
  15897. +
  15898. +#else /* FASTEST */
  15899. +/* ---------------------------------------------------------------------------
  15900. + * Optimized version for level == 1 only
  15901. + */
  15902. +local uInt longest_match(s, cur_match)
  15903. + deflate_state *s;
  15904. + IPos cur_match; /* current match */
  15905. +{
  15906. + register Bytef *scan = s->window + s->strstart; /* current string */
  15907. + register Bytef *match; /* matched string */
  15908. + register int len; /* length of current match */
  15909. + register Bytef *strend = s->window + s->strstart + MAX_MATCH;
  15910. +
  15911. + /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  15912. + * It is easy to get rid of this optimization if necessary.
  15913. + */
  15914. + Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  15915. +
  15916. + Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  15917. +
  15918. + Assert(cur_match < s->strstart, "no future");
  15919. +
  15920. + match = s->window + cur_match;
  15921. +
  15922. + /* Return failure if the match length is less than 2:
  15923. + */
  15924. + if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
  15925. +
  15926. + /* The check at best_len-1 can be removed because it will be made
  15927. + * again later. (This heuristic is not always a win.)
  15928. + * It is not necessary to compare scan[2] and match[2] since they
  15929. + * are always equal when the other bytes match, given that
  15930. + * the hash keys are equal and that HASH_BITS >= 8.
  15931. + */
  15932. + scan += 2, match += 2;
  15933. + Assert(*scan == *match, "match[2]?");
  15934. +
  15935. + /* We check for insufficient lookahead only every 8th comparison;
  15936. + * the 256th check will be made at strstart+258.
  15937. + */
  15938. + do {
  15939. + } while (*++scan == *++match && *++scan == *++match &&
  15940. + *++scan == *++match && *++scan == *++match &&
  15941. + *++scan == *++match && *++scan == *++match &&
  15942. + *++scan == *++match && *++scan == *++match &&
  15943. + scan < strend);
  15944. +
  15945. + Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  15946. +
  15947. + len = MAX_MATCH - (int)(strend - scan);
  15948. +
  15949. + if (len < MIN_MATCH) return MIN_MATCH - 1;
  15950. +
  15951. + s->match_start = cur_match;
  15952. + return len <= s->lookahead ? len : s->lookahead;
  15953. +}
  15954. +#endif /* FASTEST */
  15955. +#endif /* ASMV */
  15956. +
  15957. +#ifdef DEBUG
  15958. +/* ===========================================================================
  15959. + * Check that the match at match_start is indeed a match.
  15960. + */
  15961. +local void check_match(s, start, match, length)
  15962. + deflate_state *s;
  15963. + IPos start, match;
  15964. + int length;
  15965. +{
  15966. + /* check that the match is indeed a match */
  15967. + if (zmemcmp(s->window + match,
  15968. + s->window + start, length) != EQUAL) {
  15969. + fprintf(stderr, " start %u, match %u, length %d\n",
  15970. + start, match, length);
  15971. + do {
  15972. + fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
  15973. + } while (--length != 0);
  15974. + z_error("invalid match");
  15975. + }
  15976. + if (z_verbose > 1) {
  15977. + fprintf(stderr,"\\[%d,%d]", start-match, length);
  15978. + do { putc(s->window[start++], stderr); } while (--length != 0);
  15979. + }
  15980. +}
  15981. +#else
  15982. +# define check_match(s, start, match, length)
  15983. +#endif
  15984. +
  15985. +/* ===========================================================================
  15986. + * Fill the window when the lookahead becomes insufficient.
  15987. + * Updates strstart and lookahead.
  15988. + *
  15989. + * IN assertion: lookahead < MIN_LOOKAHEAD
  15990. + * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
  15991. + * At least one byte has been read, or avail_in == 0; reads are
  15992. + * performed for at least two bytes (required for the zip translate_eol
  15993. + * option -- not supported here).
  15994. + */
  15995. +local void fill_window(s)
  15996. + deflate_state *s;
  15997. +{
  15998. + register unsigned n, m;
  15999. + register Posf *p;
  16000. + unsigned more; /* Amount of free space at the end of the window. */
  16001. + uInt wsize = s->w_size;
  16002. +
  16003. + do {
  16004. + more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
  16005. +
  16006. + /* Deal with !@#$% 64K limit: */
  16007. + if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
  16008. + more = wsize;
  16009. +
  16010. + } else if (more == (unsigned)(-1)) {
  16011. + /* Very unlikely, but possible on 16 bit machine if strstart == 0
  16012. + * and lookahead == 1 (input done one byte at time)
  16013. + */
  16014. + more--;
  16015. +
  16016. + /* If the window is almost full and there is insufficient lookahead,
  16017. + * move the upper half to the lower one to make room in the upper half.
  16018. + */
  16019. + } else if (s->strstart >= wsize+MAX_DIST(s)) {
  16020. +
  16021. + zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
  16022. + s->match_start -= wsize;
  16023. + s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
  16024. + s->block_start -= (long) wsize;
  16025. +
  16026. + /* Slide the hash table (could be avoided with 32 bit values
  16027. + at the expense of memory usage). We slide even when level == 0
  16028. + to keep the hash table consistent if we switch back to level > 0
  16029. + later. (Using level 0 permanently is not an optimal usage of
  16030. + zlib, so we don't care about this pathological case.)
  16031. + */
  16032. + n = s->hash_size;
  16033. + p = &s->head[n];
  16034. + do {
  16035. + m = *--p;
  16036. + *p = (Pos)(m >= wsize ? m-wsize : NIL);
  16037. + } while (--n);
  16038. +
  16039. + n = wsize;
  16040. +#ifndef FASTEST
  16041. + p = &s->prev[n];
  16042. + do {
  16043. + m = *--p;
  16044. + *p = (Pos)(m >= wsize ? m-wsize : NIL);
  16045. + /* If n is not on any hash chain, prev[n] is garbage but
  16046. + * its value will never be used.
  16047. + */
  16048. + } while (--n);
  16049. +#endif
  16050. + more += wsize;
  16051. + }
  16052. + if (s->strm->avail_in == 0) return;
  16053. +
  16054. + /* If there was no sliding:
  16055. + * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
  16056. + * more == window_size - lookahead - strstart
  16057. + * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
  16058. + * => more >= window_size - 2*WSIZE + 2
  16059. + * In the BIG_MEM or MMAP case (not yet supported),
  16060. + * window_size == input_size + MIN_LOOKAHEAD &&
  16061. + * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
  16062. + * Otherwise, window_size == 2*WSIZE so more >= 2.
  16063. + * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
  16064. + */
  16065. + Assert(more >= 2, "more < 2");
  16066. +
  16067. + n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
  16068. + s->lookahead += n;
  16069. +
  16070. + /* Initialize the hash value now that we have some input: */
  16071. + if (s->lookahead >= MIN_MATCH) {
  16072. + s->ins_h = s->window[s->strstart];
  16073. + UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
  16074. +#if MIN_MATCH != 3
  16075. + Call UPDATE_HASH() MIN_MATCH-3 more times
  16076. +#endif
  16077. + }
  16078. + /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
  16079. + * but this is not important since only literal bytes will be emitted.
  16080. + */
  16081. +
  16082. + } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
  16083. +}
  16084. +
  16085. +/* ===========================================================================
  16086. + * Flush the current block, with given end-of-file flag.
  16087. + * IN assertion: strstart is set to the end of the current match.
  16088. + */
  16089. +#define FLUSH_BLOCK_ONLY(s, eof) { \
  16090. + _tr_flush_block(s, (s->block_start >= 0L ? \
  16091. + (charf *)&s->window[(unsigned)s->block_start] : \
  16092. + (charf *)Z_NULL), \
  16093. + (ulg)((long)s->strstart - s->block_start), \
  16094. + (eof)); \
  16095. + s->block_start = s->strstart; \
  16096. + flush_pending(s->strm); \
  16097. + Tracev((stderr,"[FLUSH]")); \
  16098. +}
  16099. +
  16100. +/* Same but force premature exit if necessary. */
  16101. +#define FLUSH_BLOCK(s, eof) { \
  16102. + FLUSH_BLOCK_ONLY(s, eof); \
  16103. + if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
  16104. +}
  16105. +
  16106. +/* ===========================================================================
  16107. + * Copy without compression as much as possible from the input stream, return
  16108. + * the current block state.
  16109. + * This function does not insert new strings in the dictionary since
  16110. + * uncompressible data is probably not useful. This function is used
  16111. + * only for the level=0 compression option.
  16112. + * NOTE: this function should be optimized to avoid extra copying from
  16113. + * window to pending_buf.
  16114. + */
  16115. +local block_state deflate_stored(s, flush)
  16116. + deflate_state *s;
  16117. + int flush;
  16118. +{
  16119. + /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
  16120. + * to pending_buf_size, and each stored block has a 5 byte header:
  16121. + */
  16122. + ulg max_block_size = 0xffff;
  16123. + ulg max_start;
  16124. +
  16125. + if (max_block_size > s->pending_buf_size - 5) {
  16126. + max_block_size = s->pending_buf_size - 5;
  16127. + }
  16128. +
  16129. + /* Copy as much as possible from input to output: */
  16130. + for (;;) {
  16131. + /* Fill the window as much as possible: */
  16132. + if (s->lookahead <= 1) {
  16133. +
  16134. + Assert(s->strstart < s->w_size+MAX_DIST(s) ||
  16135. + s->block_start >= (long)s->w_size, "slide too late");
  16136. +
  16137. + fill_window(s);
  16138. + if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
  16139. +
  16140. + if (s->lookahead == 0) break; /* flush the current block */
  16141. + }
  16142. + Assert(s->block_start >= 0L, "block gone");
  16143. +
  16144. + s->strstart += s->lookahead;
  16145. + s->lookahead = 0;
  16146. +
  16147. + /* Emit a stored block if pending_buf will be full: */
  16148. + max_start = s->block_start + max_block_size;
  16149. + if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
  16150. + /* strstart == 0 is possible when wraparound on 16-bit machine */
  16151. + s->lookahead = (uInt)(s->strstart - max_start);
  16152. + s->strstart = (uInt)max_start;
  16153. + FLUSH_BLOCK(s, 0);
  16154. + }
  16155. + /* Flush if we may have to slide, otherwise block_start may become
  16156. + * negative and the data will be gone:
  16157. + */
  16158. + if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
  16159. + FLUSH_BLOCK(s, 0);
  16160. + }
  16161. + }
  16162. + FLUSH_BLOCK(s, flush == Z_FINISH);
  16163. + return flush == Z_FINISH ? finish_done : block_done;
  16164. +}
  16165. +
  16166. +/* ===========================================================================
  16167. + * Compress as much as possible from the input stream, return the current
  16168. + * block state.
  16169. + * This function does not perform lazy evaluation of matches and inserts
  16170. + * new strings in the dictionary only for unmatched strings or for short
  16171. + * matches. It is used only for the fast compression options.
  16172. + */
  16173. +local block_state deflate_fast(s, flush)
  16174. + deflate_state *s;
  16175. + int flush;
  16176. +{
  16177. + IPos hash_head = NIL; /* head of the hash chain */
  16178. + int bflush; /* set if current block must be flushed */
  16179. +
  16180. + for (;;) {
  16181. + /* Make sure that we always have enough lookahead, except
  16182. + * at the end of the input file. We need MAX_MATCH bytes
  16183. + * for the next match, plus MIN_MATCH bytes to insert the
  16184. + * string following the next match.
  16185. + */
  16186. + if (s->lookahead < MIN_LOOKAHEAD) {
  16187. + fill_window(s);
  16188. + if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  16189. + return need_more;
  16190. + }
  16191. + if (s->lookahead == 0) break; /* flush the current block */
  16192. + }
  16193. +
  16194. + /* Insert the string window[strstart .. strstart+2] in the
  16195. + * dictionary, and set hash_head to the head of the hash chain:
  16196. + */
  16197. + if (s->lookahead >= MIN_MATCH) {
  16198. + INSERT_STRING(s, s->strstart, hash_head);
  16199. + }
  16200. +
  16201. + /* Find the longest match, discarding those <= prev_length.
  16202. + * At this point we have always match_length < MIN_MATCH
  16203. + */
  16204. + if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
  16205. + /* To simplify the code, we prevent matches with the string
  16206. + * of window index 0 (in particular we have to avoid a match
  16207. + * of the string with itself at the start of the input file).
  16208. + */
  16209. + if (s->strategy != Z_HUFFMAN_ONLY) {
  16210. + s->match_length = longest_match (s, hash_head);
  16211. + }
  16212. + /* longest_match() sets match_start */
  16213. + }
  16214. + if (s->match_length >= MIN_MATCH) {
  16215. + check_match(s, s->strstart, s->match_start, s->match_length);
  16216. +
  16217. + _tr_tally_dist(s, s->strstart - s->match_start,
  16218. + s->match_length - MIN_MATCH, bflush);
  16219. +
  16220. + s->lookahead -= s->match_length;
  16221. +
  16222. + /* Insert new strings in the hash table only if the match length
  16223. + * is not too large. This saves time but degrades compression.
  16224. + */
  16225. +#ifndef FASTEST
  16226. + if (s->match_length <= s->max_insert_length &&
  16227. + s->lookahead >= MIN_MATCH) {
  16228. + s->match_length--; /* string at strstart already in hash table */
  16229. + do {
  16230. + s->strstart++;
  16231. + INSERT_STRING(s, s->strstart, hash_head);
  16232. + /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  16233. + * always MIN_MATCH bytes ahead.
  16234. + */
  16235. + } while (--s->match_length != 0);
  16236. + s->strstart++;
  16237. + } else
  16238. +#endif
  16239. + {
  16240. + s->strstart += s->match_length;
  16241. + s->match_length = 0;
  16242. + s->ins_h = s->window[s->strstart];
  16243. + UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
  16244. +#if MIN_MATCH != 3
  16245. + Call UPDATE_HASH() MIN_MATCH-3 more times
  16246. +#endif
  16247. + /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
  16248. + * matter since it will be recomputed at next deflate call.
  16249. + */
  16250. + }
  16251. + } else {
  16252. + /* No match, output a literal byte */
  16253. + Tracevv((stderr,"%c", s->window[s->strstart]));
  16254. + _tr_tally_lit (s, s->window[s->strstart], bflush);
  16255. + s->lookahead--;
  16256. + s->strstart++;
  16257. + }
  16258. + if (bflush) FLUSH_BLOCK(s, 0);
  16259. + }
  16260. + FLUSH_BLOCK(s, flush == Z_FINISH);
  16261. + return flush == Z_FINISH ? finish_done : block_done;
  16262. +}
  16263. +
  16264. +/* ===========================================================================
  16265. + * Same as above, but achieves better compression. We use a lazy
  16266. + * evaluation for matches: a match is finally adopted only if there is
  16267. + * no better match at the next window position.
  16268. + */
  16269. +local block_state deflate_slow(s, flush)
  16270. + deflate_state *s;
  16271. + int flush;
  16272. +{
  16273. + IPos hash_head = NIL; /* head of hash chain */
  16274. + int bflush; /* set if current block must be flushed */
  16275. +
  16276. + /* Process the input block. */
  16277. + for (;;) {
  16278. + /* Make sure that we always have enough lookahead, except
  16279. + * at the end of the input file. We need MAX_MATCH bytes
  16280. + * for the next match, plus MIN_MATCH bytes to insert the
  16281. + * string following the next match.
  16282. + */
  16283. + if (s->lookahead < MIN_LOOKAHEAD) {
  16284. + fill_window(s);
  16285. + if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  16286. + return need_more;
  16287. + }
  16288. + if (s->lookahead == 0) break; /* flush the current block */
  16289. + }
  16290. +
  16291. + /* Insert the string window[strstart .. strstart+2] in the
  16292. + * dictionary, and set hash_head to the head of the hash chain:
  16293. + */
  16294. + if (s->lookahead >= MIN_MATCH) {
  16295. + INSERT_STRING(s, s->strstart, hash_head);
  16296. + }
  16297. +
  16298. + /* Find the longest match, discarding those <= prev_length.
  16299. + */
  16300. + s->prev_length = s->match_length, s->prev_match = s->match_start;
  16301. + s->match_length = MIN_MATCH-1;
  16302. +
  16303. + if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
  16304. + s->strstart - hash_head <= MAX_DIST(s)) {
  16305. + /* To simplify the code, we prevent matches with the string
  16306. + * of window index 0 (in particular we have to avoid a match
  16307. + * of the string with itself at the start of the input file).
  16308. + */
  16309. + if (s->strategy != Z_HUFFMAN_ONLY) {
  16310. + s->match_length = longest_match (s, hash_head);
  16311. + }
  16312. + /* longest_match() sets match_start */
  16313. +
  16314. + if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
  16315. + (s->match_length == MIN_MATCH &&
  16316. + s->strstart - s->match_start > TOO_FAR))) {
  16317. +
  16318. + /* If prev_match is also MIN_MATCH, match_start is garbage
  16319. + * but we will ignore the current match anyway.
  16320. + */
  16321. + s->match_length = MIN_MATCH-1;
  16322. + }
  16323. + }
  16324. + /* If there was a match at the previous step and the current
  16325. + * match is not better, output the previous match:
  16326. + */
  16327. + if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
  16328. + uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
  16329. + /* Do not insert strings in hash table beyond this. */
  16330. +
  16331. + check_match(s, s->strstart-1, s->prev_match, s->prev_length);
  16332. +
  16333. + _tr_tally_dist(s, s->strstart -1 - s->prev_match,
  16334. + s->prev_length - MIN_MATCH, bflush);
  16335. +
  16336. + /* Insert in hash table all strings up to the end of the match.
  16337. + * strstart-1 and strstart are already inserted. If there is not
  16338. + * enough lookahead, the last two strings are not inserted in
  16339. + * the hash table.
  16340. + */
  16341. + s->lookahead -= s->prev_length-1;
  16342. + s->prev_length -= 2;
  16343. + do {
  16344. + if (++s->strstart <= max_insert) {
  16345. + INSERT_STRING(s, s->strstart, hash_head);
  16346. + }
  16347. + } while (--s->prev_length != 0);
  16348. + s->match_available = 0;
  16349. + s->match_length = MIN_MATCH-1;
  16350. + s->strstart++;
  16351. +
  16352. + if (bflush) FLUSH_BLOCK(s, 0);
  16353. +
  16354. + } else if (s->match_available) {
  16355. + /* If there was no match at the previous position, output a
  16356. + * single literal. If there was a match but the current match
  16357. + * is longer, truncate the previous match to a single literal.
  16358. + */
  16359. + Tracevv((stderr,"%c", s->window[s->strstart-1]));
  16360. + _tr_tally_lit(s, s->window[s->strstart-1], bflush);
  16361. + if (bflush) {
  16362. + FLUSH_BLOCK_ONLY(s, 0);
  16363. + }
  16364. + s->strstart++;
  16365. + s->lookahead--;
  16366. + if (s->strm->avail_out == 0) return need_more;
  16367. + } else {
  16368. + /* There is no previous match to compare with, wait for
  16369. + * the next step to decide.
  16370. + */
  16371. + s->match_available = 1;
  16372. + s->strstart++;
  16373. + s->lookahead--;
  16374. + }
  16375. + }
  16376. + Assert (flush != Z_NO_FLUSH, "no flush?");
  16377. + if (s->match_available) {
  16378. + Tracevv((stderr,"%c", s->window[s->strstart-1]));
  16379. + _tr_tally_lit(s, s->window[s->strstart-1], bflush);
  16380. + s->match_available = 0;
  16381. + }
  16382. + FLUSH_BLOCK(s, flush == Z_FINISH);
  16383. + return flush == Z_FINISH ? finish_done : block_done;
  16384. +}
  16385. --- /dev/null Tue Mar 11 13:02:56 2003
  16386. +++ linux/net/ipsec/deflate.h Mon Feb 9 13:51:03 2004
  16387. @@ -0,0 +1,318 @@
  16388. +/* deflate.h -- internal compression state
  16389. + * Copyright (C) 1995-2002 Jean-loup Gailly
  16390. + * For conditions of distribution and use, see copyright notice in zlib.h
  16391. + */
  16392. +
  16393. +/* WARNING: this file should *not* be used by applications. It is
  16394. + part of the implementation of the compression library and is
  16395. + subject to change. Applications should only use zlib.h.
  16396. + */
  16397. +
  16398. +/* @(#) $Id: deflate.h,v 1.5 2004/07/10 07:48:38 mcr Exp $ */
  16399. +
  16400. +#ifndef _DEFLATE_H
  16401. +#define _DEFLATE_H
  16402. +
  16403. +#include "zlib/zutil.h"
  16404. +
  16405. +/* ===========================================================================
  16406. + * Internal compression state.
  16407. + */
  16408. +
  16409. +#define LENGTH_CODES 29
  16410. +/* number of length codes, not counting the special END_BLOCK code */
  16411. +
  16412. +#define LITERALS 256
  16413. +/* number of literal bytes 0..255 */
  16414. +
  16415. +#define L_CODES (LITERALS+1+LENGTH_CODES)
  16416. +/* number of Literal or Length codes, including the END_BLOCK code */
  16417. +
  16418. +#define D_CODES 30
  16419. +/* number of distance codes */
  16420. +
  16421. +#define BL_CODES 19
  16422. +/* number of codes used to transfer the bit lengths */
  16423. +
  16424. +#define HEAP_SIZE (2*L_CODES+1)
  16425. +/* maximum heap size */
  16426. +
  16427. +#define MAX_BITS 15
  16428. +/* All codes must not exceed MAX_BITS bits */
  16429. +
  16430. +#define INIT_STATE 42
  16431. +#define BUSY_STATE 113
  16432. +#define FINISH_STATE 666
  16433. +/* Stream status */
  16434. +
  16435. +
  16436. +/* Data structure describing a single value and its code string. */
  16437. +typedef struct ct_data_s {
  16438. + union {
  16439. + ush freq; /* frequency count */
  16440. + ush code; /* bit string */
  16441. + } fc;
  16442. + union {
  16443. + ush dad; /* father node in Huffman tree */
  16444. + ush len; /* length of bit string */
  16445. + } dl;
  16446. +} FAR ct_data;
  16447. +
  16448. +#define Freq fc.freq
  16449. +#define Code fc.code
  16450. +#define Dad dl.dad
  16451. +#define Len dl.len
  16452. +
  16453. +typedef struct static_tree_desc_s static_tree_desc;
  16454. +
  16455. +typedef struct tree_desc_s {
  16456. + ct_data *dyn_tree; /* the dynamic tree */
  16457. + int max_code; /* largest code with non zero frequency */
  16458. + static_tree_desc *stat_desc; /* the corresponding static tree */
  16459. +} FAR tree_desc;
  16460. +
  16461. +typedef ush Pos;
  16462. +typedef Pos FAR Posf;
  16463. +typedef unsigned IPos;
  16464. +
  16465. +/* A Pos is an index in the character window. We use short instead of int to
  16466. + * save space in the various tables. IPos is used only for parameter passing.
  16467. + */
  16468. +
  16469. +typedef struct internal_state {
  16470. + z_streamp strm; /* pointer back to this zlib stream */
  16471. + int status; /* as the name implies */
  16472. + Bytef *pending_buf; /* output still pending */
  16473. + ulg pending_buf_size; /* size of pending_buf */
  16474. + Bytef *pending_out; /* next pending byte to output to the stream */
  16475. + int pending; /* nb of bytes in the pending buffer */
  16476. + int noheader; /* suppress zlib header and adler32 */
  16477. + Byte data_type; /* UNKNOWN, BINARY or ASCII */
  16478. + Byte method; /* STORED (for zip only) or DEFLATED */
  16479. + int last_flush; /* value of flush param for previous deflate call */
  16480. +
  16481. + /* used by deflate.c: */
  16482. +
  16483. + uInt w_size; /* LZ77 window size (32K by default) */
  16484. + uInt w_bits; /* log2(w_size) (8..16) */
  16485. + uInt w_mask; /* w_size - 1 */
  16486. +
  16487. + Bytef *window;
  16488. + /* Sliding window. Input bytes are read into the second half of the window,
  16489. + * and move to the first half later to keep a dictionary of at least wSize
  16490. + * bytes. With this organization, matches are limited to a distance of
  16491. + * wSize-MAX_MATCH bytes, but this ensures that IO is always
  16492. + * performed with a length multiple of the block size. Also, it limits
  16493. + * the window size to 64K, which is quite useful on MSDOS.
  16494. + * To do: use the user input buffer as sliding window.
  16495. + */
  16496. +
  16497. + ulg window_size;
  16498. + /* Actual size of window: 2*wSize, except when the user input buffer
  16499. + * is directly used as sliding window.
  16500. + */
  16501. +
  16502. + Posf *prev;
  16503. + /* Link to older string with same hash index. To limit the size of this
  16504. + * array to 64K, this link is maintained only for the last 32K strings.
  16505. + * An index in this array is thus a window index modulo 32K.
  16506. + */
  16507. +
  16508. + Posf *head; /* Heads of the hash chains or NIL. */
  16509. +
  16510. + uInt ins_h; /* hash index of string to be inserted */
  16511. + uInt hash_size; /* number of elements in hash table */
  16512. + uInt hash_bits; /* log2(hash_size) */
  16513. + uInt hash_mask; /* hash_size-1 */
  16514. +
  16515. + uInt hash_shift;
  16516. + /* Number of bits by which ins_h must be shifted at each input
  16517. + * step. It must be such that after MIN_MATCH steps, the oldest
  16518. + * byte no longer takes part in the hash key, that is:
  16519. + * hash_shift * MIN_MATCH >= hash_bits
  16520. + */
  16521. +
  16522. + long block_start;
  16523. + /* Window position at the beginning of the current output block. Gets
  16524. + * negative when the window is moved backwards.
  16525. + */
  16526. +
  16527. + uInt match_length; /* length of best match */
  16528. + IPos prev_match; /* previous match */
  16529. + int match_available; /* set if previous match exists */
  16530. + uInt strstart; /* start of string to insert */
  16531. + uInt match_start; /* start of matching string */
  16532. + uInt lookahead; /* number of valid bytes ahead in window */
  16533. +
  16534. + uInt prev_length;
  16535. + /* Length of the best match at previous step. Matches not greater than this
  16536. + * are discarded. This is used in the lazy match evaluation.
  16537. + */
  16538. +
  16539. + uInt max_chain_length;
  16540. + /* To speed up deflation, hash chains are never searched beyond this
  16541. + * length. A higher limit improves compression ratio but degrades the
  16542. + * speed.
  16543. + */
  16544. +
  16545. + uInt max_lazy_match;
  16546. + /* Attempt to find a better match only when the current match is strictly
  16547. + * smaller than this value. This mechanism is used only for compression
  16548. + * levels >= 4.
  16549. + */
  16550. +# define max_insert_length max_lazy_match
  16551. + /* Insert new strings in the hash table only if the match length is not
  16552. + * greater than this length. This saves time but degrades compression.
  16553. + * max_insert_length is used only for compression levels <= 3.
  16554. + */
  16555. +
  16556. + int level; /* compression level (1..9) */
  16557. + int strategy; /* favor or force Huffman coding*/
  16558. +
  16559. + uInt good_match;
  16560. + /* Use a faster search when the previous match is longer than this */
  16561. +
  16562. + int nice_match; /* Stop searching when current match exceeds this */
  16563. +
  16564. + /* used by trees.c: */
  16565. + /* Didn't use ct_data typedef below to supress compiler warning */
  16566. + struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */
  16567. + struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
  16568. + struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */
  16569. +
  16570. + struct tree_desc_s l_desc; /* desc. for literal tree */
  16571. + struct tree_desc_s d_desc; /* desc. for distance tree */
  16572. + struct tree_desc_s bl_desc; /* desc. for bit length tree */
  16573. +
  16574. + ush bl_count[MAX_BITS+1];
  16575. + /* number of codes at each bit length for an optimal tree */
  16576. +
  16577. + int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */
  16578. + int heap_len; /* number of elements in the heap */
  16579. + int heap_max; /* element of largest frequency */
  16580. + /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  16581. + * The same heap array is used to build all trees.
  16582. + */
  16583. +
  16584. + uch depth[2*L_CODES+1];
  16585. + /* Depth of each subtree used as tie breaker for trees of equal frequency
  16586. + */
  16587. +
  16588. + uchf *l_buf; /* buffer for literals or lengths */
  16589. +
  16590. + uInt lit_bufsize;
  16591. + /* Size of match buffer for literals/lengths. There are 4 reasons for
  16592. + * limiting lit_bufsize to 64K:
  16593. + * - frequencies can be kept in 16 bit counters
  16594. + * - if compression is not successful for the first block, all input
  16595. + * data is still in the window so we can still emit a stored block even
  16596. + * when input comes from standard input. (This can also be done for
  16597. + * all blocks if lit_bufsize is not greater than 32K.)
  16598. + * - if compression is not successful for a file smaller than 64K, we can
  16599. + * even emit a stored file instead of a stored block (saving 5 bytes).
  16600. + * This is applicable only for zip (not gzip or zlib).
  16601. + * - creating new Huffman trees less frequently may not provide fast
  16602. + * adaptation to changes in the input data statistics. (Take for
  16603. + * example a binary file with poorly compressible code followed by
  16604. + * a highly compressible string table.) Smaller buffer sizes give
  16605. + * fast adaptation but have of course the overhead of transmitting
  16606. + * trees more frequently.
  16607. + * - I can't count above 4
  16608. + */
  16609. +
  16610. + uInt last_lit; /* running index in l_buf */
  16611. +
  16612. + ushf *d_buf;
  16613. + /* Buffer for distances. To simplify the code, d_buf and l_buf have
  16614. + * the same number of elements. To use different lengths, an extra flag
  16615. + * array would be necessary.
  16616. + */
  16617. +
  16618. + ulg opt_len; /* bit length of current block with optimal trees */
  16619. + ulg static_len; /* bit length of current block with static trees */
  16620. + uInt matches; /* number of string matches in current block */
  16621. + int last_eob_len; /* bit length of EOB code for last block */
  16622. +
  16623. +#ifdef DEBUG
  16624. + ulg compressed_len; /* total bit length of compressed file mod 2^32 */
  16625. + ulg bits_sent; /* bit length of compressed data sent mod 2^32 */
  16626. +#endif
  16627. +
  16628. + ush bi_buf;
  16629. + /* Output buffer. bits are inserted starting at the bottom (least
  16630. + * significant bits).
  16631. + */
  16632. + int bi_valid;
  16633. + /* Number of valid bits in bi_buf. All bits above the last valid bit
  16634. + * are always zero.
  16635. + */
  16636. +
  16637. +} FAR deflate_state;
  16638. +
  16639. +/* Output a byte on the stream.
  16640. + * IN assertion: there is enough room in pending_buf.
  16641. + */
  16642. +#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
  16643. +
  16644. +
  16645. +#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
  16646. +/* Minimum amount of lookahead, except at the end of the input file.
  16647. + * See deflate.c for comments about the MIN_MATCH+1.
  16648. + */
  16649. +
  16650. +#define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD)
  16651. +/* In order to simplify the code, particularly on 16 bit machines, match
  16652. + * distances are limited to MAX_DIST instead of WSIZE.
  16653. + */
  16654. +
  16655. + /* in trees.c */
  16656. +void _tr_init OF((deflate_state *s));
  16657. +int _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc));
  16658. +void _tr_flush_block OF((deflate_state *s, charf *buf, ulg stored_len,
  16659. + int eof));
  16660. +void _tr_align OF((deflate_state *s));
  16661. +void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
  16662. + int eof));
  16663. +
  16664. +#define d_code(dist) \
  16665. + ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)])
  16666. +/* Mapping from a distance to a distance code. dist is the distance - 1 and
  16667. + * must not have side effects. _dist_code[256] and _dist_code[257] are never
  16668. + * used.
  16669. + */
  16670. +
  16671. +#ifndef DEBUG
  16672. +/* Inline versions of _tr_tally for speed: */
  16673. +
  16674. +#if defined(GEN_TREES_H) || !defined(STDC)
  16675. + extern uch _length_code[];
  16676. + extern uch _dist_code[];
  16677. +#else
  16678. + extern const uch _length_code[];
  16679. + extern const uch _dist_code[];
  16680. +#endif
  16681. +
  16682. +# define _tr_tally_lit(s, c, flush) \
  16683. + { uch cc = (c); \
  16684. + s->d_buf[s->last_lit] = 0; \
  16685. + s->l_buf[s->last_lit++] = cc; \
  16686. + s->dyn_ltree[cc].Freq++; \
  16687. + flush = (s->last_lit == s->lit_bufsize-1); \
  16688. + }
  16689. +# define _tr_tally_dist(s, distance, length, flush) \
  16690. + { uch len = (length); \
  16691. + ush dist = (distance); \
  16692. + s->d_buf[s->last_lit] = dist; \
  16693. + s->l_buf[s->last_lit++] = len; \
  16694. + dist--; \
  16695. + s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \
  16696. + s->dyn_dtree[d_code(dist)].Freq++; \
  16697. + flush = (s->last_lit == s->lit_bufsize-1); \
  16698. + }
  16699. +#else
  16700. +# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c)
  16701. +# define _tr_tally_dist(s, distance, length, flush) \
  16702. + flush = _tr_tally(s, distance, length)
  16703. +#endif
  16704. +
  16705. +#endif /* _DEFLATE_H */
  16706. --- /dev/null Tue Mar 11 13:02:56 2003
  16707. +++ linux/net/ipsec/des/COPYRIGHT Mon Feb 9 13:51:03 2004
  16708. @@ -0,0 +1,50 @@
  16709. +Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  16710. +All rights reserved.
  16711. +
  16712. +This package is an DES implementation written by Eric Young (eay@cryptsoft.com).
  16713. +The implementation was written so as to conform with MIT's libdes.
  16714. +
  16715. +This library is free for commercial and non-commercial use as long as
  16716. +the following conditions are aheared to. The following conditions
  16717. +apply to all code found in this distribution.
  16718. +
  16719. +Copyright remains Eric Young's, and as such any Copyright notices in
  16720. +the code are not to be removed.
  16721. +If this package is used in a product, Eric Young should be given attribution
  16722. +as the author of that the SSL library. This can be in the form of a textual
  16723. +message at program startup or in documentation (online or textual) provided
  16724. +with the package.
  16725. +
  16726. +Redistribution and use in source and binary forms, with or without
  16727. +modification, are permitted provided that the following conditions
  16728. +are met:
  16729. +1. Redistributions of source code must retain the copyright
  16730. + notice, this list of conditions and the following disclaimer.
  16731. +2. Redistributions in binary form must reproduce the above copyright
  16732. + notice, this list of conditions and the following disclaimer in the
  16733. + documentation and/or other materials provided with the distribution.
  16734. +3. All advertising materials mentioning features or use of this software
  16735. + must display the following acknowledgement:
  16736. + This product includes software developed by Eric Young (eay@cryptsoft.com)
  16737. +
  16738. +THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  16739. +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16740. +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16741. +ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  16742. +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16743. +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  16744. +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  16745. +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  16746. +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  16747. +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  16748. +SUCH DAMAGE.
  16749. +
  16750. +The license and distribution terms for any publically available version or
  16751. +derivative of this code cannot be changed. i.e. this code cannot simply be
  16752. +copied and put under another distrubution license
  16753. +[including the GNU Public License.]
  16754. +
  16755. +The reason behind this being stated in this direct manner is past
  16756. +experience in code simply being copied and the attribution removed
  16757. +from it and then being distributed as part of other packages. This
  16758. +implementation was a non-trivial and unpaid effort.
  16759. --- /dev/null Tue Mar 11 13:02:56 2003
  16760. +++ linux/net/ipsec/des/INSTALL Mon Feb 9 13:51:03 2004
  16761. @@ -0,0 +1,69 @@
  16762. +Check the CC and CFLAGS lines in the makefile
  16763. +
  16764. +If your C library does not support the times(3) function, change the
  16765. +#define TIMES to
  16766. +#undef TIMES in speed.c
  16767. +If it does, check the HZ value for the times(3) function.
  16768. +If your system does not define CLK_TCK it will be assumed to
  16769. +be 100.0.
  16770. +
  16771. +If possible use gcc v 2.7.?
  16772. +Turn on the maximum optimising (normally '-O3 -fomit-frame-pointer' for gcc)
  16773. +In recent times, some system compilers give better performace.
  16774. +
  16775. +type 'make'
  16776. +
  16777. +run './destest' to check things are ok.
  16778. +run './rpw' to check the tty code for reading passwords works.
  16779. +run './speed' to see how fast those optimisations make the library run :-)
  16780. +run './des_opts' to determin the best compile time options.
  16781. +
  16782. +The output from des_opts should be put in the makefile options and des_enc.c
  16783. +should be rebuilt. For 64 bit computers, do not use the DES_PTR option.
  16784. +For the DEC Alpha, edit des.h and change DES_LONG to 'unsigned int'
  16785. +and then you can use the 'DES_PTR' option.
  16786. +
  16787. +The file options.txt has the options listed for best speed on quite a
  16788. +few systems. Look and the options (UNROLL, PTR, RISC2 etc) and then
  16789. +turn on the relevent option in the Makefile
  16790. +
  16791. +There are some special Makefile targets that make life easier.
  16792. +make cc - standard cc build
  16793. +make gcc - standard gcc build
  16794. +make x86-elf - x86 assembler (elf), linux-elf.
  16795. +make x86-out - x86 assembler (a.out), FreeBSD
  16796. +make x86-solaris- x86 assembler
  16797. +make x86-bsdi - x86 assembler (a.out with primative assembler).
  16798. +
  16799. +If at all possible use the assembler (for Windows NT/95, use
  16800. +asm/win32.obj to link with). The x86 assembler is very very fast.
  16801. +
  16802. +A make install will by default install
  16803. +libdes.a in /usr/local/lib/libdes.a
  16804. +des in /usr/local/bin/des
  16805. +des_crypt.man in /usr/local/man/man3/des_crypt.3
  16806. +des.man in /usr/local/man/man1/des.1
  16807. +des.h in /usr/include/des.h
  16808. +
  16809. +des(1) should be compatible with sunOS's but I have been unable to
  16810. +test it.
  16811. +
  16812. +These routines should compile on MSDOS, most 32bit and 64bit version
  16813. +of Unix (BSD and SYSV) and VMS, without modification.
  16814. +The only problems should be #include files that are in the wrong places.
  16815. +
  16816. +These routines can be compiled under MSDOS.
  16817. +I have successfully encrypted files using des(1) under MSDOS and then
  16818. +decrypted the files on a SparcStation.
  16819. +I have been able to compile and test the routines with
  16820. +Microsoft C v 5.1 and Turbo C v 2.0.
  16821. +The code in this library is in no way optimised for the 16bit
  16822. +operation of MSDOS.
  16823. +
  16824. +When building for glibc, ignore all of the above and just unpack into
  16825. +glibc-1.??/des and then gmake as per normal.
  16826. +
  16827. +As a final note on performace. Certain CPUs like sparcs and Alpha often give
  16828. +a %10 speed difference depending on the link order. It is rather anoying
  16829. +when one program reports 'x' DES encrypts a second and another reports
  16830. +'x*0.9' the speed.
  16831. --- /dev/null Tue Mar 11 13:02:56 2003
  16832. +++ linux/net/ipsec/des/Makefile Mon Feb 9 13:51:03 2004
  16833. @@ -0,0 +1,63 @@
  16834. +# Makefile for KLIPS kernel code as a module for 2.6 kernels
  16835. +#
  16836. +# Makefile for KLIPS kernel code as a module
  16837. +# Copyright (C) 1998, 1999, 2000,2001 Richard Guy Briggs.
  16838. +# Copyright (C) 2002-2004 Michael Richardson <mcr@freeswan.org>
  16839. +#
  16840. +# This program is free software; you can redistribute it and/or modify it
  16841. +# under the terms of the GNU General Public License as published by the
  16842. +# Free Software Foundation; either version 2 of the License, or (at your
  16843. +# option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  16844. +#
  16845. +# This program is distributed in the hope that it will be useful, but
  16846. +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16847. +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16848. +# for more details.
  16849. +#
  16850. +# RCSID $Id: Makefile.fs2_6,v 1.2.2.1 2005/08/12 16:10:57 ken Exp $
  16851. +#
  16852. +# Note! Dependencies are done automagically by 'make dep', which also
  16853. +# removes any old dependencies. DON'T put your own dependencies here
  16854. +# unless it's something special (ie not a .c file).
  16855. +#
  16856. +
  16857. +obj-$(CONFIG_KLIPS_ENC_3DES) += ipsec_alg_3des.o
  16858. +obj-$(CONFIG_KLIPS_ENC_3DES) += cbc_enc.o
  16859. +obj-$(CONFIG_KLIPS_ENC_3DES) += ecb_enc.o
  16860. +obj-$(CONFIG_KLIPS_ENC_3DES) += set_key.o
  16861. +
  16862. +ifeq ($(strip ${SUBARCH}),)
  16863. +SUBARCH:=${ARCH}
  16864. +endif
  16865. +
  16866. +# the assembly version expects frame pointers, which are
  16867. +# optional in many kernel builds. If you want speed, you should
  16868. +# probably use cryptoapi code instead.
  16869. +USEASSEMBLY=${SUBARCH}${CONFIG_FRAME_POINTER}
  16870. +ifeq (${USEASSEMBLY},i386y)
  16871. +obj-$(CONFIG_KLIPS_ENC_3DES) += dx86unix.o
  16872. +else
  16873. +obj-$(CONFIG_KLIPS_ENC_3DES) += des_enc.o
  16874. +endif
  16875. +
  16876. +#
  16877. +# $Log: Makefile.fs2_6,v $
  16878. +# Revision 1.2.2.1 2005/08/12 16:10:57 ken
  16879. +# do not use assembly code with there are no frame pointers
  16880. +#
  16881. +# Revision 1.3 2005/08/12 14:13:59 mcr
  16882. +# do not use assembly code with there are no frame pointers,
  16883. +# as it does not have the right linkages.
  16884. +#
  16885. +# Revision 1.2 2005/04/29 05:13:07 mcr
  16886. +# 3DES algorithm code.
  16887. +#
  16888. +# Revision 1.1 2004/08/17 03:27:30 mcr
  16889. +# klips 2.6 edits.
  16890. +#
  16891. +#
  16892. +# Local Variables:
  16893. +# compile-command: "(cd ../../.. && source umlsetup.sh && make -C ${POOLSPACE} module/ipsec.o)"
  16894. +# End Variables:
  16895. +#
  16896. +
  16897. --- /dev/null Tue Mar 11 13:02:56 2003
  16898. +++ linux/net/ipsec/des/README Mon Feb 9 13:51:03 2004
  16899. @@ -0,0 +1,54 @@
  16900. +
  16901. + libdes, Version 4.01 10-Jan-97
  16902. +
  16903. + Copyright (c) 1997, Eric Young
  16904. + All rights reserved.
  16905. +
  16906. + This program is free software; you can redistribute it and/or modify
  16907. + it under the terms specified in COPYRIGHT.
  16908. +
  16909. +--
  16910. +The primary ftp site for this library is
  16911. +ftp://ftp.psy.uq.oz.au/pub/Crypto/DES/libdes-x.xx.tar.gz
  16912. +libdes is now also shipped with SSLeay. Primary ftp site of
  16913. +ftp://ftp.psy.uq.oz.au/pub/Crypto/SSL/SSLeay-x.x.x.tar.gz
  16914. +
  16915. +The best way to build this library is to build it as part of SSLeay.
  16916. +
  16917. +This kit builds a DES encryption library and a DES encryption program.
  16918. +It supports ecb, cbc, ofb, cfb, triple ecb, triple cbc, triple ofb,
  16919. +triple cfb, desx, and MIT's pcbc encryption modes and also has a fast
  16920. +implementation of crypt(3).
  16921. +It contains support routines to read keys from a terminal,
  16922. +generate a random key, generate a key from an arbitrary length string,
  16923. +read/write encrypted data from/to a file descriptor.
  16924. +
  16925. +The implementation was written so as to conform with the manual entry
  16926. +for the des_crypt(3) library routines from MIT's project Athena.
  16927. +
  16928. +destest should be run after compilation to test the des routines.
  16929. +rpw should be run after compilation to test the read password routines.
  16930. +The des program is a replacement for the sun des command. I believe it
  16931. +conforms to the sun version.
  16932. +
  16933. +The Imakefile is setup for use in the kerberos distribution.
  16934. +
  16935. +These routines are best compiled with gcc or any other good
  16936. +optimising compiler.
  16937. +Just turn you optimiser up to the highest settings and run destest
  16938. +after the build to make sure everything works.
  16939. +
  16940. +I believe these routines are close to the fastest and most portable DES
  16941. +routines that use small lookup tables (4.5k) that are publicly available.
  16942. +The fcrypt routine is faster than ufc's fcrypt (when compiling with
  16943. +gcc2 -O2) on the sparc 2 (1410 vs 1270) but is not so good on other machines
  16944. +(on a sun3/260 168 vs 336). It is a function of CPU on chip cache size.
  16945. +[ 10-Jan-97 and a function of an incorrect speed testing program in
  16946. + ufc which gave much better test figures that reality ].
  16947. +
  16948. +It is worth noting that on sparc and Alpha CPUs, performance of the DES
  16949. +library can vary by upto %10 due to the positioning of files after application
  16950. +linkage.
  16951. +
  16952. +Eric Young (eay@cryptsoft.com)
  16953. +
  16954. --- /dev/null Tue Mar 11 13:02:56 2003
  16955. +++ linux/net/ipsec/des/README.freeswan Mon Feb 9 13:51:03 2004
  16956. @@ -0,0 +1,33 @@
  16957. +The only changes the FreeS/WAN project has made to libdes-lite 4.04b are:
  16958. +
  16959. +We #ifdef-ed the declaration of DES_LONG in des.h, so it's more efficient
  16960. +on the Alpha, instead of just noting the issue in a comment.
  16961. +
  16962. +We #ifdef-ed out the des_options() function in ecb_enc.c, because we don't
  16963. +use it, and its call to sprintf() can cause subtle difficulties when KLIPS
  16964. +is built as a module (depending on details of Linux configuration options).
  16965. +
  16966. +We changed some instances of CC=$(CC) in the Makefile to CC='$(CC)' to make
  16967. +it cope better with Linux kernel Makefile stupidities, and took out an
  16968. +explicit CC=gcc (unwise on systems with strange compilers).
  16969. +
  16970. +We deleted some references to <stdio.h> and <stdlib.h>, and a declaration
  16971. +of one function found only in the full libdes (not in libdes-lite), to
  16972. +avoid dragging in bits of stdio/stdlib unnecessarily. (Our thanks to Hans
  16973. +Schultz for spotting this and pointing out the fixes.)
  16974. +
  16975. +We deleted a couple of .obj files in the asm subdirectory, which appear to
  16976. +have been included in the original library by accident.
  16977. +
  16978. +We have added an include of our Makefile.inc file, to permit overriding
  16979. +things like choice of compiler (although the libdes Makefile would
  16980. +probably need some work to make this effective).
  16981. +
  16982. +
  16983. +
  16984. +Note that Eric Young is no longer at the email address listed in these
  16985. +files, and is (alas) no longer working on free crypto software.
  16986. +
  16987. +
  16988. +
  16989. +This file is RCSID $Id: README.freeswan,v 1.12 2004/07/10 08:06:51 mcr Exp $
  16990. --- /dev/null Tue Mar 11 13:02:56 2003
  16991. +++ linux/net/ipsec/des/VERSION Mon Feb 9 13:51:03 2004
  16992. @@ -0,0 +1,406 @@
  16993. +Version 4.04
  16994. + Fixed a few tests in destest. Also added x86 assember for
  16995. + des_ncbc_encrypt() which is the standard cbc mode function.
  16996. + This makes a very very large performace difference.
  16997. + Ariel Glenn ariel@columbia.edu reports that the terminal
  16998. + 'turn echo off' can return (errno == EINVAL) under solaris
  16999. + when redirection is used. So I now catch that as well as ENOTTY.
  17000. +
  17001. +
  17002. +Version 4.03
  17003. + Left a static out of enc_write.c, which caused to buffer to be
  17004. + continiously malloc()ed. Does anyone use these functions? I keep
  17005. + on feeling like removing them since I only had these in there
  17006. + for a version of kerberised login. Anyway, this was pointed out
  17007. + by Theo de Raadt <deraadt@cvs.openbsd.org>
  17008. + The 'n' bit ofb code was wrong, it was not shifting the shift
  17009. + register. It worked correctly for n == 64. Thanks to
  17010. + Gigi Ankeny <Gigi.Ankeny@Eng.Sun.COM> for pointing this one out.
  17011. +
  17012. +Version 4.02
  17013. + I was doing 'if (memcmp(weak_keys[i],key,sizeof(key)) == 0)'
  17014. + when checking for weak keys which is wrong :-(, pointed out by
  17015. + Markus F.X.J. Oberhumer <markus.oberhumer@jk.uni-linz.ac.at>.
  17016. +
  17017. +Version 4.01
  17018. + Even faster inner loop in the DES assembler for x86 and a modification
  17019. + for IP/FP which is faster on x86. Both of these changes are
  17020. + from Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk>. His
  17021. + changes make the assembler run %40 faster on a pentium. This is just
  17022. + a case of getting the instruction sequence 'just right'.
  17023. + All credit to 'Svend' :-)
  17024. + Quite a few special x86 'make' targets.
  17025. + A libdes-l (lite) distribution.
  17026. +
  17027. +Version 4.00
  17028. + After a bit of a pause, I'll up the major version number since this
  17029. + is mostly a performace release. I've added x86 assembler and
  17030. + added more options for performance. A %28 speedup for gcc
  17031. + on a pentium and the assembler is a %50 speedup.
  17032. + MIPS CPU's, sparc and Alpha are the main CPU's with speedups.
  17033. + Run des_opts to work out which options should be used.
  17034. + DES_RISC1/DES_RISC2 use alternative inner loops which use
  17035. + more registers but should give speedups on any CPU that does
  17036. + dual issue (pentium). DES_UNROLL unrolls the inner loop,
  17037. + which costs in code size.
  17038. +
  17039. +Version 3.26
  17040. + I've finally removed one of the shifts in D_ENCRYPT. This
  17041. + meant I've changed the des_SPtrans table (spr.h), the set_key()
  17042. + function and some things in des_enc.c. This has definitly
  17043. + made things faster :-). I've known about this one for some
  17044. + time but I've been too lazy to follow it up :-).
  17045. + Noticed that in the D_ENCRYPT() macro, we can just do L^=(..)^(..)^..
  17046. + instead of L^=((..)|(..)|(..).. This should save a register at
  17047. + least.
  17048. + Assember for x86. The file to replace is des_enc.c, which is replaced
  17049. + by one of the assembler files found in asm. Look at des/asm/readme
  17050. + for more info.
  17051. +
  17052. + /* Modification to fcrypt so it can be compiled to support
  17053. + HPUX 10.x's long password format, define -DLONGCRYPT to use this.
  17054. + Thanks to Jens Kupferschmidt <bt1cu@hpboot.rz.uni-leipzig.de>. */
  17055. +
  17056. + SIGWINCH case put in des_read_passwd() so the function does not
  17057. + 'exit' if this function is recieved.
  17058. +
  17059. +Version 3.25 17/07/96
  17060. + Modified read_pwd.c so that stdin can be read if not a tty.
  17061. + Thanks to Jeff Barber <jeffb@issl.atl.hp.com> for the patches.
  17062. + des_init_random_number_generator() shortened due to VMS linker
  17063. + limits.
  17064. + Added RSA's DESX cbc mode. It is a form of cbc encryption, with 2
  17065. + 8 byte quantites xored before and after encryption.
  17066. + des_xcbc_encryption() - the name is funny to preserve the des_
  17067. + prefix on all functions.
  17068. +
  17069. +Version 3.24 20/04/96
  17070. + The DES_PTR macro option checked and used by SSLeay configuration
  17071. +
  17072. +Version 3.23 11/04/96
  17073. + Added DES_LONG. If defined to 'unsigned int' on the DEC Alpha,
  17074. + it gives a %20 speedup :-)
  17075. + Fixed the problem with des.pl under perl5. The patches were
  17076. + sent by Ed Kubaitis (ejk@uiuc.edu).
  17077. + if fcrypt.c, changed values to handle illegal salt values the way
  17078. + normal crypt() implementations do. Some programs apparently use
  17079. + them :-(. The patch was sent by Bjorn Gronvall <bg@sics.se>
  17080. +
  17081. +Version 3.22 29/11/95
  17082. + Bug in des(1), an error with the uuencoding stuff when the
  17083. + 'data' is small, thanks to Geoff Keating <keagchon@mehta.anu.edu.au>
  17084. + for the patch.
  17085. +
  17086. +Version 3.21 22/11/95
  17087. + After some emailing back and forth with
  17088. + Colin Plumb <colin@nyx10.cs.du.edu>, I've tweaked a few things
  17089. + and in a future version I will probably put in some of the
  17090. + optimisation he suggested for use with the DES_USE_PTR option.
  17091. + Extra routines from Mark Murray <mark@grondar.za> for use in
  17092. + freeBSD. They mostly involve random number generation for use
  17093. + with kerberos. They involve evil machine specific system calls
  17094. + etc so I would normally suggest pushing this stuff into the
  17095. + application and/or using RAND_seed()/RAND_bytes() if you are
  17096. + using this DES library as part of SSLeay.
  17097. + Redone the read_pw() function so that it is cleaner and
  17098. + supports termios, thanks to Sameer Parekh <sameer@c2.org>
  17099. + for the initial patches for this.
  17100. + Renamed 3ecb_encrypt() to ecb3_encrypt(). This has been
  17101. + done just to make things more consistent.
  17102. + I have also now added triple DES versions of cfb and ofb.
  17103. +
  17104. +Version 3.20
  17105. + Damn, Damn, Damn, as pointed out by Mike_Spreitzer.PARC@xerox.com,
  17106. + my des_random_seed() function was only copying 4 bytes of the
  17107. + passed seed into the init structure. It is now fixed to copy 8.
  17108. + My own suggestion is to used something like MD5 :-)
  17109. +
  17110. +Version 3.19
  17111. + While looking at my code one day, I though, why do I keep on
  17112. + calling des_encrypt(in,out,ks,enc) when every function that
  17113. + calls it has in and out the same. So I dropped the 'out'
  17114. + parameter, people should not be using this function.
  17115. +
  17116. +Version 3.18 30/08/95
  17117. + Fixed a few bit with the distribution and the filenames.
  17118. + 3.17 had been munged via a move to DOS and back again.
  17119. + NO CODE CHANGES
  17120. +
  17121. +Version 3.17 14/07/95
  17122. + Fixed ede3 cbc which I had broken in 3.16. I have also
  17123. + removed some unneeded variables in 7-8 of the routines.
  17124. +
  17125. +Version 3.16 26/06/95
  17126. + Added des_encrypt2() which does not use IP/FP, used by triple
  17127. + des routines. Tweaked things a bit elsewhere. %13 speedup on
  17128. + sparc and %6 on a R4400 for ede3 cbc mode.
  17129. +
  17130. +Version 3.15 06/06/95
  17131. + Added des_ncbc_encrypt(), it is des_cbc mode except that it is
  17132. + 'normal' and copies the new iv value back over the top of the
  17133. + passed parameter.
  17134. + CHANGED des_ede3_cbc_encrypt() so that it too now overwrites
  17135. + the iv. THIS WILL BREAK EXISTING CODE, but since this function
  17136. + only new, I feel I can change it, not so with des_cbc_encrypt :-(.
  17137. + I need to update the documentation.
  17138. +
  17139. +Version 3.14 31/05/95
  17140. + New release upon the world, as part of my SSL implementation.
  17141. + New copyright and usage stuff. Basically free for all to use
  17142. + as long as you say it came from me :-)
  17143. +
  17144. +Version 3.13 31/05/95
  17145. + A fix in speed.c, if HZ is not defined, I set it to 100.0
  17146. + which is reasonable for most unixes except SunOS 4.x.
  17147. + I now have a #ifdef sun but timing for SunOS 4.x looked very
  17148. + good :-(. At my last job where I used SunOS 4.x, it was
  17149. + defined to be 60.0 (look at the old INSTALL documentation), at
  17150. + the last release had it changed to 100.0 since I now work with
  17151. + Solaris2 and SVR4 boxes.
  17152. + Thanks to Rory Chisholm <rchishol@math.ethz.ch> for pointing this
  17153. + one out.
  17154. +
  17155. +Version 3.12 08/05/95
  17156. + As pointed out by The Crypt Keeper <tck@bend.UCSD.EDU>,
  17157. + my D_ENCRYPT macro in crypt() had an un-necessary variable.
  17158. + It has been removed.
  17159. +
  17160. +Version 3.11 03/05/95
  17161. + Added des_ede3_cbc_encrypt() which is cbc mode des with 3 keys
  17162. + and one iv. It is a standard and I needed it for my SSL code.
  17163. + It makes more sense to use this for triple DES than
  17164. + 3cbc_encrypt(). I have also added (or should I say tested :-)
  17165. + cfb64_encrypt() which is cfb64 but it will encrypt a partial
  17166. + number of bytes - 3 bytes in 3 bytes out. Again this is for
  17167. + my SSL library, as a form of encryption to use with SSL
  17168. + telnet.
  17169. +
  17170. +Version 3.10 22/03/95
  17171. + Fixed a bug in 3cbc_encrypt() :-(. When making repeated calls
  17172. + to cbc3_encrypt, the 2 iv values that were being returned to
  17173. + be used in the next call were reversed :-(.
  17174. + Many thanks to Bill Wade <wade@Stoner.COM> for pointing out
  17175. + this error.
  17176. +
  17177. +Version 3.09 01/02/95
  17178. + Fixed des_random_key to far more random, it was rather feeble
  17179. + with regards to picking the initial seed. The problem was
  17180. + pointed out by Olaf Kirch <okir@monad.swb.de>.
  17181. +
  17182. +Version 3.08 14/12/94
  17183. + Added Makefile.PL so libdes can be built into perl5.
  17184. + Changed des_locl.h so RAND is always defined.
  17185. +
  17186. +Version 3.07 05/12/94
  17187. + Added GNUmake and stuff so the library can be build with
  17188. + glibc.
  17189. +
  17190. +Version 3.06 30/08/94
  17191. + Added rpc_enc.c which contains _des_crypt. This is for use in
  17192. + secure_rpc v 4.0
  17193. + Finally fixed the cfb_enc problems.
  17194. + Fixed a few parameter parsing bugs in des (-3 and -b), thanks
  17195. + to Rob McMillan <R.McMillan@its.gu.edu.au>
  17196. +
  17197. +Version 3.05 21/04/94
  17198. + for unsigned long l; gcc does not produce ((l>>34) == 0)
  17199. + This causes bugs in cfb_enc.
  17200. + Thanks to Hadmut Danisch <danisch@ira.uka.de>
  17201. +
  17202. +Version 3.04 20/04/94
  17203. + Added a version number to des.c and libdes.a
  17204. +
  17205. +Version 3.03 12/01/94
  17206. + Fixed a bug in non zero iv in 3cbc_enc.
  17207. +
  17208. +Version 3.02 29/10/93
  17209. + I now work in a place where there are 6+ architectures and 14+
  17210. + OS versions :-).
  17211. + Fixed TERMIO definition so the most sys V boxes will work :-)
  17212. +
  17213. +Release upon comp.sources.misc
  17214. +Version 3.01 08/10/93
  17215. + Added des_3cbc_encrypt()
  17216. +
  17217. +Version 3.00 07/10/93
  17218. + Fixed up documentation.
  17219. + quad_cksum definitely compatible with MIT's now.
  17220. +
  17221. +Version 2.30 24/08/93
  17222. + Triple DES now defaults to triple cbc but can do triple ecb
  17223. + with the -b flag.
  17224. + Fixed some MSDOS uuen/uudecoding problems, thanks to
  17225. + Added prototypes.
  17226. +
  17227. +Version 2.22 29/06/93
  17228. + Fixed a bug in des_is_weak_key() which stopped it working :-(
  17229. + thanks to engineering@MorningStar.Com.
  17230. +
  17231. +Version 2.21 03/06/93
  17232. + des(1) with no arguments gives quite a bit of help.
  17233. + Added -c (generate ckecksum) flag to des(1).
  17234. + Added -3 (triple DES) flag to des(1).
  17235. + Added cfb and ofb routines to the library.
  17236. +
  17237. +Version 2.20 11/03/93
  17238. + Added -u (uuencode) flag to des(1).
  17239. + I have been playing with byte order in quad_cksum to make it
  17240. + compatible with MIT's version. All I can say is avid this
  17241. + function if possible since MIT's output is endian dependent.
  17242. +
  17243. +Version 2.12 14/10/92
  17244. + Added MSDOS specific macro in ecb_encrypt which gives a %70
  17245. + speed up when the code is compiled with turbo C.
  17246. +
  17247. +Version 2.11 12/10/92
  17248. + Speedup in set_key (recoding of PC-1)
  17249. + I now do it in 47 simple operations, down from 60.
  17250. + Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)
  17251. + for motivating me to look for a faster system :-)
  17252. + The speedup is probably less that 1% but it is still 13
  17253. + instructions less :-).
  17254. +
  17255. +Version 2.10 06/10/92
  17256. + The code now works on the 64bit ETA10 and CRAY without modifications or
  17257. + #defines. I believe the code should work on any machine that
  17258. + defines long, int or short to be 8 bytes long.
  17259. + Thanks to Shabbir J. Safdar (shabby@mentor.cc.purdue.edu)
  17260. + for helping me fix the code to run on 64bit machines (he had
  17261. + access to an ETA10).
  17262. + Thanks also to John Fletcher <john_fletcher@lccmail.ocf.llnl.gov>
  17263. + for testing the routines on a CRAY.
  17264. + read_password.c has been renamed to read_passwd.c
  17265. + string_to_key.c has been renamed to string2key.c
  17266. +
  17267. +Version 2.00 14/09/92
  17268. + Made mods so that the library should work on 64bit CPU's.
  17269. + Removed all my uchar and ulong defs. To many different
  17270. + versions of unix define them in their header files in too many
  17271. + different combinations :-)
  17272. + IRIX - Sillicon Graphics mods (mostly in read_password.c).
  17273. + Thanks to Andrew Daviel (advax@erich.triumf.ca)
  17274. +
  17275. +Version 1.99 26/08/92
  17276. + Fixed a bug or 2 in enc_read.c
  17277. + Fixed a bug in enc_write.c
  17278. + Fixed a pseudo bug in fcrypt.c (very obscure).
  17279. +
  17280. +Version 1.98 31/07/92
  17281. + Support for the ETA10. This is a strange machine that defines
  17282. + longs and ints as 8 bytes and shorts as 4 bytes.
  17283. + Since I do evil things with long * that assume that they are 4
  17284. + bytes. Look in the Makefile for the option to compile for
  17285. + this machine. quad_cksum appears to have problems but I
  17286. + will don't have the time to fix it right now, and this is not
  17287. + a function that uses DES and so will not effect the main uses
  17288. + of the library.
  17289. +
  17290. +Version 1.97 20/05/92 eay
  17291. + Fixed the Imakefile and made some changes to des.h to fix some
  17292. + problems when building this package with Kerberos v 4.
  17293. +
  17294. +Version 1.96 18/05/92 eay
  17295. + Fixed a small bug in string_to_key() where problems could
  17296. + occur if des_check_key was set to true and the string
  17297. + generated a weak key.
  17298. +
  17299. +Patch2 posted to comp.sources.misc
  17300. +Version 1.95 13/05/92 eay
  17301. + Added an alternative version of the D_ENCRYPT macro in
  17302. + ecb_encrypt and fcrypt. Depending on the compiler, one version or the
  17303. + other will be faster. This was inspired by
  17304. + Dana How <how@isl.stanford.edu>, and her pointers about doing the
  17305. + *(ulong *)((uchar *)ptr+(value&0xfc))
  17306. + vs
  17307. + ptr[value&0x3f]
  17308. + to stop the C compiler doing a <<2 to convert the long array index.
  17309. +
  17310. +Version 1.94 05/05/92 eay
  17311. + Fixed an incompatibility between my string_to_key and the MIT
  17312. + version. When the key is longer than 8 chars, I was wrapping
  17313. + with a different method. To use the old version, define
  17314. + OLD_STR_TO_KEY in the makefile. Thanks to
  17315. + viktor@newsu.shearson.com (Viktor Dukhovni).
  17316. +
  17317. +Version 1.93 28/04/92 eay
  17318. + Fixed the VMS mods so that echo is now turned off in
  17319. + read_password. Thanks again to brennan@coco.cchs.su.oz.AU.
  17320. + MSDOS support added. The routines can be compiled with
  17321. + Turbo C (v2.0) and MSC (v5.1). Make sure MSDOS is defined.
  17322. +
  17323. +Patch1 posted to comp.sources.misc
  17324. +Version 1.92 13/04/92 eay
  17325. + Changed D_ENCRYPT so that the rotation of R occurs outside of
  17326. + the loop. This required rotating all the longs in sp.h (now
  17327. + called spr.h). Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
  17328. + speed.c has been changed so it will work without SIGALRM. If
  17329. + times(3) is not present it will try to use ftime() instead.
  17330. +
  17331. +Version 1.91 08/04/92 eay
  17332. + Added -E/-D options to des(1) so it can use string_to_key.
  17333. + Added SVR4 mods suggested by witr@rwwa.COM
  17334. + Added VMS mods suggested by brennan@coco.cchs.su.oz.AU. If
  17335. + anyone knows how to turn of tty echo in VMS please tell me or
  17336. + implement it yourself :-).
  17337. + Changed FILE *IN/*OUT to *DES_IN/*DES_OUT since it appears VMS
  17338. + does not like IN/OUT being used.
  17339. +
  17340. +Libdes posted to comp.sources.misc
  17341. +Version 1.9 24/03/92 eay
  17342. + Now contains a fast small crypt replacement.
  17343. + Added des(1) command.
  17344. + Added des_rw_mode so people can use cbc encryption with
  17345. + enc_read and enc_write.
  17346. +
  17347. +Version 1.8 15/10/91 eay
  17348. + Bug in cbc_cksum.
  17349. + Many thanks to Keith Reynolds (keithr@sco.COM) for pointing this
  17350. + one out.
  17351. +
  17352. +Version 1.7 24/09/91 eay
  17353. + Fixed set_key :-)
  17354. + set_key is 4 times faster and takes less space.
  17355. + There are a few minor changes that could be made.
  17356. +
  17357. +Version 1.6 19/09/1991 eay
  17358. + Finally go IP and FP finished.
  17359. + Now I need to fix set_key.
  17360. + This version is quite a bit faster that 1.51
  17361. +
  17362. +Version 1.52 15/06/1991 eay
  17363. + 20% speedup in ecb_encrypt by changing the E bit selection
  17364. + to use 2 32bit words. This also required modification of the
  17365. + sp table. There is still a way to speedup the IP and IP-1
  17366. + (hints from outer@sq.com) still working on this one :-(.
  17367. +
  17368. +Version 1.51 07/06/1991 eay
  17369. + Faster des_encrypt by loop unrolling
  17370. + Fixed bug in quad_cksum.c (thanks to hughes@logos.ucs.indiana.edu)
  17371. +
  17372. +Version 1.50 28/05/1991 eay
  17373. + Optimised the code a bit more for the sparc. I have improved the
  17374. + speed of the inner des_encrypt by speeding up the initial and
  17375. + final permutations.
  17376. +
  17377. +Version 1.40 23/10/1990 eay
  17378. + Fixed des_random_key, it did not produce a random key :-(
  17379. +
  17380. +Version 1.30 2/10/1990 eay
  17381. + Have made des_quad_cksum the same as MIT's, the full package
  17382. + should be compatible with MIT's
  17383. + Have tested on a DECstation 3100
  17384. + Still need to fix des_set_key (make it faster).
  17385. + Does des_cbc_encrypts at 70.5k/sec on a 3100.
  17386. +
  17387. +Version 1.20 18/09/1990 eay
  17388. + Fixed byte order dependencies.
  17389. + Fixed (I hope) all the word alignment problems.
  17390. + Speedup in des_ecb_encrypt.
  17391. +
  17392. +Version 1.10 11/09/1990 eay
  17393. + Added des_enc_read and des_enc_write.
  17394. + Still need to fix des_quad_cksum.
  17395. + Still need to document des_enc_read and des_enc_write.
  17396. +
  17397. +Version 1.00 27/08/1990 eay
  17398. +
  17399. --- /dev/null Tue Mar 11 13:02:56 2003
  17400. +++ linux/net/ipsec/des/asm/des-586.pl Mon Feb 9 13:51:03 2004
  17401. @@ -0,0 +1,251 @@
  17402. +#!/usr/local/bin/perl
  17403. +#
  17404. +# The inner loop instruction sequence and the IP/FP modifications are from
  17405. +# Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk>
  17406. +#
  17407. +
  17408. +push(@INC,"perlasm","../../perlasm");
  17409. +require "x86asm.pl";
  17410. +require "cbc.pl";
  17411. +require "desboth.pl";
  17412. +
  17413. +# base code is in microsft
  17414. +# op dest, source
  17415. +# format.
  17416. +#
  17417. +
  17418. +&asm_init($ARGV[0],"des-586.pl");
  17419. +
  17420. +$L="edi";
  17421. +$R="esi";
  17422. +
  17423. +&external_label("des_SPtrans");
  17424. +&des_encrypt("des_encrypt",1);
  17425. +&des_encrypt("des_encrypt2",0);
  17426. +&des_encrypt3("des_encrypt3",1);
  17427. +&des_encrypt3("des_decrypt3",0);
  17428. +&cbc("des_ncbc_encrypt","des_encrypt","des_encrypt",0,4,5,3,5,-1);
  17429. +&cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3",0,6,7,3,4,5);
  17430. +
  17431. +&asm_finish();
  17432. +
  17433. +sub des_encrypt
  17434. + {
  17435. + local($name,$do_ip)=@_;
  17436. +
  17437. + &function_begin_B($name,"EXTRN _des_SPtrans:DWORD");
  17438. +
  17439. + &push("esi");
  17440. + &push("edi");
  17441. +
  17442. + &comment("");
  17443. + &comment("Load the 2 words");
  17444. + $ks="ebp";
  17445. +
  17446. + if ($do_ip)
  17447. + {
  17448. + &mov($R,&wparam(0));
  17449. + &xor( "ecx", "ecx" );
  17450. +
  17451. + &push("ebx");
  17452. + &push("ebp");
  17453. +
  17454. + &mov("eax",&DWP(0,$R,"",0));
  17455. + &mov("ebx",&wparam(2)); # get encrypt flag
  17456. + &mov($L,&DWP(4,$R,"",0));
  17457. + &comment("");
  17458. + &comment("IP");
  17459. + &IP_new("eax",$L,$R,3);
  17460. + }
  17461. + else
  17462. + {
  17463. + &mov("eax",&wparam(0));
  17464. + &xor( "ecx", "ecx" );
  17465. +
  17466. + &push("ebx");
  17467. + &push("ebp");
  17468. +
  17469. + &mov($R,&DWP(0,"eax","",0));
  17470. + &mov("ebx",&wparam(2)); # get encrypt flag
  17471. + &rotl($R,3);
  17472. + &mov($L,&DWP(4,"eax","",0));
  17473. + &rotl($L,3);
  17474. + }
  17475. +
  17476. + &mov( $ks, &wparam(1) );
  17477. + &cmp("ebx","0");
  17478. + &je(&label("start_decrypt"));
  17479. +
  17480. + for ($i=0; $i<16; $i+=2)
  17481. + {
  17482. + &comment("");
  17483. + &comment("Round $i");
  17484. + &D_ENCRYPT($i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
  17485. +
  17486. + &comment("");
  17487. + &comment("Round ".sprintf("%d",$i+1));
  17488. + &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
  17489. + }
  17490. + &jmp(&label("end"));
  17491. +
  17492. + &set_label("start_decrypt");
  17493. +
  17494. + for ($i=15; $i>0; $i-=2)
  17495. + {
  17496. + &comment("");
  17497. + &comment("Round $i");
  17498. + &D_ENCRYPT(15-$i,$L,$R,$i*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
  17499. + &comment("");
  17500. + &comment("Round ".sprintf("%d",$i-1));
  17501. + &D_ENCRYPT(15-$i+1,$R,$L,($i-1)*2,$ks,"des_SPtrans","eax","ebx","ecx","edx");
  17502. + }
  17503. +
  17504. + &set_label("end");
  17505. +
  17506. + if ($do_ip)
  17507. + {
  17508. + &comment("");
  17509. + &comment("FP");
  17510. + &mov("edx",&wparam(0));
  17511. + &FP_new($L,$R,"eax",3);
  17512. +
  17513. + &mov(&DWP(0,"edx","",0),"eax");
  17514. + &mov(&DWP(4,"edx","",0),$R);
  17515. + }
  17516. + else
  17517. + {
  17518. + &comment("");
  17519. + &comment("Fixup");
  17520. + &rotr($L,3); # r
  17521. + &mov("eax",&wparam(0));
  17522. + &rotr($R,3); # l
  17523. + &mov(&DWP(0,"eax","",0),$L);
  17524. + &mov(&DWP(4,"eax","",0),$R);
  17525. + }
  17526. +
  17527. + &pop("ebp");
  17528. + &pop("ebx");
  17529. + &pop("edi");
  17530. + &pop("esi");
  17531. + &ret();
  17532. +
  17533. + &function_end_B($name);
  17534. + }
  17535. +
  17536. +sub D_ENCRYPT
  17537. + {
  17538. + local($r,$L,$R,$S,$ks,$desSP,$u,$tmp1,$tmp2,$t)=@_;
  17539. +
  17540. + &mov( $u, &DWP(&n2a($S*4),$ks,"",0));
  17541. + &xor( $tmp1, $tmp1);
  17542. + &mov( $t, &DWP(&n2a(($S+1)*4),$ks,"",0));
  17543. + &xor( $u, $R);
  17544. + &xor( $t, $R);
  17545. + &and( $u, "0xfcfcfcfc" );
  17546. + &and( $t, "0xcfcfcfcf" );
  17547. + &movb( &LB($tmp1), &LB($u) );
  17548. + &movb( &LB($tmp2), &HB($u) );
  17549. + &rotr( $t, 4 );
  17550. + &mov( $ks, &DWP(" $desSP",$tmp1,"",0));
  17551. + &movb( &LB($tmp1), &LB($t) );
  17552. + &xor( $L, $ks);
  17553. + &mov( $ks, &DWP("0x200+$desSP",$tmp2,"",0));
  17554. + &xor( $L, $ks); ######
  17555. + &movb( &LB($tmp2), &HB($t) );
  17556. + &shr( $u, 16);
  17557. + &mov( $ks, &DWP("0x100+$desSP",$tmp1,"",0));
  17558. + &xor( $L, $ks); ######
  17559. + &movb( &LB($tmp1), &HB($u) );
  17560. + &shr( $t, 16);
  17561. + &mov( $ks, &DWP("0x300+$desSP",$tmp2,"",0));
  17562. + &xor( $L, $ks);
  17563. + &mov( $ks, &wparam(1) );
  17564. + &movb( &LB($tmp2), &HB($t) );
  17565. + &and( $u, "0xff" );
  17566. + &and( $t, "0xff" );
  17567. + &mov( $tmp1, &DWP("0x600+$desSP",$tmp1,"",0));
  17568. + &xor( $L, $tmp1);
  17569. + &mov( $tmp1, &DWP("0x700+$desSP",$tmp2,"",0));
  17570. + &xor( $L, $tmp1);
  17571. + &mov( $tmp1, &DWP("0x400+$desSP",$u,"",0));
  17572. + &xor( $L, $tmp1);
  17573. + &mov( $tmp1, &DWP("0x500+$desSP",$t,"",0));
  17574. + &xor( $L, $tmp1);
  17575. + }
  17576. +
  17577. +sub n2a
  17578. + {
  17579. + sprintf("%d",$_[0]);
  17580. + }
  17581. +
  17582. +# now has a side affect of rotating $a by $shift
  17583. +sub R_PERM_OP
  17584. + {
  17585. + local($a,$b,$tt,$shift,$mask,$last)=@_;
  17586. +
  17587. + &rotl( $a, $shift ) if ($shift != 0);
  17588. + &mov( $tt, $a );
  17589. + &xor( $a, $b );
  17590. + &and( $a, $mask );
  17591. + if (!$last eq $b)
  17592. + {
  17593. + &xor( $b, $a );
  17594. + &xor( $tt, $a );
  17595. + }
  17596. + else
  17597. + {
  17598. + &xor( $tt, $a );
  17599. + &xor( $b, $a );
  17600. + }
  17601. + &comment("");
  17602. + }
  17603. +
  17604. +sub IP_new
  17605. + {
  17606. + local($l,$r,$tt,$lr)=@_;
  17607. +
  17608. + &R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l);
  17609. + &R_PERM_OP($r,$tt,$l,20,"0xfff0000f",$l);
  17610. + &R_PERM_OP($l,$tt,$r,14,"0x33333333",$r);
  17611. + &R_PERM_OP($tt,$r,$l,22,"0x03fc03fc",$r);
  17612. + &R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r);
  17613. +
  17614. + if ($lr != 3)
  17615. + {
  17616. + if (($lr-3) < 0)
  17617. + { &rotr($tt, 3-$lr); }
  17618. + else { &rotl($tt, $lr-3); }
  17619. + }
  17620. + if ($lr != 2)
  17621. + {
  17622. + if (($lr-2) < 0)
  17623. + { &rotr($r, 2-$lr); }
  17624. + else { &rotl($r, $lr-2); }
  17625. + }
  17626. + }
  17627. +
  17628. +sub FP_new
  17629. + {
  17630. + local($l,$r,$tt,$lr)=@_;
  17631. +
  17632. + if ($lr != 2)
  17633. + {
  17634. + if (($lr-2) < 0)
  17635. + { &rotl($r, 2-$lr); }
  17636. + else { &rotr($r, $lr-2); }
  17637. + }
  17638. + if ($lr != 3)
  17639. + {
  17640. + if (($lr-3) < 0)
  17641. + { &rotl($l, 3-$lr); }
  17642. + else { &rotr($l, $lr-3); }
  17643. + }
  17644. +
  17645. + &R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r);
  17646. + &R_PERM_OP($tt,$r,$l,23,"0x03fc03fc",$r);
  17647. + &R_PERM_OP($l,$r,$tt,10,"0x33333333",$l);
  17648. + &R_PERM_OP($r,$tt,$l,18,"0xfff0000f",$l);
  17649. + &R_PERM_OP($l,$tt,$r,12,"0xf0f0f0f0",$r);
  17650. + &rotr($tt , 4);
  17651. + }
  17652. +
  17653. --- /dev/null Tue Mar 11 13:02:56 2003
  17654. +++ linux/net/ipsec/des/asm/des686.pl Mon Feb 9 13:51:03 2004
  17655. @@ -0,0 +1,230 @@
  17656. +#!/usr/local/bin/perl
  17657. +
  17658. +$prog="des686.pl";
  17659. +
  17660. +# base code is in microsft
  17661. +# op dest, source
  17662. +# format.
  17663. +#
  17664. +
  17665. +# WILL NOT WORK ANYMORE WITH desboth.pl
  17666. +require "desboth.pl";
  17667. +
  17668. +if ( ($ARGV[0] eq "elf"))
  17669. + { require "x86unix.pl"; }
  17670. +elsif ( ($ARGV[0] eq "a.out"))
  17671. + { $aout=1; require "x86unix.pl"; }
  17672. +elsif ( ($ARGV[0] eq "sol"))
  17673. + { $sol=1; require "x86unix.pl"; }
  17674. +elsif ( ($ARGV[0] eq "cpp"))
  17675. + { $cpp=1; require "x86unix.pl"; }
  17676. +elsif ( ($ARGV[0] eq "win32"))
  17677. + { require "x86ms.pl"; }
  17678. +else
  17679. + {
  17680. + print STDERR <<"EOF";
  17681. +Pick one target type from
  17682. + elf - linux, FreeBSD etc
  17683. + a.out - old linux
  17684. + sol - x86 solaris
  17685. + cpp - format so x86unix.cpp can be used
  17686. + win32 - Windows 95/Windows NT
  17687. +EOF
  17688. + exit(1);
  17689. + }
  17690. +
  17691. +&comment("Don't even think of reading this code");
  17692. +&comment("It was automatically generated by $prog");
  17693. +&comment("Which is a perl program used to generate the x86 assember for");
  17694. +&comment("any of elf, a.out, Win32, or Solaris");
  17695. +&comment("It can be found in SSLeay 0.6.5+ or in libdes 3.26+");
  17696. +&comment("eric <eay\@cryptsoft.com>");
  17697. +&comment("");
  17698. +
  17699. +&file("dx86xxxx");
  17700. +
  17701. +$L="edi";
  17702. +$R="esi";
  17703. +
  17704. +&des_encrypt("des_encrypt",1);
  17705. +&des_encrypt("des_encrypt2",0);
  17706. +
  17707. +&des_encrypt3("des_encrypt3",1);
  17708. +&des_encrypt3("des_decrypt3",0);
  17709. +
  17710. +&file_end();
  17711. +
  17712. +sub des_encrypt
  17713. + {
  17714. + local($name,$do_ip)=@_;
  17715. +
  17716. + &function_begin($name,"EXTRN _des_SPtrans:DWORD");
  17717. +
  17718. + &comment("");
  17719. + &comment("Load the 2 words");
  17720. + &mov("eax",&wparam(0));
  17721. + &mov($L,&DWP(0,"eax","",0));
  17722. + &mov($R,&DWP(4,"eax","",0));
  17723. +
  17724. + $ksp=&wparam(1);
  17725. +
  17726. + if ($do_ip)
  17727. + {
  17728. + &comment("");
  17729. + &comment("IP");
  17730. + &IP_new($L,$R,"eax");
  17731. + }
  17732. +
  17733. + &comment("");
  17734. + &comment("fixup rotate");
  17735. + &rotl($R,3);
  17736. + &rotl($L,3);
  17737. + &exch($L,$R);
  17738. +
  17739. + &comment("");
  17740. + &comment("load counter, key_schedule and enc flag");
  17741. + &mov("eax",&wparam(2)); # get encrypt flag
  17742. + &mov("ebp",&wparam(1)); # get ks
  17743. + &cmp("eax","0");
  17744. + &je(&label("start_decrypt"));
  17745. +
  17746. + # encrypting part
  17747. +
  17748. + for ($i=0; $i<16; $i+=2)
  17749. + {
  17750. + &comment("");
  17751. + &comment("Round $i");
  17752. + &D_ENCRYPT($L,$R,$i*2,"ebp","des_SPtrans","ecx","edx","eax","ebx");
  17753. +
  17754. + &comment("");
  17755. + &comment("Round ".sprintf("%d",$i+1));
  17756. + &D_ENCRYPT($R,$L,($i+1)*2,"ebp","des_SPtrans","ecx","edx","eax","ebx");
  17757. + }
  17758. + &jmp(&label("end"));
  17759. +
  17760. + &set_label("start_decrypt");
  17761. +
  17762. + for ($i=15; $i>0; $i-=2)
  17763. + {
  17764. + &comment("");
  17765. + &comment("Round $i");
  17766. + &D_ENCRYPT($L,$R,$i*2,"ebp","des_SPtrans","ecx","edx","eax","ebx");
  17767. + &comment("");
  17768. + &comment("Round ".sprintf("%d",$i-1));
  17769. + &D_ENCRYPT($R,$L,($i-1)*2,"ebp","des_SPtrans","ecx","edx","eax","ebx");
  17770. + }
  17771. +
  17772. + &set_label("end");
  17773. +
  17774. + &comment("");
  17775. + &comment("Fixup");
  17776. + &rotr($L,3); # r
  17777. + &rotr($R,3); # l
  17778. +
  17779. + if ($do_ip)
  17780. + {
  17781. + &comment("");
  17782. + &comment("FP");
  17783. + &FP_new($R,$L,"eax");
  17784. + }
  17785. +
  17786. + &mov("eax",&wparam(0));
  17787. + &mov(&DWP(0,"eax","",0),$L);
  17788. + &mov(&DWP(4,"eax","",0),$R);
  17789. +
  17790. + &function_end($name);
  17791. + }
  17792. +
  17793. +
  17794. +# The logic is to load R into 2 registers and operate on both at the same time.
  17795. +# We also load the 2 R's into 2 more registers so we can do the 'move word down a byte'
  17796. +# while also masking the other copy and doing a lookup. We then also accumulate the
  17797. +# L value in 2 registers then combine them at the end.
  17798. +sub D_ENCRYPT
  17799. + {
  17800. + local($L,$R,$S,$ks,$desSP,$u,$t,$tmp1,$tmp2,$tmp3)=@_;
  17801. +
  17802. + &mov( $u, &DWP(&n2a($S*4),$ks,"",0));
  17803. + &mov( $t, &DWP(&n2a(($S+1)*4),$ks,"",0));
  17804. + &xor( $u, $R );
  17805. + &xor( $t, $R );
  17806. + &rotr( $t, 4 );
  17807. +
  17808. + # the numbers at the end of the line are origional instruction order
  17809. + &mov( $tmp2, $u ); # 1 2
  17810. + &mov( $tmp1, $t ); # 1 1
  17811. + &and( $tmp2, "0xfc" ); # 1 4
  17812. + &and( $tmp1, "0xfc" ); # 1 3
  17813. + &shr( $t, 8 ); # 1 5
  17814. + &xor( $L, &DWP("0x100+$desSP",$tmp1,"",0)); # 1 7
  17815. + &shr( $u, 8 ); # 1 6
  17816. + &mov( $tmp1, &DWP(" $desSP",$tmp2,"",0)); # 1 8
  17817. +
  17818. + &mov( $tmp2, $u ); # 2 2
  17819. + &xor( $L, $tmp1 ); # 1 9
  17820. + &and( $tmp2, "0xfc" ); # 2 4
  17821. + &mov( $tmp1, $t ); # 2 1
  17822. + &and( $tmp1, "0xfc" ); # 2 3
  17823. + &shr( $t, 8 ); # 2 5
  17824. + &xor( $L, &DWP("0x300+$desSP",$tmp1,"",0)); # 2 7
  17825. + &shr( $u, 8 ); # 2 6
  17826. + &mov( $tmp1, &DWP("0x200+$desSP",$tmp2,"",0)); # 2 8
  17827. + &mov( $tmp2, $u ); # 3 2
  17828. +
  17829. + &xor( $L, $tmp1 ); # 2 9
  17830. + &and( $tmp2, "0xfc" ); # 3 4
  17831. +
  17832. + &mov( $tmp1, $t ); # 3 1
  17833. + &shr( $u, 8 ); # 3 6
  17834. + &and( $tmp1, "0xfc" ); # 3 3
  17835. + &shr( $t, 8 ); # 3 5
  17836. + &xor( $L, &DWP("0x500+$desSP",$tmp1,"",0)); # 3 7
  17837. + &mov( $tmp1, &DWP("0x400+$desSP",$tmp2,"",0)); # 3 8
  17838. +
  17839. + &and( $t, "0xfc" ); # 4 1
  17840. + &xor( $L, $tmp1 ); # 3 9
  17841. +
  17842. + &and( $u, "0xfc" ); # 4 2
  17843. + &xor( $L, &DWP("0x700+$desSP",$t,"",0)); # 4 3
  17844. + &xor( $L, &DWP("0x600+$desSP",$u,"",0)); # 4 4
  17845. + }
  17846. +
  17847. +sub PERM_OP
  17848. + {
  17849. + local($a,$b,$tt,$shift,$mask)=@_;
  17850. +
  17851. + &mov( $tt, $a );
  17852. + &shr( $tt, $shift );
  17853. + &xor( $tt, $b );
  17854. + &and( $tt, $mask );
  17855. + &xor( $b, $tt );
  17856. + &shl( $tt, $shift );
  17857. + &xor( $a, $tt );
  17858. + }
  17859. +
  17860. +sub IP_new
  17861. + {
  17862. + local($l,$r,$tt)=@_;
  17863. +
  17864. + &PERM_OP($r,$l,$tt, 4,"0x0f0f0f0f");
  17865. + &PERM_OP($l,$r,$tt,16,"0x0000ffff");
  17866. + &PERM_OP($r,$l,$tt, 2,"0x33333333");
  17867. + &PERM_OP($l,$r,$tt, 8,"0x00ff00ff");
  17868. + &PERM_OP($r,$l,$tt, 1,"0x55555555");
  17869. + }
  17870. +
  17871. +sub FP_new
  17872. + {
  17873. + local($l,$r,$tt)=@_;
  17874. +
  17875. + &PERM_OP($l,$r,$tt, 1,"0x55555555");
  17876. + &PERM_OP($r,$l,$tt, 8,"0x00ff00ff");
  17877. + &PERM_OP($l,$r,$tt, 2,"0x33333333");
  17878. + &PERM_OP($r,$l,$tt,16,"0x0000ffff");
  17879. + &PERM_OP($l,$r,$tt, 4,"0x0f0f0f0f");
  17880. + }
  17881. +
  17882. +sub n2a
  17883. + {
  17884. + sprintf("%d",$_[0]);
  17885. + }
  17886. --- /dev/null Tue Mar 11 13:02:56 2003
  17887. +++ linux/net/ipsec/des/asm/desboth.pl Mon Feb 9 13:51:03 2004
  17888. @@ -0,0 +1,79 @@
  17889. +#!/usr/local/bin/perl
  17890. +
  17891. +$L="edi";
  17892. +$R="esi";
  17893. +
  17894. +sub des_encrypt3
  17895. + {
  17896. + local($name,$enc)=@_;
  17897. +
  17898. + &function_begin_B($name,"");
  17899. + &push("ebx");
  17900. + &mov("ebx",&wparam(0));
  17901. +
  17902. + &push("ebp");
  17903. + &push("esi");
  17904. +
  17905. + &push("edi");
  17906. +
  17907. + &comment("");
  17908. + &comment("Load the data words");
  17909. + &mov($L,&DWP(0,"ebx","",0));
  17910. + &mov($R,&DWP(4,"ebx","",0));
  17911. + &stack_push(3);
  17912. +
  17913. + &comment("");
  17914. + &comment("IP");
  17915. + &IP_new($L,$R,"edx",0);
  17916. +
  17917. + # put them back
  17918. +
  17919. + if ($enc)
  17920. + {
  17921. + &mov(&DWP(4,"ebx","",0),$R);
  17922. + &mov("eax",&wparam(1));
  17923. + &mov(&DWP(0,"ebx","",0),"edx");
  17924. + &mov("edi",&wparam(2));
  17925. + &mov("esi",&wparam(3));
  17926. + }
  17927. + else
  17928. + {
  17929. + &mov(&DWP(4,"ebx","",0),$R);
  17930. + &mov("esi",&wparam(1));
  17931. + &mov(&DWP(0,"ebx","",0),"edx");
  17932. + &mov("edi",&wparam(2));
  17933. + &mov("eax",&wparam(3));
  17934. + }
  17935. + &mov(&swtmp(2), (($enc)?"1":"0"));
  17936. + &mov(&swtmp(1), "eax");
  17937. + &mov(&swtmp(0), "ebx");
  17938. + &call("des_encrypt2");
  17939. + &mov(&swtmp(2), (($enc)?"0":"1"));
  17940. + &mov(&swtmp(1), "edi");
  17941. + &mov(&swtmp(0), "ebx");
  17942. + &call("des_encrypt2");
  17943. + &mov(&swtmp(2), (($enc)?"1":"0"));
  17944. + &mov(&swtmp(1), "esi");
  17945. + &mov(&swtmp(0), "ebx");
  17946. + &call("des_encrypt2");
  17947. +
  17948. + &stack_pop(3);
  17949. + &mov($L,&DWP(0,"ebx","",0));
  17950. + &mov($R,&DWP(4,"ebx","",0));
  17951. +
  17952. + &comment("");
  17953. + &comment("FP");
  17954. + &FP_new($L,$R,"eax",0);
  17955. +
  17956. + &mov(&DWP(0,"ebx","",0),"eax");
  17957. + &mov(&DWP(4,"ebx","",0),$R);
  17958. +
  17959. + &pop("edi");
  17960. + &pop("esi");
  17961. + &pop("ebp");
  17962. + &pop("ebx");
  17963. + &ret();
  17964. + &function_end_B($name);
  17965. + }
  17966. +
  17967. +
  17968. --- /dev/null Tue Mar 11 13:02:56 2003
  17969. +++ linux/net/ipsec/des/asm/readme Mon Feb 9 13:51:03 2004
  17970. @@ -0,0 +1,131 @@
  17971. +First up, let me say I don't like writing in assembler. It is not portable,
  17972. +dependant on the particular CPU architecture release and is generally a pig
  17973. +to debug and get right. Having said that, the x86 architecture is probably
  17974. +the most important for speed due to number of boxes and since
  17975. +it appears to be the worst architecture to to get
  17976. +good C compilers for. So due to this, I have lowered myself to do
  17977. +assembler for the inner DES routines in libdes :-).
  17978. +
  17979. +The file to implement in assembler is des_enc.c. Replace the following
  17980. +4 functions
  17981. +des_encrypt(DES_LONG data[2],des_key_schedule ks, int encrypt);
  17982. +des_encrypt2(DES_LONG data[2],des_key_schedule ks, int encrypt);
  17983. +des_encrypt3(DES_LONG data[2],des_key_schedule ks1,ks2,ks3);
  17984. +des_decrypt3(DES_LONG data[2],des_key_schedule ks1,ks2,ks3);
  17985. +
  17986. +They encrypt/decrypt the 64 bits held in 'data' using
  17987. +the 'ks' key schedules. The only difference between the 4 functions is that
  17988. +des_encrypt2() does not perform IP() or FP() on the data (this is an
  17989. +optimization for when doing triple DES and des_encrypt3() and des_decrypt3()
  17990. +perform triple des. The triple DES routines are in here because it does
  17991. +make a big difference to have them located near the des_encrypt2 function
  17992. +at link time..
  17993. +
  17994. +Now as we all know, there are lots of different operating systems running on
  17995. +x86 boxes, and unfortunately they normally try to make sure their assembler
  17996. +formating is not the same as the other peoples.
  17997. +The 4 main formats I know of are
  17998. +Microsoft Windows 95/Windows NT
  17999. +Elf Includes Linux and FreeBSD(?).
  18000. +a.out The older Linux.
  18001. +Solaris Same as Elf but different comments :-(.
  18002. +
  18003. +Now I was not overly keen to write 4 different copies of the same code,
  18004. +so I wrote a few perl routines to output the correct assembler, given
  18005. +a target assembler type. This code is ugly and is just a hack.
  18006. +The libraries are x86unix.pl and x86ms.pl.
  18007. +des586.pl, des686.pl and des-som[23].pl are the programs to actually
  18008. +generate the assembler.
  18009. +
  18010. +So to generate elf assembler
  18011. +perl des-som3.pl elf >dx86-elf.s
  18012. +For Windows 95/NT
  18013. +perl des-som2.pl win32 >win32.asm
  18014. +
  18015. +[ update 4 Jan 1996 ]
  18016. +I have added another way to do things.
  18017. +perl des-som3.pl cpp >dx86-cpp.s
  18018. +generates a file that will be included by dx86unix.cpp when it is compiled.
  18019. +To build for elf, a.out, solaris, bsdi etc,
  18020. +cc -E -DELF asm/dx86unix.cpp | as -o asm/dx86-elf.o
  18021. +cc -E -DSOL asm/dx86unix.cpp | as -o asm/dx86-sol.o
  18022. +cc -E -DOUT asm/dx86unix.cpp | as -o asm/dx86-out.o
  18023. +cc -E -DBSDI asm/dx86unix.cpp | as -o asm/dx86bsdi.o
  18024. +This was done to cut down the number of files in the distribution.
  18025. +
  18026. +Now the ugly part. I acquired my copy of Intels
  18027. +"Optimization's For Intel's 32-Bit Processors" and found a few interesting
  18028. +things. First, the aim of the exersize is to 'extract' one byte at a time
  18029. +from a word and do an array lookup. This involves getting the byte from
  18030. +the 4 locations in the word and moving it to a new word and doing the lookup.
  18031. +The most obvious way to do this is
  18032. +xor eax, eax # clear word
  18033. +movb al, cl # get low byte
  18034. +xor edi DWORD PTR 0x100+des_SP[eax] # xor in word
  18035. +movb al, ch # get next byte
  18036. +xor edi DWORD PTR 0x300+des_SP[eax] # xor in word
  18037. +shr ecx 16
  18038. +which seems ok. For the pentium, this system appears to be the best.
  18039. +One has to do instruction interleaving to keep both functional units
  18040. +operating, but it is basically very efficient.
  18041. +
  18042. +Now the crunch. When a full register is used after a partial write, eg.
  18043. +mov al, cl
  18044. +xor edi, DWORD PTR 0x100+des_SP[eax]
  18045. +386 - 1 cycle stall
  18046. +486 - 1 cycle stall
  18047. +586 - 0 cycle stall
  18048. +686 - at least 7 cycle stall (page 22 of the above mentioned document).
  18049. +
  18050. +So the technique that produces the best results on a pentium, according to
  18051. +the documentation, will produce hideous results on a pentium pro.
  18052. +
  18053. +To get around this, des686.pl will generate code that is not as fast on
  18054. +a pentium, should be very good on a pentium pro.
  18055. +mov eax, ecx # copy word
  18056. +shr ecx, 8 # line up next byte
  18057. +and eax, 0fch # mask byte
  18058. +xor edi DWORD PTR 0x100+des_SP[eax] # xor in array lookup
  18059. +mov eax, ecx # get word
  18060. +shr ecx 8 # line up next byte
  18061. +and eax, 0fch # mask byte
  18062. +xor edi DWORD PTR 0x300+des_SP[eax] # xor in array lookup
  18063. +
  18064. +Due to the execution units in the pentium, this actually works quite well.
  18065. +For a pentium pro it should be very good. This is the type of output
  18066. +Visual C++ generates.
  18067. +
  18068. +There is a third option. instead of using
  18069. +mov al, ch
  18070. +which is bad on the pentium pro, one may be able to use
  18071. +movzx eax, ch
  18072. +which may not incur the partial write penalty. On the pentium,
  18073. +this instruction takes 4 cycles so is not worth using but on the
  18074. +pentium pro it appears it may be worth while. I need access to one to
  18075. +experiment :-).
  18076. +
  18077. +eric (20 Oct 1996)
  18078. +
  18079. +22 Nov 1996 - I have asked people to run the 2 different version on pentium
  18080. +pros and it appears that the intel documentation is wrong. The
  18081. +mov al,bh is still faster on a pentium pro, so just use the des586.pl
  18082. +install des686.pl
  18083. +
  18084. +3 Dec 1996 - I added des_encrypt3/des_decrypt3 because I have moved these
  18085. +functions into des_enc.c because it does make a massive performance
  18086. +difference on some boxes to have the functions code located close to
  18087. +the des_encrypt2() function.
  18088. +
  18089. +9 Jan 1997 - des-som2.pl is now the correct perl script to use for
  18090. +pentiums. It contains an inner loop from
  18091. +Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk> which does raw ecb DES calls at
  18092. +273,000 per second. He had a previous version at 250,000 and the best
  18093. +I was able to get was 203,000. The content has not changed, this is all
  18094. +due to instruction sequencing (and actual instructions choice) which is able
  18095. +to keep both functional units of the pentium going.
  18096. +We may have lost the ugly register usage restrictions when x86 went 32 bit
  18097. +but for the pentium it has been replaced by evil instruction ordering tricks.
  18098. +
  18099. +13 Jan 1997 - des-som3.pl, more optimizations from Svend Olaf.
  18100. +raw DES at 281,000 per second on a pentium 100.
  18101. +
  18102. --- /dev/null Tue Mar 11 13:02:56 2003
  18103. +++ linux/net/ipsec/des/cbc_enc.c Mon Feb 9 13:51:03 2004
  18104. @@ -0,0 +1,135 @@
  18105. +/* crypto/des/cbc_enc.c */
  18106. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  18107. + * All rights reserved.
  18108. + *
  18109. + * This package is an SSL implementation written
  18110. + * by Eric Young (eay@cryptsoft.com).
  18111. + * The implementation was written so as to conform with Netscapes SSL.
  18112. + *
  18113. + * This library is free for commercial and non-commercial use as long as
  18114. + * the following conditions are aheared to. The following conditions
  18115. + * apply to all code found in this distribution, be it the RC4, RSA,
  18116. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  18117. + * included with this distribution is covered by the same copyright terms
  18118. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  18119. + *
  18120. + * Copyright remains Eric Young's, and as such any Copyright notices in
  18121. + * the code are not to be removed.
  18122. + * If this package is used in a product, Eric Young should be given attribution
  18123. + * as the author of the parts of the library used.
  18124. + * This can be in the form of a textual message at program startup or
  18125. + * in documentation (online or textual) provided with the package.
  18126. + *
  18127. + * Redistribution and use in source and binary forms, with or without
  18128. + * modification, are permitted provided that the following conditions
  18129. + * are met:
  18130. + * 1. Redistributions of source code must retain the copyright
  18131. + * notice, this list of conditions and the following disclaimer.
  18132. + * 2. Redistributions in binary form must reproduce the above copyright
  18133. + * notice, this list of conditions and the following disclaimer in the
  18134. + * documentation and/or other materials provided with the distribution.
  18135. + * 3. All advertising materials mentioning features or use of this software
  18136. + * must display the following acknowledgement:
  18137. + * "This product includes cryptographic software written by
  18138. + * Eric Young (eay@cryptsoft.com)"
  18139. + * The word 'cryptographic' can be left out if the rouines from the library
  18140. + * being used are not cryptographic related :-).
  18141. + * 4. If you include any Windows specific code (or a derivative thereof) from
  18142. + * the apps directory (application code) you must include an acknowledgement:
  18143. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  18144. + *
  18145. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  18146. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18147. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18148. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18149. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18150. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  18151. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  18152. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  18153. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  18154. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  18155. + * SUCH DAMAGE.
  18156. + *
  18157. + * The licence and distribution terms for any publically available version or
  18158. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  18159. + * copied and put under another distribution licence
  18160. + * [including the GNU Public Licence.]
  18161. + */
  18162. +
  18163. +#include "des/des_locl.h"
  18164. +
  18165. +void des_cbc_encrypt(input, output, length, schedule, ivec, enc)
  18166. +des_cblock (*input);
  18167. +des_cblock (*output);
  18168. +long length;
  18169. +des_key_schedule schedule;
  18170. +des_cblock (*ivec);
  18171. +int enc;
  18172. + {
  18173. + register DES_LONG tin0,tin1;
  18174. + register DES_LONG tout0,tout1,xor0,xor1;
  18175. + register unsigned char *in,*out;
  18176. + register long l=length;
  18177. + DES_LONG tin[2];
  18178. + unsigned char *iv;
  18179. +
  18180. + in=(unsigned char *)input;
  18181. + out=(unsigned char *)output;
  18182. + iv=(unsigned char *)ivec;
  18183. +
  18184. + if (enc)
  18185. + {
  18186. + c2l(iv,tout0);
  18187. + c2l(iv,tout1);
  18188. + for (l-=8; l>=0; l-=8)
  18189. + {
  18190. + c2l(in,tin0);
  18191. + c2l(in,tin1);
  18192. + tin0^=tout0; tin[0]=tin0;
  18193. + tin1^=tout1; tin[1]=tin1;
  18194. + des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
  18195. + tout0=tin[0]; l2c(tout0,out);
  18196. + tout1=tin[1]; l2c(tout1,out);
  18197. + }
  18198. + if (l != -8)
  18199. + {
  18200. + c2ln(in,tin0,tin1,l+8);
  18201. + tin0^=tout0; tin[0]=tin0;
  18202. + tin1^=tout1; tin[1]=tin1;
  18203. + des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
  18204. + tout0=tin[0]; l2c(tout0,out);
  18205. + tout1=tin[1]; l2c(tout1,out);
  18206. + }
  18207. + }
  18208. + else
  18209. + {
  18210. + c2l(iv,xor0);
  18211. + c2l(iv,xor1);
  18212. + for (l-=8; l>=0; l-=8)
  18213. + {
  18214. + c2l(in,tin0); tin[0]=tin0;
  18215. + c2l(in,tin1); tin[1]=tin1;
  18216. + des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
  18217. + tout0=tin[0]^xor0;
  18218. + tout1=tin[1]^xor1;
  18219. + l2c(tout0,out);
  18220. + l2c(tout1,out);
  18221. + xor0=tin0;
  18222. + xor1=tin1;
  18223. + }
  18224. + if (l != -8)
  18225. + {
  18226. + c2l(in,tin0); tin[0]=tin0;
  18227. + c2l(in,tin1); tin[1]=tin1;
  18228. + des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
  18229. + tout0=tin[0]^xor0;
  18230. + tout1=tin[1]^xor1;
  18231. + l2cn(tout0,tout1,out,l+8);
  18232. + /* xor0=tin0;
  18233. + xor1=tin1; */
  18234. + }
  18235. + }
  18236. + tin0=tin1=tout0=tout1=xor0=xor1=0;
  18237. + tin[0]=tin[1]=0;
  18238. + }
  18239. +
  18240. --- /dev/null Tue Mar 11 13:02:56 2003
  18241. +++ linux/net/ipsec/des/des.doc Mon Feb 9 13:51:03 2004
  18242. @@ -0,0 +1,505 @@
  18243. +The DES library.
  18244. +
  18245. +Please note that this library was originally written to operate with
  18246. +eBones, a version of Kerberos that had had encryption removed when it left
  18247. +the USA and then put back in. As such there are some routines that I will
  18248. +advise not using but they are still in the library for historical reasons.
  18249. +For all calls that have an 'input' and 'output' variables, they can be the
  18250. +same.
  18251. +
  18252. +This library requires the inclusion of 'des.h'.
  18253. +
  18254. +All of the encryption functions take what is called a des_key_schedule as an
  18255. +argument. A des_key_schedule is an expanded form of the des key.
  18256. +A des_key is 8 bytes of odd parity, the type used to hold the key is a
  18257. +des_cblock. A des_cblock is an array of 8 bytes, often in this library
  18258. +description I will refer to input bytes when the function specifies
  18259. +des_cblock's as input or output, this just means that the variable should
  18260. +be a multiple of 8 bytes.
  18261. +
  18262. +The define DES_ENCRYPT is passed to specify encryption, DES_DECRYPT to
  18263. +specify decryption. The functions and global variable are as follows:
  18264. +
  18265. +int des_check_key;
  18266. + DES keys are supposed to be odd parity. If this variable is set to
  18267. + a non-zero value, des_set_key() will check that the key has odd
  18268. + parity and is not one of the known weak DES keys. By default this
  18269. + variable is turned off;
  18270. +
  18271. +void des_set_odd_parity(
  18272. +des_cblock *key );
  18273. + This function takes a DES key (8 bytes) and sets the parity to odd.
  18274. +
  18275. +int des_is_weak_key(
  18276. +des_cblock *key );
  18277. + This function returns a non-zero value if the DES key passed is a
  18278. + weak, DES key. If it is a weak key, don't use it, try a different
  18279. + one. If you are using 'random' keys, the chances of hitting a weak
  18280. + key are 1/2^52 so it is probably not worth checking for them.
  18281. +
  18282. +int des_set_key(
  18283. +des_cblock *key,
  18284. +des_key_schedule schedule);
  18285. + Des_set_key converts an 8 byte DES key into a des_key_schedule.
  18286. + A des_key_schedule is an expanded form of the key which is used to
  18287. + perform actual encryption. It can be regenerated from the DES key
  18288. + so it only needs to be kept when encryption or decryption is about
  18289. + to occur. Don't save or pass around des_key_schedule's since they
  18290. + are CPU architecture dependent, DES keys are not. If des_check_key
  18291. + is non zero, zero is returned if the key has the wrong parity or
  18292. + the key is a weak key, else 1 is returned.
  18293. +
  18294. +int des_key_sched(
  18295. +des_cblock *key,
  18296. +des_key_schedule schedule);
  18297. + An alternative name for des_set_key().
  18298. +
  18299. +int des_rw_mode; /* defaults to DES_PCBC_MODE */
  18300. + This flag holds either DES_CBC_MODE or DES_PCBC_MODE (default).
  18301. + This specifies the function to use in the enc_read() and enc_write()
  18302. + functions.
  18303. +
  18304. +void des_encrypt(
  18305. +unsigned long *data,
  18306. +des_key_schedule ks,
  18307. +int enc);
  18308. + This is the DES encryption function that gets called by just about
  18309. + every other DES routine in the library. You should not use this
  18310. + function except to implement 'modes' of DES. I say this because the
  18311. + functions that call this routine do the conversion from 'char *' to
  18312. + long, and this needs to be done to make sure 'non-aligned' memory
  18313. + access do not occur. The characters are loaded 'little endian',
  18314. + have a look at my source code for more details on how I use this
  18315. + function.
  18316. + Data is a pointer to 2 unsigned long's and ks is the
  18317. + des_key_schedule to use. enc, is non zero specifies encryption,
  18318. + zero if decryption.
  18319. +
  18320. +void des_encrypt2(
  18321. +unsigned long *data,
  18322. +des_key_schedule ks,
  18323. +int enc);
  18324. + This functions is the same as des_encrypt() except that the DES
  18325. + initial permutation (IP) and final permutation (FP) have been left
  18326. + out. As for des_encrypt(), you should not use this function.
  18327. + It is used by the routines in my library that implement triple DES.
  18328. + IP() des_encrypt2() des_encrypt2() des_encrypt2() FP() is the same
  18329. + as des_encrypt() des_encrypt() des_encrypt() except faster :-).
  18330. +
  18331. +void des_ecb_encrypt(
  18332. +des_cblock *input,
  18333. +des_cblock *output,
  18334. +des_key_schedule ks,
  18335. +int enc);
  18336. + This is the basic Electronic Code Book form of DES, the most basic
  18337. + form. Input is encrypted into output using the key represented by
  18338. + ks. If enc is non zero (DES_ENCRYPT), encryption occurs, otherwise
  18339. + decryption occurs. Input is 8 bytes long and output is 8 bytes.
  18340. + (the des_cblock structure is 8 chars).
  18341. +
  18342. +void des_ecb3_encrypt(
  18343. +des_cblock *input,
  18344. +des_cblock *output,
  18345. +des_key_schedule ks1,
  18346. +des_key_schedule ks2,
  18347. +des_key_schedule ks3,
  18348. +int enc);
  18349. + This is the 3 key EDE mode of ECB DES. What this means is that
  18350. + the 8 bytes of input is encrypted with ks1, decrypted with ks2 and
  18351. + then encrypted again with ks3, before being put into output;
  18352. + C=E(ks3,D(ks2,E(ks1,M))). There is a macro, des_ecb2_encrypt()
  18353. + that only takes 2 des_key_schedules that implements,
  18354. + C=E(ks1,D(ks2,E(ks1,M))) in that the final encrypt is done with ks1.
  18355. +
  18356. +void des_cbc_encrypt(
  18357. +des_cblock *input,
  18358. +des_cblock *output,
  18359. +long length,
  18360. +des_key_schedule ks,
  18361. +des_cblock *ivec,
  18362. +int enc);
  18363. + This routine implements DES in Cipher Block Chaining mode.
  18364. + Input, which should be a multiple of 8 bytes is encrypted
  18365. + (or decrypted) to output which will also be a multiple of 8 bytes.
  18366. + The number of bytes is in length (and from what I've said above,
  18367. + should be a multiple of 8). If length is not a multiple of 8, I'm
  18368. + not being held responsible :-). ivec is the initialisation vector.
  18369. + This function does not modify this variable. To correctly implement
  18370. + cbc mode, you need to do one of 2 things; copy the last 8 bytes of
  18371. + cipher text for use as the next ivec in your application,
  18372. + or use des_ncbc_encrypt().
  18373. + Only this routine has this problem with updating the ivec, all
  18374. + other routines that are implementing cbc mode update ivec.
  18375. +
  18376. +void des_ncbc_encrypt(
  18377. +des_cblock *input,
  18378. +des_cblock *output,
  18379. +long length,
  18380. +des_key_schedule sk,
  18381. +des_cblock *ivec,
  18382. +int enc);
  18383. + For historical reasons, des_cbc_encrypt() did not update the
  18384. + ivec with the value requires so that subsequent calls to
  18385. + des_cbc_encrypt() would 'chain'. This was needed so that the same
  18386. + 'length' values would not need to be used when decrypting.
  18387. + des_ncbc_encrypt() does the right thing. It is the same as
  18388. + des_cbc_encrypt accept that ivec is updates with the correct value
  18389. + to pass in subsequent calls to des_ncbc_encrypt(). I advise using
  18390. + des_ncbc_encrypt() instead of des_cbc_encrypt();
  18391. +
  18392. +void des_xcbc_encrypt(
  18393. +des_cblock *input,
  18394. +des_cblock *output,
  18395. +long length,
  18396. +des_key_schedule sk,
  18397. +des_cblock *ivec,
  18398. +des_cblock *inw,
  18399. +des_cblock *outw,
  18400. +int enc);
  18401. + This is RSA's DESX mode of DES. It uses inw and outw to
  18402. + 'whiten' the encryption. inw and outw are secret (unlike the iv)
  18403. + and are as such, part of the key. So the key is sort of 24 bytes.
  18404. + This is much better than cbc des.
  18405. +
  18406. +void des_3cbc_encrypt(
  18407. +des_cblock *input,
  18408. +des_cblock *output,
  18409. +long length,
  18410. +des_key_schedule sk1,
  18411. +des_key_schedule sk2,
  18412. +des_cblock *ivec1,
  18413. +des_cblock *ivec2,
  18414. +int enc);
  18415. + This function is flawed, do not use it. I have left it in the
  18416. + library because it is used in my des(1) program and will function
  18417. + correctly when used by des(1). If I removed the function, people
  18418. + could end up unable to decrypt files.
  18419. + This routine implements outer triple cbc encryption using 2 ks and
  18420. + 2 ivec's. Use des_ede2_cbc_encrypt() instead.
  18421. +
  18422. +void des_ede3_cbc_encrypt(
  18423. +des_cblock *input,
  18424. +des_cblock *output,
  18425. +long length,
  18426. +des_key_schedule ks1,
  18427. +des_key_schedule ks2,
  18428. +des_key_schedule ks3,
  18429. +des_cblock *ivec,
  18430. +int enc);
  18431. + This function implements inner triple CBC DES encryption with 3
  18432. + keys. What this means is that each 'DES' operation
  18433. + inside the cbc mode is really an C=E(ks3,D(ks2,E(ks1,M))).
  18434. + Again, this is cbc mode so an ivec is requires.
  18435. + This mode is used by SSL.
  18436. + There is also a des_ede2_cbc_encrypt() that only uses 2
  18437. + des_key_schedule's, the first being reused for the final
  18438. + encryption. C=E(ks1,D(ks2,E(ks1,M))). This form of triple DES
  18439. + is used by the RSAref library.
  18440. +
  18441. +void des_pcbc_encrypt(
  18442. +des_cblock *input,
  18443. +des_cblock *output,
  18444. +long length,
  18445. +des_key_schedule ks,
  18446. +des_cblock *ivec,
  18447. +int enc);
  18448. + This is Propagating Cipher Block Chaining mode of DES. It is used
  18449. + by Kerberos v4. It's parameters are the same as des_ncbc_encrypt().
  18450. +
  18451. +void des_cfb_encrypt(
  18452. +unsigned char *in,
  18453. +unsigned char *out,
  18454. +int numbits,
  18455. +long length,
  18456. +des_key_schedule ks,
  18457. +des_cblock *ivec,
  18458. +int enc);
  18459. + Cipher Feedback Back mode of DES. This implementation 'feeds back'
  18460. + in numbit blocks. The input (and output) is in multiples of numbits
  18461. + bits. numbits should to be a multiple of 8 bits. Length is the
  18462. + number of bytes input. If numbits is not a multiple of 8 bits,
  18463. + the extra bits in the bytes will be considered padding. So if
  18464. + numbits is 12, for each 2 input bytes, the 4 high bits of the
  18465. + second byte will be ignored. So to encode 72 bits when using
  18466. + a numbits of 12 take 12 bytes. To encode 72 bits when using
  18467. + numbits of 9 will take 16 bytes. To encode 80 bits when using
  18468. + numbits of 16 will take 10 bytes. etc, etc. This padding will
  18469. + apply to both input and output.
  18470. +
  18471. +
  18472. +void des_cfb64_encrypt(
  18473. +unsigned char *in,
  18474. +unsigned char *out,
  18475. +long length,
  18476. +des_key_schedule ks,
  18477. +des_cblock *ivec,
  18478. +int *num,
  18479. +int enc);
  18480. + This is one of the more useful functions in this DES library, it
  18481. + implements CFB mode of DES with 64bit feedback. Why is this
  18482. + useful you ask? Because this routine will allow you to encrypt an
  18483. + arbitrary number of bytes, no 8 byte padding. Each call to this
  18484. + routine will encrypt the input bytes to output and then update ivec
  18485. + and num. num contains 'how far' we are though ivec. If this does
  18486. + not make much sense, read more about cfb mode of DES :-).
  18487. +
  18488. +void des_ede3_cfb64_encrypt(
  18489. +unsigned char *in,
  18490. +unsigned char *out,
  18491. +long length,
  18492. +des_key_schedule ks1,
  18493. +des_key_schedule ks2,
  18494. +des_key_schedule ks3,
  18495. +des_cblock *ivec,
  18496. +int *num,
  18497. +int enc);
  18498. + Same as des_cfb64_encrypt() accept that the DES operation is
  18499. + triple DES. As usual, there is a macro for
  18500. + des_ede2_cfb64_encrypt() which reuses ks1.
  18501. +
  18502. +void des_ofb_encrypt(
  18503. +unsigned char *in,
  18504. +unsigned char *out,
  18505. +int numbits,
  18506. +long length,
  18507. +des_key_schedule ks,
  18508. +des_cblock *ivec);
  18509. + This is a implementation of Output Feed Back mode of DES. It is
  18510. + the same as des_cfb_encrypt() in that numbits is the size of the
  18511. + units dealt with during input and output (in bits).
  18512. +
  18513. +void des_ofb64_encrypt(
  18514. +unsigned char *in,
  18515. +unsigned char *out,
  18516. +long length,
  18517. +des_key_schedule ks,
  18518. +des_cblock *ivec,
  18519. +int *num);
  18520. + The same as des_cfb64_encrypt() except that it is Output Feed Back
  18521. + mode.
  18522. +
  18523. +void des_ede3_ofb64_encrypt(
  18524. +unsigned char *in,
  18525. +unsigned char *out,
  18526. +long length,
  18527. +des_key_schedule ks1,
  18528. +des_key_schedule ks2,
  18529. +des_key_schedule ks3,
  18530. +des_cblock *ivec,
  18531. +int *num);
  18532. + Same as des_ofb64_encrypt() accept that the DES operation is
  18533. + triple DES. As usual, there is a macro for
  18534. + des_ede2_ofb64_encrypt() which reuses ks1.
  18535. +
  18536. +int des_read_pw_string(
  18537. +char *buf,
  18538. +int length,
  18539. +char *prompt,
  18540. +int verify);
  18541. + This routine is used to get a password from the terminal with echo
  18542. + turned off. Buf is where the string will end up and length is the
  18543. + size of buf. Prompt is a string presented to the 'user' and if
  18544. + verify is set, the key is asked for twice and unless the 2 copies
  18545. + match, an error is returned. A return code of -1 indicates a
  18546. + system error, 1 failure due to use interaction, and 0 is success.
  18547. +
  18548. +unsigned long des_cbc_cksum(
  18549. +des_cblock *input,
  18550. +des_cblock *output,
  18551. +long length,
  18552. +des_key_schedule ks,
  18553. +des_cblock *ivec);
  18554. + This function produces an 8 byte checksum from input that it puts in
  18555. + output and returns the last 4 bytes as a long. The checksum is
  18556. + generated via cbc mode of DES in which only the last 8 byes are
  18557. + kept. I would recommend not using this function but instead using
  18558. + the EVP_Digest routines, or at least using MD5 or SHA. This
  18559. + function is used by Kerberos v4 so that is why it stays in the
  18560. + library.
  18561. +
  18562. +char *des_fcrypt(
  18563. +const char *buf,
  18564. +const char *salt
  18565. +char *ret);
  18566. + This is my fast version of the unix crypt(3) function. This version
  18567. + takes only a small amount of space relative to other fast
  18568. + crypt() implementations. This is different to the normal crypt
  18569. + in that the third parameter is the buffer that the return value
  18570. + is written into. It needs to be at least 14 bytes long. This
  18571. + function is thread safe, unlike the normal crypt.
  18572. +
  18573. +char *crypt(
  18574. +const char *buf,
  18575. +const char *salt);
  18576. + This function calls des_fcrypt() with a static array passed as the
  18577. + third parameter. This emulates the normal non-thread safe semantics
  18578. + of crypt(3).
  18579. +
  18580. +void des_string_to_key(
  18581. +char *str,
  18582. +des_cblock *key);
  18583. + This function takes str and converts it into a DES key. I would
  18584. + recommend using MD5 instead and use the first 8 bytes of output.
  18585. + When I wrote the first version of these routines back in 1990, MD5
  18586. + did not exist but I feel these routines are still sound. This
  18587. + routines is compatible with the one in MIT's libdes.
  18588. +
  18589. +void des_string_to_2keys(
  18590. +char *str,
  18591. +des_cblock *key1,
  18592. +des_cblock *key2);
  18593. + This function takes str and converts it into 2 DES keys.
  18594. + I would recommend using MD5 and using the 16 bytes as the 2 keys.
  18595. + I have nothing against these 2 'string_to_key' routines, it's just
  18596. + that if you say that your encryption key is generated by using the
  18597. + 16 bytes of an MD5 hash, every-one knows how you generated your
  18598. + keys.
  18599. +
  18600. +int des_read_password(
  18601. +des_cblock *key,
  18602. +char *prompt,
  18603. +int verify);
  18604. + This routine combines des_read_pw_string() with des_string_to_key().
  18605. +
  18606. +int des_read_2passwords(
  18607. +des_cblock *key1,
  18608. +des_cblock *key2,
  18609. +char *prompt,
  18610. +int verify);
  18611. + This routine combines des_read_pw_string() with des_string_to_2key().
  18612. +
  18613. +void des_random_seed(
  18614. +des_cblock key);
  18615. + This routine sets a starting point for des_random_key().
  18616. +
  18617. +void des_random_key(
  18618. +des_cblock ret);
  18619. + This function return a random key. Make sure to 'seed' the random
  18620. + number generator (with des_random_seed()) before using this function.
  18621. + I personally now use a MD5 based random number system.
  18622. +
  18623. +int des_enc_read(
  18624. +int fd,
  18625. +char *buf,
  18626. +int len,
  18627. +des_key_schedule ks,
  18628. +des_cblock *iv);
  18629. + This function will write to a file descriptor the encrypted data
  18630. + from buf. This data will be preceded by a 4 byte 'byte count' and
  18631. + will be padded out to 8 bytes. The encryption is either CBC of
  18632. + PCBC depending on the value of des_rw_mode. If it is DES_PCBC_MODE,
  18633. + pcbc is used, if DES_CBC_MODE, cbc is used. The default is to use
  18634. + DES_PCBC_MODE.
  18635. +
  18636. +int des_enc_write(
  18637. +int fd,
  18638. +char *buf,
  18639. +int len,
  18640. +des_key_schedule ks,
  18641. +des_cblock *iv);
  18642. + This routines read stuff written by des_enc_read() and decrypts it.
  18643. + I have used these routines quite a lot but I don't believe they are
  18644. + suitable for non-blocking io. If you are after a full
  18645. + authentication/encryption over networks, have a look at SSL instead.
  18646. +
  18647. +unsigned long des_quad_cksum(
  18648. +des_cblock *input,
  18649. +des_cblock *output,
  18650. +long length,
  18651. +int out_count,
  18652. +des_cblock *seed);
  18653. + This is a function from Kerberos v4 that is not anything to do with
  18654. + DES but was needed. It is a cksum that is quicker to generate than
  18655. + des_cbc_cksum(); I personally would use MD5 routines now.
  18656. +=====
  18657. +Modes of DES
  18658. +Quite a bit of the following information has been taken from
  18659. + AS 2805.5.2
  18660. + Australian Standard
  18661. + Electronic funds transfer - Requirements for interfaces,
  18662. + Part 5.2: Modes of operation for an n-bit block cipher algorithm
  18663. + Appendix A
  18664. +
  18665. +There are several different modes in which DES can be used, they are
  18666. +as follows.
  18667. +
  18668. +Electronic Codebook Mode (ECB) (des_ecb_encrypt())
  18669. +- 64 bits are enciphered at a time.
  18670. +- The order of the blocks can be rearranged without detection.
  18671. +- The same plaintext block always produces the same ciphertext block
  18672. + (for the same key) making it vulnerable to a 'dictionary attack'.
  18673. +- An error will only affect one ciphertext block.
  18674. +
  18675. +Cipher Block Chaining Mode (CBC) (des_cbc_encrypt())
  18676. +- a multiple of 64 bits are enciphered at a time.
  18677. +- The CBC mode produces the same ciphertext whenever the same
  18678. + plaintext is encrypted using the same key and starting variable.
  18679. +- The chaining operation makes the ciphertext blocks dependent on the
  18680. + current and all preceding plaintext blocks and therefore blocks can not
  18681. + be rearranged.
  18682. +- The use of different starting variables prevents the same plaintext
  18683. + enciphering to the same ciphertext.
  18684. +- An error will affect the current and the following ciphertext blocks.
  18685. +
  18686. +Cipher Feedback Mode (CFB) (des_cfb_encrypt())
  18687. +- a number of bits (j) <= 64 are enciphered at a time.
  18688. +- The CFB mode produces the same ciphertext whenever the same
  18689. + plaintext is encrypted using the same key and starting variable.
  18690. +- The chaining operation makes the ciphertext variables dependent on the
  18691. + current and all preceding variables and therefore j-bit variables are
  18692. + chained together and can not be rearranged.
  18693. +- The use of different starting variables prevents the same plaintext
  18694. + enciphering to the same ciphertext.
  18695. +- The strength of the CFB mode depends on the size of k (maximal if
  18696. + j == k). In my implementation this is always the case.
  18697. +- Selection of a small value for j will require more cycles through
  18698. + the encipherment algorithm per unit of plaintext and thus cause
  18699. + greater processing overheads.
  18700. +- Only multiples of j bits can be enciphered.
  18701. +- An error will affect the current and the following ciphertext variables.
  18702. +
  18703. +Output Feedback Mode (OFB) (des_ofb_encrypt())
  18704. +- a number of bits (j) <= 64 are enciphered at a time.
  18705. +- The OFB mode produces the same ciphertext whenever the same
  18706. + plaintext enciphered using the same key and starting variable. More
  18707. + over, in the OFB mode the same key stream is produced when the same
  18708. + key and start variable are used. Consequently, for security reasons
  18709. + a specific start variable should be used only once for a given key.
  18710. +- The absence of chaining makes the OFB more vulnerable to specific attacks.
  18711. +- The use of different start variables values prevents the same
  18712. + plaintext enciphering to the same ciphertext, by producing different
  18713. + key streams.
  18714. +- Selection of a small value for j will require more cycles through
  18715. + the encipherment algorithm per unit of plaintext and thus cause
  18716. + greater processing overheads.
  18717. +- Only multiples of j bits can be enciphered.
  18718. +- OFB mode of operation does not extend ciphertext errors in the
  18719. + resultant plaintext output. Every bit error in the ciphertext causes
  18720. + only one bit to be in error in the deciphered plaintext.
  18721. +- OFB mode is not self-synchronising. If the two operation of
  18722. + encipherment and decipherment get out of synchronism, the system needs
  18723. + to be re-initialised.
  18724. +- Each re-initialisation should use a value of the start variable
  18725. + different from the start variable values used before with the same
  18726. + key. The reason for this is that an identical bit stream would be
  18727. + produced each time from the same parameters. This would be
  18728. + susceptible to a ' known plaintext' attack.
  18729. +
  18730. +Triple ECB Mode (des_ecb3_encrypt())
  18731. +- Encrypt with key1, decrypt with key2 and encrypt with key3 again.
  18732. +- As for ECB encryption but increases the key length to 168 bits.
  18733. + There are theoretic attacks that can be used that make the effective
  18734. + key length 112 bits, but this attack also requires 2^56 blocks of
  18735. + memory, not very likely, even for the NSA.
  18736. +- If both keys are the same it is equivalent to encrypting once with
  18737. + just one key.
  18738. +- If the first and last key are the same, the key length is 112 bits.
  18739. + There are attacks that could reduce the key space to 55 bit's but it
  18740. + requires 2^56 blocks of memory.
  18741. +- If all 3 keys are the same, this is effectively the same as normal
  18742. + ecb mode.
  18743. +
  18744. +Triple CBC Mode (des_ede3_cbc_encrypt())
  18745. +- Encrypt with key1, decrypt with key2 and then encrypt with key3.
  18746. +- As for CBC encryption but increases the key length to 168 bits with
  18747. + the same restrictions as for triple ecb mode.
  18748. --- /dev/null Tue Mar 11 13:02:56 2003
  18749. +++ linux/net/ipsec/des/des_enc.c Mon Feb 9 13:51:03 2004
  18750. @@ -0,0 +1,502 @@
  18751. +/* crypto/des/des_enc.c */
  18752. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  18753. + * All rights reserved.
  18754. + *
  18755. + * This package is an SSL implementation written
  18756. + * by Eric Young (eay@cryptsoft.com).
  18757. + * The implementation was written so as to conform with Netscapes SSL.
  18758. + *
  18759. + * This library is free for commercial and non-commercial use as long as
  18760. + * the following conditions are aheared to. The following conditions
  18761. + * apply to all code found in this distribution, be it the RC4, RSA,
  18762. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  18763. + * included with this distribution is covered by the same copyright terms
  18764. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  18765. + *
  18766. + * Copyright remains Eric Young's, and as such any Copyright notices in
  18767. + * the code are not to be removed.
  18768. + * If this package is used in a product, Eric Young should be given attribution
  18769. + * as the author of the parts of the library used.
  18770. + * This can be in the form of a textual message at program startup or
  18771. + * in documentation (online or textual) provided with the package.
  18772. + *
  18773. + * Redistribution and use in source and binary forms, with or without
  18774. + * modification, are permitted provided that the following conditions
  18775. + * are met:
  18776. + * 1. Redistributions of source code must retain the copyright
  18777. + * notice, this list of conditions and the following disclaimer.
  18778. + * 2. Redistributions in binary form must reproduce the above copyright
  18779. + * notice, this list of conditions and the following disclaimer in the
  18780. + * documentation and/or other materials provided with the distribution.
  18781. + * 3. All advertising materials mentioning features or use of this software
  18782. + * must display the following acknowledgement:
  18783. + * "This product includes cryptographic software written by
  18784. + * Eric Young (eay@cryptsoft.com)"
  18785. + * The word 'cryptographic' can be left out if the rouines from the library
  18786. + * being used are not cryptographic related :-).
  18787. + * 4. If you include any Windows specific code (or a derivative thereof) from
  18788. + * the apps directory (application code) you must include an acknowledgement:
  18789. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  18790. + *
  18791. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  18792. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18793. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18794. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18795. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18796. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  18797. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  18798. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  18799. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  18800. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  18801. + * SUCH DAMAGE.
  18802. + *
  18803. + * The licence and distribution terms for any publically available version or
  18804. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  18805. + * copied and put under another distribution licence
  18806. + * [including the GNU Public Licence.]
  18807. + */
  18808. +
  18809. +#include "des/des_locl.h"
  18810. +
  18811. +void des_encrypt(data, ks, enc)
  18812. +DES_LONG *data;
  18813. +des_key_schedule ks;
  18814. +int enc;
  18815. + {
  18816. + register DES_LONG l,r,t,u;
  18817. +#ifdef DES_PTR
  18818. + register unsigned char *des_SP=(unsigned char *)des_SPtrans;
  18819. +#endif
  18820. +#ifndef DES_UNROLL
  18821. + register int i;
  18822. +#endif
  18823. + register DES_LONG *s;
  18824. +
  18825. + r=data[0];
  18826. + l=data[1];
  18827. +
  18828. + IP(r,l);
  18829. + /* Things have been modified so that the initial rotate is
  18830. + * done outside the loop. This required the
  18831. + * des_SPtrans values in sp.h to be rotated 1 bit to the right.
  18832. + * One perl script later and things have a 5% speed up on a sparc2.
  18833. + * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
  18834. + * for pointing this out. */
  18835. + /* clear the top bits on machines with 8byte longs */
  18836. + /* shift left by 2 */
  18837. + r=ROTATE(r,29)&0xffffffffL;
  18838. + l=ROTATE(l,29)&0xffffffffL;
  18839. +
  18840. + s=(DES_LONG *)ks;
  18841. + /* I don't know if it is worth the effort of loop unrolling the
  18842. + * inner loop */
  18843. + if (enc)
  18844. + {
  18845. +#ifdef DES_UNROLL
  18846. + D_ENCRYPT(l,r, 0); /* 1 */
  18847. + D_ENCRYPT(r,l, 2); /* 2 */
  18848. + D_ENCRYPT(l,r, 4); /* 3 */
  18849. + D_ENCRYPT(r,l, 6); /* 4 */
  18850. + D_ENCRYPT(l,r, 8); /* 5 */
  18851. + D_ENCRYPT(r,l,10); /* 6 */
  18852. + D_ENCRYPT(l,r,12); /* 7 */
  18853. + D_ENCRYPT(r,l,14); /* 8 */
  18854. + D_ENCRYPT(l,r,16); /* 9 */
  18855. + D_ENCRYPT(r,l,18); /* 10 */
  18856. + D_ENCRYPT(l,r,20); /* 11 */
  18857. + D_ENCRYPT(r,l,22); /* 12 */
  18858. + D_ENCRYPT(l,r,24); /* 13 */
  18859. + D_ENCRYPT(r,l,26); /* 14 */
  18860. + D_ENCRYPT(l,r,28); /* 15 */
  18861. + D_ENCRYPT(r,l,30); /* 16 */
  18862. +#else
  18863. + for (i=0; i<32; i+=8)
  18864. + {
  18865. + D_ENCRYPT(l,r,i+0); /* 1 */
  18866. + D_ENCRYPT(r,l,i+2); /* 2 */
  18867. + D_ENCRYPT(l,r,i+4); /* 3 */
  18868. + D_ENCRYPT(r,l,i+6); /* 4 */
  18869. + }
  18870. +#endif
  18871. + }
  18872. + else
  18873. + {
  18874. +#ifdef DES_UNROLL
  18875. + D_ENCRYPT(l,r,30); /* 16 */
  18876. + D_ENCRYPT(r,l,28); /* 15 */
  18877. + D_ENCRYPT(l,r,26); /* 14 */
  18878. + D_ENCRYPT(r,l,24); /* 13 */
  18879. + D_ENCRYPT(l,r,22); /* 12 */
  18880. + D_ENCRYPT(r,l,20); /* 11 */
  18881. + D_ENCRYPT(l,r,18); /* 10 */
  18882. + D_ENCRYPT(r,l,16); /* 9 */
  18883. + D_ENCRYPT(l,r,14); /* 8 */
  18884. + D_ENCRYPT(r,l,12); /* 7 */
  18885. + D_ENCRYPT(l,r,10); /* 6 */
  18886. + D_ENCRYPT(r,l, 8); /* 5 */
  18887. + D_ENCRYPT(l,r, 6); /* 4 */
  18888. + D_ENCRYPT(r,l, 4); /* 3 */
  18889. + D_ENCRYPT(l,r, 2); /* 2 */
  18890. + D_ENCRYPT(r,l, 0); /* 1 */
  18891. +#else
  18892. + for (i=30; i>0; i-=8)
  18893. + {
  18894. + D_ENCRYPT(l,r,i-0); /* 16 */
  18895. + D_ENCRYPT(r,l,i-2); /* 15 */
  18896. + D_ENCRYPT(l,r,i-4); /* 14 */
  18897. + D_ENCRYPT(r,l,i-6); /* 13 */
  18898. + }
  18899. +#endif
  18900. + }
  18901. +
  18902. + /* rotate and clear the top bits on machines with 8byte longs */
  18903. + l=ROTATE(l,3)&0xffffffffL;
  18904. + r=ROTATE(r,3)&0xffffffffL;
  18905. +
  18906. + FP(r,l);
  18907. + data[0]=l;
  18908. + data[1]=r;
  18909. + l=r=t=u=0;
  18910. + }
  18911. +
  18912. +void des_encrypt2(data, ks, enc)
  18913. +DES_LONG *data;
  18914. +des_key_schedule ks;
  18915. +int enc;
  18916. + {
  18917. + register DES_LONG l,r,t,u;
  18918. +#ifdef DES_PTR
  18919. + register unsigned char *des_SP=(unsigned char *)des_SPtrans;
  18920. +#endif
  18921. +#ifndef DES_UNROLL
  18922. + register int i;
  18923. +#endif
  18924. + register DES_LONG *s;
  18925. +
  18926. + r=data[0];
  18927. + l=data[1];
  18928. +
  18929. + /* Things have been modified so that the initial rotate is
  18930. + * done outside the loop. This required the
  18931. + * des_SPtrans values in sp.h to be rotated 1 bit to the right.
  18932. + * One perl script later and things have a 5% speed up on a sparc2.
  18933. + * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
  18934. + * for pointing this out. */
  18935. + /* clear the top bits on machines with 8byte longs */
  18936. + r=ROTATE(r,29)&0xffffffffL;
  18937. + l=ROTATE(l,29)&0xffffffffL;
  18938. +
  18939. + s=(DES_LONG *)ks;
  18940. + /* I don't know if it is worth the effort of loop unrolling the
  18941. + * inner loop */
  18942. + if (enc)
  18943. + {
  18944. +#ifdef DES_UNROLL
  18945. + D_ENCRYPT(l,r, 0); /* 1 */
  18946. + D_ENCRYPT(r,l, 2); /* 2 */
  18947. + D_ENCRYPT(l,r, 4); /* 3 */
  18948. + D_ENCRYPT(r,l, 6); /* 4 */
  18949. + D_ENCRYPT(l,r, 8); /* 5 */
  18950. + D_ENCRYPT(r,l,10); /* 6 */
  18951. + D_ENCRYPT(l,r,12); /* 7 */
  18952. + D_ENCRYPT(r,l,14); /* 8 */
  18953. + D_ENCRYPT(l,r,16); /* 9 */
  18954. + D_ENCRYPT(r,l,18); /* 10 */
  18955. + D_ENCRYPT(l,r,20); /* 11 */
  18956. + D_ENCRYPT(r,l,22); /* 12 */
  18957. + D_ENCRYPT(l,r,24); /* 13 */
  18958. + D_ENCRYPT(r,l,26); /* 14 */
  18959. + D_ENCRYPT(l,r,28); /* 15 */
  18960. + D_ENCRYPT(r,l,30); /* 16 */
  18961. +#else
  18962. + for (i=0; i<32; i+=8)
  18963. + {
  18964. + D_ENCRYPT(l,r,i+0); /* 1 */
  18965. + D_ENCRYPT(r,l,i+2); /* 2 */
  18966. + D_ENCRYPT(l,r,i+4); /* 3 */
  18967. + D_ENCRYPT(r,l,i+6); /* 4 */
  18968. + }
  18969. +#endif
  18970. + }
  18971. + else
  18972. + {
  18973. +#ifdef DES_UNROLL
  18974. + D_ENCRYPT(l,r,30); /* 16 */
  18975. + D_ENCRYPT(r,l,28); /* 15 */
  18976. + D_ENCRYPT(l,r,26); /* 14 */
  18977. + D_ENCRYPT(r,l,24); /* 13 */
  18978. + D_ENCRYPT(l,r,22); /* 12 */
  18979. + D_ENCRYPT(r,l,20); /* 11 */
  18980. + D_ENCRYPT(l,r,18); /* 10 */
  18981. + D_ENCRYPT(r,l,16); /* 9 */
  18982. + D_ENCRYPT(l,r,14); /* 8 */
  18983. + D_ENCRYPT(r,l,12); /* 7 */
  18984. + D_ENCRYPT(l,r,10); /* 6 */
  18985. + D_ENCRYPT(r,l, 8); /* 5 */
  18986. + D_ENCRYPT(l,r, 6); /* 4 */
  18987. + D_ENCRYPT(r,l, 4); /* 3 */
  18988. + D_ENCRYPT(l,r, 2); /* 2 */
  18989. + D_ENCRYPT(r,l, 0); /* 1 */
  18990. +#else
  18991. + for (i=30; i>0; i-=8)
  18992. + {
  18993. + D_ENCRYPT(l,r,i-0); /* 16 */
  18994. + D_ENCRYPT(r,l,i-2); /* 15 */
  18995. + D_ENCRYPT(l,r,i-4); /* 14 */
  18996. + D_ENCRYPT(r,l,i-6); /* 13 */
  18997. + }
  18998. +#endif
  18999. + }
  19000. + /* rotate and clear the top bits on machines with 8byte longs */
  19001. + data[0]=ROTATE(l,3)&0xffffffffL;
  19002. + data[1]=ROTATE(r,3)&0xffffffffL;
  19003. + l=r=t=u=0;
  19004. + }
  19005. +
  19006. +void des_encrypt3(data,ks1,ks2,ks3)
  19007. +DES_LONG *data;
  19008. +des_key_schedule ks1;
  19009. +des_key_schedule ks2;
  19010. +des_key_schedule ks3;
  19011. + {
  19012. + register DES_LONG l,r;
  19013. +
  19014. + l=data[0];
  19015. + r=data[1];
  19016. + IP(l,r);
  19017. + data[0]=l;
  19018. + data[1]=r;
  19019. + des_encrypt2((DES_LONG *)data,ks1,DES_ENCRYPT);
  19020. + des_encrypt2((DES_LONG *)data,ks2,DES_DECRYPT);
  19021. + des_encrypt2((DES_LONG *)data,ks3,DES_ENCRYPT);
  19022. + l=data[0];
  19023. + r=data[1];
  19024. + FP(r,l);
  19025. + data[0]=l;
  19026. + data[1]=r;
  19027. + }
  19028. +
  19029. +void des_decrypt3(data,ks1,ks2,ks3)
  19030. +DES_LONG *data;
  19031. +des_key_schedule ks1;
  19032. +des_key_schedule ks2;
  19033. +des_key_schedule ks3;
  19034. + {
  19035. + register DES_LONG l,r;
  19036. +
  19037. + l=data[0];
  19038. + r=data[1];
  19039. + IP(l,r);
  19040. + data[0]=l;
  19041. + data[1]=r;
  19042. + des_encrypt2((DES_LONG *)data,ks3,DES_DECRYPT);
  19043. + des_encrypt2((DES_LONG *)data,ks2,DES_ENCRYPT);
  19044. + des_encrypt2((DES_LONG *)data,ks1,DES_DECRYPT);
  19045. + l=data[0];
  19046. + r=data[1];
  19047. + FP(r,l);
  19048. + data[0]=l;
  19049. + data[1]=r;
  19050. + }
  19051. +
  19052. +#ifndef DES_DEFAULT_OPTIONS
  19053. +
  19054. +void des_ncbc_encrypt(input, output, length, schedule, ivec, enc)
  19055. +des_cblock (*input);
  19056. +des_cblock (*output);
  19057. +long length;
  19058. +des_key_schedule schedule;
  19059. +des_cblock (*ivec);
  19060. +int enc;
  19061. + {
  19062. + register DES_LONG tin0,tin1;
  19063. + register DES_LONG tout0,tout1,xor0,xor1;
  19064. + register unsigned char *in,*out;
  19065. + register long l=length;
  19066. + DES_LONG tin[2];
  19067. + unsigned char *iv;
  19068. +
  19069. + in=(unsigned char *)input;
  19070. + out=(unsigned char *)output;
  19071. + iv=(unsigned char *)ivec;
  19072. +
  19073. + if (enc)
  19074. + {
  19075. + c2l(iv,tout0);
  19076. + c2l(iv,tout1);
  19077. + for (l-=8; l>=0; l-=8)
  19078. + {
  19079. + c2l(in,tin0);
  19080. + c2l(in,tin1);
  19081. + tin0^=tout0; tin[0]=tin0;
  19082. + tin1^=tout1; tin[1]=tin1;
  19083. + des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
  19084. + tout0=tin[0]; l2c(tout0,out);
  19085. + tout1=tin[1]; l2c(tout1,out);
  19086. + }
  19087. + if (l != -8)
  19088. + {
  19089. + c2ln(in,tin0,tin1,l+8);
  19090. + tin0^=tout0; tin[0]=tin0;
  19091. + tin1^=tout1; tin[1]=tin1;
  19092. + des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
  19093. + tout0=tin[0]; l2c(tout0,out);
  19094. + tout1=tin[1]; l2c(tout1,out);
  19095. + }
  19096. + iv=(unsigned char *)ivec;
  19097. + l2c(tout0,iv);
  19098. + l2c(tout1,iv);
  19099. + }
  19100. + else
  19101. + {
  19102. + c2l(iv,xor0);
  19103. + c2l(iv,xor1);
  19104. + for (l-=8; l>=0; l-=8)
  19105. + {
  19106. + c2l(in,tin0); tin[0]=tin0;
  19107. + c2l(in,tin1); tin[1]=tin1;
  19108. + des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
  19109. + tout0=tin[0]^xor0;
  19110. + tout1=tin[1]^xor1;
  19111. + l2c(tout0,out);
  19112. + l2c(tout1,out);
  19113. + xor0=tin0;
  19114. + xor1=tin1;
  19115. + }
  19116. + if (l != -8)
  19117. + {
  19118. + c2l(in,tin0); tin[0]=tin0;
  19119. + c2l(in,tin1); tin[1]=tin1;
  19120. + des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
  19121. + tout0=tin[0]^xor0;
  19122. + tout1=tin[1]^xor1;
  19123. + l2cn(tout0,tout1,out,l+8);
  19124. + xor0=tin0;
  19125. + xor1=tin1;
  19126. + }
  19127. +
  19128. + iv=(unsigned char *)ivec;
  19129. + l2c(xor0,iv);
  19130. + l2c(xor1,iv);
  19131. + }
  19132. + tin0=tin1=tout0=tout1=xor0=xor1=0;
  19133. + tin[0]=tin[1]=0;
  19134. + }
  19135. +
  19136. +void des_ede3_cbc_encrypt(input, output, length, ks1, ks2, ks3, ivec, enc)
  19137. +des_cblock (*input);
  19138. +des_cblock (*output);
  19139. +long length;
  19140. +des_key_schedule ks1;
  19141. +des_key_schedule ks2;
  19142. +des_key_schedule ks3;
  19143. +des_cblock (*ivec);
  19144. +int enc;
  19145. + {
  19146. + register DES_LONG tin0,tin1;
  19147. + register DES_LONG tout0,tout1,xor0,xor1;
  19148. + register unsigned char *in,*out;
  19149. + register long l=length;
  19150. + DES_LONG tin[2];
  19151. + unsigned char *iv;
  19152. +
  19153. + in=(unsigned char *)input;
  19154. + out=(unsigned char *)output;
  19155. + iv=(unsigned char *)ivec;
  19156. +
  19157. + if (enc)
  19158. + {
  19159. + c2l(iv,tout0);
  19160. + c2l(iv,tout1);
  19161. + for (l-=8; l>=0; l-=8)
  19162. + {
  19163. + c2l(in,tin0);
  19164. + c2l(in,tin1);
  19165. + tin0^=tout0;
  19166. + tin1^=tout1;
  19167. +
  19168. + tin[0]=tin0;
  19169. + tin[1]=tin1;
  19170. + des_encrypt3((DES_LONG *)tin,ks1,ks2,ks3);
  19171. + tout0=tin[0];
  19172. + tout1=tin[1];
  19173. +
  19174. + l2c(tout0,out);
  19175. + l2c(tout1,out);
  19176. + }
  19177. + if (l != -8)
  19178. + {
  19179. + c2ln(in,tin0,tin1,l+8);
  19180. + tin0^=tout0;
  19181. + tin1^=tout1;
  19182. +
  19183. + tin[0]=tin0;
  19184. + tin[1]=tin1;
  19185. + des_encrypt3((DES_LONG *)tin,ks1,ks2,ks3);
  19186. + tout0=tin[0];
  19187. + tout1=tin[1];
  19188. +
  19189. + l2c(tout0,out);
  19190. + l2c(tout1,out);
  19191. + }
  19192. + iv=(unsigned char *)ivec;
  19193. + l2c(tout0,iv);
  19194. + l2c(tout1,iv);
  19195. + }
  19196. + else
  19197. + {
  19198. + register DES_LONG t0,t1;
  19199. +
  19200. + c2l(iv,xor0);
  19201. + c2l(iv,xor1);
  19202. + for (l-=8; l>=0; l-=8)
  19203. + {
  19204. + c2l(in,tin0);
  19205. + c2l(in,tin1);
  19206. +
  19207. + t0=tin0;
  19208. + t1=tin1;
  19209. +
  19210. + tin[0]=tin0;
  19211. + tin[1]=tin1;
  19212. + des_decrypt3((DES_LONG *)tin,ks1,ks2,ks3);
  19213. + tout0=tin[0];
  19214. + tout1=tin[1];
  19215. +
  19216. + tout0^=xor0;
  19217. + tout1^=xor1;
  19218. + l2c(tout0,out);
  19219. + l2c(tout1,out);
  19220. + xor0=t0;
  19221. + xor1=t1;
  19222. + }
  19223. + if (l != -8)
  19224. + {
  19225. + c2l(in,tin0);
  19226. + c2l(in,tin1);
  19227. +
  19228. + t0=tin0;
  19229. + t1=tin1;
  19230. +
  19231. + tin[0]=tin0;
  19232. + tin[1]=tin1;
  19233. + des_decrypt3((DES_LONG *)tin,ks1,ks2,ks3);
  19234. + tout0=tin[0];
  19235. + tout1=tin[1];
  19236. +
  19237. + tout0^=xor0;
  19238. + tout1^=xor1;
  19239. + l2cn(tout0,tout1,out,l+8);
  19240. + xor0=t0;
  19241. + xor1=t1;
  19242. + }
  19243. +
  19244. + iv=(unsigned char *)ivec;
  19245. + l2c(xor0,iv);
  19246. + l2c(xor1,iv);
  19247. + }
  19248. + tin0=tin1=tout0=tout1=xor0=xor1=0;
  19249. + tin[0]=tin[1]=0;
  19250. + }
  19251. +
  19252. +#endif /* DES_DEFAULT_OPTIONS */
  19253. --- /dev/null Tue Mar 11 13:02:56 2003
  19254. +++ linux/net/ipsec/des/des_opts.c Mon Feb 9 13:51:03 2004
  19255. @@ -0,0 +1,620 @@
  19256. +/* crypto/des/des_opts.c */
  19257. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  19258. + * All rights reserved.
  19259. + *
  19260. + * This package is an SSL implementation written
  19261. + * by Eric Young (eay@cryptsoft.com).
  19262. + * The implementation was written so as to conform with Netscapes SSL.
  19263. + *
  19264. + * This library is free for commercial and non-commercial use as long as
  19265. + * the following conditions are aheared to. The following conditions
  19266. + * apply to all code found in this distribution, be it the RC4, RSA,
  19267. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  19268. + * included with this distribution is covered by the same copyright terms
  19269. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  19270. + *
  19271. + * Copyright remains Eric Young's, and as such any Copyright notices in
  19272. + * the code are not to be removed.
  19273. + * If this package is used in a product, Eric Young should be given attribution
  19274. + * as the author of the parts of the library used.
  19275. + * This can be in the form of a textual message at program startup or
  19276. + * in documentation (online or textual) provided with the package.
  19277. + *
  19278. + * Redistribution and use in source and binary forms, with or without
  19279. + * modification, are permitted provided that the following conditions
  19280. + * are met:
  19281. + * 1. Redistributions of source code must retain the copyright
  19282. + * notice, this list of conditions and the following disclaimer.
  19283. + * 2. Redistributions in binary form must reproduce the above copyright
  19284. + * notice, this list of conditions and the following disclaimer in the
  19285. + * documentation and/or other materials provided with the distribution.
  19286. + * 3. All advertising materials mentioning features or use of this software
  19287. + * must display the following acknowledgement:
  19288. + * "This product includes cryptographic software written by
  19289. + * Eric Young (eay@cryptsoft.com)"
  19290. + * The word 'cryptographic' can be left out if the rouines from the library
  19291. + * being used are not cryptographic related :-).
  19292. + * 4. If you include any Windows specific code (or a derivative thereof) from
  19293. + * the apps directory (application code) you must include an acknowledgement:
  19294. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  19295. + *
  19296. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  19297. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19298. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19299. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19300. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19301. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19302. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  19303. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  19304. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  19305. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  19306. + * SUCH DAMAGE.
  19307. + *
  19308. + * The licence and distribution terms for any publically available version or
  19309. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  19310. + * copied and put under another distribution licence
  19311. + * [including the GNU Public Licence.]
  19312. + */
  19313. +
  19314. +/* define PART1, PART2, PART3 or PART4 to build only with a few of the options.
  19315. + * This is for machines with 64k code segment size restrictions. */
  19316. +
  19317. +#ifndef MSDOS
  19318. +#define TIMES
  19319. +#endif
  19320. +
  19321. +#include <stdio.h>
  19322. +#ifndef MSDOS
  19323. +#include <unistd.h>
  19324. +#else
  19325. +#include <io.h>
  19326. +extern void exit();
  19327. +#endif
  19328. +#include <signal.h>
  19329. +#ifndef VMS
  19330. +#ifndef _IRIX
  19331. +#include <time.h>
  19332. +#endif
  19333. +#ifdef TIMES
  19334. +#include <sys/types.h>
  19335. +#include <sys/times.h>
  19336. +#endif
  19337. +#else /* VMS */
  19338. +#include <types.h>
  19339. +struct tms {
  19340. + time_t tms_utime;
  19341. + time_t tms_stime;
  19342. + time_t tms_uchild; /* I dunno... */
  19343. + time_t tms_uchildsys; /* so these names are a guess :-) */
  19344. + }
  19345. +#endif
  19346. +#ifndef TIMES
  19347. +#include <sys/timeb.h>
  19348. +#endif
  19349. +
  19350. +#ifdef sun
  19351. +#include <limits.h>
  19352. +#include <sys/param.h>
  19353. +#endif
  19354. +
  19355. +#include "des/des_locl.h"
  19356. +#include "des/spr.h"
  19357. +
  19358. +#define DES_DEFAULT_OPTIONS
  19359. +
  19360. +#if !defined(PART1) && !defined(PART2) && !defined(PART3) && !defined(PART4)
  19361. +#define PART1
  19362. +#define PART2
  19363. +#define PART3
  19364. +#define PART4
  19365. +#endif
  19366. +
  19367. +#ifdef PART1
  19368. +
  19369. +#undef DES_UNROLL
  19370. +#undef DES_RISC1
  19371. +#undef DES_RISC2
  19372. +#undef DES_PTR
  19373. +#undef D_ENCRYPT
  19374. +#define des_encrypt des_encrypt_u4_cisc_idx
  19375. +#define des_encrypt2 des_encrypt2_u4_cisc_idx
  19376. +#define des_encrypt3 des_encrypt3_u4_cisc_idx
  19377. +#define des_decrypt3 des_decrypt3_u4_cisc_idx
  19378. +#undef HEADER_DES_LOCL_H
  19379. +#include "des_enc.c"
  19380. +
  19381. +#define DES_UNROLL
  19382. +#undef DES_RISC1
  19383. +#undef DES_RISC2
  19384. +#undef DES_PTR
  19385. +#undef D_ENCRYPT
  19386. +#undef des_encrypt
  19387. +#undef des_encrypt2
  19388. +#undef des_encrypt3
  19389. +#undef des_decrypt3
  19390. +#define des_encrypt des_encrypt_u16_cisc_idx
  19391. +#define des_encrypt2 des_encrypt2_u16_cisc_idx
  19392. +#define des_encrypt3 des_encrypt3_u16_cisc_idx
  19393. +#define des_decrypt3 des_decrypt3_u16_cisc_idx
  19394. +#undef HEADER_DES_LOCL_H
  19395. +#include "des_enc.c"
  19396. +
  19397. +#undef DES_UNROLL
  19398. +#define DES_RISC1
  19399. +#undef DES_RISC2
  19400. +#undef DES_PTR
  19401. +#undef D_ENCRYPT
  19402. +#undef des_encrypt
  19403. +#undef des_encrypt2
  19404. +#undef des_encrypt3
  19405. +#undef des_decrypt3
  19406. +#define des_encrypt des_encrypt_u4_risc1_idx
  19407. +#define des_encrypt2 des_encrypt2_u4_risc1_idx
  19408. +#define des_encrypt3 des_encrypt3_u4_risc1_idx
  19409. +#define des_decrypt3 des_decrypt3_u4_risc1_idx
  19410. +#undef HEADER_DES_LOCL_H
  19411. +#include "des_enc.c"
  19412. +
  19413. +#endif
  19414. +
  19415. +#ifdef PART2
  19416. +
  19417. +#undef DES_UNROLL
  19418. +#undef DES_RISC1
  19419. +#define DES_RISC2
  19420. +#undef DES_PTR
  19421. +#undef D_ENCRYPT
  19422. +#undef des_encrypt
  19423. +#undef des_encrypt2
  19424. +#undef des_encrypt3
  19425. +#undef des_decrypt3
  19426. +#define des_encrypt des_encrypt_u4_risc2_idx
  19427. +#define des_encrypt2 des_encrypt2_u4_risc2_idx
  19428. +#define des_encrypt3 des_encrypt3_u4_risc2_idx
  19429. +#define des_decrypt3 des_decrypt3_u4_risc2_idx
  19430. +#undef HEADER_DES_LOCL_H
  19431. +#include "des_enc.c"
  19432. +
  19433. +#define DES_UNROLL
  19434. +#define DES_RISC1
  19435. +#undef DES_RISC2
  19436. +#undef DES_PTR
  19437. +#undef D_ENCRYPT
  19438. +#undef des_encrypt
  19439. +#undef des_encrypt2
  19440. +#undef des_encrypt3
  19441. +#undef des_decrypt3
  19442. +#define des_encrypt des_encrypt_u16_risc1_idx
  19443. +#define des_encrypt2 des_encrypt2_u16_risc1_idx
  19444. +#define des_encrypt3 des_encrypt3_u16_risc1_idx
  19445. +#define des_decrypt3 des_decrypt3_u16_risc1_idx
  19446. +#undef HEADER_DES_LOCL_H
  19447. +#include "des_enc.c"
  19448. +
  19449. +#define DES_UNROLL
  19450. +#undef DES_RISC1
  19451. +#define DES_RISC2
  19452. +#undef DES_PTR
  19453. +#undef D_ENCRYPT
  19454. +#undef des_encrypt
  19455. +#undef des_encrypt2
  19456. +#undef des_encrypt3
  19457. +#undef des_decrypt3
  19458. +#define des_encrypt des_encrypt_u16_risc2_idx
  19459. +#define des_encrypt2 des_encrypt2_u16_risc2_idx
  19460. +#define des_encrypt3 des_encrypt3_u16_risc2_idx
  19461. +#define des_decrypt3 des_decrypt3_u16_risc2_idx
  19462. +#undef HEADER_DES_LOCL_H
  19463. +#include "des_enc.c"
  19464. +
  19465. +#endif
  19466. +
  19467. +#ifdef PART3
  19468. +
  19469. +#undef DES_UNROLL
  19470. +#undef DES_RISC1
  19471. +#undef DES_RISC2
  19472. +#define DES_PTR
  19473. +#undef D_ENCRYPT
  19474. +#undef des_encrypt
  19475. +#undef des_encrypt2
  19476. +#undef des_encrypt3
  19477. +#undef des_decrypt3
  19478. +#define des_encrypt des_encrypt_u4_cisc_ptr
  19479. +#define des_encrypt2 des_encrypt2_u4_cisc_ptr
  19480. +#define des_encrypt3 des_encrypt3_u4_cisc_ptr
  19481. +#define des_decrypt3 des_decrypt3_u4_cisc_ptr
  19482. +#undef HEADER_DES_LOCL_H
  19483. +#include "des_enc.c"
  19484. +
  19485. +#define DES_UNROLL
  19486. +#undef DES_RISC1
  19487. +#undef DES_RISC2
  19488. +#define DES_PTR
  19489. +#undef D_ENCRYPT
  19490. +#undef des_encrypt
  19491. +#undef des_encrypt2
  19492. +#undef des_encrypt3
  19493. +#undef des_decrypt3
  19494. +#define des_encrypt des_encrypt_u16_cisc_ptr
  19495. +#define des_encrypt2 des_encrypt2_u16_cisc_ptr
  19496. +#define des_encrypt3 des_encrypt3_u16_cisc_ptr
  19497. +#define des_decrypt3 des_decrypt3_u16_cisc_ptr
  19498. +#undef HEADER_DES_LOCL_H
  19499. +#include "des_enc.c"
  19500. +
  19501. +#undef DES_UNROLL
  19502. +#define DES_RISC1
  19503. +#undef DES_RISC2
  19504. +#define DES_PTR
  19505. +#undef D_ENCRYPT
  19506. +#undef des_encrypt
  19507. +#undef des_encrypt2
  19508. +#undef des_encrypt3
  19509. +#undef des_decrypt3
  19510. +#define des_encrypt des_encrypt_u4_risc1_ptr
  19511. +#define des_encrypt2 des_encrypt2_u4_risc1_ptr
  19512. +#define des_encrypt3 des_encrypt3_u4_risc1_ptr
  19513. +#define des_decrypt3 des_decrypt3_u4_risc1_ptr
  19514. +#undef HEADER_DES_LOCL_H
  19515. +#include "des_enc.c"
  19516. +
  19517. +#endif
  19518. +
  19519. +#ifdef PART4
  19520. +
  19521. +#undef DES_UNROLL
  19522. +#undef DES_RISC1
  19523. +#define DES_RISC2
  19524. +#define DES_PTR
  19525. +#undef D_ENCRYPT
  19526. +#undef des_encrypt
  19527. +#undef des_encrypt2
  19528. +#undef des_encrypt3
  19529. +#undef des_decrypt3
  19530. +#define des_encrypt des_encrypt_u4_risc2_ptr
  19531. +#define des_encrypt2 des_encrypt2_u4_risc2_ptr
  19532. +#define des_encrypt3 des_encrypt3_u4_risc2_ptr
  19533. +#define des_decrypt3 des_decrypt3_u4_risc2_ptr
  19534. +#undef HEADER_DES_LOCL_H
  19535. +#include "des_enc.c"
  19536. +
  19537. +#define DES_UNROLL
  19538. +#define DES_RISC1
  19539. +#undef DES_RISC2
  19540. +#define DES_PTR
  19541. +#undef D_ENCRYPT
  19542. +#undef des_encrypt
  19543. +#undef des_encrypt2
  19544. +#undef des_encrypt3
  19545. +#undef des_decrypt3
  19546. +#define des_encrypt des_encrypt_u16_risc1_ptr
  19547. +#define des_encrypt2 des_encrypt2_u16_risc1_ptr
  19548. +#define des_encrypt3 des_encrypt3_u16_risc1_ptr
  19549. +#define des_decrypt3 des_decrypt3_u16_risc1_ptr
  19550. +#undef HEADER_DES_LOCL_H
  19551. +#include "des_enc.c"
  19552. +
  19553. +#define DES_UNROLL
  19554. +#undef DES_RISC1
  19555. +#define DES_RISC2
  19556. +#define DES_PTR
  19557. +#undef D_ENCRYPT
  19558. +#undef des_encrypt
  19559. +#undef des_encrypt2
  19560. +#undef des_encrypt3
  19561. +#undef des_decrypt3
  19562. +#define des_encrypt des_encrypt_u16_risc2_ptr
  19563. +#define des_encrypt2 des_encrypt2_u16_risc2_ptr
  19564. +#define des_encrypt3 des_encrypt3_u16_risc2_ptr
  19565. +#define des_decrypt3 des_decrypt3_u16_risc2_ptr
  19566. +#undef HEADER_DES_LOCL_H
  19567. +#include "des_enc.c"
  19568. +
  19569. +#endif
  19570. +
  19571. +/* The following if from times(3) man page. It may need to be changed */
  19572. +#ifndef HZ
  19573. +# ifndef CLK_TCK
  19574. +# ifndef _BSD_CLK_TCK_ /* FreeBSD fix */
  19575. +# ifndef VMS
  19576. +# define HZ 100.0
  19577. +# else /* VMS */
  19578. +# define HZ 100.0
  19579. +# endif
  19580. +# else /* _BSD_CLK_TCK_ */
  19581. +# define HZ ((double)_BSD_CLK_TCK_)
  19582. +# endif
  19583. +# else /* CLK_TCK */
  19584. +# define HZ ((double)CLK_TCK)
  19585. +# endif
  19586. +#endif
  19587. +
  19588. +#define BUFSIZE ((long)1024)
  19589. +long run=0;
  19590. +
  19591. +#ifndef NOPROTO
  19592. +double Time_F(int s);
  19593. +#else
  19594. +double Time_F();
  19595. +#endif
  19596. +
  19597. +#ifdef SIGALRM
  19598. +#if defined(__STDC__) || defined(sgi)
  19599. +#define SIGRETTYPE void
  19600. +#else
  19601. +#define SIGRETTYPE int
  19602. +#endif
  19603. +
  19604. +#ifndef NOPROTO
  19605. +SIGRETTYPE sig_done(int sig);
  19606. +#else
  19607. +SIGRETTYPE sig_done();
  19608. +#endif
  19609. +
  19610. +SIGRETTYPE sig_done(sig)
  19611. +int sig;
  19612. + {
  19613. + signal(SIGALRM,sig_done);
  19614. + run=0;
  19615. +#ifdef LINT
  19616. + sig=sig;
  19617. +#endif
  19618. + }
  19619. +#endif
  19620. +
  19621. +#define START 0
  19622. +#define STOP 1
  19623. +
  19624. +double Time_F(s)
  19625. +int s;
  19626. + {
  19627. + double ret;
  19628. +#ifdef TIMES
  19629. + static struct tms tstart,tend;
  19630. +
  19631. + if (s == START)
  19632. + {
  19633. + times(&tstart);
  19634. + return(0);
  19635. + }
  19636. + else
  19637. + {
  19638. + times(&tend);
  19639. + ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
  19640. + return((ret == 0.0)?1e-6:ret);
  19641. + }
  19642. +#else /* !times() */
  19643. + static struct timeb tstart,tend;
  19644. + long i;
  19645. +
  19646. + if (s == START)
  19647. + {
  19648. + ftime(&tstart);
  19649. + return(0);
  19650. + }
  19651. + else
  19652. + {
  19653. + ftime(&tend);
  19654. + i=(long)tend.millitm-(long)tstart.millitm;
  19655. + ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
  19656. + return((ret == 0.0)?1e-6:ret);
  19657. + }
  19658. +#endif
  19659. + }
  19660. +
  19661. +#ifdef SIGALRM
  19662. +#define print_name(name) fprintf(stderr,"Doing %s's for 10 seconds\n",name); alarm(10);
  19663. +#else
  19664. +#define print_name(name) fprintf(stderr,"Doing %s %ld times\n",name,cb);
  19665. +#endif
  19666. +
  19667. +#define time_it(func,name,index) \
  19668. + print_name(name); \
  19669. + Time_F(START); \
  19670. + for (count=0,run=1; COND(cb); count++) \
  19671. + { \
  19672. + unsigned long d[2]; \
  19673. + func(d,&(sch[0]),DES_ENCRYPT); \
  19674. + } \
  19675. + tm[index]=Time_F(STOP); \
  19676. + fprintf(stderr,"%ld %s's in %.2f second\n",count,name,tm[index]); \
  19677. + tm[index]=((double)COUNT(cb))/tm[index];
  19678. +
  19679. +#define print_it(name,index) \
  19680. + fprintf(stderr,"%s bytes per sec = %12.2f (%5.1fuS)\n",name, \
  19681. + tm[index]*8,1.0e6/tm[index]);
  19682. +
  19683. +int main(argc,argv)
  19684. +int argc;
  19685. +char **argv;
  19686. + {
  19687. + long count;
  19688. + static unsigned char buf[BUFSIZE];
  19689. + static des_cblock key ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
  19690. + static des_cblock key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};
  19691. + static des_cblock key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34};
  19692. + des_key_schedule sch,sch2,sch3;
  19693. + double d,tm[16],max=0;
  19694. + int rank[16];
  19695. + char *str[16];
  19696. + int max_idx=0,i,num=0,j;
  19697. +#ifndef SIGALARM
  19698. + long ca,cb,cc,cd,ce;
  19699. +#endif
  19700. +
  19701. + for (i=0; i<12; i++)
  19702. + {
  19703. + tm[i]=0.0;
  19704. + rank[i]=0;
  19705. + }
  19706. +
  19707. +#ifndef TIMES
  19708. + fprintf(stderr,"To get the most acurate results, try to run this\n");
  19709. + fprintf(stderr,"program when this computer is idle.\n");
  19710. +#endif
  19711. +
  19712. + des_set_key((C_Block *)key,sch);
  19713. + des_set_key((C_Block *)key2,sch2);
  19714. + des_set_key((C_Block *)key3,sch3);
  19715. +
  19716. +#ifndef SIGALRM
  19717. + fprintf(stderr,"First we calculate the approximate speed ...\n");
  19718. + des_set_key((C_Block *)key,sch);
  19719. + count=10;
  19720. + do {
  19721. + long i;
  19722. + unsigned long data[2];
  19723. +
  19724. + count*=2;
  19725. + Time_F(START);
  19726. + for (i=count; i; i--)
  19727. + des_encrypt(data,&(sch[0]),DES_ENCRYPT);
  19728. + d=Time_F(STOP);
  19729. + } while (d < 3.0);
  19730. + ca=count;
  19731. + cb=count*3;
  19732. + cc=count*3*8/BUFSIZE+1;
  19733. + cd=count*8/BUFSIZE+1;
  19734. +
  19735. + ce=count/20+1;
  19736. +#define COND(d) (count != (d))
  19737. +#define COUNT(d) (d)
  19738. +#else
  19739. +#define COND(c) (run)
  19740. +#define COUNT(d) (count)
  19741. + signal(SIGALRM,sig_done);
  19742. + alarm(10);
  19743. +#endif
  19744. +
  19745. +#ifdef PART1
  19746. + time_it(des_encrypt_u4_cisc_idx, "des_encrypt_u4_cisc_idx ", 0);
  19747. + time_it(des_encrypt_u16_cisc_idx, "des_encrypt_u16_cisc_idx ", 1);
  19748. + time_it(des_encrypt_u4_risc1_idx, "des_encrypt_u4_risc1_idx ", 2);
  19749. + num+=3;
  19750. +#endif
  19751. +#ifdef PART2
  19752. + time_it(des_encrypt_u16_risc1_idx,"des_encrypt_u16_risc1_idx", 3);
  19753. + time_it(des_encrypt_u4_risc2_idx, "des_encrypt_u4_risc2_idx ", 4);
  19754. + time_it(des_encrypt_u16_risc2_idx,"des_encrypt_u16_risc2_idx", 5);
  19755. + num+=3;
  19756. +#endif
  19757. +#ifdef PART3
  19758. + time_it(des_encrypt_u4_cisc_ptr, "des_encrypt_u4_cisc_ptr ", 6);
  19759. + time_it(des_encrypt_u16_cisc_ptr, "des_encrypt_u16_cisc_ptr ", 7);
  19760. + time_it(des_encrypt_u4_risc1_ptr, "des_encrypt_u4_risc1_ptr ", 8);
  19761. + num+=3;
  19762. +#endif
  19763. +#ifdef PART4
  19764. + time_it(des_encrypt_u16_risc1_ptr,"des_encrypt_u16_risc1_ptr", 9);
  19765. + time_it(des_encrypt_u4_risc2_ptr, "des_encrypt_u4_risc2_ptr ",10);
  19766. + time_it(des_encrypt_u16_risc2_ptr,"des_encrypt_u16_risc2_ptr",11);
  19767. + num+=3;
  19768. +#endif
  19769. +
  19770. +#ifdef PART1
  19771. + str[0]=" 4 c i";
  19772. + print_it("des_encrypt_u4_cisc_idx ",0);
  19773. + max=tm[0];
  19774. + max_idx=0;
  19775. + str[1]="16 c i";
  19776. + print_it("des_encrypt_u16_cisc_idx ",1);
  19777. + if (max < tm[1]) { max=tm[1]; max_idx=1; }
  19778. + str[2]=" 4 r1 i";
  19779. + print_it("des_encrypt_u4_risc1_idx ",2);
  19780. + if (max < tm[2]) { max=tm[2]; max_idx=2; }
  19781. +#endif
  19782. +#ifdef PART2
  19783. + str[3]="16 r1 i";
  19784. + print_it("des_encrypt_u16_risc1_idx",3);
  19785. + if (max < tm[3]) { max=tm[3]; max_idx=3; }
  19786. + str[4]=" 4 r2 i";
  19787. + print_it("des_encrypt_u4_risc2_idx ",4);
  19788. + if (max < tm[4]) { max=tm[4]; max_idx=4; }
  19789. + str[5]="16 r2 i";
  19790. + print_it("des_encrypt_u16_risc2_idx",5);
  19791. + if (max < tm[5]) { max=tm[5]; max_idx=5; }
  19792. +#endif
  19793. +#ifdef PART3
  19794. + str[6]=" 4 c p";
  19795. + print_it("des_encrypt_u4_cisc_ptr ",6);
  19796. + if (max < tm[6]) { max=tm[6]; max_idx=6; }
  19797. + str[7]="16 c p";
  19798. + print_it("des_encrypt_u16_cisc_ptr ",7);
  19799. + if (max < tm[7]) { max=tm[7]; max_idx=7; }
  19800. + str[8]=" 4 r1 p";
  19801. + print_it("des_encrypt_u4_risc1_ptr ",8);
  19802. + if (max < tm[8]) { max=tm[8]; max_idx=8; }
  19803. +#endif
  19804. +#ifdef PART4
  19805. + str[9]="16 r1 p";
  19806. + print_it("des_encrypt_u16_risc1_ptr",9);
  19807. + if (max < tm[9]) { max=tm[9]; max_idx=9; }
  19808. + str[10]=" 4 r2 p";
  19809. + print_it("des_encrypt_u4_risc2_ptr ",10);
  19810. + if (max < tm[10]) { max=tm[10]; max_idx=10; }
  19811. + str[11]="16 r2 p";
  19812. + print_it("des_encrypt_u16_risc2_ptr",11);
  19813. + if (max < tm[11]) { max=tm[11]; max_idx=11; }
  19814. +#endif
  19815. + printf("options des ecb/s\n");
  19816. + printf("%s %12.2f 100.0%%\n",str[max_idx],tm[max_idx]);
  19817. + d=tm[max_idx];
  19818. + tm[max_idx]= -2.0;
  19819. + max= -1.0;
  19820. + for (;;)
  19821. + {
  19822. + for (i=0; i<12; i++)
  19823. + {
  19824. + if (max < tm[i]) { max=tm[i]; j=i; }
  19825. + }
  19826. + if (max < 0.0) break;
  19827. + printf("%s %12.2f %4.1f%%\n",str[j],tm[j],tm[j]/d*100.0);
  19828. + tm[j]= -2.0;
  19829. + max= -1.0;
  19830. + }
  19831. +
  19832. + switch (max_idx)
  19833. + {
  19834. + case 0:
  19835. + printf("-DDES_DEFAULT_OPTIONS\n");
  19836. + break;
  19837. + case 1:
  19838. + printf("-DDES_UNROLL\n");
  19839. + break;
  19840. + case 2:
  19841. + printf("-DDES_RISC1\n");
  19842. + break;
  19843. + case 3:
  19844. + printf("-DDES_UNROLL -DDES_RISC1\n");
  19845. + break;
  19846. + case 4:
  19847. + printf("-DDES_RISC2\n");
  19848. + break;
  19849. + case 5:
  19850. + printf("-DDES_UNROLL -DDES_RISC2\n");
  19851. + break;
  19852. + case 6:
  19853. + printf("-DDES_PTR\n");
  19854. + break;
  19855. + case 7:
  19856. + printf("-DDES_UNROLL -DDES_PTR\n");
  19857. + break;
  19858. + case 8:
  19859. + printf("-DDES_RISC1 -DDES_PTR\n");
  19860. + break;
  19861. + case 9:
  19862. + printf("-DDES_UNROLL -DDES_RISC1 -DDES_PTR\n");
  19863. + break;
  19864. + case 10:
  19865. + printf("-DDES_RISC2 -DDES_PTR\n");
  19866. + break;
  19867. + case 11:
  19868. + printf("-DDES_UNROLL -DDES_RISC2 -DDES_PTR\n");
  19869. + break;
  19870. + }
  19871. + exit(0);
  19872. +#if defined(LINT) || defined(MSDOS)
  19873. + return(0);
  19874. +#endif
  19875. + }
  19876. --- /dev/null Tue Mar 11 13:02:56 2003
  19877. +++ linux/net/ipsec/des/dx86unix.S Mon Feb 9 13:51:03 2004
  19878. @@ -0,0 +1,3160 @@
  19879. +/*
  19880. + * This file was originally generated by Michael Richardson <mcr@freeswan.org>
  19881. + * via the perl scripts found in the ASM subdir. It remains copyright of
  19882. + * Eric Young, see the file COPYRIGHT.
  19883. + *
  19884. + * This was last done on October 9, 2002.
  19885. + *
  19886. + * While this file does not need to go through cpp, we pass it through
  19887. + * CPP by naming it dx86unix.S instead of dx86unix.s because there is
  19888. + * a bug in Rules.make for .s builds - specifically it references EXTRA_CFLAGS
  19889. + * which may contain stuff that AS doesn't understand instead of
  19890. + * referencing EXTRA_AFLAGS.
  19891. + */
  19892. +
  19893. + .file "dx86unix.S"
  19894. + .version "01.01"
  19895. +.text
  19896. + .align 16
  19897. +.globl des_encrypt
  19898. + .type des_encrypt , @function
  19899. +des_encrypt:
  19900. + pushl %esi
  19901. + pushl %edi
  19902. +
  19903. +
  19904. + movl 12(%esp), %esi
  19905. + xorl %ecx, %ecx
  19906. + pushl %ebx
  19907. + pushl %ebp
  19908. + movl (%esi), %eax
  19909. + movl 28(%esp), %ebx
  19910. + movl 4(%esi), %edi
  19911. +
  19912. +
  19913. + roll $4, %eax
  19914. + movl %eax, %esi
  19915. + xorl %edi, %eax
  19916. + andl $0xf0f0f0f0, %eax
  19917. + xorl %eax, %esi
  19918. + xorl %eax, %edi
  19919. +
  19920. + roll $20, %edi
  19921. + movl %edi, %eax
  19922. + xorl %esi, %edi
  19923. + andl $0xfff0000f, %edi
  19924. + xorl %edi, %eax
  19925. + xorl %edi, %esi
  19926. +
  19927. + roll $14, %eax
  19928. + movl %eax, %edi
  19929. + xorl %esi, %eax
  19930. + andl $0x33333333, %eax
  19931. + xorl %eax, %edi
  19932. + xorl %eax, %esi
  19933. +
  19934. + roll $22, %esi
  19935. + movl %esi, %eax
  19936. + xorl %edi, %esi
  19937. + andl $0x03fc03fc, %esi
  19938. + xorl %esi, %eax
  19939. + xorl %esi, %edi
  19940. +
  19941. + roll $9, %eax
  19942. + movl %eax, %esi
  19943. + xorl %edi, %eax
  19944. + andl $0xaaaaaaaa, %eax
  19945. + xorl %eax, %esi
  19946. + xorl %eax, %edi
  19947. +
  19948. +.byte 209
  19949. +.byte 199
  19950. + movl 24(%esp), %ebp
  19951. + cmpl $0, %ebx
  19952. + je .L000start_decrypt
  19953. +
  19954. +
  19955. + movl (%ebp), %eax
  19956. + xorl %ebx, %ebx
  19957. + movl 4(%ebp), %edx
  19958. + xorl %esi, %eax
  19959. + xorl %esi, %edx
  19960. + andl $0xfcfcfcfc, %eax
  19961. + andl $0xcfcfcfcf, %edx
  19962. + movb %al, %bl
  19963. + movb %ah, %cl
  19964. + rorl $4, %edx
  19965. + movl des_SPtrans(%ebx),%ebp
  19966. + movb %dl, %bl
  19967. + xorl %ebp, %edi
  19968. + movl 0x200+des_SPtrans(%ecx),%ebp
  19969. + xorl %ebp, %edi
  19970. + movb %dh, %cl
  19971. + shrl $16, %eax
  19972. + movl 0x100+des_SPtrans(%ebx),%ebp
  19973. + xorl %ebp, %edi
  19974. + movb %ah, %bl
  19975. + shrl $16, %edx
  19976. + movl 0x300+des_SPtrans(%ecx),%ebp
  19977. + xorl %ebp, %edi
  19978. + movl 24(%esp), %ebp
  19979. + movb %dh, %cl
  19980. + andl $0xff, %eax
  19981. + andl $0xff, %edx
  19982. + movl 0x600+des_SPtrans(%ebx),%ebx
  19983. + xorl %ebx, %edi
  19984. + movl 0x700+des_SPtrans(%ecx),%ebx
  19985. + xorl %ebx, %edi
  19986. + movl 0x400+des_SPtrans(%eax),%ebx
  19987. + xorl %ebx, %edi
  19988. + movl 0x500+des_SPtrans(%edx),%ebx
  19989. + xorl %ebx, %edi
  19990. +
  19991. +
  19992. + movl 8(%ebp), %eax
  19993. + xorl %ebx, %ebx
  19994. + movl 12(%ebp), %edx
  19995. + xorl %edi, %eax
  19996. + xorl %edi, %edx
  19997. + andl $0xfcfcfcfc, %eax
  19998. + andl $0xcfcfcfcf, %edx
  19999. + movb %al, %bl
  20000. + movb %ah, %cl
  20001. + rorl $4, %edx
  20002. + movl des_SPtrans(%ebx),%ebp
  20003. + movb %dl, %bl
  20004. + xorl %ebp, %esi
  20005. + movl 0x200+des_SPtrans(%ecx),%ebp
  20006. + xorl %ebp, %esi
  20007. + movb %dh, %cl
  20008. + shrl $16, %eax
  20009. + movl 0x100+des_SPtrans(%ebx),%ebp
  20010. + xorl %ebp, %esi
  20011. + movb %ah, %bl
  20012. + shrl $16, %edx
  20013. + movl 0x300+des_SPtrans(%ecx),%ebp
  20014. + xorl %ebp, %esi
  20015. + movl 24(%esp), %ebp
  20016. + movb %dh, %cl
  20017. + andl $0xff, %eax
  20018. + andl $0xff, %edx
  20019. + movl 0x600+des_SPtrans(%ebx),%ebx
  20020. + xorl %ebx, %esi
  20021. + movl 0x700+des_SPtrans(%ecx),%ebx
  20022. + xorl %ebx, %esi
  20023. + movl 0x400+des_SPtrans(%eax),%ebx
  20024. + xorl %ebx, %esi
  20025. + movl 0x500+des_SPtrans(%edx),%ebx
  20026. + xorl %ebx, %esi
  20027. +
  20028. +
  20029. + movl 16(%ebp), %eax
  20030. + xorl %ebx, %ebx
  20031. + movl 20(%ebp), %edx
  20032. + xorl %esi, %eax
  20033. + xorl %esi, %edx
  20034. + andl $0xfcfcfcfc, %eax
  20035. + andl $0xcfcfcfcf, %edx
  20036. + movb %al, %bl
  20037. + movb %ah, %cl
  20038. + rorl $4, %edx
  20039. + movl des_SPtrans(%ebx),%ebp
  20040. + movb %dl, %bl
  20041. + xorl %ebp, %edi
  20042. + movl 0x200+des_SPtrans(%ecx),%ebp
  20043. + xorl %ebp, %edi
  20044. + movb %dh, %cl
  20045. + shrl $16, %eax
  20046. + movl 0x100+des_SPtrans(%ebx),%ebp
  20047. + xorl %ebp, %edi
  20048. + movb %ah, %bl
  20049. + shrl $16, %edx
  20050. + movl 0x300+des_SPtrans(%ecx),%ebp
  20051. + xorl %ebp, %edi
  20052. + movl 24(%esp), %ebp
  20053. + movb %dh, %cl
  20054. + andl $0xff, %eax
  20055. + andl $0xff, %edx
  20056. + movl 0x600+des_SPtrans(%ebx),%ebx
  20057. + xorl %ebx, %edi
  20058. + movl 0x700+des_SPtrans(%ecx),%ebx
  20059. + xorl %ebx, %edi
  20060. + movl 0x400+des_SPtrans(%eax),%ebx
  20061. + xorl %ebx, %edi
  20062. + movl 0x500+des_SPtrans(%edx),%ebx
  20063. + xorl %ebx, %edi
  20064. +
  20065. +
  20066. + movl 24(%ebp), %eax
  20067. + xorl %ebx, %ebx
  20068. + movl 28(%ebp), %edx
  20069. + xorl %edi, %eax
  20070. + xorl %edi, %edx
  20071. + andl $0xfcfcfcfc, %eax
  20072. + andl $0xcfcfcfcf, %edx
  20073. + movb %al, %bl
  20074. + movb %ah, %cl
  20075. + rorl $4, %edx
  20076. + movl des_SPtrans(%ebx),%ebp
  20077. + movb %dl, %bl
  20078. + xorl %ebp, %esi
  20079. + movl 0x200+des_SPtrans(%ecx),%ebp
  20080. + xorl %ebp, %esi
  20081. + movb %dh, %cl
  20082. + shrl $16, %eax
  20083. + movl 0x100+des_SPtrans(%ebx),%ebp
  20084. + xorl %ebp, %esi
  20085. + movb %ah, %bl
  20086. + shrl $16, %edx
  20087. + movl 0x300+des_SPtrans(%ecx),%ebp
  20088. + xorl %ebp, %esi
  20089. + movl 24(%esp), %ebp
  20090. + movb %dh, %cl
  20091. + andl $0xff, %eax
  20092. + andl $0xff, %edx
  20093. + movl 0x600+des_SPtrans(%ebx),%ebx
  20094. + xorl %ebx, %esi
  20095. + movl 0x700+des_SPtrans(%ecx),%ebx
  20096. + xorl %ebx, %esi
  20097. + movl 0x400+des_SPtrans(%eax),%ebx
  20098. + xorl %ebx, %esi
  20099. + movl 0x500+des_SPtrans(%edx),%ebx
  20100. + xorl %ebx, %esi
  20101. +
  20102. +
  20103. + movl 32(%ebp), %eax
  20104. + xorl %ebx, %ebx
  20105. + movl 36(%ebp), %edx
  20106. + xorl %esi, %eax
  20107. + xorl %esi, %edx
  20108. + andl $0xfcfcfcfc, %eax
  20109. + andl $0xcfcfcfcf, %edx
  20110. + movb %al, %bl
  20111. + movb %ah, %cl
  20112. + rorl $4, %edx
  20113. + movl des_SPtrans(%ebx),%ebp
  20114. + movb %dl, %bl
  20115. + xorl %ebp, %edi
  20116. + movl 0x200+des_SPtrans(%ecx),%ebp
  20117. + xorl %ebp, %edi
  20118. + movb %dh, %cl
  20119. + shrl $16, %eax
  20120. + movl 0x100+des_SPtrans(%ebx),%ebp
  20121. + xorl %ebp, %edi
  20122. + movb %ah, %bl
  20123. + shrl $16, %edx
  20124. + movl 0x300+des_SPtrans(%ecx),%ebp
  20125. + xorl %ebp, %edi
  20126. + movl 24(%esp), %ebp
  20127. + movb %dh, %cl
  20128. + andl $0xff, %eax
  20129. + andl $0xff, %edx
  20130. + movl 0x600+des_SPtrans(%ebx),%ebx
  20131. + xorl %ebx, %edi
  20132. + movl 0x700+des_SPtrans(%ecx),%ebx
  20133. + xorl %ebx, %edi
  20134. + movl 0x400+des_SPtrans(%eax),%ebx
  20135. + xorl %ebx, %edi
  20136. + movl 0x500+des_SPtrans(%edx),%ebx
  20137. + xorl %ebx, %edi
  20138. +
  20139. +
  20140. + movl 40(%ebp), %eax
  20141. + xorl %ebx, %ebx
  20142. + movl 44(%ebp), %edx
  20143. + xorl %edi, %eax
  20144. + xorl %edi, %edx
  20145. + andl $0xfcfcfcfc, %eax
  20146. + andl $0xcfcfcfcf, %edx
  20147. + movb %al, %bl
  20148. + movb %ah, %cl
  20149. + rorl $4, %edx
  20150. + movl des_SPtrans(%ebx),%ebp
  20151. + movb %dl, %bl
  20152. + xorl %ebp, %esi
  20153. + movl 0x200+des_SPtrans(%ecx),%ebp
  20154. + xorl %ebp, %esi
  20155. + movb %dh, %cl
  20156. + shrl $16, %eax
  20157. + movl 0x100+des_SPtrans(%ebx),%ebp
  20158. + xorl %ebp, %esi
  20159. + movb %ah, %bl
  20160. + shrl $16, %edx
  20161. + movl 0x300+des_SPtrans(%ecx),%ebp
  20162. + xorl %ebp, %esi
  20163. + movl 24(%esp), %ebp
  20164. + movb %dh, %cl
  20165. + andl $0xff, %eax
  20166. + andl $0xff, %edx
  20167. + movl 0x600+des_SPtrans(%ebx),%ebx
  20168. + xorl %ebx, %esi
  20169. + movl 0x700+des_SPtrans(%ecx),%ebx
  20170. + xorl %ebx, %esi
  20171. + movl 0x400+des_SPtrans(%eax),%ebx
  20172. + xorl %ebx, %esi
  20173. + movl 0x500+des_SPtrans(%edx),%ebx
  20174. + xorl %ebx, %esi
  20175. +
  20176. +
  20177. + movl 48(%ebp), %eax
  20178. + xorl %ebx, %ebx
  20179. + movl 52(%ebp), %edx
  20180. + xorl %esi, %eax
  20181. + xorl %esi, %edx
  20182. + andl $0xfcfcfcfc, %eax
  20183. + andl $0xcfcfcfcf, %edx
  20184. + movb %al, %bl
  20185. + movb %ah, %cl
  20186. + rorl $4, %edx
  20187. + movl des_SPtrans(%ebx),%ebp
  20188. + movb %dl, %bl
  20189. + xorl %ebp, %edi
  20190. + movl 0x200+des_SPtrans(%ecx),%ebp
  20191. + xorl %ebp, %edi
  20192. + movb %dh, %cl
  20193. + shrl $16, %eax
  20194. + movl 0x100+des_SPtrans(%ebx),%ebp
  20195. + xorl %ebp, %edi
  20196. + movb %ah, %bl
  20197. + shrl $16, %edx
  20198. + movl 0x300+des_SPtrans(%ecx),%ebp
  20199. + xorl %ebp, %edi
  20200. + movl 24(%esp), %ebp
  20201. + movb %dh, %cl
  20202. + andl $0xff, %eax
  20203. + andl $0xff, %edx
  20204. + movl 0x600+des_SPtrans(%ebx),%ebx
  20205. + xorl %ebx, %edi
  20206. + movl 0x700+des_SPtrans(%ecx),%ebx
  20207. + xorl %ebx, %edi
  20208. + movl 0x400+des_SPtrans(%eax),%ebx
  20209. + xorl %ebx, %edi
  20210. + movl 0x500+des_SPtrans(%edx),%ebx
  20211. + xorl %ebx, %edi
  20212. +
  20213. +
  20214. + movl 56(%ebp), %eax
  20215. + xorl %ebx, %ebx
  20216. + movl 60(%ebp), %edx
  20217. + xorl %edi, %eax
  20218. + xorl %edi, %edx
  20219. + andl $0xfcfcfcfc, %eax
  20220. + andl $0xcfcfcfcf, %edx
  20221. + movb %al, %bl
  20222. + movb %ah, %cl
  20223. + rorl $4, %edx
  20224. + movl des_SPtrans(%ebx),%ebp
  20225. + movb %dl, %bl
  20226. + xorl %ebp, %esi
  20227. + movl 0x200+des_SPtrans(%ecx),%ebp
  20228. + xorl %ebp, %esi
  20229. + movb %dh, %cl
  20230. + shrl $16, %eax
  20231. + movl 0x100+des_SPtrans(%ebx),%ebp
  20232. + xorl %ebp, %esi
  20233. + movb %ah, %bl
  20234. + shrl $16, %edx
  20235. + movl 0x300+des_SPtrans(%ecx),%ebp
  20236. + xorl %ebp, %esi
  20237. + movl 24(%esp), %ebp
  20238. + movb %dh, %cl
  20239. + andl $0xff, %eax
  20240. + andl $0xff, %edx
  20241. + movl 0x600+des_SPtrans(%ebx),%ebx
  20242. + xorl %ebx, %esi
  20243. + movl 0x700+des_SPtrans(%ecx),%ebx
  20244. + xorl %ebx, %esi
  20245. + movl 0x400+des_SPtrans(%eax),%ebx
  20246. + xorl %ebx, %esi
  20247. + movl 0x500+des_SPtrans(%edx),%ebx
  20248. + xorl %ebx, %esi
  20249. +
  20250. +
  20251. + movl 64(%ebp), %eax
  20252. + xorl %ebx, %ebx
  20253. + movl 68(%ebp), %edx
  20254. + xorl %esi, %eax
  20255. + xorl %esi, %edx
  20256. + andl $0xfcfcfcfc, %eax
  20257. + andl $0xcfcfcfcf, %edx
  20258. + movb %al, %bl
  20259. + movb %ah, %cl
  20260. + rorl $4, %edx
  20261. + movl des_SPtrans(%ebx),%ebp
  20262. + movb %dl, %bl
  20263. + xorl %ebp, %edi
  20264. + movl 0x200+des_SPtrans(%ecx),%ebp
  20265. + xorl %ebp, %edi
  20266. + movb %dh, %cl
  20267. + shrl $16, %eax
  20268. + movl 0x100+des_SPtrans(%ebx),%ebp
  20269. + xorl %ebp, %edi
  20270. + movb %ah, %bl
  20271. + shrl $16, %edx
  20272. + movl 0x300+des_SPtrans(%ecx),%ebp
  20273. + xorl %ebp, %edi
  20274. + movl 24(%esp), %ebp
  20275. + movb %dh, %cl
  20276. + andl $0xff, %eax
  20277. + andl $0xff, %edx
  20278. + movl 0x600+des_SPtrans(%ebx),%ebx
  20279. + xorl %ebx, %edi
  20280. + movl 0x700+des_SPtrans(%ecx),%ebx
  20281. + xorl %ebx, %edi
  20282. + movl 0x400+des_SPtrans(%eax),%ebx
  20283. + xorl %ebx, %edi
  20284. + movl 0x500+des_SPtrans(%edx),%ebx
  20285. + xorl %ebx, %edi
  20286. +
  20287. +
  20288. + movl 72(%ebp), %eax
  20289. + xorl %ebx, %ebx
  20290. + movl 76(%ebp), %edx
  20291. + xorl %edi, %eax
  20292. + xorl %edi, %edx
  20293. + andl $0xfcfcfcfc, %eax
  20294. + andl $0xcfcfcfcf, %edx
  20295. + movb %al, %bl
  20296. + movb %ah, %cl
  20297. + rorl $4, %edx
  20298. + movl des_SPtrans(%ebx),%ebp
  20299. + movb %dl, %bl
  20300. + xorl %ebp, %esi
  20301. + movl 0x200+des_SPtrans(%ecx),%ebp
  20302. + xorl %ebp, %esi
  20303. + movb %dh, %cl
  20304. + shrl $16, %eax
  20305. + movl 0x100+des_SPtrans(%ebx),%ebp
  20306. + xorl %ebp, %esi
  20307. + movb %ah, %bl
  20308. + shrl $16, %edx
  20309. + movl 0x300+des_SPtrans(%ecx),%ebp
  20310. + xorl %ebp, %esi
  20311. + movl 24(%esp), %ebp
  20312. + movb %dh, %cl
  20313. + andl $0xff, %eax
  20314. + andl $0xff, %edx
  20315. + movl 0x600+des_SPtrans(%ebx),%ebx
  20316. + xorl %ebx, %esi
  20317. + movl 0x700+des_SPtrans(%ecx),%ebx
  20318. + xorl %ebx, %esi
  20319. + movl 0x400+des_SPtrans(%eax),%ebx
  20320. + xorl %ebx, %esi
  20321. + movl 0x500+des_SPtrans(%edx),%ebx
  20322. + xorl %ebx, %esi
  20323. +
  20324. +
  20325. + movl 80(%ebp), %eax
  20326. + xorl %ebx, %ebx
  20327. + movl 84(%ebp), %edx
  20328. + xorl %esi, %eax
  20329. + xorl %esi, %edx
  20330. + andl $0xfcfcfcfc, %eax
  20331. + andl $0xcfcfcfcf, %edx
  20332. + movb %al, %bl
  20333. + movb %ah, %cl
  20334. + rorl $4, %edx
  20335. + movl des_SPtrans(%ebx),%ebp
  20336. + movb %dl, %bl
  20337. + xorl %ebp, %edi
  20338. + movl 0x200+des_SPtrans(%ecx),%ebp
  20339. + xorl %ebp, %edi
  20340. + movb %dh, %cl
  20341. + shrl $16, %eax
  20342. + movl 0x100+des_SPtrans(%ebx),%ebp
  20343. + xorl %ebp, %edi
  20344. + movb %ah, %bl
  20345. + shrl $16, %edx
  20346. + movl 0x300+des_SPtrans(%ecx),%ebp
  20347. + xorl %ebp, %edi
  20348. + movl 24(%esp), %ebp
  20349. + movb %dh, %cl
  20350. + andl $0xff, %eax
  20351. + andl $0xff, %edx
  20352. + movl 0x600+des_SPtrans(%ebx),%ebx
  20353. + xorl %ebx, %edi
  20354. + movl 0x700+des_SPtrans(%ecx),%ebx
  20355. + xorl %ebx, %edi
  20356. + movl 0x400+des_SPtrans(%eax),%ebx
  20357. + xorl %ebx, %edi
  20358. + movl 0x500+des_SPtrans(%edx),%ebx
  20359. + xorl %ebx, %edi
  20360. +
  20361. +
  20362. + movl 88(%ebp), %eax
  20363. + xorl %ebx, %ebx
  20364. + movl 92(%ebp), %edx
  20365. + xorl %edi, %eax
  20366. + xorl %edi, %edx
  20367. + andl $0xfcfcfcfc, %eax
  20368. + andl $0xcfcfcfcf, %edx
  20369. + movb %al, %bl
  20370. + movb %ah, %cl
  20371. + rorl $4, %edx
  20372. + movl des_SPtrans(%ebx),%ebp
  20373. + movb %dl, %bl
  20374. + xorl %ebp, %esi
  20375. + movl 0x200+des_SPtrans(%ecx),%ebp
  20376. + xorl %ebp, %esi
  20377. + movb %dh, %cl
  20378. + shrl $16, %eax
  20379. + movl 0x100+des_SPtrans(%ebx),%ebp
  20380. + xorl %ebp, %esi
  20381. + movb %ah, %bl
  20382. + shrl $16, %edx
  20383. + movl 0x300+des_SPtrans(%ecx),%ebp
  20384. + xorl %ebp, %esi
  20385. + movl 24(%esp), %ebp
  20386. + movb %dh, %cl
  20387. + andl $0xff, %eax
  20388. + andl $0xff, %edx
  20389. + movl 0x600+des_SPtrans(%ebx),%ebx
  20390. + xorl %ebx, %esi
  20391. + movl 0x700+des_SPtrans(%ecx),%ebx
  20392. + xorl %ebx, %esi
  20393. + movl 0x400+des_SPtrans(%eax),%ebx
  20394. + xorl %ebx, %esi
  20395. + movl 0x500+des_SPtrans(%edx),%ebx
  20396. + xorl %ebx, %esi
  20397. +
  20398. +
  20399. + movl 96(%ebp), %eax
  20400. + xorl %ebx, %ebx
  20401. + movl 100(%ebp), %edx
  20402. + xorl %esi, %eax
  20403. + xorl %esi, %edx
  20404. + andl $0xfcfcfcfc, %eax
  20405. + andl $0xcfcfcfcf, %edx
  20406. + movb %al, %bl
  20407. + movb %ah, %cl
  20408. + rorl $4, %edx
  20409. + movl des_SPtrans(%ebx),%ebp
  20410. + movb %dl, %bl
  20411. + xorl %ebp, %edi
  20412. + movl 0x200+des_SPtrans(%ecx),%ebp
  20413. + xorl %ebp, %edi
  20414. + movb %dh, %cl
  20415. + shrl $16, %eax
  20416. + movl 0x100+des_SPtrans(%ebx),%ebp
  20417. + xorl %ebp, %edi
  20418. + movb %ah, %bl
  20419. + shrl $16, %edx
  20420. + movl 0x300+des_SPtrans(%ecx),%ebp
  20421. + xorl %ebp, %edi
  20422. + movl 24(%esp), %ebp
  20423. + movb %dh, %cl
  20424. + andl $0xff, %eax
  20425. + andl $0xff, %edx
  20426. + movl 0x600+des_SPtrans(%ebx),%ebx
  20427. + xorl %ebx, %edi
  20428. + movl 0x700+des_SPtrans(%ecx),%ebx
  20429. + xorl %ebx, %edi
  20430. + movl 0x400+des_SPtrans(%eax),%ebx
  20431. + xorl %ebx, %edi
  20432. + movl 0x500+des_SPtrans(%edx),%ebx
  20433. + xorl %ebx, %edi
  20434. +
  20435. +
  20436. + movl 104(%ebp), %eax
  20437. + xorl %ebx, %ebx
  20438. + movl 108(%ebp), %edx
  20439. + xorl %edi, %eax
  20440. + xorl %edi, %edx
  20441. + andl $0xfcfcfcfc, %eax
  20442. + andl $0xcfcfcfcf, %edx
  20443. + movb %al, %bl
  20444. + movb %ah, %cl
  20445. + rorl $4, %edx
  20446. + movl des_SPtrans(%ebx),%ebp
  20447. + movb %dl, %bl
  20448. + xorl %ebp, %esi
  20449. + movl 0x200+des_SPtrans(%ecx),%ebp
  20450. + xorl %ebp, %esi
  20451. + movb %dh, %cl
  20452. + shrl $16, %eax
  20453. + movl 0x100+des_SPtrans(%ebx),%ebp
  20454. + xorl %ebp, %esi
  20455. + movb %ah, %bl
  20456. + shrl $16, %edx
  20457. + movl 0x300+des_SPtrans(%ecx),%ebp
  20458. + xorl %ebp, %esi
  20459. + movl 24(%esp), %ebp
  20460. + movb %dh, %cl
  20461. + andl $0xff, %eax
  20462. + andl $0xff, %edx
  20463. + movl 0x600+des_SPtrans(%ebx),%ebx
  20464. + xorl %ebx, %esi
  20465. + movl 0x700+des_SPtrans(%ecx),%ebx
  20466. + xorl %ebx, %esi
  20467. + movl 0x400+des_SPtrans(%eax),%ebx
  20468. + xorl %ebx, %esi
  20469. + movl 0x500+des_SPtrans(%edx),%ebx
  20470. + xorl %ebx, %esi
  20471. +
  20472. +
  20473. + movl 112(%ebp), %eax
  20474. + xorl %ebx, %ebx
  20475. + movl 116(%ebp), %edx
  20476. + xorl %esi, %eax
  20477. + xorl %esi, %edx
  20478. + andl $0xfcfcfcfc, %eax
  20479. + andl $0xcfcfcfcf, %edx
  20480. + movb %al, %bl
  20481. + movb %ah, %cl
  20482. + rorl $4, %edx
  20483. + movl des_SPtrans(%ebx),%ebp
  20484. + movb %dl, %bl
  20485. + xorl %ebp, %edi
  20486. + movl 0x200+des_SPtrans(%ecx),%ebp
  20487. + xorl %ebp, %edi
  20488. + movb %dh, %cl
  20489. + shrl $16, %eax
  20490. + movl 0x100+des_SPtrans(%ebx),%ebp
  20491. + xorl %ebp, %edi
  20492. + movb %ah, %bl
  20493. + shrl $16, %edx
  20494. + movl 0x300+des_SPtrans(%ecx),%ebp
  20495. + xorl %ebp, %edi
  20496. + movl 24(%esp), %ebp
  20497. + movb %dh, %cl
  20498. + andl $0xff, %eax
  20499. + andl $0xff, %edx
  20500. + movl 0x600+des_SPtrans(%ebx),%ebx
  20501. + xorl %ebx, %edi
  20502. + movl 0x700+des_SPtrans(%ecx),%ebx
  20503. + xorl %ebx, %edi
  20504. + movl 0x400+des_SPtrans(%eax),%ebx
  20505. + xorl %ebx, %edi
  20506. + movl 0x500+des_SPtrans(%edx),%ebx
  20507. + xorl %ebx, %edi
  20508. +
  20509. +
  20510. + movl 120(%ebp), %eax
  20511. + xorl %ebx, %ebx
  20512. + movl 124(%ebp), %edx
  20513. + xorl %edi, %eax
  20514. + xorl %edi, %edx
  20515. + andl $0xfcfcfcfc, %eax
  20516. + andl $0xcfcfcfcf, %edx
  20517. + movb %al, %bl
  20518. + movb %ah, %cl
  20519. + rorl $4, %edx
  20520. + movl des_SPtrans(%ebx),%ebp
  20521. + movb %dl, %bl
  20522. + xorl %ebp, %esi
  20523. + movl 0x200+des_SPtrans(%ecx),%ebp
  20524. + xorl %ebp, %esi
  20525. + movb %dh, %cl
  20526. + shrl $16, %eax
  20527. + movl 0x100+des_SPtrans(%ebx),%ebp
  20528. + xorl %ebp, %esi
  20529. + movb %ah, %bl
  20530. + shrl $16, %edx
  20531. + movl 0x300+des_SPtrans(%ecx),%ebp
  20532. + xorl %ebp, %esi
  20533. + movl 24(%esp), %ebp
  20534. + movb %dh, %cl
  20535. + andl $0xff, %eax
  20536. + andl $0xff, %edx
  20537. + movl 0x600+des_SPtrans(%ebx),%ebx
  20538. + xorl %ebx, %esi
  20539. + movl 0x700+des_SPtrans(%ecx),%ebx
  20540. + xorl %ebx, %esi
  20541. + movl 0x400+des_SPtrans(%eax),%ebx
  20542. + xorl %ebx, %esi
  20543. + movl 0x500+des_SPtrans(%edx),%ebx
  20544. + xorl %ebx, %esi
  20545. + jmp .L001end
  20546. +.L000start_decrypt:
  20547. +
  20548. +
  20549. + movl 120(%ebp), %eax
  20550. + xorl %ebx, %ebx
  20551. + movl 124(%ebp), %edx
  20552. + xorl %esi, %eax
  20553. + xorl %esi, %edx
  20554. + andl $0xfcfcfcfc, %eax
  20555. + andl $0xcfcfcfcf, %edx
  20556. + movb %al, %bl
  20557. + movb %ah, %cl
  20558. + rorl $4, %edx
  20559. + movl des_SPtrans(%ebx),%ebp
  20560. + movb %dl, %bl
  20561. + xorl %ebp, %edi
  20562. + movl 0x200+des_SPtrans(%ecx),%ebp
  20563. + xorl %ebp, %edi
  20564. + movb %dh, %cl
  20565. + shrl $16, %eax
  20566. + movl 0x100+des_SPtrans(%ebx),%ebp
  20567. + xorl %ebp, %edi
  20568. + movb %ah, %bl
  20569. + shrl $16, %edx
  20570. + movl 0x300+des_SPtrans(%ecx),%ebp
  20571. + xorl %ebp, %edi
  20572. + movl 24(%esp), %ebp
  20573. + movb %dh, %cl
  20574. + andl $0xff, %eax
  20575. + andl $0xff, %edx
  20576. + movl 0x600+des_SPtrans(%ebx),%ebx
  20577. + xorl %ebx, %edi
  20578. + movl 0x700+des_SPtrans(%ecx),%ebx
  20579. + xorl %ebx, %edi
  20580. + movl 0x400+des_SPtrans(%eax),%ebx
  20581. + xorl %ebx, %edi
  20582. + movl 0x500+des_SPtrans(%edx),%ebx
  20583. + xorl %ebx, %edi
  20584. +
  20585. +
  20586. + movl 112(%ebp), %eax
  20587. + xorl %ebx, %ebx
  20588. + movl 116(%ebp), %edx
  20589. + xorl %edi, %eax
  20590. + xorl %edi, %edx
  20591. + andl $0xfcfcfcfc, %eax
  20592. + andl $0xcfcfcfcf, %edx
  20593. + movb %al, %bl
  20594. + movb %ah, %cl
  20595. + rorl $4, %edx
  20596. + movl des_SPtrans(%ebx),%ebp
  20597. + movb %dl, %bl
  20598. + xorl %ebp, %esi
  20599. + movl 0x200+des_SPtrans(%ecx),%ebp
  20600. + xorl %ebp, %esi
  20601. + movb %dh, %cl
  20602. + shrl $16, %eax
  20603. + movl 0x100+des_SPtrans(%ebx),%ebp
  20604. + xorl %ebp, %esi
  20605. + movb %ah, %bl
  20606. + shrl $16, %edx
  20607. + movl 0x300+des_SPtrans(%ecx),%ebp
  20608. + xorl %ebp, %esi
  20609. + movl 24(%esp), %ebp
  20610. + movb %dh, %cl
  20611. + andl $0xff, %eax
  20612. + andl $0xff, %edx
  20613. + movl 0x600+des_SPtrans(%ebx),%ebx
  20614. + xorl %ebx, %esi
  20615. + movl 0x700+des_SPtrans(%ecx),%ebx
  20616. + xorl %ebx, %esi
  20617. + movl 0x400+des_SPtrans(%eax),%ebx
  20618. + xorl %ebx, %esi
  20619. + movl 0x500+des_SPtrans(%edx),%ebx
  20620. + xorl %ebx, %esi
  20621. +
  20622. +
  20623. + movl 104(%ebp), %eax
  20624. + xorl %ebx, %ebx
  20625. + movl 108(%ebp), %edx
  20626. + xorl %esi, %eax
  20627. + xorl %esi, %edx
  20628. + andl $0xfcfcfcfc, %eax
  20629. + andl $0xcfcfcfcf, %edx
  20630. + movb %al, %bl
  20631. + movb %ah, %cl
  20632. + rorl $4, %edx
  20633. + movl des_SPtrans(%ebx),%ebp
  20634. + movb %dl, %bl
  20635. + xorl %ebp, %edi
  20636. + movl 0x200+des_SPtrans(%ecx),%ebp
  20637. + xorl %ebp, %edi
  20638. + movb %dh, %cl
  20639. + shrl $16, %eax
  20640. + movl 0x100+des_SPtrans(%ebx),%ebp
  20641. + xorl %ebp, %edi
  20642. + movb %ah, %bl
  20643. + shrl $16, %edx
  20644. + movl 0x300+des_SPtrans(%ecx),%ebp
  20645. + xorl %ebp, %edi
  20646. + movl 24(%esp), %ebp
  20647. + movb %dh, %cl
  20648. + andl $0xff, %eax
  20649. + andl $0xff, %edx
  20650. + movl 0x600+des_SPtrans(%ebx),%ebx
  20651. + xorl %ebx, %edi
  20652. + movl 0x700+des_SPtrans(%ecx),%ebx
  20653. + xorl %ebx, %edi
  20654. + movl 0x400+des_SPtrans(%eax),%ebx
  20655. + xorl %ebx, %edi
  20656. + movl 0x500+des_SPtrans(%edx),%ebx
  20657. + xorl %ebx, %edi
  20658. +
  20659. +
  20660. + movl 96(%ebp), %eax
  20661. + xorl %ebx, %ebx
  20662. + movl 100(%ebp), %edx
  20663. + xorl %edi, %eax
  20664. + xorl %edi, %edx
  20665. + andl $0xfcfcfcfc, %eax
  20666. + andl $0xcfcfcfcf, %edx
  20667. + movb %al, %bl
  20668. + movb %ah, %cl
  20669. + rorl $4, %edx
  20670. + movl des_SPtrans(%ebx),%ebp
  20671. + movb %dl, %bl
  20672. + xorl %ebp, %esi
  20673. + movl 0x200+des_SPtrans(%ecx),%ebp
  20674. + xorl %ebp, %esi
  20675. + movb %dh, %cl
  20676. + shrl $16, %eax
  20677. + movl 0x100+des_SPtrans(%ebx),%ebp
  20678. + xorl %ebp, %esi
  20679. + movb %ah, %bl
  20680. + shrl $16, %edx
  20681. + movl 0x300+des_SPtrans(%ecx),%ebp
  20682. + xorl %ebp, %esi
  20683. + movl 24(%esp), %ebp
  20684. + movb %dh, %cl
  20685. + andl $0xff, %eax
  20686. + andl $0xff, %edx
  20687. + movl 0x600+des_SPtrans(%ebx),%ebx
  20688. + xorl %ebx, %esi
  20689. + movl 0x700+des_SPtrans(%ecx),%ebx
  20690. + xorl %ebx, %esi
  20691. + movl 0x400+des_SPtrans(%eax),%ebx
  20692. + xorl %ebx, %esi
  20693. + movl 0x500+des_SPtrans(%edx),%ebx
  20694. + xorl %ebx, %esi
  20695. +
  20696. +
  20697. + movl 88(%ebp), %eax
  20698. + xorl %ebx, %ebx
  20699. + movl 92(%ebp), %edx
  20700. + xorl %esi, %eax
  20701. + xorl %esi, %edx
  20702. + andl $0xfcfcfcfc, %eax
  20703. + andl $0xcfcfcfcf, %edx
  20704. + movb %al, %bl
  20705. + movb %ah, %cl
  20706. + rorl $4, %edx
  20707. + movl des_SPtrans(%ebx),%ebp
  20708. + movb %dl, %bl
  20709. + xorl %ebp, %edi
  20710. + movl 0x200+des_SPtrans(%ecx),%ebp
  20711. + xorl %ebp, %edi
  20712. + movb %dh, %cl
  20713. + shrl $16, %eax
  20714. + movl 0x100+des_SPtrans(%ebx),%ebp
  20715. + xorl %ebp, %edi
  20716. + movb %ah, %bl
  20717. + shrl $16, %edx
  20718. + movl 0x300+des_SPtrans(%ecx),%ebp
  20719. + xorl %ebp, %edi
  20720. + movl 24(%esp), %ebp
  20721. + movb %dh, %cl
  20722. + andl $0xff, %eax
  20723. + andl $0xff, %edx
  20724. + movl 0x600+des_SPtrans(%ebx),%ebx
  20725. + xorl %ebx, %edi
  20726. + movl 0x700+des_SPtrans(%ecx),%ebx
  20727. + xorl %ebx, %edi
  20728. + movl 0x400+des_SPtrans(%eax),%ebx
  20729. + xorl %ebx, %edi
  20730. + movl 0x500+des_SPtrans(%edx),%ebx
  20731. + xorl %ebx, %edi
  20732. +
  20733. +
  20734. + movl 80(%ebp), %eax
  20735. + xorl %ebx, %ebx
  20736. + movl 84(%ebp), %edx
  20737. + xorl %edi, %eax
  20738. + xorl %edi, %edx
  20739. + andl $0xfcfcfcfc, %eax
  20740. + andl $0xcfcfcfcf, %edx
  20741. + movb %al, %bl
  20742. + movb %ah, %cl
  20743. + rorl $4, %edx
  20744. + movl des_SPtrans(%ebx),%ebp
  20745. + movb %dl, %bl
  20746. + xorl %ebp, %esi
  20747. + movl 0x200+des_SPtrans(%ecx),%ebp
  20748. + xorl %ebp, %esi
  20749. + movb %dh, %cl
  20750. + shrl $16, %eax
  20751. + movl 0x100+des_SPtrans(%ebx),%ebp
  20752. + xorl %ebp, %esi
  20753. + movb %ah, %bl
  20754. + shrl $16, %edx
  20755. + movl 0x300+des_SPtrans(%ecx),%ebp
  20756. + xorl %ebp, %esi
  20757. + movl 24(%esp), %ebp
  20758. + movb %dh, %cl
  20759. + andl $0xff, %eax
  20760. + andl $0xff, %edx
  20761. + movl 0x600+des_SPtrans(%ebx),%ebx
  20762. + xorl %ebx, %esi
  20763. + movl 0x700+des_SPtrans(%ecx),%ebx
  20764. + xorl %ebx, %esi
  20765. + movl 0x400+des_SPtrans(%eax),%ebx
  20766. + xorl %ebx, %esi
  20767. + movl 0x500+des_SPtrans(%edx),%ebx
  20768. + xorl %ebx, %esi
  20769. +
  20770. +
  20771. + movl 72(%ebp), %eax
  20772. + xorl %ebx, %ebx
  20773. + movl 76(%ebp), %edx
  20774. + xorl %esi, %eax
  20775. + xorl %esi, %edx
  20776. + andl $0xfcfcfcfc, %eax
  20777. + andl $0xcfcfcfcf, %edx
  20778. + movb %al, %bl
  20779. + movb %ah, %cl
  20780. + rorl $4, %edx
  20781. + movl des_SPtrans(%ebx),%ebp
  20782. + movb %dl, %bl
  20783. + xorl %ebp, %edi
  20784. + movl 0x200+des_SPtrans(%ecx),%ebp
  20785. + xorl %ebp, %edi
  20786. + movb %dh, %cl
  20787. + shrl $16, %eax
  20788. + movl 0x100+des_SPtrans(%ebx),%ebp
  20789. + xorl %ebp, %edi
  20790. + movb %ah, %bl
  20791. + shrl $16, %edx
  20792. + movl 0x300+des_SPtrans(%ecx),%ebp
  20793. + xorl %ebp, %edi
  20794. + movl 24(%esp), %ebp
  20795. + movb %dh, %cl
  20796. + andl $0xff, %eax
  20797. + andl $0xff, %edx
  20798. + movl 0x600+des_SPtrans(%ebx),%ebx
  20799. + xorl %ebx, %edi
  20800. + movl 0x700+des_SPtrans(%ecx),%ebx
  20801. + xorl %ebx, %edi
  20802. + movl 0x400+des_SPtrans(%eax),%ebx
  20803. + xorl %ebx, %edi
  20804. + movl 0x500+des_SPtrans(%edx),%ebx
  20805. + xorl %ebx, %edi
  20806. +
  20807. +
  20808. + movl 64(%ebp), %eax
  20809. + xorl %ebx, %ebx
  20810. + movl 68(%ebp), %edx
  20811. + xorl %edi, %eax
  20812. + xorl %edi, %edx
  20813. + andl $0xfcfcfcfc, %eax
  20814. + andl $0xcfcfcfcf, %edx
  20815. + movb %al, %bl
  20816. + movb %ah, %cl
  20817. + rorl $4, %edx
  20818. + movl des_SPtrans(%ebx),%ebp
  20819. + movb %dl, %bl
  20820. + xorl %ebp, %esi
  20821. + movl 0x200+des_SPtrans(%ecx),%ebp
  20822. + xorl %ebp, %esi
  20823. + movb %dh, %cl
  20824. + shrl $16, %eax
  20825. + movl 0x100+des_SPtrans(%ebx),%ebp
  20826. + xorl %ebp, %esi
  20827. + movb %ah, %bl
  20828. + shrl $16, %edx
  20829. + movl 0x300+des_SPtrans(%ecx),%ebp
  20830. + xorl %ebp, %esi
  20831. + movl 24(%esp), %ebp
  20832. + movb %dh, %cl
  20833. + andl $0xff, %eax
  20834. + andl $0xff, %edx
  20835. + movl 0x600+des_SPtrans(%ebx),%ebx
  20836. + xorl %ebx, %esi
  20837. + movl 0x700+des_SPtrans(%ecx),%ebx
  20838. + xorl %ebx, %esi
  20839. + movl 0x400+des_SPtrans(%eax),%ebx
  20840. + xorl %ebx, %esi
  20841. + movl 0x500+des_SPtrans(%edx),%ebx
  20842. + xorl %ebx, %esi
  20843. +
  20844. +
  20845. + movl 56(%ebp), %eax
  20846. + xorl %ebx, %ebx
  20847. + movl 60(%ebp), %edx
  20848. + xorl %esi, %eax
  20849. + xorl %esi, %edx
  20850. + andl $0xfcfcfcfc, %eax
  20851. + andl $0xcfcfcfcf, %edx
  20852. + movb %al, %bl
  20853. + movb %ah, %cl
  20854. + rorl $4, %edx
  20855. + movl des_SPtrans(%ebx),%ebp
  20856. + movb %dl, %bl
  20857. + xorl %ebp, %edi
  20858. + movl 0x200+des_SPtrans(%ecx),%ebp
  20859. + xorl %ebp, %edi
  20860. + movb %dh, %cl
  20861. + shrl $16, %eax
  20862. + movl 0x100+des_SPtrans(%ebx),%ebp
  20863. + xorl %ebp, %edi
  20864. + movb %ah, %bl
  20865. + shrl $16, %edx
  20866. + movl 0x300+des_SPtrans(%ecx),%ebp
  20867. + xorl %ebp, %edi
  20868. + movl 24(%esp), %ebp
  20869. + movb %dh, %cl
  20870. + andl $0xff, %eax
  20871. + andl $0xff, %edx
  20872. + movl 0x600+des_SPtrans(%ebx),%ebx
  20873. + xorl %ebx, %edi
  20874. + movl 0x700+des_SPtrans(%ecx),%ebx
  20875. + xorl %ebx, %edi
  20876. + movl 0x400+des_SPtrans(%eax),%ebx
  20877. + xorl %ebx, %edi
  20878. + movl 0x500+des_SPtrans(%edx),%ebx
  20879. + xorl %ebx, %edi
  20880. +
  20881. +
  20882. + movl 48(%ebp), %eax
  20883. + xorl %ebx, %ebx
  20884. + movl 52(%ebp), %edx
  20885. + xorl %edi, %eax
  20886. + xorl %edi, %edx
  20887. + andl $0xfcfcfcfc, %eax
  20888. + andl $0xcfcfcfcf, %edx
  20889. + movb %al, %bl
  20890. + movb %ah, %cl
  20891. + rorl $4, %edx
  20892. + movl des_SPtrans(%ebx),%ebp
  20893. + movb %dl, %bl
  20894. + xorl %ebp, %esi
  20895. + movl 0x200+des_SPtrans(%ecx),%ebp
  20896. + xorl %ebp, %esi
  20897. + movb %dh, %cl
  20898. + shrl $16, %eax
  20899. + movl 0x100+des_SPtrans(%ebx),%ebp
  20900. + xorl %ebp, %esi
  20901. + movb %ah, %bl
  20902. + shrl $16, %edx
  20903. + movl 0x300+des_SPtrans(%ecx),%ebp
  20904. + xorl %ebp, %esi
  20905. + movl 24(%esp), %ebp
  20906. + movb %dh, %cl
  20907. + andl $0xff, %eax
  20908. + andl $0xff, %edx
  20909. + movl 0x600+des_SPtrans(%ebx),%ebx
  20910. + xorl %ebx, %esi
  20911. + movl 0x700+des_SPtrans(%ecx),%ebx
  20912. + xorl %ebx, %esi
  20913. + movl 0x400+des_SPtrans(%eax),%ebx
  20914. + xorl %ebx, %esi
  20915. + movl 0x500+des_SPtrans(%edx),%ebx
  20916. + xorl %ebx, %esi
  20917. +
  20918. +
  20919. + movl 40(%ebp), %eax
  20920. + xorl %ebx, %ebx
  20921. + movl 44(%ebp), %edx
  20922. + xorl %esi, %eax
  20923. + xorl %esi, %edx
  20924. + andl $0xfcfcfcfc, %eax
  20925. + andl $0xcfcfcfcf, %edx
  20926. + movb %al, %bl
  20927. + movb %ah, %cl
  20928. + rorl $4, %edx
  20929. + movl des_SPtrans(%ebx),%ebp
  20930. + movb %dl, %bl
  20931. + xorl %ebp, %edi
  20932. + movl 0x200+des_SPtrans(%ecx),%ebp
  20933. + xorl %ebp, %edi
  20934. + movb %dh, %cl
  20935. + shrl $16, %eax
  20936. + movl 0x100+des_SPtrans(%ebx),%ebp
  20937. + xorl %ebp, %edi
  20938. + movb %ah, %bl
  20939. + shrl $16, %edx
  20940. + movl 0x300+des_SPtrans(%ecx),%ebp
  20941. + xorl %ebp, %edi
  20942. + movl 24(%esp), %ebp
  20943. + movb %dh, %cl
  20944. + andl $0xff, %eax
  20945. + andl $0xff, %edx
  20946. + movl 0x600+des_SPtrans(%ebx),%ebx
  20947. + xorl %ebx, %edi
  20948. + movl 0x700+des_SPtrans(%ecx),%ebx
  20949. + xorl %ebx, %edi
  20950. + movl 0x400+des_SPtrans(%eax),%ebx
  20951. + xorl %ebx, %edi
  20952. + movl 0x500+des_SPtrans(%edx),%ebx
  20953. + xorl %ebx, %edi
  20954. +
  20955. +
  20956. + movl 32(%ebp), %eax
  20957. + xorl %ebx, %ebx
  20958. + movl 36(%ebp), %edx
  20959. + xorl %edi, %eax
  20960. + xorl %edi, %edx
  20961. + andl $0xfcfcfcfc, %eax
  20962. + andl $0xcfcfcfcf, %edx
  20963. + movb %al, %bl
  20964. + movb %ah, %cl
  20965. + rorl $4, %edx
  20966. + movl des_SPtrans(%ebx),%ebp
  20967. + movb %dl, %bl
  20968. + xorl %ebp, %esi
  20969. + movl 0x200+des_SPtrans(%ecx),%ebp
  20970. + xorl %ebp, %esi
  20971. + movb %dh, %cl
  20972. + shrl $16, %eax
  20973. + movl 0x100+des_SPtrans(%ebx),%ebp
  20974. + xorl %ebp, %esi
  20975. + movb %ah, %bl
  20976. + shrl $16, %edx
  20977. + movl 0x300+des_SPtrans(%ecx),%ebp
  20978. + xorl %ebp, %esi
  20979. + movl 24(%esp), %ebp
  20980. + movb %dh, %cl
  20981. + andl $0xff, %eax
  20982. + andl $0xff, %edx
  20983. + movl 0x600+des_SPtrans(%ebx),%ebx
  20984. + xorl %ebx, %esi
  20985. + movl 0x700+des_SPtrans(%ecx),%ebx
  20986. + xorl %ebx, %esi
  20987. + movl 0x400+des_SPtrans(%eax),%ebx
  20988. + xorl %ebx, %esi
  20989. + movl 0x500+des_SPtrans(%edx),%ebx
  20990. + xorl %ebx, %esi
  20991. +
  20992. +
  20993. + movl 24(%ebp), %eax
  20994. + xorl %ebx, %ebx
  20995. + movl 28(%ebp), %edx
  20996. + xorl %esi, %eax
  20997. + xorl %esi, %edx
  20998. + andl $0xfcfcfcfc, %eax
  20999. + andl $0xcfcfcfcf, %edx
  21000. + movb %al, %bl
  21001. + movb %ah, %cl
  21002. + rorl $4, %edx
  21003. + movl des_SPtrans(%ebx),%ebp
  21004. + movb %dl, %bl
  21005. + xorl %ebp, %edi
  21006. + movl 0x200+des_SPtrans(%ecx),%ebp
  21007. + xorl %ebp, %edi
  21008. + movb %dh, %cl
  21009. + shrl $16, %eax
  21010. + movl 0x100+des_SPtrans(%ebx),%ebp
  21011. + xorl %ebp, %edi
  21012. + movb %ah, %bl
  21013. + shrl $16, %edx
  21014. + movl 0x300+des_SPtrans(%ecx),%ebp
  21015. + xorl %ebp, %edi
  21016. + movl 24(%esp), %ebp
  21017. + movb %dh, %cl
  21018. + andl $0xff, %eax
  21019. + andl $0xff, %edx
  21020. + movl 0x600+des_SPtrans(%ebx),%ebx
  21021. + xorl %ebx, %edi
  21022. + movl 0x700+des_SPtrans(%ecx),%ebx
  21023. + xorl %ebx, %edi
  21024. + movl 0x400+des_SPtrans(%eax),%ebx
  21025. + xorl %ebx, %edi
  21026. + movl 0x500+des_SPtrans(%edx),%ebx
  21027. + xorl %ebx, %edi
  21028. +
  21029. +
  21030. + movl 16(%ebp), %eax
  21031. + xorl %ebx, %ebx
  21032. + movl 20(%ebp), %edx
  21033. + xorl %edi, %eax
  21034. + xorl %edi, %edx
  21035. + andl $0xfcfcfcfc, %eax
  21036. + andl $0xcfcfcfcf, %edx
  21037. + movb %al, %bl
  21038. + movb %ah, %cl
  21039. + rorl $4, %edx
  21040. + movl des_SPtrans(%ebx),%ebp
  21041. + movb %dl, %bl
  21042. + xorl %ebp, %esi
  21043. + movl 0x200+des_SPtrans(%ecx),%ebp
  21044. + xorl %ebp, %esi
  21045. + movb %dh, %cl
  21046. + shrl $16, %eax
  21047. + movl 0x100+des_SPtrans(%ebx),%ebp
  21048. + xorl %ebp, %esi
  21049. + movb %ah, %bl
  21050. + shrl $16, %edx
  21051. + movl 0x300+des_SPtrans(%ecx),%ebp
  21052. + xorl %ebp, %esi
  21053. + movl 24(%esp), %ebp
  21054. + movb %dh, %cl
  21055. + andl $0xff, %eax
  21056. + andl $0xff, %edx
  21057. + movl 0x600+des_SPtrans(%ebx),%ebx
  21058. + xorl %ebx, %esi
  21059. + movl 0x700+des_SPtrans(%ecx),%ebx
  21060. + xorl %ebx, %esi
  21061. + movl 0x400+des_SPtrans(%eax),%ebx
  21062. + xorl %ebx, %esi
  21063. + movl 0x500+des_SPtrans(%edx),%ebx
  21064. + xorl %ebx, %esi
  21065. +
  21066. +
  21067. + movl 8(%ebp), %eax
  21068. + xorl %ebx, %ebx
  21069. + movl 12(%ebp), %edx
  21070. + xorl %esi, %eax
  21071. + xorl %esi, %edx
  21072. + andl $0xfcfcfcfc, %eax
  21073. + andl $0xcfcfcfcf, %edx
  21074. + movb %al, %bl
  21075. + movb %ah, %cl
  21076. + rorl $4, %edx
  21077. + movl des_SPtrans(%ebx),%ebp
  21078. + movb %dl, %bl
  21079. + xorl %ebp, %edi
  21080. + movl 0x200+des_SPtrans(%ecx),%ebp
  21081. + xorl %ebp, %edi
  21082. + movb %dh, %cl
  21083. + shrl $16, %eax
  21084. + movl 0x100+des_SPtrans(%ebx),%ebp
  21085. + xorl %ebp, %edi
  21086. + movb %ah, %bl
  21087. + shrl $16, %edx
  21088. + movl 0x300+des_SPtrans(%ecx),%ebp
  21089. + xorl %ebp, %edi
  21090. + movl 24(%esp), %ebp
  21091. + movb %dh, %cl
  21092. + andl $0xff, %eax
  21093. + andl $0xff, %edx
  21094. + movl 0x600+des_SPtrans(%ebx),%ebx
  21095. + xorl %ebx, %edi
  21096. + movl 0x700+des_SPtrans(%ecx),%ebx
  21097. + xorl %ebx, %edi
  21098. + movl 0x400+des_SPtrans(%eax),%ebx
  21099. + xorl %ebx, %edi
  21100. + movl 0x500+des_SPtrans(%edx),%ebx
  21101. + xorl %ebx, %edi
  21102. +
  21103. +
  21104. + movl (%ebp), %eax
  21105. + xorl %ebx, %ebx
  21106. + movl 4(%ebp), %edx
  21107. + xorl %edi, %eax
  21108. + xorl %edi, %edx
  21109. + andl $0xfcfcfcfc, %eax
  21110. + andl $0xcfcfcfcf, %edx
  21111. + movb %al, %bl
  21112. + movb %ah, %cl
  21113. + rorl $4, %edx
  21114. + movl des_SPtrans(%ebx),%ebp
  21115. + movb %dl, %bl
  21116. + xorl %ebp, %esi
  21117. + movl 0x200+des_SPtrans(%ecx),%ebp
  21118. + xorl %ebp, %esi
  21119. + movb %dh, %cl
  21120. + shrl $16, %eax
  21121. + movl 0x100+des_SPtrans(%ebx),%ebp
  21122. + xorl %ebp, %esi
  21123. + movb %ah, %bl
  21124. + shrl $16, %edx
  21125. + movl 0x300+des_SPtrans(%ecx),%ebp
  21126. + xorl %ebp, %esi
  21127. + movl 24(%esp), %ebp
  21128. + movb %dh, %cl
  21129. + andl $0xff, %eax
  21130. + andl $0xff, %edx
  21131. + movl 0x600+des_SPtrans(%ebx),%ebx
  21132. + xorl %ebx, %esi
  21133. + movl 0x700+des_SPtrans(%ecx),%ebx
  21134. + xorl %ebx, %esi
  21135. + movl 0x400+des_SPtrans(%eax),%ebx
  21136. + xorl %ebx, %esi
  21137. + movl 0x500+des_SPtrans(%edx),%ebx
  21138. + xorl %ebx, %esi
  21139. +.L001end:
  21140. +
  21141. +
  21142. + movl 20(%esp), %edx
  21143. +.byte 209
  21144. +.byte 206
  21145. + movl %edi, %eax
  21146. + xorl %esi, %edi
  21147. + andl $0xaaaaaaaa, %edi
  21148. + xorl %edi, %eax
  21149. + xorl %edi, %esi
  21150. +
  21151. + roll $23, %eax
  21152. + movl %eax, %edi
  21153. + xorl %esi, %eax
  21154. + andl $0x03fc03fc, %eax
  21155. + xorl %eax, %edi
  21156. + xorl %eax, %esi
  21157. +
  21158. + roll $10, %edi
  21159. + movl %edi, %eax
  21160. + xorl %esi, %edi
  21161. + andl $0x33333333, %edi
  21162. + xorl %edi, %eax
  21163. + xorl %edi, %esi
  21164. +
  21165. + roll $18, %esi
  21166. + movl %esi, %edi
  21167. + xorl %eax, %esi
  21168. + andl $0xfff0000f, %esi
  21169. + xorl %esi, %edi
  21170. + xorl %esi, %eax
  21171. +
  21172. + roll $12, %edi
  21173. + movl %edi, %esi
  21174. + xorl %eax, %edi
  21175. + andl $0xf0f0f0f0, %edi
  21176. + xorl %edi, %esi
  21177. + xorl %edi, %eax
  21178. +
  21179. + rorl $4, %eax
  21180. + movl %eax, (%edx)
  21181. + movl %esi, 4(%edx)
  21182. + popl %ebp
  21183. + popl %ebx
  21184. + popl %edi
  21185. + popl %esi
  21186. + ret
  21187. +.des_encrypt_end:
  21188. + .size des_encrypt , .des_encrypt_end-des_encrypt
  21189. +.ident "desasm.pl"
  21190. +.text
  21191. + .align 16
  21192. +.globl des_encrypt2
  21193. + .type des_encrypt2 , @function
  21194. +des_encrypt2:
  21195. + pushl %esi
  21196. + pushl %edi
  21197. +
  21198. +
  21199. + movl 12(%esp), %eax
  21200. + xorl %ecx, %ecx
  21201. + pushl %ebx
  21202. + pushl %ebp
  21203. + movl (%eax), %esi
  21204. + movl 28(%esp), %ebx
  21205. + roll $3, %esi
  21206. + movl 4(%eax), %edi
  21207. + roll $3, %edi
  21208. + movl 24(%esp), %ebp
  21209. + cmpl $0, %ebx
  21210. + je .L002start_decrypt
  21211. +
  21212. +
  21213. + movl (%ebp), %eax
  21214. + xorl %ebx, %ebx
  21215. + movl 4(%ebp), %edx
  21216. + xorl %esi, %eax
  21217. + xorl %esi, %edx
  21218. + andl $0xfcfcfcfc, %eax
  21219. + andl $0xcfcfcfcf, %edx
  21220. + movb %al, %bl
  21221. + movb %ah, %cl
  21222. + rorl $4, %edx
  21223. + movl des_SPtrans(%ebx),%ebp
  21224. + movb %dl, %bl
  21225. + xorl %ebp, %edi
  21226. + movl 0x200+des_SPtrans(%ecx),%ebp
  21227. + xorl %ebp, %edi
  21228. + movb %dh, %cl
  21229. + shrl $16, %eax
  21230. + movl 0x100+des_SPtrans(%ebx),%ebp
  21231. + xorl %ebp, %edi
  21232. + movb %ah, %bl
  21233. + shrl $16, %edx
  21234. + movl 0x300+des_SPtrans(%ecx),%ebp
  21235. + xorl %ebp, %edi
  21236. + movl 24(%esp), %ebp
  21237. + movb %dh, %cl
  21238. + andl $0xff, %eax
  21239. + andl $0xff, %edx
  21240. + movl 0x600+des_SPtrans(%ebx),%ebx
  21241. + xorl %ebx, %edi
  21242. + movl 0x700+des_SPtrans(%ecx),%ebx
  21243. + xorl %ebx, %edi
  21244. + movl 0x400+des_SPtrans(%eax),%ebx
  21245. + xorl %ebx, %edi
  21246. + movl 0x500+des_SPtrans(%edx),%ebx
  21247. + xorl %ebx, %edi
  21248. +
  21249. +
  21250. + movl 8(%ebp), %eax
  21251. + xorl %ebx, %ebx
  21252. + movl 12(%ebp), %edx
  21253. + xorl %edi, %eax
  21254. + xorl %edi, %edx
  21255. + andl $0xfcfcfcfc, %eax
  21256. + andl $0xcfcfcfcf, %edx
  21257. + movb %al, %bl
  21258. + movb %ah, %cl
  21259. + rorl $4, %edx
  21260. + movl des_SPtrans(%ebx),%ebp
  21261. + movb %dl, %bl
  21262. + xorl %ebp, %esi
  21263. + movl 0x200+des_SPtrans(%ecx),%ebp
  21264. + xorl %ebp, %esi
  21265. + movb %dh, %cl
  21266. + shrl $16, %eax
  21267. + movl 0x100+des_SPtrans(%ebx),%ebp
  21268. + xorl %ebp, %esi
  21269. + movb %ah, %bl
  21270. + shrl $16, %edx
  21271. + movl 0x300+des_SPtrans(%ecx),%ebp
  21272. + xorl %ebp, %esi
  21273. + movl 24(%esp), %ebp
  21274. + movb %dh, %cl
  21275. + andl $0xff, %eax
  21276. + andl $0xff, %edx
  21277. + movl 0x600+des_SPtrans(%ebx),%ebx
  21278. + xorl %ebx, %esi
  21279. + movl 0x700+des_SPtrans(%ecx),%ebx
  21280. + xorl %ebx, %esi
  21281. + movl 0x400+des_SPtrans(%eax),%ebx
  21282. + xorl %ebx, %esi
  21283. + movl 0x500+des_SPtrans(%edx),%ebx
  21284. + xorl %ebx, %esi
  21285. +
  21286. +
  21287. + movl 16(%ebp), %eax
  21288. + xorl %ebx, %ebx
  21289. + movl 20(%ebp), %edx
  21290. + xorl %esi, %eax
  21291. + xorl %esi, %edx
  21292. + andl $0xfcfcfcfc, %eax
  21293. + andl $0xcfcfcfcf, %edx
  21294. + movb %al, %bl
  21295. + movb %ah, %cl
  21296. + rorl $4, %edx
  21297. + movl des_SPtrans(%ebx),%ebp
  21298. + movb %dl, %bl
  21299. + xorl %ebp, %edi
  21300. + movl 0x200+des_SPtrans(%ecx),%ebp
  21301. + xorl %ebp, %edi
  21302. + movb %dh, %cl
  21303. + shrl $16, %eax
  21304. + movl 0x100+des_SPtrans(%ebx),%ebp
  21305. + xorl %ebp, %edi
  21306. + movb %ah, %bl
  21307. + shrl $16, %edx
  21308. + movl 0x300+des_SPtrans(%ecx),%ebp
  21309. + xorl %ebp, %edi
  21310. + movl 24(%esp), %ebp
  21311. + movb %dh, %cl
  21312. + andl $0xff, %eax
  21313. + andl $0xff, %edx
  21314. + movl 0x600+des_SPtrans(%ebx),%ebx
  21315. + xorl %ebx, %edi
  21316. + movl 0x700+des_SPtrans(%ecx),%ebx
  21317. + xorl %ebx, %edi
  21318. + movl 0x400+des_SPtrans(%eax),%ebx
  21319. + xorl %ebx, %edi
  21320. + movl 0x500+des_SPtrans(%edx),%ebx
  21321. + xorl %ebx, %edi
  21322. +
  21323. +
  21324. + movl 24(%ebp), %eax
  21325. + xorl %ebx, %ebx
  21326. + movl 28(%ebp), %edx
  21327. + xorl %edi, %eax
  21328. + xorl %edi, %edx
  21329. + andl $0xfcfcfcfc, %eax
  21330. + andl $0xcfcfcfcf, %edx
  21331. + movb %al, %bl
  21332. + movb %ah, %cl
  21333. + rorl $4, %edx
  21334. + movl des_SPtrans(%ebx),%ebp
  21335. + movb %dl, %bl
  21336. + xorl %ebp, %esi
  21337. + movl 0x200+des_SPtrans(%ecx),%ebp
  21338. + xorl %ebp, %esi
  21339. + movb %dh, %cl
  21340. + shrl $16, %eax
  21341. + movl 0x100+des_SPtrans(%ebx),%ebp
  21342. + xorl %ebp, %esi
  21343. + movb %ah, %bl
  21344. + shrl $16, %edx
  21345. + movl 0x300+des_SPtrans(%ecx),%ebp
  21346. + xorl %ebp, %esi
  21347. + movl 24(%esp), %ebp
  21348. + movb %dh, %cl
  21349. + andl $0xff, %eax
  21350. + andl $0xff, %edx
  21351. + movl 0x600+des_SPtrans(%ebx),%ebx
  21352. + xorl %ebx, %esi
  21353. + movl 0x700+des_SPtrans(%ecx),%ebx
  21354. + xorl %ebx, %esi
  21355. + movl 0x400+des_SPtrans(%eax),%ebx
  21356. + xorl %ebx, %esi
  21357. + movl 0x500+des_SPtrans(%edx),%ebx
  21358. + xorl %ebx, %esi
  21359. +
  21360. +
  21361. + movl 32(%ebp), %eax
  21362. + xorl %ebx, %ebx
  21363. + movl 36(%ebp), %edx
  21364. + xorl %esi, %eax
  21365. + xorl %esi, %edx
  21366. + andl $0xfcfcfcfc, %eax
  21367. + andl $0xcfcfcfcf, %edx
  21368. + movb %al, %bl
  21369. + movb %ah, %cl
  21370. + rorl $4, %edx
  21371. + movl des_SPtrans(%ebx),%ebp
  21372. + movb %dl, %bl
  21373. + xorl %ebp, %edi
  21374. + movl 0x200+des_SPtrans(%ecx),%ebp
  21375. + xorl %ebp, %edi
  21376. + movb %dh, %cl
  21377. + shrl $16, %eax
  21378. + movl 0x100+des_SPtrans(%ebx),%ebp
  21379. + xorl %ebp, %edi
  21380. + movb %ah, %bl
  21381. + shrl $16, %edx
  21382. + movl 0x300+des_SPtrans(%ecx),%ebp
  21383. + xorl %ebp, %edi
  21384. + movl 24(%esp), %ebp
  21385. + movb %dh, %cl
  21386. + andl $0xff, %eax
  21387. + andl $0xff, %edx
  21388. + movl 0x600+des_SPtrans(%ebx),%ebx
  21389. + xorl %ebx, %edi
  21390. + movl 0x700+des_SPtrans(%ecx),%ebx
  21391. + xorl %ebx, %edi
  21392. + movl 0x400+des_SPtrans(%eax),%ebx
  21393. + xorl %ebx, %edi
  21394. + movl 0x500+des_SPtrans(%edx),%ebx
  21395. + xorl %ebx, %edi
  21396. +
  21397. +
  21398. + movl 40(%ebp), %eax
  21399. + xorl %ebx, %ebx
  21400. + movl 44(%ebp), %edx
  21401. + xorl %edi, %eax
  21402. + xorl %edi, %edx
  21403. + andl $0xfcfcfcfc, %eax
  21404. + andl $0xcfcfcfcf, %edx
  21405. + movb %al, %bl
  21406. + movb %ah, %cl
  21407. + rorl $4, %edx
  21408. + movl des_SPtrans(%ebx),%ebp
  21409. + movb %dl, %bl
  21410. + xorl %ebp, %esi
  21411. + movl 0x200+des_SPtrans(%ecx),%ebp
  21412. + xorl %ebp, %esi
  21413. + movb %dh, %cl
  21414. + shrl $16, %eax
  21415. + movl 0x100+des_SPtrans(%ebx),%ebp
  21416. + xorl %ebp, %esi
  21417. + movb %ah, %bl
  21418. + shrl $16, %edx
  21419. + movl 0x300+des_SPtrans(%ecx),%ebp
  21420. + xorl %ebp, %esi
  21421. + movl 24(%esp), %ebp
  21422. + movb %dh, %cl
  21423. + andl $0xff, %eax
  21424. + andl $0xff, %edx
  21425. + movl 0x600+des_SPtrans(%ebx),%ebx
  21426. + xorl %ebx, %esi
  21427. + movl 0x700+des_SPtrans(%ecx),%ebx
  21428. + xorl %ebx, %esi
  21429. + movl 0x400+des_SPtrans(%eax),%ebx
  21430. + xorl %ebx, %esi
  21431. + movl 0x500+des_SPtrans(%edx),%ebx
  21432. + xorl %ebx, %esi
  21433. +
  21434. +
  21435. + movl 48(%ebp), %eax
  21436. + xorl %ebx, %ebx
  21437. + movl 52(%ebp), %edx
  21438. + xorl %esi, %eax
  21439. + xorl %esi, %edx
  21440. + andl $0xfcfcfcfc, %eax
  21441. + andl $0xcfcfcfcf, %edx
  21442. + movb %al, %bl
  21443. + movb %ah, %cl
  21444. + rorl $4, %edx
  21445. + movl des_SPtrans(%ebx),%ebp
  21446. + movb %dl, %bl
  21447. + xorl %ebp, %edi
  21448. + movl 0x200+des_SPtrans(%ecx),%ebp
  21449. + xorl %ebp, %edi
  21450. + movb %dh, %cl
  21451. + shrl $16, %eax
  21452. + movl 0x100+des_SPtrans(%ebx),%ebp
  21453. + xorl %ebp, %edi
  21454. + movb %ah, %bl
  21455. + shrl $16, %edx
  21456. + movl 0x300+des_SPtrans(%ecx),%ebp
  21457. + xorl %ebp, %edi
  21458. + movl 24(%esp), %ebp
  21459. + movb %dh, %cl
  21460. + andl $0xff, %eax
  21461. + andl $0xff, %edx
  21462. + movl 0x600+des_SPtrans(%ebx),%ebx
  21463. + xorl %ebx, %edi
  21464. + movl 0x700+des_SPtrans(%ecx),%ebx
  21465. + xorl %ebx, %edi
  21466. + movl 0x400+des_SPtrans(%eax),%ebx
  21467. + xorl %ebx, %edi
  21468. + movl 0x500+des_SPtrans(%edx),%ebx
  21469. + xorl %ebx, %edi
  21470. +
  21471. +
  21472. + movl 56(%ebp), %eax
  21473. + xorl %ebx, %ebx
  21474. + movl 60(%ebp), %edx
  21475. + xorl %edi, %eax
  21476. + xorl %edi, %edx
  21477. + andl $0xfcfcfcfc, %eax
  21478. + andl $0xcfcfcfcf, %edx
  21479. + movb %al, %bl
  21480. + movb %ah, %cl
  21481. + rorl $4, %edx
  21482. + movl des_SPtrans(%ebx),%ebp
  21483. + movb %dl, %bl
  21484. + xorl %ebp, %esi
  21485. + movl 0x200+des_SPtrans(%ecx),%ebp
  21486. + xorl %ebp, %esi
  21487. + movb %dh, %cl
  21488. + shrl $16, %eax
  21489. + movl 0x100+des_SPtrans(%ebx),%ebp
  21490. + xorl %ebp, %esi
  21491. + movb %ah, %bl
  21492. + shrl $16, %edx
  21493. + movl 0x300+des_SPtrans(%ecx),%ebp
  21494. + xorl %ebp, %esi
  21495. + movl 24(%esp), %ebp
  21496. + movb %dh, %cl
  21497. + andl $0xff, %eax
  21498. + andl $0xff, %edx
  21499. + movl 0x600+des_SPtrans(%ebx),%ebx
  21500. + xorl %ebx, %esi
  21501. + movl 0x700+des_SPtrans(%ecx),%ebx
  21502. + xorl %ebx, %esi
  21503. + movl 0x400+des_SPtrans(%eax),%ebx
  21504. + xorl %ebx, %esi
  21505. + movl 0x500+des_SPtrans(%edx),%ebx
  21506. + xorl %ebx, %esi
  21507. +
  21508. +
  21509. + movl 64(%ebp), %eax
  21510. + xorl %ebx, %ebx
  21511. + movl 68(%ebp), %edx
  21512. + xorl %esi, %eax
  21513. + xorl %esi, %edx
  21514. + andl $0xfcfcfcfc, %eax
  21515. + andl $0xcfcfcfcf, %edx
  21516. + movb %al, %bl
  21517. + movb %ah, %cl
  21518. + rorl $4, %edx
  21519. + movl des_SPtrans(%ebx),%ebp
  21520. + movb %dl, %bl
  21521. + xorl %ebp, %edi
  21522. + movl 0x200+des_SPtrans(%ecx),%ebp
  21523. + xorl %ebp, %edi
  21524. + movb %dh, %cl
  21525. + shrl $16, %eax
  21526. + movl 0x100+des_SPtrans(%ebx),%ebp
  21527. + xorl %ebp, %edi
  21528. + movb %ah, %bl
  21529. + shrl $16, %edx
  21530. + movl 0x300+des_SPtrans(%ecx),%ebp
  21531. + xorl %ebp, %edi
  21532. + movl 24(%esp), %ebp
  21533. + movb %dh, %cl
  21534. + andl $0xff, %eax
  21535. + andl $0xff, %edx
  21536. + movl 0x600+des_SPtrans(%ebx),%ebx
  21537. + xorl %ebx, %edi
  21538. + movl 0x700+des_SPtrans(%ecx),%ebx
  21539. + xorl %ebx, %edi
  21540. + movl 0x400+des_SPtrans(%eax),%ebx
  21541. + xorl %ebx, %edi
  21542. + movl 0x500+des_SPtrans(%edx),%ebx
  21543. + xorl %ebx, %edi
  21544. +
  21545. +
  21546. + movl 72(%ebp), %eax
  21547. + xorl %ebx, %ebx
  21548. + movl 76(%ebp), %edx
  21549. + xorl %edi, %eax
  21550. + xorl %edi, %edx
  21551. + andl $0xfcfcfcfc, %eax
  21552. + andl $0xcfcfcfcf, %edx
  21553. + movb %al, %bl
  21554. + movb %ah, %cl
  21555. + rorl $4, %edx
  21556. + movl des_SPtrans(%ebx),%ebp
  21557. + movb %dl, %bl
  21558. + xorl %ebp, %esi
  21559. + movl 0x200+des_SPtrans(%ecx),%ebp
  21560. + xorl %ebp, %esi
  21561. + movb %dh, %cl
  21562. + shrl $16, %eax
  21563. + movl 0x100+des_SPtrans(%ebx),%ebp
  21564. + xorl %ebp, %esi
  21565. + movb %ah, %bl
  21566. + shrl $16, %edx
  21567. + movl 0x300+des_SPtrans(%ecx),%ebp
  21568. + xorl %ebp, %esi
  21569. + movl 24(%esp), %ebp
  21570. + movb %dh, %cl
  21571. + andl $0xff, %eax
  21572. + andl $0xff, %edx
  21573. + movl 0x600+des_SPtrans(%ebx),%ebx
  21574. + xorl %ebx, %esi
  21575. + movl 0x700+des_SPtrans(%ecx),%ebx
  21576. + xorl %ebx, %esi
  21577. + movl 0x400+des_SPtrans(%eax),%ebx
  21578. + xorl %ebx, %esi
  21579. + movl 0x500+des_SPtrans(%edx),%ebx
  21580. + xorl %ebx, %esi
  21581. +
  21582. +
  21583. + movl 80(%ebp), %eax
  21584. + xorl %ebx, %ebx
  21585. + movl 84(%ebp), %edx
  21586. + xorl %esi, %eax
  21587. + xorl %esi, %edx
  21588. + andl $0xfcfcfcfc, %eax
  21589. + andl $0xcfcfcfcf, %edx
  21590. + movb %al, %bl
  21591. + movb %ah, %cl
  21592. + rorl $4, %edx
  21593. + movl des_SPtrans(%ebx),%ebp
  21594. + movb %dl, %bl
  21595. + xorl %ebp, %edi
  21596. + movl 0x200+des_SPtrans(%ecx),%ebp
  21597. + xorl %ebp, %edi
  21598. + movb %dh, %cl
  21599. + shrl $16, %eax
  21600. + movl 0x100+des_SPtrans(%ebx),%ebp
  21601. + xorl %ebp, %edi
  21602. + movb %ah, %bl
  21603. + shrl $16, %edx
  21604. + movl 0x300+des_SPtrans(%ecx),%ebp
  21605. + xorl %ebp, %edi
  21606. + movl 24(%esp), %ebp
  21607. + movb %dh, %cl
  21608. + andl $0xff, %eax
  21609. + andl $0xff, %edx
  21610. + movl 0x600+des_SPtrans(%ebx),%ebx
  21611. + xorl %ebx, %edi
  21612. + movl 0x700+des_SPtrans(%ecx),%ebx
  21613. + xorl %ebx, %edi
  21614. + movl 0x400+des_SPtrans(%eax),%ebx
  21615. + xorl %ebx, %edi
  21616. + movl 0x500+des_SPtrans(%edx),%ebx
  21617. + xorl %ebx, %edi
  21618. +
  21619. +
  21620. + movl 88(%ebp), %eax
  21621. + xorl %ebx, %ebx
  21622. + movl 92(%ebp), %edx
  21623. + xorl %edi, %eax
  21624. + xorl %edi, %edx
  21625. + andl $0xfcfcfcfc, %eax
  21626. + andl $0xcfcfcfcf, %edx
  21627. + movb %al, %bl
  21628. + movb %ah, %cl
  21629. + rorl $4, %edx
  21630. + movl des_SPtrans(%ebx),%ebp
  21631. + movb %dl, %bl
  21632. + xorl %ebp, %esi
  21633. + movl 0x200+des_SPtrans(%ecx),%ebp
  21634. + xorl %ebp, %esi
  21635. + movb %dh, %cl
  21636. + shrl $16, %eax
  21637. + movl 0x100+des_SPtrans(%ebx),%ebp
  21638. + xorl %ebp, %esi
  21639. + movb %ah, %bl
  21640. + shrl $16, %edx
  21641. + movl 0x300+des_SPtrans(%ecx),%ebp
  21642. + xorl %ebp, %esi
  21643. + movl 24(%esp), %ebp
  21644. + movb %dh, %cl
  21645. + andl $0xff, %eax
  21646. + andl $0xff, %edx
  21647. + movl 0x600+des_SPtrans(%ebx),%ebx
  21648. + xorl %ebx, %esi
  21649. + movl 0x700+des_SPtrans(%ecx),%ebx
  21650. + xorl %ebx, %esi
  21651. + movl 0x400+des_SPtrans(%eax),%ebx
  21652. + xorl %ebx, %esi
  21653. + movl 0x500+des_SPtrans(%edx),%ebx
  21654. + xorl %ebx, %esi
  21655. +
  21656. +
  21657. + movl 96(%ebp), %eax
  21658. + xorl %ebx, %ebx
  21659. + movl 100(%ebp), %edx
  21660. + xorl %esi, %eax
  21661. + xorl %esi, %edx
  21662. + andl $0xfcfcfcfc, %eax
  21663. + andl $0xcfcfcfcf, %edx
  21664. + movb %al, %bl
  21665. + movb %ah, %cl
  21666. + rorl $4, %edx
  21667. + movl des_SPtrans(%ebx),%ebp
  21668. + movb %dl, %bl
  21669. + xorl %ebp, %edi
  21670. + movl 0x200+des_SPtrans(%ecx),%ebp
  21671. + xorl %ebp, %edi
  21672. + movb %dh, %cl
  21673. + shrl $16, %eax
  21674. + movl 0x100+des_SPtrans(%ebx),%ebp
  21675. + xorl %ebp, %edi
  21676. + movb %ah, %bl
  21677. + shrl $16, %edx
  21678. + movl 0x300+des_SPtrans(%ecx),%ebp
  21679. + xorl %ebp, %edi
  21680. + movl 24(%esp), %ebp
  21681. + movb %dh, %cl
  21682. + andl $0xff, %eax
  21683. + andl $0xff, %edx
  21684. + movl 0x600+des_SPtrans(%ebx),%ebx
  21685. + xorl %ebx, %edi
  21686. + movl 0x700+des_SPtrans(%ecx),%ebx
  21687. + xorl %ebx, %edi
  21688. + movl 0x400+des_SPtrans(%eax),%ebx
  21689. + xorl %ebx, %edi
  21690. + movl 0x500+des_SPtrans(%edx),%ebx
  21691. + xorl %ebx, %edi
  21692. +
  21693. +
  21694. + movl 104(%ebp), %eax
  21695. + xorl %ebx, %ebx
  21696. + movl 108(%ebp), %edx
  21697. + xorl %edi, %eax
  21698. + xorl %edi, %edx
  21699. + andl $0xfcfcfcfc, %eax
  21700. + andl $0xcfcfcfcf, %edx
  21701. + movb %al, %bl
  21702. + movb %ah, %cl
  21703. + rorl $4, %edx
  21704. + movl des_SPtrans(%ebx),%ebp
  21705. + movb %dl, %bl
  21706. + xorl %ebp, %esi
  21707. + movl 0x200+des_SPtrans(%ecx),%ebp
  21708. + xorl %ebp, %esi
  21709. + movb %dh, %cl
  21710. + shrl $16, %eax
  21711. + movl 0x100+des_SPtrans(%ebx),%ebp
  21712. + xorl %ebp, %esi
  21713. + movb %ah, %bl
  21714. + shrl $16, %edx
  21715. + movl 0x300+des_SPtrans(%ecx),%ebp
  21716. + xorl %ebp, %esi
  21717. + movl 24(%esp), %ebp
  21718. + movb %dh, %cl
  21719. + andl $0xff, %eax
  21720. + andl $0xff, %edx
  21721. + movl 0x600+des_SPtrans(%ebx),%ebx
  21722. + xorl %ebx, %esi
  21723. + movl 0x700+des_SPtrans(%ecx),%ebx
  21724. + xorl %ebx, %esi
  21725. + movl 0x400+des_SPtrans(%eax),%ebx
  21726. + xorl %ebx, %esi
  21727. + movl 0x500+des_SPtrans(%edx),%ebx
  21728. + xorl %ebx, %esi
  21729. +
  21730. +
  21731. + movl 112(%ebp), %eax
  21732. + xorl %ebx, %ebx
  21733. + movl 116(%ebp), %edx
  21734. + xorl %esi, %eax
  21735. + xorl %esi, %edx
  21736. + andl $0xfcfcfcfc, %eax
  21737. + andl $0xcfcfcfcf, %edx
  21738. + movb %al, %bl
  21739. + movb %ah, %cl
  21740. + rorl $4, %edx
  21741. + movl des_SPtrans(%ebx),%ebp
  21742. + movb %dl, %bl
  21743. + xorl %ebp, %edi
  21744. + movl 0x200+des_SPtrans(%ecx),%ebp
  21745. + xorl %ebp, %edi
  21746. + movb %dh, %cl
  21747. + shrl $16, %eax
  21748. + movl 0x100+des_SPtrans(%ebx),%ebp
  21749. + xorl %ebp, %edi
  21750. + movb %ah, %bl
  21751. + shrl $16, %edx
  21752. + movl 0x300+des_SPtrans(%ecx),%ebp
  21753. + xorl %ebp, %edi
  21754. + movl 24(%esp), %ebp
  21755. + movb %dh, %cl
  21756. + andl $0xff, %eax
  21757. + andl $0xff, %edx
  21758. + movl 0x600+des_SPtrans(%ebx),%ebx
  21759. + xorl %ebx, %edi
  21760. + movl 0x700+des_SPtrans(%ecx),%ebx
  21761. + xorl %ebx, %edi
  21762. + movl 0x400+des_SPtrans(%eax),%ebx
  21763. + xorl %ebx, %edi
  21764. + movl 0x500+des_SPtrans(%edx),%ebx
  21765. + xorl %ebx, %edi
  21766. +
  21767. +
  21768. + movl 120(%ebp), %eax
  21769. + xorl %ebx, %ebx
  21770. + movl 124(%ebp), %edx
  21771. + xorl %edi, %eax
  21772. + xorl %edi, %edx
  21773. + andl $0xfcfcfcfc, %eax
  21774. + andl $0xcfcfcfcf, %edx
  21775. + movb %al, %bl
  21776. + movb %ah, %cl
  21777. + rorl $4, %edx
  21778. + movl des_SPtrans(%ebx),%ebp
  21779. + movb %dl, %bl
  21780. + xorl %ebp, %esi
  21781. + movl 0x200+des_SPtrans(%ecx),%ebp
  21782. + xorl %ebp, %esi
  21783. + movb %dh, %cl
  21784. + shrl $16, %eax
  21785. + movl 0x100+des_SPtrans(%ebx),%ebp
  21786. + xorl %ebp, %esi
  21787. + movb %ah, %bl
  21788. + shrl $16, %edx
  21789. + movl 0x300+des_SPtrans(%ecx),%ebp
  21790. + xorl %ebp, %esi
  21791. + movl 24(%esp), %ebp
  21792. + movb %dh, %cl
  21793. + andl $0xff, %eax
  21794. + andl $0xff, %edx
  21795. + movl 0x600+des_SPtrans(%ebx),%ebx
  21796. + xorl %ebx, %esi
  21797. + movl 0x700+des_SPtrans(%ecx),%ebx
  21798. + xorl %ebx, %esi
  21799. + movl 0x400+des_SPtrans(%eax),%ebx
  21800. + xorl %ebx, %esi
  21801. + movl 0x500+des_SPtrans(%edx),%ebx
  21802. + xorl %ebx, %esi
  21803. + jmp .L003end
  21804. +.L002start_decrypt:
  21805. +
  21806. +
  21807. + movl 120(%ebp), %eax
  21808. + xorl %ebx, %ebx
  21809. + movl 124(%ebp), %edx
  21810. + xorl %esi, %eax
  21811. + xorl %esi, %edx
  21812. + andl $0xfcfcfcfc, %eax
  21813. + andl $0xcfcfcfcf, %edx
  21814. + movb %al, %bl
  21815. + movb %ah, %cl
  21816. + rorl $4, %edx
  21817. + movl des_SPtrans(%ebx),%ebp
  21818. + movb %dl, %bl
  21819. + xorl %ebp, %edi
  21820. + movl 0x200+des_SPtrans(%ecx),%ebp
  21821. + xorl %ebp, %edi
  21822. + movb %dh, %cl
  21823. + shrl $16, %eax
  21824. + movl 0x100+des_SPtrans(%ebx),%ebp
  21825. + xorl %ebp, %edi
  21826. + movb %ah, %bl
  21827. + shrl $16, %edx
  21828. + movl 0x300+des_SPtrans(%ecx),%ebp
  21829. + xorl %ebp, %edi
  21830. + movl 24(%esp), %ebp
  21831. + movb %dh, %cl
  21832. + andl $0xff, %eax
  21833. + andl $0xff, %edx
  21834. + movl 0x600+des_SPtrans(%ebx),%ebx
  21835. + xorl %ebx, %edi
  21836. + movl 0x700+des_SPtrans(%ecx),%ebx
  21837. + xorl %ebx, %edi
  21838. + movl 0x400+des_SPtrans(%eax),%ebx
  21839. + xorl %ebx, %edi
  21840. + movl 0x500+des_SPtrans(%edx),%ebx
  21841. + xorl %ebx, %edi
  21842. +
  21843. +
  21844. + movl 112(%ebp), %eax
  21845. + xorl %ebx, %ebx
  21846. + movl 116(%ebp), %edx
  21847. + xorl %edi, %eax
  21848. + xorl %edi, %edx
  21849. + andl $0xfcfcfcfc, %eax
  21850. + andl $0xcfcfcfcf, %edx
  21851. + movb %al, %bl
  21852. + movb %ah, %cl
  21853. + rorl $4, %edx
  21854. + movl des_SPtrans(%ebx),%ebp
  21855. + movb %dl, %bl
  21856. + xorl %ebp, %esi
  21857. + movl 0x200+des_SPtrans(%ecx),%ebp
  21858. + xorl %ebp, %esi
  21859. + movb %dh, %cl
  21860. + shrl $16, %eax
  21861. + movl 0x100+des_SPtrans(%ebx),%ebp
  21862. + xorl %ebp, %esi
  21863. + movb %ah, %bl
  21864. + shrl $16, %edx
  21865. + movl 0x300+des_SPtrans(%ecx),%ebp
  21866. + xorl %ebp, %esi
  21867. + movl 24(%esp), %ebp
  21868. + movb %dh, %cl
  21869. + andl $0xff, %eax
  21870. + andl $0xff, %edx
  21871. + movl 0x600+des_SPtrans(%ebx),%ebx
  21872. + xorl %ebx, %esi
  21873. + movl 0x700+des_SPtrans(%ecx),%ebx
  21874. + xorl %ebx, %esi
  21875. + movl 0x400+des_SPtrans(%eax),%ebx
  21876. + xorl %ebx, %esi
  21877. + movl 0x500+des_SPtrans(%edx),%ebx
  21878. + xorl %ebx, %esi
  21879. +
  21880. +
  21881. + movl 104(%ebp), %eax
  21882. + xorl %ebx, %ebx
  21883. + movl 108(%ebp), %edx
  21884. + xorl %esi, %eax
  21885. + xorl %esi, %edx
  21886. + andl $0xfcfcfcfc, %eax
  21887. + andl $0xcfcfcfcf, %edx
  21888. + movb %al, %bl
  21889. + movb %ah, %cl
  21890. + rorl $4, %edx
  21891. + movl des_SPtrans(%ebx),%ebp
  21892. + movb %dl, %bl
  21893. + xorl %ebp, %edi
  21894. + movl 0x200+des_SPtrans(%ecx),%ebp
  21895. + xorl %ebp, %edi
  21896. + movb %dh, %cl
  21897. + shrl $16, %eax
  21898. + movl 0x100+des_SPtrans(%ebx),%ebp
  21899. + xorl %ebp, %edi
  21900. + movb %ah, %bl
  21901. + shrl $16, %edx
  21902. + movl 0x300+des_SPtrans(%ecx),%ebp
  21903. + xorl %ebp, %edi
  21904. + movl 24(%esp), %ebp
  21905. + movb %dh, %cl
  21906. + andl $0xff, %eax
  21907. + andl $0xff, %edx
  21908. + movl 0x600+des_SPtrans(%ebx),%ebx
  21909. + xorl %ebx, %edi
  21910. + movl 0x700+des_SPtrans(%ecx),%ebx
  21911. + xorl %ebx, %edi
  21912. + movl 0x400+des_SPtrans(%eax),%ebx
  21913. + xorl %ebx, %edi
  21914. + movl 0x500+des_SPtrans(%edx),%ebx
  21915. + xorl %ebx, %edi
  21916. +
  21917. +
  21918. + movl 96(%ebp), %eax
  21919. + xorl %ebx, %ebx
  21920. + movl 100(%ebp), %edx
  21921. + xorl %edi, %eax
  21922. + xorl %edi, %edx
  21923. + andl $0xfcfcfcfc, %eax
  21924. + andl $0xcfcfcfcf, %edx
  21925. + movb %al, %bl
  21926. + movb %ah, %cl
  21927. + rorl $4, %edx
  21928. + movl des_SPtrans(%ebx),%ebp
  21929. + movb %dl, %bl
  21930. + xorl %ebp, %esi
  21931. + movl 0x200+des_SPtrans(%ecx),%ebp
  21932. + xorl %ebp, %esi
  21933. + movb %dh, %cl
  21934. + shrl $16, %eax
  21935. + movl 0x100+des_SPtrans(%ebx),%ebp
  21936. + xorl %ebp, %esi
  21937. + movb %ah, %bl
  21938. + shrl $16, %edx
  21939. + movl 0x300+des_SPtrans(%ecx),%ebp
  21940. + xorl %ebp, %esi
  21941. + movl 24(%esp), %ebp
  21942. + movb %dh, %cl
  21943. + andl $0xff, %eax
  21944. + andl $0xff, %edx
  21945. + movl 0x600+des_SPtrans(%ebx),%ebx
  21946. + xorl %ebx, %esi
  21947. + movl 0x700+des_SPtrans(%ecx),%ebx
  21948. + xorl %ebx, %esi
  21949. + movl 0x400+des_SPtrans(%eax),%ebx
  21950. + xorl %ebx, %esi
  21951. + movl 0x500+des_SPtrans(%edx),%ebx
  21952. + xorl %ebx, %esi
  21953. +
  21954. +
  21955. + movl 88(%ebp), %eax
  21956. + xorl %ebx, %ebx
  21957. + movl 92(%ebp), %edx
  21958. + xorl %esi, %eax
  21959. + xorl %esi, %edx
  21960. + andl $0xfcfcfcfc, %eax
  21961. + andl $0xcfcfcfcf, %edx
  21962. + movb %al, %bl
  21963. + movb %ah, %cl
  21964. + rorl $4, %edx
  21965. + movl des_SPtrans(%ebx),%ebp
  21966. + movb %dl, %bl
  21967. + xorl %ebp, %edi
  21968. + movl 0x200+des_SPtrans(%ecx),%ebp
  21969. + xorl %ebp, %edi
  21970. + movb %dh, %cl
  21971. + shrl $16, %eax
  21972. + movl 0x100+des_SPtrans(%ebx),%ebp
  21973. + xorl %ebp, %edi
  21974. + movb %ah, %bl
  21975. + shrl $16, %edx
  21976. + movl 0x300+des_SPtrans(%ecx),%ebp
  21977. + xorl %ebp, %edi
  21978. + movl 24(%esp), %ebp
  21979. + movb %dh, %cl
  21980. + andl $0xff, %eax
  21981. + andl $0xff, %edx
  21982. + movl 0x600+des_SPtrans(%ebx),%ebx
  21983. + xorl %ebx, %edi
  21984. + movl 0x700+des_SPtrans(%ecx),%ebx
  21985. + xorl %ebx, %edi
  21986. + movl 0x400+des_SPtrans(%eax),%ebx
  21987. + xorl %ebx, %edi
  21988. + movl 0x500+des_SPtrans(%edx),%ebx
  21989. + xorl %ebx, %edi
  21990. +
  21991. +
  21992. + movl 80(%ebp), %eax
  21993. + xorl %ebx, %ebx
  21994. + movl 84(%ebp), %edx
  21995. + xorl %edi, %eax
  21996. + xorl %edi, %edx
  21997. + andl $0xfcfcfcfc, %eax
  21998. + andl $0xcfcfcfcf, %edx
  21999. + movb %al, %bl
  22000. + movb %ah, %cl
  22001. + rorl $4, %edx
  22002. + movl des_SPtrans(%ebx),%ebp
  22003. + movb %dl, %bl
  22004. + xorl %ebp, %esi
  22005. + movl 0x200+des_SPtrans(%ecx),%ebp
  22006. + xorl %ebp, %esi
  22007. + movb %dh, %cl
  22008. + shrl $16, %eax
  22009. + movl 0x100+des_SPtrans(%ebx),%ebp
  22010. + xorl %ebp, %esi
  22011. + movb %ah, %bl
  22012. + shrl $16, %edx
  22013. + movl 0x300+des_SPtrans(%ecx),%ebp
  22014. + xorl %ebp, %esi
  22015. + movl 24(%esp), %ebp
  22016. + movb %dh, %cl
  22017. + andl $0xff, %eax
  22018. + andl $0xff, %edx
  22019. + movl 0x600+des_SPtrans(%ebx),%ebx
  22020. + xorl %ebx, %esi
  22021. + movl 0x700+des_SPtrans(%ecx),%ebx
  22022. + xorl %ebx, %esi
  22023. + movl 0x400+des_SPtrans(%eax),%ebx
  22024. + xorl %ebx, %esi
  22025. + movl 0x500+des_SPtrans(%edx),%ebx
  22026. + xorl %ebx, %esi
  22027. +
  22028. +
  22029. + movl 72(%ebp), %eax
  22030. + xorl %ebx, %ebx
  22031. + movl 76(%ebp), %edx
  22032. + xorl %esi, %eax
  22033. + xorl %esi, %edx
  22034. + andl $0xfcfcfcfc, %eax
  22035. + andl $0xcfcfcfcf, %edx
  22036. + movb %al, %bl
  22037. + movb %ah, %cl
  22038. + rorl $4, %edx
  22039. + movl des_SPtrans(%ebx),%ebp
  22040. + movb %dl, %bl
  22041. + xorl %ebp, %edi
  22042. + movl 0x200+des_SPtrans(%ecx),%ebp
  22043. + xorl %ebp, %edi
  22044. + movb %dh, %cl
  22045. + shrl $16, %eax
  22046. + movl 0x100+des_SPtrans(%ebx),%ebp
  22047. + xorl %ebp, %edi
  22048. + movb %ah, %bl
  22049. + shrl $16, %edx
  22050. + movl 0x300+des_SPtrans(%ecx),%ebp
  22051. + xorl %ebp, %edi
  22052. + movl 24(%esp), %ebp
  22053. + movb %dh, %cl
  22054. + andl $0xff, %eax
  22055. + andl $0xff, %edx
  22056. + movl 0x600+des_SPtrans(%ebx),%ebx
  22057. + xorl %ebx, %edi
  22058. + movl 0x700+des_SPtrans(%ecx),%ebx
  22059. + xorl %ebx, %edi
  22060. + movl 0x400+des_SPtrans(%eax),%ebx
  22061. + xorl %ebx, %edi
  22062. + movl 0x500+des_SPtrans(%edx),%ebx
  22063. + xorl %ebx, %edi
  22064. +
  22065. +
  22066. + movl 64(%ebp), %eax
  22067. + xorl %ebx, %ebx
  22068. + movl 68(%ebp), %edx
  22069. + xorl %edi, %eax
  22070. + xorl %edi, %edx
  22071. + andl $0xfcfcfcfc, %eax
  22072. + andl $0xcfcfcfcf, %edx
  22073. + movb %al, %bl
  22074. + movb %ah, %cl
  22075. + rorl $4, %edx
  22076. + movl des_SPtrans(%ebx),%ebp
  22077. + movb %dl, %bl
  22078. + xorl %ebp, %esi
  22079. + movl 0x200+des_SPtrans(%ecx),%ebp
  22080. + xorl %ebp, %esi
  22081. + movb %dh, %cl
  22082. + shrl $16, %eax
  22083. + movl 0x100+des_SPtrans(%ebx),%ebp
  22084. + xorl %ebp, %esi
  22085. + movb %ah, %bl
  22086. + shrl $16, %edx
  22087. + movl 0x300+des_SPtrans(%ecx),%ebp
  22088. + xorl %ebp, %esi
  22089. + movl 24(%esp), %ebp
  22090. + movb %dh, %cl
  22091. + andl $0xff, %eax
  22092. + andl $0xff, %edx
  22093. + movl 0x600+des_SPtrans(%ebx),%ebx
  22094. + xorl %ebx, %esi
  22095. + movl 0x700+des_SPtrans(%ecx),%ebx
  22096. + xorl %ebx, %esi
  22097. + movl 0x400+des_SPtrans(%eax),%ebx
  22098. + xorl %ebx, %esi
  22099. + movl 0x500+des_SPtrans(%edx),%ebx
  22100. + xorl %ebx, %esi
  22101. +
  22102. +
  22103. + movl 56(%ebp), %eax
  22104. + xorl %ebx, %ebx
  22105. + movl 60(%ebp), %edx
  22106. + xorl %esi, %eax
  22107. + xorl %esi, %edx
  22108. + andl $0xfcfcfcfc, %eax
  22109. + andl $0xcfcfcfcf, %edx
  22110. + movb %al, %bl
  22111. + movb %ah, %cl
  22112. + rorl $4, %edx
  22113. + movl des_SPtrans(%ebx),%ebp
  22114. + movb %dl, %bl
  22115. + xorl %ebp, %edi
  22116. + movl 0x200+des_SPtrans(%ecx),%ebp
  22117. + xorl %ebp, %edi
  22118. + movb %dh, %cl
  22119. + shrl $16, %eax
  22120. + movl 0x100+des_SPtrans(%ebx),%ebp
  22121. + xorl %ebp, %edi
  22122. + movb %ah, %bl
  22123. + shrl $16, %edx
  22124. + movl 0x300+des_SPtrans(%ecx),%ebp
  22125. + xorl %ebp, %edi
  22126. + movl 24(%esp), %ebp
  22127. + movb %dh, %cl
  22128. + andl $0xff, %eax
  22129. + andl $0xff, %edx
  22130. + movl 0x600+des_SPtrans(%ebx),%ebx
  22131. + xorl %ebx, %edi
  22132. + movl 0x700+des_SPtrans(%ecx),%ebx
  22133. + xorl %ebx, %edi
  22134. + movl 0x400+des_SPtrans(%eax),%ebx
  22135. + xorl %ebx, %edi
  22136. + movl 0x500+des_SPtrans(%edx),%ebx
  22137. + xorl %ebx, %edi
  22138. +
  22139. +
  22140. + movl 48(%ebp), %eax
  22141. + xorl %ebx, %ebx
  22142. + movl 52(%ebp), %edx
  22143. + xorl %edi, %eax
  22144. + xorl %edi, %edx
  22145. + andl $0xfcfcfcfc, %eax
  22146. + andl $0xcfcfcfcf, %edx
  22147. + movb %al, %bl
  22148. + movb %ah, %cl
  22149. + rorl $4, %edx
  22150. + movl des_SPtrans(%ebx),%ebp
  22151. + movb %dl, %bl
  22152. + xorl %ebp, %esi
  22153. + movl 0x200+des_SPtrans(%ecx),%ebp
  22154. + xorl %ebp, %esi
  22155. + movb %dh, %cl
  22156. + shrl $16, %eax
  22157. + movl 0x100+des_SPtrans(%ebx),%ebp
  22158. + xorl %ebp, %esi
  22159. + movb %ah, %bl
  22160. + shrl $16, %edx
  22161. + movl 0x300+des_SPtrans(%ecx),%ebp
  22162. + xorl %ebp, %esi
  22163. + movl 24(%esp), %ebp
  22164. + movb %dh, %cl
  22165. + andl $0xff, %eax
  22166. + andl $0xff, %edx
  22167. + movl 0x600+des_SPtrans(%ebx),%ebx
  22168. + xorl %ebx, %esi
  22169. + movl 0x700+des_SPtrans(%ecx),%ebx
  22170. + xorl %ebx, %esi
  22171. + movl 0x400+des_SPtrans(%eax),%ebx
  22172. + xorl %ebx, %esi
  22173. + movl 0x500+des_SPtrans(%edx),%ebx
  22174. + xorl %ebx, %esi
  22175. +
  22176. +
  22177. + movl 40(%ebp), %eax
  22178. + xorl %ebx, %ebx
  22179. + movl 44(%ebp), %edx
  22180. + xorl %esi, %eax
  22181. + xorl %esi, %edx
  22182. + andl $0xfcfcfcfc, %eax
  22183. + andl $0xcfcfcfcf, %edx
  22184. + movb %al, %bl
  22185. + movb %ah, %cl
  22186. + rorl $4, %edx
  22187. + movl des_SPtrans(%ebx),%ebp
  22188. + movb %dl, %bl
  22189. + xorl %ebp, %edi
  22190. + movl 0x200+des_SPtrans(%ecx),%ebp
  22191. + xorl %ebp, %edi
  22192. + movb %dh, %cl
  22193. + shrl $16, %eax
  22194. + movl 0x100+des_SPtrans(%ebx),%ebp
  22195. + xorl %ebp, %edi
  22196. + movb %ah, %bl
  22197. + shrl $16, %edx
  22198. + movl 0x300+des_SPtrans(%ecx),%ebp
  22199. + xorl %ebp, %edi
  22200. + movl 24(%esp), %ebp
  22201. + movb %dh, %cl
  22202. + andl $0xff, %eax
  22203. + andl $0xff, %edx
  22204. + movl 0x600+des_SPtrans(%ebx),%ebx
  22205. + xorl %ebx, %edi
  22206. + movl 0x700+des_SPtrans(%ecx),%ebx
  22207. + xorl %ebx, %edi
  22208. + movl 0x400+des_SPtrans(%eax),%ebx
  22209. + xorl %ebx, %edi
  22210. + movl 0x500+des_SPtrans(%edx),%ebx
  22211. + xorl %ebx, %edi
  22212. +
  22213. +
  22214. + movl 32(%ebp), %eax
  22215. + xorl %ebx, %ebx
  22216. + movl 36(%ebp), %edx
  22217. + xorl %edi, %eax
  22218. + xorl %edi, %edx
  22219. + andl $0xfcfcfcfc, %eax
  22220. + andl $0xcfcfcfcf, %edx
  22221. + movb %al, %bl
  22222. + movb %ah, %cl
  22223. + rorl $4, %edx
  22224. + movl des_SPtrans(%ebx),%ebp
  22225. + movb %dl, %bl
  22226. + xorl %ebp, %esi
  22227. + movl 0x200+des_SPtrans(%ecx),%ebp
  22228. + xorl %ebp, %esi
  22229. + movb %dh, %cl
  22230. + shrl $16, %eax
  22231. + movl 0x100+des_SPtrans(%ebx),%ebp
  22232. + xorl %ebp, %esi
  22233. + movb %ah, %bl
  22234. + shrl $16, %edx
  22235. + movl 0x300+des_SPtrans(%ecx),%ebp
  22236. + xorl %ebp, %esi
  22237. + movl 24(%esp), %ebp
  22238. + movb %dh, %cl
  22239. + andl $0xff, %eax
  22240. + andl $0xff, %edx
  22241. + movl 0x600+des_SPtrans(%ebx),%ebx
  22242. + xorl %ebx, %esi
  22243. + movl 0x700+des_SPtrans(%ecx),%ebx
  22244. + xorl %ebx, %esi
  22245. + movl 0x400+des_SPtrans(%eax),%ebx
  22246. + xorl %ebx, %esi
  22247. + movl 0x500+des_SPtrans(%edx),%ebx
  22248. + xorl %ebx, %esi
  22249. +
  22250. +
  22251. + movl 24(%ebp), %eax
  22252. + xorl %ebx, %ebx
  22253. + movl 28(%ebp), %edx
  22254. + xorl %esi, %eax
  22255. + xorl %esi, %edx
  22256. + andl $0xfcfcfcfc, %eax
  22257. + andl $0xcfcfcfcf, %edx
  22258. + movb %al, %bl
  22259. + movb %ah, %cl
  22260. + rorl $4, %edx
  22261. + movl des_SPtrans(%ebx),%ebp
  22262. + movb %dl, %bl
  22263. + xorl %ebp, %edi
  22264. + movl 0x200+des_SPtrans(%ecx),%ebp
  22265. + xorl %ebp, %edi
  22266. + movb %dh, %cl
  22267. + shrl $16, %eax
  22268. + movl 0x100+des_SPtrans(%ebx),%ebp
  22269. + xorl %ebp, %edi
  22270. + movb %ah, %bl
  22271. + shrl $16, %edx
  22272. + movl 0x300+des_SPtrans(%ecx),%ebp
  22273. + xorl %ebp, %edi
  22274. + movl 24(%esp), %ebp
  22275. + movb %dh, %cl
  22276. + andl $0xff, %eax
  22277. + andl $0xff, %edx
  22278. + movl 0x600+des_SPtrans(%ebx),%ebx
  22279. + xorl %ebx, %edi
  22280. + movl 0x700+des_SPtrans(%ecx),%ebx
  22281. + xorl %ebx, %edi
  22282. + movl 0x400+des_SPtrans(%eax),%ebx
  22283. + xorl %ebx, %edi
  22284. + movl 0x500+des_SPtrans(%edx),%ebx
  22285. + xorl %ebx, %edi
  22286. +
  22287. +
  22288. + movl 16(%ebp), %eax
  22289. + xorl %ebx, %ebx
  22290. + movl 20(%ebp), %edx
  22291. + xorl %edi, %eax
  22292. + xorl %edi, %edx
  22293. + andl $0xfcfcfcfc, %eax
  22294. + andl $0xcfcfcfcf, %edx
  22295. + movb %al, %bl
  22296. + movb %ah, %cl
  22297. + rorl $4, %edx
  22298. + movl des_SPtrans(%ebx),%ebp
  22299. + movb %dl, %bl
  22300. + xorl %ebp, %esi
  22301. + movl 0x200+des_SPtrans(%ecx),%ebp
  22302. + xorl %ebp, %esi
  22303. + movb %dh, %cl
  22304. + shrl $16, %eax
  22305. + movl 0x100+des_SPtrans(%ebx),%ebp
  22306. + xorl %ebp, %esi
  22307. + movb %ah, %bl
  22308. + shrl $16, %edx
  22309. + movl 0x300+des_SPtrans(%ecx),%ebp
  22310. + xorl %ebp, %esi
  22311. + movl 24(%esp), %ebp
  22312. + movb %dh, %cl
  22313. + andl $0xff, %eax
  22314. + andl $0xff, %edx
  22315. + movl 0x600+des_SPtrans(%ebx),%ebx
  22316. + xorl %ebx, %esi
  22317. + movl 0x700+des_SPtrans(%ecx),%ebx
  22318. + xorl %ebx, %esi
  22319. + movl 0x400+des_SPtrans(%eax),%ebx
  22320. + xorl %ebx, %esi
  22321. + movl 0x500+des_SPtrans(%edx),%ebx
  22322. + xorl %ebx, %esi
  22323. +
  22324. +
  22325. + movl 8(%ebp), %eax
  22326. + xorl %ebx, %ebx
  22327. + movl 12(%ebp), %edx
  22328. + xorl %esi, %eax
  22329. + xorl %esi, %edx
  22330. + andl $0xfcfcfcfc, %eax
  22331. + andl $0xcfcfcfcf, %edx
  22332. + movb %al, %bl
  22333. + movb %ah, %cl
  22334. + rorl $4, %edx
  22335. + movl des_SPtrans(%ebx),%ebp
  22336. + movb %dl, %bl
  22337. + xorl %ebp, %edi
  22338. + movl 0x200+des_SPtrans(%ecx),%ebp
  22339. + xorl %ebp, %edi
  22340. + movb %dh, %cl
  22341. + shrl $16, %eax
  22342. + movl 0x100+des_SPtrans(%ebx),%ebp
  22343. + xorl %ebp, %edi
  22344. + movb %ah, %bl
  22345. + shrl $16, %edx
  22346. + movl 0x300+des_SPtrans(%ecx),%ebp
  22347. + xorl %ebp, %edi
  22348. + movl 24(%esp), %ebp
  22349. + movb %dh, %cl
  22350. + andl $0xff, %eax
  22351. + andl $0xff, %edx
  22352. + movl 0x600+des_SPtrans(%ebx),%ebx
  22353. + xorl %ebx, %edi
  22354. + movl 0x700+des_SPtrans(%ecx),%ebx
  22355. + xorl %ebx, %edi
  22356. + movl 0x400+des_SPtrans(%eax),%ebx
  22357. + xorl %ebx, %edi
  22358. + movl 0x500+des_SPtrans(%edx),%ebx
  22359. + xorl %ebx, %edi
  22360. +
  22361. +
  22362. + movl (%ebp), %eax
  22363. + xorl %ebx, %ebx
  22364. + movl 4(%ebp), %edx
  22365. + xorl %edi, %eax
  22366. + xorl %edi, %edx
  22367. + andl $0xfcfcfcfc, %eax
  22368. + andl $0xcfcfcfcf, %edx
  22369. + movb %al, %bl
  22370. + movb %ah, %cl
  22371. + rorl $4, %edx
  22372. + movl des_SPtrans(%ebx),%ebp
  22373. + movb %dl, %bl
  22374. + xorl %ebp, %esi
  22375. + movl 0x200+des_SPtrans(%ecx),%ebp
  22376. + xorl %ebp, %esi
  22377. + movb %dh, %cl
  22378. + shrl $16, %eax
  22379. + movl 0x100+des_SPtrans(%ebx),%ebp
  22380. + xorl %ebp, %esi
  22381. + movb %ah, %bl
  22382. + shrl $16, %edx
  22383. + movl 0x300+des_SPtrans(%ecx),%ebp
  22384. + xorl %ebp, %esi
  22385. + movl 24(%esp), %ebp
  22386. + movb %dh, %cl
  22387. + andl $0xff, %eax
  22388. + andl $0xff, %edx
  22389. + movl 0x600+des_SPtrans(%ebx),%ebx
  22390. + xorl %ebx, %esi
  22391. + movl 0x700+des_SPtrans(%ecx),%ebx
  22392. + xorl %ebx, %esi
  22393. + movl 0x400+des_SPtrans(%eax),%ebx
  22394. + xorl %ebx, %esi
  22395. + movl 0x500+des_SPtrans(%edx),%ebx
  22396. + xorl %ebx, %esi
  22397. +.L003end:
  22398. +
  22399. +
  22400. + rorl $3, %edi
  22401. + movl 20(%esp), %eax
  22402. + rorl $3, %esi
  22403. + movl %edi, (%eax)
  22404. + movl %esi, 4(%eax)
  22405. + popl %ebp
  22406. + popl %ebx
  22407. + popl %edi
  22408. + popl %esi
  22409. + ret
  22410. +.des_encrypt2_end:
  22411. + .size des_encrypt2 , .des_encrypt2_end-des_encrypt2
  22412. +.ident "desasm.pl"
  22413. +.text
  22414. + .align 16
  22415. +.globl des_encrypt3
  22416. + .type des_encrypt3 , @function
  22417. +des_encrypt3:
  22418. + pushl %ebx
  22419. + movl 8(%esp), %ebx
  22420. + pushl %ebp
  22421. + pushl %esi
  22422. + pushl %edi
  22423. +
  22424. +
  22425. + movl (%ebx), %edi
  22426. + movl 4(%ebx), %esi
  22427. + subl $12, %esp
  22428. +
  22429. +
  22430. + roll $4, %edi
  22431. + movl %edi, %edx
  22432. + xorl %esi, %edi
  22433. + andl $0xf0f0f0f0, %edi
  22434. + xorl %edi, %edx
  22435. + xorl %edi, %esi
  22436. +
  22437. + roll $20, %esi
  22438. + movl %esi, %edi
  22439. + xorl %edx, %esi
  22440. + andl $0xfff0000f, %esi
  22441. + xorl %esi, %edi
  22442. + xorl %esi, %edx
  22443. +
  22444. + roll $14, %edi
  22445. + movl %edi, %esi
  22446. + xorl %edx, %edi
  22447. + andl $0x33333333, %edi
  22448. + xorl %edi, %esi
  22449. + xorl %edi, %edx
  22450. +
  22451. + roll $22, %edx
  22452. + movl %edx, %edi
  22453. + xorl %esi, %edx
  22454. + andl $0x03fc03fc, %edx
  22455. + xorl %edx, %edi
  22456. + xorl %edx, %esi
  22457. +
  22458. + roll $9, %edi
  22459. + movl %edi, %edx
  22460. + xorl %esi, %edi
  22461. + andl $0xaaaaaaaa, %edi
  22462. + xorl %edi, %edx
  22463. + xorl %edi, %esi
  22464. +
  22465. + rorl $3, %edx
  22466. + rorl $2, %esi
  22467. + movl %esi, 4(%ebx)
  22468. + movl 36(%esp), %eax
  22469. + movl %edx, (%ebx)
  22470. + movl 40(%esp), %edi
  22471. + movl 44(%esp), %esi
  22472. + movl $1, 8(%esp)
  22473. + movl %eax, 4(%esp)
  22474. + movl %ebx, (%esp)
  22475. + call des_encrypt2
  22476. + movl $0, 8(%esp)
  22477. + movl %edi, 4(%esp)
  22478. + movl %ebx, (%esp)
  22479. + call des_encrypt2
  22480. + movl $1, 8(%esp)
  22481. + movl %esi, 4(%esp)
  22482. + movl %ebx, (%esp)
  22483. + call des_encrypt2
  22484. + addl $12, %esp
  22485. + movl (%ebx), %edi
  22486. + movl 4(%ebx), %esi
  22487. +
  22488. +
  22489. + roll $2, %esi
  22490. + roll $3, %edi
  22491. + movl %edi, %eax
  22492. + xorl %esi, %edi
  22493. + andl $0xaaaaaaaa, %edi
  22494. + xorl %edi, %eax
  22495. + xorl %edi, %esi
  22496. +
  22497. + roll $23, %eax
  22498. + movl %eax, %edi
  22499. + xorl %esi, %eax
  22500. + andl $0x03fc03fc, %eax
  22501. + xorl %eax, %edi
  22502. + xorl %eax, %esi
  22503. +
  22504. + roll $10, %edi
  22505. + movl %edi, %eax
  22506. + xorl %esi, %edi
  22507. + andl $0x33333333, %edi
  22508. + xorl %edi, %eax
  22509. + xorl %edi, %esi
  22510. +
  22511. + roll $18, %esi
  22512. + movl %esi, %edi
  22513. + xorl %eax, %esi
  22514. + andl $0xfff0000f, %esi
  22515. + xorl %esi, %edi
  22516. + xorl %esi, %eax
  22517. +
  22518. + roll $12, %edi
  22519. + movl %edi, %esi
  22520. + xorl %eax, %edi
  22521. + andl $0xf0f0f0f0, %edi
  22522. + xorl %edi, %esi
  22523. + xorl %edi, %eax
  22524. +
  22525. + rorl $4, %eax
  22526. + movl %eax, (%ebx)
  22527. + movl %esi, 4(%ebx)
  22528. + popl %edi
  22529. + popl %esi
  22530. + popl %ebp
  22531. + popl %ebx
  22532. + ret
  22533. +.des_encrypt3_end:
  22534. + .size des_encrypt3 , .des_encrypt3_end-des_encrypt3
  22535. +.ident "desasm.pl"
  22536. +.text
  22537. + .align 16
  22538. +.globl des_decrypt3
  22539. + .type des_decrypt3 , @function
  22540. +des_decrypt3:
  22541. + pushl %ebx
  22542. + movl 8(%esp), %ebx
  22543. + pushl %ebp
  22544. + pushl %esi
  22545. + pushl %edi
  22546. +
  22547. +
  22548. + movl (%ebx), %edi
  22549. + movl 4(%ebx), %esi
  22550. + subl $12, %esp
  22551. +
  22552. +
  22553. + roll $4, %edi
  22554. + movl %edi, %edx
  22555. + xorl %esi, %edi
  22556. + andl $0xf0f0f0f0, %edi
  22557. + xorl %edi, %edx
  22558. + xorl %edi, %esi
  22559. +
  22560. + roll $20, %esi
  22561. + movl %esi, %edi
  22562. + xorl %edx, %esi
  22563. + andl $0xfff0000f, %esi
  22564. + xorl %esi, %edi
  22565. + xorl %esi, %edx
  22566. +
  22567. + roll $14, %edi
  22568. + movl %edi, %esi
  22569. + xorl %edx, %edi
  22570. + andl $0x33333333, %edi
  22571. + xorl %edi, %esi
  22572. + xorl %edi, %edx
  22573. +
  22574. + roll $22, %edx
  22575. + movl %edx, %edi
  22576. + xorl %esi, %edx
  22577. + andl $0x03fc03fc, %edx
  22578. + xorl %edx, %edi
  22579. + xorl %edx, %esi
  22580. +
  22581. + roll $9, %edi
  22582. + movl %edi, %edx
  22583. + xorl %esi, %edi
  22584. + andl $0xaaaaaaaa, %edi
  22585. + xorl %edi, %edx
  22586. + xorl %edi, %esi
  22587. +
  22588. + rorl $3, %edx
  22589. + rorl $2, %esi
  22590. + movl %esi, 4(%ebx)
  22591. + movl 36(%esp), %esi
  22592. + movl %edx, (%ebx)
  22593. + movl 40(%esp), %edi
  22594. + movl 44(%esp), %eax
  22595. + movl $0, 8(%esp)
  22596. + movl %eax, 4(%esp)
  22597. + movl %ebx, (%esp)
  22598. + call des_encrypt2
  22599. + movl $1, 8(%esp)
  22600. + movl %edi, 4(%esp)
  22601. + movl %ebx, (%esp)
  22602. + call des_encrypt2
  22603. + movl $0, 8(%esp)
  22604. + movl %esi, 4(%esp)
  22605. + movl %ebx, (%esp)
  22606. + call des_encrypt2
  22607. + addl $12, %esp
  22608. + movl (%ebx), %edi
  22609. + movl 4(%ebx), %esi
  22610. +
  22611. +
  22612. + roll $2, %esi
  22613. + roll $3, %edi
  22614. + movl %edi, %eax
  22615. + xorl %esi, %edi
  22616. + andl $0xaaaaaaaa, %edi
  22617. + xorl %edi, %eax
  22618. + xorl %edi, %esi
  22619. +
  22620. + roll $23, %eax
  22621. + movl %eax, %edi
  22622. + xorl %esi, %eax
  22623. + andl $0x03fc03fc, %eax
  22624. + xorl %eax, %edi
  22625. + xorl %eax, %esi
  22626. +
  22627. + roll $10, %edi
  22628. + movl %edi, %eax
  22629. + xorl %esi, %edi
  22630. + andl $0x33333333, %edi
  22631. + xorl %edi, %eax
  22632. + xorl %edi, %esi
  22633. +
  22634. + roll $18, %esi
  22635. + movl %esi, %edi
  22636. + xorl %eax, %esi
  22637. + andl $0xfff0000f, %esi
  22638. + xorl %esi, %edi
  22639. + xorl %esi, %eax
  22640. +
  22641. + roll $12, %edi
  22642. + movl %edi, %esi
  22643. + xorl %eax, %edi
  22644. + andl $0xf0f0f0f0, %edi
  22645. + xorl %edi, %esi
  22646. + xorl %edi, %eax
  22647. +
  22648. + rorl $4, %eax
  22649. + movl %eax, (%ebx)
  22650. + movl %esi, 4(%ebx)
  22651. + popl %edi
  22652. + popl %esi
  22653. + popl %ebp
  22654. + popl %ebx
  22655. + ret
  22656. +.des_decrypt3_end:
  22657. + .size des_decrypt3 , .des_decrypt3_end-des_decrypt3
  22658. +.ident "desasm.pl"
  22659. +.text
  22660. + .align 16
  22661. +.globl des_ncbc_encrypt
  22662. + .type des_ncbc_encrypt , @function
  22663. +des_ncbc_encrypt:
  22664. +
  22665. + pushl %ebp
  22666. + pushl %ebx
  22667. + pushl %esi
  22668. + pushl %edi
  22669. + movl 28(%esp), %ebp
  22670. +
  22671. + movl 36(%esp), %ebx
  22672. + movl (%ebx), %esi
  22673. + movl 4(%ebx), %edi
  22674. + pushl %edi
  22675. + pushl %esi
  22676. + pushl %edi
  22677. + pushl %esi
  22678. + movl %esp, %ebx
  22679. + movl 36(%esp), %esi
  22680. + movl 40(%esp), %edi
  22681. +
  22682. + movl 56(%esp), %ecx
  22683. +
  22684. + pushl %ecx
  22685. +
  22686. + movl 52(%esp), %eax
  22687. + pushl %eax
  22688. + pushl %ebx
  22689. + cmpl $0, %ecx
  22690. + jz .L004decrypt
  22691. + andl $4294967288, %ebp
  22692. + movl 12(%esp), %eax
  22693. + movl 16(%esp), %ebx
  22694. + jz .L005encrypt_finish
  22695. +.L006encrypt_loop:
  22696. + movl (%esi), %ecx
  22697. + movl 4(%esi), %edx
  22698. + xorl %ecx, %eax
  22699. + xorl %edx, %ebx
  22700. + movl %eax, 12(%esp)
  22701. + movl %ebx, 16(%esp)
  22702. + call des_encrypt
  22703. + movl 12(%esp), %eax
  22704. + movl 16(%esp), %ebx
  22705. + movl %eax, (%edi)
  22706. + movl %ebx, 4(%edi)
  22707. + addl $8, %esi
  22708. + addl $8, %edi
  22709. + subl $8, %ebp
  22710. + jnz .L006encrypt_loop
  22711. +.L005encrypt_finish:
  22712. + movl 56(%esp), %ebp
  22713. + andl $7, %ebp
  22714. + jz .L007finish
  22715. + xorl %ecx, %ecx
  22716. + xorl %edx, %edx
  22717. + movl .L008cbc_enc_jmp_table(,%ebp,4),%ebp
  22718. + jmp *%ebp
  22719. +.L009ej7:
  22720. + movb 6(%esi), %dh
  22721. + sall $8, %edx
  22722. +.L010ej6:
  22723. + movb 5(%esi), %dh
  22724. +.L011ej5:
  22725. + movb 4(%esi), %dl
  22726. +.L012ej4:
  22727. + movl (%esi), %ecx
  22728. + jmp .L013ejend
  22729. +.L014ej3:
  22730. + movb 2(%esi), %ch
  22731. + sall $8, %ecx
  22732. +.L015ej2:
  22733. + movb 1(%esi), %ch
  22734. +.L016ej1:
  22735. + movb (%esi), %cl
  22736. +.L013ejend:
  22737. + xorl %ecx, %eax
  22738. + xorl %edx, %ebx
  22739. + movl %eax, 12(%esp)
  22740. + movl %ebx, 16(%esp)
  22741. + call des_encrypt
  22742. + movl 12(%esp), %eax
  22743. + movl 16(%esp), %ebx
  22744. + movl %eax, (%edi)
  22745. + movl %ebx, 4(%edi)
  22746. + jmp .L007finish
  22747. +.align 16
  22748. +.L004decrypt:
  22749. + andl $4294967288, %ebp
  22750. + movl 20(%esp), %eax
  22751. + movl 24(%esp), %ebx
  22752. + jz .L017decrypt_finish
  22753. +.L018decrypt_loop:
  22754. + movl (%esi), %eax
  22755. + movl 4(%esi), %ebx
  22756. + movl %eax, 12(%esp)
  22757. + movl %ebx, 16(%esp)
  22758. + call des_encrypt
  22759. + movl 12(%esp), %eax
  22760. + movl 16(%esp), %ebx
  22761. + movl 20(%esp), %ecx
  22762. + movl 24(%esp), %edx
  22763. + xorl %eax, %ecx
  22764. + xorl %ebx, %edx
  22765. + movl (%esi), %eax
  22766. + movl 4(%esi), %ebx
  22767. + movl %ecx, (%edi)
  22768. + movl %edx, 4(%edi)
  22769. + movl %eax, 20(%esp)
  22770. + movl %ebx, 24(%esp)
  22771. + addl $8, %esi
  22772. + addl $8, %edi
  22773. + subl $8, %ebp
  22774. + jnz .L018decrypt_loop
  22775. +.L017decrypt_finish:
  22776. + movl 56(%esp), %ebp
  22777. + andl $7, %ebp
  22778. + jz .L007finish
  22779. + movl (%esi), %eax
  22780. + movl 4(%esi), %ebx
  22781. + movl %eax, 12(%esp)
  22782. + movl %ebx, 16(%esp)
  22783. + call des_encrypt
  22784. + movl 12(%esp), %eax
  22785. + movl 16(%esp), %ebx
  22786. + movl 20(%esp), %ecx
  22787. + movl 24(%esp), %edx
  22788. + xorl %eax, %ecx
  22789. + xorl %ebx, %edx
  22790. + movl (%esi), %eax
  22791. + movl 4(%esi), %ebx
  22792. +.L019dj7:
  22793. + rorl $16, %edx
  22794. + movb %dl, 6(%edi)
  22795. + shrl $16, %edx
  22796. +.L020dj6:
  22797. + movb %dh, 5(%edi)
  22798. +.L021dj5:
  22799. + movb %dl, 4(%edi)
  22800. +.L022dj4:
  22801. + movl %ecx, (%edi)
  22802. + jmp .L023djend
  22803. +.L024dj3:
  22804. + rorl $16, %ecx
  22805. + movb %cl, 2(%edi)
  22806. + sall $16, %ecx
  22807. +.L025dj2:
  22808. + movb %ch, 1(%esi)
  22809. +.L026dj1:
  22810. + movb %cl, (%esi)
  22811. +.L023djend:
  22812. + jmp .L007finish
  22813. +.align 16
  22814. +.L007finish:
  22815. + movl 64(%esp), %ecx
  22816. + addl $28, %esp
  22817. + movl %eax, (%ecx)
  22818. + movl %ebx, 4(%ecx)
  22819. + popl %edi
  22820. + popl %esi
  22821. + popl %ebx
  22822. + popl %ebp
  22823. + ret
  22824. +.align 16
  22825. +.L008cbc_enc_jmp_table:
  22826. + .long 0
  22827. + .long .L016ej1
  22828. + .long .L015ej2
  22829. + .long .L014ej3
  22830. + .long .L012ej4
  22831. + .long .L011ej5
  22832. + .long .L010ej6
  22833. + .long .L009ej7
  22834. +.align 16
  22835. +.L027cbc_dec_jmp_table:
  22836. + .long 0
  22837. + .long .L026dj1
  22838. + .long .L025dj2
  22839. + .long .L024dj3
  22840. + .long .L022dj4
  22841. + .long .L021dj5
  22842. + .long .L020dj6
  22843. + .long .L019dj7
  22844. +.des_ncbc_encrypt_end:
  22845. + .size des_ncbc_encrypt , .des_ncbc_encrypt_end-des_ncbc_encrypt
  22846. +.ident "desasm.pl"
  22847. +.text
  22848. + .align 16
  22849. +.globl des_ede3_cbc_encrypt
  22850. + .type des_ede3_cbc_encrypt , @function
  22851. +des_ede3_cbc_encrypt:
  22852. +
  22853. + pushl %ebp
  22854. + pushl %ebx
  22855. + pushl %esi
  22856. + pushl %edi
  22857. + movl 28(%esp), %ebp
  22858. +
  22859. + movl 44(%esp), %ebx
  22860. + movl (%ebx), %esi
  22861. + movl 4(%ebx), %edi
  22862. + pushl %edi
  22863. + pushl %esi
  22864. + pushl %edi
  22865. + pushl %esi
  22866. + movl %esp, %ebx
  22867. + movl 36(%esp), %esi
  22868. + movl 40(%esp), %edi
  22869. +
  22870. + movl 64(%esp), %ecx
  22871. +
  22872. + movl 56(%esp), %eax
  22873. + pushl %eax
  22874. +
  22875. + movl 56(%esp), %eax
  22876. + pushl %eax
  22877. +
  22878. + movl 56(%esp), %eax
  22879. + pushl %eax
  22880. + pushl %ebx
  22881. + cmpl $0, %ecx
  22882. + jz .L028decrypt
  22883. + andl $4294967288, %ebp
  22884. + movl 16(%esp), %eax
  22885. + movl 20(%esp), %ebx
  22886. + jz .L029encrypt_finish
  22887. +.L030encrypt_loop:
  22888. + movl (%esi), %ecx
  22889. + movl 4(%esi), %edx
  22890. + xorl %ecx, %eax
  22891. + xorl %edx, %ebx
  22892. + movl %eax, 16(%esp)
  22893. + movl %ebx, 20(%esp)
  22894. + call des_encrypt3
  22895. + movl 16(%esp), %eax
  22896. + movl 20(%esp), %ebx
  22897. + movl %eax, (%edi)
  22898. + movl %ebx, 4(%edi)
  22899. + addl $8, %esi
  22900. + addl $8, %edi
  22901. + subl $8, %ebp
  22902. + jnz .L030encrypt_loop
  22903. +.L029encrypt_finish:
  22904. + movl 60(%esp), %ebp
  22905. + andl $7, %ebp
  22906. + jz .L031finish
  22907. + xorl %ecx, %ecx
  22908. + xorl %edx, %edx
  22909. + movl .L032cbc_enc_jmp_table(,%ebp,4),%ebp
  22910. + jmp *%ebp
  22911. +.L033ej7:
  22912. + movb 6(%esi), %dh
  22913. + sall $8, %edx
  22914. +.L034ej6:
  22915. + movb 5(%esi), %dh
  22916. +.L035ej5:
  22917. + movb 4(%esi), %dl
  22918. +.L036ej4:
  22919. + movl (%esi), %ecx
  22920. + jmp .L037ejend
  22921. +.L038ej3:
  22922. + movb 2(%esi), %ch
  22923. + sall $8, %ecx
  22924. +.L039ej2:
  22925. + movb 1(%esi), %ch
  22926. +.L040ej1:
  22927. + movb (%esi), %cl
  22928. +.L037ejend:
  22929. + xorl %ecx, %eax
  22930. + xorl %edx, %ebx
  22931. + movl %eax, 16(%esp)
  22932. + movl %ebx, 20(%esp)
  22933. + call des_encrypt3
  22934. + movl 16(%esp), %eax
  22935. + movl 20(%esp), %ebx
  22936. + movl %eax, (%edi)
  22937. + movl %ebx, 4(%edi)
  22938. + jmp .L031finish
  22939. +.align 16
  22940. +.L028decrypt:
  22941. + andl $4294967288, %ebp
  22942. + movl 24(%esp), %eax
  22943. + movl 28(%esp), %ebx
  22944. + jz .L041decrypt_finish
  22945. +.L042decrypt_loop:
  22946. + movl (%esi), %eax
  22947. + movl 4(%esi), %ebx
  22948. + movl %eax, 16(%esp)
  22949. + movl %ebx, 20(%esp)
  22950. + call des_decrypt3
  22951. + movl 16(%esp), %eax
  22952. + movl 20(%esp), %ebx
  22953. + movl 24(%esp), %ecx
  22954. + movl 28(%esp), %edx
  22955. + xorl %eax, %ecx
  22956. + xorl %ebx, %edx
  22957. + movl (%esi), %eax
  22958. + movl 4(%esi), %ebx
  22959. + movl %ecx, (%edi)
  22960. + movl %edx, 4(%edi)
  22961. + movl %eax, 24(%esp)
  22962. + movl %ebx, 28(%esp)
  22963. + addl $8, %esi
  22964. + addl $8, %edi
  22965. + subl $8, %ebp
  22966. + jnz .L042decrypt_loop
  22967. +.L041decrypt_finish:
  22968. + movl 60(%esp), %ebp
  22969. + andl $7, %ebp
  22970. + jz .L031finish
  22971. + movl (%esi), %eax
  22972. + movl 4(%esi), %ebx
  22973. + movl %eax, 16(%esp)
  22974. + movl %ebx, 20(%esp)
  22975. + call des_decrypt3
  22976. + movl 16(%esp), %eax
  22977. + movl 20(%esp), %ebx
  22978. + movl 24(%esp), %ecx
  22979. + movl 28(%esp), %edx
  22980. + xorl %eax, %ecx
  22981. + xorl %ebx, %edx
  22982. + movl (%esi), %eax
  22983. + movl 4(%esi), %ebx
  22984. +.L043dj7:
  22985. + rorl $16, %edx
  22986. + movb %dl, 6(%edi)
  22987. + shrl $16, %edx
  22988. +.L044dj6:
  22989. + movb %dh, 5(%edi)
  22990. +.L045dj5:
  22991. + movb %dl, 4(%edi)
  22992. +.L046dj4:
  22993. + movl %ecx, (%edi)
  22994. + jmp .L047djend
  22995. +.L048dj3:
  22996. + rorl $16, %ecx
  22997. + movb %cl, 2(%edi)
  22998. + sall $16, %ecx
  22999. +.L049dj2:
  23000. + movb %ch, 1(%esi)
  23001. +.L050dj1:
  23002. + movb %cl, (%esi)
  23003. +.L047djend:
  23004. + jmp .L031finish
  23005. +.align 16
  23006. +.L031finish:
  23007. + movl 76(%esp), %ecx
  23008. + addl $32, %esp
  23009. + movl %eax, (%ecx)
  23010. + movl %ebx, 4(%ecx)
  23011. + popl %edi
  23012. + popl %esi
  23013. + popl %ebx
  23014. + popl %ebp
  23015. + ret
  23016. +.align 16
  23017. +.L032cbc_enc_jmp_table:
  23018. + .long 0
  23019. + .long .L040ej1
  23020. + .long .L039ej2
  23021. + .long .L038ej3
  23022. + .long .L036ej4
  23023. + .long .L035ej5
  23024. + .long .L034ej6
  23025. + .long .L033ej7
  23026. +.align 16
  23027. +.L051cbc_dec_jmp_table:
  23028. + .long 0
  23029. + .long .L050dj1
  23030. + .long .L049dj2
  23031. + .long .L048dj3
  23032. + .long .L046dj4
  23033. + .long .L045dj5
  23034. + .long .L044dj6
  23035. + .long .L043dj7
  23036. +.des_ede3_cbc_encrypt_end:
  23037. + .size des_ede3_cbc_encrypt , .des_ede3_cbc_encrypt_end-des_ede3_cbc_encrypt
  23038. +.ident "desasm.pl"
  23039. --- /dev/null Tue Mar 11 13:02:56 2003
  23040. +++ linux/net/ipsec/des/ecb_enc.c Mon Feb 9 13:51:03 2004
  23041. @@ -0,0 +1,128 @@
  23042. +/* crypto/des/ecb_enc.c */
  23043. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  23044. + * All rights reserved.
  23045. + *
  23046. + * This package is an SSL implementation written
  23047. + * by Eric Young (eay@cryptsoft.com).
  23048. + * The implementation was written so as to conform with Netscapes SSL.
  23049. + *
  23050. + * This library is free for commercial and non-commercial use as long as
  23051. + * the following conditions are aheared to. The following conditions
  23052. + * apply to all code found in this distribution, be it the RC4, RSA,
  23053. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  23054. + * included with this distribution is covered by the same copyright terms
  23055. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  23056. + *
  23057. + * Copyright remains Eric Young's, and as such any Copyright notices in
  23058. + * the code are not to be removed.
  23059. + * If this package is used in a product, Eric Young should be given attribution
  23060. + * as the author of the parts of the library used.
  23061. + * This can be in the form of a textual message at program startup or
  23062. + * in documentation (online or textual) provided with the package.
  23063. + *
  23064. + * Redistribution and use in source and binary forms, with or without
  23065. + * modification, are permitted provided that the following conditions
  23066. + * are met:
  23067. + * 1. Redistributions of source code must retain the copyright
  23068. + * notice, this list of conditions and the following disclaimer.
  23069. + * 2. Redistributions in binary form must reproduce the above copyright
  23070. + * notice, this list of conditions and the following disclaimer in the
  23071. + * documentation and/or other materials provided with the distribution.
  23072. + * 3. All advertising materials mentioning features or use of this software
  23073. + * must display the following acknowledgement:
  23074. + * "This product includes cryptographic software written by
  23075. + * Eric Young (eay@cryptsoft.com)"
  23076. + * The word 'cryptographic' can be left out if the rouines from the library
  23077. + * being used are not cryptographic related :-).
  23078. + * 4. If you include any Windows specific code (or a derivative thereof) from
  23079. + * the apps directory (application code) you must include an acknowledgement:
  23080. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  23081. + *
  23082. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  23083. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23084. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23085. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  23086. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23087. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23088. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23089. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23090. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23091. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23092. + * SUCH DAMAGE.
  23093. + *
  23094. + * The licence and distribution terms for any publically available version or
  23095. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  23096. + * copied and put under another distribution licence
  23097. + * [including the GNU Public Licence.]
  23098. + */
  23099. +
  23100. +#include "des/des_locl.h"
  23101. +#include "des/spr.h"
  23102. +
  23103. +char *libdes_version="libdes v 3.24 - 20-Apr-1996 - eay";
  23104. +char *DES_version="DES part of SSLeay 0.8.2b 08-Jan-1998";
  23105. +
  23106. +/* RCSID $Id: ecb_enc.c,v 1.8 2004/08/04 15:57:22 mcr Exp $ */
  23107. +/* This function ifdef'ed out for FreeS/WAN project. */
  23108. +#ifdef notdef
  23109. +char *des_options()
  23110. + {
  23111. + static int init=1;
  23112. + static char buf[32];
  23113. +
  23114. + if (init)
  23115. + {
  23116. + char *ptr,*unroll,*risc,*size;
  23117. +
  23118. + init=0;
  23119. +#ifdef DES_PTR
  23120. + ptr="ptr";
  23121. +#else
  23122. + ptr="idx";
  23123. +#endif
  23124. +#if defined(DES_RISC1) || defined(DES_RISC2)
  23125. +#ifdef DES_RISC1
  23126. + risc="risc1";
  23127. +#endif
  23128. +#ifdef DES_RISC2
  23129. + risc="risc2";
  23130. +#endif
  23131. +#else
  23132. + risc="cisc";
  23133. +#endif
  23134. +#ifdef DES_UNROLL
  23135. + unroll="16";
  23136. +#else
  23137. + unroll="4";
  23138. +#endif
  23139. + if (sizeof(DES_LONG) != sizeof(long))
  23140. + size="int";
  23141. + else
  23142. + size="long";
  23143. + sprintf(buf,"des(%s,%s,%s,%s)",ptr,risc,unroll,size);
  23144. + }
  23145. + return(buf);
  23146. + }
  23147. +#endif
  23148. +
  23149. +
  23150. +void des_ecb_encrypt(input, output, ks, enc)
  23151. +des_cblock (*input);
  23152. +des_cblock (*output);
  23153. +des_key_schedule ks;
  23154. +int enc;
  23155. + {
  23156. + register DES_LONG l;
  23157. + register unsigned char *in,*out;
  23158. + DES_LONG ll[2];
  23159. +
  23160. + in=(unsigned char *)input;
  23161. + out=(unsigned char *)output;
  23162. + c2l(in,l); ll[0]=l;
  23163. + c2l(in,l); ll[1]=l;
  23164. + des_encrypt(ll,ks,enc);
  23165. + l=ll[0]; l2c(l,out);
  23166. + l=ll[1]; l2c(l,out);
  23167. + l=ll[0]=ll[1]=0;
  23168. + }
  23169. +
  23170. --- /dev/null Tue Mar 11 13:02:56 2003
  23171. +++ linux/net/ipsec/des/ipsec_alg_3des.c Mon Feb 9 13:51:03 2004
  23172. @@ -0,0 +1,181 @@
  23173. +/*
  23174. + * ipsec_alg 3DES cipher stubs
  23175. + *
  23176. + * Copyright (C) 2005 Michael Richardson <mcr@xelerance.com>
  23177. + *
  23178. + * Adapted from ipsec_alg_aes.c by JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  23179. + *
  23180. + * ipsec_alg_aes.c,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
  23181. + *
  23182. + * This program is free software; you can redistribute it and/or modify it
  23183. + * under the terms of the GNU General Public License as published by the
  23184. + * Free Software Foundation; either version 2 of the License, or (at your
  23185. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  23186. + *
  23187. + * This program is distributed in the hope that it will be useful, but
  23188. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  23189. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  23190. + * for more details.
  23191. + *
  23192. + */
  23193. +#ifndef AUTOCONF_INCLUDED
  23194. +#include <linux/config.h>
  23195. +#endif
  23196. +#include <linux/version.h>
  23197. +
  23198. +/*
  23199. + * special case: ipsec core modular with this static algo inside:
  23200. + * must avoid MODULE magic for this file
  23201. + */
  23202. +#if defined(CONFIG_KLIPS_MODULE) && defined(CONFIG_KLIPS_ENC_3DES)
  23203. +#undef MODULE
  23204. +#endif
  23205. +
  23206. +#include <linux/module.h>
  23207. +#include <linux/init.h>
  23208. +
  23209. +#include <linux/kernel.h> /* printk() */
  23210. +#include <linux/errno.h> /* error codes */
  23211. +#include <linux/types.h> /* size_t */
  23212. +#include <linux/string.h>
  23213. +
  23214. +/* Low freeswan header coupling */
  23215. +#include "openswan/ipsec_xform.h"
  23216. +#include "openswan/ipsec_alg.h"
  23217. +#include "crypto/des.h"
  23218. +#include "openswan/ipsec_alg_3des.h"
  23219. +
  23220. +#define AES_CONTEXT_T aes_context
  23221. +static int debug_3des=0;
  23222. +static int test_3des=0;
  23223. +static int excl_3des=0;
  23224. +
  23225. +#if defined(CONFIG_KLIPS_ENC_3DES_MODULE)
  23226. +MODULE_AUTHOR("Michael Richardson <mcr@xelerance.com>");
  23227. +#ifdef module_param
  23228. +module_param(debug_3des,int,0600)
  23229. +module_param(test_des,int,0600)
  23230. +module_param(excl_des,int,0600)
  23231. +#else
  23232. +MODULE_PARM(debug_3des, "i");
  23233. +MODULE_PARM(test_des, "i");
  23234. +MODULE_PARM(excl_des, "i");
  23235. +#endif
  23236. +#endif
  23237. +
  23238. +#define ESP_AES_MAC_KEY_SZ 16 /* 128 bit MAC key */
  23239. +#define ESP_AES_MAC_BLK_LEN 16 /* 128 bit block */
  23240. +
  23241. +static int _3des_set_key(struct ipsec_alg_enc *alg,
  23242. + __u8 * key_e, const __u8 * key,
  23243. + size_t keysize)
  23244. +{
  23245. + int ret = 0;
  23246. + TripleDES_context *ctx = (TripleDES_context*)key_e;
  23247. +
  23248. + if(keysize != 192/8) {
  23249. + return EINVAL;
  23250. + }
  23251. +
  23252. + des_set_key((des_cblock *)(key + DES_KEY_SZ*0), ctx->s1);
  23253. + des_set_key((des_cblock *)(key + DES_KEY_SZ*1), ctx->s2);
  23254. + des_set_key((des_cblock *)(key + DES_KEY_SZ*2), ctx->s3);
  23255. +
  23256. + if (debug_3des > 0)
  23257. + printk(KERN_DEBUG "klips_debug:_3des_set_key:"
  23258. + "ret=%d key_e=%p key=%p keysize=%ld\n",
  23259. + ret, key_e, key, (unsigned long int) keysize);
  23260. + return ret;
  23261. +}
  23262. +
  23263. +static int _3des_cbc_encrypt(struct ipsec_alg_enc *alg,
  23264. + __u8 * key_e,
  23265. + __u8 * in,
  23266. + int ilen, const __u8 * iv,
  23267. + int encrypt)
  23268. +{
  23269. + TripleDES_context *ctx=(TripleDES_context*)key_e;
  23270. + des_cblock miv;
  23271. +
  23272. + memcpy(&miv, iv, sizeof(miv));
  23273. +
  23274. + if (debug_3des > 0)
  23275. + printk(KERN_DEBUG "klips_debug:_aes_cbc_encrypt:"
  23276. + "key_e=%p in=%p ilen=%d iv=%p encrypt=%d\n",
  23277. + key_e, in, ilen, iv, encrypt);
  23278. +
  23279. + des_ede3_cbc_encrypt((des_cblock *)in,
  23280. + (des_cblock *)in,
  23281. + ilen,
  23282. + ctx->s1,
  23283. + ctx->s2,
  23284. + ctx->s3,
  23285. + &miv, encrypt);
  23286. + return 1;
  23287. +}
  23288. +
  23289. +static struct ipsec_alg_enc ipsec_alg_3DES = {
  23290. + ixt_common: { ixt_version: IPSEC_ALG_VERSION,
  23291. + ixt_refcnt: ATOMIC_INIT(0),
  23292. + ixt_name: "3des",
  23293. + ixt_blocksize: ESP_3DES_CBC_BLK_LEN,
  23294. + ixt_support: {
  23295. + ias_exttype: IPSEC_ALG_TYPE_ENCRYPT,
  23296. + ias_id: ESP_3DES,
  23297. + ias_keyminbits: ESP_3DES_KEY_SZ*8,
  23298. + ias_keymaxbits: ESP_3DES_KEY_SZ*8,
  23299. + },
  23300. + },
  23301. +#if defined(MODULE_KLIPS_ENC_3DES_MODULE)
  23302. + ixt_module: THIS_MODULE,
  23303. +#endif
  23304. + ixt_e_keylen: ESP_3DES_KEY_SZ*8,
  23305. + ixt_e_ctx_size: sizeof(TripleDES_context),
  23306. + ixt_e_set_key: _3des_set_key,
  23307. + ixt_e_cbc_encrypt:_3des_cbc_encrypt,
  23308. +};
  23309. +
  23310. +#if defined(CONFIG_KLIPS_ENC_3DES_MODULE)
  23311. +IPSEC_ALG_MODULE_INIT_MOD( ipsec_3des_init )
  23312. +#else
  23313. +IPSEC_ALG_MODULE_INIT_STATIC( ipsec_3des_init )
  23314. +#endif
  23315. +{
  23316. + int ret, test_ret;
  23317. +
  23318. + if (excl_3des) ipsec_alg_3DES.ixt_common.ixt_state |= IPSEC_ALG_ST_EXCL;
  23319. + ret=register_ipsec_alg_enc(&ipsec_alg_3DES);
  23320. + printk("ipsec_3des_init(alg_type=%d alg_id=%d name=%s): ret=%d\n",
  23321. + ipsec_alg_3DES.ixt_common.ixt_support.ias_exttype,
  23322. + ipsec_alg_3DES.ixt_common.ixt_support.ias_id,
  23323. + ipsec_alg_3DES.ixt_common.ixt_name,
  23324. + ret);
  23325. + if (ret==0 && test_3des) {
  23326. + test_ret=ipsec_alg_test(
  23327. + ipsec_alg_3DES.ixt_common.ixt_support.ias_exttype,
  23328. + ipsec_alg_3DES.ixt_common.ixt_support.ias_id,
  23329. + test_3des);
  23330. + printk("ipsec_aes_init(alg_type=%d alg_id=%d): test_ret=%d\n",
  23331. + ipsec_alg_3DES.ixt_common.ixt_support.ias_exttype,
  23332. + ipsec_alg_3DES.ixt_common.ixt_support.ias_id,
  23333. + test_ret);
  23334. + }
  23335. + return ret;
  23336. +}
  23337. +
  23338. +#if defined(CONFIG_KLIPS_ENC_3DES_MODULE)
  23339. +IPSEC_ALG_MODULE_EXIT_MOD( ipsec_3des_fini )
  23340. +#else
  23341. +IPSEC_ALG_MODULE_EXIT_STATIC( ipsec_3des_fini )
  23342. +#endif
  23343. +{
  23344. + unregister_ipsec_alg_enc(&ipsec_alg_3DES);
  23345. + return;
  23346. +}
  23347. +
  23348. +/* Dual, because 3des code is 4-clause BSD licensed */
  23349. +#ifdef MODULE_LICENSE
  23350. +MODULE_LICENSE("Dual BSD/GPL");
  23351. +#endif
  23352. +
  23353. +
  23354. --- /dev/null Tue Mar 11 13:02:56 2003
  23355. +++ linux/net/ipsec/des/set_key.c Mon Feb 9 13:51:03 2004
  23356. @@ -0,0 +1,246 @@
  23357. +/* crypto/des/set_key.c */
  23358. +/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  23359. + * All rights reserved.
  23360. + *
  23361. + * This package is an SSL implementation written
  23362. + * by Eric Young (eay@cryptsoft.com).
  23363. + * The implementation was written so as to conform with Netscapes SSL.
  23364. + *
  23365. + * This library is free for commercial and non-commercial use as long as
  23366. + * the following conditions are aheared to. The following conditions
  23367. + * apply to all code found in this distribution, be it the RC4, RSA,
  23368. + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  23369. + * included with this distribution is covered by the same copyright terms
  23370. + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  23371. + *
  23372. + * Copyright remains Eric Young's, and as such any Copyright notices in
  23373. + * the code are not to be removed.
  23374. + * If this package is used in a product, Eric Young should be given attribution
  23375. + * as the author of the parts of the library used.
  23376. + * This can be in the form of a textual message at program startup or
  23377. + * in documentation (online or textual) provided with the package.
  23378. + *
  23379. + * Redistribution and use in source and binary forms, with or without
  23380. + * modification, are permitted provided that the following conditions
  23381. + * are met:
  23382. + * 1. Redistributions of source code must retain the copyright
  23383. + * notice, this list of conditions and the following disclaimer.
  23384. + * 2. Redistributions in binary form must reproduce the above copyright
  23385. + * notice, this list of conditions and the following disclaimer in the
  23386. + * documentation and/or other materials provided with the distribution.
  23387. + * 3. All advertising materials mentioning features or use of this software
  23388. + * must display the following acknowledgement:
  23389. + * "This product includes cryptographic software written by
  23390. + * Eric Young (eay@cryptsoft.com)"
  23391. + * The word 'cryptographic' can be left out if the rouines from the library
  23392. + * being used are not cryptographic related :-).
  23393. + * 4. If you include any Windows specific code (or a derivative thereof) from
  23394. + * the apps directory (application code) you must include an acknowledgement:
  23395. + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  23396. + *
  23397. + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  23398. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23399. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23400. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  23401. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23402. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23403. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23404. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23405. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23406. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23407. + * SUCH DAMAGE.
  23408. + *
  23409. + * The licence and distribution terms for any publically available version or
  23410. + * derivative of this code cannot be changed. i.e. this code cannot simply be
  23411. + * copied and put under another distribution licence
  23412. + * [including the GNU Public Licence.]
  23413. + */
  23414. +
  23415. +/* set_key.c v 1.4 eay 24/9/91
  23416. + * 1.4 Speed up by 400% :-)
  23417. + * 1.3 added register declarations.
  23418. + * 1.2 unrolled make_key_sched a bit more
  23419. + * 1.1 added norm_expand_bits
  23420. + * 1.0 First working version
  23421. + */
  23422. +#include "des/des_locl.h"
  23423. +#include "des/podd.h"
  23424. +#include "des/sk.h"
  23425. +
  23426. +#ifndef NOPROTO
  23427. +static int check_parity(des_cblock (*key));
  23428. +#else
  23429. +static int check_parity();
  23430. +#endif
  23431. +
  23432. +int des_check_key=0;
  23433. +
  23434. +void des_set_odd_parity(key)
  23435. +des_cblock (*key);
  23436. + {
  23437. + int i;
  23438. +
  23439. + for (i=0; i<DES_KEY_SZ; i++)
  23440. + (*key)[i]=odd_parity[(*key)[i]];
  23441. + }
  23442. +
  23443. +static int check_parity(key)
  23444. +des_cblock (*key);
  23445. + {
  23446. + int i;
  23447. +
  23448. + for (i=0; i<DES_KEY_SZ; i++)
  23449. + {
  23450. + if ((*key)[i] != odd_parity[(*key)[i]])
  23451. + return(0);
  23452. + }
  23453. + return(1);
  23454. + }
  23455. +
  23456. +/* Weak and semi week keys as take from
  23457. + * %A D.W. Davies
  23458. + * %A W.L. Price
  23459. + * %T Security for Computer Networks
  23460. + * %I John Wiley & Sons
  23461. + * %D 1984
  23462. + * Many thanks to smb@ulysses.att.com (Steven Bellovin) for the reference
  23463. + * (and actual cblock values).
  23464. + */
  23465. +#define NUM_WEAK_KEY 16
  23466. +static des_cblock weak_keys[NUM_WEAK_KEY]={
  23467. + /* weak keys */
  23468. + {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
  23469. + {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
  23470. + {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F},
  23471. + {0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0},
  23472. + /* semi-weak keys */
  23473. + {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
  23474. + {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
  23475. + {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
  23476. + {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
  23477. + {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
  23478. + {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
  23479. + {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
  23480. + {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
  23481. + {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
  23482. + {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
  23483. + {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
  23484. + {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
  23485. +
  23486. +int des_is_weak_key(key)
  23487. +des_cblock (*key);
  23488. + {
  23489. + int i;
  23490. +
  23491. + for (i=0; i<NUM_WEAK_KEY; i++)
  23492. + /* Added == 0 to comparision, I obviously don't run
  23493. + * this section very often :-(, thanks to
  23494. + * engineering@MorningStar.Com for the fix
  23495. + * eay 93/06/29
  23496. + * Another problem, I was comparing only the first 4
  23497. + * bytes, 97/03/18 */
  23498. + if (memcmp(weak_keys[i],key,sizeof(des_cblock)) == 0) return(1);
  23499. + return(0);
  23500. + }
  23501. +
  23502. +/* NOW DEFINED IN des_local.h
  23503. + * See ecb_encrypt.c for a pseudo description of these macros.
  23504. + * #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
  23505. + * (b)^=(t),\
  23506. + * (a)=((a)^((t)<<(n))))
  23507. + */
  23508. +
  23509. +#define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
  23510. + (a)=(a)^(t)^(t>>(16-(n))))
  23511. +
  23512. +/* return 0 if key parity is odd (correct),
  23513. + * return -1 if key parity error,
  23514. + * return -2 if illegal weak key.
  23515. + */
  23516. +int des_set_key(key, schedule)
  23517. +des_cblock (*key);
  23518. +des_key_schedule schedule;
  23519. + {
  23520. + static int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};
  23521. + register DES_LONG c,d,t,s,t2;
  23522. + register unsigned char *in;
  23523. + register DES_LONG *k;
  23524. + register int i;
  23525. +
  23526. + if (des_check_key)
  23527. + {
  23528. + if (!check_parity(key))
  23529. + return(-1);
  23530. +
  23531. + if (des_is_weak_key(key))
  23532. + return(-2);
  23533. + }
  23534. +
  23535. + k=(DES_LONG *)schedule;
  23536. + in=(unsigned char *)key;
  23537. +
  23538. + c2l(in,c);
  23539. + c2l(in,d);
  23540. +
  23541. + /* do PC1 in 60 simple operations */
  23542. +/* PERM_OP(d,c,t,4,0x0f0f0f0fL);
  23543. + HPERM_OP(c,t,-2, 0xcccc0000L);
  23544. + HPERM_OP(c,t,-1, 0xaaaa0000L);
  23545. + HPERM_OP(c,t, 8, 0x00ff0000L);
  23546. + HPERM_OP(c,t,-1, 0xaaaa0000L);
  23547. + HPERM_OP(d,t,-8, 0xff000000L);
  23548. + HPERM_OP(d,t, 8, 0x00ff0000L);
  23549. + HPERM_OP(d,t, 2, 0x33330000L);
  23550. + d=((d&0x00aa00aaL)<<7L)|((d&0x55005500L)>>7L)|(d&0xaa55aa55L);
  23551. + d=(d>>8)|((c&0xf0000000L)>>4);
  23552. + c&=0x0fffffffL; */
  23553. +
  23554. + /* I now do it in 47 simple operations :-)
  23555. + * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)
  23556. + * for the inspiration. :-) */
  23557. + PERM_OP (d,c,t,4,0x0f0f0f0fL);
  23558. + HPERM_OP(c,t,-2,0xcccc0000L);
  23559. + HPERM_OP(d,t,-2,0xcccc0000L);
  23560. + PERM_OP (d,c,t,1,0x55555555L);
  23561. + PERM_OP (c,d,t,8,0x00ff00ffL);
  23562. + PERM_OP (d,c,t,1,0x55555555L);
  23563. + d= (((d&0x000000ffL)<<16L)| (d&0x0000ff00L) |
  23564. + ((d&0x00ff0000L)>>16L)|((c&0xf0000000L)>>4L));
  23565. + c&=0x0fffffffL;
  23566. +
  23567. + for (i=0; i<ITERATIONS; i++)
  23568. + {
  23569. + if (shifts2[i])
  23570. + { c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); }
  23571. + else
  23572. + { c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); }
  23573. + c&=0x0fffffffL;
  23574. + d&=0x0fffffffL;
  23575. + /* could be a few less shifts but I am to lazy at this
  23576. + * point in time to investigate */
  23577. + s= des_skb[0][ (c )&0x3f ]|
  23578. + des_skb[1][((c>> 6)&0x03)|((c>> 7L)&0x3c)]|
  23579. + des_skb[2][((c>>13)&0x0f)|((c>>14L)&0x30)]|
  23580. + des_skb[3][((c>>20)&0x01)|((c>>21L)&0x06) |
  23581. + ((c>>22L)&0x38)];
  23582. + t= des_skb[4][ (d )&0x3f ]|
  23583. + des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|
  23584. + des_skb[6][ (d>>15L)&0x3f ]|
  23585. + des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)];
  23586. +
  23587. + /* table contained 0213 4657 */
  23588. + t2=((t<<16L)|(s&0x0000ffffL))&0xffffffffL;
  23589. + *(k++)=ROTATE(t2,30)&0xffffffffL;
  23590. +
  23591. + t2=((s>>16L)|(t&0xffff0000L));
  23592. + *(k++)=ROTATE(t2,26)&0xffffffffL;
  23593. + }
  23594. + return(0);
  23595. + }
  23596. +
  23597. +int des_key_sched(key, schedule)
  23598. +des_cblock (*key);
  23599. +des_key_schedule schedule;
  23600. + {
  23601. + return(des_set_key(key,schedule));
  23602. + }
  23603. --- /dev/null Tue Mar 11 13:02:56 2003
  23604. +++ linux/net/ipsec/goodmask.c Mon Feb 9 13:51:03 2004
  23605. @@ -0,0 +1,100 @@
  23606. +/*
  23607. + * minor utilities for subnet-mask manipulation
  23608. + * Copyright (C) 1998, 1999 Henry Spencer.
  23609. + *
  23610. + * This library is free software; you can redistribute it and/or modify it
  23611. + * under the terms of the GNU Library General Public License as published by
  23612. + * the Free Software Foundation; either version 2 of the License, or (at your
  23613. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  23614. + *
  23615. + * This library is distributed in the hope that it will be useful, but
  23616. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  23617. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  23618. + * License for more details.
  23619. + *
  23620. + * RCSID $Id: goodmask.c,v 1.12 2004/07/10 07:43:47 mcr Exp $
  23621. + */
  23622. +#include "openswan.h"
  23623. +
  23624. +#ifndef ABITS
  23625. +#define ABITS 32 /* bits in an IPv4 address */
  23626. +#endif
  23627. +
  23628. +/*
  23629. + - goodmask - is this a good (^1*0*$) subnet mask?
  23630. + * You are not expected to understand this. See Henry S. Warren Jr,
  23631. + * "Functions realizable with word-parallel logical and two's-complement
  23632. + * addition instructions", CACM 20.6 (June 1977), p.439.
  23633. + */
  23634. +int /* predicate */
  23635. +goodmask(mask)
  23636. +struct in_addr mask;
  23637. +{
  23638. + unsigned long x = ntohl(mask.s_addr);
  23639. + /* clear rightmost contiguous string of 1-bits */
  23640. +# define CRCS1B(x) (((x|(x-1))+1)&x)
  23641. +# define TOPBIT (1UL << 31)
  23642. +
  23643. + /* either zero, or has one string of 1-bits which is left-justified */
  23644. + if (x == 0 || (CRCS1B(x) == 0 && (x&TOPBIT)))
  23645. + return 1;
  23646. + return 0;
  23647. +}
  23648. +
  23649. +/*
  23650. + - masktobits - how many bits in this mask?
  23651. + * The algorithm is essentially a binary search, but highly optimized
  23652. + * for this particular task.
  23653. + */
  23654. +int /* -1 means !goodmask() */
  23655. +masktobits(mask)
  23656. +struct in_addr mask;
  23657. +{
  23658. + unsigned long m = ntohl(mask.s_addr);
  23659. + int masklen;
  23660. +
  23661. + if (!goodmask(mask))
  23662. + return -1;
  23663. +
  23664. + if (m&0x00000001UL)
  23665. + return 32;
  23666. + masklen = 0;
  23667. + if (m&(0x0000ffffUL<<1)) { /* <<1 for 1-origin numbering */
  23668. + masklen |= 0x10;
  23669. + m <<= 16;
  23670. + }
  23671. + if (m&(0x00ff0000UL<<1)) {
  23672. + masklen |= 0x08;
  23673. + m <<= 8;
  23674. + }
  23675. + if (m&(0x0f000000UL<<1)) {
  23676. + masklen |= 0x04;
  23677. + m <<= 4;
  23678. + }
  23679. + if (m&(0x30000000UL<<1)) {
  23680. + masklen |= 0x02;
  23681. + m <<= 2;
  23682. + }
  23683. + if (m&(0x40000000UL<<1))
  23684. + masklen |= 0x01;
  23685. +
  23686. + return masklen;
  23687. +}
  23688. +
  23689. +/*
  23690. + - bitstomask - return a mask with this many high bits on
  23691. + */
  23692. +struct in_addr
  23693. +bitstomask(n)
  23694. +int n;
  23695. +{
  23696. + struct in_addr result;
  23697. +
  23698. + if (n > 0 && n <= ABITS)
  23699. + result.s_addr = htonl(~((1UL << (ABITS - n)) - 1));
  23700. + else if (n == 0)
  23701. + result.s_addr = 0;
  23702. + else
  23703. + result.s_addr = 0; /* best error report we can do */
  23704. + return result;
  23705. +}
  23706. --- /dev/null Tue Mar 11 13:02:56 2003
  23707. +++ linux/net/ipsec/infblock.c Mon Feb 9 13:51:03 2004
  23708. @@ -0,0 +1,403 @@
  23709. +/* infblock.c -- interpret and process block types to last block
  23710. + * Copyright (C) 1995-2002 Mark Adler
  23711. + * For conditions of distribution and use, see copyright notice in zlib.h
  23712. + */
  23713. +
  23714. +#include <zlib/zutil.h>
  23715. +#include "infblock.h"
  23716. +#include "inftrees.h"
  23717. +#include "infcodes.h"
  23718. +#include "infutil.h"
  23719. +
  23720. +struct inflate_codes_state {int dummy;}; /* for buggy compilers */
  23721. +
  23722. +/* simplify the use of the inflate_huft type with some defines */
  23723. +#define exop word.what.Exop
  23724. +#define bits word.what.Bits
  23725. +
  23726. +/* Table for deflate from PKZIP's appnote.txt. */
  23727. +local const uInt border[] = { /* Order of the bit length code lengths */
  23728. + 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  23729. +
  23730. +/*
  23731. + Notes beyond the 1.93a appnote.txt:
  23732. +
  23733. + 1. Distance pointers never point before the beginning of the output
  23734. + stream.
  23735. + 2. Distance pointers can point back across blocks, up to 32k away.
  23736. + 3. There is an implied maximum of 7 bits for the bit length table and
  23737. + 15 bits for the actual data.
  23738. + 4. If only one code exists, then it is encoded using one bit. (Zero
  23739. + would be more efficient, but perhaps a little confusing.) If two
  23740. + codes exist, they are coded using one bit each (0 and 1).
  23741. + 5. There is no way of sending zero distance codes--a dummy must be
  23742. + sent if there are none. (History: a pre 2.0 version of PKZIP would
  23743. + store blocks with no distance codes, but this was discovered to be
  23744. + too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
  23745. + zero distance codes, which is sent as one code of zero bits in
  23746. + length.
  23747. + 6. There are up to 286 literal/length codes. Code 256 represents the
  23748. + end-of-block. Note however that the static length tree defines
  23749. + 288 codes just to fill out the Huffman codes. Codes 286 and 287
  23750. + cannot be used though, since there is no length base or extra bits
  23751. + defined for them. Similarily, there are up to 30 distance codes.
  23752. + However, static trees define 32 codes (all 5 bits) to fill out the
  23753. + Huffman codes, but the last two had better not show up in the data.
  23754. + 7. Unzip can check dynamic Huffman blocks for complete code sets.
  23755. + The exception is that a single code would not be complete (see #4).
  23756. + 8. The five bits following the block type is really the number of
  23757. + literal codes sent minus 257.
  23758. + 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
  23759. + (1+6+6). Therefore, to output three times the length, you output
  23760. + three codes (1+1+1), whereas to output four times the same length,
  23761. + you only need two codes (1+3). Hmm.
  23762. + 10. In the tree reconstruction algorithm, Code = Code + Increment
  23763. + only if BitLength(i) is not zero. (Pretty obvious.)
  23764. + 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
  23765. + 12. Note: length code 284 can represent 227-258, but length code 285
  23766. + really is 258. The last length deserves its own, short code
  23767. + since it gets used a lot in very redundant files. The length
  23768. + 258 is special since 258 - 3 (the min match length) is 255.
  23769. + 13. The literal/length and distance code bit lengths are read as a
  23770. + single stream of lengths. It is possible (and advantageous) for
  23771. + a repeat code (16, 17, or 18) to go across the boundary between
  23772. + the two sets of lengths.
  23773. + */
  23774. +
  23775. +
  23776. +void inflate_blocks_reset(s, z, c)
  23777. +inflate_blocks_statef *s;
  23778. +z_streamp z;
  23779. +uLongf *c;
  23780. +{
  23781. + if (c != Z_NULL)
  23782. + *c = s->check;
  23783. + if (s->mode == BTREE || s->mode == DTREE)
  23784. + ZFREE(z, s->sub.trees.blens);
  23785. + if (s->mode == CODES)
  23786. + inflate_codes_free(s->sub.decode.codes, z);
  23787. + s->mode = TYPE;
  23788. + s->bitk = 0;
  23789. + s->bitb = 0;
  23790. + s->read = s->write = s->window;
  23791. + if (s->checkfn != Z_NULL)
  23792. + z->adler = s->check = (*s->checkfn)(0L, (const Bytef *)Z_NULL, 0);
  23793. + Tracev((stderr, "inflate: blocks reset\n"));
  23794. +}
  23795. +
  23796. +
  23797. +inflate_blocks_statef *inflate_blocks_new(z, c, w)
  23798. +z_streamp z;
  23799. +check_func c;
  23800. +uInt w;
  23801. +{
  23802. + inflate_blocks_statef *s;
  23803. +
  23804. + if ((s = (inflate_blocks_statef *)ZALLOC
  23805. + (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
  23806. + return s;
  23807. + if ((s->hufts =
  23808. + (inflate_huft *)ZALLOC(z, sizeof(inflate_huft), MANY)) == Z_NULL)
  23809. + {
  23810. + ZFREE(z, s);
  23811. + return Z_NULL;
  23812. + }
  23813. + if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
  23814. + {
  23815. + ZFREE(z, s->hufts);
  23816. + ZFREE(z, s);
  23817. + return Z_NULL;
  23818. + }
  23819. + s->end = s->window + w;
  23820. + s->checkfn = c;
  23821. + s->mode = TYPE;
  23822. + Tracev((stderr, "inflate: blocks allocated\n"));
  23823. + inflate_blocks_reset(s, z, Z_NULL);
  23824. + return s;
  23825. +}
  23826. +
  23827. +
  23828. +int inflate_blocks(s, z, r)
  23829. +inflate_blocks_statef *s;
  23830. +z_streamp z;
  23831. +int r;
  23832. +{
  23833. + uInt t; /* temporary storage */
  23834. + uLong b; /* bit buffer */
  23835. + uInt k; /* bits in bit buffer */
  23836. + Bytef *p; /* input data pointer */
  23837. + uInt n; /* bytes available there */
  23838. + Bytef *q; /* output window write pointer */
  23839. + uInt m; /* bytes to end of window or read pointer */
  23840. +
  23841. + /* copy input/output information to locals (UPDATE macro restores) */
  23842. + LOAD
  23843. +
  23844. + /* process input based on current state */
  23845. + while (1) switch (s->mode)
  23846. + {
  23847. + case TYPE:
  23848. + NEEDBITS(3)
  23849. + t = (uInt)b & 7;
  23850. + s->last = t & 1;
  23851. + switch (t >> 1)
  23852. + {
  23853. + case 0: /* stored */
  23854. + Tracev((stderr, "inflate: stored block%s\n",
  23855. + s->last ? " (last)" : ""));
  23856. + DUMPBITS(3)
  23857. + t = k & 7; /* go to byte boundary */
  23858. + DUMPBITS(t)
  23859. + s->mode = LENS; /* get length of stored block */
  23860. + break;
  23861. + case 1: /* fixed */
  23862. + Tracev((stderr, "inflate: fixed codes block%s\n",
  23863. + s->last ? " (last)" : ""));
  23864. + {
  23865. + uInt bl, bd;
  23866. + inflate_huft *tl, *td;
  23867. +
  23868. + inflate_trees_fixed(&bl, &bd, &tl, &td, z);
  23869. + s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
  23870. + if (s->sub.decode.codes == Z_NULL)
  23871. + {
  23872. + r = Z_MEM_ERROR;
  23873. + LEAVE
  23874. + }
  23875. + }
  23876. + DUMPBITS(3)
  23877. + s->mode = CODES;
  23878. + break;
  23879. + case 2: /* dynamic */
  23880. + Tracev((stderr, "inflate: dynamic codes block%s\n",
  23881. + s->last ? " (last)" : ""));
  23882. + DUMPBITS(3)
  23883. + s->mode = TABLE;
  23884. + break;
  23885. + case 3: /* illegal */
  23886. + DUMPBITS(3)
  23887. + s->mode = BAD;
  23888. + z->msg = (char*)"invalid block type";
  23889. + r = Z_DATA_ERROR;
  23890. + LEAVE
  23891. + }
  23892. + break;
  23893. + case LENS:
  23894. + NEEDBITS(32)
  23895. + if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
  23896. + {
  23897. + s->mode = BAD;
  23898. + z->msg = (char*)"invalid stored block lengths";
  23899. + r = Z_DATA_ERROR;
  23900. + LEAVE
  23901. + }
  23902. + s->sub.left = (uInt)b & 0xffff;
  23903. + b = k = 0; /* dump bits */
  23904. + Tracev((stderr, "inflate: stored length %u\n", s->sub.left));
  23905. + s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
  23906. + break;
  23907. + case STORED:
  23908. + if (n == 0)
  23909. + LEAVE
  23910. + NEEDOUT
  23911. + t = s->sub.left;
  23912. + if (t > n) t = n;
  23913. + if (t > m) t = m;
  23914. + zmemcpy(q, p, t);
  23915. + p += t; n -= t;
  23916. + q += t; m -= t;
  23917. + if ((s->sub.left -= t) != 0)
  23918. + break;
  23919. + Tracev((stderr, "inflate: stored end, %lu total out\n",
  23920. + z->total_out + (q >= s->read ? q - s->read :
  23921. + (s->end - s->read) + (q - s->window))));
  23922. + s->mode = s->last ? DRY : TYPE;
  23923. + break;
  23924. + case TABLE:
  23925. + NEEDBITS(14)
  23926. + s->sub.trees.table = t = (uInt)b & 0x3fff;
  23927. +#ifndef PKZIP_BUG_WORKAROUND
  23928. + if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
  23929. + {
  23930. + s->mode = BAD;
  23931. + z->msg = (char*)"too many length or distance symbols";
  23932. + r = Z_DATA_ERROR;
  23933. + LEAVE
  23934. + }
  23935. +#endif
  23936. + t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
  23937. + if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
  23938. + {
  23939. + r = Z_MEM_ERROR;
  23940. + LEAVE
  23941. + }
  23942. + DUMPBITS(14)
  23943. + s->sub.trees.index = 0;
  23944. + Tracev((stderr, "inflate: table sizes ok\n"));
  23945. + s->mode = BTREE;
  23946. + case BTREE:
  23947. + while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
  23948. + {
  23949. + NEEDBITS(3)
  23950. + s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
  23951. + DUMPBITS(3)
  23952. + }
  23953. + while (s->sub.trees.index < 19)
  23954. + s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
  23955. + s->sub.trees.bb = 7;
  23956. + t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
  23957. + &s->sub.trees.tb, s->hufts, z);
  23958. + if (t != Z_OK)
  23959. + {
  23960. + r = t;
  23961. + if (r == Z_DATA_ERROR)
  23962. + {
  23963. + ZFREE(z, s->sub.trees.blens);
  23964. + s->mode = BAD;
  23965. + }
  23966. + LEAVE
  23967. + }
  23968. + s->sub.trees.index = 0;
  23969. + Tracev((stderr, "inflate: bits tree ok\n"));
  23970. + s->mode = DTREE;
  23971. + case DTREE:
  23972. + while (t = s->sub.trees.table,
  23973. + s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
  23974. + {
  23975. + inflate_huft *h;
  23976. + uInt i, j, c;
  23977. +
  23978. + t = s->sub.trees.bb;
  23979. + NEEDBITS(t)
  23980. + h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
  23981. + t = h->bits;
  23982. + c = h->base;
  23983. + if (c < 16)
  23984. + {
  23985. + DUMPBITS(t)
  23986. + s->sub.trees.blens[s->sub.trees.index++] = c;
  23987. + }
  23988. + else /* c == 16..18 */
  23989. + {
  23990. + i = c == 18 ? 7 : c - 14;
  23991. + j = c == 18 ? 11 : 3;
  23992. + NEEDBITS(t + i)
  23993. + DUMPBITS(t)
  23994. + j += (uInt)b & inflate_mask[i];
  23995. + DUMPBITS(i)
  23996. + i = s->sub.trees.index;
  23997. + t = s->sub.trees.table;
  23998. + if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
  23999. + (c == 16 && i < 1))
  24000. + {
  24001. + ZFREE(z, s->sub.trees.blens);
  24002. + s->mode = BAD;
  24003. + z->msg = (char*)"invalid bit length repeat";
  24004. + r = Z_DATA_ERROR;
  24005. + LEAVE
  24006. + }
  24007. + c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
  24008. + do {
  24009. + s->sub.trees.blens[i++] = c;
  24010. + } while (--j);
  24011. + s->sub.trees.index = i;
  24012. + }
  24013. + }
  24014. + s->sub.trees.tb = Z_NULL;
  24015. + {
  24016. + uInt bl, bd;
  24017. + inflate_huft *tl, *td;
  24018. + inflate_codes_statef *c;
  24019. +
  24020. + bl = 9; /* must be <= 9 for lookahead assumptions */
  24021. + bd = 6; /* must be <= 9 for lookahead assumptions */
  24022. + t = s->sub.trees.table;
  24023. + t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
  24024. + s->sub.trees.blens, &bl, &bd, &tl, &td,
  24025. + s->hufts, z);
  24026. + if (t != Z_OK)
  24027. + {
  24028. + if (t == (uInt)Z_DATA_ERROR)
  24029. + {
  24030. + ZFREE(z, s->sub.trees.blens);
  24031. + s->mode = BAD;
  24032. + }
  24033. + r = t;
  24034. + LEAVE
  24035. + }
  24036. + Tracev((stderr, "inflate: trees ok\n"));
  24037. + if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
  24038. + {
  24039. + r = Z_MEM_ERROR;
  24040. + LEAVE
  24041. + }
  24042. + s->sub.decode.codes = c;
  24043. + }
  24044. + ZFREE(z, s->sub.trees.blens);
  24045. + s->mode = CODES;
  24046. + case CODES:
  24047. + UPDATE
  24048. + if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
  24049. + return inflate_flush(s, z, r);
  24050. + r = Z_OK;
  24051. + inflate_codes_free(s->sub.decode.codes, z);
  24052. + LOAD
  24053. + Tracev((stderr, "inflate: codes end, %lu total out\n",
  24054. + z->total_out + (q >= s->read ? q - s->read :
  24055. + (s->end - s->read) + (q - s->window))));
  24056. + if (!s->last)
  24057. + {
  24058. + s->mode = TYPE;
  24059. + break;
  24060. + }
  24061. + s->mode = DRY;
  24062. + case DRY:
  24063. + FLUSH
  24064. + if (s->read != s->write)
  24065. + LEAVE
  24066. + s->mode = DONE;
  24067. + case DONE:
  24068. + r = Z_STREAM_END;
  24069. + LEAVE
  24070. + case BAD:
  24071. + r = Z_DATA_ERROR;
  24072. + LEAVE
  24073. + default:
  24074. + r = Z_STREAM_ERROR;
  24075. + LEAVE
  24076. + }
  24077. +}
  24078. +
  24079. +
  24080. +int inflate_blocks_free(s, z)
  24081. +inflate_blocks_statef *s;
  24082. +z_streamp z;
  24083. +{
  24084. + inflate_blocks_reset(s, z, Z_NULL);
  24085. + ZFREE(z, s->window);
  24086. + ZFREE(z, s->hufts);
  24087. + ZFREE(z, s);
  24088. + Tracev((stderr, "inflate: blocks freed\n"));
  24089. + return Z_OK;
  24090. +}
  24091. +
  24092. +
  24093. +void inflate_set_dictionary(s, d, n)
  24094. +inflate_blocks_statef *s;
  24095. +const Bytef *d;
  24096. +uInt n;
  24097. +{
  24098. + zmemcpy(s->window, d, n);
  24099. + s->read = s->write = s->window + n;
  24100. +}
  24101. +
  24102. +
  24103. +/* Returns true if inflate is currently at the end of a block generated
  24104. + * by Z_SYNC_FLUSH or Z_FULL_FLUSH.
  24105. + * IN assertion: s != Z_NULL
  24106. + */
  24107. +int inflate_blocks_sync_point(s)
  24108. +inflate_blocks_statef *s;
  24109. +{
  24110. + return s->mode == LENS;
  24111. +}
  24112. --- /dev/null Tue Mar 11 13:02:56 2003
  24113. +++ linux/net/ipsec/infblock.h Mon Feb 9 13:51:03 2004
  24114. @@ -0,0 +1,39 @@
  24115. +/* infblock.h -- header to use infblock.c
  24116. + * Copyright (C) 1995-2002 Mark Adler
  24117. + * For conditions of distribution and use, see copyright notice in zlib.h
  24118. + */
  24119. +
  24120. +/* WARNING: this file should *not* be used by applications. It is
  24121. + part of the implementation of the compression library and is
  24122. + subject to change. Applications should only use zlib.h.
  24123. + */
  24124. +
  24125. +struct inflate_blocks_state;
  24126. +typedef struct inflate_blocks_state FAR inflate_blocks_statef;
  24127. +
  24128. +extern inflate_blocks_statef * inflate_blocks_new OF((
  24129. + z_streamp z,
  24130. + check_func c, /* check function */
  24131. + uInt w)); /* window size */
  24132. +
  24133. +extern int inflate_blocks OF((
  24134. + inflate_blocks_statef *,
  24135. + z_streamp ,
  24136. + int)); /* initial return code */
  24137. +
  24138. +extern void inflate_blocks_reset OF((
  24139. + inflate_blocks_statef *,
  24140. + z_streamp ,
  24141. + uLongf *)); /* check value on output */
  24142. +
  24143. +extern int inflate_blocks_free OF((
  24144. + inflate_blocks_statef *,
  24145. + z_streamp));
  24146. +
  24147. +extern void inflate_set_dictionary OF((
  24148. + inflate_blocks_statef *s,
  24149. + const Bytef *d, /* dictionary */
  24150. + uInt n)); /* dictionary length */
  24151. +
  24152. +extern int inflate_blocks_sync_point OF((
  24153. + inflate_blocks_statef *s));
  24154. --- /dev/null Tue Mar 11 13:02:56 2003
  24155. +++ linux/net/ipsec/infcodes.c Mon Feb 9 13:51:03 2004
  24156. @@ -0,0 +1,251 @@
  24157. +/* infcodes.c -- process literals and length/distance pairs
  24158. + * Copyright (C) 1995-2002 Mark Adler
  24159. + * For conditions of distribution and use, see copyright notice in zlib.h
  24160. + */
  24161. +
  24162. +#include <zlib/zutil.h>
  24163. +#include "inftrees.h"
  24164. +#include "infblock.h"
  24165. +#include "infcodes.h"
  24166. +#include "infutil.h"
  24167. +#include "inffast.h"
  24168. +
  24169. +/* simplify the use of the inflate_huft type with some defines */
  24170. +#define exop word.what.Exop
  24171. +#define bits word.what.Bits
  24172. +
  24173. +typedef enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
  24174. + START, /* x: set up for LEN */
  24175. + LEN, /* i: get length/literal/eob next */
  24176. + LENEXT, /* i: getting length extra (have base) */
  24177. + DIST, /* i: get distance next */
  24178. + DISTEXT, /* i: getting distance extra */
  24179. + COPY, /* o: copying bytes in window, waiting for space */
  24180. + LIT, /* o: got literal, waiting for output space */
  24181. + WASH, /* o: got eob, possibly still output waiting */
  24182. + END, /* x: got eob and all data flushed */
  24183. + BADCODE} /* x: got error */
  24184. +inflate_codes_mode;
  24185. +
  24186. +/* inflate codes private state */
  24187. +struct inflate_codes_state {
  24188. +
  24189. + /* mode */
  24190. + inflate_codes_mode mode; /* current inflate_codes mode */
  24191. +
  24192. + /* mode dependent information */
  24193. + uInt len;
  24194. + union {
  24195. + struct {
  24196. + inflate_huft *tree; /* pointer into tree */
  24197. + uInt need; /* bits needed */
  24198. + } code; /* if LEN or DIST, where in tree */
  24199. + uInt lit; /* if LIT, literal */
  24200. + struct {
  24201. + uInt get; /* bits to get for extra */
  24202. + uInt dist; /* distance back to copy from */
  24203. + } copy; /* if EXT or COPY, where and how much */
  24204. + } sub; /* submode */
  24205. +
  24206. + /* mode independent information */
  24207. + Byte lbits; /* ltree bits decoded per branch */
  24208. + Byte dbits; /* dtree bits decoder per branch */
  24209. + inflate_huft *ltree; /* literal/length/eob tree */
  24210. + inflate_huft *dtree; /* distance tree */
  24211. +
  24212. +};
  24213. +
  24214. +
  24215. +inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
  24216. +uInt bl, bd;
  24217. +inflate_huft *tl;
  24218. +inflate_huft *td; /* need separate declaration for Borland C++ */
  24219. +z_streamp z;
  24220. +{
  24221. + inflate_codes_statef *c;
  24222. +
  24223. + if ((c = (inflate_codes_statef *)
  24224. + ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
  24225. + {
  24226. + c->mode = START;
  24227. + c->lbits = (Byte)bl;
  24228. + c->dbits = (Byte)bd;
  24229. + c->ltree = tl;
  24230. + c->dtree = td;
  24231. + Tracev((stderr, "inflate: codes new\n"));
  24232. + }
  24233. + return c;
  24234. +}
  24235. +
  24236. +
  24237. +int inflate_codes(s, z, r)
  24238. +inflate_blocks_statef *s;
  24239. +z_streamp z;
  24240. +int r;
  24241. +{
  24242. + uInt j; /* temporary storage */
  24243. + inflate_huft *t; /* temporary pointer */
  24244. + uInt e; /* extra bits or operation */
  24245. + uLong b; /* bit buffer */
  24246. + uInt k; /* bits in bit buffer */
  24247. + Bytef *p; /* input data pointer */
  24248. + uInt n; /* bytes available there */
  24249. + Bytef *q; /* output window write pointer */
  24250. + uInt m; /* bytes to end of window or read pointer */
  24251. + Bytef *f; /* pointer to copy strings from */
  24252. + inflate_codes_statef *c = s->sub.decode.codes; /* codes state */
  24253. +
  24254. + /* copy input/output information to locals (UPDATE macro restores) */
  24255. + LOAD
  24256. +
  24257. + /* process input and output based on current state */
  24258. + while (1) switch (c->mode)
  24259. + { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
  24260. + case START: /* x: set up for LEN */
  24261. +#ifndef SLOW
  24262. + if (m >= 258 && n >= 10)
  24263. + {
  24264. + UPDATE
  24265. + r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
  24266. + LOAD
  24267. + if (r != Z_OK)
  24268. + {
  24269. + c->mode = r == Z_STREAM_END ? WASH : BADCODE;
  24270. + break;
  24271. + }
  24272. + }
  24273. +#endif /* !SLOW */
  24274. + c->sub.code.need = c->lbits;
  24275. + c->sub.code.tree = c->ltree;
  24276. + c->mode = LEN;
  24277. + case LEN: /* i: get length/literal/eob next */
  24278. + j = c->sub.code.need;
  24279. + NEEDBITS(j)
  24280. + t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
  24281. + DUMPBITS(t->bits)
  24282. + e = (uInt)(t->exop);
  24283. + if (e == 0) /* literal */
  24284. + {
  24285. + c->sub.lit = t->base;
  24286. + Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
  24287. + "inflate: literal '%c'\n" :
  24288. + "inflate: literal 0x%02x\n", t->base));
  24289. + c->mode = LIT;
  24290. + break;
  24291. + }
  24292. + if (e & 16) /* length */
  24293. + {
  24294. + c->sub.copy.get = e & 15;
  24295. + c->len = t->base;
  24296. + c->mode = LENEXT;
  24297. + break;
  24298. + }
  24299. + if ((e & 64) == 0) /* next table */
  24300. + {
  24301. + c->sub.code.need = e;
  24302. + c->sub.code.tree = t + t->base;
  24303. + break;
  24304. + }
  24305. + if (e & 32) /* end of block */
  24306. + {
  24307. + Tracevv((stderr, "inflate: end of block\n"));
  24308. + c->mode = WASH;
  24309. + break;
  24310. + }
  24311. + c->mode = BADCODE; /* invalid code */
  24312. + z->msg = (char*)"invalid literal/length code";
  24313. + r = Z_DATA_ERROR;
  24314. + LEAVE
  24315. + case LENEXT: /* i: getting length extra (have base) */
  24316. + j = c->sub.copy.get;
  24317. + NEEDBITS(j)
  24318. + c->len += (uInt)b & inflate_mask[j];
  24319. + DUMPBITS(j)
  24320. + c->sub.code.need = c->dbits;
  24321. + c->sub.code.tree = c->dtree;
  24322. + Tracevv((stderr, "inflate: length %u\n", c->len));
  24323. + c->mode = DIST;
  24324. + case DIST: /* i: get distance next */
  24325. + j = c->sub.code.need;
  24326. + NEEDBITS(j)
  24327. + t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
  24328. + DUMPBITS(t->bits)
  24329. + e = (uInt)(t->exop);
  24330. + if (e & 16) /* distance */
  24331. + {
  24332. + c->sub.copy.get = e & 15;
  24333. + c->sub.copy.dist = t->base;
  24334. + c->mode = DISTEXT;
  24335. + break;
  24336. + }
  24337. + if ((e & 64) == 0) /* next table */
  24338. + {
  24339. + c->sub.code.need = e;
  24340. + c->sub.code.tree = t + t->base;
  24341. + break;
  24342. + }
  24343. + c->mode = BADCODE; /* invalid code */
  24344. + z->msg = (char*)"invalid distance code";
  24345. + r = Z_DATA_ERROR;
  24346. + LEAVE
  24347. + case DISTEXT: /* i: getting distance extra */
  24348. + j = c->sub.copy.get;
  24349. + NEEDBITS(j)
  24350. + c->sub.copy.dist += (uInt)b & inflate_mask[j];
  24351. + DUMPBITS(j)
  24352. + Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist));
  24353. + c->mode = COPY;
  24354. + case COPY: /* o: copying bytes in window, waiting for space */
  24355. + f = q - c->sub.copy.dist;
  24356. + while (f < s->window) /* modulo window size-"while" instead */
  24357. + f += s->end - s->window; /* of "if" handles invalid distances */
  24358. + while (c->len)
  24359. + {
  24360. + NEEDOUT
  24361. + OUTBYTE(*f++)
  24362. + if (f == s->end)
  24363. + f = s->window;
  24364. + c->len--;
  24365. + }
  24366. + c->mode = START;
  24367. + break;
  24368. + case LIT: /* o: got literal, waiting for output space */
  24369. + NEEDOUT
  24370. + OUTBYTE(c->sub.lit)
  24371. + c->mode = START;
  24372. + break;
  24373. + case WASH: /* o: got eob, possibly more output */
  24374. + if (k > 7) /* return unused byte, if any */
  24375. + {
  24376. + Assert(k < 16, "inflate_codes grabbed too many bytes")
  24377. + k -= 8;
  24378. + n++;
  24379. + p--; /* can always return one */
  24380. + }
  24381. + FLUSH
  24382. + if (s->read != s->write)
  24383. + LEAVE
  24384. + c->mode = END;
  24385. + case END:
  24386. + r = Z_STREAM_END;
  24387. + LEAVE
  24388. + case BADCODE: /* x: got error */
  24389. + r = Z_DATA_ERROR;
  24390. + LEAVE
  24391. + default:
  24392. + r = Z_STREAM_ERROR;
  24393. + LEAVE
  24394. + }
  24395. +#ifdef NEED_DUMMY_RETURN
  24396. + return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
  24397. +#endif
  24398. +}
  24399. +
  24400. +
  24401. +void inflate_codes_free(c, z)
  24402. +inflate_codes_statef *c;
  24403. +z_streamp z;
  24404. +{
  24405. + ZFREE(z, c);
  24406. + Tracev((stderr, "inflate: codes free\n"));
  24407. +}
  24408. --- /dev/null Tue Mar 11 13:02:56 2003
  24409. +++ linux/net/ipsec/infcodes.h Mon Feb 9 13:51:03 2004
  24410. @@ -0,0 +1,31 @@
  24411. +/* infcodes.h -- header to use infcodes.c
  24412. + * Copyright (C) 1995-2002 Mark Adler
  24413. + * For conditions of distribution and use, see copyright notice in zlib.h
  24414. + */
  24415. +
  24416. +/* WARNING: this file should *not* be used by applications. It is
  24417. + part of the implementation of the compression library and is
  24418. + subject to change. Applications should only use zlib.h.
  24419. + */
  24420. +
  24421. +#ifndef _INFCODES_H
  24422. +#define _INFCODES_H
  24423. +
  24424. +struct inflate_codes_state;
  24425. +typedef struct inflate_codes_state FAR inflate_codes_statef;
  24426. +
  24427. +extern inflate_codes_statef *inflate_codes_new OF((
  24428. + uInt, uInt,
  24429. + inflate_huft *, inflate_huft *,
  24430. + z_streamp ));
  24431. +
  24432. +extern int inflate_codes OF((
  24433. + inflate_blocks_statef *,
  24434. + z_streamp ,
  24435. + int));
  24436. +
  24437. +extern void inflate_codes_free OF((
  24438. + inflate_codes_statef *,
  24439. + z_streamp ));
  24440. +
  24441. +#endif /* _INFCODES_H */
  24442. --- /dev/null Tue Mar 11 13:02:56 2003
  24443. +++ linux/net/ipsec/inffast.c Mon Feb 9 13:51:03 2004
  24444. @@ -0,0 +1,183 @@
  24445. +/* inffast.c -- process literals and length/distance pairs fast
  24446. + * Copyright (C) 1995-2002 Mark Adler
  24447. + * For conditions of distribution and use, see copyright notice in zlib.h
  24448. + */
  24449. +
  24450. +#include <zlib/zutil.h>
  24451. +#include "inftrees.h"
  24452. +#include "infblock.h"
  24453. +#include "infcodes.h"
  24454. +#include "infutil.h"
  24455. +#include "inffast.h"
  24456. +
  24457. +struct inflate_codes_state {int dummy;}; /* for buggy compilers */
  24458. +
  24459. +/* simplify the use of the inflate_huft type with some defines */
  24460. +#define exop word.what.Exop
  24461. +#define bits word.what.Bits
  24462. +
  24463. +/* macros for bit input with no checking and for returning unused bytes */
  24464. +#define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
  24465. +#define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;}
  24466. +
  24467. +/* Called with number of bytes left to write in window at least 258
  24468. + (the maximum string length) and number of input bytes available
  24469. + at least ten. The ten bytes are six bytes for the longest length/
  24470. + distance pair plus four bytes for overloading the bit buffer. */
  24471. +
  24472. +int inflate_fast(bl, bd, tl, td, s, z)
  24473. +uInt bl, bd;
  24474. +inflate_huft *tl;
  24475. +inflate_huft *td; /* need separate declaration for Borland C++ */
  24476. +inflate_blocks_statef *s;
  24477. +z_streamp z;
  24478. +{
  24479. + inflate_huft *t; /* temporary pointer */
  24480. + uInt e; /* extra bits or operation */
  24481. + uLong b; /* bit buffer */
  24482. + uInt k; /* bits in bit buffer */
  24483. + Bytef *p; /* input data pointer */
  24484. + uInt n; /* bytes available there */
  24485. + Bytef *q; /* output window write pointer */
  24486. + uInt m; /* bytes to end of window or read pointer */
  24487. + uInt ml; /* mask for literal/length tree */
  24488. + uInt md; /* mask for distance tree */
  24489. + uInt c; /* bytes to copy */
  24490. + uInt d; /* distance back to copy from */
  24491. + Bytef *r; /* copy source pointer */
  24492. +
  24493. + /* load input, output, bit values */
  24494. + LOAD
  24495. +
  24496. + /* initialize masks */
  24497. + ml = inflate_mask[bl];
  24498. + md = inflate_mask[bd];
  24499. +
  24500. + /* do until not enough input or output space for fast loop */
  24501. + do { /* assume called with m >= 258 && n >= 10 */
  24502. + /* get literal/length code */
  24503. + GRABBITS(20) /* max bits for literal/length code */
  24504. + if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
  24505. + {
  24506. + DUMPBITS(t->bits)
  24507. + Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
  24508. + "inflate: * literal '%c'\n" :
  24509. + "inflate: * literal 0x%02x\n", t->base));
  24510. + *q++ = (Byte)t->base;
  24511. + m--;
  24512. + continue;
  24513. + }
  24514. + do {
  24515. + DUMPBITS(t->bits)
  24516. + if (e & 16)
  24517. + {
  24518. + /* get extra bits for length */
  24519. + e &= 15;
  24520. + c = t->base + ((uInt)b & inflate_mask[e]);
  24521. + DUMPBITS(e)
  24522. + Tracevv((stderr, "inflate: * length %u\n", c));
  24523. +
  24524. + /* decode distance base of block to copy */
  24525. + GRABBITS(15); /* max bits for distance code */
  24526. + e = (t = td + ((uInt)b & md))->exop;
  24527. + do {
  24528. + DUMPBITS(t->bits)
  24529. + if (e & 16)
  24530. + {
  24531. + /* get extra bits to add to distance base */
  24532. + e &= 15;
  24533. + GRABBITS(e) /* get extra bits (up to 13) */
  24534. + d = t->base + ((uInt)b & inflate_mask[e]);
  24535. + DUMPBITS(e)
  24536. + Tracevv((stderr, "inflate: * distance %u\n", d));
  24537. +
  24538. + /* do the copy */
  24539. + m -= c;
  24540. + r = q - d;
  24541. + if (r < s->window) /* wrap if needed */
  24542. + {
  24543. + do {
  24544. + r += s->end - s->window; /* force pointer in window */
  24545. + } while (r < s->window); /* covers invalid distances */
  24546. + e = s->end - r;
  24547. + if (c > e)
  24548. + {
  24549. + c -= e; /* wrapped copy */
  24550. + do {
  24551. + *q++ = *r++;
  24552. + } while (--e);
  24553. + r = s->window;
  24554. + do {
  24555. + *q++ = *r++;
  24556. + } while (--c);
  24557. + }
  24558. + else /* normal copy */
  24559. + {
  24560. + *q++ = *r++; c--;
  24561. + *q++ = *r++; c--;
  24562. + do {
  24563. + *q++ = *r++;
  24564. + } while (--c);
  24565. + }
  24566. + }
  24567. + else /* normal copy */
  24568. + {
  24569. + *q++ = *r++; c--;
  24570. + *q++ = *r++; c--;
  24571. + do {
  24572. + *q++ = *r++;
  24573. + } while (--c);
  24574. + }
  24575. + break;
  24576. + }
  24577. + else if ((e & 64) == 0)
  24578. + {
  24579. + t += t->base;
  24580. + e = (t += ((uInt)b & inflate_mask[e]))->exop;
  24581. + }
  24582. + else
  24583. + {
  24584. + z->msg = (char*)"invalid distance code";
  24585. + UNGRAB
  24586. + UPDATE
  24587. + return Z_DATA_ERROR;
  24588. + }
  24589. + } while (1);
  24590. + break;
  24591. + }
  24592. + if ((e & 64) == 0)
  24593. + {
  24594. + t += t->base;
  24595. + if ((e = (t += ((uInt)b & inflate_mask[e]))->exop) == 0)
  24596. + {
  24597. + DUMPBITS(t->bits)
  24598. + Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
  24599. + "inflate: * literal '%c'\n" :
  24600. + "inflate: * literal 0x%02x\n", t->base));
  24601. + *q++ = (Byte)t->base;
  24602. + m--;
  24603. + break;
  24604. + }
  24605. + }
  24606. + else if (e & 32)
  24607. + {
  24608. + Tracevv((stderr, "inflate: * end of block\n"));
  24609. + UNGRAB
  24610. + UPDATE
  24611. + return Z_STREAM_END;
  24612. + }
  24613. + else
  24614. + {
  24615. + z->msg = (char*)"invalid literal/length code";
  24616. + UNGRAB
  24617. + UPDATE
  24618. + return Z_DATA_ERROR;
  24619. + }
  24620. + } while (1);
  24621. + } while (m >= 258 && n >= 10);
  24622. +
  24623. + /* not enough input or output--restore pointers and return */
  24624. + UNGRAB
  24625. + UPDATE
  24626. + return Z_OK;
  24627. +}
  24628. --- /dev/null Tue Mar 11 13:02:56 2003
  24629. +++ linux/net/ipsec/inffast.h Mon Feb 9 13:51:03 2004
  24630. @@ -0,0 +1,22 @@
  24631. +/* inffast.h -- header to use inffast.c
  24632. + * Copyright (C) 1995-2002 Mark Adler
  24633. + * For conditions of distribution and use, see copyright notice in zlib.h
  24634. + */
  24635. +
  24636. +/* WARNING: this file should *not* be used by applications. It is
  24637. + part of the implementation of the compression library and is
  24638. + subject to change. Applications should only use zlib.h.
  24639. + */
  24640. +
  24641. +#ifndef _INFFAST_H
  24642. +#define _INFFAST_H
  24643. +
  24644. +extern int inflate_fast OF((
  24645. + uInt,
  24646. + uInt,
  24647. + inflate_huft *,
  24648. + inflate_huft *,
  24649. + inflate_blocks_statef *,
  24650. + z_streamp ));
  24651. +
  24652. +#endif /* _INFFAST_H */
  24653. --- /dev/null Tue Mar 11 13:02:56 2003
  24654. +++ linux/net/ipsec/inffixed.h Mon Feb 9 13:51:03 2004
  24655. @@ -0,0 +1,151 @@
  24656. +/* inffixed.h -- table for decoding fixed codes
  24657. + * Generated automatically by the maketree.c program
  24658. + */
  24659. +
  24660. +/* WARNING: this file should *not* be used by applications. It is
  24661. + part of the implementation of the compression library and is
  24662. + subject to change. Applications should only use zlib.h.
  24663. + */
  24664. +
  24665. +local uInt fixed_bl = 9;
  24666. +local uInt fixed_bd = 5;
  24667. +local inflate_huft fixed_tl[] = {
  24668. + {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
  24669. + {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},192},
  24670. + {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},160},
  24671. + {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},224},
  24672. + {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},144},
  24673. + {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},208},
  24674. + {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},176},
  24675. + {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},240},
  24676. + {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
  24677. + {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},200},
  24678. + {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},168},
  24679. + {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},232},
  24680. + {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},152},
  24681. + {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},216},
  24682. + {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},184},
  24683. + {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},248},
  24684. + {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
  24685. + {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},196},
  24686. + {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},164},
  24687. + {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},228},
  24688. + {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},148},
  24689. + {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},212},
  24690. + {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},180},
  24691. + {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},244},
  24692. + {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
  24693. + {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},204},
  24694. + {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},172},
  24695. + {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},236},
  24696. + {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},156},
  24697. + {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},220},
  24698. + {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},188},
  24699. + {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},252},
  24700. + {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
  24701. + {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},194},
  24702. + {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},162},
  24703. + {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},226},
  24704. + {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},146},
  24705. + {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},210},
  24706. + {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},178},
  24707. + {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},242},
  24708. + {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
  24709. + {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},202},
  24710. + {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},170},
  24711. + {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},234},
  24712. + {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},154},
  24713. + {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},218},
  24714. + {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},186},
  24715. + {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},250},
  24716. + {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
  24717. + {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},198},
  24718. + {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},166},
  24719. + {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},230},
  24720. + {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},150},
  24721. + {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},214},
  24722. + {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},182},
  24723. + {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},246},
  24724. + {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
  24725. + {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},206},
  24726. + {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},174},
  24727. + {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},238},
  24728. + {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},158},
  24729. + {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},222},
  24730. + {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},190},
  24731. + {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},254},
  24732. + {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
  24733. + {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},193},
  24734. + {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},161},
  24735. + {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},225},
  24736. + {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},145},
  24737. + {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},209},
  24738. + {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},177},
  24739. + {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},241},
  24740. + {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
  24741. + {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},201},
  24742. + {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},169},
  24743. + {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},233},
  24744. + {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},153},
  24745. + {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},217},
  24746. + {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},185},
  24747. + {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},249},
  24748. + {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
  24749. + {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},197},
  24750. + {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},165},
  24751. + {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},229},
  24752. + {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},149},
  24753. + {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},213},
  24754. + {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},181},
  24755. + {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},245},
  24756. + {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
  24757. + {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},205},
  24758. + {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},173},
  24759. + {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},237},
  24760. + {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},157},
  24761. + {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},221},
  24762. + {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},189},
  24763. + {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},253},
  24764. + {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
  24765. + {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},195},
  24766. + {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},163},
  24767. + {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},227},
  24768. + {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},147},
  24769. + {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},211},
  24770. + {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},179},
  24771. + {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},243},
  24772. + {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
  24773. + {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},203},
  24774. + {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},171},
  24775. + {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},235},
  24776. + {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},155},
  24777. + {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},219},
  24778. + {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},187},
  24779. + {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},251},
  24780. + {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
  24781. + {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},199},
  24782. + {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},167},
  24783. + {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},231},
  24784. + {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},151},
  24785. + {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},215},
  24786. + {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},183},
  24787. + {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},247},
  24788. + {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
  24789. + {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},207},
  24790. + {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},175},
  24791. + {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},239},
  24792. + {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},159},
  24793. + {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},223},
  24794. + {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},191},
  24795. + {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},255}
  24796. + };
  24797. +local inflate_huft fixed_td[] = {
  24798. + {{{80,5}},1}, {{{87,5}},257}, {{{83,5}},17}, {{{91,5}},4097},
  24799. + {{{81,5}},5}, {{{89,5}},1025}, {{{85,5}},65}, {{{93,5}},16385},
  24800. + {{{80,5}},3}, {{{88,5}},513}, {{{84,5}},33}, {{{92,5}},8193},
  24801. + {{{82,5}},9}, {{{90,5}},2049}, {{{86,5}},129}, {{{192,5}},24577},
  24802. + {{{80,5}},2}, {{{87,5}},385}, {{{83,5}},25}, {{{91,5}},6145},
  24803. + {{{81,5}},7}, {{{89,5}},1537}, {{{85,5}},97}, {{{93,5}},24577},
  24804. + {{{80,5}},4}, {{{88,5}},769}, {{{84,5}},49}, {{{92,5}},12289},
  24805. + {{{82,5}},13}, {{{90,5}},3073}, {{{86,5}},193}, {{{192,5}},24577}
  24806. + };
  24807. --- /dev/null Tue Mar 11 13:02:56 2003
  24808. +++ linux/net/ipsec/inflate.c Mon Feb 9 13:51:03 2004
  24809. @@ -0,0 +1,368 @@
  24810. +/* inflate.c -- zlib interface to inflate modules
  24811. + * Copyright (C) 1995-2002 Mark Adler
  24812. + * For conditions of distribution and use, see copyright notice in zlib.h
  24813. + */
  24814. +
  24815. +#include <zlib/zutil.h>
  24816. +#include "infblock.h"
  24817. +
  24818. +struct inflate_blocks_state {int dummy;}; /* for buggy compilers */
  24819. +
  24820. +typedef enum {
  24821. + METHOD, /* waiting for method byte */
  24822. + FLAG, /* waiting for flag byte */
  24823. + DICT4, /* four dictionary check bytes to go */
  24824. + DICT3, /* three dictionary check bytes to go */
  24825. + DICT2, /* two dictionary check bytes to go */
  24826. + DICT1, /* one dictionary check byte to go */
  24827. + DICT0, /* waiting for inflateSetDictionary */
  24828. + BLOCKS, /* decompressing blocks */
  24829. + CHECK4, /* four check bytes to go */
  24830. + CHECK3, /* three check bytes to go */
  24831. + CHECK2, /* two check bytes to go */
  24832. + CHECK1, /* one check byte to go */
  24833. + DONE, /* finished check, done */
  24834. + BAD} /* got an error--stay here */
  24835. +inflate_mode;
  24836. +
  24837. +/* inflate private state */
  24838. +struct internal_state {
  24839. +
  24840. + /* mode */
  24841. + inflate_mode mode; /* current inflate mode */
  24842. +
  24843. + /* mode dependent information */
  24844. + union {
  24845. + uInt method; /* if FLAGS, method byte */
  24846. + struct {
  24847. + uLong was; /* computed check value */
  24848. + uLong need; /* stream check value */
  24849. + } check; /* if CHECK, check values to compare */
  24850. + uInt marker; /* if BAD, inflateSync's marker bytes count */
  24851. + } sub; /* submode */
  24852. +
  24853. + /* mode independent information */
  24854. + int nowrap; /* flag for no wrapper */
  24855. + uInt wbits; /* log2(window size) (8..15, defaults to 15) */
  24856. + inflate_blocks_statef
  24857. + *blocks; /* current inflate_blocks state */
  24858. +
  24859. +};
  24860. +
  24861. +
  24862. +int ZEXPORT inflateReset(z)
  24863. +z_streamp z;
  24864. +{
  24865. + if (z == Z_NULL || z->state == Z_NULL)
  24866. + return Z_STREAM_ERROR;
  24867. + z->total_in = z->total_out = 0;
  24868. + z->msg = Z_NULL;
  24869. + z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
  24870. + inflate_blocks_reset(z->state->blocks, z, Z_NULL);
  24871. + Tracev((stderr, "inflate: reset\n"));
  24872. + return Z_OK;
  24873. +}
  24874. +
  24875. +
  24876. +int ZEXPORT inflateEnd(z)
  24877. +z_streamp z;
  24878. +{
  24879. + if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
  24880. + return Z_STREAM_ERROR;
  24881. + if (z->state->blocks != Z_NULL)
  24882. + inflate_blocks_free(z->state->blocks, z);
  24883. + ZFREE(z, z->state);
  24884. + z->state = Z_NULL;
  24885. + Tracev((stderr, "inflate: end\n"));
  24886. + return Z_OK;
  24887. +}
  24888. +
  24889. +
  24890. +int ZEXPORT inflateInit2_(z, w, version, stream_size)
  24891. +z_streamp z;
  24892. +int w;
  24893. +const char *version;
  24894. +int stream_size;
  24895. +{
  24896. + if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
  24897. + stream_size != sizeof(z_stream))
  24898. + return Z_VERSION_ERROR;
  24899. +
  24900. + /* initialize state */
  24901. + if (z == Z_NULL)
  24902. + return Z_STREAM_ERROR;
  24903. + z->msg = Z_NULL;
  24904. + if (z->zalloc == Z_NULL)
  24905. + {
  24906. + return Z_STREAM_ERROR;
  24907. +/* z->zalloc = zcalloc;
  24908. + z->opaque = (voidpf)0;
  24909. +*/
  24910. + }
  24911. + if (z->zfree == Z_NULL) return Z_STREAM_ERROR; /* z->zfree = zcfree; */
  24912. + if ((z->state = (struct internal_state FAR *)
  24913. + ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
  24914. + return Z_MEM_ERROR;
  24915. + z->state->blocks = Z_NULL;
  24916. +
  24917. + /* handle undocumented nowrap option (no zlib header or check) */
  24918. + z->state->nowrap = 0;
  24919. + if (w < 0)
  24920. + {
  24921. + w = - w;
  24922. + z->state->nowrap = 1;
  24923. + }
  24924. +
  24925. + /* set window size */
  24926. + if (w < 8 || w > 15)
  24927. + {
  24928. + inflateEnd(z);
  24929. + return Z_STREAM_ERROR;
  24930. + }
  24931. + z->state->wbits = (uInt)w;
  24932. +
  24933. + /* create inflate_blocks state */
  24934. + if ((z->state->blocks =
  24935. + inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
  24936. + == Z_NULL)
  24937. + {
  24938. + inflateEnd(z);
  24939. + return Z_MEM_ERROR;
  24940. + }
  24941. + Tracev((stderr, "inflate: allocated\n"));
  24942. +
  24943. + /* reset state */
  24944. + inflateReset(z);
  24945. + return Z_OK;
  24946. +}
  24947. +
  24948. +
  24949. +int ZEXPORT inflateInit_(z, version, stream_size)
  24950. +z_streamp z;
  24951. +const char *version;
  24952. +int stream_size;
  24953. +{
  24954. + return inflateInit2_(z, DEF_WBITS, version, stream_size);
  24955. +}
  24956. +
  24957. +
  24958. +#define NEEDBYTE {if(z->avail_in==0)return r;r=f;}
  24959. +#define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
  24960. +
  24961. +int ZEXPORT inflate(z, f)
  24962. +z_streamp z;
  24963. +int f;
  24964. +{
  24965. + int r;
  24966. + uInt b;
  24967. +
  24968. + if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
  24969. + return Z_STREAM_ERROR;
  24970. + f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
  24971. + r = Z_BUF_ERROR;
  24972. + while (1) switch (z->state->mode)
  24973. + {
  24974. + case METHOD:
  24975. + NEEDBYTE
  24976. + if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
  24977. + {
  24978. + z->state->mode = BAD;
  24979. + z->msg = (char*)"unknown compression method";
  24980. + z->state->sub.marker = 5; /* can't try inflateSync */
  24981. + break;
  24982. + }
  24983. + if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
  24984. + {
  24985. + z->state->mode = BAD;
  24986. + z->msg = (char*)"invalid window size";
  24987. + z->state->sub.marker = 5; /* can't try inflateSync */
  24988. + break;
  24989. + }
  24990. + z->state->mode = FLAG;
  24991. + case FLAG:
  24992. + NEEDBYTE
  24993. + b = NEXTBYTE;
  24994. + if (((z->state->sub.method << 8) + b) % 31)
  24995. + {
  24996. + z->state->mode = BAD;
  24997. + z->msg = (char*)"incorrect header check";
  24998. + z->state->sub.marker = 5; /* can't try inflateSync */
  24999. + break;
  25000. + }
  25001. + Tracev((stderr, "inflate: zlib header ok\n"));
  25002. + if (!(b & PRESET_DICT))
  25003. + {
  25004. + z->state->mode = BLOCKS;
  25005. + break;
  25006. + }
  25007. + z->state->mode = DICT4;
  25008. + case DICT4:
  25009. + NEEDBYTE
  25010. + z->state->sub.check.need = (uLong)NEXTBYTE << 24;
  25011. + z->state->mode = DICT3;
  25012. + case DICT3:
  25013. + NEEDBYTE
  25014. + z->state->sub.check.need += (uLong)NEXTBYTE << 16;
  25015. + z->state->mode = DICT2;
  25016. + case DICT2:
  25017. + NEEDBYTE
  25018. + z->state->sub.check.need += (uLong)NEXTBYTE << 8;
  25019. + z->state->mode = DICT1;
  25020. + case DICT1:
  25021. + NEEDBYTE
  25022. + z->state->sub.check.need += (uLong)NEXTBYTE;
  25023. + z->adler = z->state->sub.check.need;
  25024. + z->state->mode = DICT0;
  25025. + return Z_NEED_DICT;
  25026. + case DICT0:
  25027. + z->state->mode = BAD;
  25028. + z->msg = (char*)"need dictionary";
  25029. + z->state->sub.marker = 0; /* can try inflateSync */
  25030. + return Z_STREAM_ERROR;
  25031. + case BLOCKS:
  25032. + r = inflate_blocks(z->state->blocks, z, r);
  25033. + if (r == Z_DATA_ERROR)
  25034. + {
  25035. + z->state->mode = BAD;
  25036. + z->state->sub.marker = 0; /* can try inflateSync */
  25037. + break;
  25038. + }
  25039. + if (r == Z_OK)
  25040. + r = f;
  25041. + if (r != Z_STREAM_END)
  25042. + return r;
  25043. + r = f;
  25044. + inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
  25045. + if (z->state->nowrap)
  25046. + {
  25047. + z->state->mode = DONE;
  25048. + break;
  25049. + }
  25050. + z->state->mode = CHECK4;
  25051. + case CHECK4:
  25052. + NEEDBYTE
  25053. + z->state->sub.check.need = (uLong)NEXTBYTE << 24;
  25054. + z->state->mode = CHECK3;
  25055. + case CHECK3:
  25056. + NEEDBYTE
  25057. + z->state->sub.check.need += (uLong)NEXTBYTE << 16;
  25058. + z->state->mode = CHECK2;
  25059. + case CHECK2:
  25060. + NEEDBYTE
  25061. + z->state->sub.check.need += (uLong)NEXTBYTE << 8;
  25062. + z->state->mode = CHECK1;
  25063. + case CHECK1:
  25064. + NEEDBYTE
  25065. + z->state->sub.check.need += (uLong)NEXTBYTE;
  25066. +
  25067. + if (z->state->sub.check.was != z->state->sub.check.need)
  25068. + {
  25069. + z->state->mode = BAD;
  25070. + z->msg = (char*)"incorrect data check";
  25071. + z->state->sub.marker = 5; /* can't try inflateSync */
  25072. + break;
  25073. + }
  25074. + Tracev((stderr, "inflate: zlib check ok\n"));
  25075. + z->state->mode = DONE;
  25076. + case DONE:
  25077. + return Z_STREAM_END;
  25078. + case BAD:
  25079. + return Z_DATA_ERROR;
  25080. + default:
  25081. + return Z_STREAM_ERROR;
  25082. + }
  25083. +#ifdef NEED_DUMMY_RETURN
  25084. + return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
  25085. +#endif
  25086. +}
  25087. +
  25088. +
  25089. +int ZEXPORT inflateSetDictionary(z, dictionary, dictLength)
  25090. +z_streamp z;
  25091. +const Bytef *dictionary;
  25092. +uInt dictLength;
  25093. +{
  25094. + uInt length = dictLength;
  25095. +
  25096. + if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
  25097. + return Z_STREAM_ERROR;
  25098. +
  25099. + if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
  25100. + z->adler = 1L;
  25101. +
  25102. + if (length >= ((uInt)1<<z->state->wbits))
  25103. + {
  25104. + length = (1<<z->state->wbits)-1;
  25105. + dictionary += dictLength - length;
  25106. + }
  25107. + inflate_set_dictionary(z->state->blocks, dictionary, length);
  25108. + z->state->mode = BLOCKS;
  25109. + return Z_OK;
  25110. +}
  25111. +
  25112. +
  25113. +int ZEXPORT inflateSync(z)
  25114. +z_streamp z;
  25115. +{
  25116. + uInt n; /* number of bytes to look at */
  25117. + Bytef *p; /* pointer to bytes */
  25118. + uInt m; /* number of marker bytes found in a row */
  25119. + uLong r, w; /* temporaries to save total_in and total_out */
  25120. +
  25121. + /* set up */
  25122. + if (z == Z_NULL || z->state == Z_NULL)
  25123. + return Z_STREAM_ERROR;
  25124. + if (z->state->mode != BAD)
  25125. + {
  25126. + z->state->mode = BAD;
  25127. + z->state->sub.marker = 0;
  25128. + }
  25129. + if ((n = z->avail_in) == 0)
  25130. + return Z_BUF_ERROR;
  25131. + p = z->next_in;
  25132. + m = z->state->sub.marker;
  25133. +
  25134. + /* search */
  25135. + while (n && m < 4)
  25136. + {
  25137. + static const Byte mark[4] = {0, 0, 0xff, 0xff};
  25138. + if (*p == mark[m])
  25139. + m++;
  25140. + else if (*p)
  25141. + m = 0;
  25142. + else
  25143. + m = 4 - m;
  25144. + p++, n--;
  25145. + }
  25146. +
  25147. + /* restore */
  25148. + z->total_in += p - z->next_in;
  25149. + z->next_in = p;
  25150. + z->avail_in = n;
  25151. + z->state->sub.marker = m;
  25152. +
  25153. + /* return no joy or set up to restart on a new block */
  25154. + if (m != 4)
  25155. + return Z_DATA_ERROR;
  25156. + r = z->total_in; w = z->total_out;
  25157. + inflateReset(z);
  25158. + z->total_in = r; z->total_out = w;
  25159. + z->state->mode = BLOCKS;
  25160. + return Z_OK;
  25161. +}
  25162. +
  25163. +
  25164. +/* Returns true if inflate is currently at the end of a block generated
  25165. + * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  25166. + * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
  25167. + * but removes the length bytes of the resulting empty stored block. When
  25168. + * decompressing, PPP checks that at the end of input packet, inflate is
  25169. + * waiting for these length bytes.
  25170. + */
  25171. +int ZEXPORT inflateSyncPoint(z)
  25172. +z_streamp z;
  25173. +{
  25174. + if (z == Z_NULL || z->state == Z_NULL || z->state->blocks == Z_NULL)
  25175. + return Z_STREAM_ERROR;
  25176. + return inflate_blocks_sync_point(z->state->blocks);
  25177. +}
  25178. --- /dev/null Tue Mar 11 13:02:56 2003
  25179. +++ linux/net/ipsec/inftrees.c Mon Feb 9 13:51:03 2004
  25180. @@ -0,0 +1,454 @@
  25181. +/* inftrees.c -- generate Huffman trees for efficient decoding
  25182. + * Copyright (C) 1995-2002 Mark Adler
  25183. + * For conditions of distribution and use, see copyright notice in zlib.h
  25184. + */
  25185. +
  25186. +#include <zlib/zutil.h>
  25187. +#include "inftrees.h"
  25188. +
  25189. +#if !defined(BUILDFIXED) && !defined(STDC)
  25190. +# define BUILDFIXED /* non ANSI compilers may not accept inffixed.h */
  25191. +#endif
  25192. +
  25193. +local const char inflate_copyright[] =
  25194. + " inflate 1.1.4 Copyright 1995-2002 Mark Adler ";
  25195. +/*
  25196. + If you use the zlib library in a product, an acknowledgment is welcome
  25197. + in the documentation of your product. If for some reason you cannot
  25198. + include such an acknowledgment, I would appreciate that you keep this
  25199. + copyright string in the executable of your product.
  25200. + */
  25201. +struct internal_state {int dummy;}; /* for buggy compilers */
  25202. +
  25203. +/* simplify the use of the inflate_huft type with some defines */
  25204. +#define exop word.what.Exop
  25205. +#define bits word.what.Bits
  25206. +
  25207. +
  25208. +local int huft_build OF((
  25209. + uIntf *, /* code lengths in bits */
  25210. + uInt, /* number of codes */
  25211. + uInt, /* number of "simple" codes */
  25212. + const uIntf *, /* list of base values for non-simple codes */
  25213. + const uIntf *, /* list of extra bits for non-simple codes */
  25214. + inflate_huft * FAR*,/* result: starting table */
  25215. + uIntf *, /* maximum lookup bits (returns actual) */
  25216. + inflate_huft *, /* space for trees */
  25217. + uInt *, /* hufts used in space */
  25218. + uIntf * )); /* space for values */
  25219. +
  25220. +/* Tables for deflate from PKZIP's appnote.txt. */
  25221. +local const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */
  25222. + 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  25223. + 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
  25224. + /* see note #13 above about 258 */
  25225. +local const uInt cplext[31] = { /* Extra bits for literal codes 257..285 */
  25226. + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  25227. + 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
  25228. +local const uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */
  25229. + 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  25230. + 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  25231. + 8193, 12289, 16385, 24577};
  25232. +local const uInt cpdext[30] = { /* Extra bits for distance codes */
  25233. + 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  25234. + 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  25235. + 12, 12, 13, 13};
  25236. +
  25237. +/*
  25238. + Huffman code decoding is performed using a multi-level table lookup.
  25239. + The fastest way to decode is to simply build a lookup table whose
  25240. + size is determined by the longest code. However, the time it takes
  25241. + to build this table can also be a factor if the data being decoded
  25242. + is not very long. The most common codes are necessarily the
  25243. + shortest codes, so those codes dominate the decoding time, and hence
  25244. + the speed. The idea is you can have a shorter table that decodes the
  25245. + shorter, more probable codes, and then point to subsidiary tables for
  25246. + the longer codes. The time it costs to decode the longer codes is
  25247. + then traded against the time it takes to make longer tables.
  25248. +
  25249. + This results of this trade are in the variables lbits and dbits
  25250. + below. lbits is the number of bits the first level table for literal/
  25251. + length codes can decode in one step, and dbits is the same thing for
  25252. + the distance codes. Subsequent tables are also less than or equal to
  25253. + those sizes. These values may be adjusted either when all of the
  25254. + codes are shorter than that, in which case the longest code length in
  25255. + bits is used, or when the shortest code is *longer* than the requested
  25256. + table size, in which case the length of the shortest code in bits is
  25257. + used.
  25258. +
  25259. + There are two different values for the two tables, since they code a
  25260. + different number of possibilities each. The literal/length table
  25261. + codes 286 possible values, or in a flat code, a little over eight
  25262. + bits. The distance table codes 30 possible values, or a little less
  25263. + than five bits, flat. The optimum values for speed end up being
  25264. + about one bit more than those, so lbits is 8+1 and dbits is 5+1.
  25265. + The optimum values may differ though from machine to machine, and
  25266. + possibly even between compilers. Your mileage may vary.
  25267. + */
  25268. +
  25269. +
  25270. +/* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
  25271. +#define BMAX 15 /* maximum bit length of any code */
  25272. +
  25273. +local int huft_build(b, n, s, d, e, t, m, hp, hn, v)
  25274. +uIntf *b; /* code lengths in bits (all assumed <= BMAX) */
  25275. +uInt n; /* number of codes (assumed <= 288) */
  25276. +uInt s; /* number of simple-valued codes (0..s-1) */
  25277. +const uIntf *d; /* list of base values for non-simple codes */
  25278. +const uIntf *e; /* list of extra bits for non-simple codes */
  25279. +inflate_huft * FAR *t; /* result: starting table */
  25280. +uIntf *m; /* maximum lookup bits, returns actual */
  25281. +inflate_huft *hp; /* space for trees */
  25282. +uInt *hn; /* hufts used in space */
  25283. +uIntf *v; /* working area: values in order of bit length */
  25284. +/* Given a list of code lengths and a maximum table size, make a set of
  25285. + tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
  25286. + if the given code set is incomplete (the tables are still built in this
  25287. + case), or Z_DATA_ERROR if the input is invalid. */
  25288. +{
  25289. +
  25290. + uInt a; /* counter for codes of length k */
  25291. + uInt c[BMAX+1]; /* bit length count table */
  25292. + uInt f; /* i repeats in table every f entries */
  25293. + int g; /* maximum code length */
  25294. + int h; /* table level */
  25295. + register uInt i; /* counter, current code */
  25296. + register uInt j; /* counter */
  25297. + register int k; /* number of bits in current code */
  25298. + int l; /* bits per table (returned in m) */
  25299. + uInt mask; /* (1 << w) - 1, to avoid cc -O bug on HP */
  25300. + register uIntf *p; /* pointer into c[], b[], or v[] */
  25301. + inflate_huft *q; /* points to current table */
  25302. + struct inflate_huft_s r; /* table entry for structure assignment */
  25303. + inflate_huft *u[BMAX]; /* table stack */
  25304. + register int w; /* bits before this table == (l * h) */
  25305. + uInt x[BMAX+1]; /* bit offsets, then code stack */
  25306. + uIntf *xp; /* pointer into x */
  25307. + int y; /* number of dummy codes added */
  25308. + uInt z; /* number of entries in current table */
  25309. +
  25310. +
  25311. + /* Generate counts for each bit length */
  25312. + p = c;
  25313. +#define C0 *p++ = 0;
  25314. +#define C2 C0 C0 C0 C0
  25315. +#define C4 C2 C2 C2 C2
  25316. + C4 /* clear c[]--assume BMAX+1 is 16 */
  25317. + p = b; i = n;
  25318. + do {
  25319. + c[*p++]++; /* assume all entries <= BMAX */
  25320. + } while (--i);
  25321. + if (c[0] == n) /* null input--all zero length codes */
  25322. + {
  25323. + *t = (inflate_huft *)Z_NULL;
  25324. + *m = 0;
  25325. + return Z_OK;
  25326. + }
  25327. +
  25328. +
  25329. + /* Find minimum and maximum length, bound *m by those */
  25330. + l = *m;
  25331. + for (j = 1; j <= BMAX; j++)
  25332. + if (c[j])
  25333. + break;
  25334. + k = j; /* minimum code length */
  25335. + if ((uInt)l < j)
  25336. + l = j;
  25337. + for (i = BMAX; i; i--)
  25338. + if (c[i])
  25339. + break;
  25340. + g = i; /* maximum code length */
  25341. + if ((uInt)l > i)
  25342. + l = i;
  25343. + *m = l;
  25344. +
  25345. +
  25346. + /* Adjust last length count to fill out codes, if needed */
  25347. + for (y = 1 << j; j < i; j++, y <<= 1)
  25348. + if ((y -= c[j]) < 0)
  25349. + return Z_DATA_ERROR;
  25350. + if ((y -= c[i]) < 0)
  25351. + return Z_DATA_ERROR;
  25352. + c[i] += y;
  25353. +
  25354. +
  25355. + /* Generate starting offsets into the value table for each length */
  25356. + x[1] = j = 0;
  25357. + p = c + 1; xp = x + 2;
  25358. + while (--i) { /* note that i == g from above */
  25359. + *xp++ = (j += *p++);
  25360. + }
  25361. +
  25362. +
  25363. + /* Make a table of values in order of bit lengths */
  25364. + p = b; i = 0;
  25365. + do {
  25366. + if ((j = *p++) != 0)
  25367. + v[x[j]++] = i;
  25368. + } while (++i < n);
  25369. + n = x[g]; /* set n to length of v */
  25370. +
  25371. +
  25372. + /* Generate the Huffman codes and for each, make the table entries */
  25373. + x[0] = i = 0; /* first Huffman code is zero */
  25374. + p = v; /* grab values in bit order */
  25375. + h = -1; /* no tables yet--level -1 */
  25376. + w = -l; /* bits decoded == (l * h) */
  25377. + u[0] = (inflate_huft *)Z_NULL; /* just to keep compilers happy */
  25378. + q = (inflate_huft *)Z_NULL; /* ditto */
  25379. + z = 0; /* ditto */
  25380. +
  25381. + /* go through the bit lengths (k already is bits in shortest code) */
  25382. + for (; k <= g; k++)
  25383. + {
  25384. + a = c[k];
  25385. + while (a--)
  25386. + {
  25387. + /* here i is the Huffman code of length k bits for value *p */
  25388. + /* make tables up to required level */
  25389. + while (k > w + l)
  25390. + {
  25391. + h++;
  25392. + w += l; /* previous table always l bits */
  25393. +
  25394. + /* compute minimum size table less than or equal to l bits */
  25395. + z = g - w;
  25396. + z = z > (uInt)l ? l : z; /* table size upper limit */
  25397. + if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
  25398. + { /* too few codes for k-w bit table */
  25399. + f -= a + 1; /* deduct codes from patterns left */
  25400. + xp = c + k;
  25401. + if (j < z)
  25402. + while (++j < z) /* try smaller tables up to z bits */
  25403. + {
  25404. + if ((f <<= 1) <= *++xp)
  25405. + break; /* enough codes to use up j bits */
  25406. + f -= *xp; /* else deduct codes from patterns */
  25407. + }
  25408. + }
  25409. + z = 1 << j; /* table entries for j-bit table */
  25410. +
  25411. + /* allocate new table */
  25412. + if (*hn + z > MANY) /* (note: doesn't matter for fixed) */
  25413. + return Z_DATA_ERROR; /* overflow of MANY */
  25414. + u[h] = q = hp + *hn;
  25415. + *hn += z;
  25416. +
  25417. + /* connect to last table, if there is one */
  25418. + if (h)
  25419. + {
  25420. + x[h] = i; /* save pattern for backing up */
  25421. + r.bits = (Byte)l; /* bits to dump before this table */
  25422. + r.exop = (Byte)j; /* bits in this table */
  25423. + j = i >> (w - l);
  25424. + r.base = (uInt)(q - u[h-1] - j); /* offset to this table */
  25425. + u[h-1][j] = r; /* connect to last table */
  25426. + }
  25427. + else
  25428. + *t = q; /* first table is returned result */
  25429. + }
  25430. +
  25431. + /* set up table entry in r */
  25432. + r.bits = (Byte)(k - w);
  25433. + if (p >= v + n)
  25434. + r.exop = 128 + 64; /* out of values--invalid code */
  25435. + else if (*p < s)
  25436. + {
  25437. + r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); /* 256 is end-of-block */
  25438. + r.base = *p++; /* simple code is just the value */
  25439. + }
  25440. + else
  25441. + {
  25442. + r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
  25443. + r.base = d[*p++ - s];
  25444. + }
  25445. +
  25446. + /* fill code-like entries with r */
  25447. + f = 1 << (k - w);
  25448. + for (j = i >> w; j < z; j += f)
  25449. + q[j] = r;
  25450. +
  25451. + /* backwards increment the k-bit code i */
  25452. + for (j = 1 << (k - 1); i & j; j >>= 1)
  25453. + i ^= j;
  25454. + i ^= j;
  25455. +
  25456. + /* backup over finished tables */
  25457. + mask = (1 << w) - 1; /* needed on HP, cc -O bug */
  25458. + while ((i & mask) != x[h])
  25459. + {
  25460. + h--; /* don't need to update q */
  25461. + w -= l;
  25462. + mask = (1 << w) - 1;
  25463. + }
  25464. + }
  25465. + }
  25466. +
  25467. +
  25468. + /* Return Z_BUF_ERROR if we were given an incomplete table */
  25469. + return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
  25470. +}
  25471. +
  25472. +
  25473. +int inflate_trees_bits(c, bb, tb, hp, z)
  25474. +uIntf *c; /* 19 code lengths */
  25475. +uIntf *bb; /* bits tree desired/actual depth */
  25476. +inflate_huft * FAR *tb; /* bits tree result */
  25477. +inflate_huft *hp; /* space for trees */
  25478. +z_streamp z; /* for messages */
  25479. +{
  25480. + int r;
  25481. + uInt hn = 0; /* hufts used in space */
  25482. + uIntf *v; /* work area for huft_build */
  25483. +
  25484. + if ((v = (uIntf*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL)
  25485. + return Z_MEM_ERROR;
  25486. + r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL,
  25487. + tb, bb, hp, &hn, v);
  25488. + if (r == Z_DATA_ERROR)
  25489. + z->msg = (char*)"oversubscribed dynamic bit lengths tree";
  25490. + else if (r == Z_BUF_ERROR || *bb == 0)
  25491. + {
  25492. + z->msg = (char*)"incomplete dynamic bit lengths tree";
  25493. + r = Z_DATA_ERROR;
  25494. + }
  25495. + ZFREE(z, v);
  25496. + return r;
  25497. +}
  25498. +
  25499. +
  25500. +int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, hp, z)
  25501. +uInt nl; /* number of literal/length codes */
  25502. +uInt nd; /* number of distance codes */
  25503. +uIntf *c; /* that many (total) code lengths */
  25504. +uIntf *bl; /* literal desired/actual bit depth */
  25505. +uIntf *bd; /* distance desired/actual bit depth */
  25506. +inflate_huft * FAR *tl; /* literal/length tree result */
  25507. +inflate_huft * FAR *td; /* distance tree result */
  25508. +inflate_huft *hp; /* space for trees */
  25509. +z_streamp z; /* for messages */
  25510. +{
  25511. + int r;
  25512. + uInt hn = 0; /* hufts used in space */
  25513. + uIntf *v; /* work area for huft_build */
  25514. +
  25515. + /* allocate work area */
  25516. + if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
  25517. + return Z_MEM_ERROR;
  25518. +
  25519. + /* build literal/length tree */
  25520. + r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
  25521. + if (r != Z_OK || *bl == 0)
  25522. + {
  25523. + if (r == Z_DATA_ERROR)
  25524. + z->msg = (char*)"oversubscribed literal/length tree";
  25525. + else if (r != Z_MEM_ERROR)
  25526. + {
  25527. + z->msg = (char*)"incomplete literal/length tree";
  25528. + r = Z_DATA_ERROR;
  25529. + }
  25530. + ZFREE(z, v);
  25531. + return r;
  25532. + }
  25533. +
  25534. + /* build distance tree */
  25535. + r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
  25536. + if (r != Z_OK || (*bd == 0 && nl > 257))
  25537. + {
  25538. + if (r == Z_DATA_ERROR)
  25539. + z->msg = (char*)"oversubscribed distance tree";
  25540. + else if (r == Z_BUF_ERROR) {
  25541. +#ifdef PKZIP_BUG_WORKAROUND
  25542. + r = Z_OK;
  25543. + }
  25544. +#else
  25545. + z->msg = (char*)"incomplete distance tree";
  25546. + r = Z_DATA_ERROR;
  25547. + }
  25548. + else if (r != Z_MEM_ERROR)
  25549. + {
  25550. + z->msg = (char*)"empty distance tree with lengths";
  25551. + r = Z_DATA_ERROR;
  25552. + }
  25553. + ZFREE(z, v);
  25554. + return r;
  25555. +#endif
  25556. + }
  25557. +
  25558. + /* done */
  25559. + ZFREE(z, v);
  25560. + return Z_OK;
  25561. +}
  25562. +
  25563. +
  25564. +/* build fixed tables only once--keep them here */
  25565. +#ifdef BUILDFIXED
  25566. +local int fixed_built = 0;
  25567. +#define FIXEDH 544 /* number of hufts used by fixed tables */
  25568. +local inflate_huft fixed_mem[FIXEDH];
  25569. +local uInt fixed_bl;
  25570. +local uInt fixed_bd;
  25571. +local inflate_huft *fixed_tl;
  25572. +local inflate_huft *fixed_td;
  25573. +#else
  25574. +#include "inffixed.h"
  25575. +#endif
  25576. +
  25577. +
  25578. +int inflate_trees_fixed(bl, bd, tl, td, z)
  25579. +uIntf *bl; /* literal desired/actual bit depth */
  25580. +uIntf *bd; /* distance desired/actual bit depth */
  25581. +inflate_huft * FAR *tl; /* literal/length tree result */
  25582. +inflate_huft * FAR *td; /* distance tree result */
  25583. +z_streamp z; /* for memory allocation */
  25584. +{
  25585. +#ifdef BUILDFIXED
  25586. + /* build fixed tables if not already */
  25587. + if (!fixed_built)
  25588. + {
  25589. + int k; /* temporary variable */
  25590. + uInt f = 0; /* number of hufts used in fixed_mem */
  25591. + uIntf *c; /* length list for huft_build */
  25592. + uIntf *v; /* work area for huft_build */
  25593. +
  25594. + /* allocate memory */
  25595. + if ((c = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
  25596. + return Z_MEM_ERROR;
  25597. + if ((v = (uIntf*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
  25598. + {
  25599. + ZFREE(z, c);
  25600. + return Z_MEM_ERROR;
  25601. + }
  25602. +
  25603. + /* literal table */
  25604. + for (k = 0; k < 144; k++)
  25605. + c[k] = 8;
  25606. + for (; k < 256; k++)
  25607. + c[k] = 9;
  25608. + for (; k < 280; k++)
  25609. + c[k] = 7;
  25610. + for (; k < 288; k++)
  25611. + c[k] = 8;
  25612. + fixed_bl = 9;
  25613. + huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl,
  25614. + fixed_mem, &f, v);
  25615. +
  25616. + /* distance table */
  25617. + for (k = 0; k < 30; k++)
  25618. + c[k] = 5;
  25619. + fixed_bd = 5;
  25620. + huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd,
  25621. + fixed_mem, &f, v);
  25622. +
  25623. + /* done */
  25624. + ZFREE(z, v);
  25625. + ZFREE(z, c);
  25626. + fixed_built = 1;
  25627. + }
  25628. +#endif
  25629. + *bl = fixed_bl;
  25630. + *bd = fixed_bd;
  25631. + *tl = fixed_tl;
  25632. + *td = fixed_td;
  25633. + return Z_OK;
  25634. +}
  25635. --- /dev/null Tue Mar 11 13:02:56 2003
  25636. +++ linux/net/ipsec/inftrees.h Mon Feb 9 13:51:03 2004
  25637. @@ -0,0 +1,63 @@
  25638. +/* inftrees.h -- header to use inftrees.c
  25639. + * Copyright (C) 1995-2002 Mark Adler
  25640. + * For conditions of distribution and use, see copyright notice in zlib.h
  25641. + */
  25642. +
  25643. +/* WARNING: this file should *not* be used by applications. It is
  25644. + part of the implementation of the compression library and is
  25645. + subject to change. Applications should only use zlib.h.
  25646. + */
  25647. +
  25648. +/* Huffman code lookup table entry--this entry is four bytes for machines
  25649. + that have 16-bit pointers (e.g. PC's in the small or medium model). */
  25650. +
  25651. +#ifndef _INFTREES_H
  25652. +#define _INFTREES_H
  25653. +
  25654. +typedef struct inflate_huft_s FAR inflate_huft;
  25655. +
  25656. +struct inflate_huft_s {
  25657. + union {
  25658. + struct {
  25659. + Byte Exop; /* number of extra bits or operation */
  25660. + Byte Bits; /* number of bits in this code or subcode */
  25661. + } what;
  25662. + uInt pad; /* pad structure to a power of 2 (4 bytes for */
  25663. + } word; /* 16-bit, 8 bytes for 32-bit int's) */
  25664. + uInt base; /* literal, length base, distance base,
  25665. + or table offset */
  25666. +};
  25667. +
  25668. +/* Maximum size of dynamic tree. The maximum found in a long but non-
  25669. + exhaustive search was 1004 huft structures (850 for length/literals
  25670. + and 154 for distances, the latter actually the result of an
  25671. + exhaustive search). The actual maximum is not known, but the
  25672. + value below is more than safe. */
  25673. +#define MANY 1440
  25674. +
  25675. +extern int inflate_trees_bits OF((
  25676. + uIntf *, /* 19 code lengths */
  25677. + uIntf *, /* bits tree desired/actual depth */
  25678. + inflate_huft * FAR *, /* bits tree result */
  25679. + inflate_huft *, /* space for trees */
  25680. + z_streamp)); /* for messages */
  25681. +
  25682. +extern int inflate_trees_dynamic OF((
  25683. + uInt, /* number of literal/length codes */
  25684. + uInt, /* number of distance codes */
  25685. + uIntf *, /* that many (total) code lengths */
  25686. + uIntf *, /* literal desired/actual bit depth */
  25687. + uIntf *, /* distance desired/actual bit depth */
  25688. + inflate_huft * FAR *, /* literal/length tree result */
  25689. + inflate_huft * FAR *, /* distance tree result */
  25690. + inflate_huft *, /* space for trees */
  25691. + z_streamp)); /* for messages */
  25692. +
  25693. +extern int inflate_trees_fixed OF((
  25694. + uIntf *, /* literal desired/actual bit depth */
  25695. + uIntf *, /* distance desired/actual bit depth */
  25696. + inflate_huft * FAR *, /* literal/length tree result */
  25697. + inflate_huft * FAR *, /* distance tree result */
  25698. + z_streamp)); /* for memory allocation */
  25699. +
  25700. +#endif /* _INFTREES_H */
  25701. --- /dev/null Tue Mar 11 13:02:56 2003
  25702. +++ linux/net/ipsec/infutil.c Mon Feb 9 13:51:03 2004
  25703. @@ -0,0 +1,87 @@
  25704. +/* inflate_util.c -- data and routines common to blocks and codes
  25705. + * Copyright (C) 1995-2002 Mark Adler
  25706. + * For conditions of distribution and use, see copyright notice in zlib.h
  25707. + */
  25708. +
  25709. +#include <zlib/zutil.h>
  25710. +#include "infblock.h"
  25711. +#include "inftrees.h"
  25712. +#include "infcodes.h"
  25713. +#include "infutil.h"
  25714. +
  25715. +struct inflate_codes_state {int dummy;}; /* for buggy compilers */
  25716. +
  25717. +/* And'ing with mask[n] masks the lower n bits */
  25718. +uInt inflate_mask[17] = {
  25719. + 0x0000,
  25720. + 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
  25721. + 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
  25722. +};
  25723. +
  25724. +
  25725. +/* copy as much as possible from the sliding window to the output area */
  25726. +int inflate_flush(s, z, r)
  25727. +inflate_blocks_statef *s;
  25728. +z_streamp z;
  25729. +int r;
  25730. +{
  25731. + uInt n;
  25732. + Bytef *p;
  25733. + Bytef *q;
  25734. +
  25735. + /* local copies of source and destination pointers */
  25736. + p = z->next_out;
  25737. + q = s->read;
  25738. +
  25739. + /* compute number of bytes to copy as far as end of window */
  25740. + n = (uInt)((q <= s->write ? s->write : s->end) - q);
  25741. + if (n > z->avail_out) n = z->avail_out;
  25742. + if (n && r == Z_BUF_ERROR) r = Z_OK;
  25743. +
  25744. + /* update counters */
  25745. + z->avail_out -= n;
  25746. + z->total_out += n;
  25747. +
  25748. + /* update check information */
  25749. + if (s->checkfn != Z_NULL)
  25750. + z->adler = s->check = (*s->checkfn)(s->check, q, n);
  25751. +
  25752. + /* copy as far as end of window */
  25753. + zmemcpy(p, q, n);
  25754. + p += n;
  25755. + q += n;
  25756. +
  25757. + /* see if more to copy at beginning of window */
  25758. + if (q == s->end)
  25759. + {
  25760. + /* wrap pointers */
  25761. + q = s->window;
  25762. + if (s->write == s->end)
  25763. + s->write = s->window;
  25764. +
  25765. + /* compute bytes to copy */
  25766. + n = (uInt)(s->write - q);
  25767. + if (n > z->avail_out) n = z->avail_out;
  25768. + if (n && r == Z_BUF_ERROR) r = Z_OK;
  25769. +
  25770. + /* update counters */
  25771. + z->avail_out -= n;
  25772. + z->total_out += n;
  25773. +
  25774. + /* update check information */
  25775. + if (s->checkfn != Z_NULL)
  25776. + z->adler = s->check = (*s->checkfn)(s->check, q, n);
  25777. +
  25778. + /* copy */
  25779. + zmemcpy(p, q, n);
  25780. + p += n;
  25781. + q += n;
  25782. + }
  25783. +
  25784. + /* update pointers */
  25785. + z->next_out = p;
  25786. + s->read = q;
  25787. +
  25788. + /* done */
  25789. + return r;
  25790. +}
  25791. --- /dev/null Tue Mar 11 13:02:56 2003
  25792. +++ linux/net/ipsec/infutil.h Mon Feb 9 13:51:03 2004
  25793. @@ -0,0 +1,98 @@
  25794. +/* infutil.h -- types and macros common to blocks and codes
  25795. + * Copyright (C) 1995-2002 Mark Adler
  25796. + * For conditions of distribution and use, see copyright notice in zlib.h
  25797. + */
  25798. +
  25799. +/* WARNING: this file should *not* be used by applications. It is
  25800. + part of the implementation of the compression library and is
  25801. + subject to change. Applications should only use zlib.h.
  25802. + */
  25803. +
  25804. +#ifndef _INFUTIL_H
  25805. +#define _INFUTIL_H
  25806. +
  25807. +typedef enum {
  25808. + TYPE, /* get type bits (3, including end bit) */
  25809. + LENS, /* get lengths for stored */
  25810. + STORED, /* processing stored block */
  25811. + TABLE, /* get table lengths */
  25812. + BTREE, /* get bit lengths tree for a dynamic block */
  25813. + DTREE, /* get length, distance trees for a dynamic block */
  25814. + CODES, /* processing fixed or dynamic block */
  25815. + DRY, /* output remaining window bytes */
  25816. + DONE, /* finished last block, done */
  25817. + BAD} /* got a data error--stuck here */
  25818. +inflate_block_mode;
  25819. +
  25820. +/* inflate blocks semi-private state */
  25821. +struct inflate_blocks_state {
  25822. +
  25823. + /* mode */
  25824. + inflate_block_mode mode; /* current inflate_block mode */
  25825. +
  25826. + /* mode dependent information */
  25827. + union {
  25828. + uInt left; /* if STORED, bytes left to copy */
  25829. + struct {
  25830. + uInt table; /* table lengths (14 bits) */
  25831. + uInt index; /* index into blens (or border) */
  25832. + uIntf *blens; /* bit lengths of codes */
  25833. + uInt bb; /* bit length tree depth */
  25834. + inflate_huft *tb; /* bit length decoding tree */
  25835. + } trees; /* if DTREE, decoding info for trees */
  25836. + struct {
  25837. + inflate_codes_statef
  25838. + *codes;
  25839. + } decode; /* if CODES, current state */
  25840. + } sub; /* submode */
  25841. + uInt last; /* true if this block is the last block */
  25842. +
  25843. + /* mode independent information */
  25844. + uInt bitk; /* bits in bit buffer */
  25845. + uLong bitb; /* bit buffer */
  25846. + inflate_huft *hufts; /* single malloc for tree space */
  25847. + Bytef *window; /* sliding window */
  25848. + Bytef *end; /* one byte after sliding window */
  25849. + Bytef *read; /* window read pointer */
  25850. + Bytef *write; /* window write pointer */
  25851. + check_func checkfn; /* check function */
  25852. + uLong check; /* check on output */
  25853. +
  25854. +};
  25855. +
  25856. +
  25857. +/* defines for inflate input/output */
  25858. +/* update pointers and return */
  25859. +#define UPDBITS {s->bitb=b;s->bitk=k;}
  25860. +#define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;}
  25861. +#define UPDOUT {s->write=q;}
  25862. +#define UPDATE {UPDBITS UPDIN UPDOUT}
  25863. +#define LEAVE {UPDATE return inflate_flush(s,z,r);}
  25864. +/* get bytes and bits */
  25865. +#define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
  25866. +#define NEEDBYTE {if(n)r=Z_OK;else LEAVE}
  25867. +#define NEXTBYTE (n--,*p++)
  25868. +#define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
  25869. +#define DUMPBITS(j) {b>>=(j);k-=(j);}
  25870. +/* output bytes */
  25871. +#define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
  25872. +#define LOADOUT {q=s->write;m=(uInt)WAVAIL;}
  25873. +#define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}}
  25874. +#define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}
  25875. +#define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;}
  25876. +#define OUTBYTE(a) {*q++=(Byte)(a);m--;}
  25877. +/* load local pointers */
  25878. +#define LOAD {LOADIN LOADOUT}
  25879. +
  25880. +/* masks for lower bits (size given to avoid silly warnings with Visual C++) */
  25881. +extern uInt inflate_mask[17];
  25882. +
  25883. +/* copy as much as possible from the sliding window to the output area */
  25884. +extern int inflate_flush OF((
  25885. + inflate_blocks_statef *,
  25886. + z_streamp ,
  25887. + int));
  25888. +
  25889. +struct internal_state {int dummy;}; /* for buggy compilers */
  25890. +
  25891. +#endif /* _INFUTIL_H */
  25892. --- /dev/null Tue Mar 11 13:02:56 2003
  25893. +++ linux/net/ipsec/initaddr.c Mon Feb 9 13:51:03 2004
  25894. @@ -0,0 +1,50 @@
  25895. +/*
  25896. + * initialize address structure
  25897. + * Copyright (C) 2000 Henry Spencer.
  25898. + *
  25899. + * This library is free software; you can redistribute it and/or modify it
  25900. + * under the terms of the GNU Library General Public License as published by
  25901. + * the Free Software Foundation; either version 2 of the License, or (at your
  25902. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  25903. + *
  25904. + * This library is distributed in the hope that it will be useful, but
  25905. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  25906. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  25907. + * License for more details.
  25908. + *
  25909. + * RCSID $Id: initaddr.c,v 1.6 2004/07/10 07:43:47 mcr Exp $
  25910. + */
  25911. +#include "openswan.h"
  25912. +
  25913. +/*
  25914. + - initaddr - initialize ip_address from bytes
  25915. + */
  25916. +err_t /* NULL for success, else string literal */
  25917. +initaddr(src, srclen, af, dst)
  25918. +const unsigned char *src;
  25919. +size_t srclen;
  25920. +int af; /* address family */
  25921. +ip_address *dst;
  25922. +{
  25923. + switch (af) {
  25924. + case AF_INET:
  25925. + if (srclen != 4)
  25926. + return "IPv4 address must be exactly 4 bytes";
  25927. + dst->u.v4.sin_family = af;
  25928. + dst->u.v4.sin_port = 0; /* unused */
  25929. + memcpy((char *)&dst->u.v4.sin_addr.s_addr, src, srclen);
  25930. + break;
  25931. + case AF_INET6:
  25932. + if (srclen != 16)
  25933. + return "IPv6 address must be exactly 16 bytes";
  25934. + dst->u.v6.sin6_family = af;
  25935. + dst->u.v6.sin6_flowinfo = 0; /* unused */
  25936. + dst->u.v6.sin6_port = 0; /* unused */
  25937. + memcpy((char *)&dst->u.v6.sin6_addr, src, srclen);
  25938. + break;
  25939. + default:
  25940. + return "unknown address family in initaddr";
  25941. + break;
  25942. + }
  25943. + return NULL;
  25944. +}
  25945. --- /dev/null Tue Mar 11 13:02:56 2003
  25946. +++ linux/net/ipsec/ipcomp.c Mon Feb 9 13:51:03 2004
  25947. @@ -0,0 +1,701 @@
  25948. +/*
  25949. + * IPCOMP zlib interface code.
  25950. + * Copyright (C) 2000 Svenning Soerensen <svenning@post5.tele.dk>
  25951. + * Copyright (C) 2000, 2001 Richard Guy Briggs <rgb@conscoop.ottawa.on.ca>
  25952. + *
  25953. + * This program is free software; you can redistribute it and/or modify it
  25954. + * under the terms of the GNU General Public License as published by the
  25955. + * Free Software Foundation; either version 2 of the License, or (at your
  25956. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  25957. + *
  25958. + * This program is distributed in the hope that it will be useful, but
  25959. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  25960. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  25961. + * for more details.
  25962. + */
  25963. +
  25964. +char ipcomp_c_version[] = "RCSID $Id: ipcomp.c,v 1.41.2.5 2006/10/06 21:39:26 paul Exp $";
  25965. +
  25966. +/* SSS */
  25967. +
  25968. +#ifndef AUTOCONF_INCLUDED
  25969. +#include <linux/config.h>
  25970. +#endif
  25971. +#include <linux/version.h>
  25972. +
  25973. +#define __NO_VERSION__
  25974. +#include <linux/module.h>
  25975. +#include <linux/kernel.h> /* printk() */
  25976. +
  25977. +#include "openswan/ipsec_param.h"
  25978. +
  25979. +#ifdef MALLOC_SLAB
  25980. +# include <linux/slab.h> /* kmalloc() */
  25981. +#else /* MALLOC_SLAB */
  25982. +# include <linux/malloc.h> /* kmalloc() */
  25983. +#endif /* MALLOC_SLAB */
  25984. +#include <linux/errno.h> /* error codes */
  25985. +#include <linux/types.h>
  25986. +#include <linux/netdevice.h>
  25987. +#include <linux/ip.h>
  25988. +#include <linux/skbuff.h>
  25989. +
  25990. +#include <linux/netdevice.h> /* struct device, and other headers */
  25991. +#include <linux/etherdevice.h> /* eth_type_trans */
  25992. +#include <linux/ip.h> /* struct iphdr */
  25993. +#include <linux/skbuff.h>
  25994. +#include <asm/uaccess.h>
  25995. +#include <asm/checksum.h>
  25996. +
  25997. +#include <openswan.h>
  25998. +
  25999. +#include <net/ip.h>
  26000. +
  26001. +#include "openswan/radij.h"
  26002. +#include "openswan/ipsec_encap.h"
  26003. +#include "openswan/ipsec_sa.h"
  26004. +
  26005. +#include "openswan/ipsec_xform.h"
  26006. +#include "openswan/ipsec_tunnel.h"
  26007. +#include "openswan/ipsec_rcv.h" /* sysctl_ipsec_inbound_policy_check */
  26008. +#include "openswan/ipsec_proto.h"
  26009. +#include "openswan/ipcomp.h"
  26010. +#include "zlib/zlib.h"
  26011. +#include "zlib/zutil.h"
  26012. +
  26013. +#include <pfkeyv2.h> /* SADB_X_CALG_DEFLATE */
  26014. +
  26015. +#ifdef CONFIG_KLIPS_DEBUG
  26016. +int sysctl_ipsec_debug_ipcomp = 0;
  26017. +#endif /* CONFIG_KLIPS_DEBUG */
  26018. +
  26019. +static
  26020. +struct sk_buff *skb_copy_ipcomp(struct sk_buff *skb, int data_growth, int gfp_mask);
  26021. +
  26022. +static
  26023. +voidpf my_zcalloc(voidpf opaque, uInt items, uInt size)
  26024. +{
  26025. + return (voidpf) kmalloc(items*size, GFP_ATOMIC);
  26026. +}
  26027. +
  26028. +static
  26029. +void my_zfree(voidpf opaque, voidpf address)
  26030. +{
  26031. + kfree(address);
  26032. +}
  26033. +
  26034. +/*
  26035. + * We use this function because sometimes we want to pass a negative offset
  26036. + * into skb_put(), this does not work on 64bit platforms because long to
  26037. + * unsigned int casting.
  26038. + */
  26039. +static inline unsigned char *
  26040. +safe_skb_put(struct sk_buff *skb, int extend)
  26041. +{
  26042. + unsigned char *ptr;
  26043. +
  26044. + if (extend>0) {
  26045. + // increase the size of the packet
  26046. + ptr = skb_put(skb, extend);
  26047. + } else {
  26048. + // shrink the size of the packet
  26049. + ptr = skb->tail;
  26050. + skb_trim (skb, skb->len + extend);
  26051. + }
  26052. +
  26053. + return ptr;
  26054. +}
  26055. +
  26056. +struct sk_buff *skb_compress(struct sk_buff *skb, struct ipsec_sa *ips, unsigned int *flags)
  26057. +{
  26058. + struct iphdr *iph;
  26059. + unsigned int iphlen, pyldsz, cpyldsz;
  26060. + unsigned char *buffer;
  26061. + z_stream zs;
  26062. + int zresult;
  26063. +
  26064. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26065. + "klips_debug:skb_compress: .\n");
  26066. +
  26067. + if(skb == NULL) {
  26068. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26069. + "klips_debug:skb_compress: "
  26070. + "passed in NULL skb, returning ERROR.\n");
  26071. + if(flags != NULL) {
  26072. + *flags |= IPCOMP_PARMERROR;
  26073. + }
  26074. + return skb;
  26075. + }
  26076. +
  26077. + if(ips == NULL) {
  26078. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26079. + "klips_debug:skb_compress: "
  26080. + "passed in NULL ipsec_sa needed for cpi, returning ERROR.\n");
  26081. + if(flags) {
  26082. + *flags |= IPCOMP_PARMERROR;
  26083. + }
  26084. + return skb;
  26085. + }
  26086. +
  26087. + if (flags == NULL) {
  26088. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26089. + "klips_debug:skb_compress: "
  26090. + "passed in NULL flags, returning ERROR.\n");
  26091. + ipsec_kfree_skb(skb);
  26092. + return NULL;
  26093. + }
  26094. +
  26095. +#ifdef NET_21
  26096. + iph = skb->nh.iph;
  26097. +#else /* NET_21 */
  26098. + iph = skb->ip_hdr;
  26099. +#endif /* NET_21 */
  26100. +
  26101. + switch (iph->protocol) {
  26102. + case IPPROTO_COMP:
  26103. + case IPPROTO_AH:
  26104. + case IPPROTO_ESP:
  26105. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26106. + "klips_debug:skb_compress: "
  26107. + "skipping compression of packet with ip protocol %d.\n",
  26108. + iph->protocol);
  26109. + *flags |= IPCOMP_UNCOMPRESSABLE;
  26110. + return skb;
  26111. + }
  26112. +
  26113. + /* Don't compress packets already fragmented */
  26114. + if (iph->frag_off & __constant_htons(IP_MF | IP_OFFSET)) {
  26115. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26116. + "klips_debug:skb_compress: "
  26117. + "skipping compression of fragmented packet.\n");
  26118. + *flags |= IPCOMP_UNCOMPRESSABLE;
  26119. + return skb;
  26120. + }
  26121. +
  26122. + iphlen = iph->ihl << 2;
  26123. + pyldsz = ntohs(iph->tot_len) - iphlen;
  26124. +
  26125. + /* Don't compress less than 90 bytes (rfc 2394) */
  26126. + if (pyldsz < 90) {
  26127. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26128. + "klips_debug:skb_compress: "
  26129. + "skipping compression of tiny packet, len=%d.\n",
  26130. + pyldsz);
  26131. + *flags |= IPCOMP_UNCOMPRESSABLE;
  26132. + return skb;
  26133. + }
  26134. +
  26135. + /* Adaptive decision */
  26136. + if (ips->ips_comp_adapt_skip) {
  26137. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26138. + "klips_debug:skb_compress: "
  26139. + "skipping compression: ips_comp_adapt_skip=%d.\n",
  26140. + ips->ips_comp_adapt_skip);
  26141. + ips->ips_comp_adapt_skip--;
  26142. + *flags |= IPCOMP_UNCOMPRESSABLE;
  26143. + return skb;
  26144. + }
  26145. +
  26146. + zs.zalloc = my_zcalloc;
  26147. + zs.zfree = my_zfree;
  26148. + zs.opaque = 0;
  26149. +
  26150. + /* We want to use deflateInit2 because we don't want the adler
  26151. + header. */
  26152. + zresult = deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -11,
  26153. + DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
  26154. + if (zresult != Z_OK) {
  26155. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26156. + "klips_error:skb_compress: "
  26157. + "deflateInit2() returned error %d (%s), "
  26158. + "skipping compression.\n",
  26159. + zresult,
  26160. + zs.msg ? zs.msg : zError(zresult));
  26161. + *flags |= IPCOMP_COMPRESSIONERROR;
  26162. + return skb;
  26163. + }
  26164. +
  26165. +
  26166. + /* Max output size. Result should be max this size.
  26167. + * Implementation specific tweak:
  26168. + * If it's not at least 32 bytes and 6.25% smaller than
  26169. + * the original packet, it's probably not worth wasting
  26170. + * the receiver's CPU cycles decompressing it.
  26171. + * Your mileage may vary.
  26172. + */
  26173. + cpyldsz = pyldsz - sizeof(struct ipcomphdr) - (pyldsz <= 512 ? 32 : pyldsz >> 4);
  26174. +
  26175. + buffer = kmalloc(cpyldsz, GFP_ATOMIC);
  26176. + if (!buffer) {
  26177. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26178. + "klips_error:skb_compress: "
  26179. + "unable to kmalloc(%d, GFP_ATOMIC), "
  26180. + "skipping compression.\n",
  26181. + cpyldsz);
  26182. + *flags |= IPCOMP_COMPRESSIONERROR;
  26183. + deflateEnd(&zs);
  26184. + return skb;
  26185. + }
  26186. +
  26187. +#ifdef CONFIG_KLIPS_DEBUG
  26188. + if(sysctl_ipsec_debug_ipcomp && sysctl_ipsec_debug_verbose) {
  26189. + __u8 *c;
  26190. +
  26191. + c = (__u8*)iph + iphlen;
  26192. + ipsec_dmp_block("compress before", c, pyldsz);
  26193. + }
  26194. +#endif /* CONFIG_KLIPS_DEBUG */
  26195. +
  26196. + zs.next_in = (char *) iph + iphlen; /* start of payload */
  26197. + zs.avail_in = pyldsz;
  26198. + zs.next_out = buffer; /* start of compressed payload */
  26199. + zs.avail_out = cpyldsz;
  26200. +
  26201. + /* Finish compression in one step */
  26202. + zresult = deflate(&zs, Z_FINISH);
  26203. +
  26204. + /* Free all dynamically allocated buffers */
  26205. + deflateEnd(&zs);
  26206. + if (zresult != Z_STREAM_END) {
  26207. + *flags |= IPCOMP_UNCOMPRESSABLE;
  26208. + kfree(buffer);
  26209. +
  26210. + /* Adjust adaptive counters */
  26211. + if (++(ips->ips_comp_adapt_tries) == IPCOMP_ADAPT_INITIAL_TRIES) {
  26212. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26213. + "klips_debug:skb_compress: "
  26214. + "first %d packets didn't compress, "
  26215. + "skipping next %d\n",
  26216. + IPCOMP_ADAPT_INITIAL_TRIES,
  26217. + IPCOMP_ADAPT_INITIAL_SKIP);
  26218. + ips->ips_comp_adapt_skip = IPCOMP_ADAPT_INITIAL_SKIP;
  26219. + }
  26220. + else if (ips->ips_comp_adapt_tries == IPCOMP_ADAPT_INITIAL_TRIES + IPCOMP_ADAPT_SUBSEQ_TRIES) {
  26221. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26222. + "klips_debug:skb_compress: "
  26223. + "next %d packets didn't compress, "
  26224. + "skipping next %d\n",
  26225. + IPCOMP_ADAPT_SUBSEQ_TRIES,
  26226. + IPCOMP_ADAPT_SUBSEQ_SKIP);
  26227. + ips->ips_comp_adapt_skip = IPCOMP_ADAPT_SUBSEQ_SKIP;
  26228. + ips->ips_comp_adapt_tries = IPCOMP_ADAPT_INITIAL_TRIES;
  26229. + }
  26230. +
  26231. + return skb;
  26232. + }
  26233. +
  26234. + /* resulting compressed size */
  26235. + cpyldsz -= zs.avail_out;
  26236. +
  26237. + /* Insert IPCOMP header */
  26238. + ((struct ipcomphdr*) ((char*) iph + iphlen))->ipcomp_nh = iph->protocol;
  26239. + ((struct ipcomphdr*) ((char*) iph + iphlen))->ipcomp_flags = 0;
  26240. + /* use the bottom 16 bits of the spi for the cpi. The top 16 bits are
  26241. + for internal reference only. */
  26242. + ((struct ipcomphdr*) (((char*)iph) + iphlen))->ipcomp_cpi = htons((__u16)(ntohl(ips->ips_said.spi) & 0x0000ffff));
  26243. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26244. + "klips_debug:skb_compress: "
  26245. + "spi=%08x, spi&0xffff=%04x, cpi=%04x, payload size: raw=%d, comp=%d.\n",
  26246. + ntohl(ips->ips_said.spi),
  26247. + ntohl(ips->ips_said.spi) & 0x0000ffff,
  26248. + ntohs(((struct ipcomphdr*)(((char*)iph)+iphlen))->ipcomp_cpi),
  26249. + pyldsz,
  26250. + cpyldsz);
  26251. +
  26252. + /* Update IP header */
  26253. + iph->protocol = IPPROTO_COMP;
  26254. + iph->tot_len = htons(iphlen + sizeof(struct ipcomphdr) + cpyldsz);
  26255. +#if 1 /* XXX checksum is done by ipsec_tunnel ? */
  26256. + iph->check = 0;
  26257. + iph->check = ip_fast_csum((char *) iph, iph->ihl);
  26258. +#endif
  26259. +
  26260. + /* Copy compressed payload */
  26261. + memcpy((char *) iph + iphlen + sizeof(struct ipcomphdr),
  26262. + buffer,
  26263. + cpyldsz);
  26264. + kfree(buffer);
  26265. +
  26266. + /* Update skb length/tail by "unputting" the shrinkage */
  26267. + safe_skb_put (skb, cpyldsz + sizeof(struct ipcomphdr) - pyldsz);
  26268. +
  26269. +#ifdef CONFIG_KLIPS_DEBUG
  26270. + if(sysctl_ipsec_debug_ipcomp && sysctl_ipsec_debug_verbose) {
  26271. + __u8 *c;
  26272. +
  26273. + c = (__u8*)iph + iphlen + sizeof(struct ipcomphdr);
  26274. + ipsec_dmp_block("compress result", c, cpyldsz);
  26275. + }
  26276. +#endif /* CONFIG_KLIPS_DEBUG */
  26277. +
  26278. + ips->ips_comp_adapt_skip = 0;
  26279. + ips->ips_comp_adapt_tries = 0;
  26280. +
  26281. + return skb;
  26282. +}
  26283. +
  26284. +struct sk_buff *skb_decompress(struct sk_buff *skb, struct ipsec_sa *ips, unsigned int *flags)
  26285. +{
  26286. + struct sk_buff *nskb = NULL;
  26287. +
  26288. + /* original ip header */
  26289. + struct iphdr *oiph, *iph;
  26290. + unsigned int iphlen, pyldsz, cpyldsz;
  26291. + z_stream zs;
  26292. + int zresult;
  26293. +
  26294. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26295. + "klips_debug:skb_decompress: .\n");
  26296. +
  26297. + if(!skb) {
  26298. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26299. + "klips_error:skb_decompress: "
  26300. + "passed in NULL skb, returning ERROR.\n");
  26301. + if (flags) *flags |= IPCOMP_PARMERROR;
  26302. + return skb;
  26303. + }
  26304. +
  26305. + if(!ips && sysctl_ipsec_inbound_policy_check) {
  26306. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26307. + "klips_error:skb_decompress: "
  26308. + "passed in NULL ipsec_sa needed for comp alg, returning ERROR.\n");
  26309. + if (flags) *flags |= IPCOMP_PARMERROR;
  26310. + return skb;
  26311. + }
  26312. +
  26313. + if (!flags) {
  26314. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26315. + "klips_error:skb_decompress: "
  26316. + "passed in NULL flags, returning ERROR.\n");
  26317. + ipsec_kfree_skb(skb);
  26318. + return NULL;
  26319. + }
  26320. +
  26321. +#ifdef NET_21
  26322. + oiph = skb->nh.iph;
  26323. +#else /* NET_21 */
  26324. + oiph = skb->ip_hdr;
  26325. +#endif /* NET_21 */
  26326. +
  26327. + iphlen = oiph->ihl << 2;
  26328. +
  26329. + if (oiph->protocol != IPPROTO_COMP) {
  26330. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26331. + "klips_error:skb_decompress: "
  26332. + "called with non-IPCOMP packet (protocol=%d),"
  26333. + "skipping decompression.\n",
  26334. + oiph->protocol);
  26335. + *flags |= IPCOMP_PARMERROR;
  26336. + return skb;
  26337. + }
  26338. +
  26339. + if ( (((struct ipcomphdr*)((char*) oiph + iphlen))->ipcomp_flags != 0)
  26340. + || ((((struct ipcomphdr*) ((char*) oiph + iphlen))->ipcomp_cpi
  26341. + != htons(SADB_X_CALG_DEFLATE))
  26342. + && sysctl_ipsec_inbound_policy_check
  26343. + && (!ips || (ips && (ips->ips_encalg != SADB_X_CALG_DEFLATE)))) ) {
  26344. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26345. + "klips_error:skb_decompress: "
  26346. + "called with incompatible IPCOMP packet (flags=%d, "
  26347. + "cpi=%d), ips-compalg=%d, skipping decompression.\n",
  26348. + ntohs(((struct ipcomphdr*) ((char*) oiph + iphlen))->ipcomp_flags),
  26349. + ntohs(((struct ipcomphdr*) ((char*) oiph + iphlen))->ipcomp_cpi),
  26350. + ips ? ips->ips_encalg : 0);
  26351. + *flags |= IPCOMP_PARMERROR;
  26352. +
  26353. + return skb;
  26354. + }
  26355. +
  26356. + if (ntohs(oiph->frag_off) & ~0x4000) {
  26357. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26358. + "klips_error:skb_decompress: "
  26359. + "called with fragmented IPCOMP packet, "
  26360. + "skipping decompression.\n");
  26361. + *flags |= IPCOMP_PARMERROR;
  26362. + return skb;
  26363. + }
  26364. +
  26365. + /* original compressed payload size */
  26366. + cpyldsz = ntohs(oiph->tot_len) - iphlen - sizeof(struct ipcomphdr);
  26367. +
  26368. + zs.zalloc = my_zcalloc;
  26369. + zs.zfree = my_zfree;
  26370. + zs.opaque = 0;
  26371. +
  26372. + zs.next_in = (char *) oiph + iphlen + sizeof(struct ipcomphdr);
  26373. + zs.avail_in = cpyldsz;
  26374. +
  26375. + /* Maybe we should be a bit conservative about memory
  26376. + requirements and use inflateInit2 */
  26377. + /* Beware, that this might make us unable to decompress packets
  26378. + from other implementations - HINT: check PGPnet source code */
  26379. + /* We want to use inflateInit2 because we don't want the adler
  26380. + header. */
  26381. + zresult = inflateInit2(&zs, -15);
  26382. + if (zresult != Z_OK) {
  26383. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26384. + "klips_error:skb_decompress: "
  26385. + "inflateInit2() returned error %d (%s), "
  26386. + "skipping decompression.\n",
  26387. + zresult,
  26388. + zs.msg ? zs.msg : zError(zresult));
  26389. + *flags |= IPCOMP_DECOMPRESSIONERROR;
  26390. +
  26391. + return skb;
  26392. + }
  26393. +
  26394. + /* We have no way of knowing the exact length of the resulting
  26395. + decompressed output before we have actually done the decompression.
  26396. + For now, we guess that the packet will not be bigger than the
  26397. + attached ipsec device's mtu or 16260, whichever is biggest.
  26398. + This may be wrong, since the sender's mtu may be bigger yet.
  26399. + XXX This must be dealt with later XXX
  26400. + */
  26401. +
  26402. + /* max payload size */
  26403. + pyldsz = skb->dev ? (skb->dev->mtu < 16260 ? 16260 : skb->dev->mtu)
  26404. + : (65520 - iphlen);
  26405. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26406. + "klips_debug:skb_decompress: "
  26407. + "max payload size: %d\n", pyldsz);
  26408. +
  26409. + while (pyldsz > (cpyldsz + sizeof(struct ipcomphdr)) &&
  26410. + (nskb = skb_copy_ipcomp(skb,
  26411. + pyldsz - cpyldsz - sizeof(struct ipcomphdr),
  26412. + GFP_ATOMIC)) == NULL) {
  26413. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26414. + "klips_error:skb_decompress: "
  26415. + "unable to skb_copy_ipcomp(skb, %d, GFP_ATOMIC), "
  26416. + "trying with less payload size.\n",
  26417. + (int)(pyldsz - cpyldsz - sizeof(struct ipcomphdr)));
  26418. + pyldsz >>=1;
  26419. + }
  26420. +
  26421. + if (!nskb) {
  26422. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26423. + "klips_error:skb_decompress: "
  26424. + "unable to allocate memory, dropping packet.\n");
  26425. + *flags |= IPCOMP_DECOMPRESSIONERROR;
  26426. + inflateEnd(&zs);
  26427. +
  26428. + return skb;
  26429. + }
  26430. +
  26431. +#ifdef CONFIG_KLIPS_DEBUG
  26432. + if(sysctl_ipsec_debug_ipcomp && sysctl_ipsec_debug_verbose) {
  26433. + __u8 *c;
  26434. +
  26435. + c = (__u8*)oiph + iphlen + sizeof(struct ipcomphdr);
  26436. + ipsec_dmp_block("decompress before", c, cpyldsz);
  26437. + }
  26438. +#endif /* CONFIG_KLIPS_DEBUG */
  26439. +
  26440. +#ifdef NET_21
  26441. + iph = nskb->nh.iph;
  26442. +#else /* NET_21 */
  26443. + iph = nskb->ip_hdr;
  26444. +#endif /* NET_21 */
  26445. + zs.next_out = (char *)iph + iphlen;
  26446. + zs.avail_out = pyldsz;
  26447. +
  26448. + zresult = inflate(&zs, Z_SYNC_FLUSH);
  26449. +
  26450. + /* work around a bug in zlib, which sometimes wants to taste an extra
  26451. + * byte when being used in the (undocumented) raw deflate mode.
  26452. + */
  26453. + if (zresult == Z_OK && !zs.avail_in && zs.avail_out) {
  26454. + __u8 zerostuff = 0;
  26455. +
  26456. + zs.next_in = &zerostuff;
  26457. + zs.avail_in = 1;
  26458. + zresult = inflate(&zs, Z_FINISH);
  26459. + }
  26460. +
  26461. + inflateEnd(&zs);
  26462. + if (zresult != Z_STREAM_END) {
  26463. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26464. + "klips_error:skb_decompress: "
  26465. + "inflate() returned error %d (%s), "
  26466. + "skipping decompression.\n",
  26467. + zresult,
  26468. + zs.msg ? zs.msg : zError(zresult));
  26469. + *flags |= IPCOMP_DECOMPRESSIONERROR;
  26470. + ipsec_kfree_skb(nskb);
  26471. +
  26472. + return skb;
  26473. + }
  26474. +
  26475. + /* Update IP header */
  26476. + /* resulting decompressed size */
  26477. + pyldsz -= zs.avail_out;
  26478. + iph->tot_len = htons(iphlen + pyldsz);
  26479. + iph->protocol = ((struct ipcomphdr*) ((char*) oiph + iphlen))->ipcomp_nh;
  26480. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26481. + "klips_debug:skb_decompress: "
  26482. + "spi=%08x, spi&0xffff=%04x, cpi=%04x, payload size: comp=%d, raw=%d, nh=%d.\n",
  26483. + ips ? ntohl(ips->ips_said.spi) : 0,
  26484. + ips ? ntohl(ips->ips_said.spi) & 0x0000ffff : 0,
  26485. + ntohs(((struct ipcomphdr*)(((char*)oiph)+iphlen))->ipcomp_cpi),
  26486. + cpyldsz,
  26487. + pyldsz,
  26488. + iph->protocol);
  26489. +
  26490. +#if 1 /* XXX checksum is done by ipsec_rcv ? */
  26491. + iph->check = 0;
  26492. + iph->check = ip_fast_csum((char*) iph, iph->ihl);
  26493. +#endif
  26494. +
  26495. + /* Update skb length/tail by "unputting" the unused data area */
  26496. + safe_skb_put(nskb, -zs.avail_out);
  26497. +
  26498. + ipsec_kfree_skb(skb);
  26499. +
  26500. + if (iph->protocol == IPPROTO_COMP)
  26501. + {
  26502. +#ifdef CONFIG_KLIPS_DEBUG
  26503. + if(sysctl_ipsec_debug_ipcomp)
  26504. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26505. + "klips_debug:skb_decompress: "
  26506. + "Eh? inner packet is also compressed, dropping.\n");
  26507. +#endif /* CONFIG_KLIPS_DEBUG */
  26508. +
  26509. + ipsec_kfree_skb(nskb);
  26510. + return NULL;
  26511. + }
  26512. +
  26513. +#ifdef CONFIG_KLIPS_DEBUG
  26514. + if(sysctl_ipsec_debug_ipcomp && sysctl_ipsec_debug_verbose) {
  26515. + __u8 *c;
  26516. +
  26517. + c = (__u8*)iph + iphlen;
  26518. + ipsec_dmp_block("decompress result", c, pyldsz);
  26519. + }
  26520. +#endif /* CONFIG_KLIPS_DEBUG */
  26521. +
  26522. + return nskb;
  26523. +}
  26524. +
  26525. +
  26526. +/* this is derived from skb_copy() in linux 2.2.14 */
  26527. +/* May be incompatible with other kernel versions!! */
  26528. +static
  26529. +struct sk_buff *skb_copy_ipcomp(struct sk_buff *skb, int data_growth, int gfp_mask)
  26530. +{
  26531. + struct sk_buff *n;
  26532. + struct iphdr *iph;
  26533. + unsigned long offset;
  26534. + unsigned int iphlen;
  26535. +
  26536. + if(!skb) {
  26537. + KLIPS_PRINT(sysctl_ipsec_debug_ipcomp,
  26538. + "klips_debug:skb_copy_ipcomp: "
  26539. + "passed in NULL skb, returning NULL.\n");
  26540. + return NULL;
  26541. + }
  26542. +
  26543. + /*
  26544. + * Allocate the copy buffer
  26545. + */
  26546. +
  26547. +#ifdef NET_21
  26548. + iph = skb->nh.iph;
  26549. +#else /* NET_21 */
  26550. + iph = skb->ip_hdr;
  26551. +#endif /* NET_21 */
  26552. + if (!iph) return NULL;
  26553. + iphlen = iph->ihl << 2;
  26554. +
  26555. + n=alloc_skb(skb->end - skb->head + data_growth, gfp_mask);
  26556. + if(n==NULL)
  26557. + return NULL;
  26558. +
  26559. + /*
  26560. + * Shift between the two data areas in bytes
  26561. + */
  26562. +
  26563. + offset=n->head-skb->head;
  26564. +
  26565. + /* Set the data pointer */
  26566. + skb_reserve(n,skb->data-skb->head);
  26567. + /* Set the tail pointer and length */
  26568. + safe_skb_put(n,skb->len+data_growth);
  26569. + /* Copy the bytes up to and including the ip header */
  26570. + memcpy(n->head,
  26571. + skb->head,
  26572. + ((char *)iph - (char *)skb->head) + iphlen);
  26573. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
  26574. + n->list=NULL;
  26575. +#endif
  26576. + n->next=NULL;
  26577. + n->prev=NULL;
  26578. + n->sk=NULL;
  26579. + n->dev=skb->dev;
  26580. + if (skb->h.raw)
  26581. + n->h.raw=skb->h.raw+offset;
  26582. + else
  26583. + n->h.raw=NULL;
  26584. + n->protocol=skb->protocol;
  26585. +#ifdef NET_21
  26586. + n->csum = 0;
  26587. + n->priority=skb->priority;
  26588. + n->dst=dst_clone(skb->dst);
  26589. + n->nh.raw=skb->nh.raw+offset;
  26590. +#ifndef NETDEV_23
  26591. + n->is_clone=0;
  26592. +#endif /* NETDEV_23 */
  26593. + atomic_set(&n->users, 1);
  26594. + n->destructor = NULL;
  26595. +#ifdef HAVE_SOCK_SECURITY
  26596. + n->security=skb->security;
  26597. +#endif
  26598. + memcpy(n->cb, skb->cb, sizeof(skb->cb));
  26599. +#ifdef CONFIG_IP_FIREWALL
  26600. + n->fwmark = skb->fwmark;
  26601. +#endif
  26602. +#else /* NET_21 */
  26603. + n->link3=NULL;
  26604. + n->when=skb->when;
  26605. + n->ip_hdr=(struct iphdr *)(((char *)skb->ip_hdr)+offset);
  26606. + n->saddr=skb->saddr;
  26607. + n->daddr=skb->daddr;
  26608. + n->raddr=skb->raddr;
  26609. + n->seq=skb->seq;
  26610. + n->end_seq=skb->end_seq;
  26611. + n->ack_seq=skb->ack_seq;
  26612. + n->acked=skb->acked;
  26613. + n->free=1;
  26614. + n->arp=skb->arp;
  26615. + n->tries=0;
  26616. + n->lock=0;
  26617. + n->users=0;
  26618. + memcpy(n->proto_priv, skb->proto_priv, sizeof(skb->proto_priv));
  26619. +#endif /* NET_21 */
  26620. + if (skb->mac.raw)
  26621. + n->mac.raw=skb->mac.raw+offset;
  26622. + else
  26623. + n->mac.raw=NULL;
  26624. +#ifndef NETDEV_23
  26625. + n->used=skb->used;
  26626. +#endif /* !NETDEV_23 */
  26627. + n->pkt_type=skb->pkt_type;
  26628. +#ifndef NETDEV_23
  26629. + n->pkt_bridged=skb->pkt_bridged;
  26630. +#endif /* NETDEV_23 */
  26631. + n->ip_summed=0;
  26632. +#ifdef HAVE_TSTAMP
  26633. + n->tstamp = skb->tstamp;
  26634. +#else
  26635. + n->stamp=skb->stamp;
  26636. +#endif
  26637. +#ifndef NETDEV_23 /* this seems to have been removed in 2.4 */
  26638. +#if defined(CONFIG_SHAPER) || defined(CONFIG_SHAPER_MODULE)
  26639. + n->shapelatency=skb->shapelatency; /* Latency on frame */
  26640. + n->shapeclock=skb->shapeclock; /* Time it should go out */
  26641. + n->shapelen=skb->shapelen; /* Frame length in clocks */
  26642. + n->shapestamp=skb->shapestamp; /* Stamp for shaper */
  26643. + n->shapepend=skb->shapepend; /* Pending */
  26644. +#endif /* defined(CONFIG_SHAPER) || defined(CONFIG_SHAPER_MODULE) */
  26645. +#endif /* NETDEV_23 */
  26646. +
  26647. + return n;
  26648. +}
  26649. --- /dev/null Tue Mar 11 13:02:56 2003
  26650. +++ linux/net/ipsec/ipsec_ah.c Mon Feb 9 13:51:03 2004
  26651. @@ -0,0 +1,407 @@
  26652. +/*
  26653. + * processing code for AH
  26654. + * Copyright (C) 2003-2004 Michael Richardson <mcr@xelerance.com>
  26655. + *
  26656. + * This program is free software; you can redistribute it and/or modify it
  26657. + * under the terms of the GNU General Public License as published by the
  26658. + * Free Software Foundation; either version 2 of the License, or (at your
  26659. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  26660. + *
  26661. + * This program is distributed in the hope that it will be useful, but
  26662. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  26663. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  26664. + * for more details.
  26665. + */
  26666. +
  26667. +char ipsec_ah_c_version[] = "RCSID $Id: ipsec_ah.c,v 1.12.2.2 2006/10/06 21:39:26 paul Exp $";
  26668. +#ifndef AUTOCONF_INCLUDED
  26669. +#include <linux/config.h>
  26670. +#endif
  26671. +#include <linux/version.h>
  26672. +
  26673. +#define __NO_VERSION__
  26674. +#include <linux/module.h>
  26675. +#include <linux/kernel.h> /* printk() */
  26676. +
  26677. +#include "openswan/ipsec_param.h"
  26678. +
  26679. +#ifdef MALLOC_SLAB
  26680. +# include <linux/slab.h> /* kmalloc() */
  26681. +#else /* MALLOC_SLAB */
  26682. +# include <linux/malloc.h> /* kmalloc() */
  26683. +#endif /* MALLOC_SLAB */
  26684. +#include <linux/errno.h> /* error codes */
  26685. +#include <linux/types.h> /* size_t */
  26686. +#include <linux/interrupt.h> /* mark_bh */
  26687. +
  26688. +#include <linux/netdevice.h> /* struct device, and other headers */
  26689. +#include <linux/etherdevice.h> /* eth_type_trans */
  26690. +#include <linux/ip.h> /* struct iphdr */
  26691. +#include <linux/skbuff.h>
  26692. +#include <openswan.h>
  26693. +#ifdef SPINLOCK
  26694. +# ifdef SPINLOCK_23
  26695. +# include <linux/spinlock.h> /* *lock* */
  26696. +# else /* SPINLOCK_23 */
  26697. +# include <asm/spinlock.h> /* *lock* */
  26698. +# endif /* SPINLOCK_23 */
  26699. +#endif /* SPINLOCK */
  26700. +
  26701. +#include <net/ip.h>
  26702. +#include <net/protocol.h>
  26703. +
  26704. +#include "openswan/radij.h"
  26705. +#include "openswan/ipsec_encap.h"
  26706. +#include "openswan/ipsec_sa.h"
  26707. +
  26708. +#include "openswan/ipsec_radij.h"
  26709. +#include "openswan/ipsec_xform.h"
  26710. +#include "openswan/ipsec_tunnel.h"
  26711. +#include "openswan/ipsec_rcv.h"
  26712. +#include "openswan/ipsec_xmit.h"
  26713. +
  26714. +#include "openswan/ipsec_auth.h"
  26715. +#include "openswan/ipsec_ah.h"
  26716. +#include "openswan/ipsec_proto.h"
  26717. +
  26718. +__u32 zeroes[AH_AMAX];
  26719. +
  26720. +enum ipsec_rcv_value
  26721. +ipsec_rcv_ah_checks(struct ipsec_rcv_state *irs,
  26722. + struct sk_buff *skb)
  26723. +{
  26724. + int ahminlen;
  26725. +
  26726. + ahminlen = irs->hard_header_len + sizeof(struct iphdr);
  26727. +
  26728. + /* take care not to deref this pointer until we check the minlen though */
  26729. + irs->protostuff.ahstuff.ahp = (struct ahhdr *)skb->h.raw;
  26730. +
  26731. + if((skb->len < ahminlen+sizeof(struct ahhdr)) ||
  26732. + (skb->len < ahminlen+(irs->protostuff.ahstuff.ahp->ah_hl << 2))) {
  26733. + KLIPS_PRINT(debug_rcv & DB_RX_INAU,
  26734. + "klips_debug:ipsec_rcv: "
  26735. + "runt ah packet of skb->len=%d received from %s, dropped.\n",
  26736. + skb->len,
  26737. + irs->ipsaddr_txt);
  26738. + if(irs->stats) {
  26739. + irs->stats->rx_errors++;
  26740. + }
  26741. + return IPSEC_RCV_BADLEN;
  26742. + }
  26743. +
  26744. + irs->said.spi = irs->protostuff.ahstuff.ahp->ah_spi;
  26745. +
  26746. + /* XXX we only support the one 12-byte authenticator for now */
  26747. + if(irs->protostuff.ahstuff.ahp->ah_hl != ((AHHMAC_HASHLEN+AHHMAC_RPLLEN) >> 2)) {
  26748. + KLIPS_PRINT(debug_rcv & DB_RX_INAU,
  26749. + "klips_debug:ipsec_rcv: "
  26750. + "bad authenticator length %ld, expected %lu from %s.\n",
  26751. + (long)(irs->protostuff.ahstuff.ahp->ah_hl << 2),
  26752. + (unsigned long) sizeof(struct ahhdr),
  26753. + irs->ipsaddr_txt);
  26754. + if(irs->stats) {
  26755. + irs->stats->rx_errors++;
  26756. + }
  26757. + return IPSEC_RCV_BADLEN;
  26758. + }
  26759. +
  26760. + return IPSEC_RCV_OK;
  26761. +}
  26762. +
  26763. +
  26764. +enum ipsec_rcv_value
  26765. +ipsec_rcv_ah_setup_auth(struct ipsec_rcv_state *irs,
  26766. + struct sk_buff *skb,
  26767. + __u32 *replay,
  26768. + unsigned char **authenticator)
  26769. +{
  26770. + struct ahhdr *ahp = irs->protostuff.ahstuff.ahp;
  26771. +
  26772. + *replay = ntohl(ahp->ah_rpl);
  26773. + *authenticator = ahp->ah_data;
  26774. +
  26775. + return IPSEC_RCV_OK;
  26776. +}
  26777. +
  26778. +enum ipsec_rcv_value
  26779. +ipsec_rcv_ah_authcalc(struct ipsec_rcv_state *irs,
  26780. + struct sk_buff *skb)
  26781. +{
  26782. + struct auth_alg *aa;
  26783. + struct ahhdr *ahp = irs->protostuff.ahstuff.ahp;
  26784. + union {
  26785. + MD5_CTX md5;
  26786. + SHA1_CTX sha1;
  26787. + } tctx;
  26788. + struct iphdr ipo;
  26789. + int ahhlen;
  26790. +
  26791. + aa = irs->authfuncs;
  26792. +
  26793. + /* copy the initialized keying material */
  26794. + memcpy(&tctx, irs->ictx, irs->ictx_len);
  26795. +
  26796. + ipo = *irs->ipp;
  26797. + ipo.tos = 0; /* mutable RFC 2402 3.3.3.1.1.1 */
  26798. + ipo.frag_off = 0;
  26799. + ipo.ttl = 0;
  26800. + ipo.check = 0;
  26801. +
  26802. +
  26803. + /* do the sanitized header */
  26804. + (*aa->update)((void*)&tctx, (caddr_t)&ipo, sizeof(struct iphdr));
  26805. +
  26806. + /* XXX we didn't do the options here! */
  26807. +
  26808. + /* now do the AH header itself */
  26809. + ahhlen = AH_BASIC_LEN + (ahp->ah_hl << 2);
  26810. + (*aa->update)((void*)&tctx, (caddr_t)ahp, ahhlen - AHHMAC_HASHLEN);
  26811. +
  26812. + /* now, do some zeroes */
  26813. + (*aa->update)((void*)&tctx, (caddr_t)zeroes, AHHMAC_HASHLEN);
  26814. +
  26815. + /* finally, do the packet contents themselves */
  26816. + (*aa->update)((void*)&tctx,
  26817. + (caddr_t)skb->h.raw + ahhlen,
  26818. + skb->len - ahhlen);
  26819. +
  26820. + (*aa->final)(irs->hash, (void *)&tctx);
  26821. +
  26822. + memcpy(&tctx, irs->octx, irs->octx_len);
  26823. +
  26824. + (*aa->update)((void *)&tctx, irs->hash, aa->hashlen);
  26825. + (*aa->final)(irs->hash, (void *)&tctx);
  26826. +
  26827. + return IPSEC_RCV_OK;
  26828. +}
  26829. +
  26830. +enum ipsec_rcv_value
  26831. +ipsec_rcv_ah_decap(struct ipsec_rcv_state *irs)
  26832. +{
  26833. + struct ahhdr *ahp = irs->protostuff.ahstuff.ahp;
  26834. + struct sk_buff *skb;
  26835. + int ahhlen;
  26836. +
  26837. + skb=irs->skb;
  26838. +
  26839. + ahhlen = AH_BASIC_LEN + (ahp->ah_hl << 2);
  26840. +
  26841. + irs->ipp->tot_len = htons(ntohs(irs->ipp->tot_len) - ahhlen);
  26842. + irs->next_header = ahp->ah_nh;
  26843. +
  26844. + /*
  26845. + * move the IP header forward by the size of the AH header, which
  26846. + * will remove the the AH header from the packet.
  26847. + */
  26848. + memmove((void *)(skb->nh.raw + ahhlen),
  26849. + (void *)(skb->nh.raw), irs->iphlen);
  26850. +
  26851. + ipsec_rcv_dmp("ah postmove", skb->data, skb->len);
  26852. +
  26853. + /* skb_pull below, will move up by ahhlen */
  26854. +
  26855. + /* XXX not clear how this can happen, as the message indicates */
  26856. + if(skb->len < ahhlen) {
  26857. + printk(KERN_WARNING
  26858. + "klips_error:ipsec_rcv: "
  26859. + "tried to skb_pull ahhlen=%d, %d available. This should never happen, please report.\n",
  26860. + ahhlen,
  26861. + (int)(skb->len));
  26862. + return IPSEC_RCV_DECAPFAIL;
  26863. + }
  26864. + skb_pull(skb, ahhlen);
  26865. +
  26866. + skb->nh.raw = skb->nh.raw + ahhlen;
  26867. + irs->ipp = skb->nh.iph;
  26868. +
  26869. + ipsec_rcv_dmp("ah postpull", (void *)skb->nh.iph, skb->len);
  26870. +
  26871. + return IPSEC_RCV_OK;
  26872. +}
  26873. +
  26874. +enum ipsec_xmit_value
  26875. +ipsec_xmit_ah_setup(struct ipsec_xmit_state *ixs)
  26876. +{
  26877. + struct iphdr ipo;
  26878. + struct ahhdr *ahp;
  26879. + __u8 hash[AH_AMAX];
  26880. + union {
  26881. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  26882. + MD5_CTX md5;
  26883. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  26884. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  26885. + SHA1_CTX sha1;
  26886. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  26887. + } tctx;
  26888. + unsigned char *dat = (unsigned char *)ixs->iph;
  26889. +
  26890. + ahp = (struct ahhdr *)(dat + ixs->iphlen);
  26891. + ahp->ah_spi = ixs->ipsp->ips_said.spi;
  26892. + ahp->ah_rpl = htonl(++(ixs->ipsp->ips_replaywin_lastseq));
  26893. + ahp->ah_rv = 0;
  26894. + ahp->ah_nh = ixs->iph->protocol;
  26895. + ahp->ah_hl = (sizeof(struct ahhdr) >> 2) - sizeof(__u64)/sizeof(__u32);
  26896. + ixs->iph->protocol = IPPROTO_AH;
  26897. + ipsec_xmit_dmp("ahp", (char*)ahp, sizeof(*ahp));
  26898. +
  26899. + ipo = *ixs->iph;
  26900. + ipo.tos = 0;
  26901. + ipo.frag_off = 0;
  26902. + ipo.ttl = 0;
  26903. + ipo.check = 0;
  26904. + ipsec_xmit_dmp("ipo", (char*)&ipo, sizeof(ipo));
  26905. +
  26906. + switch(ixs->ipsp->ips_authalg) {
  26907. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  26908. + case AH_MD5:
  26909. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  26910. + ipsec_xmit_dmp("ictx", (char*)&tctx.md5, sizeof(tctx.md5));
  26911. + osMD5Update(&tctx.md5, (unsigned char *)&ipo, sizeof (struct iphdr));
  26912. + ipsec_xmit_dmp("ictx+ipo", (char*)&tctx.md5, sizeof(tctx.md5));
  26913. + osMD5Update(&tctx.md5, (unsigned char *)ahp,
  26914. + sizeof(struct ahhdr) - sizeof(ahp->ah_data));
  26915. + ipsec_xmit_dmp("ictx+ahp", (char*)&tctx.md5, sizeof(tctx.md5));
  26916. + osMD5Update(&tctx.md5, (unsigned char *)zeroes, AHHMAC_HASHLEN);
  26917. + ipsec_xmit_dmp("ictx+zeroes", (char*)&tctx.md5, sizeof(tctx.md5));
  26918. + osMD5Update(&tctx.md5, dat + ixs->iphlen + sizeof(struct ahhdr),
  26919. + ixs->skb->len - ixs->iphlen - sizeof(struct ahhdr));
  26920. + ipsec_xmit_dmp("ictx+dat", (char*)&tctx.md5, sizeof(tctx.md5));
  26921. + osMD5Final(hash, &tctx.md5);
  26922. + ipsec_xmit_dmp("ictx hash", (char*)&hash, sizeof(hash));
  26923. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->octx;
  26924. + ipsec_xmit_dmp("octx", (char*)&tctx.md5, sizeof(tctx.md5));
  26925. + osMD5Update(&tctx.md5, hash, AHMD596_ALEN);
  26926. + ipsec_xmit_dmp("octx+hash", (char*)&tctx.md5, sizeof(tctx.md5));
  26927. + osMD5Final(hash, &tctx.md5);
  26928. + ipsec_xmit_dmp("octx hash", (char*)&hash, sizeof(hash));
  26929. +
  26930. + memcpy(ahp->ah_data, hash, AHHMAC_HASHLEN);
  26931. +
  26932. + /* paranoid */
  26933. + memset((caddr_t)&tctx.md5, 0, sizeof(tctx.md5));
  26934. + memset((caddr_t)hash, 0, sizeof(*hash));
  26935. + break;
  26936. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  26937. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  26938. + case AH_SHA:
  26939. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  26940. + SHA1Update(&tctx.sha1, (unsigned char *)&ipo, sizeof (struct iphdr));
  26941. + SHA1Update(&tctx.sha1, (unsigned char *)ahp, sizeof(struct ahhdr) - sizeof(ahp->ah_data));
  26942. + SHA1Update(&tctx.sha1, (unsigned char *)zeroes, AHHMAC_HASHLEN);
  26943. + SHA1Update(&tctx.sha1, dat + ixs->iphlen + sizeof(struct ahhdr),
  26944. + ixs->skb->len - ixs->iphlen - sizeof(struct ahhdr));
  26945. + SHA1Final(hash, &tctx.sha1);
  26946. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->octx;
  26947. + SHA1Update(&tctx.sha1, hash, AHSHA196_ALEN);
  26948. + SHA1Final(hash, &tctx.sha1);
  26949. +
  26950. + memcpy(ahp->ah_data, hash, AHHMAC_HASHLEN);
  26951. +
  26952. + /* paranoid */
  26953. + memset((caddr_t)&tctx.sha1, 0, sizeof(tctx.sha1));
  26954. + memset((caddr_t)hash, 0, sizeof(*hash));
  26955. + break;
  26956. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  26957. + default:
  26958. + ixs->stats->tx_errors++;
  26959. + return IPSEC_XMIT_AH_BADALG;
  26960. + }
  26961. +#ifdef NET_21
  26962. + ixs->skb->h.raw = (unsigned char*)ahp;
  26963. +#endif /* NET_21 */
  26964. +
  26965. + return IPSEC_XMIT_OK;
  26966. +}
  26967. +
  26968. +struct xform_functions ah_xform_funcs[]={
  26969. + { rcv_checks: ipsec_rcv_ah_checks,
  26970. + rcv_setup_auth: ipsec_rcv_ah_setup_auth,
  26971. + rcv_calc_auth: ipsec_rcv_ah_authcalc,
  26972. + rcv_decrypt: ipsec_rcv_ah_decap,
  26973. +
  26974. + xmit_setup: ipsec_xmit_ah_setup,
  26975. + xmit_headroom: sizeof(struct ahhdr),
  26976. + xmit_needtailroom: 0,
  26977. + },
  26978. +};
  26979. +
  26980. +
  26981. +#ifdef NET_26
  26982. +struct inet_protocol ah_protocol = {
  26983. + .handler = ipsec_rcv,
  26984. + .no_policy = 1,
  26985. +};
  26986. +#else
  26987. +struct inet_protocol ah_protocol =
  26988. +{
  26989. + ipsec_rcv, /* AH handler */
  26990. + NULL, /* TUNNEL error control */
  26991. +#ifdef NETDEV_25
  26992. + 1, /* no policy */
  26993. +#else
  26994. + 0, /* next */
  26995. + IPPROTO_AH, /* protocol ID */
  26996. + 0, /* copy */
  26997. + NULL, /* data */
  26998. + "AH" /* name */
  26999. +#endif
  27000. +};
  27001. +#endif /* NET_26 */
  27002. +
  27003. +/*
  27004. + * $Log: ipsec_ah.c,v $
  27005. + * Revision 1.12.2.2 2006/10/06 21:39:26 paul
  27006. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  27007. + * set. This is defined through autoconf.h which is included through the
  27008. + * linux kernel build macros.
  27009. + *
  27010. + * Revision 1.12.2.1 2006/02/15 05:35:14 paul
  27011. + * Patch by David McCullough <davidm@snapgear.com>
  27012. + * If you setup a tunnel without ESP it doesn't work. It used to work in
  27013. + * an older openswan version but stopped when klips was modified to deal
  27014. + * with the pulled IP header on the received SKB's.
  27015. + *
  27016. + * The code in ipsec_ah.c still thinks the IP header is there and runs the
  27017. + * hash on the incorrect data.
  27018. + *
  27019. + * Revision 1.12 2005/04/29 05:10:22 mcr
  27020. + * removed from extraenous includes to make unit testing easier.
  27021. + *
  27022. + * Revision 1.11 2005/04/15 19:50:55 mcr
  27023. + * adjustments to use proper skb fields for data.
  27024. + *
  27025. + * Revision 1.10 2004/09/14 00:22:57 mcr
  27026. + * adjustment of MD5* functions.
  27027. + *
  27028. + * Revision 1.9 2004/09/13 02:22:47 mcr
  27029. + * #define inet_protocol if necessary.
  27030. + *
  27031. + * Revision 1.8 2004/09/06 18:35:48 mcr
  27032. + * 2.6.8.1 gets rid of inet_protocol->net_protocol compatibility,
  27033. + * so adjust for that.
  27034. + *
  27035. + * Revision 1.7 2004/08/22 05:00:48 mcr
  27036. + * if we choose to compile the file, we want the contents,
  27037. + * so don't pull any punches.
  27038. + *
  27039. + * Revision 1.6 2004/08/17 03:27:23 mcr
  27040. + * klips 2.6 edits.
  27041. + *
  27042. + * Revision 1.5 2004/08/14 03:28:24 mcr
  27043. + * fixed log comment to remove warning about embedded comment.
  27044. + *
  27045. + * Revision 1.4 2004/08/04 15:57:07 mcr
  27046. + * moved des .h files to include/des/ *
  27047. + * included 2.6 protocol specific things
  27048. + * started at NAT-T support, but it will require a kernel patch.
  27049. + *
  27050. + * Revision 1.3 2004/07/10 19:11:18 mcr
  27051. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  27052. + *
  27053. + * Revision 1.2 2004/04/06 02:49:25 mcr
  27054. + * pullup of algo code from alg-branch.
  27055. + *
  27056. + *
  27057. + *
  27058. + */
  27059. --- /dev/null Tue Mar 11 13:02:56 2003
  27060. +++ linux/net/ipsec/ipsec_alg.c Mon Feb 9 13:51:03 2004
  27061. @@ -0,0 +1,1057 @@
  27062. +/*
  27063. + * Modular extensions service and registration functions
  27064. + *
  27065. + * Author: JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  27066. + *
  27067. + * Version: 0.8.1
  27068. + *
  27069. + * ipsec_alg.c,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
  27070. + *
  27071. + * This program is free software; you can redistribute it and/or modify it
  27072. + * under the terms of the GNU General Public License as published by the
  27073. + * Free Software Foundation; either version 2 of the License, or (at your
  27074. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  27075. + *
  27076. + * This program is distributed in the hope that it will be useful, but
  27077. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  27078. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  27079. + * for more details.
  27080. + *
  27081. + */
  27082. +#define __NO_VERSION__
  27083. +
  27084. +#if defined (MODULE)
  27085. +#include <linux/module.h>
  27086. +#endif
  27087. +
  27088. +#include <linux/kernel.h> /* printk() */
  27089. +
  27090. +#include <linux/netdevice.h> /* struct device, and other headers */
  27091. +#include <linux/etherdevice.h> /* eth_type_trans */
  27092. +#include <linux/ip.h> /* struct iphdr */
  27093. +#include <linux/skbuff.h>
  27094. +#include <linux/socket.h>
  27095. +#include <linux/in.h>
  27096. +#include <linux/types.h>
  27097. +#include <linux/string.h> /* memcmp() */
  27098. +#include <linux/random.h> /* get_random_bytes() */
  27099. +#include <linux/errno.h> /* error codes */
  27100. +#ifdef SPINLOCK
  27101. +# ifdef SPINLOCK_23
  27102. +# include <linux/spinlock.h> /* *lock* */
  27103. +# else /* SPINLOCK_23 */
  27104. +# include <asm/spinlock.h> /* *lock* */
  27105. +# endif /* SPINLOCK_23 */
  27106. +#endif /* SPINLOCK */
  27107. +
  27108. +#include "openswan/ipsec_param.h"
  27109. +#include <openswan.h>
  27110. +#include "openswan/ipsec_sa.h"
  27111. +#include "openswan/radij.h"
  27112. +#include "openswan/ipsec_encap.h"
  27113. +#include "openswan/ipsec_radij.h"
  27114. +#include "openswan/ipsec_xform.h"
  27115. +#include "openswan/ipsec_tunnel.h"
  27116. +#include "openswan/ipsec_rcv.h"
  27117. +#if defined(CONFIG_KLIPS_ESP) || defined(CONFIG_KLIPS_AH)
  27118. +# include "openswan/ipsec_ah.h"
  27119. +#endif /* defined(CONFIG_KLIPS_ESP) || defined(CONFIG_KLIPS_AH) */
  27120. +#ifdef CONFIG_KLIPS_ESP
  27121. +# include "openswan/ipsec_esp.h"
  27122. +#endif /* !CONFIG_KLIPS_ESP */
  27123. +#ifdef CONFIG_KLIPS_IPCOMP
  27124. +# include "openswan/ipcomp.h"
  27125. +#endif /* CONFIG_KLIPS_COMP */
  27126. +
  27127. +#include <pfkeyv2.h>
  27128. +#include <pfkey.h>
  27129. +
  27130. +#include "openswan/ipsec_alg.h"
  27131. +#include "openswan/ipsec_proto.h"
  27132. +
  27133. +#if SADB_EALG_MAX < 255
  27134. +#warning Compiling with limited ESP support ( SADB_EALG_MAX < 256 )
  27135. +#endif
  27136. +
  27137. +static rwlock_t ipsec_alg_lock = RW_LOCK_UNLOCKED;
  27138. +#define IPSEC_ALG_HASHSZ 16 /* must be power of 2, even 2^0=1 */
  27139. +static struct list_head ipsec_alg_hash_table[IPSEC_ALG_HASHSZ];
  27140. +
  27141. +/* Old gcc's will fail here */
  27142. +#define barf_out(fmt, args...) do { struct ipsec_alg *ixtc = (struct ipsec_alg *)ixt; printk(KERN_ERR "%s: (%s) " fmt, __FUNCTION__, ixtc->ixt_name , ## args) \
  27143. + ; goto out; } while(0)
  27144. +
  27145. +#ifdef NET_26
  27146. +/*
  27147. + * Must be already protected by lock
  27148. + */
  27149. +static void __ipsec_alg_usage_inc(struct ipsec_alg *ixt)
  27150. +{
  27151. +#ifdef MODULE
  27152. + if (ixt->ixt_module)
  27153. + try_module_get(ixt->ixt_module);
  27154. +#endif
  27155. + atomic_inc(&ixt->ixt_refcnt);
  27156. +}
  27157. +static void __ipsec_alg_usage_dec(struct ipsec_alg *ixt) {
  27158. + atomic_dec(&ixt->ixt_refcnt);
  27159. +#ifdef MODULE
  27160. + if (ixt->ixt_module)
  27161. + module_put(ixt->ixt_module);
  27162. +#endif
  27163. +}
  27164. +
  27165. +#else
  27166. +
  27167. +/*
  27168. + * Must be already protected by lock
  27169. + */
  27170. +static void __ipsec_alg_usage_inc(struct ipsec_alg *ixt) {
  27171. +#ifdef MODULE
  27172. + if (ixt->ixt_module) {
  27173. + __MOD_INC_USE_COUNT(ixt->ixt_module);
  27174. + }
  27175. +#endif
  27176. + atomic_inc(&ixt->ixt_refcnt);
  27177. +}
  27178. +static void __ipsec_alg_usage_dec(struct ipsec_alg *ixt) {
  27179. + atomic_dec(&ixt->ixt_refcnt);
  27180. +#ifdef MODULE
  27181. + if (ixt->ixt_module)
  27182. + __MOD_DEC_USE_COUNT(ixt->ixt_module);
  27183. +#endif
  27184. +}
  27185. +#endif
  27186. +
  27187. +/*
  27188. + * simple hash function, optimized for 0-hash (1 list) special
  27189. + * case
  27190. + */
  27191. +#if IPSEC_ALG_HASHSZ > 1
  27192. +static inline unsigned ipsec_alg_hashfn(int alg_type, int alg_id) {
  27193. + return ((alg_type^alg_id)&(IPSEC_ALG_HASHSZ-1));
  27194. +}
  27195. +#else
  27196. +#define ipsec_alg_hashfn(x,y) (0)
  27197. +#endif
  27198. +
  27199. +/*****************************************************************
  27200. + *
  27201. + * INTERNAL table handling: insert, delete, find
  27202. + *
  27203. + *****************************************************************/
  27204. +
  27205. +/*
  27206. + * hash table initialization, called from ipsec_alg_init()
  27207. + */
  27208. +static void ipsec_alg_hash_init(void) {
  27209. + struct list_head *head = ipsec_alg_hash_table;
  27210. + int i = IPSEC_ALG_HASHSZ;
  27211. + do {
  27212. + INIT_LIST_HEAD(head);
  27213. + head++;
  27214. + i--;
  27215. + } while (i);
  27216. +}
  27217. +/*
  27218. + * hash list lookup by {alg_type, alg_id} and table head,
  27219. + * must be already protected by lock
  27220. + */
  27221. +static struct ipsec_alg *__ipsec_alg_find(unsigned alg_type, unsigned alg_id, struct list_head * head) {
  27222. + struct list_head *p;
  27223. + struct ipsec_alg *ixt=NULL;
  27224. + for (p=head->next; p!=head; p=p->next) {
  27225. + ixt = list_entry(p, struct ipsec_alg, ixt_list);
  27226. + if (ixt->ixt_alg_type == alg_type && ixt->ixt_alg_id==alg_id) {
  27227. + goto out;
  27228. + }
  27229. + }
  27230. + ixt=NULL;
  27231. +out:
  27232. + return ixt;
  27233. +}
  27234. +/*
  27235. + * inserts (in front) a new entry in hash table,
  27236. + * called from ipsec_alg_register() when new algorithm is registered.
  27237. + */
  27238. +static int ipsec_alg_insert(struct ipsec_alg *ixt) {
  27239. + int ret=-EINVAL;
  27240. + unsigned hashval=ipsec_alg_hashfn(ixt->ixt_alg_type, ixt->ixt_alg_id);
  27241. + struct list_head *head= ipsec_alg_hash_table + hashval;
  27242. + struct ipsec_alg *ixt_cur;
  27243. +
  27244. + /* new element must be virgin ... */
  27245. + if (ixt->ixt_list.next != &ixt->ixt_list ||
  27246. + ixt->ixt_list.prev != &ixt->ixt_list) {
  27247. + printk(KERN_ERR "ipsec_alg_insert: ixt object \"%s\" "
  27248. + "list head not initialized\n",
  27249. + ixt->ixt_name);
  27250. + return ret;
  27251. + }
  27252. + write_lock_bh(&ipsec_alg_lock);
  27253. +
  27254. + ixt_cur = __ipsec_alg_find(ixt->ixt_alg_type, ixt->ixt_alg_id, head);
  27255. +
  27256. + /* if previous (current) ipsec_alg found check excl flag of _anyone_ */
  27257. + if (ixt_cur
  27258. + && ((ixt->ixt_state|ixt_cur->ixt_state) & IPSEC_ALG_ST_EXCL)) {
  27259. + barf_out("ipsec_alg for alg_type=%d, alg_id=%d already exist. "
  27260. + "Not loaded (ret=%d).\n",
  27261. + ixt->ixt_alg_type,
  27262. + ixt->ixt_alg_id, ret=-EEXIST);
  27263. + }
  27264. + list_add(&ixt->ixt_list, head);
  27265. + ixt->ixt_state |= IPSEC_ALG_ST_REGISTERED;
  27266. + ret=0;
  27267. +out:
  27268. + write_unlock_bh(&ipsec_alg_lock);
  27269. + return ret;
  27270. +}
  27271. +
  27272. +/*
  27273. + * deletes an existing entry in hash table,
  27274. + * called from ipsec_alg_unregister() when algorithm is unregistered.
  27275. + */
  27276. +static int ipsec_alg_delete(struct ipsec_alg *ixt) {
  27277. + write_lock_bh(&ipsec_alg_lock);
  27278. + list_del(&ixt->ixt_list);
  27279. + write_unlock_bh(&ipsec_alg_lock);
  27280. + return 0;
  27281. +}
  27282. +
  27283. +/*
  27284. + * here @user context (read-only when @kernel bh context)
  27285. + * -> no bh disabling
  27286. + *
  27287. + * called from ipsec_sa_init() -> ipsec_alg_sa_init()
  27288. + */
  27289. +static struct ipsec_alg *ipsec_alg_get(int alg_type, int alg_id)
  27290. +{
  27291. + unsigned hashval=ipsec_alg_hashfn(alg_type, alg_id);
  27292. + struct list_head *head= ipsec_alg_hash_table + hashval;
  27293. + struct ipsec_alg *ixt;
  27294. +
  27295. + read_lock(&ipsec_alg_lock);
  27296. + ixt=__ipsec_alg_find(alg_type, alg_id, head);
  27297. + if (ixt) __ipsec_alg_usage_inc(ixt);
  27298. + read_unlock(&ipsec_alg_lock);
  27299. +
  27300. + return ixt;
  27301. +}
  27302. +
  27303. +static void ipsec_alg_put(struct ipsec_alg *ixt) {
  27304. + __ipsec_alg_usage_dec((struct ipsec_alg *)ixt);
  27305. +}
  27306. +
  27307. +/*****************************************************************
  27308. + *
  27309. + * INTERFACE for ENC services: key creation, encrypt function
  27310. + *
  27311. + *****************************************************************/
  27312. +
  27313. +/*
  27314. + * main encrypt service entry point
  27315. + * called from ipsec_rcv() with encrypt=IPSEC_ALG_DECRYPT and
  27316. + * ipsec_tunnel_start_xmit with encrypt=IPSEC_ALG_ENCRYPT
  27317. + */
  27318. +int ipsec_alg_esp_encrypt(struct ipsec_sa *sa_p, __u8 * idat,
  27319. + int ilen, const __u8 * iv, int encrypt)
  27320. +{
  27321. + int ret;
  27322. + struct ipsec_alg_enc *ixt_e=sa_p->ips_alg_enc;
  27323. +#ifdef CONFIG_KLIPS_DEBUG
  27324. + int debug_flag = (encrypt==IPSEC_ALG_ENCRYPT ?
  27325. + debug_tunnel : debug_rcv);
  27326. +#endif
  27327. +
  27328. + KLIPS_PRINT(debug_flag,
  27329. + "klips_debug:ipsec_alg_esp_encrypt: "
  27330. + "entering with encalg=%d, ixt_e=%p\n",
  27331. + sa_p->ips_encalg, ixt_e);
  27332. + if (ixt_e == NULL) {
  27333. +#ifdef CONFIG_KLIPS_DEBUG
  27334. + KLIPS_ERROR(debug_flag,
  27335. + "klips_debug:ipsec_alg_esp_encrypt: "
  27336. + "NULL ipsec_alg_enc object\n");
  27337. +#endif
  27338. + return -1;
  27339. + }
  27340. + KLIPS_PRINT(debug_flag,
  27341. + "klips_debug:ipsec_alg_esp_encrypt: "
  27342. + "calling cbc_encrypt encalg=%d "
  27343. + "ips_key_e=%p idat=%p ilen=%d iv=%p, encrypt=%d\n",
  27344. + sa_p->ips_encalg,
  27345. + sa_p->ips_key_e, idat, ilen, iv, encrypt);
  27346. + ret=ixt_e->ixt_e_cbc_encrypt(ixt_e, sa_p->ips_key_e, idat,
  27347. + ilen, iv, encrypt);
  27348. + KLIPS_PRINT(debug_flag,
  27349. + "klips_debug:ipsec_alg_esp_encrypt: "
  27350. + "returned ret=%d\n",
  27351. + ret);
  27352. + return ret;
  27353. +}
  27354. +
  27355. +/*
  27356. + * encryption key context creation function
  27357. + * called from pfkey_v2_parser.c:pfkey_ips_init()
  27358. + */
  27359. +int ipsec_alg_enc_key_create(struct ipsec_sa *sa_p) {
  27360. + int ret=-EINVAL;
  27361. + int keyminbits, keymaxbits;
  27362. + caddr_t ekp;
  27363. + struct ipsec_alg_enc *ixt_e=sa_p->ips_alg_enc;
  27364. +
  27365. + KLIPS_PRINT(debug_pfkey,
  27366. + "klips_debug:ipsec_alg_enc_key_create: "
  27367. + "entering with encalg=%d ixt_e=%p\n",
  27368. + sa_p->ips_encalg, ixt_e);
  27369. + if (!ixt_e) {
  27370. + KLIPS_PRINT(debug_pfkey,
  27371. + "klips_debug:ipsec_alg_enc_key_create: "
  27372. + "NULL ipsec_alg_enc object\n");
  27373. + return -EPROTO;
  27374. + }
  27375. +
  27376. + /*
  27377. + * grRRR... DES 7bits jurassic stuff ... f*ckk --jjo
  27378. + */
  27379. + switch(ixt_e->ixt_common.ixt_support.ias_id) {
  27380. + case ESP_3DES:
  27381. + keyminbits=keymaxbits=192;break;
  27382. + case ESP_DES:
  27383. + keyminbits=keymaxbits=64;break;
  27384. + default:
  27385. + keyminbits=ixt_e->ixt_common.ixt_support.ias_keyminbits;
  27386. + keymaxbits=ixt_e->ixt_common.ixt_support.ias_keymaxbits;
  27387. + }
  27388. + if(sa_p->ips_key_bits_e<keyminbits ||
  27389. + sa_p->ips_key_bits_e>keymaxbits) {
  27390. + KLIPS_PRINT(debug_pfkey,
  27391. + "klips_debug:ipsec_alg_enc_key_create: "
  27392. + "incorrect encryption key size for id=%d: %d bits -- "
  27393. + "must be between %d,%d bits\n" /*octets (bytes)\n"*/,
  27394. + ixt_e->ixt_common.ixt_support.ias_id,
  27395. + sa_p->ips_key_bits_e, keyminbits, keymaxbits);
  27396. + ret=-EINVAL;
  27397. + goto ixt_out;
  27398. + }
  27399. + /* save encryption key pointer */
  27400. + ekp = sa_p->ips_key_e;
  27401. +
  27402. +
  27403. + if (ixt_e->ixt_e_new_key) {
  27404. + sa_p->ips_key_e = ixt_e->ixt_e_new_key(ixt_e,
  27405. + ekp, sa_p->ips_key_bits_e/8);
  27406. + ret = (sa_p->ips_key_e)? 0 : -EINVAL;
  27407. + } else {
  27408. + if((sa_p->ips_key_e = (caddr_t)
  27409. + kmalloc((sa_p->ips_key_e_size = ixt_e->ixt_e_ctx_size),
  27410. + GFP_ATOMIC)) == NULL) {
  27411. + ret=-ENOMEM;
  27412. + goto ixt_out;
  27413. + }
  27414. + /* zero-out key_e */
  27415. + memset(sa_p->ips_key_e, 0, sa_p->ips_key_e_size);
  27416. +
  27417. + /* I cast here to allow more decoupling in alg module */
  27418. + KLIPS_PRINT(debug_pfkey,
  27419. + "klips_debug:ipsec_alg_enc_key_create: about to call:"
  27420. + "set_key(key_e=%p, ekp=%p, key_size=%d)\n",
  27421. + (caddr_t)sa_p->ips_key_e, ekp, sa_p->ips_key_bits_e/8);
  27422. + ret = ixt_e->ixt_e_set_key(ixt_e, (caddr_t)sa_p->ips_key_e, ekp, sa_p->ips_key_bits_e/8);
  27423. + }
  27424. + /* paranoid */
  27425. + memset(ekp, 0, sa_p->ips_key_bits_e/8);
  27426. + kfree(ekp);
  27427. +ixt_out:
  27428. + return ret;
  27429. +}
  27430. +
  27431. +/***************************************************************
  27432. + *
  27433. + * INTERFACE for AUTH services: key creation, hash functions
  27434. + *
  27435. + ***************************************************************/
  27436. +
  27437. +/*
  27438. + * auth key context creation function
  27439. + * called from pfkey_v2_parser.c:pfkey_ips_init()
  27440. + */
  27441. +int ipsec_alg_auth_key_create(struct ipsec_sa *sa_p) {
  27442. + int ret=-EINVAL;
  27443. + struct ipsec_alg_auth *ixt_a=sa_p->ips_alg_auth;
  27444. + int keyminbits, keymaxbits;
  27445. + unsigned char *akp;
  27446. + unsigned int aks;
  27447. + KLIPS_PRINT(debug_pfkey,
  27448. + "klips_debug:ipsec_alg_auth_key_create: "
  27449. + "entering with authalg=%d ixt_a=%p\n",
  27450. + sa_p->ips_authalg, ixt_a);
  27451. + if (!ixt_a) {
  27452. + KLIPS_PRINT(debug_pfkey,
  27453. + "klips_debug:ipsec_alg_auth_key_create: "
  27454. + "NULL ipsec_alg_auth object\n");
  27455. + return -EPROTO;
  27456. + }
  27457. + keyminbits=ixt_a->ixt_common.ixt_support.ias_keyminbits;
  27458. + keymaxbits=ixt_a->ixt_common.ixt_support.ias_keymaxbits;
  27459. + if(sa_p->ips_key_bits_a<keyminbits || sa_p->ips_key_bits_a>keymaxbits) {
  27460. + KLIPS_PRINT(debug_pfkey,
  27461. + "klips_debug:ipsec_alg_auth_key_create: incorrect auth"
  27462. + "key size: %d bits -- must be between %d,%d bits\n"/*octets (bytes)\n"*/,
  27463. + sa_p->ips_key_bits_a, keyminbits, keymaxbits);
  27464. + ret=-EINVAL;
  27465. + goto ixt_out;
  27466. + }
  27467. + /* save auth key pointer */
  27468. + sa_p->ips_auth_bits = ixt_a->ixt_a_keylen * 8; /* XXX XXX */
  27469. + akp = sa_p->ips_key_a;
  27470. + aks = sa_p->ips_key_a_size;
  27471. +
  27472. + /* will hold: 2 ctx and a blocksize buffer: kb */
  27473. + sa_p->ips_key_a_size = ixt_a->ixt_a_ctx_size;
  27474. + if((sa_p->ips_key_a =
  27475. + (caddr_t) kmalloc(sa_p->ips_key_a_size, GFP_ATOMIC)) == NULL) {
  27476. + ret=-ENOMEM;
  27477. + goto ixt_out;
  27478. + }
  27479. + ixt_a->ixt_a_hmac_set_key(ixt_a, sa_p->ips_key_a, akp, sa_p->ips_key_bits_a/8); /* XXX XXX */
  27480. + ret=0;
  27481. + memset(akp, 0, aks);
  27482. + kfree(akp);
  27483. +
  27484. +ixt_out:
  27485. + return ret;
  27486. +}
  27487. +
  27488. +
  27489. +int ipsec_alg_sa_esp_hash(const struct ipsec_sa *sa_p, const __u8 *espp,
  27490. + int len, __u8 *hash, int hashlen)
  27491. +{
  27492. + struct ipsec_alg_auth *ixt_a=sa_p->ips_alg_auth;
  27493. + if (!ixt_a) {
  27494. + KLIPS_PRINT(debug_pfkey,
  27495. + "klips_debug:ipsec_sa_esp_hash: "
  27496. + "NULL ipsec_alg_auth object\n");
  27497. + return -EPROTO;
  27498. + }
  27499. + KLIPS_PRINT(debug_tunnel|debug_rcv,
  27500. + "klips_debug:ipsec_sa_esp_hash: "
  27501. + "hashing %p (%d bytes) to %p (%d bytes)\n",
  27502. + espp, len,
  27503. + hash, hashlen);
  27504. + ixt_a->ixt_a_hmac_hash(ixt_a,
  27505. + sa_p->ips_key_a,
  27506. + espp, len,
  27507. + hash, hashlen);
  27508. + return 0;
  27509. +}
  27510. +
  27511. +/***************************************************************
  27512. + *
  27513. + * INTERFACE for module loading,testing, and unloading
  27514. + *
  27515. + ***************************************************************/
  27516. +
  27517. +/* validation for registering (enc) module */
  27518. +static int check_enc(struct ipsec_alg_enc *ixt)
  27519. +{
  27520. + int ret=-EINVAL;
  27521. + if (ixt->ixt_common.ixt_blocksize==0) /* || ixt->ixt_common.ixt_blocksize%2) need for ESP_NULL */
  27522. + barf_out(KERN_ERR "invalid blocksize=%d\n", ixt->ixt_common.ixt_blocksize);
  27523. + if (ixt->ixt_common.ixt_support.ias_keyminbits==0
  27524. + && ixt->ixt_common.ixt_support.ias_keymaxbits==0
  27525. + && ixt->ixt_e_keylen==0)
  27526. + goto zero_key_ok;
  27527. +
  27528. + if (ixt->ixt_common.ixt_support.ias_keyminbits==0)
  27529. + barf_out(KERN_ERR "invalid keyminbits=%d\n", ixt->ixt_common.ixt_support.ias_keyminbits);
  27530. +
  27531. + if (ixt->ixt_common.ixt_support.ias_keymaxbits==0)
  27532. + barf_out(KERN_ERR "invalid keymaxbits=%d\n", ixt->ixt_common.ixt_support.ias_keymaxbits);
  27533. +
  27534. + if (ixt->ixt_e_keylen==0)
  27535. + barf_out(KERN_ERR "invalid keysize=%d\n", ixt->ixt_e_keylen);
  27536. +
  27537. +zero_key_ok:
  27538. + if (ixt->ixt_e_ctx_size==0 && ixt->ixt_e_new_key == NULL)
  27539. + barf_out(KERN_ERR "invalid key_e_size=%d and ixt_e_new_key=NULL\n", ixt->ixt_e_ctx_size);
  27540. + if (ixt->ixt_e_cbc_encrypt==NULL)
  27541. + barf_out(KERN_ERR "e_cbc_encrypt() must be not NULL\n");
  27542. + ret=0;
  27543. +out:
  27544. + return ret;
  27545. +}
  27546. +
  27547. +/* validation for registering (auth) module */
  27548. +static int check_auth(struct ipsec_alg_auth *ixt)
  27549. +{
  27550. + int ret=-EINVAL;
  27551. + if (ixt->ixt_common.ixt_support.ias_id==0 || ixt->ixt_common.ixt_support.ias_id > SADB_AALG_MAX)
  27552. + barf_out("invalid alg_id=%d > %d (SADB_AALG_MAX)\n",
  27553. + ixt->ixt_common.ixt_support.ias_id, SADB_AALG_MAX);
  27554. +
  27555. + if (ixt->ixt_common.ixt_blocksize==0
  27556. + || ixt->ixt_common.ixt_blocksize%2)
  27557. + barf_out(KERN_ERR "invalid blocksize=%d\n",
  27558. + ixt->ixt_common.ixt_blocksize);
  27559. +
  27560. + if (ixt->ixt_common.ixt_blocksize>AH_BLKLEN_MAX)
  27561. + barf_out(KERN_ERR "sorry blocksize=%d > %d. "
  27562. + "Please increase AH_BLKLEN_MAX and recompile\n",
  27563. + ixt->ixt_common.ixt_blocksize,
  27564. + AH_BLKLEN_MAX);
  27565. + if (ixt->ixt_common.ixt_support.ias_keyminbits==0 && ixt->ixt_common.ixt_support.ias_keymaxbits==0 && ixt->ixt_a_keylen==0)
  27566. + goto zero_key_ok;
  27567. + if (ixt->ixt_common.ixt_support.ias_keyminbits==0)
  27568. + barf_out(KERN_ERR "invalid keyminbits=%d\n", ixt->ixt_common.ixt_support.ias_keyminbits);
  27569. + if (ixt->ixt_common.ixt_support.ias_keymaxbits==0)
  27570. + barf_out(KERN_ERR "invalid keymaxbits=%d\n", ixt->ixt_common.ixt_support.ias_keymaxbits);
  27571. + if (ixt->ixt_common.ixt_support.ias_keymaxbits!=ixt->ixt_common.ixt_support.ias_keyminbits)
  27572. + barf_out(KERN_ERR "keymaxbits must equal keyminbits (not sure).\n");
  27573. + if (ixt->ixt_a_keylen==0)
  27574. + barf_out(KERN_ERR "invalid keysize=%d\n", ixt->ixt_a_keylen);
  27575. +zero_key_ok:
  27576. + if (ixt->ixt_a_ctx_size==0)
  27577. + barf_out(KERN_ERR "invalid a_ctx_size=%d\n", ixt->ixt_a_ctx_size);
  27578. + if (ixt->ixt_a_hmac_set_key==NULL)
  27579. + barf_out(KERN_ERR "a_hmac_set_key() must be not NULL\n");
  27580. + if (ixt->ixt_a_hmac_hash==NULL)
  27581. + barf_out(KERN_ERR "a_hmac_hash() must be not NULL\n");
  27582. + ret=0;
  27583. +out:
  27584. + return ret;
  27585. +}
  27586. +
  27587. +/*
  27588. + * Generic (enc, auth) registration entry point
  27589. + */
  27590. +int register_ipsec_alg(struct ipsec_alg *ixt)
  27591. +{
  27592. + int ret=-EINVAL;
  27593. + /* Validation */
  27594. + if (ixt==NULL)
  27595. + barf_out("NULL ipsec_alg object passed\n");
  27596. + if ((ixt->ixt_version&0xffffff00) != (IPSEC_ALG_VERSION&0xffffff00))
  27597. + barf_out("incorrect version: %d.%d.%d-%d, "
  27598. + "must be %d.%d.%d[-%d]\n",
  27599. + IPSEC_ALG_VERSION_QUAD(ixt->ixt_version),
  27600. + IPSEC_ALG_VERSION_QUAD(IPSEC_ALG_VERSION));
  27601. +
  27602. + switch(ixt->ixt_alg_type) {
  27603. + case IPSEC_ALG_TYPE_AUTH:
  27604. + if ((ret=check_auth((struct ipsec_alg_auth *)ixt)<0))
  27605. + goto out;
  27606. + break;
  27607. + case IPSEC_ALG_TYPE_ENCRYPT:
  27608. + if ((ret=check_enc((struct ipsec_alg_enc *)ixt)<0))
  27609. + goto out;
  27610. + /*
  27611. + * Adapted two lines below:
  27612. + * ivlen == 0 is possible (NULL enc has blocksize==1)
  27613. + *
  27614. + * fixed NULL support by David De Reu <DeReu@tComLabs.com>
  27615. + */
  27616. + if (ixt->ixt_support.ias_ivlen == 0
  27617. + && ixt->ixt_blocksize > 1) {
  27618. + ixt->ixt_support.ias_ivlen = ixt->ixt_blocksize*8;
  27619. + }
  27620. + break;
  27621. + default:
  27622. + barf_out("alg_type=%d not supported\n", ixt->ixt_alg_type);
  27623. + }
  27624. + INIT_LIST_HEAD(&ixt->ixt_list);
  27625. + ret = ipsec_alg_insert(ixt);
  27626. + if (ret<0)
  27627. + barf_out(KERN_WARNING "ipsec_alg for alg_id=%d failed."
  27628. + "Not loaded (ret=%d).\n",
  27629. + ixt->ixt_support.ias_id, ret);
  27630. +
  27631. +
  27632. + ret = pfkey_list_insert_supported((struct ipsec_alg_supported *)&ixt->ixt_support
  27633. + , &(pfkey_supported_list[SADB_SATYPE_ESP]));
  27634. +
  27635. + if (ret==0) {
  27636. + ixt->ixt_state |= IPSEC_ALG_ST_SUPP;
  27637. + /* send register event to userspace */
  27638. + pfkey_register_reply(SADB_SATYPE_ESP, NULL);
  27639. + } else
  27640. + printk(KERN_ERR "pfkey_list_insert_supported returned %d. "
  27641. + "Loading anyway.\n", ret);
  27642. + ret=0;
  27643. +out:
  27644. + return ret;
  27645. +}
  27646. +
  27647. +/*
  27648. + * unregister ipsec_alg object from own tables, if
  27649. + * success => calls pfkey_list_remove_supported()
  27650. + */
  27651. +int unregister_ipsec_alg(struct ipsec_alg *ixt) {
  27652. + int ret= -EINVAL;
  27653. + switch(ixt->ixt_alg_type) {
  27654. + case IPSEC_ALG_TYPE_AUTH:
  27655. + case IPSEC_ALG_TYPE_ENCRYPT:
  27656. + break;
  27657. + default:
  27658. + /* this is not a typo :) */
  27659. + barf_out("frog found in list (\"%s\"): ixt_p=NULL\n",
  27660. + ixt->ixt_name);
  27661. + }
  27662. +
  27663. + ret=ipsec_alg_delete(ixt);
  27664. + if (ixt->ixt_state&IPSEC_ALG_ST_SUPP) {
  27665. + ixt->ixt_state &= ~IPSEC_ALG_ST_SUPP;
  27666. + pfkey_list_remove_supported((struct ipsec_alg_supported *)&ixt->ixt_support
  27667. + , &(pfkey_supported_list[SADB_SATYPE_ESP]));
  27668. +
  27669. + /* send register event to userspace */
  27670. + pfkey_register_reply(SADB_SATYPE_ESP, NULL);
  27671. + }
  27672. +
  27673. +out:
  27674. + return ret;
  27675. +}
  27676. +
  27677. +/*
  27678. + * Must be called from user context
  27679. + * used at module load type for testing algo implementation
  27680. + */
  27681. +static int ipsec_alg_test_encrypt(int enc_alg, int test) {
  27682. + int ret;
  27683. + caddr_t buf = NULL;
  27684. + int iv_size, keysize, key_e_size;
  27685. + struct ipsec_alg_enc *ixt_e;
  27686. + void *tmp_key_e = NULL;
  27687. + #define BUFSZ 1024
  27688. + #define MARGIN 0
  27689. + #define test_enc (buf+MARGIN)
  27690. + #define test_dec (test_enc+BUFSZ+MARGIN)
  27691. + #define test_tmp (test_dec+BUFSZ+MARGIN)
  27692. + #define test_key_e (test_tmp+BUFSZ+MARGIN)
  27693. + #define test_iv (test_key_e+key_e_size+MARGIN)
  27694. + #define test_key (test_iv+iv_size+MARGIN)
  27695. + #define test_size (BUFSZ*3+key_e_size+iv_size+keysize+MARGIN*7)
  27696. + ixt_e=(struct ipsec_alg_enc *)ipsec_alg_get(IPSEC_ALG_TYPE_ENCRYPT, enc_alg);
  27697. + if (ixt_e==NULL) {
  27698. + KLIPS_PRINT(1,
  27699. + "klips_debug: ipsec_alg_test_encrypt: "
  27700. + "encalg=%d object not found\n",
  27701. + enc_alg);
  27702. + ret=-EINVAL;
  27703. + goto out;
  27704. + }
  27705. + iv_size=ixt_e->ixt_common.ixt_support.ias_ivlen / 8;
  27706. + key_e_size=ixt_e->ixt_e_ctx_size;
  27707. + keysize=ixt_e->ixt_e_keylen;
  27708. + KLIPS_PRINT(1,
  27709. + "klips_debug: ipsec_alg_test_encrypt: "
  27710. + "enc_alg=%d blocksize=%d key_e_size=%d keysize=%d\n",
  27711. + enc_alg, iv_size, key_e_size, keysize);
  27712. + if ((buf=kmalloc (test_size, GFP_KERNEL)) == NULL) {
  27713. + ret= -ENOMEM;
  27714. + goto out;
  27715. + }
  27716. + get_random_bytes(test_key, keysize);
  27717. + get_random_bytes(test_iv, iv_size);
  27718. + if (ixt_e->ixt_e_new_key) {
  27719. + tmp_key_e = ixt_e->ixt_e_new_key(ixt_e, test_key, keysize);
  27720. + ret = tmp_key_e ? 0 : -EINVAL;
  27721. + } else {
  27722. + tmp_key_e = test_key_e;
  27723. + ret = ixt_e->ixt_e_set_key(ixt_e, test_key_e, test_key, keysize);
  27724. + }
  27725. + if (ret < 0)
  27726. + goto out;
  27727. + get_random_bytes(test_enc, BUFSZ);
  27728. + memcpy(test_tmp, test_enc, BUFSZ);
  27729. + ret=ixt_e->ixt_e_cbc_encrypt(ixt_e, tmp_key_e, test_enc, BUFSZ, test_iv, 1);
  27730. + printk(KERN_INFO
  27731. + "klips_info: ipsec_alg_test_encrypt: "
  27732. + "cbc_encrypt=1 ret=%d\n",
  27733. + ret);
  27734. + ret=memcmp(test_enc, test_tmp, BUFSZ);
  27735. + printk(KERN_INFO
  27736. + "klips_info: ipsec_alg_test_encrypt: "
  27737. + "memcmp(enc, tmp) ret=%d: %s\n", ret,
  27738. + ret!=0? "OK. (encr->DIFFers)" : "FAIL! (encr->SAME)" );
  27739. + memcpy(test_dec, test_enc, BUFSZ);
  27740. + ret=ixt_e->ixt_e_cbc_encrypt(ixt_e, tmp_key_e, test_dec, BUFSZ, test_iv, 0);
  27741. + printk(KERN_INFO
  27742. + "klips_info: ipsec_alg_test_encrypt: "
  27743. + "cbc_encrypt=0 ret=%d\n", ret);
  27744. + ret=memcmp(test_dec, test_tmp, BUFSZ);
  27745. + printk(KERN_INFO
  27746. + "klips_info: ipsec_alg_test_encrypt: "
  27747. + "memcmp(dec,tmp) ret=%d: %s\n", ret,
  27748. + ret==0? "OK. (encr->decr->SAME)" : "FAIL! (encr->decr->DIFFers)" );
  27749. + {
  27750. + /* Shamelessly taken from drivers/md sources O:) */
  27751. + unsigned long now;
  27752. + int i, count, max=0;
  27753. + int encrypt, speed;
  27754. + for (encrypt=0; encrypt <2;encrypt ++) {
  27755. + for (i = 0; i < 5; i++) {
  27756. + now = jiffies;
  27757. + count = 0;
  27758. + while (jiffies == now) {
  27759. + mb();
  27760. + ixt_e->ixt_e_cbc_encrypt(ixt_e,
  27761. + tmp_key_e, test_tmp,
  27762. + BUFSZ, test_iv, encrypt);
  27763. + mb();
  27764. + count++;
  27765. + mb();
  27766. + }
  27767. + if (count > max)
  27768. + max = count;
  27769. + }
  27770. + speed = max * (HZ * BUFSZ / 1024);
  27771. + printk(KERN_INFO
  27772. + "klips_info: ipsec_alg_test_encrypt: "
  27773. + "%s %s speed=%d KB/s\n",
  27774. + ixt_e->ixt_common.ixt_name,
  27775. + encrypt? "encrypt": "decrypt", speed);
  27776. + }
  27777. + }
  27778. +out:
  27779. + if (tmp_key_e && ixt_e->ixt_e_destroy_key) ixt_e->ixt_e_destroy_key(ixt_e, tmp_key_e);
  27780. + if (buf) kfree(buf);
  27781. + if (ixt_e) ipsec_alg_put((struct ipsec_alg *)ixt_e);
  27782. + return ret;
  27783. + #undef test_enc
  27784. + #undef test_dec
  27785. + #undef test_tmp
  27786. + #undef test_key_e
  27787. + #undef test_iv
  27788. + #undef test_key
  27789. + #undef test_size
  27790. +}
  27791. +
  27792. +/*
  27793. + * Must be called from user context
  27794. + * used at module load type for testing algo implementation
  27795. + */
  27796. +static int ipsec_alg_test_auth(int auth_alg, int test) {
  27797. + int ret;
  27798. + caddr_t buf = NULL;
  27799. + int blocksize, keysize, key_a_size;
  27800. + struct ipsec_alg_auth *ixt_a;
  27801. + #define BUFSZ 1024
  27802. + #define MARGIN 0
  27803. + #define test_auth (buf+MARGIN)
  27804. + #define test_key_a (test_auth+BUFSZ+MARGIN)
  27805. + #define test_key (test_key_a+key_a_size+MARGIN)
  27806. + #define test_hash (test_key+keysize+MARGIN)
  27807. + #define test_size (BUFSZ+key_a_size+keysize+AHHMAC_HASHLEN+MARGIN*4)
  27808. + ixt_a=(struct ipsec_alg_auth *)ipsec_alg_get(IPSEC_ALG_TYPE_AUTH, auth_alg);
  27809. + if (ixt_a==NULL) {
  27810. + KLIPS_PRINT(1,
  27811. + "klips_debug: ipsec_alg_test_auth: "
  27812. + "encalg=%d object not found\n",
  27813. + auth_alg);
  27814. + ret=-EINVAL;
  27815. + goto out;
  27816. + }
  27817. + blocksize=ixt_a->ixt_common.ixt_blocksize;
  27818. + key_a_size=ixt_a->ixt_a_ctx_size;
  27819. + keysize=ixt_a->ixt_a_keylen;
  27820. + KLIPS_PRINT(1,
  27821. + "klips_debug: ipsec_alg_test_auth: "
  27822. + "auth_alg=%d blocksize=%d key_a_size=%d keysize=%d\n",
  27823. + auth_alg, blocksize, key_a_size, keysize);
  27824. + if ((buf=kmalloc (test_size, GFP_KERNEL)) == NULL) {
  27825. + ret= -ENOMEM;
  27826. + goto out;
  27827. + }
  27828. + get_random_bytes(test_key, keysize);
  27829. + ret = ixt_a->ixt_a_hmac_set_key(ixt_a, test_key_a, test_key, keysize);
  27830. + if (ret < 0 )
  27831. + goto out;
  27832. + get_random_bytes(test_auth, BUFSZ);
  27833. + ret=ixt_a->ixt_a_hmac_hash(ixt_a, test_key_a, test_auth, BUFSZ, test_hash, AHHMAC_HASHLEN);
  27834. + printk(KERN_INFO
  27835. + "klips_info: ipsec_alg_test_auth: "
  27836. + "ret=%d\n", ret);
  27837. + {
  27838. + /* Shamelessly taken from drivers/md sources O:) */
  27839. + unsigned long now;
  27840. + int i, count, max=0;
  27841. + int speed;
  27842. + for (i = 0; i < 5; i++) {
  27843. + now = jiffies;
  27844. + count = 0;
  27845. + while (jiffies == now) {
  27846. + mb();
  27847. + ixt_a->ixt_a_hmac_hash(ixt_a, test_key_a, test_auth, BUFSZ, test_hash, AHHMAC_HASHLEN);
  27848. + mb();
  27849. + count++;
  27850. + mb();
  27851. + }
  27852. + if (count > max)
  27853. + max = count;
  27854. + }
  27855. + speed = max * (HZ * BUFSZ / 1024);
  27856. + printk(KERN_INFO
  27857. + "klips_info: ipsec_alg_test_auth: "
  27858. + "%s hash speed=%d KB/s\n",
  27859. + ixt_a->ixt_common.ixt_name,
  27860. + speed);
  27861. + }
  27862. +out:
  27863. + if (buf) kfree(buf);
  27864. + if (ixt_a) ipsec_alg_put((struct ipsec_alg *)ixt_a);
  27865. + return ret;
  27866. + #undef test_auth
  27867. + #undef test_key_a
  27868. + #undef test_key
  27869. + #undef test_hash
  27870. + #undef test_size
  27871. +}
  27872. +
  27873. +int ipsec_alg_test(unsigned alg_type, unsigned alg_id, int test) {
  27874. + switch(alg_type) {
  27875. + case IPSEC_ALG_TYPE_ENCRYPT:
  27876. + return ipsec_alg_test_encrypt(alg_id, test);
  27877. + break;
  27878. + case IPSEC_ALG_TYPE_AUTH:
  27879. + return ipsec_alg_test_auth(alg_id, test);
  27880. + break;
  27881. + }
  27882. + printk(KERN_ERR "klips_info: ipsec_alg_test() called incorrectly: "
  27883. + "alg_type=%d alg_id=%d\n",
  27884. + alg_type, alg_id);
  27885. + return -EINVAL;
  27886. +}
  27887. +
  27888. +int ipsec_alg_init(void) {
  27889. + KLIPS_PRINT(1, "klips_info:ipsec_alg_init: "
  27890. + "KLIPS alg v=%d.%d.%d-%d (EALG_MAX=%d, AALG_MAX=%d)\n",
  27891. + IPSEC_ALG_VERSION_QUAD(IPSEC_ALG_VERSION),
  27892. + SADB_EALG_MAX, SADB_AALG_MAX);
  27893. + /* Initialize tables */
  27894. + write_lock_bh(&ipsec_alg_lock);
  27895. + ipsec_alg_hash_init();
  27896. + write_unlock_bh(&ipsec_alg_lock);
  27897. +
  27898. + /* Initialize static algos */
  27899. + KLIPS_PRINT(1, "klips_info:ipsec_alg_init: "
  27900. + "calling ipsec_alg_static_init()\n");
  27901. +
  27902. + /* If we are suppose to use our AES, and don't have
  27903. + * CryptoAPI enabled...
  27904. + */
  27905. +#if defined(CONFIG_KLIPS_ENC_AES) && CONFIG_KLIPS_ENC_AES && !defined(CONFIG_KLIPS_ENC_AES_MODULE)
  27906. +#if defined(CONFIG_KLIPS_ENC_CRYPTOAPI) && CONFIG_KLIPS_ENC_CRYPTOAPI
  27907. +#warning "Using built-in AES rather than CryptoAPI AES"
  27908. +#endif
  27909. + {
  27910. + extern int ipsec_aes_init(void);
  27911. + ipsec_aes_init();
  27912. + }
  27913. +#endif
  27914. +
  27915. +#if defined(CONFIG_KLIPS_ENC_3DES) && CONFIG_KLIPS_ENC_3DES && !defined(CONFIG_KLIPS_ENC_3DES_MODULE)
  27916. +#if defined(CONFIG_KLIPS_ENC_CRYPTOAPI) && CONFIG_KLIPS_ENC_CRYPTOAPI
  27917. +#warning "Using built-in 3des rather than CryptoAPI 3des"
  27918. +#endif
  27919. + {
  27920. + extern int ipsec_3des_init(void);
  27921. + ipsec_3des_init();
  27922. + }
  27923. +#endif
  27924. +#if defined(CONFIG_KLIPS_ENC_NULL) && CONFIG_KLIPS_ENC_NULL && !defined(CONFIG_KLIPS_ENC_NULL_MODULE)
  27925. +#if defined(CONFIG_KLIPS_ENC_CRYPTOAPI) && CONFIG_KLIPS_ENC_CRYPTOAPI
  27926. +#warning "Using built-in null cipher rather than CryptoAPI null cipher"
  27927. +#endif
  27928. +#warning "Building with null cipher (ESP_NULL), blame on you :-)"
  27929. + {
  27930. + extern int ipsec_null_init(void);
  27931. + ipsec_null_init();
  27932. + }
  27933. +#endif
  27934. +
  27935. +
  27936. + /* If we are doing CryptoAPI, then init */
  27937. +#if defined(CONFIG_KLIPS_ENC_CRYPTOAPI) && CONFIG_KLIPS_ENC_CRYPTOAPI && !defined(CONFIG_KLIPS_ENC_CRYPTOAPI_MODULE)
  27938. + {
  27939. + extern int ipsec_cryptoapi_init(void);
  27940. + ipsec_cryptoapi_init();
  27941. + }
  27942. +#endif
  27943. +
  27944. +
  27945. + return 0;
  27946. +}
  27947. +
  27948. +/**********************************************
  27949. + *
  27950. + * INTERFACE for ipsec_sa init and wipe
  27951. + *
  27952. + **********************************************/
  27953. +
  27954. +/*
  27955. + * Called from pluto -> pfkey_v2_parser.c:pfkey_ipsec_sa_init()
  27956. + */
  27957. +int ipsec_alg_sa_init(struct ipsec_sa *sa_p) {
  27958. + struct ipsec_alg_enc *ixt_e;
  27959. + struct ipsec_alg_auth *ixt_a;
  27960. +
  27961. + /* Only ESP for now ... */
  27962. + if (sa_p->ips_said.proto != IPPROTO_ESP)
  27963. + return -EPROTONOSUPPORT;
  27964. +
  27965. + KLIPS_PRINT(debug_pfkey, "klips_debug: ipsec_alg_sa_init() :"
  27966. + "entering for encalg=%d, authalg=%d\n",
  27967. + sa_p->ips_encalg, sa_p->ips_authalg);
  27968. +
  27969. + if ((ixt_e=(struct ipsec_alg_enc *)
  27970. + ipsec_alg_get(IPSEC_ALG_TYPE_ENCRYPT, sa_p->ips_encalg))) {
  27971. + KLIPS_PRINT(debug_pfkey,
  27972. + "klips_debug: ipsec_alg_sa_init() :"
  27973. + "found ipsec_alg (ixt_e=%p) for encalg=%d\n",
  27974. + ixt_e, sa_p->ips_encalg);
  27975. + sa_p->ips_alg_enc=ixt_e;
  27976. + }
  27977. +
  27978. + if ((ixt_a=(struct ipsec_alg_auth *)
  27979. + ipsec_alg_get(IPSEC_ALG_TYPE_AUTH, sa_p->ips_authalg))) {
  27980. + KLIPS_PRINT(debug_pfkey,
  27981. + "klips_debug: ipsec_alg_sa_init() :"
  27982. + "found ipsec_alg (ixt_a=%p) for auth=%d\n",
  27983. + ixt_a, sa_p->ips_authalg);
  27984. + sa_p->ips_alg_auth=ixt_a;
  27985. + }
  27986. + return 0;
  27987. +}
  27988. +
  27989. +/*
  27990. + * Called from pluto -> ipsec_sa.c:ipsec_sa_delchain()
  27991. + */
  27992. +int ipsec_alg_sa_wipe(struct ipsec_sa *sa_p) {
  27993. + struct ipsec_alg *ixt;
  27994. + if ((ixt=(struct ipsec_alg *)sa_p->ips_alg_enc)) {
  27995. + KLIPS_PRINT(debug_pfkey, "klips_debug: ipsec_alg_sa_wipe() :"
  27996. + "unlinking for encalg=%d\n",
  27997. + ixt->ixt_support.ias_id);
  27998. + ipsec_alg_put(ixt);
  27999. + }
  28000. + if ((ixt=(struct ipsec_alg *)sa_p->ips_alg_auth)) {
  28001. + KLIPS_PRINT(debug_pfkey, "klips_debug: ipsec_alg_sa_wipe() :"
  28002. + "unlinking for authalg=%d\n",
  28003. + ixt->ixt_support.ias_id);
  28004. + ipsec_alg_put(ixt);
  28005. + }
  28006. + return 0;
  28007. +}
  28008. +
  28009. +IPSEC_PROCFS_DEBUG_NO_STATIC
  28010. +int
  28011. +ipsec_xform_get_info(char *buffer,
  28012. + char **start,
  28013. + off_t offset,
  28014. + int length IPSEC_PROC_LAST_ARG)
  28015. +{
  28016. + int len = 0;
  28017. + off_t begin = 0;
  28018. + int i;
  28019. + struct list_head *head;
  28020. + struct ipsec_alg *ixt;
  28021. +
  28022. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  28023. + "klips_debug:ipsec_tncfg_get_info: "
  28024. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  28025. + buffer,
  28026. + *start,
  28027. + (int)offset,
  28028. + length);
  28029. +
  28030. + for(i = 0, head = ipsec_alg_hash_table;
  28031. + i<IPSEC_ALG_HASHSZ;
  28032. + i++, head++)
  28033. + {
  28034. + struct list_head *p;
  28035. + for (p=head->next; p!=head; p=p->next)
  28036. + {
  28037. + ixt = list_entry(p, struct ipsec_alg, ixt_list);
  28038. + len += ipsec_snprintf(buffer+len, length-len,
  28039. + "VERSION=%d TYPE=%d ID=%d NAME=%s REFCNT=%d ",
  28040. + ixt->ixt_version, ixt->ixt_alg_type, ixt->ixt_support.ias_id,
  28041. + ixt->ixt_name, ixt->ixt_refcnt);
  28042. +
  28043. + len += ipsec_snprintf(buffer+len, length-len,
  28044. + "STATE=%08x BLOCKSIZE=%d IVLEN=%d KEYMINBITS=%d KEYMAXBITS=%d ",
  28045. + ixt->ixt_state, ixt->ixt_blocksize,
  28046. + ixt->ixt_support.ias_ivlen, ixt->ixt_support.ias_keyminbits, ixt->ixt_support.ias_keymaxbits);
  28047. +
  28048. + len += ipsec_snprintf(buffer+len, length-len,
  28049. + "IVLEN=%d KEYMINBITS=%d KEYMAXBITS=%d ",
  28050. + ixt->ixt_support.ias_ivlen, ixt->ixt_support.ias_keyminbits, ixt->ixt_support.ias_keymaxbits);
  28051. +
  28052. + switch(ixt->ixt_alg_type)
  28053. + {
  28054. + case IPSEC_ALG_TYPE_AUTH:
  28055. + {
  28056. + struct ipsec_alg_auth *auth = (struct ipsec_alg_auth *)ixt;
  28057. +
  28058. + len += ipsec_snprintf(buffer+len, length-len,
  28059. + "KEYLEN=%d CTXSIZE=%d AUTHLEN=%d ",
  28060. + auth->ixt_a_keylen, auth->ixt_a_ctx_size,
  28061. + auth->ixt_a_authlen);
  28062. + break;
  28063. + }
  28064. + case IPSEC_ALG_TYPE_ENCRYPT:
  28065. + {
  28066. + struct ipsec_alg_enc *enc = (struct ipsec_alg_enc *)ixt;
  28067. + len += ipsec_snprintf(buffer+len, length-len,
  28068. + "KEYLEN=%d CTXSIZE=%d ",
  28069. + enc->ixt_e_keylen, enc->ixt_e_ctx_size);
  28070. +
  28071. + break;
  28072. + }
  28073. + }
  28074. +
  28075. + len += ipsec_snprintf(buffer+len, length-len, "\n");
  28076. + }
  28077. + }
  28078. +
  28079. + *start = buffer + (offset - begin); /* Start of wanted data */
  28080. + len -= (offset - begin); /* Start slop */
  28081. + if (len > length)
  28082. + len = length;
  28083. + return len;
  28084. +}
  28085. +
  28086. +
  28087. +/*
  28088. + * As the author of this module, I ONLY ALLOW using it from
  28089. + * GPL (or same LICENSE TERMS as kernel source) modules.
  28090. + *
  28091. + * In respect to hardware crypto engines this means:
  28092. + * * Closed-source device drivers ARE NOT ALLOWED to use
  28093. + * this interface.
  28094. + * * Closed-source VHDL/Verilog firmware running on
  28095. + * the crypto hardware device IS ALLOWED to use this interface
  28096. + * via a GPL (or same LICENSE TERMS as kernel source) device driver.
  28097. + * --Juan Jose Ciarlante 20/03/2002 (thanks RGB for the correct wording)
  28098. + */
  28099. +
  28100. +/*
  28101. + * These symbols can only be used from GPL modules
  28102. + * for now, I'm disabling this because it creates false
  28103. + * symbol problems for old modutils.
  28104. + */
  28105. +
  28106. +#ifdef CONFIG_MODULES
  28107. +#ifndef NET_26
  28108. +#if 0
  28109. +#ifndef EXPORT_SYMBOL_GPL
  28110. +#undef EXPORT_SYMBOL_GPL
  28111. +#define EXPORT_SYMBOL_GPL EXPORT_SYMBOL
  28112. +#endif
  28113. +#endif
  28114. +EXPORT_SYMBOL(register_ipsec_alg);
  28115. +EXPORT_SYMBOL(unregister_ipsec_alg);
  28116. +EXPORT_SYMBOL(ipsec_alg_test);
  28117. +#endif
  28118. +#endif
  28119. --- /dev/null Tue Mar 11 13:02:56 2003
  28120. +++ linux/net/ipsec/ipsec_alg_cryptoapi.c Mon Feb 9 13:51:03 2004
  28121. @@ -0,0 +1,455 @@
  28122. +/*
  28123. + * ipsec_alg to linux cryptoapi GLUE
  28124. + *
  28125. + * Authors: CODE.ar TEAM
  28126. + * Harpo MAxx <harpo@linuxmendoza.org.ar>
  28127. + * JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  28128. + * Luciano Ruete <docemeses@softhome.net>
  28129. + *
  28130. + * ipsec_alg_cryptoapi.c,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
  28131. + *
  28132. + * This program is free software; you can redistribute it and/or modify it
  28133. + * under the terms of the GNU General Public License as published by the
  28134. + * Free Software Foundation; either version 2 of the License, or (at your
  28135. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  28136. + *
  28137. + * This program is distributed in the hope that it will be useful, but
  28138. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  28139. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  28140. + * for more details.
  28141. + *
  28142. + * Example usage:
  28143. + * modinfo -p ipsec_cryptoapi (quite useful info, including supported algos)
  28144. + * modprobe ipsec_cryptoapi
  28145. + * modprobe ipsec_cryptoapi test=1
  28146. + * modprobe ipsec_cryptoapi excl=1 (exclusive cipher/algo)
  28147. + * modprobe ipsec_cryptoapi noauto=1 aes=1 twofish=1 (only these ciphers)
  28148. + * modprobe ipsec_cryptoapi aes=128,128 (force these keylens)
  28149. + * modprobe ipsec_cryptoapi des_ede3=0 (everything but 3DES)
  28150. + */
  28151. +#ifndef AUTOCONF_INCLUDED
  28152. +#include <linux/config.h>
  28153. +#endif
  28154. +#include <linux/version.h>
  28155. +
  28156. +/*
  28157. + * special case: ipsec core modular with this static algo inside:
  28158. + * must avoid MODULE magic for this file
  28159. + */
  28160. +#if defined(CONFIG_KLIPS_MODULE) && defined(CONFIG_KLIPS_ENC_CRYPTOAPI)
  28161. +#undef MODULE
  28162. +#endif
  28163. +
  28164. +#include <linux/module.h>
  28165. +#include <linux/init.h>
  28166. +
  28167. +#include <linux/kernel.h> /* printk() */
  28168. +#include <linux/errno.h> /* error codes */
  28169. +#include <linux/types.h> /* size_t */
  28170. +#include <linux/string.h>
  28171. +
  28172. +/* Check if __exit is defined, if not null it */
  28173. +#ifndef __exit
  28174. +#define __exit
  28175. +#endif
  28176. +
  28177. +/* warn the innocent */
  28178. +#if !defined (CONFIG_CRYPTO) && !defined (CONFIG_CRYPTO_MODULE)
  28179. +#warning "No linux CryptoAPI found, install 2.4.22+ or 2.6.x"
  28180. +#define NO_CRYPTOAPI_SUPPORT
  28181. +#endif
  28182. +
  28183. +#include "openswan.h"
  28184. +#include "openswan/ipsec_alg.h"
  28185. +#include "openswan/ipsec_policy.h"
  28186. +
  28187. +#include <linux/crypto.h>
  28188. +#ifdef CRYPTO_API_VERSION_CODE
  28189. +#warning "Old CryptoAPI is not supported. Only linux-2.4.22+ or linux-2.6.x are supported"
  28190. +#define NO_CRYPTOAPI_SUPPORT
  28191. +#endif
  28192. +
  28193. +#ifdef NO_CRYPTOAPI_SUPPORT
  28194. +#warning "Building an unusable module :P"
  28195. +/* Catch old CryptoAPI by not allowing module to load */
  28196. +IPSEC_ALG_MODULE_INIT_STATIC( ipsec_cryptoapi_init )
  28197. +{
  28198. + printk(KERN_WARNING "ipsec_cryptoapi.o was not built on stock Linux CryptoAPI (2.4.22+ or 2.6.x), not loading.\n");
  28199. + return -EINVAL;
  28200. +}
  28201. +#else
  28202. +#include <asm/scatterlist.h>
  28203. +#include <asm/pgtable.h>
  28204. +#include <linux/mm.h>
  28205. +
  28206. +#define CIPHERNAME_AES "aes"
  28207. +#define CIPHERNAME_1DES "des"
  28208. +#define CIPHERNAME_3DES "des3_ede"
  28209. +#define CIPHERNAME_BLOWFISH "blowfish"
  28210. +#define CIPHERNAME_CAST "cast5"
  28211. +#define CIPHERNAME_SERPENT "serpent"
  28212. +#define CIPHERNAME_TWOFISH "twofish"
  28213. +
  28214. +#define ESP_SERPENT 252 /* from ipsec drafts */
  28215. +#define ESP_TWOFISH 253 /* from ipsec drafts */
  28216. +
  28217. +#define DIGESTNAME_MD5 "md5"
  28218. +#define DIGESTNAME_SHA1 "sha1"
  28219. +
  28220. +MODULE_AUTHOR("Juanjo Ciarlante, Harpo MAxx, Luciano Ruete");
  28221. +static int debug_crypto=0;
  28222. +static int test_crypto=0;
  28223. +static int excl_crypto=0;
  28224. +
  28225. +static int noauto = 0;
  28226. +
  28227. +#ifdef module_param
  28228. +module_param(debug_crypto,int,0600)
  28229. +module_param(test_crypto,int,0600)
  28230. +module_param(excl_crypto,int,0600)
  28231. +
  28232. +module_param(noauto,int,0600)
  28233. +#else
  28234. +MODULE_PARM(debug_crypto, "i");
  28235. +MODULE_PARM(test_crypto, "i");
  28236. +MODULE_PARM(excl_crypto, "i");
  28237. +
  28238. +MODULE_PARM(noauto,"i");
  28239. +#endif
  28240. +MODULE_PARM_DESC(noauto, "Dont try all known algos, just setup enabled ones");
  28241. +
  28242. +#ifdef CONFIG_KLIPS_ENC_1DES
  28243. +static int des_ede1[] = {-1, -1};
  28244. +#endif
  28245. +static int des_ede3[] = {-1, -1};
  28246. +static int aes[] = {-1, -1};
  28247. +static int blowfish[] = {-1, -1};
  28248. +static int cast[] = {-1, -1};
  28249. +static int serpent[] = {-1, -1};
  28250. +static int twofish[] = {-1, -1};
  28251. +
  28252. +#ifdef CONFIG_KLIPS_ENC_1DES
  28253. +#ifdef module_param
  28254. +module_param_array(des_ede1,int,NULL,0)
  28255. +#else
  28256. +MODULE_PARM(des_ede1,"1-2i");
  28257. +#endif
  28258. +#endif
  28259. +#ifdef module_param
  28260. +module_param_array(des_ede3,int,NULL,0)
  28261. +module_param_array(aes,int,NULL,0)
  28262. +module_param_array(blowfish,int,NULL,0)
  28263. +module_param_array(cast,int,NULL,0)
  28264. +module_param_array(serpent,int,NULL,0)
  28265. +module_param_array(twofish,int,NULL,0)
  28266. +#else
  28267. +MODULE_PARM(des_ede3,"1-2i");
  28268. +MODULE_PARM(aes,"1-2i");
  28269. +MODULE_PARM(blowfish,"1-2i");
  28270. +MODULE_PARM(cast,"1-2i");
  28271. +MODULE_PARM(serpent,"1-2i");
  28272. +MODULE_PARM(twofish,"1-2i");
  28273. +#endif
  28274. +MODULE_PARM_DESC(des_ede1, "0: disable | 1: force_enable | min,max: dontuse");
  28275. +MODULE_PARM_DESC(des_ede3, "0: disable | 1: force_enable | min,max: dontuse");
  28276. +MODULE_PARM_DESC(aes, "0: disable | 1: force_enable | min,max: keybitlens");
  28277. +MODULE_PARM_DESC(blowfish, "0: disable | 1: force_enable | min,max: keybitlens");
  28278. +MODULE_PARM_DESC(cast, "0: disable | 1: force_enable | min,max: keybitlens");
  28279. +MODULE_PARM_DESC(serpent, "0: disable | 1: force_enable | min,max: keybitlens");
  28280. +MODULE_PARM_DESC(twofish, "0: disable | 1: force_enable | min,max: keybitlens");
  28281. +
  28282. +struct ipsec_alg_capi_cipher {
  28283. + const char *ciphername; /* cryptoapi's ciphername */
  28284. + unsigned blocksize;
  28285. + unsigned short minbits;
  28286. + unsigned short maxbits;
  28287. + int *parm; /* lkm param for this cipher */
  28288. + struct ipsec_alg_enc alg; /* note it's not a pointer */
  28289. +};
  28290. +
  28291. +static struct ipsec_alg_capi_cipher alg_capi_carray[] = {
  28292. + { CIPHERNAME_AES, 16, 128, 256, aes, { ixt_common:{ ixt_support:{ ias_id: ESP_AES}}}},
  28293. + { CIPHERNAME_TWOFISH, 16, 128, 256, twofish, { ixt_common:{ ixt_support:{ ias_id: ESP_TWOFISH,}}}},
  28294. + { CIPHERNAME_SERPENT, 16, 128, 256, serpent, { ixt_common:{ ixt_support:{ ias_id: ESP_SERPENT,}}}},
  28295. + { CIPHERNAME_CAST, 8, 128, 128, cast , { ixt_common:{ ixt_support:{ ias_id: ESP_CAST,}}}},
  28296. + { CIPHERNAME_BLOWFISH, 8, 96, 448, blowfish, { ixt_common:{ ixt_support:{ ias_id: ESP_BLOWFISH,}}}},
  28297. + { CIPHERNAME_3DES, 8, 192, 192, des_ede3, { ixt_common:{ ixt_support:{ ias_id: ESP_3DES,}}}},
  28298. +#ifdef CONFIG_KLIPS_ENC_1DES
  28299. + { CIPHERNAME_1DES, 8, 64, 64, des_ede1, { ixt_common:{ ixt_support:{ ias_id: ESP_DES,}}}},
  28300. +#endif
  28301. + { NULL, 0, 0, 0, NULL, {} }
  28302. +};
  28303. +
  28304. +#ifdef NOT_YET
  28305. +struct ipsec_alg_capi_digest {
  28306. + const char *digestname; /* cryptoapi's digestname */
  28307. + struct digest_implementation *di;
  28308. + struct ipsec_alg_auth alg; /* note it's not a pointer */
  28309. +};
  28310. +static struct ipsec_alg_capi_cipher alg_capi_darray[] = {
  28311. + { DIGESTNAME_MD5, NULL, { ixt_alg_id: AH_MD5, }},
  28312. + { DIGESTNAME_SHA1, NULL, { ixt_alg_id: AH_SHA, }},
  28313. + { NULL, NULL, {} }
  28314. +};
  28315. +#endif
  28316. +/*
  28317. + * "generic" linux cryptoapi setup_cipher() function
  28318. + */
  28319. +int setup_cipher(const char *ciphername)
  28320. +{
  28321. + return crypto_alg_available(ciphername, 0);
  28322. +}
  28323. +
  28324. +/*
  28325. + * setups ipsec_alg_capi_cipher "hyper" struct components, calling
  28326. + * register_ipsec_alg for cointaned ipsec_alg object
  28327. + */
  28328. +static void _capi_destroy_key (struct ipsec_alg_enc *alg, __u8 *key_e);
  28329. +static __u8 * _capi_new_key (struct ipsec_alg_enc *alg, const __u8 *key, size_t keylen);
  28330. +static int _capi_cbc_encrypt(struct ipsec_alg_enc *alg, __u8 * key_e, __u8 * in, int ilen, const __u8 * iv, int encrypt);
  28331. +
  28332. +static int
  28333. +setup_ipsec_alg_capi_cipher(struct ipsec_alg_capi_cipher *cptr)
  28334. +{
  28335. + int ret;
  28336. + cptr->alg.ixt_common.ixt_version = IPSEC_ALG_VERSION;
  28337. + cptr->alg.ixt_common.ixt_module = THIS_MODULE;
  28338. + atomic_set (& cptr->alg.ixt_common.ixt_refcnt, 0);
  28339. + strncpy (cptr->alg.ixt_common.ixt_name , cptr->ciphername, sizeof (cptr->alg.ixt_common.ixt_name));
  28340. +
  28341. + cptr->alg.ixt_common.ixt_blocksize=cptr->blocksize;
  28342. + cptr->alg.ixt_common.ixt_support.ias_keyminbits=cptr->minbits;
  28343. + cptr->alg.ixt_common.ixt_support.ias_keymaxbits=cptr->maxbits;
  28344. + cptr->alg.ixt_common.ixt_state = 0;
  28345. + if (excl_crypto) cptr->alg.ixt_common.ixt_state |= IPSEC_ALG_ST_EXCL;
  28346. + cptr->alg.ixt_e_keylen=cptr->alg.ixt_common.ixt_support.ias_keymaxbits/8;
  28347. + cptr->alg.ixt_e_ctx_size = 0;
  28348. + cptr->alg.ixt_common.ixt_support.ias_exttype = IPSEC_ALG_TYPE_ENCRYPT;
  28349. + cptr->alg.ixt_e_new_key = _capi_new_key;
  28350. + cptr->alg.ixt_e_destroy_key = _capi_destroy_key;
  28351. + cptr->alg.ixt_e_cbc_encrypt = _capi_cbc_encrypt;
  28352. + cptr->alg.ixt_common.ixt_data = cptr;
  28353. +
  28354. + ret=register_ipsec_alg_enc(&cptr->alg);
  28355. + printk(KERN_INFO "KLIPS cryptoapi interface: "
  28356. + "alg_type=%d alg_id=%d name=%s "
  28357. + "keyminbits=%d keymaxbits=%d, %s(%d)\n",
  28358. + cptr->alg.ixt_common.ixt_support.ias_exttype,
  28359. + cptr->alg.ixt_common.ixt_support.ias_id,
  28360. + cptr->alg.ixt_common.ixt_name,
  28361. + cptr->alg.ixt_common.ixt_support.ias_keyminbits,
  28362. + cptr->alg.ixt_common.ixt_support.ias_keymaxbits,
  28363. + ret ? "not found" : "found", ret);
  28364. + return ret;
  28365. +}
  28366. +/*
  28367. + * called in ipsec_sa_wipe() time, will destroy key contexts
  28368. + * and do 1 unbind()
  28369. + */
  28370. +static void
  28371. +_capi_destroy_key (struct ipsec_alg_enc *alg, __u8 *key_e)
  28372. +{
  28373. + struct crypto_tfm *tfm=(struct crypto_tfm*)key_e;
  28374. +
  28375. + if (debug_crypto > 0)
  28376. + printk(KERN_DEBUG "klips_debug: _capi_destroy_key:"
  28377. + "name=%s key_e=%p \n",
  28378. + alg->ixt_common.ixt_name, key_e);
  28379. + if (!key_e) {
  28380. + printk(KERN_ERR "klips_debug: _capi_destroy_key:"
  28381. + "name=%s NULL key_e!\n",
  28382. + alg->ixt_common.ixt_name);
  28383. + return;
  28384. + }
  28385. + crypto_free_tfm(tfm);
  28386. +}
  28387. +
  28388. +/*
  28389. + * create new key context, need alg->ixt_data to know which
  28390. + * (of many) cipher inside this module is the target
  28391. + */
  28392. +static __u8 *
  28393. +_capi_new_key (struct ipsec_alg_enc *alg, const __u8 *key, size_t keylen)
  28394. +{
  28395. + struct ipsec_alg_capi_cipher *cptr;
  28396. + struct crypto_tfm *tfm=NULL;
  28397. +
  28398. + cptr = alg->ixt_common.ixt_data;
  28399. + if (!cptr) {
  28400. + printk(KERN_ERR "_capi_new_key(): "
  28401. + "NULL ixt_data (?!) for \"%s\" algo\n"
  28402. + , alg->ixt_common.ixt_name);
  28403. + goto err;
  28404. + }
  28405. + if (debug_crypto > 0)
  28406. + printk(KERN_DEBUG "klips_debug:_capi_new_key:"
  28407. + "name=%s cptr=%p key=%p keysize=%d\n",
  28408. + alg->ixt_common.ixt_name, cptr, key, keylen);
  28409. +
  28410. + /*
  28411. + * alloc tfm
  28412. + */
  28413. + tfm = crypto_alloc_tfm(cptr->ciphername, CRYPTO_TFM_MODE_CBC);
  28414. + if (!tfm) {
  28415. + printk(KERN_ERR "_capi_new_key(): "
  28416. + "NULL tfm for \"%s\" cryptoapi (\"%s\") algo\n"
  28417. + , alg->ixt_common.ixt_name, cptr->ciphername);
  28418. + goto err;
  28419. + }
  28420. + if (crypto_cipher_setkey(tfm, key, keylen) < 0) {
  28421. + printk(KERN_ERR "_capi_new_key(): "
  28422. + "failed new_key() for \"%s\" cryptoapi algo (keylen=%d)\n"
  28423. + , alg->ixt_common.ixt_name, keylen);
  28424. + crypto_free_tfm(tfm);
  28425. + tfm=NULL;
  28426. + }
  28427. +err:
  28428. + if (debug_crypto > 0)
  28429. + printk(KERN_DEBUG "klips_debug:_capi_new_key:"
  28430. + "name=%s key=%p keylen=%d tfm=%p\n",
  28431. + alg->ixt_common.ixt_name, key, keylen, tfm);
  28432. + return (__u8 *) tfm;
  28433. +}
  28434. +/*
  28435. + * core encryption function: will use cx->ci to call actual cipher's
  28436. + * cbc function
  28437. + */
  28438. +static int
  28439. +_capi_cbc_encrypt(struct ipsec_alg_enc *alg, __u8 * key_e, __u8 * in, int ilen, const __u8 * iv, int encrypt) {
  28440. + int error =0;
  28441. + struct crypto_tfm *tfm=(struct crypto_tfm *)key_e;
  28442. + struct scatterlist sg = {
  28443. + .page = virt_to_page(in),
  28444. + .offset = (unsigned long)(in) % PAGE_SIZE,
  28445. + .length=ilen,
  28446. + };
  28447. + if (debug_crypto > 1)
  28448. + printk(KERN_DEBUG "klips_debug:_capi_cbc_encrypt:"
  28449. + "key_e=%p "
  28450. + "in=%p out=%p ilen=%d iv=%p encrypt=%d\n"
  28451. + , key_e
  28452. + , in, in, ilen, iv, encrypt);
  28453. + crypto_cipher_set_iv(tfm, iv, crypto_tfm_alg_ivsize(tfm));
  28454. + if (encrypt)
  28455. + error = crypto_cipher_encrypt (tfm, &sg, &sg, ilen);
  28456. + else
  28457. + error = crypto_cipher_decrypt (tfm, &sg, &sg, ilen);
  28458. + if (debug_crypto > 1)
  28459. + printk(KERN_DEBUG "klips_debug:_capi_cbc_encrypt:"
  28460. + "error=%d\n"
  28461. + , error);
  28462. + return (error<0)? error : ilen;
  28463. +}
  28464. +/*
  28465. + * main initialization loop: for each cipher in list, do
  28466. + * 1) setup cryptoapi cipher else continue
  28467. + * 2) register ipsec_alg object
  28468. + */
  28469. +static int
  28470. +setup_cipher_list (struct ipsec_alg_capi_cipher* clist)
  28471. +{
  28472. + struct ipsec_alg_capi_cipher *cptr;
  28473. + /* foreach cipher in list ... */
  28474. + for (cptr=clist;cptr->ciphername;cptr++) {
  28475. + /*
  28476. + * see if cipher has been disabled (0) or
  28477. + * if noauto set and not enabled (1)
  28478. + */
  28479. + if (cptr->parm[0] == 0 || (noauto && cptr->parm[0] < 0)) {
  28480. + if (debug_crypto>0)
  28481. + printk(KERN_INFO "setup_cipher_list(): "
  28482. + "ciphername=%s skipped at user request: "
  28483. + "noauto=%d parm[0]=%d parm[1]=%d\n"
  28484. + , cptr->ciphername
  28485. + , noauto
  28486. + , cptr->parm[0]
  28487. + , cptr->parm[1]);
  28488. + continue;
  28489. + }
  28490. + /*
  28491. + * use a local ci to avoid touching cptr->ci,
  28492. + * if register ipsec_alg success then bind cipher
  28493. + */
  28494. + if(cptr->alg.ixt_common.ixt_support.ias_name == NULL) {
  28495. + cptr->alg.ixt_common.ixt_support.ias_name = cptr->ciphername;
  28496. + }
  28497. +
  28498. + if( setup_cipher(cptr->ciphername) ) {
  28499. + if (debug_crypto > 0)
  28500. + printk(KERN_DEBUG "klips_debug:"
  28501. + "setup_cipher_list():"
  28502. + "ciphername=%s found\n"
  28503. + , cptr->ciphername);
  28504. +
  28505. + if (setup_ipsec_alg_capi_cipher(cptr) != 0) {
  28506. + printk(KERN_ERR "klips_debug:"
  28507. + "setup_cipher_list():"
  28508. + "ciphername=%s failed ipsec_alg_register\n"
  28509. + , cptr->ciphername);
  28510. + }
  28511. + } else {
  28512. + printk(KERN_INFO "KLIPS: lookup for ciphername=%s: not found \n",
  28513. + cptr->ciphername);
  28514. + }
  28515. + }
  28516. + return 0;
  28517. +}
  28518. +/*
  28519. + * deregister ipsec_alg objects and unbind ciphers
  28520. + */
  28521. +static int
  28522. +unsetup_cipher_list (struct ipsec_alg_capi_cipher* clist)
  28523. +{
  28524. + struct ipsec_alg_capi_cipher *cptr;
  28525. + /* foreach cipher in list ... */
  28526. + for (cptr=clist;cptr->ciphername;cptr++) {
  28527. + if (cptr->alg.ixt_common.ixt_state & IPSEC_ALG_ST_REGISTERED) {
  28528. + unregister_ipsec_alg_enc(&cptr->alg);
  28529. + }
  28530. + }
  28531. + return 0;
  28532. +}
  28533. +/*
  28534. + * test loop for registered algos
  28535. + */
  28536. +static int
  28537. +test_cipher_list (struct ipsec_alg_capi_cipher* clist)
  28538. +{
  28539. + int test_ret;
  28540. + struct ipsec_alg_capi_cipher *cptr;
  28541. + /* foreach cipher in list ... */
  28542. + for (cptr=clist;cptr->ciphername;cptr++) {
  28543. + if (cptr->alg.ixt_common.ixt_state & IPSEC_ALG_ST_REGISTERED) {
  28544. + test_ret=ipsec_alg_test(
  28545. + cptr->alg.ixt_common.ixt_support.ias_exttype,
  28546. + cptr->alg.ixt_common.ixt_support.ias_id,
  28547. + test_crypto);
  28548. + printk("test_cipher_list(alg_type=%d alg_id=%d): test_ret=%d\n",
  28549. + cptr->alg.ixt_common.ixt_support.ias_exttype,
  28550. + cptr->alg.ixt_common.ixt_support.ias_id,
  28551. + test_ret);
  28552. + }
  28553. + }
  28554. + return 0;
  28555. +}
  28556. +
  28557. +IPSEC_ALG_MODULE_INIT_STATIC( ipsec_cryptoapi_init )
  28558. +{
  28559. + int ret, test_ret;
  28560. + if ((ret=setup_cipher_list(alg_capi_carray)) < 0)
  28561. + return -EPROTONOSUPPORT;
  28562. + if (ret==0 && test_crypto) {
  28563. + test_ret=test_cipher_list(alg_capi_carray);
  28564. + }
  28565. + return ret;
  28566. +}
  28567. +IPSEC_ALG_MODULE_EXIT_STATIC( ipsec_cryptoapi_fini )
  28568. +{
  28569. + unsetup_cipher_list(alg_capi_carray);
  28570. + return;
  28571. +}
  28572. +#ifdef MODULE_LICENSE
  28573. +MODULE_LICENSE("GPL");
  28574. +#endif
  28575. +
  28576. +#endif /* NO_CRYPTOAPI_SUPPORT */
  28577. --- /dev/null Tue Mar 11 13:02:56 2003
  28578. +++ linux/net/ipsec/ipsec_esp.c Mon Feb 9 13:51:03 2004
  28579. @@ -0,0 +1,607 @@
  28580. +/*
  28581. + * processing code for ESP
  28582. + * Copyright (C) 2003 Michael Richardson <mcr@sandelman.ottawa.on.ca>
  28583. + *
  28584. + * This program is free software; you can redistribute it and/or modify it
  28585. + * under the terms of the GNU General Public License as published by the
  28586. + * Free Software Foundation; either version 2 of the License, or (at your
  28587. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  28588. + *
  28589. + * This program is distributed in the hope that it will be useful, but
  28590. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  28591. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  28592. + * for more details.
  28593. + */
  28594. +
  28595. +char ipsec_esp_c_version[] = "RCSID $Id: ipsec_esp.c,v 1.13.2.6 2006/10/06 21:39:26 paul Exp $";
  28596. +#ifndef AUTOCONF_INCLUDED
  28597. +#include <linux/config.h>
  28598. +#endif
  28599. +#include <linux/version.h>
  28600. +
  28601. +#define __NO_VERSION__
  28602. +#include <linux/module.h>
  28603. +#include <linux/kernel.h> /* printk() */
  28604. +
  28605. +#include "openswan/ipsec_param.h"
  28606. +
  28607. +#ifdef MALLOC_SLAB
  28608. +# include <linux/slab.h> /* kmalloc() */
  28609. +#else /* MALLOC_SLAB */
  28610. +# include <linux/malloc.h> /* kmalloc() */
  28611. +#endif /* MALLOC_SLAB */
  28612. +#include <linux/errno.h> /* error codes */
  28613. +#include <linux/types.h> /* size_t */
  28614. +#include <linux/interrupt.h> /* mark_bh */
  28615. +
  28616. +#include <linux/netdevice.h> /* struct device, and other headers */
  28617. +#include <linux/etherdevice.h> /* eth_type_trans */
  28618. +#include <linux/ip.h> /* struct iphdr */
  28619. +#include <linux/skbuff.h>
  28620. +#include <openswan.h>
  28621. +#ifdef SPINLOCK
  28622. +# ifdef SPINLOCK_23
  28623. +# include <linux/spinlock.h> /* *lock* */
  28624. +# else /* SPINLOCK_23 */
  28625. +# include <asm/spinlock.h> /* *lock* */
  28626. +# endif /* SPINLOCK_23 */
  28627. +#endif /* SPINLOCK */
  28628. +
  28629. +#include <net/ip.h>
  28630. +#include <net/protocol.h>
  28631. +
  28632. +#include "openswan/radij.h"
  28633. +#include "openswan/ipsec_encap.h"
  28634. +#include "openswan/ipsec_sa.h"
  28635. +
  28636. +#include "openswan/ipsec_radij.h"
  28637. +#include "openswan/ipsec_xform.h"
  28638. +#include "openswan/ipsec_tunnel.h"
  28639. +#include "openswan/ipsec_rcv.h"
  28640. +#include "openswan/ipsec_xmit.h"
  28641. +
  28642. +#include "openswan/ipsec_auth.h"
  28643. +
  28644. +#ifdef CONFIG_KLIPS_ESP
  28645. +#include "openswan/ipsec_esp.h"
  28646. +#endif /* CONFIG_KLIPS_ESP */
  28647. +
  28648. +#include "openswan/ipsec_proto.h"
  28649. +#include "openswan/ipsec_alg.h"
  28650. +
  28651. +#ifdef CONFIG_KLIPS_DEBUG
  28652. +#define ESP_DMP(_x,_y,_z) if(debug_rcv && sysctl_ipsec_debug_verbose) ipsec_dmp_block(_x,_y,_z)
  28653. +#else
  28654. +#define ESP_DMP(_x,_y,_z)
  28655. +#endif
  28656. +
  28657. +#ifdef CONFIG_KLIPS_ESP
  28658. +enum ipsec_rcv_value
  28659. +ipsec_rcv_esp_checks(struct ipsec_rcv_state *irs,
  28660. + struct sk_buff *skb)
  28661. +{
  28662. + __u8 proto;
  28663. + int len; /* packet length */
  28664. +
  28665. + len = skb->len;
  28666. + proto = irs->ipp->protocol;
  28667. +
  28668. + /* XXX this will need to be 8 for IPv6 */
  28669. + if ((proto == IPPROTO_ESP) && ((len - irs->iphlen) % 4)) {
  28670. + printk("klips_error:ipsec_rcv: "
  28671. + "got packet with content length = %d from %s -- should be on 4 octet boundary, packet dropped\n",
  28672. + len - irs->iphlen,
  28673. + irs->ipsaddr_txt);
  28674. + if(irs->stats) {
  28675. + irs->stats->rx_errors++;
  28676. + }
  28677. + return IPSEC_RCV_BADLEN;
  28678. + }
  28679. +
  28680. + if(skb->len < (irs->hard_header_len + sizeof(struct iphdr) + sizeof(struct esphdr))) {
  28681. + KLIPS_PRINT(debug_rcv & DB_RX_INAU,
  28682. + "klips_debug:ipsec_rcv: "
  28683. + "runt esp packet of skb->len=%d received from %s, dropped.\n",
  28684. + skb->len,
  28685. + irs->ipsaddr_txt);
  28686. + if(irs->stats) {
  28687. + irs->stats->rx_errors++;
  28688. + }
  28689. + return IPSEC_RCV_BADLEN;
  28690. + }
  28691. +
  28692. + irs->protostuff.espstuff.espp = (struct esphdr *)skb->h.raw;
  28693. + irs->said.spi = irs->protostuff.espstuff.espp->esp_spi;
  28694. +
  28695. + return IPSEC_RCV_OK;
  28696. +}
  28697. +
  28698. +enum ipsec_rcv_value
  28699. +ipsec_rcv_esp_decrypt_setup(struct ipsec_rcv_state *irs,
  28700. + struct sk_buff *skb,
  28701. + __u32 *replay,
  28702. + unsigned char **authenticator)
  28703. +{
  28704. + struct esphdr *espp = irs->protostuff.espstuff.espp;
  28705. + //unsigned char *idat = (unsigned char *)espp;
  28706. +
  28707. + KLIPS_PRINT(debug_rcv,
  28708. + "klips_debug:ipsec_rcv: "
  28709. + "packet from %s received with seq=%d (iv)=0x%08x%08x iplen=%d esplen=%d sa=%s\n",
  28710. + irs->ipsaddr_txt,
  28711. + (__u32)ntohl(espp->esp_rpl),
  28712. + (__u32)ntohl(*((__u32 *)(espp->esp_iv) )),
  28713. + (__u32)ntohl(*((__u32 *)(espp->esp_iv) + 1)),
  28714. + irs->len,
  28715. + irs->ilen,
  28716. + irs->sa_len ? irs->sa : " (error)");
  28717. +
  28718. + *replay = ntohl(espp->esp_rpl);
  28719. + *authenticator = &(skb->h.raw[irs->ilen]);
  28720. +
  28721. + return IPSEC_RCV_OK;
  28722. +}
  28723. +
  28724. +enum ipsec_rcv_value
  28725. +ipsec_rcv_esp_authcalc(struct ipsec_rcv_state *irs,
  28726. + struct sk_buff *skb)
  28727. +{
  28728. + struct auth_alg *aa;
  28729. + struct esphdr *espp = irs->protostuff.espstuff.espp;
  28730. + union {
  28731. + MD5_CTX md5;
  28732. + SHA1_CTX sha1;
  28733. + } tctx;
  28734. +
  28735. + if (irs->ipsp->ips_alg_auth) {
  28736. + KLIPS_PRINT(debug_rcv,
  28737. + "klips_debug:ipsec_rcv: "
  28738. + "ipsec_alg hashing proto=%d... ",
  28739. + irs->said.proto);
  28740. + if(irs->said.proto == IPPROTO_ESP) {
  28741. + ipsec_alg_sa_esp_hash(irs->ipsp,
  28742. + (caddr_t)espp, irs->ilen,
  28743. + irs->hash, AHHMAC_HASHLEN);
  28744. + return IPSEC_RCV_OK;
  28745. + }
  28746. + return IPSEC_RCV_BADPROTO;
  28747. + }
  28748. + aa = irs->authfuncs;
  28749. +
  28750. + /* copy the initialized keying material */
  28751. + memcpy(&tctx, irs->ictx, irs->ictx_len);
  28752. +
  28753. +#ifdef HASH_DEBUG
  28754. + ESP_DMP("ictx", irs->ictx, irs->ictx_len);
  28755. +
  28756. + ESP_DMP("mac_esp", (caddr_t)espp, irs->ilen);
  28757. +#endif
  28758. + (*aa->update)((void *)&tctx, (caddr_t)espp, irs->ilen);
  28759. +
  28760. + (*aa->final)(irs->hash, (void *)&tctx);
  28761. +
  28762. +#ifdef HASH_DEBUG
  28763. + ESP_DMP("hash1", irs->hash, aa->hashlen);
  28764. +#endif
  28765. +
  28766. + memcpy(&tctx, irs->octx, irs->octx_len);
  28767. +
  28768. +#ifdef HASH_DEBUG
  28769. + ESP_DMP("octx", irs->octx, irs->octx_len);
  28770. +#endif
  28771. +
  28772. + (*aa->update)((void *)&tctx, irs->hash, aa->hashlen);
  28773. + (*aa->final)(irs->hash, (void *)&tctx);
  28774. +
  28775. + return IPSEC_RCV_OK;
  28776. +}
  28777. +
  28778. +
  28779. +enum ipsec_rcv_value
  28780. +ipsec_rcv_esp_decrypt(struct ipsec_rcv_state *irs)
  28781. +{
  28782. + struct ipsec_sa *ipsp = irs->ipsp;
  28783. + struct esphdr *espp = irs->protostuff.espstuff.espp;
  28784. + int i;
  28785. + int pad = 0, padlen;
  28786. + int badpad = 0;
  28787. + int esphlen = 0;
  28788. + __u8 *idat; /* pointer to content to be decrypted/authenticated */
  28789. + int encaplen = 0;
  28790. + struct sk_buff *skb;
  28791. + struct ipsec_alg_enc *ixt_e=NULL;
  28792. +
  28793. + skb=irs->skb;
  28794. +
  28795. + idat = skb->h.raw;
  28796. +
  28797. + /* encaplen is the distance between the end of the IP
  28798. + * header and the beginning of the ESP header.
  28799. + * on ESP headers it is zero, but on UDP-encap ESP
  28800. + * it includes the space for the UDP header.
  28801. + *
  28802. + * Note: UDP-encap code has already moved the
  28803. + * skb->data forward to accomodate this.
  28804. + */
  28805. + encaplen = idat - (skb->nh.raw + irs->iphlen);
  28806. +
  28807. + ixt_e=ipsp->ips_alg_enc;
  28808. + esphlen = ESP_HEADER_LEN + ixt_e->ixt_common.ixt_support.ias_ivlen/8;
  28809. + KLIPS_PRINT(debug_rcv,
  28810. + "klips_debug:ipsec_rcv: "
  28811. + "encalg=%d esphlen=%d\n",
  28812. + ipsp->ips_encalg, esphlen);
  28813. +
  28814. + idat += esphlen;
  28815. + irs->ilen -= esphlen;
  28816. +
  28817. + if (ipsec_alg_esp_encrypt(ipsp,
  28818. + idat, irs->ilen, espp->esp_iv,
  28819. + IPSEC_ALG_DECRYPT) <= 0) {
  28820. +#ifdef CONFIG_KLIPS_DEBUG
  28821. + KLIPS_ERROR(debug_rcv, "klips_error:ipsec_rcv: "
  28822. + "got packet with esplen = %d "
  28823. + "from %s -- should be on "
  28824. + "ENC(%d) octet boundary, "
  28825. + "packet dropped\n",
  28826. + irs->ilen,
  28827. + irs->ipsaddr_txt,
  28828. + ipsp->ips_encalg);
  28829. +#endif
  28830. + if(irs->stats) {
  28831. + irs->stats->rx_errors++;
  28832. + }
  28833. + return IPSEC_RCV_BAD_DECRYPT;
  28834. + }
  28835. +
  28836. + ESP_DMP("postdecrypt", idat, irs->ilen);
  28837. +
  28838. + irs->next_header = idat[irs->ilen - 1];
  28839. + padlen = idat[irs->ilen - 2];
  28840. + pad = padlen + 2 + irs->authlen;
  28841. +
  28842. + KLIPS_PRINT(debug_rcv & DB_RX_IPAD,
  28843. + "klips_debug:ipsec_rcv: "
  28844. + "padlen=%d, contents: 0x<offset>: 0x<value> 0x<value> ...\n",
  28845. + padlen);
  28846. +
  28847. + for (i = 1; i <= padlen; i++) {
  28848. + if((i % 16) == 1) {
  28849. + KLIPS_PRINT(debug_rcv & DB_RX_IPAD,
  28850. + "klips_debug: %02x:",
  28851. + i - 1);
  28852. + }
  28853. + KLIPS_PRINTMORE(debug_rcv & DB_RX_IPAD,
  28854. + " %02x",
  28855. + idat[irs->ilen - 2 - padlen + i - 1]);
  28856. + if(i != idat[irs->ilen - 2 - padlen + i - 1]) {
  28857. + badpad = 1;
  28858. + }
  28859. + if((i % 16) == 0) {
  28860. + KLIPS_PRINTMORE(debug_rcv & DB_RX_IPAD,
  28861. + "\n");
  28862. + }
  28863. + }
  28864. + if((i % 16) != 1) {
  28865. + KLIPS_PRINTMORE(debug_rcv & DB_RX_IPAD,
  28866. + "\n");
  28867. + }
  28868. + if(badpad) {
  28869. + KLIPS_PRINT(debug_rcv & DB_RX_IPAD,
  28870. + "klips_debug:ipsec_rcv: "
  28871. + "warning, decrypted packet from %s has bad padding\n",
  28872. + irs->ipsaddr_txt);
  28873. + KLIPS_PRINT(debug_rcv & DB_RX_IPAD,
  28874. + "klips_debug:ipsec_rcv: "
  28875. + "...may be bad decryption -- not dropped\n");
  28876. + ipsp->ips_errs.ips_encpad_errs += 1;
  28877. + }
  28878. +
  28879. + KLIPS_PRINT(debug_rcv & DB_RX_IPAD,
  28880. + "klips_debug:ipsec_rcv: "
  28881. + "packet decrypted from %s: next_header = %d, padding = %d\n",
  28882. + irs->ipsaddr_txt,
  28883. + irs->next_header,
  28884. + pad - 2 - irs->authlen);
  28885. +
  28886. + irs->ipp->tot_len = htons(ntohs(irs->ipp->tot_len) - (esphlen + pad));
  28887. +
  28888. + /*
  28889. + * move the IP header forward by the size of the ESP header, which
  28890. + * will remove the the ESP header from the packet.
  28891. + *
  28892. + * XXX this is really unnecessary, since odds we are in tunnel
  28893. + * mode, and we will be *removing* this IP header.
  28894. + *
  28895. + */
  28896. + memmove((void *)(idat - irs->iphlen),
  28897. + (void *)(skb->nh.raw), irs->iphlen);
  28898. +
  28899. + ESP_DMP("esp postmove", (idat - irs->iphlen),
  28900. + irs->iphlen + irs->ilen);
  28901. +
  28902. + /* skb_pull below, will move up by esphlen */
  28903. +
  28904. + /* XXX not clear how this can happen, as the message indicates */
  28905. + if(skb->len < esphlen) {
  28906. + printk(KERN_WARNING
  28907. + "klips_error:ipsec_rcv: "
  28908. + "tried to skb_pull esphlen=%d, %d available. This should never happen, please report.\n",
  28909. + esphlen, (int)(skb->len));
  28910. + return IPSEC_RCV_ESP_DECAPFAIL;
  28911. + }
  28912. + skb_pull(skb, esphlen);
  28913. + skb->nh.raw = idat - irs->iphlen;
  28914. + irs->ipp = skb->nh.iph;
  28915. +
  28916. + ESP_DMP("esp postpull", skb->data, skb->len);
  28917. +
  28918. + /* now, trip off the padding from the end */
  28919. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  28920. + "klips_debug:ipsec_rcv: "
  28921. + "trimming to %d.\n",
  28922. + irs->len - esphlen - pad);
  28923. + if(pad + esphlen <= irs->len) {
  28924. + skb_trim(skb, irs->len - esphlen - pad);
  28925. + } else {
  28926. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  28927. + "klips_debug:ipsec_rcv: "
  28928. + "bogus packet, size is zero or negative, dropping.\n");
  28929. + return IPSEC_RCV_DECAPFAIL;
  28930. + }
  28931. +
  28932. + return IPSEC_RCV_OK;
  28933. +}
  28934. +
  28935. +/*
  28936. + *
  28937. + */
  28938. +enum ipsec_xmit_value
  28939. +ipsec_xmit_esp_setup(struct ipsec_xmit_state *ixs)
  28940. +{
  28941. +#ifdef CONFIG_KLIPS_ENC_3DES
  28942. + __u32 iv[2];
  28943. +#endif
  28944. + struct esphdr *espp;
  28945. + int ilen = 0;
  28946. + int padlen = 0, i;
  28947. + unsigned char *dat;
  28948. + unsigned char *idat, *pad;
  28949. + __u8 hash[AH_AMAX];
  28950. + union {
  28951. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  28952. + MD5_CTX md5;
  28953. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  28954. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  28955. + SHA1_CTX sha1;
  28956. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  28957. + } tctx;
  28958. +
  28959. + dat = (unsigned char *)ixs->iph;
  28960. +
  28961. + espp = (struct esphdr *)(dat + ixs->iphlen);
  28962. + espp->esp_spi = ixs->ipsp->ips_said.spi;
  28963. + espp->esp_rpl = htonl(++(ixs->ipsp->ips_replaywin_lastseq));
  28964. +
  28965. + switch(ixs->ipsp->ips_encalg) {
  28966. +#if defined(CONFIG_KLIPS_ENC_3DES)
  28967. +#ifdef CONFIG_KLIPS_ENC_3DES
  28968. + case ESP_3DES:
  28969. +#endif /* CONFIG_KLIPS_ENC_3DES */
  28970. + iv[0] = *((__u32*)&(espp->esp_iv) ) =
  28971. + ((__u32*)(ixs->ipsp->ips_iv))[0];
  28972. + iv[1] = *((__u32*)&(espp->esp_iv) + 1) =
  28973. + ((__u32*)(ixs->ipsp->ips_iv))[1];
  28974. + break;
  28975. +#endif /* defined(CONFIG_KLIPS_ENC_3DES) */
  28976. + default:
  28977. + ixs->stats->tx_errors++;
  28978. + return IPSEC_XMIT_ESP_BADALG;
  28979. + }
  28980. +
  28981. + idat = dat + ixs->iphlen + sizeof(struct esphdr);
  28982. + ilen = ixs->skb->len - (ixs->iphlen + sizeof(struct esphdr) + ixs->authlen);
  28983. +
  28984. + /* Self-describing padding */
  28985. + pad = &dat[ixs->skb->len - ixs->tailroom];
  28986. + padlen = ixs->tailroom - 2 - ixs->authlen;
  28987. + for (i = 0; i < padlen; i++) {
  28988. + pad[i] = i + 1;
  28989. + }
  28990. + dat[ixs->skb->len - ixs->authlen - 2] = padlen;
  28991. +
  28992. + dat[ixs->skb->len - ixs->authlen - 1] = ixs->iph->protocol;
  28993. + ixs->iph->protocol = IPPROTO_ESP;
  28994. +
  28995. + switch(ixs->ipsp->ips_encalg) {
  28996. +#ifdef CONFIG_KLIPS_ENC_3DES
  28997. + case ESP_3DES:
  28998. + des_ede3_cbc_encrypt((des_cblock *)idat,
  28999. + (des_cblock *)idat,
  29000. + ilen,
  29001. + ((struct des_eks *)(ixs->ipsp->ips_key_e))[0].ks,
  29002. + ((struct des_eks *)(ixs->ipsp->ips_key_e))[1].ks,
  29003. + ((struct des_eks *)(ixs->ipsp->ips_key_e))[2].ks,
  29004. + (des_cblock *)iv, 1);
  29005. + break;
  29006. +#endif /* CONFIG_KLIPS_ENC_3DES */
  29007. + default:
  29008. + ixs->stats->tx_errors++;
  29009. + return IPSEC_XMIT_ESP_BADALG;
  29010. + }
  29011. +
  29012. + switch(ixs->ipsp->ips_encalg) {
  29013. +#if defined(CONFIG_KLIPS_ENC_3DES)
  29014. +#ifdef CONFIG_KLIPS_ENC_3DES
  29015. + case ESP_3DES:
  29016. +#endif /* CONFIG_KLIPS_ENC_3DES */
  29017. + /* XXX update IV with the last 8 octets of the encryption */
  29018. +#if KLIPS_IMPAIRMENT_ESPIV_CBC_ATTACK
  29019. + ((__u32*)(ixs->ipsp->ips_iv))[0] =
  29020. + ((__u32 *)(idat))[(ilen >> 2) - 2];
  29021. + ((__u32*)(ixs->ipsp->ips_iv))[1] =
  29022. + ((__u32 *)(idat))[(ilen >> 2) - 1];
  29023. +#else /* KLIPS_IMPAIRMENT_ESPIV_CBC_ATTACK */
  29024. + prng_bytes(&ipsec_prng, (char *)ixs->ipsp->ips_iv, EMT_ESPDES_IV_SZ);
  29025. +#endif /* KLIPS_IMPAIRMENT_ESPIV_CBC_ATTACK */
  29026. + break;
  29027. +#endif /* defined(CONFIG_KLIPS_ENC_3DES) */
  29028. + default:
  29029. + ixs->stats->tx_errors++;
  29030. + return IPSEC_XMIT_ESP_BADALG;
  29031. + }
  29032. +
  29033. + switch(ixs->ipsp->ips_authalg) {
  29034. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  29035. + case AH_MD5:
  29036. + ipsec_xmit_dmp("espp", (char*)espp, ixs->skb->len - ixs->iphlen - ixs->authlen);
  29037. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  29038. + ipsec_xmit_dmp("ictx", (char*)&tctx.md5, sizeof(tctx.md5));
  29039. + osMD5Update(&tctx.md5, (caddr_t)espp, ixs->skb->len - ixs->iphlen - ixs->authlen);
  29040. + ipsec_xmit_dmp("ictx+dat", (char*)&tctx.md5, sizeof(tctx.md5));
  29041. + osMD5Final(hash, &tctx.md5);
  29042. + ipsec_xmit_dmp("ictx hash", (char*)&hash, sizeof(hash));
  29043. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->octx;
  29044. + ipsec_xmit_dmp("octx", (char*)&tctx.md5, sizeof(tctx.md5));
  29045. + osMD5Update(&tctx.md5, hash, AHMD596_ALEN);
  29046. + ipsec_xmit_dmp("octx+hash", (char*)&tctx.md5, sizeof(tctx.md5));
  29047. + osMD5Final(hash, &tctx.md5);
  29048. + ipsec_xmit_dmp("octx hash", (char*)&hash, sizeof(hash));
  29049. + memcpy(&(dat[ixs->skb->len - ixs->authlen]), hash, ixs->authlen);
  29050. +
  29051. + /* paranoid */
  29052. + memset((caddr_t)&tctx.md5, 0, sizeof(tctx.md5));
  29053. + memset((caddr_t)hash, 0, sizeof(*hash));
  29054. + break;
  29055. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  29056. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  29057. + case AH_SHA:
  29058. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  29059. + SHA1Update(&tctx.sha1, (caddr_t)espp, ixs->skb->len - ixs->iphlen - ixs->authlen);
  29060. + SHA1Final(hash, &tctx.sha1);
  29061. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->octx;
  29062. + SHA1Update(&tctx.sha1, hash, AHSHA196_ALEN);
  29063. + SHA1Final(hash, &tctx.sha1);
  29064. + memcpy(&(dat[ixs->skb->len - ixs->authlen]), hash, ixs->authlen);
  29065. +
  29066. + /* paranoid */
  29067. + memset((caddr_t)&tctx.sha1, 0, sizeof(tctx.sha1));
  29068. + memset((caddr_t)hash, 0, sizeof(*hash));
  29069. + break;
  29070. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  29071. + case AH_NONE:
  29072. + break;
  29073. + default:
  29074. + ixs->stats->tx_errors++;
  29075. + return IPSEC_XMIT_AH_BADALG;
  29076. + }
  29077. +
  29078. + ixs->skb->h.raw = (unsigned char*)espp;
  29079. +
  29080. + return IPSEC_XMIT_OK;
  29081. +}
  29082. +
  29083. +
  29084. +struct xform_functions esp_xform_funcs[]={
  29085. + { rcv_checks: ipsec_rcv_esp_checks,
  29086. + rcv_setup_auth: ipsec_rcv_esp_decrypt_setup,
  29087. + rcv_calc_auth: ipsec_rcv_esp_authcalc,
  29088. + rcv_decrypt: ipsec_rcv_esp_decrypt,
  29089. +
  29090. + xmit_setup: ipsec_xmit_esp_setup,
  29091. + xmit_headroom: sizeof(struct esphdr),
  29092. + xmit_needtailroom: 1,
  29093. + },
  29094. +};
  29095. +
  29096. +#ifdef NET_26
  29097. +struct inet_protocol esp_protocol = {
  29098. + .handler = ipsec_rcv,
  29099. + .no_policy = 1,
  29100. +};
  29101. +#else
  29102. +struct inet_protocol esp_protocol =
  29103. +{
  29104. + ipsec_rcv, /* ESP handler */
  29105. + NULL, /* TUNNEL error control */
  29106. +#ifdef NETDEV_25
  29107. + 1, /* no policy */
  29108. +#else
  29109. + 0, /* next */
  29110. + IPPROTO_ESP, /* protocol ID */
  29111. + 0, /* copy */
  29112. + NULL, /* data */
  29113. + "ESP" /* name */
  29114. +#endif
  29115. +};
  29116. +#endif /* NET_26 */
  29117. +
  29118. +#endif /* !CONFIG_KLIPS_ESP */
  29119. +
  29120. +
  29121. +/*
  29122. + * $Log: ipsec_esp.c,v $
  29123. + * Revision 1.13.2.6 2006/10/06 21:39:26 paul
  29124. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  29125. + * set. This is defined through autoconf.h which is included through the
  29126. + * linux kernel build macros.
  29127. + *
  29128. + * Revision 1.13.2.5 2006/08/24 03:02:01 paul
  29129. + * Compile fixes for when CONFIG_KLIPS_DEBUG is not set. (bug #642)
  29130. + *
  29131. + * Revision 1.13.2.4 2006/05/06 03:07:38 ken
  29132. + * Pull in proper padsize->tailroom fix from #public
  29133. + * Need to do correct math on padlen since padsize is not equal to tailroom
  29134. + *
  29135. + * Revision 1.13.2.3 2006/05/05 03:58:04 ken
  29136. + * ixs->padsize becomes ixs->tailroom
  29137. + *
  29138. + * Revision 1.13.2.2 2006/05/01 14:36:03 mcr
  29139. + * use KLIPS_ERROR for fatal things.
  29140. + *
  29141. + * Revision 1.13.2.1 2006/04/20 16:33:06 mcr
  29142. + * remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  29143. + * Fix in-kernel module compilation. Sub-makefiles do not work.
  29144. + *
  29145. + * Revision 1.13 2005/05/21 03:19:57 mcr
  29146. + * hash ctx is not really that interesting most of the time.
  29147. + *
  29148. + * Revision 1.12 2005/05/11 01:28:49 mcr
  29149. + * removed "poor-man"s OOP in favour of proper C structures.
  29150. + *
  29151. + * Revision 1.11 2005/04/29 05:10:22 mcr
  29152. + * removed from extraenous includes to make unit testing easier.
  29153. + *
  29154. + * Revision 1.10 2005/04/17 04:36:14 mcr
  29155. + * code now deals with ESP and UDP-ESP code.
  29156. + *
  29157. + * Revision 1.9 2005/04/15 19:52:30 mcr
  29158. + * adjustments to use proper skb fields for data.
  29159. + *
  29160. + * Revision 1.8 2004/09/14 00:22:57 mcr
  29161. + * adjustment of MD5* functions.
  29162. + *
  29163. + * Revision 1.7 2004/09/13 02:23:01 mcr
  29164. + * #define inet_protocol if necessary.
  29165. + *
  29166. + * Revision 1.6 2004/09/06 18:35:49 mcr
  29167. + * 2.6.8.1 gets rid of inet_protocol->net_protocol compatibility,
  29168. + * so adjust for that.
  29169. + *
  29170. + * Revision 1.5 2004/08/17 03:27:23 mcr
  29171. + * klips 2.6 edits.
  29172. + *
  29173. + * Revision 1.4 2004/08/04 15:57:07 mcr
  29174. + * moved des .h files to include/des/ *
  29175. + * included 2.6 protocol specific things
  29176. + * started at NAT-T support, but it will require a kernel patch.
  29177. + *
  29178. + * Revision 1.3 2004/07/10 19:11:18 mcr
  29179. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  29180. + *
  29181. + * Revision 1.2 2004/04/06 02:49:25 mcr
  29182. + * pullup of algo code from alg-branch.
  29183. + *
  29184. + *
  29185. + *
  29186. + */
  29187. --- /dev/null Tue Mar 11 13:02:56 2003
  29188. +++ linux/net/ipsec/ipsec_init.c Mon Feb 9 13:51:03 2004
  29189. @@ -0,0 +1,614 @@
  29190. +/*
  29191. + * @(#) Initialization code.
  29192. + * Copyright (C) 1996, 1997 John Ioannidis.
  29193. + * Copyright (C) 1998 - 2002 Richard Guy Briggs <rgb@freeswan.org>
  29194. + * 2001 - 2004 Michael Richardson <mcr@xelerance.com>
  29195. + *
  29196. + * This program is free software; you can redistribute it and/or modify it
  29197. + * under the terms of the GNU General Public License as published by the
  29198. + * Free Software Foundation; either version 2 of the License, or (at your
  29199. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  29200. + *
  29201. + * This program is distributed in the hope that it will be useful, but
  29202. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  29203. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  29204. + * for more details.
  29205. + *
  29206. + * /proc system code was split out into ipsec_proc.c after rev. 1.70.
  29207. + *
  29208. + */
  29209. +
  29210. +char ipsec_init_c_version[] = "RCSID $Id: ipsec_init.c,v 1.104.2.4 2006/10/06 21:39:26 paul Exp $";
  29211. +
  29212. +#ifndef AUTOCONF_INCLUDED
  29213. +#include <linux/config.h>
  29214. +#endif
  29215. +#include <linux/version.h>
  29216. +#include <linux/module.h>
  29217. +#include <linux/kernel.h> /* printk() */
  29218. +
  29219. +#include "openswan/ipsec_param.h"
  29220. +
  29221. +#ifdef MALLOC_SLAB
  29222. +# include <linux/slab.h> /* kmalloc() */
  29223. +#else /* MALLOC_SLAB */
  29224. +# include <linux/malloc.h> /* kmalloc() */
  29225. +#endif /* MALLOC_SLAB */
  29226. +#include <linux/errno.h> /* error codes */
  29227. +#include <linux/types.h> /* size_t */
  29228. +#include <linux/interrupt.h> /* mark_bh */
  29229. +
  29230. +#include <linux/netdevice.h> /* struct device, and other headers */
  29231. +#include <linux/etherdevice.h> /* eth_type_trans */
  29232. +#include <linux/ip.h> /* struct iphdr */
  29233. +#include <linux/in.h> /* struct sockaddr_in */
  29234. +#include <linux/skbuff.h>
  29235. +#include <linux/random.h> /* get_random_bytes() */
  29236. +#include <net/protocol.h>
  29237. +
  29238. +#include <openswan.h>
  29239. +
  29240. +#ifdef SPINLOCK
  29241. +# ifdef SPINLOCK_23
  29242. +# include <linux/spinlock.h> /* *lock* */
  29243. +# else /* 23_SPINLOCK */
  29244. +# include <asm/spinlock.h> /* *lock* */
  29245. +# endif /* 23_SPINLOCK */
  29246. +#endif /* SPINLOCK */
  29247. +
  29248. +#include <net/ip.h>
  29249. +
  29250. +#ifdef CONFIG_PROC_FS
  29251. +# include <linux/proc_fs.h>
  29252. +#endif /* CONFIG_PROC_FS */
  29253. +
  29254. +#ifdef NETLINK_SOCK
  29255. +# include <linux/netlink.h>
  29256. +#else
  29257. +# include <net/netlink.h>
  29258. +#endif
  29259. +
  29260. +#include "openswan/radij.h"
  29261. +
  29262. +#include "openswan/ipsec_life.h"
  29263. +#include "openswan/ipsec_stats.h"
  29264. +#include "openswan/ipsec_sa.h"
  29265. +
  29266. +#include "openswan/ipsec_encap.h"
  29267. +#include "openswan/ipsec_radij.h"
  29268. +#include "openswan/ipsec_xform.h"
  29269. +#include "openswan/ipsec_tunnel.h"
  29270. +
  29271. +#include "openswan/ipsec_rcv.h"
  29272. +#include "openswan/ipsec_ah.h"
  29273. +#include "openswan/ipsec_esp.h"
  29274. +
  29275. +#ifdef CONFIG_KLIPS_IPCOMP
  29276. +# include "openswan/ipcomp.h"
  29277. +#endif /* CONFIG_KLIPS_IPCOMP */
  29278. +
  29279. +#include "openswan/ipsec_proto.h"
  29280. +#include "openswan/ipsec_alg.h"
  29281. +
  29282. +#include <pfkeyv2.h>
  29283. +#include <pfkey.h>
  29284. +
  29285. +#if defined(NET_26) && defined(CONFIG_IPSEC_NAT_TRAVERSAL)
  29286. +#include <net/xfrmudp.h>
  29287. +#endif
  29288. +
  29289. +#if defined(NET_26) && defined(CONFIG_IPSEC_NAT_TRAVERSAL) && !defined(HAVE_XFRM4_UDP_REGISTER)
  29290. +#warning "You are trying to build KLIPS2.6 with NAT-T support, but you did not"
  29291. +#error "properly apply the NAT-T patch to your 2.6 kernel source tree."
  29292. +#endif
  29293. +
  29294. +#if !defined(CONFIG_KLIPS_ESP) && !defined(CONFIG_KLIPS_AH)
  29295. +#error "kernel configuration must include ESP or AH"
  29296. +#endif
  29297. +
  29298. +/*
  29299. + * seems to be present in 2.4.10 (Linus), but also in some RH and other
  29300. + * distro kernels of a lower number.
  29301. + */
  29302. +#ifdef MODULE_LICENSE
  29303. +MODULE_LICENSE("GPL");
  29304. +#endif
  29305. +
  29306. +#ifdef CONFIG_KLIPS_DEBUG
  29307. +int debug_eroute = 0;
  29308. +int debug_spi = 0;
  29309. +int debug_netlink = 0;
  29310. +#endif /* CONFIG_KLIPS_DEBUG */
  29311. +
  29312. +struct prng ipsec_prng;
  29313. +
  29314. +
  29315. +#if defined(NET_26) && defined(CONFIG_IPSEC_NAT_TRAVERSAL)
  29316. +xfrm4_rcv_encap_t klips_old_encap = NULL;
  29317. +#endif
  29318. +
  29319. +extern int ipsec_device_event(struct notifier_block *dnot, unsigned long event, void *ptr);
  29320. +/*
  29321. + * the following structure is required so that we receive
  29322. + * event notifications when network devices are enabled and
  29323. + * disabled (ifconfig up and down).
  29324. + */
  29325. +static struct notifier_block ipsec_dev_notifier={
  29326. + ipsec_device_event,
  29327. + NULL,
  29328. + 0
  29329. +};
  29330. +
  29331. +#ifdef CONFIG_SYSCTL
  29332. +extern int ipsec_sysctl_register(void);
  29333. +extern void ipsec_sysctl_unregister(void);
  29334. +#endif
  29335. +
  29336. +#if defined(NET_26) || defined(IPSKB_XFRM_TUNNEL_SIZE)
  29337. +static inline int
  29338. +openswan_inet_add_protocol(struct inet_protocol *prot, unsigned protocol)
  29339. +{
  29340. + return inet_add_protocol(prot, protocol);
  29341. +}
  29342. +
  29343. +static inline int
  29344. +openswan_inet_del_protocol(struct inet_protocol *prot, unsigned protocol)
  29345. +{
  29346. + return inet_del_protocol(prot, protocol);
  29347. +}
  29348. +
  29349. +#else
  29350. +static inline int
  29351. +openswan_inet_add_protocol(struct inet_protocol *prot, unsigned protocol)
  29352. +{
  29353. + inet_add_protocol(prot);
  29354. + return 0;
  29355. +}
  29356. +
  29357. +static inline int
  29358. +openswan_inet_del_protocol(struct inet_protocol *prot, unsigned protocol)
  29359. +{
  29360. + inet_del_protocol(prot);
  29361. + return 0;
  29362. +}
  29363. +
  29364. +#endif
  29365. +
  29366. +/* void */
  29367. +int
  29368. +ipsec_klips_init(void)
  29369. +{
  29370. + int error = 0;
  29371. + unsigned char seed[256];
  29372. +#ifdef CONFIG_KLIPS_ENC_3DES
  29373. + extern int des_check_key;
  29374. +
  29375. + /* turn off checking of keys */
  29376. + des_check_key=0;
  29377. +#endif /* CONFIG_KLIPS_ENC_3DES */
  29378. +
  29379. + KLIPS_PRINT(1, "klips_info:ipsec_init: "
  29380. + "KLIPS startup, Openswan KLIPS IPsec stack version: %s\n",
  29381. + ipsec_version_code());
  29382. +
  29383. + error |= ipsec_proc_init();
  29384. +
  29385. +#ifdef SPINLOCK
  29386. + ipsec_sadb.sadb_lock = SPIN_LOCK_UNLOCKED;
  29387. +#else /* SPINLOCK */
  29388. + ipsec_sadb.sadb_lock = 0;
  29389. +#endif /* SPINLOCK */
  29390. +
  29391. +#ifndef SPINLOCK
  29392. + tdb_lock.lock = 0;
  29393. + eroute_lock.lock = 0;
  29394. +#endif /* !SPINLOCK */
  29395. +
  29396. + error |= ipsec_sadb_init();
  29397. + error |= ipsec_radijinit();
  29398. +
  29399. + error |= pfkey_init();
  29400. +
  29401. + error |= register_netdevice_notifier(&ipsec_dev_notifier);
  29402. +
  29403. +#ifdef CONFIG_KLIPS_ESP
  29404. + openswan_inet_add_protocol(&esp_protocol, IPPROTO_ESP);
  29405. +#endif /* CONFIG_KLIPS_ESP */
  29406. +
  29407. +#ifdef CONFIG_KLIPS_AH
  29408. + openswan_inet_add_protocol(&ah_protocol, IPPROTO_AH);
  29409. +#endif /* CONFIG_KLIPS_AH */
  29410. +
  29411. +/* we never actually link IPCOMP to the stack */
  29412. +#ifdef IPCOMP_USED_ALONE
  29413. +#ifdef CONFIG_KLIPS_IPCOMP
  29414. + openswan_inet_add_protocol(&comp_protocol, IPPROTO_COMP);
  29415. +#endif /* CONFIG_KLIPS_IPCOMP */
  29416. +#endif
  29417. +
  29418. + error |= ipsec_tunnel_init_devices();
  29419. +
  29420. +#if defined(NET_26) && defined(CONFIG_IPSEC_NAT_TRAVERSAL)
  29421. + /* register our ESP-UDP handler */
  29422. + if(udp4_register_esp_rcvencap(klips26_rcv_encap
  29423. + , &klips_old_encap)!=0) {
  29424. + printk(KERN_ERR "KLIPS: can not register klips_rcv_encap function\n");
  29425. + }
  29426. +#endif
  29427. +
  29428. +
  29429. +#ifdef CONFIG_SYSCTL
  29430. + error |= ipsec_sysctl_register();
  29431. +#endif
  29432. +
  29433. + ipsec_alg_init();
  29434. +
  29435. + get_random_bytes((void *)seed, sizeof(seed));
  29436. + prng_init(&ipsec_prng, seed, sizeof(seed));
  29437. +
  29438. + return error;
  29439. +}
  29440. +
  29441. +
  29442. +/* void */
  29443. +int
  29444. +ipsec_cleanup(void)
  29445. +{
  29446. + int error = 0;
  29447. +
  29448. +#ifdef CONFIG_SYSCTL
  29449. + ipsec_sysctl_unregister();
  29450. +#endif
  29451. +#if defined(NET_26) && defined(CONFIG_IPSEC_NAT_TRAVERSAL)
  29452. + if(udp4_unregister_esp_rcvencap(klips_old_encap) < 0) {
  29453. + printk(KERN_ERR "KLIPS: can not unregister klips_rcv_encap function\n");
  29454. + }
  29455. +#endif
  29456. +
  29457. + KLIPS_PRINT(debug_netlink, /* debug_tunnel & DB_TN_INIT, */
  29458. + "klips_debug:ipsec_cleanup: "
  29459. + "calling ipsec_tunnel_cleanup_devices.\n");
  29460. + error |= ipsec_tunnel_cleanup_devices();
  29461. +
  29462. + KLIPS_PRINT(debug_netlink, "called ipsec_tunnel_cleanup_devices");
  29463. +
  29464. +/* we never actually link IPCOMP to the stack */
  29465. +#ifdef IPCOMP_USED_ALONE
  29466. +#ifdef CONFIG_KLIPS_IPCOMP
  29467. + if (openswan_inet_del_protocol(&comp_protocol, IPPROTO_COMP) < 0)
  29468. + printk(KERN_INFO "klips_debug:ipsec_cleanup: "
  29469. + "comp close: can't remove protocol\n");
  29470. +#endif /* CONFIG_KLIPS_IPCOMP */
  29471. +#endif /* IPCOMP_USED_ALONE */
  29472. +
  29473. +#ifdef CONFIG_KLIPS_AH
  29474. + if (openswan_inet_del_protocol(&ah_protocol, IPPROTO_AH) < 0)
  29475. + printk(KERN_INFO "klips_debug:ipsec_cleanup: "
  29476. + "ah close: can't remove protocol\n");
  29477. +#endif /* CONFIG_KLIPS_AH */
  29478. +
  29479. +#ifdef CONFIG_KLIPS_ESP
  29480. + if (openswan_inet_del_protocol(&esp_protocol, IPPROTO_ESP) < 0)
  29481. + printk(KERN_INFO "klips_debug:ipsec_cleanup: "
  29482. + "esp close: can't remove protocol\n");
  29483. +#endif /* CONFIG_KLIPS_ESP */
  29484. +
  29485. + error |= unregister_netdevice_notifier(&ipsec_dev_notifier);
  29486. +
  29487. + KLIPS_PRINT(debug_netlink, /* debug_tunnel & DB_TN_INIT, */
  29488. + "klips_debug:ipsec_cleanup: "
  29489. + "calling ipsec_sadb_cleanup.\n");
  29490. + error |= ipsec_sadb_cleanup(0);
  29491. + error |= ipsec_sadb_free();
  29492. +
  29493. + KLIPS_PRINT(debug_netlink, /* debug_tunnel & DB_TN_INIT, */
  29494. + "klips_debug:ipsec_cleanup: "
  29495. + "calling ipsec_radijcleanup.\n");
  29496. + error |= ipsec_radijcleanup();
  29497. +
  29498. + KLIPS_PRINT(debug_pfkey, /* debug_tunnel & DB_TN_INIT, */
  29499. + "klips_debug:ipsec_cleanup: "
  29500. + "calling pfkey_cleanup.\n");
  29501. + error |= pfkey_cleanup();
  29502. +
  29503. + ipsec_proc_cleanup();
  29504. +
  29505. + prng_final(&ipsec_prng);
  29506. +
  29507. + return error;
  29508. +}
  29509. +
  29510. +#ifdef MODULE
  29511. +int
  29512. +init_module(void)
  29513. +{
  29514. + int error = 0;
  29515. +
  29516. + error |= ipsec_klips_init();
  29517. +
  29518. + return error;
  29519. +}
  29520. +
  29521. +void
  29522. +cleanup_module(void)
  29523. +{
  29524. + KLIPS_PRINT(debug_netlink, /* debug_tunnel & DB_TN_INIT, */
  29525. + "klips_debug:cleanup_module: "
  29526. + "calling ipsec_cleanup.\n");
  29527. +
  29528. + ipsec_cleanup();
  29529. +
  29530. + KLIPS_PRINT(1, "klips_info:cleanup_module: "
  29531. + "ipsec module unloaded.\n");
  29532. +}
  29533. +#endif /* MODULE */
  29534. +
  29535. +/*
  29536. + * $Log: ipsec_init.c,v $
  29537. + * Revision 1.104.2.4 2006/10/06 21:39:26 paul
  29538. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  29539. + * set. This is defined through autoconf.h which is included through the
  29540. + * linux kernel build macros.
  29541. + *
  29542. + * Revision 1.104.2.3 2006/07/31 15:25:20 paul
  29543. + * Check for NETKEY backport in Debian using IPSKB_XFRM_TUNNEL_SIZE to
  29544. + * determine wether inet_add_protocol needs the protocol argument.
  29545. + *
  29546. + * Revision 1.104.2.2 2006/04/20 16:33:06 mcr
  29547. + * remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  29548. + * Fix in-kernel module compilation. Sub-makefiles do not work.
  29549. + *
  29550. + * Revision 1.104.2.1 2005/08/12 01:18:20 ken
  29551. + * Warn people who don't have NAT-T patch applied, but try and compile NAT-T code
  29552. + *
  29553. + * Revision 1.105 2005/08/12 00:56:33 mcr
  29554. + * add warning for people who didn't apply nat-t patch.
  29555. + *
  29556. + * Revision 1.104 2005/07/08 15:51:41 mcr
  29557. + * removed duplicate NAT-T code.
  29558. + * if CONFIG_IPSEC_NAT_TRAVERSAL isn't defined, then there is no issue.
  29559. + *
  29560. + * Revision 1.103 2005/07/08 03:02:05 paul
  29561. + * Fixed garbled define that accidentally got commited to the real tree.
  29562. + *
  29563. + * Revision 1.102 2005/07/08 02:56:37 paul
  29564. + * gcc4 fixes that were not commited because vault was down
  29565. + *
  29566. + * Revision 1.101 2005/04/29 05:10:22 mcr
  29567. + * removed from extraenous includes to make unit testing easier.
  29568. + *
  29569. + * Revision 1.100 2005/04/10 22:56:09 mcr
  29570. + * change to udp.c registration API.
  29571. + *
  29572. + * Revision 1.99 2005/04/08 18:26:13 mcr
  29573. + * register with udp.c, the klips26 encap receive function
  29574. + *
  29575. + * Revision 1.98 2004/09/13 02:23:18 mcr
  29576. + * #define inet_protocol if necessary.
  29577. + *
  29578. + * Revision 1.97 2004/09/06 18:35:49 mcr
  29579. + * 2.6.8.1 gets rid of inet_protocol->net_protocol compatibility,
  29580. + * so adjust for that.
  29581. + *
  29582. + * Revision 1.96 2004/08/17 03:27:23 mcr
  29583. + * klips 2.6 edits.
  29584. + *
  29585. + * Revision 1.95 2004/08/03 18:19:08 mcr
  29586. + * in 2.6, use "net_device" instead of #define device->net_device.
  29587. + * this probably breaks 2.0 compiles.
  29588. + *
  29589. + * Revision 1.94 2004/07/10 19:11:18 mcr
  29590. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  29591. + *
  29592. + * Revision 1.93 2004/04/06 02:49:26 mcr
  29593. + * pullup of algo code from alg-branch.
  29594. + *
  29595. + * Revision 1.92 2004/03/30 15:30:39 ken
  29596. + * Proper Capitalization
  29597. + *
  29598. + * Revision 1.91 2004/03/22 01:51:51 ken
  29599. + * We are open
  29600. + *
  29601. + * Revision 1.90.4.2 2004/04/05 04:30:46 mcr
  29602. + * patches for alg-branch to compile/work with 2.x openswan
  29603. + *
  29604. + * Revision 1.90.4.1 2003/12/22 15:25:52 jjo
  29605. + * Merged algo-0.8.1-rc11-test1 into alg-branch
  29606. + *
  29607. + * Revision 1.90 2003/10/31 02:27:55 mcr
  29608. + * pulled up port-selector patches and sa_id elimination.
  29609. + *
  29610. + * Revision 1.89.4.1 2003/10/29 01:30:41 mcr
  29611. + * elimited "struct sa_id".
  29612. + *
  29613. + * Revision 1.89 2003/07/31 22:47:16 mcr
  29614. + * preliminary (untested by FS-team) 2.5 patches.
  29615. + *
  29616. + * Revision 1.88 2003/06/22 20:05:36 mcr
  29617. + * clarified why IPCOMP was not being registered, and put a new
  29618. + * #ifdef in rather than #if 0.
  29619. + *
  29620. + * Revision 1.87 2002/09/20 15:40:51 rgb
  29621. + * Added a lock to the global ipsec_sadb struct for future use.
  29622. + * Split ipsec_sadb_cleanup from new funciton ipsec_sadb_free to avoid problem
  29623. + * of freeing newly created structures when clearing the reftable upon startup
  29624. + * to start from a known state.
  29625. + *
  29626. + * Revision 1.86 2002/08/15 18:39:15 rgb
  29627. + * Move ipsec_prng outside debug code.
  29628. + *
  29629. + * Revision 1.85 2002/05/14 02:35:29 rgb
  29630. + * Change reference to tdb to ipsa.
  29631. + *
  29632. + * Revision 1.84 2002/04/24 07:55:32 mcr
  29633. + * #include patches and Makefiles for post-reorg compilation.
  29634. + *
  29635. + * Revision 1.83 2002/04/24 07:36:28 mcr
  29636. + * Moved from ./klips/net/ipsec/ipsec_init.c,v
  29637. + *
  29638. + * Revision 1.82 2002/04/20 00:12:25 rgb
  29639. + * Added esp IV CBC attack fix, disabled.
  29640. + *
  29641. + * Revision 1.81 2002/04/09 16:13:32 mcr
  29642. + * switch license to straight GPL.
  29643. + *
  29644. + * Revision 1.80 2002/03/24 07:34:08 rgb
  29645. + * Sanity check for at least one of AH or ESP configured.
  29646. + *
  29647. + * Revision 1.79 2002/02/05 22:55:15 mcr
  29648. + * added MODULE_LICENSE declaration.
  29649. + * This macro does not appear in all kernel versions (see comment).
  29650. + *
  29651. + * Revision 1.78 2002/01/29 17:17:55 mcr
  29652. + * moved include of ipsec_param.h to after include of linux/kernel.h
  29653. + * otherwise, it seems that some option that is set in ipsec_param.h
  29654. + * screws up something subtle in the include path to kernel.h, and
  29655. + * it complains on the snprintf() prototype.
  29656. + *
  29657. + * Revision 1.77 2002/01/29 04:00:51 mcr
  29658. + * more excise of kversions.h header.
  29659. + *
  29660. + * Revision 1.76 2002/01/29 02:13:17 mcr
  29661. + * introduction of ipsec_kversion.h means that include of
  29662. + * ipsec_param.h must preceed any decisions about what files to
  29663. + * include to deal with differences in kernel source.
  29664. + *
  29665. + * Revision 1.75 2001/11/26 09:23:48 rgb
  29666. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  29667. + *
  29668. + * Revision 1.74 2001/11/22 05:44:11 henry
  29669. + * new version stuff
  29670. + *
  29671. + * Revision 1.71.2.2 2001/10/22 20:51:00 mcr
  29672. + * explicitely set des_check_key.
  29673. + *
  29674. + * Revision 1.71.2.1 2001/09/25 02:19:39 mcr
  29675. + * /proc manipulation code moved to new ipsec_proc.c
  29676. + *
  29677. + * Revision 1.73 2001/11/06 19:47:17 rgb
  29678. + * Changed lifetime_packets to uint32 from uint64.
  29679. + *
  29680. + * Revision 1.72 2001/10/18 04:45:19 rgb
  29681. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  29682. + * lib/freeswan.h version macros moved to lib/kversions.h.
  29683. + * Other compiler directive cleanups.
  29684. + *
  29685. + * Revision 1.71 2001/09/20 15:32:45 rgb
  29686. + * Minor pfkey lifetime fixes.
  29687. + *
  29688. + * Revision 1.70 2001/07/06 19:51:21 rgb
  29689. + * Added inbound policy checking code for IPIP SAs.
  29690. + *
  29691. + * Revision 1.69 2001/06/14 19:33:26 rgb
  29692. + * Silence startup message for console, but allow it to be logged.
  29693. + * Update copyright date.
  29694. + *
  29695. + * Revision 1.68 2001/05/29 05:14:36 rgb
  29696. + * Added PMTU to /proc/net/ipsec_tncfg output. See 'man 5 ipsec_tncfg'.
  29697. + *
  29698. + * Revision 1.67 2001/05/04 16:34:52 rgb
  29699. + * Rremove erroneous checking of return codes for proc_net_* in 2.4.
  29700. + *
  29701. + * Revision 1.66 2001/05/03 19:40:34 rgb
  29702. + * Check error return codes in startup and shutdown.
  29703. + *
  29704. + * Revision 1.65 2001/02/28 05:03:27 rgb
  29705. + * Clean up and rationalise startup messages.
  29706. + *
  29707. + * Revision 1.64 2001/02/27 22:24:53 rgb
  29708. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  29709. + * Check for satoa() return codes.
  29710. + *
  29711. + * Revision 1.63 2000/11/29 20:14:06 rgb
  29712. + * Add src= to the output of /proc/net/ipsec_spi and delete dst from IPIP.
  29713. + *
  29714. + * Revision 1.62 2000/11/06 04:31:24 rgb
  29715. + * Ditched spin_lock_irqsave in favour of spin_lock_bh.
  29716. + * Fixed longlong for pre-2.4 kernels (Svenning).
  29717. + * Add Svenning's adaptive content compression.
  29718. + * Disabled registration of ipcomp handler.
  29719. + *
  29720. + * Revision 1.61 2000/10/11 13:37:54 rgb
  29721. + * #ifdef out debug print that causes proc/net/ipsec_version to oops.
  29722. + *
  29723. + * Revision 1.60 2000/09/20 03:59:01 rgb
  29724. + * Change static info functions to DEBUG_NO_STATIC to reveal function names
  29725. + * in oopsen.
  29726. + *
  29727. + * Revision 1.59 2000/09/16 01:06:26 rgb
  29728. + * Added cast of var to silence compiler warning about long fed to int
  29729. + * format.
  29730. + *
  29731. + * Revision 1.58 2000/09/15 11:37:01 rgb
  29732. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  29733. + * IPCOMP zlib deflate code.
  29734. + *
  29735. + * Revision 1.57 2000/09/12 03:21:50 rgb
  29736. + * Moved radij_c_version printing to ipsec_version_get_info().
  29737. + * Reformatted ipsec_version_get_info().
  29738. + * Added sysctl_{,un}register() calls.
  29739. + *
  29740. + * Revision 1.56 2000/09/08 19:16:50 rgb
  29741. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  29742. + * Removed all references to CONFIG_IPSEC_PFKEYv2.
  29743. + *
  29744. + * Revision 1.55 2000/08/30 05:19:03 rgb
  29745. + * Cleaned up no longer used spi_next, netlink register/unregister, other
  29746. + * minor cleanup.
  29747. + * Removed cruft replaced by TDB_XFORM_NAME.
  29748. + * Removed all the rest of the references to tdb_spi, tdb_proto, tdb_dst.
  29749. + * Moved debug version strings to printk when /proc/net/ipsec_version is
  29750. + * called.
  29751. + *
  29752. + * Revision 1.54 2000/08/20 18:31:05 rgb
  29753. + * Changed cosmetic alignment in spi_info.
  29754. + * Changed addtime and usetime to use actual value which is relative
  29755. + * anyways, as intended. (Momchil)
  29756. + *
  29757. + * Revision 1.53 2000/08/18 17:37:03 rgb
  29758. + * Added an (int) cast to shut up the compiler...
  29759. + *
  29760. + * Revision 1.52 2000/08/01 14:51:50 rgb
  29761. + * Removed _all_ remaining traces of DES.
  29762. + *
  29763. + * Revision 1.51 2000/07/25 20:41:22 rgb
  29764. + * Removed duplicate parameter in spi_getinfo.
  29765. + *
  29766. + * Revision 1.50 2000/07/17 03:21:45 rgb
  29767. + * Removed /proc/net/ipsec_spinew.
  29768. + *
  29769. + * Revision 1.49 2000/06/28 05:46:51 rgb
  29770. + * Renamed ivlen to iv_bits for consistency.
  29771. + * Changed output of add and use times to be relative to now.
  29772. + *
  29773. + * Revision 1.48 2000/05/11 18:26:10 rgb
  29774. + * Commented out calls to netlink_attach/detach to avoid activating netlink
  29775. + * in the kenrel config.
  29776. + *
  29777. + * Revision 1.47 2000/05/10 22:35:26 rgb
  29778. + * Comment out most of the startup version information.
  29779. + *
  29780. + * Revision 1.46 2000/03/22 16:15:36 rgb
  29781. + * Fixed renaming of dev_get (MB).
  29782. + *
  29783. + * Revision 1.45 2000/03/16 06:40:48 rgb
  29784. + * Hardcode PF_KEYv2 support.
  29785. + *
  29786. + * Revision 1.44 2000/01/22 23:19:20 rgb
  29787. + * Simplified code to use existing macro TDB_XFORM_NAME().
  29788. + *
  29789. + * Revision 1.43 2000/01/21 06:14:04 rgb
  29790. + * Print individual stats only if non-zero.
  29791. + * Removed 'bits' from each keylength for brevity.
  29792. + * Shortened lifetimes legend for brevity.
  29793. + * Changed wording from 'last_used' to the clearer 'idle'.
  29794. + *
  29795. + * Revision 1.42 1999/12/31 14:57:19 rgb
  29796. + * MB fix for new dummy-less proc_get_info in 2.3.35.
  29797. + *
  29798. + *
  29799. + * Local variables:
  29800. + * c-file-style: "linux"
  29801. + * End:
  29802. + *
  29803. + */
  29804. --- /dev/null Tue Mar 11 13:02:56 2003
  29805. +++ linux/net/ipsec/ipsec_ipcomp.c Mon Feb 9 13:51:03 2004
  29806. @@ -0,0 +1,256 @@
  29807. +/*
  29808. + * processing code for IPCOMP
  29809. + * Copyright (C) 2003 Michael Richardson <mcr@sandelman.ottawa.on.ca>
  29810. + *
  29811. + * This program is free software; you can redistribute it and/or modify it
  29812. + * under the terms of the GNU General Public License as published by the
  29813. + * Free Software Foundation; either version 2 of the License, or (at your
  29814. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  29815. + *
  29816. + * This program is distributed in the hope that it will be useful, but
  29817. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  29818. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  29819. + * for more details.
  29820. + */
  29821. +
  29822. +char ipsec_ipcomp_c_version[] = "RCSID $Id: ipsec_ipcomp.c,v 1.5.2.2 2006/10/06 21:39:26 paul Exp $";
  29823. +#ifndef AUTOCONF_INCLUDED
  29824. +#include <linux/config.h>
  29825. +#endif
  29826. +#include <linux/version.h>
  29827. +
  29828. +#define __NO_VERSION__
  29829. +#include <linux/module.h>
  29830. +#include <linux/kernel.h> /* printk() */
  29831. +
  29832. +#include "openswan/ipsec_param.h"
  29833. +
  29834. +#ifdef MALLOC_SLAB
  29835. +# include <linux/slab.h> /* kmalloc() */
  29836. +#else /* MALLOC_SLAB */
  29837. +# include <linux/malloc.h> /* kmalloc() */
  29838. +#endif /* MALLOC_SLAB */
  29839. +#include <linux/errno.h> /* error codes */
  29840. +#include <linux/types.h> /* size_t */
  29841. +#include <linux/interrupt.h> /* mark_bh */
  29842. +
  29843. +#include <linux/netdevice.h> /* struct device, and other headers */
  29844. +#include <linux/etherdevice.h> /* eth_type_trans */
  29845. +#include <linux/ip.h> /* struct iphdr */
  29846. +#include <linux/skbuff.h>
  29847. +#include <openswan.h>
  29848. +#ifdef SPINLOCK
  29849. +# ifdef SPINLOCK_23
  29850. +# include <linux/spinlock.h> /* *lock* */
  29851. +# else /* SPINLOCK_23 */
  29852. +# include <asm/spinlock.h> /* *lock* */
  29853. +# endif /* SPINLOCK_23 */
  29854. +#endif /* SPINLOCK */
  29855. +
  29856. +#include <net/ip.h>
  29857. +
  29858. +#include "openswan/radij.h"
  29859. +#include "openswan/ipsec_encap.h"
  29860. +#include "openswan/ipsec_sa.h"
  29861. +
  29862. +#include "openswan/ipsec_radij.h"
  29863. +#include "openswan/ipsec_xform.h"
  29864. +#include "openswan/ipsec_tunnel.h"
  29865. +#include "openswan/ipsec_rcv.h"
  29866. +#include "openswan/ipsec_xmit.h"
  29867. +
  29868. +#include "openswan/ipsec_auth.h"
  29869. +
  29870. +#ifdef CONFIG_KLIPS_IPCOMP
  29871. +#include "openswan/ipsec_ipcomp.h"
  29872. +#endif /* CONFIG_KLIPS_IPCOMP */
  29873. +
  29874. +#include "openswan/ipsec_proto.h"
  29875. +
  29876. +#ifdef CONFIG_KLIPS_DEBUG
  29877. +int debug_ipcomp = 0;
  29878. +#endif /* CONFIG_KLIPS_DEBUG */
  29879. +
  29880. +
  29881. +#ifdef CONFIG_KLIPS_IPCOMP
  29882. +enum ipsec_rcv_value
  29883. +ipsec_rcv_ipcomp_checks(struct ipsec_rcv_state *irs,
  29884. + struct sk_buff *skb)
  29885. +{
  29886. + int ipcompminlen;
  29887. +
  29888. + ipcompminlen = sizeof(struct iphdr);
  29889. +
  29890. + if(skb->len < (ipcompminlen + sizeof(struct ipcomphdr))) {
  29891. + KLIPS_PRINT(debug_rcv & DB_RX_INAU,
  29892. + "klips_debug:ipsec_rcv: "
  29893. + "runt comp packet of skb->len=%d received from %s, dropped.\n",
  29894. + skb->len,
  29895. + irs->ipsaddr_txt);
  29896. + if(irs->stats) {
  29897. + irs->stats->rx_errors++;
  29898. + }
  29899. + return IPSEC_RCV_BADLEN;
  29900. + }
  29901. +
  29902. + irs->protostuff.ipcompstuff.compp = (struct ipcomphdr *)skb->h.raw;
  29903. + irs->said.spi = htonl((__u32)ntohs(irs->protostuff.ipcompstuff.compp->ipcomp_cpi));
  29904. + return IPSEC_RCV_OK;
  29905. +}
  29906. +
  29907. +enum ipsec_rcv_value
  29908. +ipsec_rcv_ipcomp_decomp(struct ipsec_rcv_state *irs)
  29909. +{
  29910. + unsigned int flags = 0;
  29911. + struct ipsec_sa *ipsp = irs->ipsp;
  29912. + struct sk_buff *skb;
  29913. +
  29914. + skb=irs->skb;
  29915. +
  29916. + ipsec_xmit_dmp("ipcomp", skb->h.raw, skb->len);
  29917. +
  29918. + if(ipsp == NULL) {
  29919. + return IPSEC_RCV_SAIDNOTFOUND;
  29920. + }
  29921. +
  29922. + if(sysctl_ipsec_inbound_policy_check &&
  29923. + ((((ntohl(ipsp->ips_said.spi) & 0x0000ffff) != ntohl(irs->said.spi)) &&
  29924. + (ipsp->ips_encalg != ntohl(irs->said.spi)) /* this is a workaround for peer non-compliance with rfc2393 */
  29925. + ))) {
  29926. + char sa2[SATOT_BUF];
  29927. + size_t sa_len2 = 0;
  29928. +
  29929. + sa_len2 = satot(&ipsp->ips_said, 0, sa2, sizeof(sa2));
  29930. +
  29931. + KLIPS_PRINT(debug_rcv,
  29932. + "klips_debug:ipsec_rcv: "
  29933. + "Incoming packet with SA(IPCA):%s does not match policy SA(IPCA):%s cpi=%04x cpi->spi=%08x spi=%08x, spi->cpi=%04x for SA grouping, dropped.\n",
  29934. + irs->sa_len ? irs->sa : " (error)",
  29935. + ipsp != NULL ? (sa_len2 ? sa2 : " (error)") : "NULL",
  29936. + ntohs(irs->protostuff.ipcompstuff.compp->ipcomp_cpi),
  29937. + (__u32)ntohl(irs->said.spi),
  29938. + ipsp != NULL ? (__u32)ntohl((ipsp->ips_said.spi)) : 0,
  29939. + ipsp != NULL ? (__u16)(ntohl(ipsp->ips_said.spi) & 0x0000ffff) : 0);
  29940. + if(irs->stats) {
  29941. + irs->stats->rx_dropped++;
  29942. + }
  29943. + return IPSEC_RCV_SAIDNOTFOUND;
  29944. + }
  29945. +
  29946. + ipsp->ips_comp_ratio_cbytes += ntohs(irs->ipp->tot_len);
  29947. + irs->next_header = irs->protostuff.ipcompstuff.compp->ipcomp_nh;
  29948. +
  29949. + skb = skb_decompress(skb, ipsp, &flags);
  29950. + if (!skb || flags) {
  29951. + spin_unlock(&tdb_lock);
  29952. + KLIPS_PRINT(debug_rcv,
  29953. + "klips_debug:ipsec_rcv: "
  29954. + "skb_decompress() returned error flags=%x, dropped.\n",
  29955. + flags);
  29956. + if (irs->stats) {
  29957. + if (flags)
  29958. + irs->stats->rx_errors++;
  29959. + else
  29960. + irs->stats->rx_dropped++;
  29961. + }
  29962. + return IPSEC_RCV_IPCOMPFAILED;
  29963. + }
  29964. +
  29965. + /* make sure we update the pointer */
  29966. + irs->skb = skb;
  29967. +
  29968. +#ifdef NET_21
  29969. + irs->ipp = skb->nh.iph;
  29970. +#else /* NET_21 */
  29971. + irs->ipp = skb->ip_hdr;
  29972. +#endif /* NET_21 */
  29973. +
  29974. + ipsp->ips_comp_ratio_dbytes += ntohs(irs->ipp->tot_len);
  29975. +
  29976. + KLIPS_PRINT(debug_rcv,
  29977. + "klips_debug:ipsec_rcv: "
  29978. + "packet decompressed SA(IPCA):%s cpi->spi=%08x spi=%08x, spi->cpi=%04x, nh=%d.\n",
  29979. + irs->sa_len ? irs->sa : " (error)",
  29980. + (__u32)ntohl(irs->said.spi),
  29981. + ipsp != NULL ? (__u32)ntohl((ipsp->ips_said.spi)) : 0,
  29982. + ipsp != NULL ? (__u16)(ntohl(ipsp->ips_said.spi) & 0x0000ffff) : 0,
  29983. + irs->next_header);
  29984. + KLIPS_IP_PRINT(debug_rcv & DB_RX_PKTRX, irs->ipp);
  29985. +
  29986. + return IPSEC_RCV_OK;
  29987. +}
  29988. +
  29989. +enum ipsec_xmit_value
  29990. +ipsec_xmit_ipcomp_setup(struct ipsec_xmit_state *ixs)
  29991. +{
  29992. + unsigned int flags = 0;
  29993. +#ifdef CONFIG_KLIPS_DEBUG
  29994. + unsigned int old_tot_len = ntohs(ixs->iph->tot_len);
  29995. +#endif /* CONFIG_KLIPS_DEBUG */
  29996. +
  29997. + ixs->ipsp->ips_comp_ratio_dbytes += ntohs(ixs->iph->tot_len);
  29998. +
  29999. + ixs->skb = skb_compress(ixs->skb, ixs->ipsp, &flags);
  30000. +
  30001. +#ifdef NET_21
  30002. + ixs->iph = ixs->skb->nh.iph;
  30003. +#else /* NET_21 */
  30004. + ixs->iph = ixs->skb->ip_hdr;
  30005. +#endif /* NET_21 */
  30006. +
  30007. + ixs->ipsp->ips_comp_ratio_cbytes += ntohs(ixs->iph->tot_len);
  30008. +
  30009. +#ifdef CONFIG_KLIPS_DEBUG
  30010. + if (debug_tunnel & DB_TN_CROUT)
  30011. + {
  30012. + if (old_tot_len > ntohs(ixs->iph->tot_len))
  30013. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  30014. + "klips_debug:ipsec_xmit_encap_once: "
  30015. + "packet shrunk from %d to %d bytes after compression, cpi=%04x (should be from spi=%08x, spi&0xffff=%04x.\n",
  30016. + old_tot_len, ntohs(ixs->iph->tot_len),
  30017. + ntohs(((struct ipcomphdr*)(((char*)ixs->iph) + ((ixs->iph->ihl) << 2)))->ipcomp_cpi),
  30018. + ntohl(ixs->ipsp->ips_said.spi),
  30019. + (__u16)(ntohl(ixs->ipsp->ips_said.spi) & 0x0000ffff));
  30020. + else
  30021. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  30022. + "klips_debug:ipsec_xmit_encap_once: "
  30023. + "packet did not compress (flags = %d).\n",
  30024. + flags);
  30025. + }
  30026. +#endif /* CONFIG_KLIPS_DEBUG */
  30027. +
  30028. + return IPSEC_XMIT_OK;
  30029. +}
  30030. +
  30031. +struct xform_functions ipcomp_xform_funcs[]={
  30032. + {rcv_checks: ipsec_rcv_ipcomp_checks,
  30033. + rcv_decrypt: ipsec_rcv_ipcomp_decomp,
  30034. + xmit_setup: ipsec_xmit_ipcomp_setup,
  30035. + xmit_headroom: 0,
  30036. + xmit_needtailroom: 0,
  30037. + },
  30038. +};
  30039. +
  30040. +#if 0
  30041. +/* We probably don't want to install a pure IPCOMP protocol handler, but
  30042. + only want to handle IPCOMP if it is encapsulated inside an ESP payload
  30043. + (which is already handled) */
  30044. +#ifdef CONFIG_KLIPS_IPCOMP
  30045. +struct inet_protocol comp_protocol =
  30046. +{
  30047. + ipsec_rcv, /* COMP handler */
  30048. + NULL, /* COMP error control */
  30049. +#ifdef NETDEV_25
  30050. + 1, /* no policy */
  30051. +#else
  30052. + 0, /* next */
  30053. + IPPROTO_COMP, /* protocol ID */
  30054. + 0, /* copy */
  30055. + NULL, /* data */
  30056. + "COMP" /* name */
  30057. +#endif
  30058. +};
  30059. +#endif /* CONFIG_KLIPS_IPCOMP */
  30060. +#endif
  30061. +
  30062. +#endif /* CONFIG_KLIPS_IPCOMP */
  30063. --- /dev/null Tue Mar 11 13:02:56 2003
  30064. +++ linux/net/ipsec/ipsec_ipip.c Mon Feb 9 13:51:03 2004
  30065. @@ -0,0 +1,122 @@
  30066. +/*
  30067. + * processing code for IPIP
  30068. + * Copyright (C) 2003 Michael Richardson <mcr@sandelman.ottawa.on.ca>
  30069. + *
  30070. + * This program is free software; you can redistribute it and/or modify it
  30071. + * under the terms of the GNU General Public License as published by the
  30072. + * Free Software Foundation; either version 2 of the License, or (at your
  30073. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  30074. + *
  30075. + * This program is distributed in the hope that it will be useful, but
  30076. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  30077. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  30078. + * for more details.
  30079. + */
  30080. +
  30081. +char ipsec_ipip_c_version[] = "RCSID $Id: ipsec_ipip.c,v 1.3.2.3 2006/10/06 21:39:26 paul Exp $";
  30082. +#ifndef AUTOCONF_INCLUDED
  30083. +#include <linux/config.h>
  30084. +#endif
  30085. +#include <linux/version.h>
  30086. +
  30087. +#define __NO_VERSION__
  30088. +#include <linux/module.h>
  30089. +#include <linux/kernel.h> /* printk() */
  30090. +
  30091. +#include "openswan/ipsec_param.h"
  30092. +
  30093. +#ifdef MALLOC_SLAB
  30094. +# include <linux/slab.h> /* kmalloc() */
  30095. +#else /* MALLOC_SLAB */
  30096. +# include <linux/malloc.h> /* kmalloc() */
  30097. +#endif /* MALLOC_SLAB */
  30098. +#include <linux/errno.h> /* error codes */
  30099. +#include <linux/types.h> /* size_t */
  30100. +#include <linux/interrupt.h> /* mark_bh */
  30101. +
  30102. +#include <linux/netdevice.h> /* struct device, and other headers */
  30103. +#include <linux/etherdevice.h> /* eth_type_trans */
  30104. +#include <linux/ip.h> /* struct iphdr */
  30105. +#include <linux/skbuff.h>
  30106. +#include <openswan.h>
  30107. +#ifdef SPINLOCK
  30108. +# ifdef SPINLOCK_23
  30109. +# include <linux/spinlock.h> /* *lock* */
  30110. +# else /* SPINLOCK_23 */
  30111. +# include <asm/spinlock.h> /* *lock* */
  30112. +# endif /* SPINLOCK_23 */
  30113. +#endif /* SPINLOCK */
  30114. +
  30115. +#include <net/ip.h>
  30116. +
  30117. +#include "openswan/radij.h"
  30118. +#include "openswan/ipsec_encap.h"
  30119. +#include "openswan/ipsec_sa.h"
  30120. +
  30121. +#include "openswan/ipsec_radij.h"
  30122. +#include "openswan/ipsec_xform.h"
  30123. +#include "openswan/ipsec_tunnel.h"
  30124. +#include "openswan/ipsec_rcv.h"
  30125. +#include "openswan/ipsec_xmit.h"
  30126. +
  30127. +#include "openswan/ipsec_auth.h"
  30128. +#include "openswan/ipsec_ipip.h"
  30129. +#include "openswan/ipsec_param.h"
  30130. +
  30131. +#include "openswan/ipsec_proto.h"
  30132. +
  30133. +enum ipsec_xmit_value
  30134. +ipsec_xmit_ipip_setup(struct ipsec_xmit_state *ixs)
  30135. +{
  30136. + ixs->iph->version = 4;
  30137. +
  30138. + switch(sysctl_ipsec_tos) {
  30139. + case 0:
  30140. +#ifdef NET_21
  30141. + ixs->iph->tos = ixs->skb->nh.iph->tos;
  30142. +#else /* NET_21 */
  30143. + ixs->iph->tos = ixs->skb->ip_hdr->tos;
  30144. +#endif /* NET_21 */
  30145. + break;
  30146. + case 1:
  30147. + ixs->iph->tos = 0;
  30148. + break;
  30149. + default:
  30150. + break;
  30151. + }
  30152. + ixs->iph->ttl = SYSCTL_IPSEC_DEFAULT_TTL;
  30153. + ixs->iph->frag_off = 0;
  30154. + ixs->iph->saddr = ((struct sockaddr_in*)(ixs->ipsp->ips_addr_s))->sin_addr.s_addr;
  30155. + ixs->iph->daddr = ((struct sockaddr_in*)(ixs->ipsp->ips_addr_d))->sin_addr.s_addr;
  30156. + ixs->iph->protocol = IPPROTO_IPIP;
  30157. + ixs->iph->ihl = sizeof(struct iphdr) >> 2;
  30158. +
  30159. + KLIPS_IP_SELECT_IDENT(ixs->iph, ixs->skb);
  30160. +
  30161. + ixs->newdst = (__u32)ixs->iph->daddr;
  30162. + ixs->newsrc = (__u32)ixs->iph->saddr;
  30163. +
  30164. +#ifdef NET_21
  30165. + ixs->skb->h.ipiph = ixs->skb->nh.iph;
  30166. +#endif /* NET_21 */
  30167. + return IPSEC_XMIT_OK;
  30168. +}
  30169. +
  30170. +struct xform_functions ipip_xform_funcs[]={
  30171. + { rcv_checks: NULL,
  30172. + rcv_setup_auth: NULL,
  30173. + rcv_calc_auth: NULL,
  30174. + rcv_decrypt: NULL,
  30175. +
  30176. + xmit_setup: ipsec_xmit_ipip_setup,
  30177. + xmit_headroom: sizeof(struct iphdr),
  30178. + xmit_needtailroom: 0,
  30179. + },
  30180. +};
  30181. +
  30182. +
  30183. +
  30184. +
  30185. +
  30186. +
  30187. +
  30188. --- /dev/null Tue Mar 11 13:02:56 2003
  30189. +++ linux/net/ipsec/ipsec_kern24.c Mon Feb 9 13:51:03 2004
  30190. @@ -0,0 +1,74 @@
  30191. +/*
  30192. + * Copyright 2005 (C) Michael Richardson <mcr@xelerance.com>
  30193. + *
  30194. + * This is a file of functions which are present in 2.6 kernels,
  30195. + * but are not available by default in the 2.4 series.
  30196. + *
  30197. + * As such this code is usually from the Linux kernel, and is covered by
  30198. + * GPL.
  30199. + *
  30200. + * This program is free software; you can redistribute it and/or modify it
  30201. + * under the terms of the GNU General Public License as published by the
  30202. + * Free Software Foundation; either version 2 of the License, or (at your
  30203. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  30204. + *
  30205. + * This program is distributed in the hope that it will be useful, but
  30206. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  30207. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  30208. + * for more details.
  30209. + *
  30210. + * $Id: ipsec_kern24.c,v 1.2 2005/05/20 03:19:18 mcr Exp $
  30211. + *
  30212. + */
  30213. +
  30214. +#include <linux/kernel.h>
  30215. +#include <linux/mm.h>
  30216. +#include <linux/spinlock.h>
  30217. +
  30218. +/*
  30219. + * printk rate limiting, lifted from the networking subsystem.
  30220. + *
  30221. + * This enforces a rate limit: not more than one kernel message
  30222. + * every printk_ratelimit_jiffies to make a denial-of-service
  30223. + * attack impossible.
  30224. + */
  30225. +static spinlock_t ratelimit_lock = SPIN_LOCK_UNLOCKED;
  30226. +
  30227. +int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
  30228. +{
  30229. + static unsigned long toks = 10*5*HZ;
  30230. + static unsigned long last_msg;
  30231. + static int missed;
  30232. + unsigned long flags;
  30233. + unsigned long now = jiffies;
  30234. +
  30235. + spin_lock_irqsave(&ratelimit_lock, flags);
  30236. + toks += now - last_msg;
  30237. + last_msg = now;
  30238. + if (toks > (ratelimit_burst * ratelimit_jiffies))
  30239. + toks = ratelimit_burst * ratelimit_jiffies;
  30240. + if (toks >= ratelimit_jiffies) {
  30241. + int lost = missed;
  30242. + missed = 0;
  30243. + toks -= ratelimit_jiffies;
  30244. + spin_unlock_irqrestore(&ratelimit_lock, flags);
  30245. + if (lost)
  30246. + printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
  30247. + return 1;
  30248. + }
  30249. + missed++;
  30250. + spin_unlock_irqrestore(&ratelimit_lock, flags);
  30251. + return 0;
  30252. +}
  30253. +
  30254. +/* minimum time in jiffies between messages */
  30255. +int printk_ratelimit_jiffies = 5*HZ;
  30256. +
  30257. +/* number of messages we send before ratelimiting */
  30258. +int printk_ratelimit_burst = 10;
  30259. +
  30260. +int printk_ratelimit(void)
  30261. +{
  30262. + return __printk_ratelimit(printk_ratelimit_jiffies,
  30263. + printk_ratelimit_burst);
  30264. +}
  30265. --- /dev/null Tue Mar 11 13:02:56 2003
  30266. +++ linux/net/ipsec/ipsec_life.c Mon Feb 9 13:51:03 2004
  30267. @@ -0,0 +1,273 @@
  30268. +/*
  30269. + * @(#) lifetime structure utilities
  30270. + *
  30271. + * Copyright (C) 2001 Richard Guy Briggs <rgb@freeswan.org>
  30272. + * and Michael Richardson <mcr@freeswan.org>
  30273. + *
  30274. + * This program is free software; you can redistribute it and/or modify it
  30275. + * under the terms of the GNU General Public License as published by the
  30276. + * Free Software Foundation; either version 2 of the License, or (at your
  30277. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  30278. + *
  30279. + * This program is distributed in the hope that it will be useful, but
  30280. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  30281. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  30282. + * for more details.
  30283. + *
  30284. + * RCSID $Id: ipsec_life.c,v 1.13.10.1 2006/10/06 21:39:26 paul Exp $
  30285. + *
  30286. + */
  30287. +
  30288. +/*
  30289. + * This provides series of utility functions for dealing with lifetime
  30290. + * structures.
  30291. + *
  30292. + * ipsec_check_lifetime - returns -1 hard lifetime exceeded
  30293. + * 0 soft lifetime exceeded
  30294. + * 1 everything is okay
  30295. + * based upon whether or not the count exceeds hard/soft
  30296. + *
  30297. + */
  30298. +
  30299. +#define __NO_VERSION__
  30300. +#include <linux/module.h>
  30301. +#ifndef AUTOCONF_INCLUDED
  30302. +#include <linux/config.h>
  30303. +#endif /* for CONFIG_IP_FORWARD */
  30304. +#include <linux/version.h>
  30305. +#include <linux/kernel.h> /* printk() */
  30306. +
  30307. +#include "openswan/ipsec_param.h"
  30308. +
  30309. +#include <linux/netdevice.h> /* struct device, struct net_device_stats and other headers */
  30310. +#include <linux/etherdevice.h> /* eth_type_trans */
  30311. +#include <linux/skbuff.h>
  30312. +#include <openswan.h>
  30313. +
  30314. +#include "openswan/radij.h"
  30315. +#include "openswan/ipsec_life.h"
  30316. +#include "openswan/ipsec_xform.h"
  30317. +#include "openswan/ipsec_eroute.h"
  30318. +#include "openswan/ipsec_encap.h"
  30319. +#include "openswan/ipsec_radij.h"
  30320. +
  30321. +#include "openswan/ipsec_sa.h"
  30322. +#include "openswan/ipsec_tunnel.h"
  30323. +#include "openswan/ipsec_ipe4.h"
  30324. +#include "openswan/ipsec_ah.h"
  30325. +#include "openswan/ipsec_esp.h"
  30326. +
  30327. +#ifdef CONFIG_KLIPS_IPCOMP
  30328. +#include "openswan/ipcomp.h"
  30329. +#endif /* CONFIG_KLIPS_IPCOMP */
  30330. +
  30331. +#include <pfkeyv2.h>
  30332. +#include <pfkey.h>
  30333. +
  30334. +#include "openswan/ipsec_proto.h"
  30335. +
  30336. +
  30337. +enum ipsec_life_alive
  30338. +ipsec_lifetime_check(struct ipsec_lifetime64 *il64,
  30339. + const char *lifename,
  30340. + const char *saname,
  30341. + enum ipsec_life_type ilt,
  30342. + enum ipsec_direction idir,
  30343. + struct ipsec_sa *ips)
  30344. +{
  30345. + __u64 count;
  30346. + const char *dir;
  30347. +
  30348. + if(saname == NULL) {
  30349. + saname = "unknown-SA";
  30350. + }
  30351. +
  30352. + if(idir == ipsec_incoming) {
  30353. + dir = "incoming";
  30354. + } else {
  30355. + dir = "outgoing";
  30356. + }
  30357. +
  30358. +
  30359. + if(ilt == ipsec_life_timebased) {
  30360. + count = jiffies/HZ - il64->ipl_count;
  30361. + } else {
  30362. + count = il64->ipl_count;
  30363. + }
  30364. +
  30365. + if(il64->ipl_hard &&
  30366. + (count > il64->ipl_hard)) {
  30367. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  30368. + "klips_debug:ipsec_lifetime_check: "
  30369. + "hard %s lifetime of SA:<%s%s%s> %s has been reached, SA expired, "
  30370. + "%s packet dropped.\n",
  30371. + lifename,
  30372. + IPS_XFORM_NAME(ips),
  30373. + saname,
  30374. + dir);
  30375. +
  30376. + pfkey_expire(ips, 1);
  30377. + return ipsec_life_harddied;
  30378. + }
  30379. +
  30380. + if(il64->ipl_soft &&
  30381. + (count > il64->ipl_soft)) {
  30382. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  30383. + "klips_debug:ipsec_lifetime_check: "
  30384. + "soft %s lifetime of SA:<%s%s%s> %s has been reached, SA expiring, "
  30385. + "soft expire message sent up, %s packet still processed.\n",
  30386. + lifename,
  30387. + IPS_XFORM_NAME(ips),
  30388. + saname,
  30389. + dir);
  30390. +
  30391. + if(ips->ips_state != SADB_SASTATE_DYING) {
  30392. + pfkey_expire(ips, 0);
  30393. + }
  30394. + ips->ips_state = SADB_SASTATE_DYING;
  30395. +
  30396. + return ipsec_life_softdied;
  30397. + }
  30398. + return ipsec_life_okay;
  30399. +}
  30400. +
  30401. +
  30402. +/*
  30403. + * This function takes a buffer (with length), a lifetime name and type,
  30404. + * and formats a string to represent the current values of the lifetime.
  30405. + *
  30406. + * It returns the number of bytes that the format took (or would take,
  30407. + * if the buffer were large enough: snprintf semantics).
  30408. + * This is used in /proc routines and in debug output.
  30409. + */
  30410. +int
  30411. +ipsec_lifetime_format(char *buffer,
  30412. + int buflen,
  30413. + char *lifename,
  30414. + enum ipsec_life_type timebaselife,
  30415. + struct ipsec_lifetime64 *lifetime)
  30416. +{
  30417. + int len = 0;
  30418. + __u64 count;
  30419. +
  30420. + if(timebaselife == ipsec_life_timebased) {
  30421. + count = jiffies/HZ - lifetime->ipl_count;
  30422. + } else {
  30423. + count = lifetime->ipl_count;
  30424. + }
  30425. +
  30426. + if(lifetime->ipl_count > 1 ||
  30427. + lifetime->ipl_soft ||
  30428. + lifetime->ipl_hard) {
  30429. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0))
  30430. + len = ipsec_snprintf(buffer, buflen,
  30431. + "%s(%Lu,%Lu,%Lu)",
  30432. + lifename,
  30433. + count,
  30434. + lifetime->ipl_soft,
  30435. + lifetime->ipl_hard);
  30436. +#else /* XXX high 32 bits are not displayed */
  30437. + len = ipsec_snprintf(buffer, buflen,
  30438. + "%s(%lu,%lu,%lu)",
  30439. + lifename,
  30440. + (unsigned long)count,
  30441. + (unsigned long)lifetime->ipl_soft,
  30442. + (unsigned long)lifetime->ipl_hard);
  30443. +#endif
  30444. + }
  30445. +
  30446. + return len;
  30447. +}
  30448. +
  30449. +void
  30450. +ipsec_lifetime_update_hard(struct ipsec_lifetime64 *lifetime,
  30451. + __u64 newvalue)
  30452. +{
  30453. + if(newvalue &&
  30454. + (!lifetime->ipl_hard ||
  30455. + (newvalue < lifetime->ipl_hard))) {
  30456. + lifetime->ipl_hard = newvalue;
  30457. +
  30458. + if(!lifetime->ipl_soft &&
  30459. + (lifetime->ipl_hard < lifetime->ipl_soft)) {
  30460. + lifetime->ipl_soft = lifetime->ipl_hard;
  30461. + }
  30462. + }
  30463. +}
  30464. +
  30465. +void
  30466. +ipsec_lifetime_update_soft(struct ipsec_lifetime64 *lifetime,
  30467. + __u64 newvalue)
  30468. +{
  30469. + if(newvalue &&
  30470. + (!lifetime->ipl_soft ||
  30471. + (newvalue < lifetime->ipl_soft))) {
  30472. + lifetime->ipl_soft = newvalue;
  30473. +
  30474. + if(lifetime->ipl_hard &&
  30475. + (lifetime->ipl_hard < lifetime->ipl_soft)) {
  30476. + lifetime->ipl_soft = lifetime->ipl_hard;
  30477. + }
  30478. + }
  30479. +}
  30480. +
  30481. +
  30482. +/*
  30483. + * $Log: ipsec_life.c,v $
  30484. + * Revision 1.13.10.1 2006/10/06 21:39:26 paul
  30485. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  30486. + * set. This is defined through autoconf.h which is included through the
  30487. + * linux kernel build macros.
  30488. + *
  30489. + * Revision 1.13 2004/07/10 19:11:18 mcr
  30490. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  30491. + *
  30492. + * Revision 1.12 2004/04/23 20:44:35 ken
  30493. + * Update comments
  30494. + *
  30495. + * Revision 1.11 2004/04/06 02:49:26 mcr
  30496. + * pullup of algo code from alg-branch.
  30497. + *
  30498. + * Revision 1.10 2004/03/30 11:03:10 paul
  30499. + * two more occurances of snprintf, found by Sam from a users oops msg.
  30500. + *
  30501. + * Revision 1.9 2003/10/31 02:27:55 mcr
  30502. + * pulled up port-selector patches and sa_id elimination.
  30503. + *
  30504. + * Revision 1.8.4.1 2003/10/29 01:30:41 mcr
  30505. + * elimited "struct sa_id".
  30506. + *
  30507. + * Revision 1.8 2003/02/06 02:00:10 rgb
  30508. + * Fixed incorrect debugging text label
  30509. + *
  30510. + * Revision 1.7 2002/05/23 07:16:26 rgb
  30511. + * Fixed absolute/relative reference to lifetime count printout.
  30512. + *
  30513. + * Revision 1.6 2002/04/24 07:55:32 mcr
  30514. + * #include patches and Makefiles for post-reorg compilation.
  30515. + *
  30516. + * Revision 1.5 2002/04/24 07:36:28 mcr
  30517. + * Moved from ./klips/net/ipsec/ipsec_life.c,v
  30518. + *
  30519. + * Revision 1.4 2002/01/29 17:17:55 mcr
  30520. + * moved include of ipsec_param.h to after include of linux/kernel.h
  30521. + * otherwise, it seems that some option that is set in ipsec_param.h
  30522. + * screws up something subtle in the include path to kernel.h, and
  30523. + * it complains on the snprintf() prototype.
  30524. + *
  30525. + * Revision 1.3 2002/01/29 02:13:17 mcr
  30526. + * introduction of ipsec_kversion.h means that include of
  30527. + * ipsec_param.h must preceed any decisions about what files to
  30528. + * include to deal with differences in kernel source.
  30529. + *
  30530. + * Revision 1.2 2001/11/26 09:16:14 rgb
  30531. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  30532. + *
  30533. + * Revision 1.1.2.1 2001/09/25 02:25:57 mcr
  30534. + * lifetime structure created and common functions created.
  30535. + *
  30536. + * Local variables:
  30537. + * c-file-style: "linux"
  30538. + * End:
  30539. + *
  30540. + */
  30541. --- /dev/null Tue Mar 11 13:02:56 2003
  30542. +++ linux/net/ipsec/ipsec_mast.c Mon Feb 9 13:51:03 2004
  30543. @@ -0,0 +1,1099 @@
  30544. +/*
  30545. + * IPSEC MAST code.
  30546. + * Copyright (C) 1996, 1997 John Ioannidis.
  30547. + * Copyright (C) 1998, 1999, 2000, 2001, 2002 Richard Guy Briggs.
  30548. + *
  30549. + * This program is free software; you can redistribute it and/or modify it
  30550. + * under the terms of the GNU General Public License as published by the
  30551. + * Free Software Foundation; either version 2 of the License, or (at your
  30552. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  30553. + *
  30554. + * This program is distributed in the hope that it will be useful, but
  30555. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  30556. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  30557. + * for more details.
  30558. + */
  30559. +
  30560. +char ipsec_mast_c_version[] = "RCSID $Id: ipsec_mast.c,v 1.7.2.1 2006/10/06 21:39:26 paul Exp $";
  30561. +
  30562. +#define __NO_VERSION__
  30563. +#include <linux/module.h>
  30564. +#ifndef AUTOCONF_INCLUDED
  30565. +#include <linux/config.h>
  30566. +#endif /* for CONFIG_IP_FORWARD */
  30567. +#include <linux/version.h>
  30568. +#include <linux/kernel.h> /* printk() */
  30569. +
  30570. +#include "freeswan/ipsec_param.h"
  30571. +
  30572. +#ifdef MALLOC_SLAB
  30573. +# include <linux/slab.h> /* kmalloc() */
  30574. +#else /* MALLOC_SLAB */
  30575. +# include <linux/malloc.h> /* kmalloc() */
  30576. +#endif /* MALLOC_SLAB */
  30577. +#include <linux/errno.h> /* error codes */
  30578. +#include <linux/types.h> /* size_t */
  30579. +#include <linux/interrupt.h> /* mark_bh */
  30580. +
  30581. +#include <linux/netdevice.h> /* struct device, struct net_device_stats, dev_queue_xmit() and other headers */
  30582. +#include <linux/etherdevice.h> /* eth_type_trans */
  30583. +#include <linux/ip.h> /* struct iphdr */
  30584. +#include <linux/tcp.h> /* struct tcphdr */
  30585. +#include <linux/udp.h> /* struct udphdr */
  30586. +#include <linux/skbuff.h>
  30587. +#include <freeswan.h>
  30588. +#include <linux/in6.h>
  30589. +#include <net/dst.h>
  30590. +#undef dev_kfree_skb
  30591. +#define dev_kfree_skb(a,b) kfree_skb(a)
  30592. +#define PHYSDEV_TYPE
  30593. +#include <net/icmp.h> /* icmp_send() */
  30594. +#include <net/ip.h>
  30595. +#include <linux/netfilter_ipv4.h>
  30596. +
  30597. +#include <linux/if_arp.h>
  30598. +
  30599. +#include "freeswan/radij.h"
  30600. +#include "freeswan/ipsec_life.h"
  30601. +#include "freeswan/ipsec_xform.h"
  30602. +#include "freeswan/ipsec_eroute.h"
  30603. +#include "freeswan/ipsec_encap.h"
  30604. +#include "freeswan/ipsec_radij.h"
  30605. +#include "freeswan/ipsec_sa.h"
  30606. +#include "freeswan/ipsec_tunnel.h"
  30607. +#include "freeswan/ipsec_mast.h"
  30608. +#include "freeswan/ipsec_ipe4.h"
  30609. +#include "freeswan/ipsec_ah.h"
  30610. +#include "freeswan/ipsec_esp.h"
  30611. +
  30612. +#include <pfkeyv2.h>
  30613. +#include <pfkey.h>
  30614. +
  30615. +#include "freeswan/ipsec_proto.h"
  30616. +
  30617. +int ipsec_maxdevice_count = -1;
  30618. +
  30619. +DEBUG_NO_STATIC int
  30620. +ipsec_mast_open(struct net_device *dev)
  30621. +{
  30622. + struct ipsecpriv *prv = dev->priv;
  30623. +
  30624. + /*
  30625. + * Can't open until attached.
  30626. + */
  30627. +
  30628. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  30629. + "klips_debug:ipsec_mast_open: "
  30630. + "dev = %s, prv->dev = %s\n",
  30631. + dev->name, prv->dev?prv->dev->name:"NONE");
  30632. +
  30633. + if (prv->dev == NULL)
  30634. + return -ENODEV;
  30635. +
  30636. + KLIPS_INC_USE;
  30637. + return 0;
  30638. +}
  30639. +
  30640. +DEBUG_NO_STATIC int
  30641. +ipsec_mast_close(struct net_device *dev)
  30642. +{
  30643. + KLIPS_DEC_USE;
  30644. + return 0;
  30645. +}
  30646. +
  30647. +static inline int ipsec_mast_xmit2(struct sk_buff *skb)
  30648. +{
  30649. + return ip_send(skb);
  30650. +}
  30651. +
  30652. +enum ipsec_xmit_value
  30653. +ipsec_mast_send(struct ipsec_xmit_state*ixs)
  30654. +{
  30655. + /* new route/dst cache code from James Morris */
  30656. + ixs->skb->dev = ixs->physdev;
  30657. + /*skb_orphan(ixs->skb);*/
  30658. + if((ixs->error = ip_route_output(&ixs->route,
  30659. + ixs->skb->nh.iph->daddr,
  30660. + ixs->pass ? 0 : ixs->skb->nh.iph->saddr,
  30661. + RT_TOS(ixs->skb->nh.iph->tos),
  30662. + ixs->physdev->iflink /* rgb: should this be 0? */))) {
  30663. + ixs->stats->tx_errors++;
  30664. + KLIPS_PRINT(debug_mast & DB_MAST_XMIT,
  30665. + "klips_debug:ipsec_xmit_send: "
  30666. + "ip_route_output failed with error code %d, rt->u.dst.dev=%s, dropped\n",
  30667. + ixs->error,
  30668. + ixs->route->u.dst.dev->name);
  30669. + return IPSEC_XMIT_ROUTEERR;
  30670. + }
  30671. + if(ixs->dev == ixs->route->u.dst.dev) {
  30672. + ip_rt_put(ixs->route);
  30673. + /* This is recursion, drop it. */
  30674. + ixs->stats->tx_errors++;
  30675. + KLIPS_PRINT(debug_mast & DB_MAST_XMIT,
  30676. + "klips_debug:ipsec_xmit_send: "
  30677. + "suspect recursion, dev=rt->u.dst.dev=%s, dropped\n",
  30678. + ixs->dev->name);
  30679. + return IPSEC_XMIT_RECURSDETECT;
  30680. + }
  30681. + dst_release(ixs->skb->dst);
  30682. + ixs->skb->dst = &ixs->route->u.dst;
  30683. + ixs->stats->tx_bytes += ixs->skb->len;
  30684. + if(ixs->skb->len < ixs->skb->nh.raw - ixs->skb->data) {
  30685. + ixs->stats->tx_errors++;
  30686. + printk(KERN_WARNING
  30687. + "klips_error:ipsec_xmit_send: "
  30688. + "tried to __skb_pull nh-data=%ld, %d available. This should never happen, please report.\n",
  30689. + (unsigned long)(ixs->skb->nh.raw - ixs->skb->data),
  30690. + ixs->skb->len);
  30691. + return IPSEC_XMIT_PUSHPULLERR;
  30692. + }
  30693. + __skb_pull(ixs->skb, ixs->skb->nh.raw - ixs->skb->data);
  30694. +#ifdef SKB_RESET_NFCT
  30695. + nf_conntrack_put(ixs->skb->nfct);
  30696. + ixs->skb->nfct = NULL;
  30697. +#ifdef CONFIG_NETFILTER_DEBUG
  30698. + ixs->skb->nf_debug = 0;
  30699. +#endif /* CONFIG_NETFILTER_DEBUG */
  30700. +#endif /* SKB_RESET_NFCT */
  30701. + KLIPS_PRINT(debug_mast & DB_MAST_XMIT,
  30702. + "klips_debug:ipsec_xmit_send: "
  30703. + "...done, calling ip_send() on device:%s\n",
  30704. + ixs->skb->dev ? ixs->skb->dev->name : "NULL");
  30705. + KLIPS_IP_PRINT(debug_mast & DB_MAST_XMIT, ixs->skb->nh.iph);
  30706. + {
  30707. + int err;
  30708. +
  30709. + err = NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, ixs->skb, NULL, ixs->route->u.dst.dev,
  30710. + ipsec_mast_xmit2);
  30711. + if(err != NET_XMIT_SUCCESS && err != NET_XMIT_CN) {
  30712. + if(net_ratelimit())
  30713. + printk(KERN_ERR
  30714. + "klips_error:ipsec_xmit_send: "
  30715. + "ip_send() failed, err=%d\n",
  30716. + -err);
  30717. + ixs->stats->tx_errors++;
  30718. + ixs->stats->tx_aborted_errors++;
  30719. + ixs->skb = NULL;
  30720. + return IPSEC_XMIT_IPSENDFAILURE;
  30721. + }
  30722. + }
  30723. + ixs->stats->tx_packets++;
  30724. +
  30725. + ixs->skb = NULL;
  30726. +
  30727. + return IPSEC_XMIT_OK;
  30728. +}
  30729. +
  30730. +void
  30731. +ipsec_mast_cleanup(struct ipsec_xmit_state*ixs)
  30732. +{
  30733. +#if defined(HAS_NETIF_QUEUE) || defined (HAVE_NETIF_QUEUE)
  30734. + netif_wake_queue(ixs->dev);
  30735. +#else /* defined(HAS_NETIF_QUEUE) || defined (HAVE_NETIF_QUEUE) */
  30736. + ixs->dev->tbusy = 0;
  30737. +#endif /* defined(HAS_NETIF_QUEUE) || defined (HAVE_NETIF_QUEUE) */
  30738. + if(ixs->saved_header) {
  30739. + kfree(ixs->saved_header);
  30740. + }
  30741. + if(ixs->skb) {
  30742. + dev_kfree_skb(ixs->skb, FREE_WRITE);
  30743. + }
  30744. + if(ixs->oskb) {
  30745. + dev_kfree_skb(ixs->oskb, FREE_WRITE);
  30746. + }
  30747. + if (ixs->ips.ips_ident_s.data) {
  30748. + kfree(ixs->ips.ips_ident_s.data);
  30749. + }
  30750. + if (ixs->ips.ips_ident_d.data) {
  30751. + kfree(ixs->ips.ips_ident_d.data);
  30752. + }
  30753. +}
  30754. +
  30755. +#if 0
  30756. +/*
  30757. + * This function assumes it is being called from dev_queue_xmit()
  30758. + * and that skb is filled properly by that function.
  30759. + */
  30760. +int
  30761. +ipsec_mast_start_xmit(struct sk_buff *skb, struct net_device *dev, IPsecSAref_t SAref)
  30762. +{
  30763. + struct ipsec_xmit_state ixs_mem;
  30764. + struct ipsec_xmit_state *ixs = &ixs_mem;
  30765. + enum ipsec_xmit_value stat = IPSEC_XMIT_OK;
  30766. +
  30767. + /* dev could be a mast device, but should be optional, I think... */
  30768. + /* SAref is also optional, but one of the two must be present. */
  30769. + /* I wonder if it could accept no device or saref and guess? */
  30770. +
  30771. +/* ipsec_xmit_sanity_check_dev(ixs); */
  30772. +
  30773. + ipsec_xmit_sanity_check_skb(ixs);
  30774. +
  30775. + ipsec_xmit_adjust_hard_header(ixs);
  30776. +
  30777. + stat = ipsec_xmit_encap_bundle(ixs);
  30778. + if(stat != IPSEC_XMIT_OK) {
  30779. + /* SA processing failed */
  30780. + }
  30781. +
  30782. + ipsec_xmit_hard_header_restore();
  30783. +}
  30784. +#endif
  30785. +
  30786. +DEBUG_NO_STATIC struct net_device_stats *
  30787. +ipsec_mast_get_stats(struct net_device *dev)
  30788. +{
  30789. + return &(((struct ipsecpriv *)(dev->priv))->mystats);
  30790. +}
  30791. +
  30792. +/*
  30793. + * Revectored calls.
  30794. + * For each of these calls, a field exists in our private structure.
  30795. + */
  30796. +
  30797. +DEBUG_NO_STATIC int
  30798. +ipsec_mast_hard_header(struct sk_buff *skb, struct net_device *dev,
  30799. + unsigned short type, void *daddr, void *saddr, unsigned len)
  30800. +{
  30801. + struct ipsecpriv *prv = dev->priv;
  30802. + struct net_device *tmp;
  30803. + int ret;
  30804. + struct net_device_stats *stats; /* This device's statistics */
  30805. +
  30806. + if(skb == NULL) {
  30807. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30808. + "klips_debug:ipsec_mast_hard_header: "
  30809. + "no skb...\n");
  30810. + return -ENODATA;
  30811. + }
  30812. +
  30813. + if(dev == NULL) {
  30814. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30815. + "klips_debug:ipsec_mast_hard_header: "
  30816. + "no device...\n");
  30817. + return -ENODEV;
  30818. + }
  30819. +
  30820. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30821. + "klips_debug:ipsec_mast_hard_header: "
  30822. + "skb->dev=%s dev=%s.\n",
  30823. + skb->dev ? skb->dev->name : "NULL",
  30824. + dev->name);
  30825. +
  30826. + if(prv == NULL) {
  30827. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30828. + "klips_debug:ipsec_mast_hard_header: "
  30829. + "no private space associated with dev=%s\n",
  30830. + dev->name ? dev->name : "NULL");
  30831. + return -ENODEV;
  30832. + }
  30833. +
  30834. + stats = (struct net_device_stats *) &(prv->mystats);
  30835. +
  30836. + if(prv->dev == NULL) {
  30837. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30838. + "klips_debug:ipsec_mast_hard_header: "
  30839. + "no physical device associated with dev=%s\n",
  30840. + dev->name ? dev->name : "NULL");
  30841. + stats->tx_dropped++;
  30842. + return -ENODEV;
  30843. + }
  30844. +
  30845. + /* check if we have to send a IPv6 packet. It might be a Router
  30846. + Solicitation, where the building of the packet happens in
  30847. + reverse order:
  30848. + 1. ll hdr,
  30849. + 2. IPv6 hdr,
  30850. + 3. ICMPv6 hdr
  30851. + -> skb->nh.raw is still uninitialized when this function is
  30852. + called!! If this is no IPv6 packet, we can print debugging
  30853. + messages, otherwise we skip all debugging messages and just
  30854. + build the ll header */
  30855. + if(type != ETH_P_IPV6) {
  30856. + /* execute this only, if we don't have to build the
  30857. + header for a IPv6 packet */
  30858. + if(!prv->hard_header) {
  30859. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30860. + "klips_debug:ipsec_mast_hard_header: "
  30861. + "physical device has been detached, packet dropped 0p%p->0p%p len=%d type=%d dev=%s->NULL ",
  30862. + saddr,
  30863. + daddr,
  30864. + len,
  30865. + type,
  30866. + dev->name);
  30867. + KLIPS_PRINTMORE(debug_mast & DB_MAST_REVEC,
  30868. + "ip=%08x->%08x\n",
  30869. + (__u32)ntohl(skb->nh.iph->saddr),
  30870. + (__u32)ntohl(skb->nh.iph->daddr) );
  30871. + stats->tx_dropped++;
  30872. + return -ENODEV;
  30873. + }
  30874. +
  30875. +#define da ((struct net_device *)(prv->dev))->dev_addr
  30876. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30877. + "klips_debug:ipsec_mast_hard_header: "
  30878. + "Revectored 0p%p->0p%p len=%d type=%d dev=%s->%s dev_addr=%02x:%02x:%02x:%02x:%02x:%02x ",
  30879. + saddr,
  30880. + daddr,
  30881. + len,
  30882. + type,
  30883. + dev->name,
  30884. + prv->dev->name,
  30885. + da[0], da[1], da[2], da[3], da[4], da[5]);
  30886. + KLIPS_PRINTMORE(debug_mast & DB_MAST_REVEC,
  30887. + "ip=%08x->%08x\n",
  30888. + (__u32)ntohl(skb->nh.iph->saddr),
  30889. + (__u32)ntohl(skb->nh.iph->daddr) );
  30890. + } else {
  30891. + KLIPS_PRINT(debug_mast,
  30892. + "klips_debug:ipsec_mast_hard_header: "
  30893. + "is IPv6 packet, skip debugging messages, only revector and build linklocal header.\n");
  30894. + }
  30895. + tmp = skb->dev;
  30896. + skb->dev = prv->dev;
  30897. + ret = prv->hard_header(skb, prv->dev, type, (void *)daddr, (void *)saddr, len);
  30898. + skb->dev = tmp;
  30899. + return ret;
  30900. +}
  30901. +
  30902. +DEBUG_NO_STATIC int
  30903. +ipsec_mast_rebuild_header(struct sk_buff *skb)
  30904. +{
  30905. + struct ipsecpriv *prv = skb->dev->priv;
  30906. + struct net_device *tmp;
  30907. + int ret;
  30908. + struct net_device_stats *stats; /* This device's statistics */
  30909. +
  30910. + if(skb->dev == NULL) {
  30911. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30912. + "klips_debug:ipsec_mast_rebuild_header: "
  30913. + "no device...");
  30914. + return -ENODEV;
  30915. + }
  30916. +
  30917. + if(prv == NULL) {
  30918. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30919. + "klips_debug:ipsec_mast_rebuild_header: "
  30920. + "no private space associated with dev=%s",
  30921. + skb->dev->name ? skb->dev->name : "NULL");
  30922. + return -ENODEV;
  30923. + }
  30924. +
  30925. + stats = (struct net_device_stats *) &(prv->mystats);
  30926. +
  30927. + if(prv->dev == NULL) {
  30928. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30929. + "klips_debug:ipsec_mast_rebuild_header: "
  30930. + "no physical device associated with dev=%s",
  30931. + skb->dev->name ? skb->dev->name : "NULL");
  30932. + stats->tx_dropped++;
  30933. + return -ENODEV;
  30934. + }
  30935. +
  30936. + if(!prv->rebuild_header) {
  30937. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30938. + "klips_debug:ipsec_mast_rebuild_header: "
  30939. + "physical device has been detached, packet dropped skb->dev=%s->NULL ",
  30940. + skb->dev->name);
  30941. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30942. + "ip=%08x->%08x\n",
  30943. + (__u32)ntohl(skb->nh.iph->saddr),
  30944. + (__u32)ntohl(skb->nh.iph->daddr) );
  30945. + stats->tx_dropped++;
  30946. + return -ENODEV;
  30947. + }
  30948. +
  30949. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30950. + "klips_debug:ipsec_mast: "
  30951. + "Revectored rebuild_header dev=%s->%s ",
  30952. + skb->dev->name, prv->dev->name);
  30953. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30954. + "ip=%08x->%08x\n",
  30955. + (__u32)ntohl(skb->nh.iph->saddr),
  30956. + (__u32)ntohl(skb->nh.iph->daddr) );
  30957. + tmp = skb->dev;
  30958. + skb->dev = prv->dev;
  30959. +
  30960. + ret = prv->rebuild_header(skb);
  30961. + skb->dev = tmp;
  30962. + return ret;
  30963. +}
  30964. +
  30965. +DEBUG_NO_STATIC int
  30966. +ipsec_mast_set_mac_address(struct net_device *dev, void *addr)
  30967. +{
  30968. + struct ipsecpriv *prv = dev->priv;
  30969. +
  30970. + struct net_device_stats *stats; /* This device's statistics */
  30971. +
  30972. + if(dev == NULL) {
  30973. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30974. + "klips_debug:ipsec_mast_set_mac_address: "
  30975. + "no device...");
  30976. + return -ENODEV;
  30977. + }
  30978. +
  30979. + if(prv == NULL) {
  30980. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30981. + "klips_debug:ipsec_mast_set_mac_address: "
  30982. + "no private space associated with dev=%s",
  30983. + dev->name ? dev->name : "NULL");
  30984. + return -ENODEV;
  30985. + }
  30986. +
  30987. + stats = (struct net_device_stats *) &(prv->mystats);
  30988. +
  30989. + if(prv->dev == NULL) {
  30990. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  30991. + "klips_debug:ipsec_mast_set_mac_address: "
  30992. + "no physical device associated with dev=%s",
  30993. + dev->name ? dev->name : "NULL");
  30994. + stats->tx_dropped++;
  30995. + return -ENODEV;
  30996. + }
  30997. +
  30998. + if(!prv->set_mac_address) {
  30999. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31000. + "klips_debug:ipsec_mast_set_mac_address: "
  31001. + "physical device has been detached, cannot set - skb->dev=%s->NULL\n",
  31002. + dev->name);
  31003. + return -ENODEV;
  31004. + }
  31005. +
  31006. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31007. + "klips_debug:ipsec_mast_set_mac_address: "
  31008. + "Revectored dev=%s->%s addr=0p%p\n",
  31009. + dev->name, prv->dev->name, addr);
  31010. + return prv->set_mac_address(prv->dev, addr);
  31011. +
  31012. +}
  31013. +
  31014. +DEBUG_NO_STATIC void
  31015. +ipsec_mast_cache_update(struct hh_cache *hh, struct net_device *dev, unsigned char * haddr)
  31016. +{
  31017. + struct ipsecpriv *prv = dev->priv;
  31018. +
  31019. + struct net_device_stats *stats; /* This device's statistics */
  31020. +
  31021. + if(dev == NULL) {
  31022. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31023. + "klips_debug:ipsec_mast_cache_update: "
  31024. + "no device...");
  31025. + return;
  31026. + }
  31027. +
  31028. + if(prv == NULL) {
  31029. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31030. + "klips_debug:ipsec_mast_cache_update: "
  31031. + "no private space associated with dev=%s",
  31032. + dev->name ? dev->name : "NULL");
  31033. + return;
  31034. + }
  31035. +
  31036. + stats = (struct net_device_stats *) &(prv->mystats);
  31037. +
  31038. + if(prv->dev == NULL) {
  31039. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31040. + "klips_debug:ipsec_mast_cache_update: "
  31041. + "no physical device associated with dev=%s",
  31042. + dev->name ? dev->name : "NULL");
  31043. + stats->tx_dropped++;
  31044. + return;
  31045. + }
  31046. +
  31047. + if(!prv->header_cache_update) {
  31048. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31049. + "klips_debug:ipsec_mast_cache_update: "
  31050. + "physical device has been detached, cannot set - skb->dev=%s->NULL\n",
  31051. + dev->name);
  31052. + return;
  31053. + }
  31054. +
  31055. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31056. + "klips_debug:ipsec_mast: "
  31057. + "Revectored cache_update\n");
  31058. + prv->header_cache_update(hh, prv->dev, haddr);
  31059. + return;
  31060. +}
  31061. +
  31062. +DEBUG_NO_STATIC int
  31063. +ipsec_mast_neigh_setup(struct neighbour *n)
  31064. +{
  31065. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31066. + "klips_debug:ipsec_mast_neigh_setup:\n");
  31067. +
  31068. + if (n->nud_state == NUD_NONE) {
  31069. + n->ops = &arp_broken_ops;
  31070. + n->output = n->ops->output;
  31071. + }
  31072. + return 0;
  31073. +}
  31074. +
  31075. +DEBUG_NO_STATIC int
  31076. +ipsec_mast_neigh_setup_dev(struct net_device *dev, struct neigh_parms *p)
  31077. +{
  31078. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31079. + "klips_debug:ipsec_mast_neigh_setup_dev: "
  31080. + "setting up %s\n",
  31081. + dev ? dev->name : "NULL");
  31082. +
  31083. + if (p->tbl->family == AF_INET) {
  31084. + p->neigh_setup = ipsec_mast_neigh_setup;
  31085. + p->ucast_probes = 0;
  31086. + p->mcast_probes = 0;
  31087. + }
  31088. + return 0;
  31089. +}
  31090. +
  31091. +/*
  31092. + * We call the attach routine to attach another device.
  31093. + */
  31094. +
  31095. +DEBUG_NO_STATIC int
  31096. +ipsec_mast_attach(struct net_device *dev, struct net_device *physdev)
  31097. +{
  31098. + int i;
  31099. + struct ipsecpriv *prv = dev->priv;
  31100. +
  31101. + if(dev == NULL) {
  31102. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31103. + "klips_debug:ipsec_mast_attach: "
  31104. + "no device...");
  31105. + return -ENODEV;
  31106. + }
  31107. +
  31108. + if(prv == NULL) {
  31109. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31110. + "klips_debug:ipsec_mast_attach: "
  31111. + "no private space associated with dev=%s",
  31112. + dev->name ? dev->name : "NULL");
  31113. + return -ENODATA;
  31114. + }
  31115. +
  31116. + prv->dev = physdev;
  31117. + prv->hard_start_xmit = physdev->hard_start_xmit;
  31118. + prv->get_stats = physdev->get_stats;
  31119. +
  31120. + if (physdev->hard_header) {
  31121. + prv->hard_header = physdev->hard_header;
  31122. + dev->hard_header = ipsec_mast_hard_header;
  31123. + } else
  31124. + dev->hard_header = NULL;
  31125. +
  31126. + if (physdev->rebuild_header) {
  31127. + prv->rebuild_header = physdev->rebuild_header;
  31128. + dev->rebuild_header = ipsec_mast_rebuild_header;
  31129. + } else
  31130. + dev->rebuild_header = NULL;
  31131. +
  31132. + if (physdev->set_mac_address) {
  31133. + prv->set_mac_address = physdev->set_mac_address;
  31134. + dev->set_mac_address = ipsec_mast_set_mac_address;
  31135. + } else
  31136. + dev->set_mac_address = NULL;
  31137. +
  31138. + if (physdev->header_cache_update) {
  31139. + prv->header_cache_update = physdev->header_cache_update;
  31140. + dev->header_cache_update = ipsec_mast_cache_update;
  31141. + } else
  31142. + dev->header_cache_update = NULL;
  31143. +
  31144. + dev->hard_header_len = physdev->hard_header_len;
  31145. +
  31146. +/* prv->neigh_setup = physdev->neigh_setup; */
  31147. + dev->neigh_setup = ipsec_mast_neigh_setup_dev;
  31148. + dev->mtu = 16260; /* 0xfff0; */ /* dev->mtu; */
  31149. + prv->mtu = physdev->mtu;
  31150. +
  31151. +#ifdef PHYSDEV_TYPE
  31152. + dev->type = physdev->type; /* ARPHRD_MAST; */
  31153. +#endif /* PHYSDEV_TYPE */
  31154. +
  31155. + dev->addr_len = physdev->addr_len;
  31156. + for (i=0; i<dev->addr_len; i++) {
  31157. + dev->dev_addr[i] = physdev->dev_addr[i];
  31158. + }
  31159. +#ifdef CONFIG_KLIPS_DEBUG
  31160. + if(debug_mast & DB_MAST_INIT) {
  31161. + printk(KERN_INFO "klips_debug:ipsec_mast_attach: "
  31162. + "physical device %s being attached has HW address: %2x",
  31163. + physdev->name, physdev->dev_addr[0]);
  31164. + for (i=1; i < physdev->addr_len; i++) {
  31165. + printk(":%02x", physdev->dev_addr[i]);
  31166. + }
  31167. + printk("\n");
  31168. + }
  31169. +#endif /* CONFIG_KLIPS_DEBUG */
  31170. +
  31171. + return 0;
  31172. +}
  31173. +
  31174. +/*
  31175. + * We call the detach routine to detach the ipsec mast from another device.
  31176. + */
  31177. +
  31178. +DEBUG_NO_STATIC int
  31179. +ipsec_mast_detach(struct net_device *dev)
  31180. +{
  31181. + int i;
  31182. + struct ipsecpriv *prv = dev->priv;
  31183. +
  31184. + if(dev == NULL) {
  31185. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31186. + "klips_debug:ipsec_mast_detach: "
  31187. + "no device...");
  31188. + return -ENODEV;
  31189. + }
  31190. +
  31191. + if(prv == NULL) {
  31192. + KLIPS_PRINT(debug_mast & DB_MAST_REVEC,
  31193. + "klips_debug:ipsec_mast_detach: "
  31194. + "no private space associated with dev=%s",
  31195. + dev->name ? dev->name : "NULL");
  31196. + return -ENODATA;
  31197. + }
  31198. +
  31199. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31200. + "klips_debug:ipsec_mast_detach: "
  31201. + "physical device %s being detached from virtual device %s\n",
  31202. + prv->dev ? prv->dev->name : "NULL",
  31203. + dev->name);
  31204. +
  31205. + prv->dev = NULL;
  31206. + prv->hard_start_xmit = NULL;
  31207. + prv->get_stats = NULL;
  31208. +
  31209. + prv->hard_header = NULL;
  31210. +#ifdef DETACH_AND_DOWN
  31211. + dev->hard_header = NULL;
  31212. +#endif /* DETACH_AND_DOWN */
  31213. +
  31214. + prv->rebuild_header = NULL;
  31215. +#ifdef DETACH_AND_DOWN
  31216. + dev->rebuild_header = NULL;
  31217. +#endif /* DETACH_AND_DOWN */
  31218. +
  31219. + prv->set_mac_address = NULL;
  31220. +#ifdef DETACH_AND_DOWN
  31221. + dev->set_mac_address = NULL;
  31222. +#endif /* DETACH_AND_DOWN */
  31223. +
  31224. + prv->header_cache_update = NULL;
  31225. +#ifdef DETACH_AND_DOWN
  31226. + dev->header_cache_update = NULL;
  31227. +#endif /* DETACH_AND_DOWN */
  31228. +
  31229. +#ifdef DETACH_AND_DOWN
  31230. + dev->neigh_setup = NULL;
  31231. +#endif /* DETACH_AND_DOWN */
  31232. +
  31233. + dev->hard_header_len = 0;
  31234. +#ifdef DETACH_AND_DOWN
  31235. + dev->mtu = 0;
  31236. +#endif /* DETACH_AND_DOWN */
  31237. + prv->mtu = 0;
  31238. + for (i=0; i<MAX_ADDR_LEN; i++) {
  31239. + dev->dev_addr[i] = 0;
  31240. + }
  31241. + dev->addr_len = 0;
  31242. +#ifdef PHYSDEV_TYPE
  31243. + dev->type = ARPHRD_VOID; /* ARPHRD_MAST; */
  31244. +#endif /* PHYSDEV_TYPE */
  31245. +
  31246. + return 0;
  31247. +}
  31248. +
  31249. +/*
  31250. + * We call the clear routine to detach all ipsec masts from other devices.
  31251. + */
  31252. +DEBUG_NO_STATIC int
  31253. +ipsec_mast_clear(void)
  31254. +{
  31255. + int i;
  31256. + struct net_device *ipsecdev = NULL, *prvdev;
  31257. + struct ipsecpriv *prv;
  31258. + char name[9];
  31259. + int ret;
  31260. +
  31261. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31262. + "klips_debug:ipsec_mast_clear: .\n");
  31263. +
  31264. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  31265. + sprintf(name, IPSEC_DEV_FORMAT, i);
  31266. + if((ipsecdev = ipsec_dev_get(name)) != NULL) {
  31267. + if((prv = (struct ipsecpriv *)(ipsecdev->priv))) {
  31268. + prvdev = (struct net_device *)(prv->dev);
  31269. + if(prvdev) {
  31270. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31271. + "klips_debug:ipsec_mast_clear: "
  31272. + "physical device for device %s is %s\n",
  31273. + name, prvdev->name);
  31274. + if((ret = ipsec_mast_detach(ipsecdev))) {
  31275. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31276. + "klips_debug:ipsec_mast_clear: "
  31277. + "error %d detatching device %s from device %s.\n",
  31278. + ret, name, prvdev->name);
  31279. + return ret;
  31280. + }
  31281. + }
  31282. + }
  31283. + }
  31284. + }
  31285. + return 0;
  31286. +}
  31287. +
  31288. +DEBUG_NO_STATIC int
  31289. +ipsec_mast_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  31290. +{
  31291. + struct ipsecmastconf *cf = (struct ipsecmastconf *)&ifr->ifr_data;
  31292. + struct ipsecpriv *prv = dev->priv;
  31293. + struct net_device *them; /* physical device */
  31294. +#ifdef CONFIG_IP_ALIAS
  31295. + char *colon;
  31296. + char realphysname[IFNAMSIZ];
  31297. +#endif /* CONFIG_IP_ALIAS */
  31298. +
  31299. + if(dev == NULL) {
  31300. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31301. + "klips_debug:ipsec_mast_ioctl: "
  31302. + "device not supplied.\n");
  31303. + return -ENODEV;
  31304. + }
  31305. +
  31306. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31307. + "klips_debug:ipsec_mast_ioctl: "
  31308. + "tncfg service call #%d for dev=%s\n",
  31309. + cmd,
  31310. + dev->name ? dev->name : "NULL");
  31311. + switch (cmd) {
  31312. + /* attach a virtual ipsec? device to a physical device */
  31313. + case IPSEC_SET_DEV:
  31314. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31315. + "klips_debug:ipsec_mast_ioctl: "
  31316. + "calling ipsec_mast_attatch...\n");
  31317. +#ifdef CONFIG_IP_ALIAS
  31318. + /* If this is an IP alias interface, get its real physical name */
  31319. + strncpy(realphysname, cf->cf_name, IFNAMSIZ);
  31320. + realphysname[IFNAMSIZ-1] = 0;
  31321. + colon = strchr(realphysname, ':');
  31322. + if (colon) *colon = 0;
  31323. + them = ipsec_dev_get(realphysname);
  31324. +#else /* CONFIG_IP_ALIAS */
  31325. + them = ipsec_dev_get(cf->cf_name);
  31326. +#endif /* CONFIG_IP_ALIAS */
  31327. +
  31328. + if (them == NULL) {
  31329. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31330. + "klips_debug:ipsec_mast_ioctl: "
  31331. + "physical device %s requested is null\n",
  31332. + cf->cf_name);
  31333. + return -ENXIO;
  31334. + }
  31335. +
  31336. +#if 0
  31337. + if (them->flags & IFF_UP) {
  31338. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31339. + "klips_debug:ipsec_mast_ioctl: "
  31340. + "physical device %s requested is not up.\n",
  31341. + cf->cf_name);
  31342. + return -ENXIO;
  31343. + }
  31344. +#endif
  31345. +
  31346. + if (prv && prv->dev) {
  31347. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31348. + "klips_debug:ipsec_mast_ioctl: "
  31349. + "virtual device is already connected to %s.\n",
  31350. + prv->dev->name ? prv->dev->name : "NULL");
  31351. + return -EBUSY;
  31352. + }
  31353. + return ipsec_mast_attach(dev, them);
  31354. +
  31355. + case IPSEC_DEL_DEV:
  31356. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31357. + "klips_debug:ipsec_mast_ioctl: "
  31358. + "calling ipsec_mast_detatch.\n");
  31359. + if (! prv->dev) {
  31360. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31361. + "klips_debug:ipsec_mast_ioctl: "
  31362. + "physical device not connected.\n");
  31363. + return -ENODEV;
  31364. + }
  31365. + return ipsec_mast_detach(dev);
  31366. +
  31367. + case IPSEC_CLR_DEV:
  31368. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31369. + "klips_debug:ipsec_mast_ioctl: "
  31370. + "calling ipsec_mast_clear.\n");
  31371. + return ipsec_mast_clear();
  31372. +
  31373. + default:
  31374. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31375. + "klips_debug:ipsec_mast_ioctl: "
  31376. + "unknown command %d.\n",
  31377. + cmd);
  31378. + return -EOPNOTSUPP;
  31379. + }
  31380. +}
  31381. +
  31382. +int
  31383. +ipsec_mast_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
  31384. +{
  31385. + struct net_device *dev = ptr;
  31386. + struct net_device *ipsec_dev;
  31387. + struct ipsecpriv *priv;
  31388. + char name[9];
  31389. + int i;
  31390. +
  31391. + if (dev == NULL) {
  31392. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31393. + "klips_debug:ipsec_mast_device_event: "
  31394. + "dev=NULL for event type %ld.\n",
  31395. + event);
  31396. + return(NOTIFY_DONE);
  31397. + }
  31398. +
  31399. + /* check for loopback devices */
  31400. + if (dev && (dev->flags & IFF_LOOPBACK)) {
  31401. + return(NOTIFY_DONE);
  31402. + }
  31403. +
  31404. + switch (event) {
  31405. + case NETDEV_DOWN:
  31406. + /* look very carefully at the scope of these compiler
  31407. + directives before changing anything... -- RGB */
  31408. +
  31409. + case NETDEV_UNREGISTER:
  31410. + switch (event) {
  31411. + case NETDEV_DOWN:
  31412. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31413. + "klips_debug:ipsec_mast_device_event: "
  31414. + "NETDEV_DOWN dev=%s flags=%x\n",
  31415. + dev->name,
  31416. + dev->flags);
  31417. + if(strncmp(dev->name, "ipsec", strlen("ipsec")) == 0) {
  31418. + printk(KERN_CRIT "IPSEC EVENT: KLIPS device %s shut down.\n",
  31419. + dev->name);
  31420. + }
  31421. + break;
  31422. + case NETDEV_UNREGISTER:
  31423. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31424. + "klips_debug:ipsec_mast_device_event: "
  31425. + "NETDEV_UNREGISTER dev=%s flags=%x\n",
  31426. + dev->name,
  31427. + dev->flags);
  31428. + break;
  31429. + }
  31430. +
  31431. + /* find the attached physical device and detach it. */
  31432. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  31433. + sprintf(name, IPSEC_DEV_FORMAT, i);
  31434. + ipsec_dev = ipsec_dev_get(name);
  31435. + if(ipsec_dev) {
  31436. + priv = (struct ipsecpriv *)(ipsec_dev->priv);
  31437. + if(priv) {
  31438. + ;
  31439. + if(((struct net_device *)(priv->dev)) == dev) {
  31440. + /* dev_close(ipsec_dev); */
  31441. + /* return */ ipsec_mast_detach(ipsec_dev);
  31442. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31443. + "klips_debug:ipsec_mast_device_event: "
  31444. + "device '%s' has been detached.\n",
  31445. + ipsec_dev->name);
  31446. + break;
  31447. + }
  31448. + } else {
  31449. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31450. + "klips_debug:ipsec_mast_device_event: "
  31451. + "device '%s' has no private data space!\n",
  31452. + ipsec_dev->name);
  31453. + }
  31454. + }
  31455. + }
  31456. + break;
  31457. + case NETDEV_UP:
  31458. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31459. + "klips_debug:ipsec_mast_device_event: "
  31460. + "NETDEV_UP dev=%s\n",
  31461. + dev->name);
  31462. + break;
  31463. + case NETDEV_REBOOT:
  31464. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31465. + "klips_debug:ipsec_mast_device_event: "
  31466. + "NETDEV_REBOOT dev=%s\n",
  31467. + dev->name);
  31468. + break;
  31469. + case NETDEV_CHANGE:
  31470. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31471. + "klips_debug:ipsec_mast_device_event: "
  31472. + "NETDEV_CHANGE dev=%s flags=%x\n",
  31473. + dev->name,
  31474. + dev->flags);
  31475. + break;
  31476. + case NETDEV_REGISTER:
  31477. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31478. + "klips_debug:ipsec_mast_device_event: "
  31479. + "NETDEV_REGISTER dev=%s\n",
  31480. + dev->name);
  31481. + break;
  31482. + case NETDEV_CHANGEMTU:
  31483. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31484. + "klips_debug:ipsec_mast_device_event: "
  31485. + "NETDEV_CHANGEMTU dev=%s to mtu=%d\n",
  31486. + dev->name,
  31487. + dev->mtu);
  31488. + break;
  31489. + case NETDEV_CHANGEADDR:
  31490. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31491. + "klips_debug:ipsec_mast_device_event: "
  31492. + "NETDEV_CHANGEADDR dev=%s\n",
  31493. + dev->name);
  31494. + break;
  31495. + case NETDEV_GOING_DOWN:
  31496. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31497. + "klips_debug:ipsec_mast_device_event: "
  31498. + "NETDEV_GOING_DOWN dev=%s\n",
  31499. + dev->name);
  31500. + break;
  31501. + case NETDEV_CHANGENAME:
  31502. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31503. + "klips_debug:ipsec_mast_device_event: "
  31504. + "NETDEV_CHANGENAME dev=%s\n",
  31505. + dev->name);
  31506. + break;
  31507. + default:
  31508. + KLIPS_PRINT(debug_mast & DB_MAST_INIT,
  31509. + "klips_debug:ipsec_mast_device_event: "
  31510. + "event type %ld unrecognised for dev=%s\n",
  31511. + event,
  31512. + dev->name);
  31513. + break;
  31514. + }
  31515. + return NOTIFY_DONE;
  31516. +}
  31517. +
  31518. +/*
  31519. + * Called when an ipsec mast device is initialized.
  31520. + * The ipsec mast device structure is passed to us.
  31521. + */
  31522. +
  31523. +int
  31524. +ipsec_mast_init(struct net_device *dev)
  31525. +{
  31526. + int i;
  31527. +
  31528. + KLIPS_PRINT(debug_mast,
  31529. + "klips_debug:ipsec_mast_init: "
  31530. + "allocating %lu bytes initialising device: %s\n",
  31531. + (unsigned long) sizeof(struct ipsecpriv),
  31532. + dev->name ? dev->name : "NULL");
  31533. +
  31534. + /* Add our mast functions to the device */
  31535. + dev->open = ipsec_mast_open;
  31536. + dev->stop = ipsec_mast_close;
  31537. + dev->hard_start_xmit = ipsec_mast_start_xmit;
  31538. + dev->get_stats = ipsec_mast_get_stats;
  31539. +
  31540. + dev->priv = kmalloc(sizeof(struct ipsecpriv), GFP_KERNEL);
  31541. + if (dev->priv == NULL)
  31542. + return -ENOMEM;
  31543. + memset((caddr_t)(dev->priv), 0, sizeof(struct ipsecpriv));
  31544. +
  31545. + for(i = 0; i < sizeof(zeroes); i++) {
  31546. + ((__u8*)(zeroes))[i] = 0;
  31547. + }
  31548. +
  31549. + dev->set_multicast_list = NULL;
  31550. + dev->do_ioctl = ipsec_mast_ioctl;
  31551. + dev->hard_header = NULL;
  31552. + dev->rebuild_header = NULL;
  31553. + dev->set_mac_address = NULL;
  31554. + dev->header_cache_update= NULL;
  31555. + dev->neigh_setup = ipsec_mast_neigh_setup_dev;
  31556. + dev->hard_header_len = 0;
  31557. + dev->mtu = 0;
  31558. + dev->addr_len = 0;
  31559. + dev->type = ARPHRD_VOID; /* ARPHRD_MAST; */ /* ARPHRD_ETHER; */
  31560. + dev->tx_queue_len = 10; /* Small queue */
  31561. + memset((caddr_t)(dev->broadcast),0xFF, ETH_ALEN); /* what if this is not attached to ethernet? */
  31562. +
  31563. + /* New-style flags. */
  31564. + dev->flags = IFF_NOARP /* 0 */ /* Petr Novak */;
  31565. + dev_init_buffers(dev);
  31566. +
  31567. + /* We're done. Have I forgotten anything? */
  31568. + return 0;
  31569. +}
  31570. +
  31571. +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  31572. +/* Module specific interface (but it links with the rest of IPSEC) */
  31573. +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  31574. +
  31575. +int
  31576. +ipsec_mast_probe(struct net_device *dev)
  31577. +{
  31578. + ipsec_mast_init(dev);
  31579. + return 0;
  31580. +}
  31581. +
  31582. +int
  31583. +ipsec_mast_init_devices(void)
  31584. +{
  31585. + return 0;
  31586. +}
  31587. +
  31588. +/* void */
  31589. +int
  31590. +ipsec_mast_cleanup_devices(void)
  31591. +{
  31592. + int error = 0;
  31593. + int i;
  31594. + char name[10];
  31595. + struct net_device *dev_mast;
  31596. +
  31597. + for(i = 0; i < ipsec_mastdevice_count; i++) {
  31598. + sprintf(name, MAST_DEV_FORMAT, i);
  31599. + if((dev_mast = ipsec_dev_get(name)) == NULL) {
  31600. + break;
  31601. + }
  31602. + unregister_netdev(dev_mast);
  31603. + kfree(dev_mast->priv);
  31604. + dev_mast->priv=NULL;
  31605. + }
  31606. + return error;
  31607. +}
  31608. +
  31609. +/*
  31610. + * $Log: ipsec_mast.c,v $
  31611. + * Revision 1.7.2.1 2006/10/06 21:39:26 paul
  31612. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  31613. + * set. This is defined through autoconf.h which is included through the
  31614. + * linux kernel build macros.
  31615. + *
  31616. + * Revision 1.7 2005/04/29 05:10:22 mcr
  31617. + * removed from extraenous includes to make unit testing easier.
  31618. + *
  31619. + * Revision 1.6 2004/12/03 21:25:57 mcr
  31620. + * compile time fixes for running on 2.6.
  31621. + * still experimental.
  31622. + *
  31623. + * Revision 1.5 2004/08/03 18:19:08 mcr
  31624. + * in 2.6, use "net_device" instead of #define device->net_device.
  31625. + * this probably breaks 2.0 compiles.
  31626. + *
  31627. + * Revision 1.4 2004/07/10 19:11:18 mcr
  31628. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  31629. + *
  31630. + * Revision 1.3 2003/10/31 02:27:55 mcr
  31631. + * pulled up port-selector patches and sa_id elimination.
  31632. + *
  31633. + * Revision 1.2.4.1 2003/10/29 01:30:41 mcr
  31634. + * elimited "struct sa_id".
  31635. + *
  31636. + * Revision 1.2 2003/06/22 20:06:17 mcr
  31637. + * refactored mast code still had lots of ipsecX junk in it.
  31638. + *
  31639. + * Revision 1.1 2003/02/12 19:31:12 rgb
  31640. + * Refactored from ipsec_tunnel.c
  31641. + *
  31642. + */
  31643. --- /dev/null Tue Mar 11 13:02:56 2003
  31644. +++ linux/net/ipsec/ipsec_md5c.c Mon Feb 9 13:51:03 2004
  31645. @@ -0,0 +1,453 @@
  31646. +/*
  31647. + * RCSID $Id: ipsec_md5c.c,v 1.10 2005/04/15 01:25:57 mcr Exp $
  31648. + */
  31649. +
  31650. +/*
  31651. + * The rest of the code is derived from MD5C.C by RSADSI. Minor cosmetic
  31652. + * changes to accomodate it in the kernel by ji.
  31653. + */
  31654. +
  31655. +#include <asm/byteorder.h>
  31656. +#include <linux/string.h>
  31657. +
  31658. +#include "openswan/ipsec_md5h.h"
  31659. +
  31660. +/* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
  31661. + */
  31662. +
  31663. +/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  31664. +rights reserved.
  31665. +
  31666. +License to copy and use this software is granted provided that it
  31667. +is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  31668. +Algorithm" in all material mentioning or referencing this software
  31669. +or this function.
  31670. +
  31671. +License is also granted to make and use derivative works provided
  31672. +that such works are identified as "derived from the RSA Data
  31673. +Security, Inc. MD5 Message-Digest Algorithm" in all material
  31674. +mentioning or referencing the derived work.
  31675. +
  31676. +RSA Data Security, Inc. makes no representations concerning either
  31677. +the merchantability of this software or the suitability of this
  31678. +software for any particular purpose. It is provided "as is"
  31679. +without express or implied warranty of any kind.
  31680. +
  31681. +These notices must be retained in any copies of any part of this
  31682. +documentation and/or software.
  31683. + */
  31684. +
  31685. +/*
  31686. + * Additions by JI
  31687. + *
  31688. + * HAVEMEMCOPY is defined if mem* routines are available
  31689. + *
  31690. + * HAVEHTON is defined if htons() and htonl() can be used
  31691. + * for big/little endian conversions
  31692. + *
  31693. + */
  31694. +
  31695. +#define HAVEMEMCOPY
  31696. +#ifdef __LITTLE_ENDIAN
  31697. +#define LITTLENDIAN
  31698. +#endif
  31699. +#ifdef __BIG_ENDIAN
  31700. +#define BIGENDIAN
  31701. +#endif
  31702. +
  31703. +/* Constants for MD5Transform routine.
  31704. + */
  31705. +
  31706. +#define S11 7
  31707. +#define S12 12
  31708. +#define S13 17
  31709. +#define S14 22
  31710. +#define S21 5
  31711. +#define S22 9
  31712. +#define S23 14
  31713. +#define S24 20
  31714. +#define S31 4
  31715. +#define S32 11
  31716. +#define S33 16
  31717. +#define S34 23
  31718. +#define S41 6
  31719. +#define S42 10
  31720. +#define S43 15
  31721. +#define S44 21
  31722. +
  31723. +static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
  31724. +
  31725. +#ifdef LITTLEENDIAN
  31726. +#define Encode MD5_memcpy
  31727. +#define Decode MD5_memcpy
  31728. +#else
  31729. +static void Encode PROTO_LIST
  31730. + ((unsigned char *, UINT4 *, unsigned int));
  31731. +static void Decode PROTO_LIST
  31732. + ((UINT4 *, unsigned char *, unsigned int));
  31733. +#endif
  31734. +
  31735. +#ifdef HAVEMEMCOPY
  31736. +/* no need to include <memory.h> here; <linux/string.h> defines these */
  31737. +#define MD5_memcpy memcpy
  31738. +#define MD5_memset memset
  31739. +#else
  31740. +#ifdef HAVEBCOPY
  31741. +#define MD5_memcpy(_a,_b,_c) bcopy((_b),(_a),(_c))
  31742. +#define MD5_memset(_a,_b,_c) bzero((_a),(_c))
  31743. +#else
  31744. +static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
  31745. +static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
  31746. +#endif
  31747. +#endif
  31748. +static unsigned char PADDING[64] = {
  31749. + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  31750. + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  31751. + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  31752. +};
  31753. +
  31754. +/* F, G, H and I are basic MD5 functions.
  31755. + */
  31756. +#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  31757. +#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  31758. +#define H(x, y, z) ((x) ^ (y) ^ (z))
  31759. +#define I(x, y, z) ((y) ^ ((x) | (~z)))
  31760. +
  31761. +/* ROTATE_LEFT rotates x left n bits.
  31762. + */
  31763. +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  31764. +
  31765. +/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  31766. +Rotation is separate from addition to prevent recomputation.
  31767. + */
  31768. +#define FF(a, b, c, d, x, s, ac) { \
  31769. + (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
  31770. + (a) = ROTATE_LEFT ((a), (s)); \
  31771. + (a) += (b); \
  31772. + }
  31773. +#define GG(a, b, c, d, x, s, ac) { \
  31774. + (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
  31775. + (a) = ROTATE_LEFT ((a), (s)); \
  31776. + (a) += (b); \
  31777. + }
  31778. +#define HH(a, b, c, d, x, s, ac) { \
  31779. + (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
  31780. + (a) = ROTATE_LEFT ((a), (s)); \
  31781. + (a) += (b); \
  31782. + }
  31783. +#define II(a, b, c, d, x, s, ac) { \
  31784. + (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
  31785. + (a) = ROTATE_LEFT ((a), (s)); \
  31786. + (a) += (b); \
  31787. + }
  31788. +
  31789. +/*
  31790. + * MD5 initialization. Begins an MD5 operation, writing a new context.
  31791. + */
  31792. +void osMD5Init(void *vcontext)
  31793. +{
  31794. + MD5_CTX *context = vcontext;
  31795. +
  31796. + context->count[0] = context->count[1] = 0;
  31797. + /* Load magic initialization constants.*/
  31798. + context->state[0] = 0x67452301;
  31799. + context->state[1] = 0xefcdab89;
  31800. + context->state[2] = 0x98badcfe;
  31801. + context->state[3] = 0x10325476;
  31802. +}
  31803. +
  31804. +/* MD5 block update operation. Continues an MD5 message-digest
  31805. + operation, processing another message block, and updating the
  31806. + context.
  31807. + */
  31808. +void osMD5Update (vcontext, input, inputLen)
  31809. + void *vcontext;
  31810. + unsigned char *input; /* input block */
  31811. + __u32 inputLen; /* length of input block */
  31812. +{
  31813. + MD5_CTX *context = vcontext;
  31814. + __u32 i;
  31815. + unsigned int index, partLen;
  31816. +
  31817. + /* Compute number of bytes mod 64 */
  31818. + index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  31819. +
  31820. + /* Update number of bits */
  31821. + if ((context->count[0] += ((UINT4)inputLen << 3))
  31822. + < ((UINT4)inputLen << 3))
  31823. + context->count[1]++;
  31824. + context->count[1] += ((UINT4)inputLen >> 29);
  31825. +
  31826. + partLen = 64 - index;
  31827. +
  31828. + /* Transform as many times as possible.
  31829. +*/
  31830. + if (inputLen >= partLen) {
  31831. + MD5_memcpy
  31832. + ((POINTER)&context->buffer[index], (POINTER)input, partLen);
  31833. + MD5Transform (context->state, context->buffer);
  31834. +
  31835. + for (i = partLen; i + 63 < inputLen; i += 64)
  31836. + MD5Transform (context->state, &input[i]);
  31837. +
  31838. + index = 0;
  31839. + }
  31840. + else
  31841. + i = 0;
  31842. +
  31843. + /* Buffer remaining input */
  31844. + MD5_memcpy
  31845. + ((POINTER)&context->buffer[index], (POINTER)&input[i],
  31846. + inputLen-i);
  31847. +}
  31848. +
  31849. +/* MD5 finalization. Ends an MD5 message-digest operation, writing the
  31850. + the message digest and zeroizing the context.
  31851. + */
  31852. +void osMD5Final (digest, vcontext)
  31853. +unsigned char digest[16]; /* message digest */
  31854. +void *vcontext; /* context */
  31855. +{
  31856. + MD5_CTX *context = vcontext;
  31857. + unsigned char bits[8];
  31858. + unsigned int index, padLen;
  31859. +
  31860. + /* Save number of bits */
  31861. + Encode (bits, context->count, 8);
  31862. +
  31863. + /* Pad out to 56 mod 64.
  31864. +*/
  31865. + index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  31866. + padLen = (index < 56) ? (56 - index) : (120 - index);
  31867. + osMD5Update (context, PADDING, padLen);
  31868. +
  31869. + /* Append length (before padding) */
  31870. + osMD5Update (context, bits, 8);
  31871. +
  31872. + if (digest != NULL) /* Bill Simpson's padding */
  31873. + {
  31874. + /* store state in digest */
  31875. + Encode (digest, context->state, 16);
  31876. +
  31877. + /* Zeroize sensitive information.
  31878. + */
  31879. + MD5_memset ((POINTER)context, 0, sizeof (*context));
  31880. + }
  31881. +}
  31882. +
  31883. +/* MD5 basic transformation. Transforms state based on block.
  31884. + */
  31885. +static void MD5Transform (state, block)
  31886. +UINT4 state[4];
  31887. +unsigned char block[64];
  31888. +{
  31889. + UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  31890. +
  31891. + Decode (x, block, 64);
  31892. +
  31893. + /* Round 1 */
  31894. + FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  31895. + FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  31896. + FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  31897. + FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  31898. + FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  31899. + FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  31900. + FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  31901. + FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  31902. + FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  31903. + FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  31904. + FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  31905. + FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  31906. + FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  31907. + FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  31908. + FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  31909. + FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  31910. +
  31911. + /* Round 2 */
  31912. + GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  31913. + GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  31914. + GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  31915. + GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  31916. + GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  31917. + GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  31918. + GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  31919. + GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  31920. + GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  31921. + GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  31922. + GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  31923. + GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  31924. + GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  31925. + GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  31926. + GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  31927. + GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  31928. +
  31929. + /* Round 3 */
  31930. + HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  31931. + HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  31932. + HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  31933. + HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  31934. + HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  31935. + HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  31936. + HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  31937. + HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  31938. + HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  31939. + HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  31940. + HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  31941. + HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  31942. + HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  31943. + HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  31944. + HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  31945. + HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  31946. +
  31947. + /* Round 4 */
  31948. + II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  31949. + II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  31950. + II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  31951. + II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  31952. + II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  31953. + II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  31954. + II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  31955. + II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  31956. + II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  31957. + II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  31958. + II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  31959. + II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  31960. + II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  31961. + II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  31962. + II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  31963. + II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  31964. +
  31965. + state[0] += a;
  31966. + state[1] += b;
  31967. + state[2] += c;
  31968. + state[3] += d;
  31969. +
  31970. + /* Zeroize sensitive information.
  31971. +*/
  31972. + MD5_memset ((POINTER)x, 0, sizeof (x));
  31973. +}
  31974. +
  31975. +#ifndef LITTLEENDIAN
  31976. +
  31977. +/* Encodes input (UINT4) into output (unsigned char). Assumes len is
  31978. + a multiple of 4.
  31979. + */
  31980. +static void Encode (output, input, len)
  31981. +unsigned char *output;
  31982. +UINT4 *input;
  31983. +unsigned int len;
  31984. +{
  31985. + unsigned int i, j;
  31986. +
  31987. + for (i = 0, j = 0; j < len; i++, j += 4) {
  31988. + output[j] = (unsigned char)(input[i] & 0xff);
  31989. + output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  31990. + output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  31991. + output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  31992. + }
  31993. +}
  31994. +
  31995. +/* Decodes input (unsigned char) into output (UINT4). Assumes len is
  31996. + a multiple of 4.
  31997. + */
  31998. +static void Decode (output, input, len)
  31999. +UINT4 *output;
  32000. +unsigned char *input;
  32001. +unsigned int len;
  32002. +{
  32003. + unsigned int i, j;
  32004. +
  32005. + for (i = 0, j = 0; j < len; i++, j += 4)
  32006. + output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
  32007. + (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
  32008. +}
  32009. +
  32010. +#endif
  32011. +
  32012. +#ifndef HAVEMEMCOPY
  32013. +#ifndef HAVEBCOPY
  32014. +/* Note: Replace "for loop" with standard memcpy if possible.
  32015. + */
  32016. +
  32017. +static void MD5_memcpy (output, input, len)
  32018. +POINTER output;
  32019. +POINTER input;
  32020. +unsigned int len;
  32021. +{
  32022. + unsigned int i;
  32023. +
  32024. + for (i = 0; i < len; i++)
  32025. +
  32026. + output[i] = input[i];
  32027. +}
  32028. +
  32029. +/* Note: Replace "for loop" with standard memset if possible.
  32030. + */
  32031. +
  32032. +static void MD5_memset (output, value, len)
  32033. +POINTER output;
  32034. +int value;
  32035. +unsigned int len;
  32036. +{
  32037. + unsigned int i;
  32038. +
  32039. + for (i = 0; i < len; i++)
  32040. + ((char *)output)[i] = (char)value;
  32041. +}
  32042. +#endif
  32043. +#endif
  32044. +
  32045. +/*
  32046. + * $Log: ipsec_md5c.c,v $
  32047. + * Revision 1.10 2005/04/15 01:25:57 mcr
  32048. + * minor fix to comments.
  32049. + *
  32050. + * Revision 1.9 2004/09/08 17:21:36 ken
  32051. + * Rename MD5* -> osMD5 functions to prevent clashes with other symbols exported by kernel modules (CIFS in 2.6 initiated this)
  32052. + *
  32053. + * Revision 1.8 2004/04/06 02:49:26 mcr
  32054. + * pullup of algo code from alg-branch.
  32055. + *
  32056. + * Revision 1.7 2002/09/10 01:45:14 mcr
  32057. + * changed type of MD5_CTX and SHA1_CTX to void * so that
  32058. + * the function prototypes would match, and could be placed
  32059. + * into a pointer to a function.
  32060. + *
  32061. + * Revision 1.6 2002/04/24 07:55:32 mcr
  32062. + * #include patches and Makefiles for post-reorg compilation.
  32063. + *
  32064. + * Revision 1.5 2002/04/24 07:36:28 mcr
  32065. + * Moved from ./klips/net/ipsec/ipsec_md5c.c,v
  32066. + *
  32067. + * Revision 1.4 1999/12/13 13:59:12 rgb
  32068. + * Quick fix to argument size to Update bugs.
  32069. + *
  32070. + * Revision 1.3 1999/05/21 18:09:28 henry
  32071. + * unnecessary <memory.h> include causes trouble in 2.2
  32072. + *
  32073. + * Revision 1.2 1999/04/06 04:54:26 rgb
  32074. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  32075. + * patch shell fixes.
  32076. + *
  32077. + * Revision 1.1 1998/06/18 21:27:48 henry
  32078. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  32079. + * kernel-build scripts happier in the presence of symlinks
  32080. + *
  32081. + * Revision 1.2 1998/04/23 20:54:02 rgb
  32082. + * Fixed md5 and sha1 include file nesting issues, to be cleaned up when
  32083. + * verified.
  32084. + *
  32085. + * Revision 1.1 1998/04/09 03:06:08 henry
  32086. + * sources moved up from linux/net/ipsec
  32087. + *
  32088. + * Revision 1.1.1.1 1998/04/08 05:35:04 henry
  32089. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  32090. + *
  32091. + * Revision 0.3 1996/11/20 14:48:53 ji
  32092. + * Release update only.
  32093. + *
  32094. + * Revision 0.2 1996/11/02 00:18:33 ji
  32095. + * First limited release.
  32096. + *
  32097. + *
  32098. + */
  32099. --- /dev/null Tue Mar 11 13:02:56 2003
  32100. +++ linux/net/ipsec/ipsec_proc.c Mon Feb 9 13:51:03 2004
  32101. @@ -0,0 +1,1186 @@
  32102. +/*
  32103. + * @(#) /proc file system interface code.
  32104. + *
  32105. + * Copyright (C) 1996, 1997 John Ioannidis.
  32106. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs <rgb@freeswan.org>
  32107. + * 2001 Michael Richardson <mcr@freeswan.org>
  32108. + *
  32109. + * This program is free software; you can redistribute it and/or modify it
  32110. + * under the terms of the GNU General Public License as published by the
  32111. + * Free Software Foundation; either version 2 of the License, or (at your
  32112. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  32113. + *
  32114. + * This program is distributed in the hope that it will be useful, but
  32115. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  32116. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  32117. + * for more details.
  32118. + *
  32119. + * Split out from ipsec_init.c version 1.70.
  32120. + */
  32121. +
  32122. +char ipsec_proc_c_version[] = "RCSID $Id: ipsec_proc.c,v 1.39.2.4 2006/11/15 22:21:39 paul Exp $";
  32123. +
  32124. +
  32125. +#ifndef AUTOCONF_INCLUDED
  32126. +#include <linux/config.h>
  32127. +#endif
  32128. +#include <linux/version.h>
  32129. +#define __NO_VERSION__
  32130. +#include <linux/module.h>
  32131. +#include <linux/kernel.h> /* printk() */
  32132. +
  32133. +#include "openswan/ipsec_kversion.h"
  32134. +#include "openswan/ipsec_param.h"
  32135. +
  32136. +#ifdef MALLOC_SLAB
  32137. +# include <linux/slab.h> /* kmalloc() */
  32138. +#else /* MALLOC_SLAB */
  32139. +# include <linux/malloc.h> /* kmalloc() */
  32140. +#endif /* MALLOC_SLAB */
  32141. +#include <linux/errno.h> /* error codes */
  32142. +#include <linux/types.h> /* size_t */
  32143. +#include <linux/interrupt.h> /* mark_bh */
  32144. +
  32145. +#include <linux/netdevice.h> /* struct device, and other headers */
  32146. +#include <linux/etherdevice.h> /* eth_type_trans */
  32147. +#include <linux/ip.h> /* struct iphdr */
  32148. +#include <linux/in.h> /* struct sockaddr_in */
  32149. +#include <linux/skbuff.h>
  32150. +#include <asm/uaccess.h> /* copy_from_user */
  32151. +#include <openswan.h>
  32152. +#ifdef SPINLOCK
  32153. +#ifdef SPINLOCK_23
  32154. +#include <linux/spinlock.h> /* *lock* */
  32155. +#else /* SPINLOCK_23 */
  32156. +#include <asm/spinlock.h> /* *lock* */
  32157. +#endif /* SPINLOCK_23 */
  32158. +#endif /* SPINLOCK */
  32159. +
  32160. +#include <net/ip.h>
  32161. +#ifdef CONFIG_PROC_FS
  32162. +#include <linux/proc_fs.h>
  32163. +#endif /* CONFIG_PROC_FS */
  32164. +#ifdef NETLINK_SOCK
  32165. +#include <linux/netlink.h>
  32166. +#else
  32167. +#include <net/netlink.h>
  32168. +#endif
  32169. +
  32170. +#include "openswan/radij.h"
  32171. +
  32172. +#include "openswan/ipsec_life.h"
  32173. +#include "openswan/ipsec_stats.h"
  32174. +#include "openswan/ipsec_sa.h"
  32175. +
  32176. +#include "openswan/ipsec_encap.h"
  32177. +#include "openswan/ipsec_radij.h"
  32178. +#include "openswan/ipsec_xform.h"
  32179. +#include "openswan/ipsec_tunnel.h"
  32180. +#include "openswan/ipsec_xmit.h"
  32181. +
  32182. +#include "openswan/ipsec_rcv.h"
  32183. +#include "openswan/ipsec_ah.h"
  32184. +#include "openswan/ipsec_esp.h"
  32185. +#include "openswan/ipsec_kern24.h"
  32186. +
  32187. +#ifdef CONFIG_KLIPS_IPCOMP
  32188. +#include "openswan/ipcomp.h"
  32189. +#endif /* CONFIG_KLIPS_IPCOMP */
  32190. +
  32191. +#include "openswan/ipsec_proto.h"
  32192. +
  32193. +#include <pfkeyv2.h>
  32194. +#include <pfkey.h>
  32195. +
  32196. +#ifdef CONFIG_PROC_FS
  32197. +
  32198. +#ifdef IPSEC_PROC_SUBDIRS
  32199. +static struct proc_dir_entry *proc_net_ipsec_dir = NULL;
  32200. +static struct proc_dir_entry *proc_eroute_dir = NULL;
  32201. +static struct proc_dir_entry *proc_spi_dir = NULL;
  32202. +static struct proc_dir_entry *proc_spigrp_dir = NULL;
  32203. +static struct proc_dir_entry *proc_birth_dir = NULL;
  32204. +static struct proc_dir_entry *proc_stats_dir = NULL;
  32205. +#endif
  32206. +
  32207. +struct ipsec_birth_reply ipsec_ipv4_birth_packet;
  32208. +struct ipsec_birth_reply ipsec_ipv6_birth_packet;
  32209. +
  32210. +#ifdef CONFIG_KLIPS_DEBUG
  32211. +int debug_esp = 0;
  32212. +int debug_ah = 0;
  32213. +#endif /* CONFIG_KLIPS_DEBUG */
  32214. +
  32215. +#define DECREMENT_UNSIGNED(X, amount) ((amount < (X)) ? (X)-amount : 0)
  32216. +
  32217. +extern int ipsec_xform_get_info(char *buffer, char **start,
  32218. + off_t offset, int length IPSEC_PROC_LAST_ARG);
  32219. +
  32220. +
  32221. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32222. +int
  32223. +ipsec_eroute_get_info(char *buffer,
  32224. + char **start,
  32225. + off_t offset,
  32226. + int length IPSEC_PROC_LAST_ARG)
  32227. +{
  32228. + struct wsbuf w = {buffer, length, offset, 0, 0};
  32229. +
  32230. +#ifdef CONFIG_KLIPS_DEBUG
  32231. + if (debug_radij & DB_RJ_DUMPTREES)
  32232. + rj_dumptrees(); /* XXXXXXXXX */
  32233. +#endif /* CONFIG_KLIPS_DEBUG */
  32234. +
  32235. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32236. + "klips_debug:ipsec_eroute_get_info: "
  32237. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  32238. + buffer,
  32239. + *start,
  32240. + (int)offset,
  32241. + length);
  32242. +
  32243. + spin_lock_bh(&eroute_lock);
  32244. +
  32245. + rj_walktree(rnh, ipsec_rj_walker_procprint, &w);
  32246. +/* rj_walktree(mask_rjhead, ipsec_rj_walker_procprint, &w); */
  32247. +
  32248. + spin_unlock_bh(&eroute_lock);
  32249. +
  32250. + *start = buffer + (offset - w.begin); /* Start of wanted data */
  32251. + return w.len - (offset - w.begin);
  32252. +}
  32253. +
  32254. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32255. +int
  32256. +ipsec_spi_get_info(char *buffer,
  32257. + char **start,
  32258. + off_t offset,
  32259. + int length IPSEC_PROC_LAST_ARG)
  32260. +{
  32261. + const int max_content = length > 0? length-1 : 0;
  32262. + int len = 0;
  32263. + off_t begin = 0;
  32264. + int i;
  32265. + struct ipsec_sa *sa_p;
  32266. + char sa[SATOT_BUF];
  32267. + char buf_s[SUBNETTOA_BUF];
  32268. + char buf_d[SUBNETTOA_BUF];
  32269. + size_t sa_len;
  32270. +
  32271. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32272. + "klips_debug:ipsec_spi_get_info: "
  32273. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  32274. + buffer,
  32275. + *start,
  32276. + (int)offset,
  32277. + length);
  32278. +
  32279. + spin_lock_bh(&tdb_lock);
  32280. +
  32281. + for (i = 0; i < SADB_HASHMOD; i++) {
  32282. + for (sa_p = ipsec_sadb_hash[i];
  32283. + sa_p;
  32284. + sa_p = sa_p->ips_hnext) {
  32285. + atomic_inc(&sa_p->ips_refcount);
  32286. + sa_len = satot(&sa_p->ips_said, 'x', sa, sizeof(sa));
  32287. + len += ipsec_snprintf(buffer+len, length-len, "%s ",
  32288. + sa_len ? sa : " (error)");
  32289. +
  32290. + len += ipsec_snprintf(buffer+len, length-len, "%s%s%s",
  32291. + IPS_XFORM_NAME(sa_p));
  32292. +
  32293. + len += ipsec_snprintf(buffer+len, length-len, ": dir=%s",
  32294. + (sa_p->ips_flags & EMT_INBOUND) ?
  32295. + "in " : "out");
  32296. +
  32297. + if(sa_p->ips_addr_s) {
  32298. + addrtoa(((struct sockaddr_in*)(sa_p->ips_addr_s))->sin_addr,
  32299. + 0, buf_s, sizeof(buf_s));
  32300. + len += ipsec_snprintf(buffer+len, length-len, " src=%s",
  32301. + buf_s);
  32302. + }
  32303. +
  32304. + if((sa_p->ips_said.proto == IPPROTO_IPIP)
  32305. + && (sa_p->ips_flags & SADB_X_SAFLAGS_INFLOW)) {
  32306. + subnettoa(sa_p->ips_flow_s.u.v4.sin_addr,
  32307. + sa_p->ips_mask_s.u.v4.sin_addr,
  32308. + 0,
  32309. + buf_s,
  32310. + sizeof(buf_s));
  32311. +
  32312. + subnettoa(sa_p->ips_flow_d.u.v4.sin_addr,
  32313. + sa_p->ips_mask_d.u.v4.sin_addr,
  32314. + 0,
  32315. + buf_d,
  32316. + sizeof(buf_d));
  32317. +
  32318. + len += ipsec_snprintf(buffer+len, length-len, " policy=%s->%s",
  32319. + buf_s, buf_d);
  32320. + }
  32321. +
  32322. + if(sa_p->ips_iv_bits) {
  32323. + int j;
  32324. + len += ipsec_snprintf(buffer+len, length-len, " iv_bits=%dbits iv=0x",
  32325. + sa_p->ips_iv_bits);
  32326. +
  32327. + for(j = 0; j < sa_p->ips_iv_bits / 8; j++) {
  32328. + len += ipsec_snprintf(buffer+len, length-len, "%02x",
  32329. + (__u32)((__u8*)(sa_p->ips_iv))[j]);
  32330. + }
  32331. + }
  32332. +
  32333. + if(sa_p->ips_encalg || sa_p->ips_authalg) {
  32334. + if(sa_p->ips_replaywin) {
  32335. + len += ipsec_snprintf(buffer+len, length-len, " ooowin=%d",
  32336. + sa_p->ips_replaywin);
  32337. + }
  32338. + if(sa_p->ips_errs.ips_replaywin_errs) {
  32339. + len += ipsec_snprintf(buffer+len, length-len, " ooo_errs=%d",
  32340. + sa_p->ips_errs.ips_replaywin_errs);
  32341. + }
  32342. + if(sa_p->ips_replaywin_lastseq) {
  32343. + len += ipsec_snprintf(buffer+len, length-len, " seq=%d",
  32344. + sa_p->ips_replaywin_lastseq);
  32345. + }
  32346. + if(sa_p->ips_replaywin_bitmap) {
  32347. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
  32348. + len += ipsec_snprintf(buffer+len, length-len, " bit=0x%Lx",
  32349. + sa_p->ips_replaywin_bitmap);
  32350. +#else
  32351. + len += ipsec_snprintf(buffer+len, length-len, " bit=0x%x%08x",
  32352. + (__u32)(sa_p->ips_replaywin_bitmap >> 32),
  32353. + (__u32)sa_p->ips_replaywin_bitmap);
  32354. +#endif
  32355. + }
  32356. + if(sa_p->ips_replaywin_maxdiff) {
  32357. + len += ipsec_snprintf(buffer+len, length-len, " max_seq_diff=%d",
  32358. + sa_p->ips_replaywin_maxdiff);
  32359. + }
  32360. + }
  32361. + if(sa_p->ips_flags & ~EMT_INBOUND) {
  32362. + len += ipsec_snprintf(buffer+len, length-len, " flags=0x%x",
  32363. + sa_p->ips_flags & ~EMT_INBOUND);
  32364. + len += ipsec_snprintf(buffer+len, length-len, "<");
  32365. + /* flag printing goes here */
  32366. + len += ipsec_snprintf(buffer+len, length-len, ">");
  32367. + }
  32368. + if(sa_p->ips_auth_bits) {
  32369. + len += ipsec_snprintf(buffer+len, length-len, " alen=%d",
  32370. + sa_p->ips_auth_bits);
  32371. + }
  32372. + if(sa_p->ips_key_bits_a) {
  32373. + len += ipsec_snprintf(buffer+len, length-len, " aklen=%d",
  32374. + sa_p->ips_key_bits_a);
  32375. + }
  32376. + if(sa_p->ips_errs.ips_auth_errs) {
  32377. + len += ipsec_snprintf(buffer+len, length-len, " auth_errs=%d",
  32378. + sa_p->ips_errs.ips_auth_errs);
  32379. + }
  32380. + if(sa_p->ips_key_bits_e) {
  32381. + len += ipsec_snprintf(buffer+len, length-len, " eklen=%d",
  32382. + sa_p->ips_key_bits_e);
  32383. + }
  32384. + if(sa_p->ips_errs.ips_encsize_errs) {
  32385. + len += ipsec_snprintf(buffer+len, length-len, " encr_size_errs=%d",
  32386. + sa_p->ips_errs.ips_encsize_errs);
  32387. + }
  32388. + if(sa_p->ips_errs.ips_encpad_errs) {
  32389. + len += ipsec_snprintf(buffer+len, length-len, " encr_pad_errs=%d",
  32390. + sa_p->ips_errs.ips_encpad_errs);
  32391. + }
  32392. +
  32393. + len += ipsec_snprintf(buffer+len, length-len, " life(c,s,h)=");
  32394. +
  32395. + len += ipsec_lifetime_format(buffer + len,
  32396. + length - len,
  32397. + "alloc",
  32398. + ipsec_life_countbased,
  32399. + &sa_p->ips_life.ipl_allocations);
  32400. +
  32401. + len += ipsec_lifetime_format(buffer + len,
  32402. + length - len,
  32403. + "bytes",
  32404. + ipsec_life_countbased,
  32405. + &sa_p->ips_life.ipl_bytes);
  32406. +
  32407. + len += ipsec_lifetime_format(buffer + len,
  32408. + length - len,
  32409. + "addtime",
  32410. + ipsec_life_timebased,
  32411. + &sa_p->ips_life.ipl_addtime);
  32412. +
  32413. + len += ipsec_lifetime_format(buffer + len,
  32414. + length - len,
  32415. + "usetime",
  32416. + ipsec_life_timebased,
  32417. + &sa_p->ips_life.ipl_usetime);
  32418. +
  32419. + len += ipsec_lifetime_format(buffer + len,
  32420. + length - len,
  32421. + "packets",
  32422. + ipsec_life_countbased,
  32423. + &sa_p->ips_life.ipl_packets);
  32424. +
  32425. + if(sa_p->ips_life.ipl_usetime.ipl_last) { /* XXX-MCR should be last? */
  32426. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
  32427. + len += ipsec_snprintf(buffer+len, length-len, " idle=%Ld",
  32428. + jiffies / HZ - sa_p->ips_life.ipl_usetime.ipl_last);
  32429. +#else
  32430. + len += ipsec_snprintf(buffer+len, length-len, " idle=%lu",
  32431. + jiffies / HZ - (unsigned long)sa_p->ips_life.ipl_usetime.ipl_last);
  32432. +#endif
  32433. + }
  32434. +
  32435. +#ifdef CONFIG_KLIPS_IPCOMP
  32436. + if(sa_p->ips_said.proto == IPPROTO_COMP &&
  32437. + (sa_p->ips_comp_ratio_dbytes ||
  32438. + sa_p->ips_comp_ratio_cbytes)) {
  32439. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
  32440. + len += ipsec_snprintf(buffer+len, length-len, " ratio=%Ld:%Ld",
  32441. + sa_p->ips_comp_ratio_dbytes,
  32442. + sa_p->ips_comp_ratio_cbytes);
  32443. +#else
  32444. + len += ipsec_snprintf(buffer+len, length-len, " ratio=%lu:%lu",
  32445. + (unsigned long)sa_p->ips_comp_ratio_dbytes,
  32446. + (unsigned long)sa_p->ips_comp_ratio_cbytes);
  32447. +#endif
  32448. + }
  32449. +#endif /* CONFIG_KLIPS_IPCOMP */
  32450. +
  32451. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  32452. + {
  32453. + char *natttype_name;
  32454. +
  32455. + switch(sa_p->ips_natt_type)
  32456. + {
  32457. + case 0:
  32458. + natttype_name="none";
  32459. + break;
  32460. + case ESPINUDP_WITH_NON_IKE:
  32461. + natttype_name="nonike";
  32462. + break;
  32463. + case ESPINUDP_WITH_NON_ESP:
  32464. + natttype_name="nonesp";
  32465. + break;
  32466. + default:
  32467. + natttype_name = "unknown";
  32468. + break;
  32469. + }
  32470. +
  32471. + len += ipsec_snprintf(buffer + len, length-len, " natencap=%s",
  32472. + natttype_name);
  32473. +
  32474. + len += ipsec_snprintf(buffer + len, length-len, " natsport=%d",
  32475. + sa_p->ips_natt_sport);
  32476. +
  32477. + len += ipsec_snprintf(buffer + len,length-len, " natdport=%d",
  32478. + sa_p->ips_natt_dport);
  32479. + }
  32480. +#else
  32481. + len += ipsec_snprintf(buffer + len, length-len, " natencap=na");
  32482. +#endif /* CONFIG_IPSEC_NAT_TRAVERSAL */
  32483. +
  32484. + len += ipsec_snprintf(buffer + len,length-len, " refcount=%d",
  32485. + atomic_read(&sa_p->ips_refcount));
  32486. +
  32487. + len += ipsec_snprintf(buffer+len, length-len, " ref=%d",
  32488. + sa_p->ips_ref);
  32489. +#ifdef CONFIG_KLIPS_DEBUG
  32490. + if(debug_xform) {
  32491. + len += ipsec_snprintf(buffer+len, length-len, " reftable=%lu refentry=%lu",
  32492. + (unsigned long)IPsecSAref2table(sa_p->ips_ref),
  32493. + (unsigned long)IPsecSAref2entry(sa_p->ips_ref));
  32494. + }
  32495. +#endif /* CONFIG_KLIPS_DEBUG */
  32496. +
  32497. + len += ipsec_snprintf(buffer+len, length-len, "\n");
  32498. +
  32499. + atomic_dec(&sa_p->ips_refcount);
  32500. +
  32501. + if (len >= max_content) {
  32502. + /* we've done all that can fit -- stop loops */
  32503. + len = max_content; /* truncate crap */
  32504. + goto done_spi_i;
  32505. + } else {
  32506. + const off_t pos = begin + len; /* file position of end of what we've generated */
  32507. +
  32508. + if (pos <= offset) {
  32509. + /* all is before first interesting character:
  32510. + * discard, but note where we are.
  32511. + */
  32512. + len = 0;
  32513. + begin = pos;
  32514. + }
  32515. + }
  32516. + }
  32517. + }
  32518. +
  32519. +done_spi_i:
  32520. + spin_unlock_bh(&tdb_lock);
  32521. +
  32522. + *start = buffer + (offset - begin); /* Start of wanted data */
  32523. + return len - (offset - begin);
  32524. +}
  32525. +
  32526. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32527. +int
  32528. +ipsec_spigrp_get_info(char *buffer,
  32529. + char **start,
  32530. + off_t offset,
  32531. + int length IPSEC_PROC_LAST_ARG)
  32532. +{
  32533. + /* Limit of useful snprintf output */
  32534. + const int max_content = length > 0? length-1 : 0;
  32535. +
  32536. + int len = 0;
  32537. + off_t begin = 0;
  32538. + int i;
  32539. + struct ipsec_sa *sa_p, *sa_p2;
  32540. + char sa[SATOT_BUF];
  32541. + size_t sa_len;
  32542. +
  32543. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32544. + "klips_debug:ipsec_spigrp_get_info: "
  32545. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  32546. + buffer,
  32547. + *start,
  32548. + (int)offset,
  32549. + length);
  32550. +
  32551. + spin_lock_bh(&tdb_lock);
  32552. +
  32553. + for (i = 0; i < SADB_HASHMOD; i++) {
  32554. + for (sa_p = ipsec_sadb_hash[i];
  32555. + sa_p != NULL;
  32556. + sa_p = sa_p->ips_hnext)
  32557. + {
  32558. + atomic_inc(&sa_p->ips_refcount);
  32559. + if(sa_p->ips_inext == NULL) {
  32560. + sa_p2 = sa_p;
  32561. + while(sa_p2 != NULL) {
  32562. + atomic_inc(&sa_p2->ips_refcount);
  32563. + sa_len = satot(&sa_p2->ips_said,
  32564. + 'x', sa, sizeof(sa));
  32565. +
  32566. + len += ipsec_snprintf(buffer+len, length-len, "%s ",
  32567. + sa_len ? sa : " (error)");
  32568. + atomic_dec(&sa_p2->ips_refcount);
  32569. + sa_p2 = sa_p2->ips_onext;
  32570. + }
  32571. + len += ipsec_snprintf(buffer+len, length-len, "\n");
  32572. + }
  32573. +
  32574. + atomic_dec(&sa_p->ips_refcount);
  32575. +
  32576. + if (len >= max_content) {
  32577. + /* we've done all that can fit -- stop loops */
  32578. + len = max_content; /* truncate crap */
  32579. + goto done_spigrp_i;
  32580. + } else {
  32581. + const off_t pos = begin + len;
  32582. +
  32583. + if (pos <= offset) {
  32584. + /* all is before first interesting character:
  32585. + * discard, but note where we are.
  32586. + */
  32587. + len = 0;
  32588. + begin = pos;
  32589. + }
  32590. + }
  32591. + }
  32592. + }
  32593. +
  32594. +done_spigrp_i:
  32595. + spin_unlock_bh(&tdb_lock);
  32596. +
  32597. + *start = buffer + (offset - begin); /* Start of wanted data */
  32598. + return len - (offset - begin);
  32599. +}
  32600. +
  32601. +
  32602. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32603. +int
  32604. +ipsec_tncfg_get_info(char *buffer,
  32605. + char **start,
  32606. + off_t offset,
  32607. + int length IPSEC_PROC_LAST_ARG)
  32608. +{
  32609. + /* limit of useful snprintf output */
  32610. + const int max_content = length > 0? length-1 : 0;
  32611. + int len = 0;
  32612. + off_t begin = 0;
  32613. + int i;
  32614. + char name[9];
  32615. + struct net_device *dev, *privdev;
  32616. + struct ipsecpriv *priv;
  32617. +
  32618. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32619. + "klips_debug:ipsec_tncfg_get_info: "
  32620. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  32621. + buffer,
  32622. + *start,
  32623. + (int)offset,
  32624. + length);
  32625. +
  32626. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  32627. + ipsec_snprintf(name, (ssize_t) sizeof(name), IPSEC_DEV_FORMAT, i);
  32628. + dev = __ipsec_dev_get(name);
  32629. + if(dev) {
  32630. + priv = (struct ipsecpriv *)(dev->priv);
  32631. + len += ipsec_snprintf(buffer+len, length-len, "%s",
  32632. + dev->name);
  32633. + if(priv) {
  32634. + privdev = (struct net_device *)(priv->dev);
  32635. + len += ipsec_snprintf(buffer+len, length-len, " -> %s",
  32636. + privdev ? privdev->name : "NULL");
  32637. + len += ipsec_snprintf(buffer+len, length-len, " mtu=%d(%d) -> %d",
  32638. + dev->mtu,
  32639. + priv->mtu,
  32640. + privdev ? privdev->mtu : 0);
  32641. + } else {
  32642. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32643. + "klips_debug:ipsec_tncfg_get_info: device '%s' has no private data space!\n",
  32644. + dev->name);
  32645. + }
  32646. + len += ipsec_snprintf(buffer+len, length-len, "\n");
  32647. +
  32648. + if (len >= max_content) {
  32649. + /* we've done all that can fit -- stop loop */
  32650. + len = max_content; /* truncate crap */
  32651. + break;
  32652. + } else {
  32653. + const off_t pos = begin + len;
  32654. + if (pos <= offset) {
  32655. + len = 0;
  32656. + begin = pos;
  32657. + }
  32658. + }
  32659. + }
  32660. + }
  32661. + *start = buffer + (offset - begin); /* Start of wanted data */
  32662. + len -= (offset - begin); /* Start slop */
  32663. + if (len > length)
  32664. + len = length;
  32665. + return len;
  32666. +}
  32667. +
  32668. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32669. +int
  32670. +ipsec_version_get_info(char *buffer,
  32671. + char **start,
  32672. + off_t offset,
  32673. + int length IPSEC_PROC_LAST_ARG)
  32674. +{
  32675. + int len = 0;
  32676. + off_t begin = 0;
  32677. +
  32678. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32679. + "klips_debug:ipsec_version_get_info: "
  32680. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  32681. + buffer,
  32682. + *start,
  32683. + (int)offset,
  32684. + length);
  32685. +
  32686. + len += ipsec_snprintf(buffer + len,length-len, "Openswan version: %s\n",
  32687. + ipsec_version_code());
  32688. +#if 0
  32689. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32690. + "klips_debug:ipsec_version_get_info: "
  32691. + "ipsec_init version: %s\n",
  32692. + ipsec_init_c_version);
  32693. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32694. + "klips_debug:ipsec_version_get_info: "
  32695. + "ipsec_tunnel version: %s\n",
  32696. + ipsec_tunnel_c_version);
  32697. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32698. + "klips_debug:ipsec_version_get_info: "
  32699. + "ipsec_netlink version: %s\n",
  32700. + ipsec_netlink_c_version);
  32701. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32702. + "klips_debug:ipsec_version_get_info: "
  32703. + "radij_c_version: %s\n",
  32704. + radij_c_version);
  32705. +#endif
  32706. +
  32707. +
  32708. + *start = buffer + (offset - begin); /* Start of wanted data */
  32709. + len -= (offset - begin); /* Start slop */
  32710. + if (len > length)
  32711. + len = length;
  32712. + return len;
  32713. +}
  32714. +
  32715. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  32716. +unsigned int natt_available = 1;
  32717. +#else
  32718. +unsigned int natt_available = 0;
  32719. +#endif
  32720. +module_param(natt_available, int, 0444);
  32721. +
  32722. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32723. +int
  32724. +ipsec_natt_get_info(char *buffer,
  32725. + char **start,
  32726. + off_t offset,
  32727. + int length IPSEC_PROC_LAST_ARG)
  32728. +{
  32729. + int len = 0;
  32730. + off_t begin = 0;
  32731. +
  32732. + len += ipsec_snprintf(buffer + len,
  32733. + length-len, "%d\n",
  32734. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  32735. + 1
  32736. +#else
  32737. + 0
  32738. +#endif
  32739. + );
  32740. +
  32741. + *start = buffer + (offset - begin); /* Start of wanted data */
  32742. + len -= (offset - begin); /* Start slop */
  32743. + if (len > length)
  32744. + len = length;
  32745. + return len;
  32746. +}
  32747. +
  32748. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32749. +int
  32750. +ipsec_birth_info(char *page,
  32751. + char **start,
  32752. + off_t offset,
  32753. + int count,
  32754. + int *eof,
  32755. + void *data)
  32756. +{
  32757. + struct ipsec_birth_reply *ibr = (struct ipsec_birth_reply *)data;
  32758. + int len;
  32759. +
  32760. + if(offset >= ibr->packet_template_len) {
  32761. + if(eof) {
  32762. + *eof=1;
  32763. + }
  32764. + return 0;
  32765. + }
  32766. +
  32767. + len = ibr->packet_template_len;
  32768. + len -= offset;
  32769. + if (len > count)
  32770. + len = count;
  32771. +
  32772. + memcpy(page + offset, ibr->packet_template+offset, len);
  32773. +
  32774. + return len;
  32775. +}
  32776. +
  32777. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32778. +int
  32779. +ipsec_birth_set(struct file *file, const char *buffer,
  32780. + unsigned long count, void *data)
  32781. +{
  32782. + struct ipsec_birth_reply *ibr = (struct ipsec_birth_reply *)data;
  32783. + int len;
  32784. +
  32785. + KLIPS_INC_USE;
  32786. + if(count > IPSEC_BIRTH_TEMPLATE_MAXLEN) {
  32787. + len = IPSEC_BIRTH_TEMPLATE_MAXLEN;
  32788. + } else {
  32789. + len = count;
  32790. + }
  32791. +
  32792. + if(copy_from_user(ibr->packet_template, buffer, len)) {
  32793. + KLIPS_DEC_USE;
  32794. + return -EFAULT;
  32795. + }
  32796. + ibr->packet_template_len = len;
  32797. +
  32798. + KLIPS_DEC_USE;
  32799. +
  32800. + return len;
  32801. +}
  32802. +
  32803. +
  32804. +#ifdef CONFIG_KLIPS_DEBUG
  32805. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32806. +int
  32807. +ipsec_klipsdebug_get_info(char *buffer,
  32808. + char **start,
  32809. + off_t offset,
  32810. + int length IPSEC_PROC_LAST_ARG)
  32811. +{
  32812. + int len = 0;
  32813. + off_t begin = 0;
  32814. +
  32815. + KLIPS_PRINT(debug_tunnel & DB_TN_PROCFS,
  32816. + "klips_debug:ipsec_klipsdebug_get_info: "
  32817. + "buffer=0p%p, *start=0p%p, offset=%d, length=%d\n",
  32818. + buffer,
  32819. + *start,
  32820. + (int)offset,
  32821. + length);
  32822. +
  32823. + len += ipsec_snprintf(buffer+len, length-len, "debug_tunnel=%08x.\n", debug_tunnel);
  32824. + len += ipsec_snprintf(buffer+len, length-len, "debug_xform=%08x.\n", debug_xform);
  32825. + len += ipsec_snprintf(buffer+len, length-len, "debug_eroute=%08x.\n", debug_eroute);
  32826. + len += ipsec_snprintf(buffer+len, length-len, "debug_spi=%08x.\n", debug_spi);
  32827. + len += ipsec_snprintf(buffer+len, length-len, "debug_radij=%08x.\n", debug_radij);
  32828. + len += ipsec_snprintf(buffer+len, length-len, "debug_esp=%08x.\n", debug_esp);
  32829. + len += ipsec_snprintf(buffer+len, length-len, "debug_ah=%08x.\n", debug_ah);
  32830. + len += ipsec_snprintf(buffer+len, length-len, "debug_rcv=%08x.\n", debug_rcv);
  32831. + len += ipsec_snprintf(buffer+len, length-len, "debug_pfkey=%08x.\n", debug_pfkey);
  32832. +
  32833. + *start = buffer + (offset - begin); /* Start of wanted data */
  32834. + len -= (offset - begin); /* Start slop */
  32835. + if (len > length)
  32836. + len = length;
  32837. + return len;
  32838. +}
  32839. +#endif /* CONFIG_KLIPS_DEBUG */
  32840. +
  32841. +IPSEC_PROCFS_DEBUG_NO_STATIC
  32842. +int
  32843. +ipsec_stats_get_int_info(char *buffer,
  32844. + char **start,
  32845. + off_t offset,
  32846. + int length,
  32847. + int *eof,
  32848. + void *data)
  32849. +{
  32850. +
  32851. + const int max_content = length > 0? length-1 : 0;
  32852. + int len = 0;
  32853. + int *thing;
  32854. +
  32855. + thing = (int *)data;
  32856. +
  32857. + len = ipsec_snprintf(buffer+len, length-len, "%08x\n", *thing);
  32858. +
  32859. + if (len >= max_content)
  32860. + len = max_content; /* truncate crap */
  32861. +
  32862. + *start = buffer + offset; /* Start of wanted data */
  32863. + return len > offset? len - offset : 0;
  32864. +
  32865. +}
  32866. +
  32867. +#ifndef PROC_FS_2325
  32868. +struct proc_dir_entry ipsec_eroute =
  32869. +{
  32870. + 0,
  32871. + 12, "ipsec_eroute",
  32872. + S_IFREG | S_IRUGO, 1, 0, 0, 0,
  32873. + &proc_net_inode_operations,
  32874. + ipsec_eroute_get_info,
  32875. + NULL, NULL, NULL, NULL, NULL
  32876. +};
  32877. +
  32878. +struct proc_dir_entry ipsec_spi =
  32879. +{
  32880. + 0,
  32881. + 9, "ipsec_spi",
  32882. + S_IFREG | S_IRUGO, 1, 0, 0, 0,
  32883. + &proc_net_inode_operations,
  32884. + ipsec_spi_get_info,
  32885. + NULL, NULL, NULL, NULL, NULL
  32886. +};
  32887. +
  32888. +struct proc_dir_entry ipsec_spigrp =
  32889. +{
  32890. + 0,
  32891. + 12, "ipsec_spigrp",
  32892. + S_IFREG | S_IRUGO, 1, 0, 0, 0,
  32893. + &proc_net_inode_operations,
  32894. + ipsec_spigrp_get_info,
  32895. + NULL, NULL, NULL, NULL, NULL
  32896. +};
  32897. +
  32898. +struct proc_dir_entry ipsec_tncfg =
  32899. +{
  32900. + 0,
  32901. + 11, "ipsec_tncfg",
  32902. + S_IFREG | S_IRUGO, 1, 0, 0, 0,
  32903. + &proc_net_inode_operations,
  32904. + ipsec_tncfg_get_info,
  32905. + NULL, NULL, NULL, NULL, NULL
  32906. +};
  32907. +
  32908. +struct proc_dir_entry ipsec_version =
  32909. +{
  32910. + 0,
  32911. + 13, "ipsec_version",
  32912. + S_IFREG | S_IRUGO, 1, 0, 0, 0,
  32913. + &proc_net_inode_operations,
  32914. + ipsec_version_get_info,
  32915. + NULL, NULL, NULL, NULL, NULL
  32916. +};
  32917. +
  32918. +#ifdef CONFIG_KLIPS_DEBUG
  32919. +struct proc_dir_entry ipsec_klipsdebug =
  32920. +{
  32921. + 0,
  32922. + 16, "ipsec_klipsdebug",
  32923. + S_IFREG | S_IRUGO, 1, 0, 0, 0,
  32924. + &proc_net_inode_operations,
  32925. + ipsec_klipsdebug_get_info,
  32926. + NULL, NULL, NULL, NULL, NULL
  32927. +};
  32928. +#endif /* CONFIG_KLIPS_DEBUG */
  32929. +#endif /* !PROC_FS_2325 */
  32930. +#endif /* CONFIG_PROC_FS */
  32931. +
  32932. +#if defined(PROC_FS_2325)
  32933. +struct ipsec_proc_list {
  32934. + char *name;
  32935. + struct proc_dir_entry **parent;
  32936. + struct proc_dir_entry **dir;
  32937. + read_proc_t *readthing;
  32938. + write_proc_t *writething;
  32939. + void *data;
  32940. +};
  32941. +static struct ipsec_proc_list proc_items[]={
  32942. +#ifdef CONFIG_KLIPS_DEBUG
  32943. + {"klipsdebug", &proc_net_ipsec_dir, NULL, ipsec_klipsdebug_get_info, NULL, NULL},
  32944. +#endif
  32945. + {"eroute", &proc_net_ipsec_dir, &proc_eroute_dir, NULL, NULL, NULL},
  32946. + {"all", &proc_eroute_dir, NULL, ipsec_eroute_get_info, NULL, NULL},
  32947. + {"spi", &proc_net_ipsec_dir, &proc_spi_dir, NULL, NULL, NULL},
  32948. + {"all", &proc_spi_dir, NULL, ipsec_spi_get_info, NULL, NULL},
  32949. + {"spigrp", &proc_net_ipsec_dir, &proc_spigrp_dir, NULL, NULL, NULL},
  32950. + {"all", &proc_spigrp_dir, NULL, ipsec_spigrp_get_info, NULL, NULL},
  32951. + {"birth", &proc_net_ipsec_dir, &proc_birth_dir, NULL, NULL, NULL},
  32952. + {"ipv4", &proc_birth_dir, NULL, ipsec_birth_info, ipsec_birth_set, (void *)&ipsec_ipv4_birth_packet},
  32953. + {"ipv6", &proc_birth_dir, NULL, ipsec_birth_info, ipsec_birth_set, (void *)&ipsec_ipv6_birth_packet},
  32954. + {"tncfg", &proc_net_ipsec_dir, NULL, ipsec_tncfg_get_info, NULL, NULL},
  32955. + {"xforms", &proc_net_ipsec_dir, NULL, ipsec_xform_get_info, NULL, NULL},
  32956. + {"stats", &proc_net_ipsec_dir, &proc_stats_dir, NULL, NULL, NULL},
  32957. + {"trap_count", &proc_stats_dir, NULL, ipsec_stats_get_int_info, NULL, &ipsec_xmit_trap_count},
  32958. + {"trap_sendcount", &proc_stats_dir, NULL, ipsec_stats_get_int_info, NULL, &ipsec_xmit_trap_sendcount},
  32959. + {"version", &proc_net_ipsec_dir, NULL, ipsec_version_get_info, NULL, NULL},
  32960. + {NULL, NULL, NULL, NULL, NULL, NULL}
  32961. +};
  32962. +#endif
  32963. +
  32964. +int
  32965. +ipsec_proc_init()
  32966. +{
  32967. + int error = 0;
  32968. +#ifdef IPSEC_PROC_SUBDIRS
  32969. + struct proc_dir_entry *item;
  32970. +#endif
  32971. +
  32972. + /*
  32973. + * just complain because pluto won't run without /proc!
  32974. + */
  32975. +#ifndef CONFIG_PROC_FS
  32976. +#error You must have PROC_FS built in to use KLIPS
  32977. +#endif
  32978. +
  32979. + /* for 2.0 kernels */
  32980. +#if !defined(PROC_FS_2325) && !defined(PROC_FS_21)
  32981. + error |= proc_register_dynamic(&proc_net, &ipsec_eroute);
  32982. + error |= proc_register_dynamic(&proc_net, &ipsec_spi);
  32983. + error |= proc_register_dynamic(&proc_net, &ipsec_spigrp);
  32984. + error |= proc_register_dynamic(&proc_net, &ipsec_tncfg);
  32985. + error |= proc_register_dynamic(&proc_net, &ipsec_version);
  32986. +#ifdef CONFIG_KLIPS_DEBUG
  32987. + error |= proc_register_dynamic(&proc_net, &ipsec_klipsdebug);
  32988. +#endif /* CONFIG_KLIPS_DEBUG */
  32989. +#endif
  32990. +
  32991. + /* for 2.2 kernels */
  32992. +#if !defined(PROC_FS_2325) && defined(PROC_FS_21)
  32993. + error |= proc_register(proc_net, &ipsec_eroute);
  32994. + error |= proc_register(proc_net, &ipsec_spi);
  32995. + error |= proc_register(proc_net, &ipsec_spigrp);
  32996. + error |= proc_register(proc_net, &ipsec_tncfg);
  32997. + error |= proc_register(proc_net, &ipsec_version);
  32998. +#ifdef CONFIG_KLIPS_DEBUG
  32999. + error |= proc_register(proc_net, &ipsec_klipsdebug);
  33000. +#endif /* CONFIG_KLIPS_DEBUG */
  33001. +#endif
  33002. +
  33003. + /* for 2.4 kernels */
  33004. +#if defined(PROC_FS_2325)
  33005. + /* create /proc/net/ipsec */
  33006. +
  33007. + /* zero these out before we initialize /proc/net/ipsec/birth/stuff */
  33008. + memset(&ipsec_ipv4_birth_packet, 0, sizeof(struct ipsec_birth_reply));
  33009. + memset(&ipsec_ipv6_birth_packet, 0, sizeof(struct ipsec_birth_reply));
  33010. +
  33011. + proc_net_ipsec_dir = proc_mkdir("ipsec", proc_net);
  33012. + if(proc_net_ipsec_dir == NULL) {
  33013. + /* no point in continuing */
  33014. + return 1;
  33015. + }
  33016. +
  33017. + {
  33018. + struct ipsec_proc_list *it;
  33019. +
  33020. + it=proc_items;
  33021. + while(it->name!=NULL) {
  33022. + if(it->dir) {
  33023. + /* make a dir instead */
  33024. + item = proc_mkdir(it->name, *it->parent);
  33025. + *it->dir = item;
  33026. + } else {
  33027. + item = create_proc_entry(it->name, 0400, *it->parent);
  33028. + }
  33029. + if(item) {
  33030. + item->read_proc = it->readthing;
  33031. + item->write_proc = it->writething;
  33032. + item->data = it->data;
  33033. +#ifdef MODULE
  33034. + item->owner = THIS_MODULE;
  33035. +#endif
  33036. + } else {
  33037. + error |= 1;
  33038. + }
  33039. + it++;
  33040. + }
  33041. + }
  33042. +
  33043. + /* now create some symlinks to provide compatibility */
  33044. + proc_symlink("ipsec_eroute", proc_net, "ipsec/eroute/all");
  33045. + proc_symlink("ipsec_spi", proc_net, "ipsec/spi/all");
  33046. + proc_symlink("ipsec_spigrp", proc_net, "ipsec/spigrp/all");
  33047. + proc_symlink("ipsec_tncfg", proc_net, "ipsec/tncfg");
  33048. + proc_symlink("ipsec_version",proc_net, "ipsec/version");
  33049. + proc_symlink("ipsec_klipsdebug",proc_net,"ipsec/klipsdebug");
  33050. +
  33051. +#endif /* !PROC_FS_2325 */
  33052. +
  33053. + return error;
  33054. +}
  33055. +
  33056. +void
  33057. +ipsec_proc_cleanup()
  33058. +{
  33059. +
  33060. + /* for 2.0 and 2.2 kernels */
  33061. +#if !defined(PROC_FS_2325)
  33062. +
  33063. +#ifdef CONFIG_KLIPS_DEBUG
  33064. + if (proc_net_unregister(ipsec_klipsdebug.low_ino) != 0)
  33065. + printk("klips_debug:ipsec_cleanup: "
  33066. + "cannot unregister /proc/net/ipsec_klipsdebug\n");
  33067. +#endif /* CONFIG_KLIPS_DEBUG */
  33068. +
  33069. + if (proc_net_unregister(ipsec_version.low_ino) != 0)
  33070. + printk("klips_debug:ipsec_cleanup: "
  33071. + "cannot unregister /proc/net/ipsec_version\n");
  33072. + if (proc_net_unregister(ipsec_eroute.low_ino) != 0)
  33073. + printk("klips_debug:ipsec_cleanup: "
  33074. + "cannot unregister /proc/net/ipsec_eroute\n");
  33075. + if (proc_net_unregister(ipsec_spi.low_ino) != 0)
  33076. + printk("klips_debug:ipsec_cleanup: "
  33077. + "cannot unregister /proc/net/ipsec_spi\n");
  33078. + if (proc_net_unregister(ipsec_spigrp.low_ino) != 0)
  33079. + printk("klips_debug:ipsec_cleanup: "
  33080. + "cannot unregister /proc/net/ipsec_spigrp\n");
  33081. + if (proc_net_unregister(ipsec_tncfg.low_ino) != 0)
  33082. + printk("klips_debug:ipsec_cleanup: "
  33083. + "cannot unregister /proc/net/ipsec_tncfg\n");
  33084. +#endif
  33085. +
  33086. + /* for 2.4 kernels */
  33087. +#if defined(PROC_FS_2325)
  33088. + {
  33089. + struct ipsec_proc_list *it;
  33090. +
  33091. + /* find end of list */
  33092. + it=proc_items;
  33093. + while(it->name!=NULL) {
  33094. + it++;
  33095. + }
  33096. + it--;
  33097. +
  33098. + do {
  33099. + remove_proc_entry(it->name, *it->parent);
  33100. + it--;
  33101. + } while(it >= proc_items);
  33102. + }
  33103. +
  33104. +
  33105. +#ifdef CONFIG_KLIPS_DEBUG
  33106. + remove_proc_entry("ipsec_klipsdebug", proc_net);
  33107. +#endif /* CONFIG_KLIPS_DEBUG */
  33108. + remove_proc_entry("ipsec_eroute", proc_net);
  33109. + remove_proc_entry("ipsec_spi", proc_net);
  33110. + remove_proc_entry("ipsec_spigrp", proc_net);
  33111. + remove_proc_entry("ipsec_tncfg", proc_net);
  33112. + remove_proc_entry("ipsec_version", proc_net);
  33113. + remove_proc_entry("ipsec", proc_net);
  33114. +#endif /* 2.4 kernel */
  33115. +}
  33116. +
  33117. +/*
  33118. + * $Log: ipsec_proc.c,v $
  33119. + * Revision 1.39.2.4 2006/11/15 22:21:39 paul
  33120. + * backport of creating a /sys/ file to test for nat-t capability in kernel.
  33121. + *
  33122. + * Revision 1.39.2.3 2006/10/06 21:39:26 paul
  33123. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  33124. + * set. This is defined through autoconf.h which is included through the
  33125. + * linux kernel build macros.
  33126. + *
  33127. + * Revision 1.39.2.2 2006/02/13 18:48:12 paul
  33128. + * Fix by Ankit Desai <ankit@elitecore.com> for module unloading.
  33129. + *
  33130. + * Revision 1.39.2.1 2005/09/07 00:45:59 paul
  33131. + * pull up of mcr's nat-t klips detection patch from head
  33132. + *
  33133. + * Revision 1.39 2005/05/20 03:19:18 mcr
  33134. + * modifications for use on 2.4.30 kernel, with backported
  33135. + * printk_ratelimit(). all warnings removed.
  33136. + *
  33137. + * Revision 1.38 2005/04/29 05:10:22 mcr
  33138. + * removed from extraenous includes to make unit testing easier.
  33139. + *
  33140. + * Revision 1.37 2005/04/13 22:49:49 mcr
  33141. + * moved KLIPS specific snprintf() wrapper to seperate file.
  33142. + *
  33143. + * Revision 1.36 2005/04/06 17:44:36 mcr
  33144. + * when NAT-T is compiled out, show encap as "NA"
  33145. + *
  33146. + * Revision 1.35 2005/01/26 00:50:35 mcr
  33147. + * adjustment of confusion of CONFIG_IPSEC_NAT vs CONFIG_KLIPS_NAT,
  33148. + * and make sure that NAT_TRAVERSAL is set as well to match
  33149. + * userspace compiles of code.
  33150. + *
  33151. + * Revision 1.34 2004/12/03 21:25:57 mcr
  33152. + * compile time fixes for running on 2.6.
  33153. + * still experimental.
  33154. + *
  33155. + * Revision 1.33 2004/08/17 03:27:23 mcr
  33156. + * klips 2.6 edits.
  33157. + *
  33158. + * Revision 1.32 2004/08/03 18:19:08 mcr
  33159. + * in 2.6, use "net_device" instead of #define device->net_device.
  33160. + * this probably breaks 2.0 compiles.
  33161. + *
  33162. + * Revision 1.31 2004/07/10 19:11:18 mcr
  33163. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  33164. + *
  33165. + * Revision 1.30 2004/04/25 21:23:11 ken
  33166. + * Pull in dhr's changes from FreeS/WAN 2.06
  33167. + *
  33168. + * Revision 1.29 2004/04/06 02:49:26 mcr
  33169. + * pullup of algo code from alg-branch.
  33170. + *
  33171. + * Revision 1.28 2004/03/28 20:29:58 paul
  33172. + * <hugh_> ssize_t, not ssized_t
  33173. + *
  33174. + * Revision 1.27 2004/03/28 20:27:20 paul
  33175. + * Included tested and confirmed fixes mcr made and dhr verified for
  33176. + * snprint statements. Changed one other snprintf to use ipsec_snprintf
  33177. + * so it wouldnt break compatibility with 2.0/2.2 kernels. Verified with
  33178. + * dhr. (thanks dhr!)
  33179. + *
  33180. + * Revision 1.26 2004/02/09 22:07:06 mcr
  33181. + * added information about nat-traversal setting to spi-output.
  33182. + *
  33183. + * Revision 1.25.4.1 2004/04/05 04:30:46 mcr
  33184. + * patches for alg-branch to compile/work with 2.x openswan
  33185. + *
  33186. + * Revision 1.25 2003/10/31 02:27:55 mcr
  33187. + * pulled up port-selector patches and sa_id elimination.
  33188. + *
  33189. + * Revision 1.24.4.1 2003/10/29 01:30:41 mcr
  33190. + * elimited "struct sa_id".
  33191. + *
  33192. + * Revision 1.24 2003/06/20 01:42:21 mcr
  33193. + * added counters to measure how many ACQUIREs we send to pluto,
  33194. + * and how many are successfully sent.
  33195. + *
  33196. + * Revision 1.23 2003/04/03 17:38:09 rgb
  33197. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  33198. + *
  33199. + * Revision 1.22 2002/09/20 15:40:57 rgb
  33200. + * Renamed saref macros for consistency and brevity.
  33201. + *
  33202. + * Revision 1.21 2002/09/20 05:01:35 rgb
  33203. + * Print ref and reftable, refentry seperately.
  33204. + *
  33205. + * Revision 1.20 2002/09/19 02:35:39 mcr
  33206. + * do not define structures needed by /proc/net/ipsec/ if we
  33207. + * aren't going create that directory.
  33208. + *
  33209. + * Revision 1.19 2002/09/10 01:43:25 mcr
  33210. + * fixed problem in /-* comment.
  33211. + *
  33212. + * Revision 1.18 2002/09/03 16:22:11 mcr
  33213. + * fixed initialization of birth/stuff values - some simple
  33214. + * screw ups in the code.
  33215. + * removed debugging that was left in by mistake.
  33216. + *
  33217. + * Revision 1.17 2002/09/02 17:54:53 mcr
  33218. + * changed how the table driven /proc entries are created so that
  33219. + * making subdirs is now explicit rather than implicit.
  33220. + *
  33221. + * Revision 1.16 2002/08/30 01:23:37 mcr
  33222. + * reorganized /proc creating code to clear up ifdefs,
  33223. + * make the 2.4 code table driven, and put things into
  33224. + * /proc/net/ipsec subdir. Symlinks are left for compatibility.
  33225. + *
  33226. + * Revision 1.15 2002/08/13 19:01:25 mcr
  33227. + * patches from kenb to permit compilation of FreeSWAN on ia64.
  33228. + * des library patched to use proper DES_LONG type for ia64.
  33229. + *
  33230. + * Revision 1.14 2002/07/26 08:48:31 rgb
  33231. + * Added SA ref table code.
  33232. + *
  33233. + * Revision 1.13 2002/07/24 18:44:54 rgb
  33234. + * Type fiddling to tame ia64 compiler.
  33235. + *
  33236. + * Revision 1.12 2002/05/27 18:56:07 rgb
  33237. + * Convert to dynamic ipsec device allocation.
  33238. + *
  33239. + * Revision 1.11 2002/05/23 07:14:50 rgb
  33240. + * Added refcount code.
  33241. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  33242. + * Convert "usecount" to "refcount" to remove ambiguity.
  33243. + *
  33244. + * Revision 1.10 2002/04/24 07:55:32 mcr
  33245. + * #include patches and Makefiles for post-reorg compilation.
  33246. + *
  33247. + * Revision 1.9 2002/04/24 07:36:28 mcr
  33248. + * Moved from ./klips/net/ipsec/ipsec_proc.c,v
  33249. + *
  33250. + * Revision 1.8 2002/01/29 17:17:55 mcr
  33251. + * moved include of ipsec_param.h to after include of linux/kernel.h
  33252. + * otherwise, it seems that some option that is set in ipsec_param.h
  33253. + * screws up something subtle in the include path to kernel.h, and
  33254. + * it complains on the snprintf() prototype.
  33255. + *
  33256. + * Revision 1.7 2002/01/29 04:00:52 mcr
  33257. + * more excise of kversions.h header.
  33258. + *
  33259. + * Revision 1.6 2002/01/29 02:13:17 mcr
  33260. + * introduction of ipsec_kversion.h means that include of
  33261. + * ipsec_param.h must preceed any decisions about what files to
  33262. + * include to deal with differences in kernel source.
  33263. + *
  33264. + * Revision 1.5 2002/01/12 02:54:30 mcr
  33265. + * beginnings of /proc/net/ipsec dir.
  33266. + *
  33267. + * Revision 1.4 2001/12/11 02:21:05 rgb
  33268. + * Don't include module version here, fixing 2.2 compile bug.
  33269. + *
  33270. + * Revision 1.3 2001/12/05 07:19:44 rgb
  33271. + * Fixed extraneous #include "version.c" bug causing modular KLIPS failure.
  33272. + *
  33273. + * Revision 1.2 2001/11/26 09:16:14 rgb
  33274. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  33275. + *
  33276. + * Revision 1.74 2001/11/22 05:44:11 henry
  33277. + * new version stuff
  33278. + *
  33279. + * Revision 1.1.2.1 2001/09/25 02:19:40 mcr
  33280. + * /proc manipulation code moved to new ipsec_proc.c
  33281. + *
  33282. + *
  33283. + * Local variables:
  33284. + * c-file-style: "linux"
  33285. + * End:
  33286. + *
  33287. + */
  33288. --- /dev/null Tue Mar 11 13:02:56 2003
  33289. +++ linux/net/ipsec/ipsec_radij.c Mon Feb 9 13:51:03 2004
  33290. @@ -0,0 +1,889 @@
  33291. +/*
  33292. + * Interface between the IPSEC code and the radix (radij) tree code
  33293. + * Copyright (C) 1996, 1997 John Ioannidis.
  33294. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  33295. + *
  33296. + * This program is free software; you can redistribute it and/or modify it
  33297. + * under the terms of the GNU General Public License as published by the
  33298. + * Free Software Foundation; either version 2 of the License, or (at your
  33299. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  33300. + *
  33301. + * This program is distributed in the hope that it will be useful, but
  33302. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  33303. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  33304. + * for more details.
  33305. + *
  33306. + * RCSID $Id: ipsec_radij.c,v 1.73.2.1 2006/10/06 21:39:26 paul Exp $
  33307. + */
  33308. +
  33309. +#ifndef AUTOCONF_INCLUDED
  33310. +#include <linux/config.h>
  33311. +#endif
  33312. +#include <linux/version.h>
  33313. +#include <linux/kernel.h> /* printk() */
  33314. +
  33315. +#include "openswan/ipsec_param.h"
  33316. +
  33317. +#ifdef MALLOC_SLAB
  33318. +# include <linux/slab.h> /* kmalloc() */
  33319. +#else /* MALLOC_SLAB */
  33320. +# include <linux/malloc.h> /* kmalloc() */
  33321. +#endif /* MALLOC_SLAB */
  33322. +#include <linux/errno.h> /* error codes */
  33323. +#include <linux/types.h> /* size_t */
  33324. +#include <linux/interrupt.h> /* mark_bh */
  33325. +
  33326. +#include <linux/netdevice.h> /* struct device, struct net_device_stats and other headers */
  33327. +#include <linux/etherdevice.h> /* eth_type_trans */
  33328. +#include <linux/ip.h> /* struct iphdr */
  33329. +#include <linux/skbuff.h>
  33330. +#include <openswan.h>
  33331. +#ifdef SPINLOCK
  33332. +# ifdef SPINLOCK_23
  33333. +# include <linux/spinlock.h> /* *lock* */
  33334. +# else /* 23_SPINLOCK */
  33335. +# include <asm/spinlock.h> /* *lock* */
  33336. +# endif /* 23_SPINLOCK */
  33337. +#endif /* SPINLOCK */
  33338. +
  33339. +#include <net/ip.h>
  33340. +
  33341. +#include "openswan/ipsec_eroute.h"
  33342. +#include "openswan/ipsec_sa.h"
  33343. +
  33344. +#include "openswan/radij.h"
  33345. +#include "openswan/ipsec_encap.h"
  33346. +#include "openswan/radij.h"
  33347. +#include "openswan/ipsec_encap.h"
  33348. +#include "openswan/ipsec_radij.h"
  33349. +#include "openswan/ipsec_tunnel.h" /* struct ipsecpriv */
  33350. +#include "openswan/ipsec_xform.h"
  33351. +
  33352. +#include <pfkeyv2.h>
  33353. +#include <pfkey.h>
  33354. +
  33355. +#include "openswan/ipsec_proto.h"
  33356. +
  33357. +#ifdef CONFIG_KLIPS_DEBUG
  33358. +int debug_radij = 0;
  33359. +#endif /* CONFIG_KLIPS_DEBUG */
  33360. +
  33361. +struct radij_node_head *rnh = NULL;
  33362. +#ifdef SPINLOCK
  33363. +spinlock_t eroute_lock = SPIN_LOCK_UNLOCKED;
  33364. +#else /* SPINLOCK */
  33365. +spinlock_t eroute_lock;
  33366. +#endif /* SPINLOCK */
  33367. +
  33368. +int
  33369. +ipsec_radijinit(void)
  33370. +{
  33371. + maj_keylen = sizeof (struct sockaddr_encap);
  33372. +
  33373. + rj_init();
  33374. +
  33375. + if (rj_inithead((void **)&rnh, /*16*/offsetof(struct sockaddr_encap, sen_type) * sizeof(__u8)) == 0) /* 16 is bit offset of sen_type */
  33376. + return -1;
  33377. + return 0;
  33378. +}
  33379. +
  33380. +int
  33381. +ipsec_radijcleanup(void)
  33382. +{
  33383. + int error;
  33384. +
  33385. + spin_lock_bh(&eroute_lock);
  33386. +
  33387. + error = radijcleanup();
  33388. +
  33389. + spin_unlock_bh(&eroute_lock);
  33390. +
  33391. + return error;
  33392. +}
  33393. +
  33394. +int
  33395. +ipsec_cleareroutes(void)
  33396. +{
  33397. + int error;
  33398. +
  33399. + spin_lock_bh(&eroute_lock);
  33400. +
  33401. + error = radijcleartree();
  33402. +
  33403. + spin_unlock_bh(&eroute_lock);
  33404. +
  33405. + return error;
  33406. +}
  33407. +
  33408. +int
  33409. +ipsec_breakroute(struct sockaddr_encap *eaddr,
  33410. + struct sockaddr_encap *emask,
  33411. + struct sk_buff **first,
  33412. + struct sk_buff **last)
  33413. +{
  33414. + struct eroute *ro;
  33415. + struct radij_node *rn;
  33416. + int error;
  33417. +#ifdef CONFIG_KLIPS_DEBUG
  33418. +
  33419. + if (debug_eroute) {
  33420. + char buf1[SUBNETTOA_BUF], buf2[SUBNETTOA_BUF];
  33421. + subnettoa(eaddr->sen_ip_src, emask->sen_ip_src, 0, buf1, sizeof(buf1));
  33422. + subnettoa(eaddr->sen_ip_dst, emask->sen_ip_dst, 0, buf2, sizeof(buf2));
  33423. + KLIPS_PRINT(debug_eroute,
  33424. + "klips_debug:ipsec_breakroute: "
  33425. + "attempting to delete eroute for %s:%d->%s:%d %d\n",
  33426. + buf1, ntohs(eaddr->sen_sport),
  33427. + buf2, ntohs(eaddr->sen_dport), eaddr->sen_proto);
  33428. + }
  33429. +#endif /* CONFIG_KLIPS_DEBUG */
  33430. +
  33431. + spin_lock_bh(&eroute_lock);
  33432. +
  33433. + if ((error = rj_delete(eaddr, emask, rnh, &rn)) != 0) {
  33434. + spin_unlock_bh(&eroute_lock);
  33435. + KLIPS_PRINT(debug_eroute,
  33436. + "klips_debug:ipsec_breakroute: "
  33437. + "node not found, eroute delete failed.\n");
  33438. + return error;
  33439. + }
  33440. +
  33441. + spin_unlock_bh(&eroute_lock);
  33442. +
  33443. + ro = (struct eroute *)rn;
  33444. +
  33445. + KLIPS_PRINT(debug_eroute,
  33446. + "klips_debug:ipsec_breakroute: "
  33447. + "deleted eroute=0p%p, ident=0p%p->0p%p, first=0p%p, last=0p%p\n",
  33448. + ro,
  33449. + ro->er_ident_s.data,
  33450. + ro->er_ident_d.data,
  33451. + ro->er_first,
  33452. + ro->er_last);
  33453. +
  33454. + if (ro->er_ident_s.data != NULL) {
  33455. + kfree(ro->er_ident_s.data);
  33456. + }
  33457. + if (ro->er_ident_d.data != NULL) {
  33458. + kfree(ro->er_ident_d.data);
  33459. + }
  33460. + if (ro->er_first != NULL) {
  33461. +#if 0
  33462. + struct net_device_stats *stats = (struct net_device_stats *) &(((struct ipsecpriv *)(ro->er_first->dev->priv))->mystats);
  33463. + stats->tx_dropped--;
  33464. +#endif
  33465. + *first = ro->er_first;
  33466. + }
  33467. + if (ro->er_last != NULL) {
  33468. +#if 0
  33469. + struct net_device_stats *stats = (struct net_device_stats *) &(((struct ipsecpriv *)(ro->er_last->dev->priv))->mystats);
  33470. + stats->tx_dropped--;
  33471. +#endif
  33472. + *last = ro->er_last;
  33473. + }
  33474. +
  33475. + if (rn->rj_flags & (RJF_ACTIVE | RJF_ROOT))
  33476. + panic ("ipsec_breakroute RMT_DELEROUTE root or active node\n");
  33477. + memset((caddr_t)rn, 0, sizeof (struct eroute));
  33478. + kfree(rn);
  33479. +
  33480. + return 0;
  33481. +}
  33482. +
  33483. +int
  33484. +ipsec_makeroute(struct sockaddr_encap *eaddr,
  33485. + struct sockaddr_encap *emask,
  33486. + ip_said said,
  33487. + uint32_t pid,
  33488. + struct sk_buff *skb,
  33489. + struct ident *ident_s,
  33490. + struct ident *ident_d)
  33491. +{
  33492. + struct eroute *retrt;
  33493. + int error;
  33494. + char sa[SATOT_BUF];
  33495. + size_t sa_len;
  33496. +
  33497. +#ifdef CONFIG_KLIPS_DEBUG
  33498. +
  33499. + if (debug_eroute) {
  33500. +
  33501. + {
  33502. + char buf1[SUBNETTOA_BUF], buf2[SUBNETTOA_BUF];
  33503. +
  33504. + subnettoa(eaddr->sen_ip_src, emask->sen_ip_src, 0, buf1, sizeof(buf1));
  33505. + subnettoa(eaddr->sen_ip_dst, emask->sen_ip_dst, 0, buf2, sizeof(buf2));
  33506. + sa_len = satot(&said, 0, sa, sizeof(sa));
  33507. + KLIPS_PRINT(debug_eroute,
  33508. + "klips_debug:ipsec_makeroute: "
  33509. + "attempting to allocate %lu bytes to insert eroute for %s->%s, SA: %s, PID:%d, skb=0p%p, ident:%s->%s\n",
  33510. + (unsigned long) sizeof(struct eroute),
  33511. + buf1,
  33512. + buf2,
  33513. + sa_len ? sa : " (error)",
  33514. + pid,
  33515. + skb,
  33516. + (ident_s ? (ident_s->data ? ident_s->data : "NULL") : "NULL"),
  33517. + (ident_d ? (ident_d->data ? ident_d->data : "NULL") : "NULL"));
  33518. + }
  33519. + {
  33520. + char buf1[sizeof(struct sockaddr_encap)*2 + 1],
  33521. + buf2[sizeof(struct sockaddr_encap)*2 + 1];
  33522. + int i;
  33523. + unsigned char *b1 = buf1,
  33524. + *b2 = buf2,
  33525. + *ea = (unsigned char *)eaddr,
  33526. + *em = (unsigned char *)emask;
  33527. +
  33528. +
  33529. + for (i=0; i<sizeof(struct sockaddr_encap); i++) {
  33530. + sprintf(b1, "%02x", ea[i]);
  33531. + sprintf(b2, "%02x", em[i]);
  33532. + b1+=2;
  33533. + b2+=2;
  33534. + }
  33535. + KLIPS_PRINT(debug_eroute, "klips_debug:ipsec_makeroute: %s / %s \n", buf1, buf2);
  33536. + }
  33537. +
  33538. + }
  33539. +#endif /* CONFIG_KLIPS_DEBUG */
  33540. +
  33541. + retrt = (struct eroute *)kmalloc(sizeof (struct eroute), GFP_ATOMIC);
  33542. + if (retrt == NULL) {
  33543. + printk("klips_error:ipsec_makeroute: "
  33544. + "not able to allocate kernel memory");
  33545. + return -ENOMEM;
  33546. + }
  33547. + memset((caddr_t)retrt, 0, sizeof (struct eroute));
  33548. +
  33549. + retrt->er_eaddr = *eaddr;
  33550. + retrt->er_emask = *emask;
  33551. + retrt->er_said = said;
  33552. + retrt->er_pid = pid;
  33553. + retrt->er_count = 0;
  33554. + retrt->er_lasttime = jiffies/HZ;
  33555. +
  33556. + {
  33557. + /* this is because gcc 3. doesn't like cast's as lvalues */
  33558. + struct rjtentry *rje = (struct rjtentry *)&(retrt->er_rjt);
  33559. + caddr_t er = (caddr_t)&(retrt->er_eaddr);
  33560. +
  33561. + rje->rd_nodes->rj_key= er;
  33562. + }
  33563. +
  33564. + if (ident_s && ident_s->type != SADB_IDENTTYPE_RESERVED) {
  33565. + int data_len = ident_s->len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  33566. +
  33567. + retrt->er_ident_s.type = ident_s->type;
  33568. + retrt->er_ident_s.id = ident_s->id;
  33569. + retrt->er_ident_s.len = ident_s->len;
  33570. + if(data_len) {
  33571. + KLIPS_PRINT(debug_eroute,
  33572. + "klips_debug:ipsec_makeroute: "
  33573. + "attempting to allocate %u bytes for ident_s.\n",
  33574. + data_len);
  33575. + if(!(retrt->er_ident_s.data = kmalloc(data_len, GFP_KERNEL))) {
  33576. + kfree(retrt);
  33577. + printk("klips_error:ipsec_makeroute: not able to allocate kernel memory (%d)\n", data_len);
  33578. + return ENOMEM;
  33579. + }
  33580. + memcpy(retrt->er_ident_s.data, ident_s->data, data_len);
  33581. + } else {
  33582. + retrt->er_ident_s.data = NULL;
  33583. + }
  33584. + }
  33585. +
  33586. + if (ident_d && ident_d->type != SADB_IDENTTYPE_RESERVED) {
  33587. + int data_len = ident_d->len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  33588. +
  33589. + retrt->er_ident_d.type = ident_d->type;
  33590. + retrt->er_ident_d.id = ident_d->id;
  33591. + retrt->er_ident_d.len = ident_d->len;
  33592. + if(data_len) {
  33593. + KLIPS_PRINT(debug_eroute,
  33594. + "klips_debug:ipsec_makeroute: "
  33595. + "attempting to allocate %u bytes for ident_d.\n",
  33596. + data_len);
  33597. + if(!(retrt->er_ident_d.data = kmalloc(data_len, GFP_KERNEL))) {
  33598. + if (retrt->er_ident_s.data)
  33599. + kfree(retrt->er_ident_s.data);
  33600. + kfree(retrt);
  33601. + printk("klips_error:ipsec_makeroute: not able to allocate kernel memory (%d)\n", data_len);
  33602. + return ENOMEM;
  33603. + }
  33604. + memcpy(retrt->er_ident_d.data, ident_d->data, data_len);
  33605. + } else {
  33606. + retrt->er_ident_d.data = NULL;
  33607. + }
  33608. + }
  33609. + retrt->er_first = skb;
  33610. + retrt->er_last = NULL;
  33611. +
  33612. + KLIPS_PRINT(debug_eroute,
  33613. + "klips_debug:ipsec_makeroute: "
  33614. + "calling rj_addroute now\n");
  33615. +
  33616. + spin_lock_bh(&eroute_lock);
  33617. +
  33618. + error = rj_addroute(&(retrt->er_eaddr), &(retrt->er_emask),
  33619. + rnh, retrt->er_rjt.rd_nodes);
  33620. +
  33621. + spin_unlock_bh(&eroute_lock);
  33622. +
  33623. + if(error) {
  33624. + sa_len = satot(&said, 0, sa, sizeof(sa));
  33625. + KLIPS_PRINT(debug_eroute,
  33626. + "klips_debug:ipsec_makeroute: "
  33627. + "rj_addroute not able to insert eroute for SA:%s (error:%d)\n",
  33628. + sa_len ? sa : " (error)", error);
  33629. + if (retrt->er_ident_s.data)
  33630. + kfree(retrt->er_ident_s.data);
  33631. + if (retrt->er_ident_d.data)
  33632. + kfree(retrt->er_ident_d.data);
  33633. +
  33634. + kfree(retrt);
  33635. +
  33636. + return error;
  33637. + }
  33638. +
  33639. +#ifdef CONFIG_KLIPS_DEBUG
  33640. + if (debug_eroute) {
  33641. + char buf1[SUBNETTOA_BUF], buf2[SUBNETTOA_BUF];
  33642. +/*
  33643. + subnettoa(eaddr->sen_ip_src, emask->sen_ip_src, 0, buf1, sizeof(buf1));
  33644. + subnettoa(eaddr->sen_ip_dst, emask->sen_ip_dst, 0, buf2, sizeof(buf2));
  33645. +*/
  33646. + subnettoa(rd_key((&(retrt->er_rjt)))->sen_ip_src, rd_mask((&(retrt->er_rjt)))->sen_ip_src, 0, buf1, sizeof(buf1));
  33647. + subnettoa(rd_key((&(retrt->er_rjt)))->sen_ip_dst, rd_mask((&(retrt->er_rjt)))->sen_ip_dst, 0, buf2, sizeof(buf2));
  33648. + sa_len = satot(&retrt->er_said, 0, sa, sizeof(sa));
  33649. +
  33650. + KLIPS_PRINT(debug_eroute,
  33651. + "klips_debug:ipsec_makeroute: "
  33652. + "pid=%05d "
  33653. + "count=%10d "
  33654. + "lasttime=%6d "
  33655. + "%-18s -> %-18s => %s\n",
  33656. + retrt->er_pid,
  33657. + retrt->er_count,
  33658. + (int)(jiffies/HZ - retrt->er_lasttime),
  33659. + buf1,
  33660. + buf2,
  33661. + sa_len ? sa : " (error)");
  33662. + }
  33663. +#endif /* CONFIG_KLIPS_DEBUG */
  33664. + KLIPS_PRINT(debug_eroute,
  33665. + "klips_debug:ipsec_makeroute: "
  33666. + "succeeded.\n");
  33667. + return 0;
  33668. +}
  33669. +
  33670. +struct eroute *
  33671. +ipsec_findroute(struct sockaddr_encap *eaddr)
  33672. +{
  33673. + struct radij_node *rn;
  33674. +#ifdef CONFIG_KLIPS_DEBUG
  33675. + char buf1[ADDRTOA_BUF], buf2[ADDRTOA_BUF];
  33676. +
  33677. + if (debug_radij & DB_RJ_FINDROUTE) {
  33678. + addrtoa(eaddr->sen_ip_src, 0, buf1, sizeof(buf1));
  33679. + addrtoa(eaddr->sen_ip_dst, 0, buf2, sizeof(buf2));
  33680. + KLIPS_PRINT(debug_eroute,
  33681. + "klips_debug:ipsec_findroute: "
  33682. + "%s:%d->%s:%d %d\n",
  33683. + buf1, ntohs(eaddr->sen_sport),
  33684. + buf2, ntohs(eaddr->sen_dport),
  33685. + eaddr->sen_proto);
  33686. + }
  33687. +#endif /* CONFIG_KLIPS_DEBUG */
  33688. + rn = rj_match((caddr_t)eaddr, rnh);
  33689. + if(rn) {
  33690. + KLIPS_PRINT(debug_eroute && sysctl_ipsec_debug_verbose,
  33691. + "klips_debug:ipsec_findroute: "
  33692. + "found, points to proto=%d, spi=%x, dst=%x.\n",
  33693. + ((struct eroute*)rn)->er_said.proto,
  33694. + ntohl(((struct eroute*)rn)->er_said.spi),
  33695. + ntohl(((struct eroute*)rn)->er_said.dst.u.v4.sin_addr.s_addr));
  33696. + }
  33697. + return (struct eroute *)rn;
  33698. +}
  33699. +
  33700. +#ifdef CONFIG_PROC_FS
  33701. +/** ipsec_rj_walker_procprint: print one line of eroute table output.
  33702. + *
  33703. + * Theoretical BUG: if w->length is less than the length
  33704. + * of some line we should produce, that line will never
  33705. + * be finished. In effect, the "file" will stop part way
  33706. + * through that line.
  33707. + */
  33708. +int
  33709. +ipsec_rj_walker_procprint(struct radij_node *rn, void *w0)
  33710. +{
  33711. + struct eroute *ro = (struct eroute *)rn;
  33712. + struct rjtentry *rd = (struct rjtentry *)rn;
  33713. + struct wsbuf *w = (struct wsbuf *)w0;
  33714. + char buf1[SUBNETTOA_BUF], buf2[SUBNETTOA_BUF];
  33715. + char buf3[16];
  33716. + char sa[SATOT_BUF];
  33717. + size_t sa_len, buf_len;
  33718. + struct sockaddr_encap *key, *mask;
  33719. +
  33720. + KLIPS_PRINT(debug_radij,
  33721. + "klips_debug:ipsec_rj_walker_procprint: "
  33722. + "rn=0p%p, w0=0p%p\n",
  33723. + rn,
  33724. + w0);
  33725. + if (rn->rj_b >= 0) {
  33726. + return 0;
  33727. + }
  33728. +
  33729. + key = rd_key(rd);
  33730. + mask = rd_mask(rd);
  33731. +
  33732. + if (key == NULL || mask == NULL) {
  33733. + return 0;
  33734. + }
  33735. +
  33736. + buf_len = subnettoa(key->sen_ip_src, mask->sen_ip_src, 0, buf1, sizeof(buf1));
  33737. + if(key->sen_sport != 0) {
  33738. + sprintf(buf1+buf_len-1, ":%d", ntohs(key->sen_sport));
  33739. + }
  33740. +
  33741. + buf_len = subnettoa(key->sen_ip_dst, mask->sen_ip_dst, 0, buf2, sizeof(buf2));
  33742. + if(key->sen_dport != 0) {
  33743. + sprintf(buf2+buf_len-1, ":%d", ntohs(key->sen_dport));
  33744. + }
  33745. +
  33746. + buf3[0]='\0';
  33747. + if(key->sen_proto != 0) {
  33748. + sprintf(buf3, ":%d", key->sen_proto);
  33749. + }
  33750. +
  33751. + sa_len = satot(&ro->er_said, 'x', sa, sizeof(sa));
  33752. + w->len += ipsec_snprintf(w->buffer + w->len,
  33753. + w->length - w->len,
  33754. + "%-10d "
  33755. + "%-18s -> %-18s => %s%s\n",
  33756. + ro->er_count,
  33757. + buf1,
  33758. + buf2,
  33759. + sa_len ? sa : " (error)",
  33760. + buf3);
  33761. +
  33762. + {
  33763. + /* snprintf can only fill the last character with NUL
  33764. + * so the maximum useful character is w->length-1.
  33765. + * However, if w->length == 0, we cannot go back.
  33766. + * (w->length surely cannot be negative.)
  33767. + */
  33768. + int max_content = w->length > 0? w->length-1 : 0;
  33769. +
  33770. + if (w->len >= max_content) {
  33771. + /* we've done all that can fit -- stop treewalking */
  33772. + w->len = max_content; /* truncate crap */
  33773. + return -ENOBUFS;
  33774. + } else {
  33775. + const off_t pos = w->begin + w->len; /* file position of end of what we've generated */
  33776. +
  33777. + if (pos <= w->offset) {
  33778. + /* all is before first interesting character:
  33779. + * discard, but note where we are.
  33780. + */
  33781. + w->len = 0;
  33782. + w->begin = pos;
  33783. + }
  33784. + return 0;
  33785. + }
  33786. + }
  33787. +}
  33788. +#endif /* CONFIG_PROC_FS */
  33789. +
  33790. +int
  33791. +ipsec_rj_walker_delete(struct radij_node *rn, void *w0)
  33792. +{
  33793. + struct eroute *ro;
  33794. + struct rjtentry *rd = (struct rjtentry *)rn;
  33795. + struct radij_node *rn2;
  33796. + int error;
  33797. + struct sockaddr_encap *key, *mask;
  33798. +
  33799. + key = rd_key(rd);
  33800. + mask = rd_mask(rd);
  33801. +
  33802. + if(!key || !mask) {
  33803. + return -ENODATA;
  33804. + }
  33805. +#ifdef CONFIG_KLIPS_DEBUG
  33806. + if(debug_radij) {
  33807. + char buf1[SUBNETTOA_BUF], buf2[SUBNETTOA_BUF];
  33808. + subnettoa(key->sen_ip_src, mask->sen_ip_src, 0, buf1, sizeof(buf1));
  33809. + subnettoa(key->sen_ip_dst, mask->sen_ip_dst, 0, buf2, sizeof(buf2));
  33810. + KLIPS_PRINT(debug_radij,
  33811. + "klips_debug:ipsec_rj_walker_delete: "
  33812. + "deleting: %s -> %s\n",
  33813. + buf1,
  33814. + buf2);
  33815. + }
  33816. +#endif /* CONFIG_KLIPS_DEBUG */
  33817. +
  33818. + if((error = rj_delete(key, mask, rnh, &rn2))) {
  33819. + KLIPS_PRINT(debug_radij,
  33820. + "klips_debug:ipsec_rj_walker_delete: "
  33821. + "rj_delete failed with error=%d.\n", error);
  33822. + return error;
  33823. + }
  33824. +
  33825. + if(rn2 != rn) {
  33826. + printk("klips_debug:ipsec_rj_walker_delete: "
  33827. + "tried to delete a different node?!? This should never happen!\n");
  33828. + }
  33829. +
  33830. + ro = (struct eroute *)rn;
  33831. +
  33832. + if (ro->er_ident_s.data)
  33833. + kfree(ro->er_ident_s.data);
  33834. + if (ro->er_ident_d.data)
  33835. + kfree(ro->er_ident_d.data);
  33836. +
  33837. + memset((caddr_t)rn, 0, sizeof (struct eroute));
  33838. + kfree(rn);
  33839. +
  33840. + return 0;
  33841. +}
  33842. +
  33843. +/*
  33844. + * $Log: ipsec_radij.c,v $
  33845. + * Revision 1.73.2.1 2006/10/06 21:39:26 paul
  33846. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  33847. + * set. This is defined through autoconf.h which is included through the
  33848. + * linux kernel build macros.
  33849. + *
  33850. + * Revision 1.73 2005/04/29 05:10:22 mcr
  33851. + * removed from extraenous includes to make unit testing easier.
  33852. + *
  33853. + * Revision 1.72 2004/12/03 21:25:57 mcr
  33854. + * compile time fixes for running on 2.6.
  33855. + * still experimental.
  33856. + *
  33857. + * Revision 1.71 2004/07/10 19:11:18 mcr
  33858. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  33859. + *
  33860. + * Revision 1.70 2004/04/25 21:10:52 ken
  33861. + * Pull in dhr's changes from FreeS/WAN 2.06
  33862. + *
  33863. + * Revision 1.69 2004/04/06 02:49:26 mcr
  33864. + * pullup of algo code from alg-branch.
  33865. + *
  33866. + * Revision 1.68 2004/03/28 20:27:20 paul
  33867. + * Included tested and confirmed fixes mcr made and dhr verified for
  33868. + * snprint statements. Changed one other snprintf to use ipsec_snprintf
  33869. + * so it wouldnt break compatibility with 2.0/2.2 kernels. Verified with
  33870. + * dhr. (thanks dhr!)
  33871. + *
  33872. + * Revision 1.67.4.1 2004/04/05 04:30:46 mcr
  33873. + * patches for alg-branch to compile/work with 2.x openswan
  33874. + *
  33875. + * Revision 1.67 2003/10/31 02:27:55 mcr
  33876. + * pulled up port-selector patches and sa_id elimination.
  33877. + *
  33878. + * Revision 1.66.24.2 2003/10/29 01:30:41 mcr
  33879. + * elimited "struct sa_id".
  33880. + *
  33881. + * Revision 1.66.24.1 2003/09/21 13:59:56 mcr
  33882. + * pre-liminary X.509 patch - does not yet pass tests.
  33883. + *
  33884. + * Revision 1.66 2002/10/12 23:11:53 dhr
  33885. + *
  33886. + * [KenB + DHR] more 64-bit cleanup
  33887. + *
  33888. + * Revision 1.65 2002/09/20 05:01:40 rgb
  33889. + * Added memory allocation debugging.
  33890. + *
  33891. + * Revision 1.64 2002/05/31 01:46:05 mcr
  33892. + * added && sysctl_ipsec_debug_verbose verbose to ipsec_findroute
  33893. + * as requested in PR#14.
  33894. + *
  33895. + * Revision 1.63 2002/05/23 07:14:11 rgb
  33896. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  33897. + *
  33898. + * Revision 1.62 2002/04/24 07:55:32 mcr
  33899. + * #include patches and Makefiles for post-reorg compilation.
  33900. + *
  33901. + * Revision 1.61 2002/04/24 07:36:29 mcr
  33902. + * Moved from ./klips/net/ipsec/ipsec_radij.c,v
  33903. + *
  33904. + * Revision 1.60 2002/02/19 23:59:45 rgb
  33905. + * Removed redundant compiler directives.
  33906. + *
  33907. + * Revision 1.59 2002/02/06 04:13:47 mcr
  33908. + * missing #ifdef CONFIG_IPSEC_DEBUG.
  33909. + *
  33910. + * Revision 1.58 2002/01/29 17:17:56 mcr
  33911. + * moved include of ipsec_param.h to after include of linux/kernel.h
  33912. + * otherwise, it seems that some option that is set in ipsec_param.h
  33913. + * screws up something subtle in the include path to kernel.h, and
  33914. + * it complains on the snprintf() prototype.
  33915. + *
  33916. + * Revision 1.57 2002/01/29 04:00:52 mcr
  33917. + * more excise of kversions.h header.
  33918. + *
  33919. + * Revision 1.56 2002/01/29 02:13:17 mcr
  33920. + * introduction of ipsec_kversion.h means that include of
  33921. + * ipsec_param.h must preceed any decisions about what files to
  33922. + * include to deal with differences in kernel source.
  33923. + *
  33924. + * Revision 1.55 2001/11/26 09:23:48 rgb
  33925. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  33926. + *
  33927. + * Revision 1.53.2.1 2001/09/25 02:26:32 mcr
  33928. + * headers adjusted for new usage.
  33929. + *
  33930. + * Revision 1.54 2001/10/18 04:45:20 rgb
  33931. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  33932. + * lib/freeswan.h version macros moved to lib/kversions.h.
  33933. + * Other compiler directive cleanups.
  33934. + *
  33935. + * Revision 1.53 2001/09/19 17:19:40 rgb
  33936. + * Debug output bugfix for NetCelo's PF_KEY ident patch.
  33937. + *
  33938. + * Revision 1.52 2001/09/19 16:33:37 rgb
  33939. + * Temporarily disable ident fields to /proc/net/ipsec_eroute.
  33940. + *
  33941. + * Revision 1.51 2001/09/15 16:24:04 rgb
  33942. + * Re-inject first and last HOLD packet when an eroute REPLACE is done.
  33943. + *
  33944. + * Revision 1.50 2001/09/14 16:58:36 rgb
  33945. + * Added support for storing the first and last packets through a HOLD.
  33946. + *
  33947. + * Revision 1.49 2001/09/08 21:13:32 rgb
  33948. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  33949. + *
  33950. + * Revision 1.48 2001/06/15 04:12:56 rgb
  33951. + * Fixed kernel memory allocation error return code polarity bug.
  33952. + *
  33953. + * Revision 1.47 2001/06/14 19:35:09 rgb
  33954. + * Update copyright date.
  33955. + *
  33956. + * Revision 1.46 2001/06/08 08:47:18 rgb
  33957. + * Fixed for debug disabled.
  33958. + *
  33959. + * Revision 1.45 2001/05/27 06:12:11 rgb
  33960. + * Added structures for pid, packet count and last access time to eroute.
  33961. + * Added packet count to beginning of /proc/net/ipsec_eroute.
  33962. + *
  33963. + * Revision 1.44 2001/05/03 19:41:01 rgb
  33964. + * Initialise error return variable.
  33965. + * Use more appropriate return value for ipsec_rj_walker_delete().
  33966. + *
  33967. + * Revision 1.43 2001/02/27 22:24:54 rgb
  33968. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  33969. + * Check for satoa() return codes.
  33970. + *
  33971. + * Revision 1.42 2001/02/27 06:21:57 rgb
  33972. + * Added findroute success instrumentation.
  33973. + *
  33974. + * Revision 1.41 2000/11/06 04:32:08 rgb
  33975. + * Ditched spin_lock_irqsave in favour of spin_lock_bh.
  33976. + *
  33977. + * Revision 1.40 2000/09/08 19:12:56 rgb
  33978. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  33979. + *
  33980. + * Revision 1.39 2000/08/30 05:25:20 rgb
  33981. + * Correct debug text in ipsec_breakroute() from incorrect
  33982. + * "ipsec_callback".
  33983. + *
  33984. + * Revision 1.38 2000/07/28 14:58:31 rgb
  33985. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  33986. + *
  33987. + * Revision 1.37 2000/03/16 14:02:50 rgb
  33988. + * Fixed debug scope to enable compilation with debug off.
  33989. + *
  33990. + * Revision 1.36 2000/01/21 06:14:46 rgb
  33991. + * Added debugging text to ipsec_rj_walker_delete().
  33992. + * Set return code to negative for consistency.
  33993. + *
  33994. + * Revision 1.35 1999/11/23 23:05:24 rgb
  33995. + * Use provided macro ADDRTOA_BUF instead of hardcoded value.
  33996. + *
  33997. + * Revision 1.34 1999/11/18 04:13:56 rgb
  33998. + * Replaced all kernel version macros to shorter, readable form.
  33999. + * Added CONFIG_PROC_FS compiler directives in case it is shut off.
  34000. + *
  34001. + * Revision 1.33 1999/11/17 15:53:39 rgb
  34002. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  34003. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  34004. + * klips/net/ipsec/Makefile.
  34005. + *
  34006. + * Revision 1.32 1999/10/26 13:58:33 rgb
  34007. + * Put spinlock flags variable declaration outside the debug compiler
  34008. + * directive to enable compilation with debug shut off.
  34009. + *
  34010. + * Revision 1.31 1999/10/15 22:13:29 rgb
  34011. + * Clean out cruft.
  34012. + * Align /proc/net/ipsec_eroute output for easier readability.
  34013. + * Fix double linefeed in radij debug output.
  34014. + * Fix double locking bug that locks up 2.0.36 but not 2.0.38.
  34015. + *
  34016. + * Revision 1.30 1999/10/08 18:37:33 rgb
  34017. + * Fix end-of-line spacing to sate whining PHMs.
  34018. + *
  34019. + * Revision 1.29 1999/10/03 18:52:45 rgb
  34020. + * Spinlock support for 2.0.xx.
  34021. + * Dumb return code spin_unlock fix.
  34022. + *
  34023. + * Revision 1.28 1999/10/01 16:22:24 rgb
  34024. + * Switch from assignment init. to functional init. of spinlocks.
  34025. + *
  34026. + * Revision 1.27 1999/10/01 15:44:53 rgb
  34027. + * Move spinlock header include to 2.1> scope.
  34028. + *
  34029. + * Revision 1.26 1999/10/01 00:01:23 rgb
  34030. + * Added eroute structure locking.
  34031. + *
  34032. + * Revision 1.25 1999/06/10 16:07:30 rgb
  34033. + * Silence delete eroute on no debug.
  34034. + *
  34035. + * Revision 1.24 1999/05/09 03:25:36 rgb
  34036. + * Fix bug introduced by 2.2 quick-and-dirty patch.
  34037. + *
  34038. + * Revision 1.23 1999/05/05 22:02:31 rgb
  34039. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  34040. + *
  34041. + * Revision 1.22 1999/04/29 15:17:23 rgb
  34042. + * Add return values to init and cleanup functions.
  34043. + * Add sanity checking for null pointer arguments.
  34044. + *
  34045. + * Revision 1.21 1999/04/11 00:28:58 henry
  34046. + * GPL boilerplate
  34047. + *
  34048. + * Revision 1.20 1999/04/06 04:54:26 rgb
  34049. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  34050. + * patch shell fixes.
  34051. + *
  34052. + * Revision 1.19 1999/02/17 16:50:35 rgb
  34053. + * Clean out unused cruft.
  34054. + * Consolidate for space and speed efficiency.
  34055. + * Convert DEBUG_IPSEC to KLIPS_PRINT
  34056. + *
  34057. + * Revision 1.18 1999/01/22 06:22:06 rgb
  34058. + * Cruft clean-out.
  34059. + * 64-bit clean-up.
  34060. + *
  34061. + * Revision 1.17 1998/12/02 03:09:39 rgb
  34062. + * Clean up debug printing conditionals to compile with debugging off.
  34063. + *
  34064. + * Revision 1.16 1998/12/01 13:49:39 rgb
  34065. + * Wrap version info printing in debug switches.
  34066. + *
  34067. + * Revision 1.15 1998/11/30 13:22:54 rgb
  34068. + * Rationalised all the klips kernel file headers. They are much shorter
  34069. + * now and won't conflict under RH5.2.
  34070. + *
  34071. + * Revision 1.14 1998/10/31 06:48:17 rgb
  34072. + * Fixed up comments in #endif directives.
  34073. + *
  34074. + * Revision 1.13 1998/10/27 13:48:09 rgb
  34075. + * Cleaned up /proc/net/ipsec_* filesystem for easy parsing by scripts.
  34076. + * Fixed less(1) truncated output bug.
  34077. + * Code clean-up.
  34078. + *
  34079. + * Revision 1.12 1998/10/25 02:41:36 rgb
  34080. + * Change return type on ipsec_breakroute and ipsec_makeroute and add an
  34081. + * argument to be able to transmit more infomation about errors.
  34082. + * Fix cut-and-paste debug statement identifier.
  34083. + *
  34084. + * Revision 1.11 1998/10/22 06:45:39 rgb
  34085. + * Cleaned up cruft.
  34086. + * Convert to use satoa for printk.
  34087. + *
  34088. + * Revision 1.10 1998/10/19 14:44:28 rgb
  34089. + * Added inclusion of freeswan.h.
  34090. + * sa_id structure implemented and used: now includes protocol.
  34091. + *
  34092. + * Revision 1.9 1998/10/09 04:30:52 rgb
  34093. + * Added 'klips_debug' prefix to all klips printk debug statements.
  34094. + * Deleted old commented out cruft.
  34095. + *
  34096. + * Revision 1.8 1998/08/06 17:24:23 rgb
  34097. + * Fix addrtoa return code bug from stale manpage advice preventing packets
  34098. + * from being erouted.
  34099. + *
  34100. + * Revision 1.7 1998/08/06 07:44:59 rgb
  34101. + * Fixed /proc/net/ipsec_eroute subnettoa and addrtoa return value bug that
  34102. + * ended up in nothing being printed.
  34103. + *
  34104. + * Revision 1.6 1998/08/05 22:16:41 rgb
  34105. + * Cleanup to prevent cosmetic errors (ie. debug output) from being fatal.
  34106. + *
  34107. + * Revision 1.5 1998/07/29 20:38:44 rgb
  34108. + * Debug and fix subnettoa and addrtoa output.
  34109. + *
  34110. + * Revision 1.4 1998/07/28 00:02:39 rgb
  34111. + * Converting to exclusive use of addrtoa.
  34112. + * Fix eroute delete.
  34113. + *
  34114. + * Revision 1.3 1998/07/14 18:21:26 rgb
  34115. + * Add function to clear the eroute table.
  34116. + *
  34117. + * Revision 1.2 1998/06/23 02:59:14 rgb
  34118. + * Added debugging output to eroute add/delete routines.
  34119. + *
  34120. + * Revision 1.9 1998/06/18 21:29:06 henry
  34121. + * move sources from klips/src to klips/net/ipsec, to keep stupid kernel
  34122. + * build scripts happier in presence of symbolic links
  34123. + *
  34124. + * Revision 1.8 1998/06/05 02:32:26 rgb
  34125. + * Fix spi ntoh kernel debug output.
  34126. + *
  34127. + * Revision 1.7 1998/05/25 20:30:37 rgb
  34128. + * Remove temporary ipsec_walk, rj_deltree and rj_delnodes functions.
  34129. + *
  34130. + * Rename ipsec_rj_walker (ipsec_walk) to ipsec_rj_walker_procprint and
  34131. + * add ipsec_rj_walker_delete.
  34132. + *
  34133. + * Revision 1.6 1998/05/21 13:08:57 rgb
  34134. + * Rewrote procinfo subroutines to avoid *bad things* when more that 3k of
  34135. + * information is available for printout.
  34136. + *
  34137. + * Revision 1.5 1998/05/18 21:35:55 rgb
  34138. + * Clean up output for numerical consistency and readability. Zero freed
  34139. + * eroute memory.
  34140. + *
  34141. + * Revision 1.4 1998/04/21 21:28:58 rgb
  34142. + * Rearrange debug switches to change on the fly debug output from user
  34143. + * space. Only kernel changes checked in at this time. radij.c was also
  34144. + * changed to temporarily remove buggy debugging code in rj_delete causing
  34145. + * an OOPS and hence, netlink device open errors.
  34146. + *
  34147. + * Revision 1.3 1998/04/14 17:30:39 rgb
  34148. + * Fix up compiling errors for radij tree memory reclamation.
  34149. + *
  34150. + * Revision 1.2 1998/04/12 22:03:23 rgb
  34151. + * Updated ESP-3DES-HMAC-MD5-96,
  34152. + * ESP-DES-HMAC-MD5-96,
  34153. + * AH-HMAC-MD5-96,
  34154. + * AH-HMAC-SHA1-96 since Henry started freeswan cvs repository
  34155. + * from old standards (RFC182[5-9] to new (as of March 1998) drafts.
  34156. + *
  34157. + * Fixed eroute references in /proc/net/ipsec*.
  34158. + *
  34159. + * Started to patch module unloading memory leaks in ipsec_netlink and
  34160. + * radij tree unloading.
  34161. + *
  34162. + * Revision 1.1 1998/04/09 03:06:10 henry
  34163. + * sources moved up from linux/net/ipsec
  34164. + *
  34165. + * Revision 1.1.1.1 1998/04/08 05:35:03 henry
  34166. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  34167. + *
  34168. + * Revision 0.4 1997/01/15 01:28:15 ji
  34169. + * No changes.
  34170. + *
  34171. + * Revision 0.3 1996/11/20 14:39:04 ji
  34172. + * Minor cleanups.
  34173. + * Rationalized debugging code.
  34174. + *
  34175. + * Revision 0.2 1996/11/02 00:18:33 ji
  34176. + * First limited release.
  34177. + *
  34178. + *
  34179. + */
  34180. --- /dev/null Tue Mar 11 13:02:56 2003
  34181. +++ linux/net/ipsec/ipsec_rcv.c Mon Feb 9 13:51:03 2004
  34182. @@ -0,0 +1,2317 @@
  34183. +/*
  34184. + * receive code
  34185. + * Copyright (C) 1996, 1997 John Ioannidis.
  34186. + * Copyright (C) 1998-2003 Richard Guy Briggs.
  34187. + * Copyright (C) 2004 Michael Richardson <mcr@xelerance.com>
  34188. + *
  34189. + * This program is free software; you can redistribute it and/or modify it
  34190. + * under the terms of the GNU General Public License as published by the
  34191. + * Free Software Foundation; either version 2 of the License, or (at your
  34192. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  34193. + *
  34194. + * This program is distributed in the hope that it will be useful, but
  34195. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  34196. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  34197. + * for more details.
  34198. + */
  34199. +
  34200. +char ipsec_rcv_c_version[] = "RCSID $Id: ipsec_rcv.c,v 1.171.2.11 2007/04/28 20:46:40 paul Exp $";
  34201. +
  34202. +#ifndef AUTOCONF_INCLUDED
  34203. +#include <linux/config.h>
  34204. +#endif
  34205. +#include <linux/version.h>
  34206. +
  34207. +#define __NO_VERSION__
  34208. +#include <linux/module.h>
  34209. +#include <linux/kernel.h> /* printk() */
  34210. +
  34211. +#include "openswan/ipsec_param.h"
  34212. +
  34213. +#ifdef MALLOC_SLAB
  34214. +# include <linux/slab.h> /* kmalloc() */
  34215. +#else /* MALLOC_SLAB */
  34216. +# include <linux/malloc.h> /* kmalloc() */
  34217. +#endif /* MALLOC_SLAB */
  34218. +#include <linux/errno.h> /* error codes */
  34219. +#include <linux/types.h> /* size_t */
  34220. +#include <linux/interrupt.h> /* mark_bh */
  34221. +
  34222. +#include <linux/netdevice.h> /* struct device, and other headers */
  34223. +#include <linux/etherdevice.h> /* eth_type_trans */
  34224. +#include <linux/ip.h> /* struct iphdr */
  34225. +
  34226. +#include <net/tcp.h>
  34227. +#include <net/udp.h>
  34228. +#include <linux/skbuff.h>
  34229. +#include <openswan.h>
  34230. +#ifdef SPINLOCK
  34231. +# ifdef SPINLOCK_23
  34232. +# include <linux/spinlock.h> /* *lock* */
  34233. +# else /* SPINLOCK_23 */
  34234. +# include <asm/spinlock.h> /* *lock* */
  34235. +# endif /* SPINLOCK_23 */
  34236. +#endif /* SPINLOCK */
  34237. +
  34238. +#include <net/ip.h>
  34239. +
  34240. +#include "openswan/ipsec_kern24.h"
  34241. +#include "openswan/radij.h"
  34242. +#include "openswan/ipsec_encap.h"
  34243. +#include "openswan/ipsec_sa.h"
  34244. +
  34245. +#include "openswan/ipsec_radij.h"
  34246. +#include "openswan/ipsec_xform.h"
  34247. +#include "openswan/ipsec_tunnel.h"
  34248. +#include "openswan/ipsec_rcv.h"
  34249. +
  34250. +#include "openswan/ipsec_auth.h"
  34251. +
  34252. +#include "openswan/ipsec_esp.h"
  34253. +
  34254. +#ifdef CONFIG_KLIPS_AH
  34255. +#include "openswan/ipsec_ah.h"
  34256. +#endif /* CONFIG_KLIPS_AH */
  34257. +
  34258. +#ifdef CONFIG_KLIPS_IPCOMP
  34259. +#include "openswan/ipsec_ipcomp.h"
  34260. +#endif /* CONFIG_KLIPS_COMP */
  34261. +
  34262. +#include <pfkeyv2.h>
  34263. +#include <pfkey.h>
  34264. +
  34265. +#include "openswan/ipsec_proto.h"
  34266. +#include "openswan/ipsec_alg.h"
  34267. +#include "openswan/ipsec_kern24.h"
  34268. +
  34269. +#ifdef CONFIG_KLIPS_DEBUG
  34270. +int debug_rcv = 0;
  34271. +#endif /* CONFIG_KLIPS_DEBUG */
  34272. +
  34273. +int sysctl_ipsec_inbound_policy_check = 1;
  34274. +
  34275. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  34276. +#include <linux/udp.h>
  34277. +#endif
  34278. +
  34279. +/* This is a private use protocol, and AT&T should be ashamed. They should have
  34280. + * used protocol # 59, which is "no next header" instead of 0xFE.
  34281. + */
  34282. +#ifndef IPPROTO_ATT_HEARTBEAT
  34283. +#define IPPROTO_ATT_HEARTBEAT 0xFE
  34284. +#endif
  34285. +
  34286. +/*
  34287. + * Check-replay-window routine, adapted from the original
  34288. + * by J. Hughes, from draft-ietf-ipsec-esp-des-md5-03.txt
  34289. + *
  34290. + * This is a routine that implements a 64 packet window. This is intend-
  34291. + * ed on being an implementation sample.
  34292. + */
  34293. +
  34294. +DEBUG_NO_STATIC int
  34295. +ipsec_checkreplaywindow(struct ipsec_sa*ipsp, __u32 seq)
  34296. +{
  34297. + __u32 diff;
  34298. +
  34299. + if (ipsp->ips_replaywin == 0) /* replay shut off */
  34300. + return 1;
  34301. + if (seq == 0)
  34302. + return 0; /* first == 0 or wrapped */
  34303. +
  34304. + /* new larger sequence number */
  34305. + if (seq > ipsp->ips_replaywin_lastseq) {
  34306. + return 1; /* larger is good */
  34307. + }
  34308. + diff = ipsp->ips_replaywin_lastseq - seq;
  34309. +
  34310. + /* too old or wrapped */ /* if wrapped, kill off SA? */
  34311. + if (diff >= ipsp->ips_replaywin) {
  34312. + return 0;
  34313. + }
  34314. + /* this packet already seen */
  34315. + if (ipsp->ips_replaywin_bitmap & (1 << diff))
  34316. + return 0;
  34317. + return 1; /* out of order but good */
  34318. +}
  34319. +
  34320. +DEBUG_NO_STATIC int
  34321. +ipsec_updatereplaywindow(struct ipsec_sa*ipsp, __u32 seq)
  34322. +{
  34323. + __u32 diff;
  34324. +
  34325. + if (ipsp->ips_replaywin == 0) /* replay shut off */
  34326. + return 1;
  34327. + if (seq == 0)
  34328. + return 0; /* first == 0 or wrapped */
  34329. +
  34330. + /* new larger sequence number */
  34331. + if (seq > ipsp->ips_replaywin_lastseq) {
  34332. + diff = seq - ipsp->ips_replaywin_lastseq;
  34333. +
  34334. + /* In win, set bit for this pkt */
  34335. + if (diff < ipsp->ips_replaywin)
  34336. + ipsp->ips_replaywin_bitmap =
  34337. + (ipsp->ips_replaywin_bitmap << diff) | 1;
  34338. + else
  34339. + /* This packet has way larger seq num */
  34340. + ipsp->ips_replaywin_bitmap = 1;
  34341. +
  34342. + if(seq - ipsp->ips_replaywin_lastseq - 1 > ipsp->ips_replaywin_maxdiff) {
  34343. + ipsp->ips_replaywin_maxdiff = seq - ipsp->ips_replaywin_lastseq - 1;
  34344. + }
  34345. + ipsp->ips_replaywin_lastseq = seq;
  34346. + return 1; /* larger is good */
  34347. + }
  34348. + diff = ipsp->ips_replaywin_lastseq - seq;
  34349. +
  34350. + /* too old or wrapped */ /* if wrapped, kill off SA? */
  34351. + if (diff >= ipsp->ips_replaywin) {
  34352. +/*
  34353. + if(seq < 0.25*max && ipsp->ips_replaywin_lastseq > 0.75*max) {
  34354. + ipsec_sa_delchain(ipsp);
  34355. + }
  34356. +*/
  34357. + return 0;
  34358. + }
  34359. + /* this packet already seen */
  34360. + if (ipsp->ips_replaywin_bitmap & (1 << diff))
  34361. + return 0;
  34362. + ipsp->ips_replaywin_bitmap |= (1 << diff); /* mark as seen */
  34363. + return 1; /* out of order but good */
  34364. +}
  34365. +
  34366. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  34367. +struct auth_alg ipsec_rcv_md5[]={
  34368. + {osMD5Init, osMD5Update, osMD5Final, AHMD596_ALEN}
  34369. +};
  34370. +
  34371. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  34372. +
  34373. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  34374. +struct auth_alg ipsec_rcv_sha1[]={
  34375. + {SHA1Init, SHA1Update, SHA1Final, AHSHA196_ALEN}
  34376. +};
  34377. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  34378. +
  34379. +/*
  34380. + * decapsulate a single layer of the system
  34381. + *
  34382. + * the following things should be setup to enter this function.
  34383. + *
  34384. + * irs->stats == stats structure (or NULL)
  34385. + * irs->ipp = IP header.
  34386. + * irs->len = total length of packet
  34387. + * skb->nh.iph = ipp;
  34388. + * skb->h.raw = start of payload
  34389. + * irs->ipsp = NULL.
  34390. + * irs->iphlen = N/A = is recalculated.
  34391. + * irs->ilen = 0;
  34392. + * irs->authlen = 0;
  34393. + * irs->authfuncs = NULL;
  34394. + * irs->skb = the skb;
  34395. + *
  34396. + * proto_funcs should be from ipsec_esp.c, ipsec_ah.c or ipsec_ipcomp.c.
  34397. + *
  34398. + */
  34399. +enum ipsec_rcv_value
  34400. +ipsec_rcv_decap_once(struct ipsec_rcv_state *irs
  34401. + , struct xform_functions *proto_funcs)
  34402. +{
  34403. + int iphlen;
  34404. + __u8 proto;
  34405. + struct in_addr ipsaddr;
  34406. + struct in_addr ipdaddr;
  34407. + int replay = 0; /* replay value in AH or ESP packet */
  34408. + struct ipsec_sa* ipsnext = NULL; /* next SA towards inside of packet */
  34409. + struct ipsec_sa *newipsp;
  34410. + struct iphdr *ipp;
  34411. + struct sk_buff *skb;
  34412. + struct ipsec_alg_auth *ixt_a=NULL;
  34413. +
  34414. + skb = irs->skb;
  34415. + irs->len = skb->len;
  34416. + ipp = irs->ipp;
  34417. + proto = ipp->protocol;
  34418. + ipsaddr.s_addr = ipp->saddr;
  34419. + addrtoa(ipsaddr, 0, irs->ipsaddr_txt, sizeof(irs->ipsaddr_txt));
  34420. + ipdaddr.s_addr = ipp->daddr;
  34421. + addrtoa(ipdaddr, 0, irs->ipdaddr_txt, sizeof(irs->ipdaddr_txt));
  34422. +
  34423. + iphlen = ipp->ihl << 2;
  34424. + irs->iphlen=iphlen;
  34425. + ipp->check = 0; /* we know the sum is good */
  34426. +
  34427. + KLIPS_PRINT(debug_rcv,
  34428. + "klips_debug:ipsec_rcv_decap_once: "
  34429. + "decap (%d) from %s -> %s\n",
  34430. + proto, irs->ipsaddr_txt, irs->ipdaddr_txt);
  34431. +
  34432. + /*
  34433. + * Find tunnel control block and (indirectly) call the
  34434. + * appropriate tranform routine. The resulting sk_buf
  34435. + * is a valid IP packet ready to go through input processing.
  34436. + */
  34437. +
  34438. + irs->said.dst.u.v4.sin_addr.s_addr = ipp->daddr;
  34439. + irs->said.dst.u.v4.sin_family = AF_INET;
  34440. +
  34441. + /* note: rcv_checks set up the said.spi value, if appropriate */
  34442. + if(proto_funcs->rcv_checks) {
  34443. + enum ipsec_rcv_value retval =
  34444. + (*proto_funcs->rcv_checks)(irs, skb);
  34445. +
  34446. + if(retval < 0) {
  34447. + return retval;
  34448. + }
  34449. + }
  34450. +
  34451. + irs->said.proto = proto;
  34452. + irs->sa_len = satot(&irs->said, 0, irs->sa, sizeof(irs->sa));
  34453. + if(irs->sa_len == 0) {
  34454. + strcpy(irs->sa, "(error)");
  34455. + }
  34456. +
  34457. + newipsp = ipsec_sa_getbyid(&irs->said);
  34458. + if (newipsp == NULL) {
  34459. + KLIPS_PRINT(debug_rcv,
  34460. + "klips_debug:ipsec_rcv: "
  34461. + "no ipsec_sa for SA:%s: incoming packet with no SA dropped\n",
  34462. + irs->sa_len ? irs->sa : " (error)");
  34463. + if(irs->stats) {
  34464. + irs->stats->rx_dropped++;
  34465. + }
  34466. + return IPSEC_RCV_SAIDNOTFOUND;
  34467. + }
  34468. +
  34469. + /* MCR - XXX this is bizarre. ipsec_sa_getbyid returned it, having
  34470. + * incremented the refcount, why in the world would we decrement it
  34471. + * here? */
  34472. + /* ipsec_sa_put(irs->ipsp);*/ /* incomplete */
  34473. +
  34474. + /* If it is in larval state, drop the packet, we cannot process yet. */
  34475. + if(newipsp->ips_state == SADB_SASTATE_LARVAL) {
  34476. + KLIPS_PRINT(debug_rcv,
  34477. + "klips_debug:ipsec_rcv: "
  34478. + "ipsec_sa in larval state, cannot be used yet, dropping packet.\n");
  34479. + if(irs->stats) {
  34480. + irs->stats->rx_dropped++;
  34481. + }
  34482. + ipsec_sa_put(newipsp);
  34483. + return IPSEC_RCV_SAIDNOTLIVE;
  34484. + }
  34485. +
  34486. + if(newipsp->ips_state == SADB_SASTATE_DEAD) {
  34487. + KLIPS_PRINT(debug_rcv,
  34488. + "klips_debug:ipsec_rcv: "
  34489. + "ipsec_sa in dead state, cannot be used any more, dropping packet.\n");
  34490. + if(irs->stats) {
  34491. + irs->stats->rx_dropped++;
  34492. + }
  34493. + ipsec_sa_put(newipsp);
  34494. + return IPSEC_RCV_SAIDNOTLIVE;
  34495. + }
  34496. +
  34497. + if(sysctl_ipsec_inbound_policy_check) {
  34498. + if(irs->ipp->saddr != ((struct sockaddr_in*)(newipsp->ips_addr_s))->sin_addr.s_addr) {
  34499. + KLIPS_PRINT(debug_rcv,
  34500. + "klips_debug:ipsec_rcv: "
  34501. + "SA:%s, src=%s of pkt does not agree with expected SA source address policy.\n",
  34502. + irs->sa_len ? irs->sa : " (error)",
  34503. + irs->ipsaddr_txt);
  34504. + if(irs->stats) {
  34505. + irs->stats->rx_dropped++;
  34506. + }
  34507. + ipsec_sa_put(newipsp);
  34508. + return IPSEC_RCV_FAILEDINBOUND;
  34509. + }
  34510. +
  34511. + KLIPS_PRINT(debug_rcv,
  34512. + "klips_debug:ipsec_rcv: "
  34513. + "SA:%s, src=%s of pkt agrees with expected SA source address policy.\n",
  34514. + irs->sa_len ? irs->sa : " (error)",
  34515. + irs->ipsaddr_txt);
  34516. +
  34517. + /*
  34518. + * at this point, we have looked up a new SA, and we want to make sure that if this
  34519. + * isn't the first SA in the list, that the previous SA actually points at this one.
  34520. + */
  34521. + if(irs->ipsp) {
  34522. + if(irs->ipsp->ips_inext != newipsp) {
  34523. + KLIPS_PRINT(debug_rcv,
  34524. + "klips_debug:ipsec_rcv: "
  34525. + "unexpected SA:%s: does not agree with ips->inext policy, dropped\n",
  34526. + irs->sa_len ? irs->sa : " (error)");
  34527. + if(irs->stats) {
  34528. + irs->stats->rx_dropped++;
  34529. + }
  34530. + ipsec_sa_put(newipsp);
  34531. + return IPSEC_RCV_FAILEDINBOUND;
  34532. + }
  34533. + KLIPS_PRINT(debug_rcv,
  34534. + "klips_debug:ipsec_rcv: "
  34535. + "SA:%s grouping from previous SA is OK.\n",
  34536. + irs->sa_len ? irs->sa : " (error)");
  34537. + } else {
  34538. + KLIPS_PRINT(debug_rcv,
  34539. + "klips_debug:ipsec_rcv: "
  34540. + "SA:%s First SA in group.\n",
  34541. + irs->sa_len ? irs->sa : " (error)");
  34542. + }
  34543. +
  34544. +
  34545. +
  34546. +
  34547. +
  34548. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  34549. + if (proto == IPPROTO_ESP) {
  34550. + KLIPS_PRINT(debug_rcv,
  34551. + "klips_debug:ipsec_rcv: "
  34552. + "natt_type=%u tdbp->ips_natt_type=%u : %s\n",
  34553. + irs->natt_type, newipsp->ips_natt_type,
  34554. + (irs->natt_type==newipsp->ips_natt_type)?"ok":"bad");
  34555. + if (irs->natt_type != newipsp->ips_natt_type) {
  34556. + KLIPS_PRINT(debug_rcv,
  34557. + "klips_debug:ipsec_rcv: "
  34558. + "SA:%s does not agree with expected NAT-T policy.\n",
  34559. + irs->sa_len ? irs->sa : " (error)");
  34560. + if(irs->stats) {
  34561. + irs->stats->rx_dropped++;
  34562. + }
  34563. + ipsec_sa_put(newipsp);
  34564. + return IPSEC_RCV_FAILEDINBOUND;
  34565. + }
  34566. + }
  34567. +#endif
  34568. + }
  34569. +
  34570. + /* okay, SA checks out, so free any previous SA, and record a new one*/
  34571. +
  34572. + if(irs->ipsp) {
  34573. + ipsec_sa_put(irs->ipsp);
  34574. + }
  34575. + irs->ipsp=newipsp;
  34576. +
  34577. + /* note that the outer code will free the irs->ipsp
  34578. + if there is an error */
  34579. +
  34580. +
  34581. + /* now check the lifetimes */
  34582. + if(ipsec_lifetime_check(&irs->ipsp->ips_life.ipl_bytes, "bytes",
  34583. + irs->sa, ipsec_life_countbased, ipsec_incoming,
  34584. + irs->ipsp) == ipsec_life_harddied ||
  34585. + ipsec_lifetime_check(&irs->ipsp->ips_life.ipl_addtime, "addtime",
  34586. + irs->sa, ipsec_life_timebased, ipsec_incoming,
  34587. + irs->ipsp) == ipsec_life_harddied ||
  34588. + ipsec_lifetime_check(&irs->ipsp->ips_life.ipl_addtime, "usetime",
  34589. + irs->sa, ipsec_life_timebased, ipsec_incoming,
  34590. + irs->ipsp) == ipsec_life_harddied ||
  34591. + ipsec_lifetime_check(&irs->ipsp->ips_life.ipl_packets, "packets",
  34592. + irs->sa, ipsec_life_countbased, ipsec_incoming,
  34593. + irs->ipsp) == ipsec_life_harddied) {
  34594. + ipsec_sa_delchain(irs->ipsp);
  34595. + if(irs->stats) {
  34596. + irs->stats->rx_dropped++;
  34597. + }
  34598. +
  34599. + KLIPS_PRINT(debug_rcv,
  34600. + "klips_debug:ipsec_rcv_decap_once: "
  34601. + "decap (%d) failed lifetime check\n",
  34602. + proto);
  34603. +
  34604. + return IPSEC_RCV_LIFETIMEFAILED;
  34605. + }
  34606. +
  34607. +#if 0
  34608. + /*
  34609. + * This is removed for some reasons:
  34610. + * 1) it needs to happen *after* authentication.
  34611. + * 2) do we really care, if it authenticates, if it came
  34612. + * from the wrong location?
  34613. + * 3) the NAT_KA messages in IKE will also get to pluto
  34614. + * and it will figure out that stuff has moved.
  34615. + * 4) the 2.6 udp-esp encap function does not pass us
  34616. + * the originating port number, and I can't tell
  34617. + * if skb->sk is guaranteed to be valid here.
  34618. + * 2005-04-16: mcr@xelerance.com
  34619. + */
  34620. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  34621. + /*
  34622. + *
  34623. + * XXX we should ONLY update pluto if the SA passes all checks,
  34624. + * which we clearly do not now.
  34625. + */
  34626. + if ((irs->natt_type) &&
  34627. + ( (irs->ipp->saddr != (((struct sockaddr_in*)(newipsp->ips_addr_s))->sin_addr.s_addr)) ||
  34628. + (irs->natt_sport != newipsp->ips_natt_sport)
  34629. + )) {
  34630. + struct sockaddr sipaddr;
  34631. + struct sockaddr_in *psin = (struct sockaddr_in*)(newipsp->ips_addr_s);
  34632. +
  34633. + /** Advertise NAT-T addr change to pluto **/
  34634. + sipaddr.sa_family = AF_INET;
  34635. + ((struct sockaddr_in*)&sipaddr)->sin_addr.s_addr = irs->ipp->saddr;
  34636. + ((struct sockaddr_in*)&sipaddr)->sin_port = htons(irs->natt_sport);
  34637. + pfkey_nat_t_new_mapping(newipsp, &sipaddr, irs->natt_sport);
  34638. +
  34639. + /**
  34640. + * Then allow or block packet depending on
  34641. + * sysctl_ipsec_inbound_policy_check.
  34642. + *
  34643. + * In all cases, pluto will update SA if new mapping is
  34644. + * accepted.
  34645. + */
  34646. + if (sysctl_ipsec_inbound_policy_check) {
  34647. + KLIPS_PRINT(debug_rcv,
  34648. + "klips_debug:ipsec_rcv: "
  34649. + "SA:%s, src=%s:%u of pkt does not agree with expected "
  34650. + "SA source address [%08x:%u] (notifying pluto of change).\n",
  34651. + irs->sa_len ? irs->sa : " (error)",
  34652. + irs->ipsaddr_txt, irs->natt_sport,
  34653. + psin->sin_addr.s_addr,
  34654. + newipsp->ips_natt_sport);
  34655. + if(irs->stats) {
  34656. + irs->stats->rx_dropped++;
  34657. + }
  34658. + ipsec_sa_put(newipsp);
  34659. + return IPSEC_RCV_FAILEDINBOUND;
  34660. + }
  34661. + }
  34662. +#endif
  34663. +#endif
  34664. +
  34665. + irs->authfuncs=NULL;
  34666. +
  34667. + /* authenticate, if required */
  34668. + if ((ixt_a=irs->ipsp->ips_alg_auth)) {
  34669. + irs->authlen = AHHMAC_HASHLEN;
  34670. + irs->authfuncs = NULL;
  34671. + irs->ictx = NULL;
  34672. + irs->octx = NULL;
  34673. + irs->ictx_len = 0;
  34674. + irs->octx_len = 0;
  34675. + KLIPS_PRINT(debug_rcv,
  34676. + "klips_debug:ipsec_rcv: "
  34677. + "authalg=%d authlen=%d\n",
  34678. + irs->ipsp->ips_authalg,
  34679. + irs->authlen);
  34680. + } else
  34681. + switch(irs->ipsp->ips_authalg) {
  34682. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  34683. + case AH_MD5:
  34684. + irs->authlen = AHHMAC_HASHLEN;
  34685. + irs->authfuncs = ipsec_rcv_md5;
  34686. + irs->ictx = (void *)&((struct md5_ctx*)(irs->ipsp->ips_key_a))->ictx;
  34687. + irs->octx = (void *)&((struct md5_ctx*)(irs->ipsp->ips_key_a))->octx;
  34688. + irs->ictx_len = sizeof(((struct md5_ctx*)(irs->ipsp->ips_key_a))->ictx);
  34689. + irs->octx_len = sizeof(((struct md5_ctx*)(irs->ipsp->ips_key_a))->octx);
  34690. + break;
  34691. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  34692. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  34693. + case AH_SHA:
  34694. + irs->authlen = AHHMAC_HASHLEN;
  34695. + irs->authfuncs = ipsec_rcv_sha1;
  34696. + irs->ictx = (void *)&((struct sha1_ctx*)(irs->ipsp->ips_key_a))->ictx;
  34697. + irs->octx = (void *)&((struct sha1_ctx*)(irs->ipsp->ips_key_a))->octx;
  34698. + irs->ictx_len = sizeof(((struct sha1_ctx*)(irs->ipsp->ips_key_a))->ictx);
  34699. + irs->octx_len = sizeof(((struct sha1_ctx*)(irs->ipsp->ips_key_a))->octx);
  34700. + break;
  34701. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  34702. + case AH_NONE:
  34703. + irs->authlen = 0;
  34704. + irs->authfuncs = NULL;
  34705. + irs->ictx = NULL;
  34706. + irs->octx = NULL;
  34707. + irs->ictx_len = 0;
  34708. + irs->octx_len = 0;
  34709. + break;
  34710. + default:
  34711. + irs->ipsp->ips_errs.ips_alg_errs += 1;
  34712. + if(irs->stats) {
  34713. + irs->stats->rx_errors++;
  34714. + }
  34715. + return IPSEC_RCV_BADAUTH;
  34716. + }
  34717. +
  34718. + /* ilen counts number of bytes in ESP portion */
  34719. + irs->ilen = ((skb->data + skb->len) - skb->h.raw) - irs->authlen;
  34720. + if(irs->ilen <= 0) {
  34721. + KLIPS_PRINT(debug_rcv,
  34722. + "klips_debug:ipsec_rcv: "
  34723. + "runt %s packet with no data, dropping.\n",
  34724. + (proto == IPPROTO_ESP ? "esp" : "ah"));
  34725. + if(irs->stats) {
  34726. + irs->stats->rx_dropped++;
  34727. + }
  34728. + return IPSEC_RCV_BADLEN;
  34729. + }
  34730. +
  34731. + if(irs->authfuncs || ixt_a) {
  34732. + unsigned char *authenticator = NULL;
  34733. +
  34734. + if(proto_funcs->rcv_setup_auth) {
  34735. + enum ipsec_rcv_value retval
  34736. + = (*proto_funcs->rcv_setup_auth)(irs, skb,
  34737. + &replay,
  34738. + &authenticator);
  34739. + if(retval < 0) {
  34740. + return retval;
  34741. + }
  34742. + }
  34743. +
  34744. + if(!authenticator) {
  34745. + irs->ipsp->ips_errs.ips_auth_errs += 1;
  34746. + if(irs->stats) {
  34747. + irs->stats->rx_dropped++;
  34748. + }
  34749. + return IPSEC_RCV_BADAUTH;
  34750. + }
  34751. +
  34752. + if(!ipsec_checkreplaywindow(irs->ipsp, replay)) {
  34753. + irs->ipsp->ips_errs.ips_replaywin_errs += 1;
  34754. + KLIPS_PRINT(debug_rcv & DB_RX_REPLAY,
  34755. + "klips_debug:ipsec_rcv: "
  34756. + "duplicate frame from %s, packet dropped\n",
  34757. + irs->ipsaddr_txt);
  34758. + if(irs->stats) {
  34759. + irs->stats->rx_dropped++;
  34760. + }
  34761. + return IPSEC_RCV_REPLAYFAILED;
  34762. + }
  34763. +
  34764. + /*
  34765. + * verify authenticator
  34766. + */
  34767. +
  34768. + KLIPS_PRINT(debug_rcv,
  34769. + "klips_debug:ipsec_rcv: "
  34770. + "encalg = %d, authalg = %d.\n",
  34771. + irs->ipsp->ips_encalg,
  34772. + irs->ipsp->ips_authalg);
  34773. +
  34774. + /* calculate authenticator */
  34775. + if(proto_funcs->rcv_calc_auth == NULL) {
  34776. + return IPSEC_RCV_BADAUTH;
  34777. + }
  34778. + (*proto_funcs->rcv_calc_auth)(irs, skb);
  34779. +
  34780. + if (memcmp(irs->hash, authenticator, irs->authlen)) {
  34781. + irs->ipsp->ips_errs.ips_auth_errs += 1;
  34782. + KLIPS_PRINT(debug_rcv & DB_RX_INAU,
  34783. + "klips_debug:ipsec_rcv: "
  34784. + "auth failed on incoming packet from %s: hash=%08x%08x%08x auth=%08x%08x%08x, dropped\n",
  34785. + irs->ipsaddr_txt,
  34786. + ntohl(*(__u32*)&irs->hash[0]),
  34787. + ntohl(*(__u32*)&irs->hash[4]),
  34788. + ntohl(*(__u32*)&irs->hash[8]),
  34789. + ntohl(*(__u32*)authenticator),
  34790. + ntohl(*((__u32*)authenticator + 1)),
  34791. + ntohl(*((__u32*)authenticator + 2)));
  34792. + if(irs->stats) {
  34793. + irs->stats->rx_dropped++;
  34794. + }
  34795. + return IPSEC_RCV_AUTHFAILED;
  34796. + } else {
  34797. + KLIPS_PRINT(debug_rcv,
  34798. + "klips_debug:ipsec_rcv: "
  34799. + "authentication successful.\n");
  34800. + }
  34801. +
  34802. + /* Crypto hygiene: clear memory used to calculate autheticator.
  34803. + * The length varies with the algorithm.
  34804. + */
  34805. + memset(irs->hash, 0, irs->authlen);
  34806. +
  34807. + /* If the sequence number == 0, expire SA, it had rolled */
  34808. + if(irs->ipsp->ips_replaywin && !replay /* !irs->ipsp->ips_replaywin_lastseq */) {
  34809. + ipsec_sa_delchain(irs->ipsp);
  34810. + KLIPS_PRINT(debug_rcv,
  34811. + "klips_debug:ipsec_rcv: "
  34812. + "replay window counter rolled, expiring SA.\n");
  34813. + if(irs->stats) {
  34814. + irs->stats->rx_dropped++;
  34815. + }
  34816. + return IPSEC_RCV_REPLAYROLLED;
  34817. + }
  34818. +
  34819. + /* now update the replay counter */
  34820. + if (!ipsec_updatereplaywindow(irs->ipsp, replay)) {
  34821. + irs->ipsp->ips_errs.ips_replaywin_errs += 1;
  34822. + KLIPS_PRINT(debug_rcv & DB_RX_REPLAY,
  34823. + "klips_debug:ipsec_rcv: "
  34824. + "duplicate frame from %s, packet dropped\n",
  34825. + irs->ipsaddr_txt);
  34826. + if(irs->stats) {
  34827. + irs->stats->rx_dropped++;
  34828. + }
  34829. + return IPSEC_RCV_REPLAYROLLED;
  34830. + }
  34831. + }
  34832. +
  34833. + if(proto_funcs->rcv_decrypt) {
  34834. + enum ipsec_rcv_value retval =
  34835. + (*proto_funcs->rcv_decrypt)(irs);
  34836. +
  34837. + if(retval != IPSEC_RCV_OK) {
  34838. + return retval;
  34839. + }
  34840. + }
  34841. +
  34842. + /*
  34843. + * Adjust pointers
  34844. + */
  34845. + skb = irs->skb;
  34846. + irs->len = skb->len;
  34847. + ipp = irs->ipp = skb->nh.iph;
  34848. + iphlen = ipp->ihl<<2;
  34849. + skb->h.raw = skb->nh.raw + iphlen;
  34850. +
  34851. + /* zero any options that there might be */
  34852. + memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
  34853. +
  34854. + ipsaddr.s_addr = ipp->saddr;
  34855. + addrtoa(ipsaddr, 0, irs->ipsaddr_txt, sizeof(irs->ipsaddr_txt));
  34856. + ipdaddr.s_addr = ipp->daddr;
  34857. + addrtoa(ipdaddr, 0, irs->ipdaddr_txt, sizeof(irs->ipdaddr_txt));
  34858. +
  34859. + /*
  34860. + * Discard the original ESP/AH header
  34861. + */
  34862. + ipp->protocol = irs->next_header;
  34863. +
  34864. + ipp->check = 0; /* NOTE: this will be included in checksum */
  34865. + ipp->check = ip_fast_csum((unsigned char *)skb->nh.iph, iphlen >> 2);
  34866. +
  34867. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  34868. + "klips_debug:ipsec_rcv: "
  34869. + "after <%s%s%s>, SA:%s:\n",
  34870. + IPS_XFORM_NAME(irs->ipsp),
  34871. + irs->sa_len ? irs->sa : " (error)");
  34872. + KLIPS_IP_PRINT(debug_rcv & DB_RX_PKTRX, ipp);
  34873. +
  34874. + skb->protocol = htons(ETH_P_IP);
  34875. + skb->ip_summed = 0;
  34876. +
  34877. + ipsnext = irs->ipsp->ips_inext;
  34878. + if(sysctl_ipsec_inbound_policy_check) {
  34879. + if(ipsnext) {
  34880. + if(
  34881. + ipp->protocol != IPPROTO_AH
  34882. + && ipp->protocol != IPPROTO_ESP
  34883. +#ifdef CONFIG_KLIPS_IPCOMP
  34884. + && ipp->protocol != IPPROTO_COMP
  34885. + && (ipsnext->ips_said.proto != IPPROTO_COMP
  34886. + || ipsnext->ips_inext)
  34887. +#endif /* CONFIG_KLIPS_IPCOMP */
  34888. + && ipp->protocol != IPPROTO_IPIP
  34889. + && ipp->protocol != IPPROTO_ATT_HEARTBEAT /* heartbeats to AT&T SIG/GIG */
  34890. + ) {
  34891. + KLIPS_PRINT(debug_rcv,
  34892. + "klips_debug:ipsec_rcv: "
  34893. + "packet with incomplete policy dropped, last successful SA:%s.\n",
  34894. + irs->sa_len ? irs->sa : " (error)");
  34895. + if(irs->stats) {
  34896. + irs->stats->rx_dropped++;
  34897. + }
  34898. + return IPSEC_RCV_FAILEDINBOUND;
  34899. + }
  34900. + KLIPS_PRINT(debug_rcv,
  34901. + "klips_debug:ipsec_rcv: "
  34902. + "SA:%s, Another IPSEC header to process.\n",
  34903. + irs->sa_len ? irs->sa : " (error)");
  34904. + } else {
  34905. + KLIPS_PRINT(debug_rcv,
  34906. + "klips_debug:ipsec_rcv: "
  34907. + "No ips_inext from this SA:%s.\n",
  34908. + irs->sa_len ? irs->sa : " (error)");
  34909. + }
  34910. + }
  34911. +
  34912. +#ifdef CONFIG_KLIPS_IPCOMP
  34913. + /* update ipcomp ratio counters, even if no ipcomp packet is present */
  34914. + if (ipsnext
  34915. + && ipsnext->ips_said.proto == IPPROTO_COMP
  34916. + && ipp->protocol != IPPROTO_COMP) {
  34917. + ipsnext->ips_comp_ratio_cbytes += ntohs(ipp->tot_len);
  34918. + ipsnext->ips_comp_ratio_dbytes += ntohs(ipp->tot_len);
  34919. + }
  34920. +#endif /* CONFIG_KLIPS_IPCOMP */
  34921. +
  34922. + irs->ipsp->ips_life.ipl_bytes.ipl_count += irs->len;
  34923. + irs->ipsp->ips_life.ipl_bytes.ipl_last = irs->len;
  34924. +
  34925. + if(!irs->ipsp->ips_life.ipl_usetime.ipl_count) {
  34926. + irs->ipsp->ips_life.ipl_usetime.ipl_count = jiffies / HZ;
  34927. + }
  34928. + irs->ipsp->ips_life.ipl_usetime.ipl_last = jiffies / HZ;
  34929. + irs->ipsp->ips_life.ipl_packets.ipl_count += 1;
  34930. +
  34931. +#ifdef CONFIG_NETFILTER
  34932. + if(proto == IPPROTO_ESP || proto == IPPROTO_AH) {
  34933. + skb->nfmark = (skb->nfmark & (~(IPsecSAref2NFmark(IPSEC_SA_REF_MASK))))
  34934. + | IPsecSAref2NFmark(IPsecSA2SAref(irs->ipsp));
  34935. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  34936. + "klips_debug:ipsec_rcv: "
  34937. + "%s SA sets skb->nfmark=0x%x.\n",
  34938. + proto == IPPROTO_ESP ? "ESP" : "AH",
  34939. + (unsigned)skb->nfmark);
  34940. + }
  34941. +#endif /* CONFIG_NETFILTER */
  34942. +
  34943. + return IPSEC_RCV_OK;
  34944. +}
  34945. +
  34946. +
  34947. +/*
  34948. + * core decapsulation loop for all protocols.
  34949. + *
  34950. + * the following things should be setup to enter this function.
  34951. + *
  34952. + * irs->stats == stats structure (or NULL)
  34953. + * irs->ipp = IP header.
  34954. + * irs->ipsp = NULL.
  34955. + * irs->ilen = 0;
  34956. + * irs->authlen = 0;
  34957. + * irs->authfuncs = NULL;
  34958. + * irs->skb = skb;
  34959. + * skb->nh.iph = ipp;
  34960. + * skb->h.raw = start of payload
  34961. + *
  34962. + */
  34963. +int ipsec_rcv_decap(struct ipsec_rcv_state *irs)
  34964. +{
  34965. + struct ipsec_sa *ipsp = NULL;
  34966. + struct ipsec_sa* ipsnext = NULL;
  34967. + struct in_addr ipsaddr;
  34968. + struct in_addr ipdaddr;
  34969. + struct iphdr *ipp;
  34970. + struct sk_buff *skb = NULL;
  34971. +
  34972. + /* begin decapsulating loop here */
  34973. +
  34974. + /*
  34975. + The spinlock is to prevent any other process from
  34976. + accessing or deleting the ipsec_sa hash table or any of the
  34977. + ipsec_sa s while we are using and updating them.
  34978. +
  34979. + This is not optimal, but was relatively straightforward
  34980. + at the time. A better way to do it has been planned for
  34981. + more than a year, to lock the hash table and put reference
  34982. + counts on each ipsec_sa instead. This is not likely to happen
  34983. + in KLIPS1 unless a volunteer contributes it, but will be
  34984. + designed into KLIPS2.
  34985. + */
  34986. + spin_lock(&tdb_lock);
  34987. +
  34988. + do {
  34989. + int decap_stat;
  34990. + struct xform_functions *proto_funcs;
  34991. +
  34992. + switch(irs->ipp->protocol) {
  34993. + case IPPROTO_ESP:
  34994. + proto_funcs = esp_xform_funcs;
  34995. + break;
  34996. +
  34997. +#ifdef CONFIG_KLIPS_AH
  34998. + case IPPROTO_AH:
  34999. + proto_funcs = ah_xform_funcs;
  35000. + break;
  35001. +#endif /* !CONFIG_KLIPS_AH */
  35002. +
  35003. +#ifdef CONFIG_KLIPS_IPCOMP
  35004. + case IPPROTO_COMP:
  35005. + proto_funcs = ipcomp_xform_funcs;
  35006. + break;
  35007. +#endif /* !CONFIG_KLIPS_IPCOMP */
  35008. + default:
  35009. + if(irs->stats) {
  35010. + irs->stats->rx_errors++;
  35011. + }
  35012. + decap_stat = IPSEC_RCV_BADPROTO;
  35013. + goto rcvleave;
  35014. + }
  35015. +
  35016. + decap_stat = ipsec_rcv_decap_once(irs, proto_funcs);
  35017. +
  35018. + if(decap_stat != IPSEC_RCV_OK) {
  35019. + spin_unlock(&tdb_lock);
  35020. + KLIPS_PRINT(debug_rcv,
  35021. + "klips_debug:ipsec_rcv: decap_once failed: %d\n",
  35022. + decap_stat);
  35023. +
  35024. + goto rcvleave;
  35025. + }
  35026. + /* end decapsulation loop here */
  35027. + } while( (irs->ipp->protocol == IPPROTO_ESP )
  35028. + || (irs->ipp->protocol == IPPROTO_AH )
  35029. +#ifdef CONFIG_KLIPS_IPCOMP
  35030. + || (irs->ipp->protocol == IPPROTO_COMP)
  35031. +#endif /* CONFIG_KLIPS_IPCOMP */
  35032. + );
  35033. +
  35034. + /* set up for decap loop */
  35035. + ipp =irs->ipp;
  35036. + ipsp =irs->ipsp;
  35037. + ipsnext = ipsp->ips_inext;
  35038. + skb = irs->skb;
  35039. +
  35040. + /* if there is an IPCOMP, but we don't have an IPPROTO_COMP,
  35041. + * then we can just skip it
  35042. + */
  35043. +#ifdef CONFIG_KLIPS_IPCOMP
  35044. + if(ipsnext && ipsnext->ips_said.proto == IPPROTO_COMP) {
  35045. + ipsp = ipsnext;
  35046. + ipsnext = ipsp->ips_inext;
  35047. + }
  35048. +#endif /* CONFIG_KLIPS_IPCOMP */
  35049. +
  35050. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  35051. + if ((irs->natt_type) && (ipp->protocol != IPPROTO_IPIP)) {
  35052. + /**
  35053. + * NAT-Traversal and Transport Mode:
  35054. + * we need to correct TCP/UDP checksum
  35055. + *
  35056. + * If we've got NAT-OA, we can fix checksum without recalculation.
  35057. + */
  35058. + __u32 natt_oa = ipsp->ips_natt_oa ?
  35059. + ((struct sockaddr_in*)(ipsp->ips_natt_oa))->sin_addr.s_addr : 0;
  35060. + __u16 pkt_len = skb->tail - (unsigned char *)ipp;
  35061. + __u16 data_len = pkt_len - (ipp->ihl << 2);
  35062. +
  35063. + switch (ipp->protocol) {
  35064. + case IPPROTO_TCP:
  35065. + if (data_len >= sizeof(struct tcphdr)) {
  35066. + struct tcphdr *tcp = skb->h.th;
  35067. + if (natt_oa) {
  35068. + __u32 buff[2] = { ~natt_oa, ipp->saddr };
  35069. + KLIPS_PRINT(debug_rcv,
  35070. + "klips_debug:ipsec_rcv: "
  35071. + "NAT-T & TRANSPORT: "
  35072. + "fix TCP checksum using NAT-OA\n");
  35073. + tcp->check = csum_fold(
  35074. + csum_partial((unsigned char *)buff, sizeof(buff),
  35075. + tcp->check^0xffff));
  35076. + }
  35077. + else {
  35078. + KLIPS_PRINT(debug_rcv,
  35079. + "klips_debug:ipsec_rcv: "
  35080. + "NAT-T & TRANSPORT: recalc TCP checksum\n");
  35081. + if (pkt_len > (ntohs(ipp->tot_len)))
  35082. + data_len -= (pkt_len - ntohs(ipp->tot_len));
  35083. + tcp->check = 0;
  35084. + tcp->check = csum_tcpudp_magic(ipp->saddr, ipp->daddr,
  35085. + data_len, IPPROTO_TCP,
  35086. + csum_partial((unsigned char *)tcp, data_len, 0));
  35087. + }
  35088. + }
  35089. + else {
  35090. + KLIPS_PRINT(debug_rcv,
  35091. + "klips_debug:ipsec_rcv: "
  35092. + "NAT-T & TRANSPORT: can't fix TCP checksum\n");
  35093. + }
  35094. + break;
  35095. + case IPPROTO_UDP:
  35096. + if (data_len >= sizeof(struct udphdr)) {
  35097. + struct udphdr *udp = skb->h.uh;
  35098. + if (udp->check == 0) {
  35099. + KLIPS_PRINT(debug_rcv,
  35100. + "klips_debug:ipsec_rcv: "
  35101. + "NAT-T & TRANSPORT: UDP checksum already 0\n");
  35102. + }
  35103. + else if (natt_oa) {
  35104. + __u32 buff[2] = { ~natt_oa, ipp->saddr };
  35105. + KLIPS_PRINT(debug_rcv,
  35106. + "klips_debug:ipsec_rcv: "
  35107. + "NAT-T & TRANSPORT: "
  35108. + "fix UDP checksum using NAT-OA\n");
  35109. +#ifdef DISABLE_UDP_CHECKSUM
  35110. + udp->check=0
  35111. + KLIPS_PRINT(debug_rcv,
  35112. + "klips_debug:ipsec_rcv: "
  35113. + "NAT-T & TRANSPORT: "
  35114. + "UDP checksum using NAT-OA disabled at compile time\n");
  35115. +#else
  35116. + udp->check = csum_fold(
  35117. + csum_partial((unsigned char *)buff, sizeof(buff),
  35118. + udp->check^0xffff));
  35119. +#endif
  35120. + }
  35121. + else {
  35122. + KLIPS_PRINT(debug_rcv,
  35123. + "klips_debug:ipsec_rcv: "
  35124. + "NAT-T & TRANSPORT: zero UDP checksum\n");
  35125. + udp->check = 0;
  35126. + }
  35127. + }
  35128. + else {
  35129. + KLIPS_PRINT(debug_rcv,
  35130. + "klips_debug:ipsec_rcv: "
  35131. + "NAT-T & TRANSPORT: can't fix UDP checksum\n");
  35132. + }
  35133. + break;
  35134. + default:
  35135. + KLIPS_PRINT(debug_rcv,
  35136. + "klips_debug:ipsec_rcv: "
  35137. + "NAT-T & TRANSPORT: non TCP/UDP packet -- do nothing\n");
  35138. + break;
  35139. + }
  35140. + }
  35141. +#endif
  35142. +
  35143. + /*
  35144. + * XXX this needs to be locked from when it was first looked
  35145. + * up in the decapsulation loop. Perhaps it is better to put
  35146. + * the IPIP decap inside the loop.
  35147. + */
  35148. + if(ipsnext) {
  35149. + ipsp = ipsnext;
  35150. + irs->sa_len = satot(&irs->said, 0, irs->sa, sizeof(irs->sa));
  35151. + if((ipp->protocol != IPPROTO_IPIP) &&
  35152. + (ipp->protocol != IPPROTO_ATT_HEARTBEAT)) { /* AT&T heartbeats to SIG/GIG */
  35153. + spin_unlock(&tdb_lock);
  35154. + KLIPS_PRINT(debug_rcv,
  35155. + "klips_debug:ipsec_rcv: "
  35156. + "SA:%s, Hey! How did this get through? Dropped.\n",
  35157. + irs->sa_len ? irs->sa : " (error)");
  35158. + if(irs->stats) {
  35159. + irs->stats->rx_dropped++;
  35160. + }
  35161. + goto rcvleave;
  35162. + }
  35163. + if(sysctl_ipsec_inbound_policy_check) {
  35164. + struct sockaddr_in *psin = (struct sockaddr_in*)(ipsp->ips_addr_s);
  35165. + if((ipsnext = ipsp->ips_inext)) {
  35166. + char sa2[SATOT_BUF];
  35167. + size_t sa_len2;
  35168. + sa_len2 = satot(&ipsnext->ips_said, 0, sa2, sizeof(sa2));
  35169. + spin_unlock(&tdb_lock);
  35170. + KLIPS_PRINT(debug_rcv,
  35171. + "klips_debug:ipsec_rcv: "
  35172. + "unexpected SA:%s after IPIP SA:%s\n",
  35173. + sa_len2 ? sa2 : " (error)",
  35174. + irs->sa_len ? irs->sa : " (error)");
  35175. + if(irs->stats) {
  35176. + irs->stats->rx_dropped++;
  35177. + }
  35178. + goto rcvleave;
  35179. + }
  35180. + if(ipp->saddr != psin->sin_addr.s_addr) {
  35181. + spin_unlock(&tdb_lock);
  35182. + KLIPS_PRINT(debug_rcv,
  35183. + "klips_debug:ipsec_rcv: "
  35184. + "SA:%s, src=%s(%08x) does match expected 0x%08x.\n",
  35185. + irs->sa_len ? irs->sa : " (error)",
  35186. + irs->ipsaddr_txt,
  35187. + ipp->saddr, psin->sin_addr.s_addr);
  35188. + if(irs->stats) {
  35189. + irs->stats->rx_dropped++;
  35190. + }
  35191. + goto rcvleave;
  35192. + }
  35193. + }
  35194. +
  35195. + if(ipp->protocol == IPPROTO_IPIP) /* added to support AT&T heartbeats to SIG/GIG */
  35196. + {
  35197. + /*
  35198. + * XXX this needs to be locked from when it was first looked
  35199. + * up in the decapsulation loop. Perhaps it is better to put
  35200. + * the IPIP decap inside the loop.
  35201. + */
  35202. + ipsp->ips_life.ipl_bytes.ipl_count += skb->len;
  35203. + ipsp->ips_life.ipl_bytes.ipl_last = skb->len;
  35204. +
  35205. + if(!ipsp->ips_life.ipl_usetime.ipl_count) {
  35206. + ipsp->ips_life.ipl_usetime.ipl_count = jiffies / HZ;
  35207. + }
  35208. + ipsp->ips_life.ipl_usetime.ipl_last = jiffies / HZ;
  35209. + ipsp->ips_life.ipl_packets.ipl_count += 1;
  35210. +
  35211. + if(skb->len < irs->iphlen) {
  35212. + spin_unlock(&tdb_lock);
  35213. + printk(KERN_WARNING "klips_debug:ipsec_rcv: "
  35214. + "tried to skb_pull iphlen=%d, %d available. This should never happen, please report.\n",
  35215. + irs->iphlen,
  35216. + (int)(skb->len));
  35217. +
  35218. + goto rcvleave;
  35219. + }
  35220. +
  35221. + /*
  35222. + * we need to pull up by size of IP header,
  35223. + * options, but also by any UDP/ESP encap there might
  35224. + * have been, and this deals with all cases.
  35225. + */
  35226. + skb_pull(skb, (skb->h.raw - skb->nh.raw));
  35227. +
  35228. + /* new L3 header is where L4 payload was */
  35229. + skb->nh.raw = skb->h.raw;
  35230. +
  35231. + /* now setup new L4 payload location */
  35232. + ipp = (struct iphdr *)skb->nh.raw;
  35233. + skb->h.raw = skb->nh.raw + (ipp->ihl << 2);
  35234. +
  35235. +
  35236. + /* remove any saved options that we might have,
  35237. + * since we have a new IP header.
  35238. + */
  35239. + memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
  35240. +
  35241. +#if 0
  35242. + KLIPS_PRINT(debug_rcv, "csum: %d\n", ip_fast_csum((u8 *)ipp, ipp->ihl));
  35243. +#endif
  35244. +
  35245. + /* re-do any strings for debugging */
  35246. + ipsaddr.s_addr = ipp->saddr;
  35247. + addrtoa(ipsaddr, 0, irs->ipsaddr_txt, sizeof(irs->ipsaddr_txt));
  35248. + ipdaddr.s_addr = ipp->daddr;
  35249. + addrtoa(ipdaddr, 0, irs->ipdaddr_txt, sizeof(irs->ipdaddr_txt));
  35250. +
  35251. + skb->protocol = htons(ETH_P_IP);
  35252. + skb->ip_summed = 0;
  35253. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  35254. + "klips_debug:ipsec_rcv: "
  35255. + "IPIP tunnel stripped.\n");
  35256. + KLIPS_IP_PRINT(debug_rcv & DB_RX_PKTRX, ipp);
  35257. + }
  35258. +
  35259. + if(sysctl_ipsec_inbound_policy_check
  35260. + /*
  35261. + Note: "xor" (^) logically replaces "not equal"
  35262. + (!=) and "bitwise or" (|) logically replaces
  35263. + "boolean or" (||). This is done to speed up
  35264. + execution by doing only bitwise operations and
  35265. + no branch operations
  35266. + */
  35267. + && (((ipp->saddr & ipsp->ips_mask_s.u.v4.sin_addr.s_addr)
  35268. + ^ ipsp->ips_flow_s.u.v4.sin_addr.s_addr)
  35269. + | ((ipp->daddr & ipsp->ips_mask_d.u.v4.sin_addr.s_addr)
  35270. + ^ ipsp->ips_flow_d.u.v4.sin_addr.s_addr)) )
  35271. + {
  35272. + char sflow_txt[SUBNETTOA_BUF], dflow_txt[SUBNETTOA_BUF];
  35273. +
  35274. + subnettoa(ipsp->ips_flow_s.u.v4.sin_addr,
  35275. + ipsp->ips_mask_s.u.v4.sin_addr,
  35276. + 0, sflow_txt, sizeof(sflow_txt));
  35277. + subnettoa(ipsp->ips_flow_d.u.v4.sin_addr,
  35278. + ipsp->ips_mask_d.u.v4.sin_addr,
  35279. + 0, dflow_txt, sizeof(dflow_txt));
  35280. + spin_unlock(&tdb_lock);
  35281. + KLIPS_PRINT(debug_rcv,
  35282. + "klips_debug:ipsec_rcv: "
  35283. + "SA:%s, inner tunnel policy [%s -> %s] does not agree with pkt contents [%s -> %s].\n",
  35284. + irs->sa_len ? irs->sa : " (error)",
  35285. + sflow_txt,
  35286. + dflow_txt,
  35287. + irs->ipsaddr_txt,
  35288. + irs->ipdaddr_txt);
  35289. + if(irs->stats) {
  35290. + irs->stats->rx_dropped++;
  35291. + }
  35292. + goto rcvleave;
  35293. + }
  35294. +#ifdef CONFIG_NETFILTER
  35295. + skb->nfmark = (skb->nfmark & (~(IPsecSAref2NFmark(IPSEC_SA_REF_TABLE_MASK))))
  35296. + | IPsecSAref2NFmark(IPsecSA2SAref(ipsp));
  35297. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  35298. + "klips_debug:ipsec_rcv: "
  35299. + "IPIP SA sets skb->nfmark=0x%x.\n",
  35300. + (unsigned)skb->nfmark);
  35301. +#endif /* CONFIG_NETFILTER */
  35302. + }
  35303. +
  35304. + spin_unlock(&tdb_lock);
  35305. +
  35306. + if(irs->stats) {
  35307. + irs->stats->rx_bytes += skb->len;
  35308. + }
  35309. + if(skb->dst) {
  35310. + dst_release(skb->dst);
  35311. + skb->dst = NULL;
  35312. + }
  35313. + skb->pkt_type = PACKET_HOST;
  35314. + if(irs->hard_header_len &&
  35315. + (skb->mac.raw != (skb->nh.raw - irs->hard_header_len)) &&
  35316. + (irs->hard_header_len <= skb_headroom(skb))) {
  35317. + /* copy back original MAC header */
  35318. + memmove(skb->nh.raw - irs->hard_header_len,
  35319. + skb->mac.raw, irs->hard_header_len);
  35320. + skb->mac.raw = skb->nh.raw - irs->hard_header_len;
  35321. + }
  35322. +
  35323. +#ifdef CONFIG_KLIPS_IPCOMP
  35324. + if(ipp->protocol == IPPROTO_COMP) {
  35325. + unsigned int flags = 0;
  35326. +
  35327. + if(sysctl_ipsec_inbound_policy_check) {
  35328. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  35329. + "klips_debug:ipsec_rcv: "
  35330. + "inbound policy checking enabled, IPCOMP follows IPIP, dropped.\n");
  35331. + if (irs->stats) {
  35332. + irs->stats->rx_errors++;
  35333. + }
  35334. + goto rcvleave;
  35335. + }
  35336. + /*
  35337. + XXX need a ipsec_sa for updating ratio counters but it is not
  35338. + following policy anyways so it is not a priority
  35339. + */
  35340. + skb = skb_decompress(skb, NULL, &flags);
  35341. + if (!skb || flags) {
  35342. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  35343. + "klips_debug:ipsec_rcv: "
  35344. + "skb_decompress() returned error flags: %d, dropped.\n",
  35345. + flags);
  35346. + if (irs->stats) {
  35347. + irs->stats->rx_errors++;
  35348. + }
  35349. + goto rcvleave;
  35350. + }
  35351. + }
  35352. +#endif /* CONFIG_KLIPS_IPCOMP */
  35353. +
  35354. + /*
  35355. + * make sure that data now starts at IP header, since we are going
  35356. + * to pass this back to ip_input (aka netif_rx). Rules for what the
  35357. + * pointers wind up a different for 2.6 vs 2.4, so we just fudge it here.
  35358. + */
  35359. +#ifdef NET_26
  35360. + skb->data = skb_push(skb, skb->h.raw - skb->nh.raw);
  35361. +#else
  35362. + skb->data = skb->nh.raw;
  35363. + {
  35364. + struct iphdr *iph = skb->nh.iph;
  35365. + int len = ntohs(iph->tot_len);
  35366. + skb->len = len;
  35367. + }
  35368. +#endif
  35369. +
  35370. +#ifdef SKB_RESET_NFCT
  35371. + nf_conntrack_put(skb->nfct);
  35372. + skb->nfct = NULL;
  35373. +#if defined(CONFIG_NETFILTER_DEBUG) && defined(HAVE_SKB_NF_DEBUG)
  35374. + skb->nf_debug = 0;
  35375. +#endif /* CONFIG_NETFILTER_DEBUG */
  35376. +#endif /* SKB_RESET_NFCT */
  35377. + KLIPS_PRINT(debug_rcv & DB_RX_PKTRX,
  35378. + "klips_debug:ipsec_rcv: "
  35379. + "netif_rx() called.\n");
  35380. + netif_rx(skb);
  35381. + skb=NULL;
  35382. +
  35383. + rcvleave:
  35384. + if(skb) {
  35385. + ipsec_kfree_skb(skb);
  35386. + }
  35387. +
  35388. + /* KLIPS_DEC_USE; Artifact from refactor? bug # 454 */
  35389. + return(0);
  35390. +}
  35391. +
  35392. +struct sk_buff *ipsec_rcv_unclone(struct sk_buff *skb,
  35393. + struct ipsec_rcv_state *irs)
  35394. +{
  35395. + /* if skb was cloned (most likely due to a packet sniffer such as
  35396. + tcpdump being momentarily attached to the interface), make
  35397. + a copy of our own to modify */
  35398. + if(skb_cloned(skb)) {
  35399. + /* include any mac header while copying.. */
  35400. + if(skb_headroom(skb) < irs->hard_header_len) {
  35401. + printk(KERN_WARNING "klips_error:ipsec_rcv: "
  35402. + "tried to skb_push hhlen=%d, %d available. This should never happen, please report.\n",
  35403. + irs->hard_header_len,
  35404. + skb_headroom(skb));
  35405. + goto rcvleave;
  35406. + }
  35407. + skb_push(skb, irs->hard_header_len);
  35408. + if
  35409. +#ifdef SKB_COW_NEW
  35410. + (skb_cow(skb, skb_headroom(skb)) != 0)
  35411. +#else /* SKB_COW_NEW */
  35412. + ((skb = skb_cow(skb, skb_headroom(skb))) == NULL)
  35413. +#endif /* SKB_COW_NEW */
  35414. + {
  35415. + goto rcvleave;
  35416. + }
  35417. + if(skb->len < irs->hard_header_len) {
  35418. + printk(KERN_WARNING "klips_error:ipsec_rcv: "
  35419. + "tried to skb_pull hhlen=%d, %d available. This should never happen, please report.\n",
  35420. + irs->hard_header_len,
  35421. + skb->len);
  35422. + goto rcvleave;
  35423. + }
  35424. + skb_pull(skb, irs->hard_header_len);
  35425. + }
  35426. + return skb;
  35427. +
  35428. +rcvleave:
  35429. + ipsec_kfree_skb(skb);
  35430. + return NULL;
  35431. +}
  35432. +
  35433. +
  35434. +#if !defined(NET_26) && defined(CONFIG_IPSEC_NAT_TRAVERSAL)
  35435. +/*
  35436. + * decapsulate a UDP encapsulated ESP packet
  35437. + */
  35438. +struct sk_buff *ipsec_rcv_natt_decap(struct sk_buff *skb
  35439. + , struct ipsec_rcv_state *irs
  35440. + , int *udp_decap_ret_p)
  35441. +{
  35442. + *udp_decap_ret_p = 0;
  35443. + if (skb->sk && skb->nh.iph && skb->nh.iph->protocol==IPPROTO_UDP) {
  35444. + /**
  35445. + * Packet comes from udp_queue_rcv_skb so it is already defrag,
  35446. + * checksum verified, ... (ie safe to use)
  35447. + *
  35448. + * If the packet is not for us, return -1 and udp_queue_rcv_skb
  35449. + * will continue to handle it (do not kfree skb !!).
  35450. + */
  35451. +
  35452. +#ifndef UDP_OPT_IN_SOCK
  35453. + struct udp_opt {
  35454. + __u32 esp_in_udp;
  35455. + };
  35456. + struct udp_opt *tp = (struct udp_opt *)&(skb->sk->tp_pinfo.af_tcp);
  35457. +#else
  35458. + struct udp_opt *tp = &(skb->sk->tp_pinfo.af_udp);
  35459. +#endif
  35460. +
  35461. + struct iphdr *ip = (struct iphdr *)skb->nh.iph;
  35462. + struct udphdr *udp = (struct udphdr *)((__u32 *)ip+ip->ihl);
  35463. + __u8 *udpdata = (__u8 *)udp + sizeof(struct udphdr);
  35464. + __u32 *udpdata32 = (__u32 *)udpdata;
  35465. +
  35466. + irs->natt_sport = ntohs(udp->source);
  35467. + irs->natt_dport = ntohs(udp->dest);
  35468. +
  35469. + KLIPS_PRINT(debug_rcv,
  35470. + "klips_debug:ipsec_rcv: "
  35471. + "suspected ESPinUDP packet (NAT-Traversal) [%d].\n",
  35472. + tp->esp_in_udp);
  35473. + KLIPS_IP_PRINT(debug_rcv, ip);
  35474. +
  35475. + if (udpdata < skb->tail) {
  35476. + unsigned int len = skb->tail - udpdata;
  35477. + if ((len==1) && (udpdata[0]==0xff)) {
  35478. + KLIPS_PRINT(debug_rcv,
  35479. + "klips_debug:ipsec_rcv: "
  35480. + /* not IPv6 compliant message */
  35481. + "NAT-keepalive from %d.%d.%d.%d.\n", NIPQUAD(ip->saddr));
  35482. + *udp_decap_ret_p = 0;
  35483. + return NULL;
  35484. + }
  35485. + else if ( (tp->esp_in_udp == ESPINUDP_WITH_NON_IKE) &&
  35486. + (len > (2*sizeof(__u32) + sizeof(struct esphdr))) &&
  35487. + (udpdata32[0]==0) && (udpdata32[1]==0) ) {
  35488. + /* ESP Packet with Non-IKE header */
  35489. + KLIPS_PRINT(debug_rcv,
  35490. + "klips_debug:ipsec_rcv: "
  35491. + "ESPinUDP pkt with Non-IKE - spi=0x%x\n",
  35492. + ntohl(udpdata32[2]));
  35493. + irs->natt_type = ESPINUDP_WITH_NON_IKE;
  35494. + irs->natt_len = sizeof(struct udphdr)+(2*sizeof(__u32));
  35495. + }
  35496. + else if ( (tp->esp_in_udp == ESPINUDP_WITH_NON_ESP) &&
  35497. + (len > sizeof(struct esphdr)) &&
  35498. + (udpdata32[0]!=0) ) {
  35499. + /* ESP Packet without Non-ESP header */
  35500. + irs->natt_type = ESPINUDP_WITH_NON_ESP;
  35501. + irs->natt_len = sizeof(struct udphdr);
  35502. + KLIPS_PRINT(debug_rcv,
  35503. + "klips_debug:ipsec_rcv: "
  35504. + "ESPinUDP pkt without Non-ESP - spi=0x%x\n",
  35505. + ntohl(udpdata32[0]));
  35506. + }
  35507. + else {
  35508. + KLIPS_PRINT(debug_rcv,
  35509. + "klips_debug:ipsec_rcv: "
  35510. + "IKE packet - not handled here\n");
  35511. + *udp_decap_ret_p = -1;
  35512. + return NULL;
  35513. + }
  35514. + }
  35515. + else {
  35516. + return NULL;
  35517. + }
  35518. + }
  35519. + return skb;
  35520. +}
  35521. +#endif
  35522. +
  35523. +
  35524. +int
  35525. +ipsec_rcv(struct sk_buff *skb
  35526. +#ifndef PROTO_HANDLER_SINGLE_PARM
  35527. + unsigned short xlen
  35528. +#endif /* PROTO_HANDLER_SINGLE_PARM */
  35529. + )
  35530. +{
  35531. +#ifdef CONFIG_KLIPS_DEBUG
  35532. + struct net_device *dev = skb->dev;
  35533. +#endif /* CONFIG_KLIPS_DEBUG */
  35534. + unsigned char protoc;
  35535. + struct net_device_stats *stats = NULL; /* This device's statistics */
  35536. + struct net_device *ipsecdev = NULL, *prvdev;
  35537. + struct ipsecpriv *prv;
  35538. + struct ipsec_rcv_state nirs, *irs = &nirs;
  35539. + struct iphdr *ipp;
  35540. + char name[9];
  35541. + int i;
  35542. +
  35543. + /* Don't unlink in the middle of a turnaround */
  35544. + KLIPS_INC_USE;
  35545. +
  35546. + memset(&nirs, 0, sizeof(struct ipsec_rcv_state));
  35547. +
  35548. + if (skb == NULL) {
  35549. + KLIPS_PRINT(debug_rcv,
  35550. + "klips_debug:ipsec_rcv: "
  35551. + "NULL skb passed in.\n");
  35552. + goto rcvleave;
  35553. + }
  35554. +
  35555. + if (skb->data == NULL) {
  35556. + KLIPS_PRINT(debug_rcv,
  35557. + "klips_debug:ipsec_rcv: "
  35558. + "NULL skb->data passed in, packet is bogus, dropping.\n");
  35559. + goto rcvleave;
  35560. + }
  35561. +
  35562. +#if defined(CONFIG_IPSEC_NAT_TRAVERSAL) && !defined(NET_26)
  35563. + {
  35564. + /* NET_26 NAT-T is handled by seperate function */
  35565. + struct sk_buff *nskb;
  35566. + int udp_decap_ret = 0;
  35567. +
  35568. + nskb = ipsec_rcv_natt_decap(skb, irs, &udp_decap_ret);
  35569. + if(nskb == NULL) {
  35570. + /* return with non-zero, because UDP.c code
  35571. + * need to send it upstream.
  35572. + */
  35573. + if(skb && udp_decap_ret == 0) {
  35574. + ipsec_kfree_skb(skb);
  35575. + }
  35576. + KLIPS_DEC_USE;
  35577. + return(udp_decap_ret);
  35578. + }
  35579. + skb = nskb;
  35580. + }
  35581. +#endif /* NAT_T */
  35582. +
  35583. + /* dev->hard_header_len is unreliable and should not be used */
  35584. + irs->hard_header_len = skb->mac.raw ? (skb->nh.raw - skb->mac.raw) : 0;
  35585. + if((irs->hard_header_len < 0) || (irs->hard_header_len > skb_headroom(skb)))
  35586. + irs->hard_header_len = 0;
  35587. +
  35588. + skb = ipsec_rcv_unclone(skb, irs);
  35589. + if(skb == NULL) {
  35590. + goto rcvleave;
  35591. + }
  35592. +
  35593. +#if IP_FRAGMENT_LINEARIZE
  35594. + /* In Linux 2.4.4, we may have to reassemble fragments. They are
  35595. + not assembled automatically to save TCP from having to copy
  35596. + twice.
  35597. + */
  35598. + if (skb_is_nonlinear(skb)) {
  35599. +#ifdef HAVE_NEW_SKB_LINEARIZE
  35600. + if (skb_linearize_cow(skb) != 0)
  35601. +#else
  35602. + if (skb_linearize(skb, GFP_ATOMIC) != 0)
  35603. +#endif
  35604. + {
  35605. + goto rcvleave;
  35606. + }
  35607. + }
  35608. +#endif /* IP_FRAGMENT_LINEARIZE */
  35609. +
  35610. +#if defined(CONFIG_IPSEC_NAT_TRAVERSAL) && !defined(NET_26)
  35611. + if (irs->natt_len) {
  35612. + /**
  35613. + * Now, we are sure packet is ESPinUDP, and we have a private
  35614. + * copy that has been linearized, remove natt_len bytes
  35615. + * from packet and modify protocol to ESP.
  35616. + */
  35617. + if (((unsigned char *)skb->data > (unsigned char *)skb->nh.iph)
  35618. + && ((unsigned char *)skb->nh.iph > (unsigned char *)skb->head))
  35619. + {
  35620. + unsigned int _len = (unsigned char *)skb->data -
  35621. + (unsigned char *)skb->nh.iph;
  35622. + KLIPS_PRINT(debug_rcv,
  35623. + "klips_debug:ipsec_rcv: adjusting skb: skb_push(%u)\n",
  35624. + _len);
  35625. + skb_push(skb, _len);
  35626. + }
  35627. + KLIPS_PRINT(debug_rcv,
  35628. + "klips_debug:ipsec_rcv: "
  35629. + "removing %d bytes from ESPinUDP packet\n", irs->natt_len);
  35630. + ipp = skb->nh.iph;
  35631. + irs->iphlen = ipp->ihl << 2;
  35632. + ipp->tot_len = htons(ntohs(ipp->tot_len) - irs->natt_len);
  35633. + if (skb->len < irs->iphlen + irs->natt_len) {
  35634. + printk(KERN_WARNING
  35635. + "klips_error:ipsec_rcv: "
  35636. + "ESPinUDP packet is too small (%d < %d+%d). "
  35637. + "This should never happen, please report.\n",
  35638. + (int)(skb->len), irs->iphlen, irs->natt_len);
  35639. + goto rcvleave;
  35640. + }
  35641. +
  35642. + /* advance payload pointer to point past the UDP header */
  35643. + skb->h.raw = skb->h.raw + irs->natt_len;
  35644. +
  35645. + /* modify protocol */
  35646. + ipp->protocol = IPPROTO_ESP;
  35647. +
  35648. + skb->sk = NULL;
  35649. +
  35650. + KLIPS_IP_PRINT(debug_rcv, skb->nh.iph);
  35651. + }
  35652. +#endif
  35653. +
  35654. + ipp = skb->nh.iph;
  35655. +
  35656. + {
  35657. + struct in_addr ipsaddr;
  35658. + struct in_addr ipdaddr;
  35659. +
  35660. + ipsaddr.s_addr = ipp->saddr;
  35661. + addrtoa(ipsaddr, 0, irs->ipsaddr_txt
  35662. + , sizeof(irs->ipsaddr_txt));
  35663. + ipdaddr.s_addr = ipp->daddr;
  35664. + addrtoa(ipdaddr, 0, irs->ipdaddr_txt
  35665. + , sizeof(irs->ipdaddr_txt));
  35666. + }
  35667. +
  35668. + irs->iphlen = ipp->ihl << 2;
  35669. +
  35670. + KLIPS_PRINT(debug_rcv,
  35671. + "klips_debug:ipsec_rcv: "
  35672. + "<<< Info -- ");
  35673. + KLIPS_PRINTMORE(debug_rcv && skb->dev, "skb->dev=%s ",
  35674. + skb->dev->name ? skb->dev->name : "NULL");
  35675. + KLIPS_PRINTMORE(debug_rcv && dev, "dev=%s ",
  35676. + dev->name ? dev->name : "NULL");
  35677. + KLIPS_PRINTMORE(debug_rcv, "\n");
  35678. +
  35679. + KLIPS_PRINT(debug_rcv && !(skb->dev && dev && (skb->dev == dev)),
  35680. + "klips_debug:ipsec_rcv: "
  35681. + "Informational -- **if this happens, find out why** skb->dev:%s is not equal to dev:%s\n",
  35682. + skb->dev ? (skb->dev->name ? skb->dev->name : "NULL") : "NULL",
  35683. + dev ? (dev->name ? dev->name : "NULL") : "NULL");
  35684. +
  35685. + protoc = ipp->protocol;
  35686. +#ifndef NET_21
  35687. + if((!protocol) || (protocol->protocol != protoc)) {
  35688. + KLIPS_PRINT(debug_rcv & DB_RX_IPSA,
  35689. + "klips_debug:ipsec_rcv: "
  35690. + "protocol arg is NULL or unequal to the packet contents, this is odd, using value in packet.\n");
  35691. + }
  35692. +#endif /* !NET_21 */
  35693. +
  35694. + if( (protoc != IPPROTO_AH) &&
  35695. +#ifdef CONFIG_KLIPS_IPCOMP_disabled_until_we_register_IPCOMP_HANDLER
  35696. + (protoc != IPPROTO_COMP) &&
  35697. +#endif /* CONFIG_KLIPS_IPCOMP */
  35698. + (protoc != IPPROTO_ESP) ) {
  35699. + KLIPS_PRINT(debug_rcv & DB_RX_IPSA,
  35700. + "klips_debug:ipsec_rcv: Why the hell is someone "
  35701. + "passing me a non-ipsec protocol = %d packet? -- dropped.\n",
  35702. + protoc);
  35703. + goto rcvleave;
  35704. + }
  35705. +
  35706. + if(skb->dev) {
  35707. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  35708. + sprintf(name, IPSEC_DEV_FORMAT, i);
  35709. + if(!strcmp(name, skb->dev->name)) {
  35710. + prv = (struct ipsecpriv *)(skb->dev->priv);
  35711. + if(prv) {
  35712. + stats = (struct net_device_stats *) &(prv->mystats);
  35713. + }
  35714. + ipsecdev = skb->dev;
  35715. + KLIPS_PRINT(debug_rcv,
  35716. + "klips_debug:ipsec_rcv: "
  35717. + "Info -- pkt already proc'ed a group of ipsec headers, processing next group of ipsec headers.\n");
  35718. + break;
  35719. + }
  35720. + if((ipsecdev = __ipsec_dev_get(name)) == NULL) {
  35721. + KLIPS_PRINT(debug_rcv,
  35722. + "klips_error:ipsec_rcv: "
  35723. + "device %s does not exist\n",
  35724. + name);
  35725. + }
  35726. + prv = ipsecdev ? (struct ipsecpriv *)(ipsecdev->priv) : NULL;
  35727. + prvdev = prv ? (struct net_device *)(prv->dev) : NULL;
  35728. +
  35729. +#if 0
  35730. + KLIPS_PRINT(debug_rcv && prvdev,
  35731. + "klips_debug:ipsec_rcv: "
  35732. + "physical device for device %s is %s\n",
  35733. + name,
  35734. + prvdev->name);
  35735. +#endif
  35736. + if(prvdev && skb->dev &&
  35737. + !strcmp(prvdev->name, skb->dev->name)) {
  35738. + stats = prv ? ((struct net_device_stats *) &(prv->mystats)) : NULL;
  35739. + skb->dev = ipsecdev;
  35740. + KLIPS_PRINT(debug_rcv && prvdev,
  35741. + "klips_debug:ipsec_rcv: "
  35742. + "assigning packet ownership to virtual device %s from physical device %s.\n",
  35743. + name, prvdev->name);
  35744. + if(stats) {
  35745. + stats->rx_packets++;
  35746. + }
  35747. + break;
  35748. + }
  35749. + }
  35750. + } else {
  35751. + KLIPS_PRINT(debug_rcv,
  35752. + "klips_debug:ipsec_rcv: "
  35753. + "device supplied with skb is NULL\n");
  35754. + }
  35755. +
  35756. + if(stats == NULL) {
  35757. + KLIPS_PRINT((debug_rcv),
  35758. + "klips_error:ipsec_rcv: "
  35759. + "packet received from physical I/F (%s) not connected to ipsec I/F. Cannot record stats. May not have SA for decoding. Is IPSEC traffic expected on this I/F? Check routing.\n",
  35760. + skb->dev ? (skb->dev->name ? skb->dev->name : "NULL") : "NULL");
  35761. + }
  35762. +
  35763. + KLIPS_IP_PRINT(debug_rcv, ipp);
  35764. +
  35765. + /* set up for decap loop */
  35766. + irs->stats= stats;
  35767. + irs->ipp = ipp;
  35768. + irs->ipsp = NULL;
  35769. + irs->ilen = 0;
  35770. + irs->authlen=0;
  35771. + irs->authfuncs=NULL;
  35772. + irs->skb = skb;
  35773. +
  35774. + ipsec_rcv_decap(irs);
  35775. + KLIPS_DEC_USE;
  35776. + return(0);
  35777. +
  35778. + rcvleave:
  35779. + if(skb) {
  35780. + ipsec_kfree_skb(skb);
  35781. + }
  35782. + KLIPS_DEC_USE;
  35783. + return(0);
  35784. +
  35785. +}
  35786. +
  35787. +#ifdef NET_26
  35788. +/*
  35789. + * this entry point is not a protocol entry point, so the entry
  35790. + * is a bit different.
  35791. + *
  35792. + * skb->iph->tot_len has been byte-swapped, and reduced by the size of
  35793. + * the IP header (and options).
  35794. + *
  35795. + * skb->h.raw has been pulled up the ESP header.
  35796. + *
  35797. + * skb->iph->protocol = 50 IPPROTO_ESP;
  35798. + *
  35799. + */
  35800. +int klips26_rcv_encap(struct sk_buff *skb, __u16 encap_type)
  35801. +{
  35802. + struct ipsec_rcv_state nirs, *irs = &nirs;
  35803. + struct iphdr *ipp;
  35804. +
  35805. + /* Don't unlink in the middle of a turnaround */
  35806. + KLIPS_INC_USE;
  35807. +
  35808. + memset(irs, 0, sizeof(*irs));
  35809. +
  35810. + /* XXX fudge it so that all nat-t stuff comes from ipsec0 */
  35811. + /* eventually, the SA itself will determine which device
  35812. + * it comes from
  35813. + */
  35814. + {
  35815. + skb->dev = ipsec_get_device(0);
  35816. + }
  35817. +
  35818. + /* set up for decap loop */
  35819. + irs->hard_header_len = skb->dev->hard_header_len;
  35820. +
  35821. + skb = ipsec_rcv_unclone(skb, irs);
  35822. +
  35823. +#if IP_FRAGMENT_LINEARIZE
  35824. + /* In Linux 2.4.4, we may have to reassemble fragments. They are
  35825. + not assembled automatically to save TCP from having to copy
  35826. + twice.
  35827. + */
  35828. + if (skb_is_nonlinear(skb)) {
  35829. +#ifdef HAVE_NEW_SKB_LINEARIZE
  35830. + if (skb_linearize_cow(skb) != 0)
  35831. +#else
  35832. + if (skb_linearize(skb, GFP_ATOMIC) != 0)
  35833. +#endif
  35834. + {
  35835. + goto rcvleave;
  35836. + }
  35837. + }
  35838. +#endif /* IP_FRAGMENT_LINEARIZE */
  35839. +
  35840. + ipp = skb->nh.iph;
  35841. +
  35842. + {
  35843. + struct in_addr ipsaddr;
  35844. + struct in_addr ipdaddr;
  35845. +
  35846. + ipsaddr.s_addr = ipp->saddr;
  35847. + addrtoa(ipsaddr, 0, irs->ipsaddr_txt
  35848. + , sizeof(irs->ipsaddr_txt));
  35849. + ipdaddr.s_addr = ipp->daddr;
  35850. + addrtoa(ipdaddr, 0, irs->ipdaddr_txt
  35851. + , sizeof(irs->ipdaddr_txt));
  35852. + }
  35853. +
  35854. + irs->iphlen = ipp->ihl << 2;
  35855. +
  35856. + KLIPS_IP_PRINT(debug_rcv, ipp);
  35857. +
  35858. + irs->stats= NULL;
  35859. + irs->ipp = ipp;
  35860. + irs->ipsp = NULL;
  35861. + irs->ilen = 0;
  35862. + irs->authlen=0;
  35863. + irs->authfuncs=NULL;
  35864. + irs->skb = skb;
  35865. +
  35866. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  35867. + switch(encap_type) {
  35868. + case UDP_ENCAP_ESPINUDP:
  35869. + irs->natt_type = ESPINUDP_WITH_NON_ESP;
  35870. + break;
  35871. +
  35872. + case UDP_ENCAP_ESPINUDP_NON_IKE:
  35873. + irs->natt_type = ESPINUDP_WITH_NON_IKE;
  35874. + break;
  35875. +
  35876. + default:
  35877. + if(printk_ratelimit()) {
  35878. + printk(KERN_INFO "KLIPS received unknown UDP-ESP encap type %u\n",
  35879. + encap_type);
  35880. + }
  35881. + return -1;
  35882. + }
  35883. +
  35884. +#endif
  35885. + ipsec_rcv_decap(irs);
  35886. + KLIPS_DEC_USE;
  35887. + return 0;
  35888. +
  35889. +rcvleave:
  35890. + if(skb) {
  35891. + ipsec_kfree_skb(skb);
  35892. + }
  35893. + KLIPS_DEC_USE;
  35894. + return 0;
  35895. +}
  35896. +#endif
  35897. +
  35898. +
  35899. +/*
  35900. + * $Log: ipsec_rcv.c,v $
  35901. + * Revision 1.171.2.11 2007/04/28 20:46:40 paul
  35902. + * Added compile time switch for -DDISABLE_UDP_CHECKSUM that seems to be
  35903. + * breaking IPsec+NAT+Transport mode with NAT-OA. Enabled this per default
  35904. + * via Makefile.inc's USERCOMPILE flags.
  35905. + *
  35906. + * Revision 1.171.2.10 2006/10/06 21:39:26 paul
  35907. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  35908. + * set. This is defined through autoconf.h which is included through the
  35909. + * linux kernel build macros.
  35910. + *
  35911. + * Revision 1.171.2.9 2006/07/30 02:09:33 paul
  35912. + * Author: Bart Trojanowski <bart@xelerance.com>
  35913. + * This fixes a NATT+ESP bug in rcv path.
  35914. + *
  35915. + * We only want to test NATT policy on the ESP packet. Doing so on the
  35916. + * bundled SA breaks because the next layer does not know anything about
  35917. + * NATT.
  35918. + *
  35919. + * Fix just puts an if(proto == IPPROTO_ESP) around the NATT policy check.
  35920. + *
  35921. + * Revision 1.171.2.8 2006/07/29 05:03:04 paul
  35922. + * Added check for new version of skb_linearize that only takes 1 argument,
  35923. + * for 2.6.18+ kernels.
  35924. + *
  35925. + * Revision 1.171.2.7 2006/04/20 16:33:07 mcr
  35926. + * remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  35927. + * Fix in-kernel module compilation. Sub-makefiles do not work.
  35928. + *
  35929. + * Revision 1.171.2.6 2005/12/07 06:07:04 paul
  35930. + * comment out KLIPS_DEC_USE in ipsec_rcv_decap. Likely an artifact from
  35931. + * refactoring. http://bugs.xelerance.com/view.php?id=454
  35932. + *
  35933. + * Revision 1.171.2.5 2005/10/21 02:22:29 mcr
  35934. + * pull up of another try at 2.4.x kernel fix
  35935. + *
  35936. + * Revision 1.171.2.4 2005/10/21 01:39:56 mcr
  35937. + * nat-t fix is 2.4/2.6 specific
  35938. + *
  35939. + * Revision 1.178 2005/10/21 02:19:34 mcr
  35940. + * on 2.4 systems, we have to fix up the length as well.
  35941. + *
  35942. + * Revision 1.177 2005/10/21 00:18:31 mcr
  35943. + * nat-t fix is 2.4 specific.
  35944. + *
  35945. + * Revision 1.176 2005/10/20 21:06:11 mcr
  35946. + * possible fix for nat-t problem on 2.4 kernels.
  35947. + *
  35948. + * Revision 1.175 2005/10/13 02:49:24 mcr
  35949. + * tested UDP-encapsulated ESP packets that were not actually ESP,
  35950. + * (but IKE) were being eaten.
  35951. + *
  35952. + * Revision 1.174 2005/10/13 01:25:22 mcr
  35953. + * UDP-encapsulated ESP packets that were not actually ESP,
  35954. + * (but IKE) were being eaten.
  35955. + *
  35956. + * Revision 1.173 2005/08/31 23:26:11 mcr
  35957. + * fixes for 2.6.13
  35958. + *
  35959. + * Revision 1.172 2005/08/05 08:44:54 mcr
  35960. + * ipsec_kern24.h (compat code for 2.4) must be include
  35961. + * explicitely now.
  35962. + *
  35963. + * Revision 1.171 2005/07/08 23:56:06 ken
  35964. + * #ifdef
  35965. + *
  35966. + * Revision 1.170 2005/07/08 23:50:05 ken
  35967. + * Don't attempt to decapsulate if NAT-T isn't available in the code
  35968. + *
  35969. + * Revision 1.169 2005/06/06 00:27:31 mcr
  35970. + * fix for making tcpdump (packet capture) work correctly for
  35971. + * nat-t received packets.
  35972. + *
  35973. + * Revision 1.168 2005/06/04 16:06:06 mcr
  35974. + * better patch for nat-t rcv-device code.
  35975. + *
  35976. + * Revision 1.167 2005/06/03 17:04:46 mcr
  35977. + * nat-t packets are forced to arrive from ipsec0.
  35978. + *
  35979. + * Revision 1.166 2005/04/29 05:10:22 mcr
  35980. + * removed from extraenous includes to make unit testing easier.
  35981. + *
  35982. + * Revision 1.165 2005/04/20 17:11:32 mcr
  35983. + * fixed to compile on 2.4.
  35984. + *
  35985. + * Revision 1.164 2005/04/18 03:09:50 ken
  35986. + * Fix typo
  35987. + *
  35988. + * Revision 1.163 2005/04/17 05:32:58 mcr
  35989. + * remove extraneous debugging
  35990. + * make sure to return success from klips26_encap_rcv().
  35991. + *
  35992. + * Revision 1.162 2005/04/17 04:37:01 mcr
  35993. + * make sure that irs->ipp is still set.
  35994. + *
  35995. + * Revision 1.161 2005/04/17 03:51:52 mcr
  35996. + * removed old comment about removed code.
  35997. + * added translation from udp.c/2.6 to KLIPS NAT-ESP naming.
  35998. + * comment about check for origin address/port for incoming NAT-ESP packets.
  35999. + *
  36000. + * Revision 1.160 2005/04/15 19:55:58 mcr
  36001. + * adjustments to use proper skb fields for data.
  36002. + *
  36003. + * Revision 1.159 2005/04/10 22:58:20 mcr
  36004. + * refactoring of receive functions to make it easier to
  36005. + * call the ESP decap.
  36006. + *
  36007. + * Revision 1.158 2005/04/08 18:27:53 mcr
  36008. + * refactored ipsec_rcv() into ipsec_rcv() and ipsec_rcv_decap().
  36009. + *
  36010. + * Revision 1.157 2004/12/28 23:13:09 mcr
  36011. + * use consistent CONFIG_IPSEC_NAT_TRAVERSAL.
  36012. + *
  36013. + * Revision 1.156 2004/12/03 21:34:51 mcr
  36014. + * mistype of KLIPS_USE_COUNT -> KLIPS_INC_USE;
  36015. + *
  36016. + * Revision 1.155 2004/12/03 21:25:57 mcr
  36017. + * compile time fixes for running on 2.6.
  36018. + * still experimental.
  36019. + *
  36020. + * Revision 1.154 2004/09/08 17:21:36 ken
  36021. + * Rename MD5* -> osMD5 functions to prevent clashes with other symbols exported by kernel modules (CIFS in 2.6 initiated this)
  36022. + *
  36023. + * Revision 1.153 2004/08/22 20:10:00 mcr
  36024. + * removed check for incorrect setting of NET_26.
  36025. + *
  36026. + * Revision 1.152 2004/08/21 15:22:39 mcr
  36027. + * added #defines for ATT heartbeat.
  36028. + *
  36029. + * Revision 1.151 2004/08/21 02:16:32 ken
  36030. + * Patch from Jochen Eisinger for AT&T MTS Heartbeat packet support
  36031. + *
  36032. + * Revision 1.150 2004/08/21 00:44:48 mcr
  36033. + * CONFIG_KLIPS_NAT was wrong, also need to include udp.h.
  36034. + *
  36035. + * Revision 1.149 2004/08/20 21:45:45 mcr
  36036. + * CONFIG_KLIPS_NAT_TRAVERSAL is not used in an attempt to
  36037. + * be 26sec compatible. But, some defines where changed.
  36038. + *
  36039. + * Revision 1.148 2004/08/17 03:27:23 mcr
  36040. + * klips 2.6 edits.
  36041. + *
  36042. + * Revision 1.147 2004/08/05 23:29:27 mcr
  36043. + * fixed nesting of #ifdef vs {} in ipsec_rcv().
  36044. + *
  36045. + * Revision 1.146 2004/08/04 15:57:07 mcr
  36046. + * moved des .h files to include/des/ *
  36047. + * included 2.6 protocol specific things
  36048. + * started at NAT-T support, but it will require a kernel patch.
  36049. + *
  36050. + * Revision 1.145 2004/08/03 18:19:08 mcr
  36051. + * in 2.6, use "net_device" instead of #define device->net_device.
  36052. + * this probably breaks 2.0 compiles.
  36053. + *
  36054. + * Revision 1.144 2004/07/10 19:11:18 mcr
  36055. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  36056. + *
  36057. + * Revision 1.143 2004/05/10 22:27:00 mcr
  36058. + * fix for ESP-3DES-noauth test case.
  36059. + *
  36060. + * Revision 1.142 2004/05/10 22:25:57 mcr
  36061. + * reformat of calls to ipsec_lifetime_check().
  36062. + *
  36063. + * Revision 1.141 2004/04/06 02:49:26 mcr
  36064. + * pullup of algo code from alg-branch.
  36065. + *
  36066. + * Revision 1.140 2004/02/03 03:12:53 mcr
  36067. + * removed erroneously, double patched code.
  36068. + *
  36069. + * Revision 1.139 2004/01/05 23:21:29 mcr
  36070. + * initialize sin_family in ipsec_rcv.c
  36071. + *
  36072. + * Revision 1.138 2003/12/24 19:46:52 mcr
  36073. + * if sock.h patch has not been applied, then define appropriate
  36074. + * structure so we can use it. This is serious inferior, and
  36075. + * depends upon the concept that the structure in question is
  36076. + * smaller than the other members of that union.
  36077. + * getting rid of differing methods is a better solution.
  36078. + *
  36079. + * Revision 1.137 2003/12/22 19:40:57 mcr
  36080. + * NAT-T patches 0.6c.
  36081. + *
  36082. + * Revision 1.136 2003/12/15 18:13:12 mcr
  36083. + * when compiling with NAT traversal, don't assume that the
  36084. + * kernel has been patched, unless CONFIG_IPSEC_NAT_NON_ESP
  36085. + * is set.
  36086. + *
  36087. + * Revision 1.135 2003/12/13 19:10:21 mcr
  36088. + * refactored rcv and xmit code - same as FS 2.05.
  36089. + *
  36090. + * Revision 1.134.2.1 2003/12/22 15:25:52 jjo
  36091. + * Merged algo-0.8.1-rc11-test1 into alg-branch
  36092. + *
  36093. + * Revision 1.134 2003/12/10 01:14:27 mcr
  36094. + * NAT-traversal patches to KLIPS.
  36095. + *
  36096. + * Revision 1.133 2003/10/31 02:27:55 mcr
  36097. + * pulled up port-selector patches and sa_id elimination.
  36098. + *
  36099. + * Revision 1.132.2.1 2003/10/29 01:30:41 mcr
  36100. + * elimited "struct sa_id".
  36101. + *
  36102. + * Revision 1.132 2003/09/02 19:51:48 mcr
  36103. + * fixes for PR#252.
  36104. + *
  36105. + * Revision 1.131 2003/07/31 22:47:16 mcr
  36106. + * preliminary (untested by FS-team) 2.5 patches.
  36107. + *
  36108. + * Revision 1.130 2003/04/03 17:38:25 rgb
  36109. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  36110. + * Clarified logic for non-connected devices.
  36111. + *
  36112. + * Revision 1.129 2003/02/06 02:21:34 rgb
  36113. + *
  36114. + * Moved "struct auth_alg" from ipsec_rcv.c to ipsec_ah.h .
  36115. + * Changed "struct ah" to "struct ahhdr" and "struct esp" to "struct esphdr".
  36116. + * Removed "#ifdef INBOUND_POLICY_CHECK_eroute" dead code.
  36117. + *
  36118. + * Revision 1.128 2002/12/13 20:58:03 rgb
  36119. + * Relegated MCR's recent "_dmp" routine to debug_verbose.
  36120. + * Cleaned up printing of source and destination addresses in debug output.
  36121. + *
  36122. + * Revision 1.127 2002/12/04 16:00:16 rgb
  36123. + *
  36124. + * Fixed AH decapsulation pointer update bug and added some comments and
  36125. + * debugging.
  36126. + * This bug was caught by west-ah-0[12].
  36127. + *
  36128. + * Revision 1.126 2002/11/04 05:03:43 mcr
  36129. + * fixes for IPCOMP. There were two problems:
  36130. + * 1) the irs->ipp pointer was not being updated properly after
  36131. + * the ESP descryption. The meant nothing for IPIP, as the
  36132. + * later IP header overwrote the earlier one.
  36133. + * 2) the more serious problem was that skb_decompress will
  36134. + * usually allocate a new SKB, so we have to make sure that
  36135. + * it doesn't get lost.
  36136. + * #2 meant removing the skb argument from the ->decrypt routine
  36137. + * and moving it to the irs->skb, so it could be value/result.
  36138. + *
  36139. + * Revision 1.125 2002/11/01 01:53:35 dhr
  36140. + *
  36141. + * fix typo
  36142. + *
  36143. + * Revision 1.124 2002/10/31 22:49:01 dhr
  36144. + *
  36145. + * - eliminate unused variable "hash"
  36146. + * - reduce scope of variable "authenticator"
  36147. + * - add comment on a couple of tricky bits
  36148. + *
  36149. + * Revision 1.123 2002/10/31 22:39:56 dhr
  36150. + *
  36151. + * use correct type for result of function calls
  36152. + *
  36153. + * Revision 1.122 2002/10/31 22:36:25 dhr
  36154. + *
  36155. + * simplify complex test
  36156. + *
  36157. + * Revision 1.121 2002/10/31 22:34:04 dhr
  36158. + *
  36159. + * ipsprev is never used: ditch it
  36160. + *
  36161. + * Revision 1.120 2002/10/31 22:30:21 dhr
  36162. + *
  36163. + * eliminate redundant assignments
  36164. + *
  36165. + * Revision 1.119 2002/10/31 22:27:43 dhr
  36166. + *
  36167. + * make whitespace canonical
  36168. + *
  36169. + * Revision 1.118 2002/10/30 05:47:17 rgb
  36170. + * Fixed cut-and-paste error mis-identifying comp runt as ah.
  36171. + *
  36172. + * Revision 1.117 2002/10/17 16:37:45 rgb
  36173. + * Remove compp intermediate variable and in-line its contents
  36174. + * where used
  36175. + *
  36176. + * Revision 1.116 2002/10/12 23:11:53 dhr
  36177. + *
  36178. + * [KenB + DHR] more 64-bit cleanup
  36179. + *
  36180. + * Revision 1.115 2002/10/07 19:06:58 rgb
  36181. + * Minor fixups and activation to west-rcv-nfmark-set-01 test to check for SA reference properly set on incoming.
  36182. + *
  36183. + * Revision 1.114 2002/10/07 18:31:31 rgb
  36184. + * Set saref on incoming packets.
  36185. + *
  36186. + * Revision 1.113 2002/09/16 21:28:12 mcr
  36187. + * adjust hash length for HMAC calculation - must look at whether
  36188. + * it is MD5 or SHA1.
  36189. + *
  36190. + * Revision 1.112 2002/09/16 21:19:15 mcr
  36191. + * fixes for west-ah-icmp-01 - length of AH header must be
  36192. + * calculated properly, and next_header field properly copied.
  36193. + *
  36194. + * Revision 1.111 2002/09/10 02:45:56 mcr
  36195. + * re-factored the ipsec_rcv function into several functions,
  36196. + * ipsec_rcv_decap_once, and a set of functions for AH, ESP and IPCOMP.
  36197. + * In addition, the MD5 and SHA1 functions are replaced with pointers.
  36198. + *
  36199. + * Revision 1.110 2002/08/30 06:34:33 rgb
  36200. + * Fix scope of shift in AH header length check.
  36201. + *
  36202. + * Revision 1.109 2002/08/27 16:49:20 rgb
  36203. + * Fixed ESP short packet DOS (and AH and IPCOMP).
  36204. + *
  36205. + * Revision 1.108 2002/07/24 18:44:54 rgb
  36206. + * Type fiddling to tame ia64 compiler.
  36207. + *
  36208. + * Revision 1.107 2002/05/27 18:58:18 rgb
  36209. + * Convert to dynamic ipsec device allocation.
  36210. + * Remove final vistiges of tdb references via IPSEC_KLIPS1_COMPAT.
  36211. + *
  36212. + * Revision 1.106 2002/05/23 07:15:21 rgb
  36213. + * Pointer clean-up.
  36214. + * Added refcount code.
  36215. + *
  36216. + * Revision 1.105 2002/05/14 02:35:06 rgb
  36217. + * Change all references to tdb, TDB or Tunnel Descriptor Block to ips,
  36218. + * ipsec_sa or ipsec_sa.
  36219. + * Change references to _TDB to _IPSA.
  36220. + *
  36221. + * Revision 1.104 2002/04/24 07:55:32 mcr
  36222. + * #include patches and Makefiles for post-reorg compilation.
  36223. + *
  36224. + * Revision 1.103 2002/04/24 07:36:30 mcr
  36225. + * Moved from ./klips/net/ipsec/ipsec_rcv.c,v
  36226. + *
  36227. + * Revision 1.102 2002/01/29 17:17:56 mcr
  36228. + * moved include of ipsec_param.h to after include of linux/kernel.h
  36229. + * otherwise, it seems that some option that is set in ipsec_param.h
  36230. + * screws up something subtle in the include path to kernel.h, and
  36231. + * it complains on the snprintf() prototype.
  36232. + *
  36233. + * Revision 1.101 2002/01/29 04:00:52 mcr
  36234. + * more excise of kversions.h header.
  36235. + *
  36236. + * Revision 1.100 2002/01/29 02:13:17 mcr
  36237. + * introduction of ipsec_kversion.h means that include of
  36238. + * ipsec_param.h must preceed any decisions about what files to
  36239. + * include to deal with differences in kernel source.
  36240. + *
  36241. + * Revision 1.99 2002/01/28 21:40:59 mcr
  36242. + * should use #if to test boolean option rather than #ifdef.
  36243. + *
  36244. + * Revision 1.98 2002/01/20 20:19:36 mcr
  36245. + * renamed option to IP_FRAGMENT_LINEARIZE.
  36246. + *
  36247. + * Revision 1.97 2002/01/12 02:55:36 mcr
  36248. + * fix for post-2.4.4 to linearize skb's when ESP packet
  36249. + * was assembled from fragments.
  36250. + *
  36251. + * Revision 1.96 2001/11/26 09:23:49 rgb
  36252. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  36253. + *
  36254. + * Revision 1.93.2.2 2001/10/22 20:54:07 mcr
  36255. + * include des.h, removed phony prototypes and fixed calling
  36256. + * conventions to match real prototypes.
  36257. + *
  36258. + * Revision 1.93.2.1 2001/09/25 02:22:22 mcr
  36259. + * struct tdb -> struct ipsec_sa.
  36260. + * lifetime checks moved to ipsec_life.c
  36261. + * some sa(tdb) manipulation functions renamed.
  36262. + *
  36263. + * Revision 1.95 2001/11/06 19:49:07 rgb
  36264. + * Added variable descriptions.
  36265. + * Removed unauthenticated sequence==0 check to prevent DoS.
  36266. + *
  36267. + * Revision 1.94 2001/10/18 04:45:20 rgb
  36268. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  36269. + * lib/freeswan.h version macros moved to lib/kversions.h.
  36270. + * Other compiler directive cleanups.
  36271. + *
  36272. + * Revision 1.93 2001/09/07 22:17:24 rgb
  36273. + * Fix for removal of transport layer protocol handler arg in 2.4.4.
  36274. + * Fix to accomodate peer non-conformance to IPCOMP rfc2393.
  36275. + *
  36276. + * Revision 1.92 2001/08/27 19:44:41 rgb
  36277. + * Fix error in comment.
  36278. + *
  36279. + * Revision 1.91 2001/07/20 19:31:48 dhr
  36280. + * [DHR] fix source and destination subnets of policy in diagnostic
  36281. + *
  36282. + * Revision 1.90 2001/07/06 19:51:09 rgb
  36283. + * Added inbound policy checking code for IPIP SAs.
  36284. + * Renamed unused function argument for ease and intuitive naming.
  36285. + *
  36286. + * Revision 1.89 2001/06/22 19:35:23 rgb
  36287. + * Disable ipcomp processing if we are handed a ipcomp packet with no esp
  36288. + * or ah header.
  36289. + * Print protocol if we are handed a non-ipsec packet.
  36290. + *
  36291. + * Revision 1.88 2001/06/20 06:30:47 rgb
  36292. + * Fixed transport mode IPCOMP policy check bug.
  36293. + *
  36294. + * Revision 1.87 2001/06/13 20:58:40 rgb
  36295. + * Added parentheses around assignment used as truth value to silence
  36296. + * compiler.
  36297. + *
  36298. + * Revision 1.86 2001/06/07 22:25:23 rgb
  36299. + * Added a source address policy check for tunnel mode. It still does
  36300. + * not check client addresses and masks.
  36301. + * Only decapsulate IPIP if it is expected.
  36302. + *
  36303. + * Revision 1.85 2001/05/30 08:14:02 rgb
  36304. + * Removed vestiges of esp-null transforms.
  36305. + *
  36306. + * Revision 1.84 2001/05/27 06:12:11 rgb
  36307. + * Added structures for pid, packet count and last access time to eroute.
  36308. + * Added packet count to beginning of /proc/net/ipsec_eroute.
  36309. + *
  36310. + * Revision 1.83 2001/05/04 16:45:47 rgb
  36311. + * Remove unneeded code. ipp is not used after this point.
  36312. + *
  36313. + * Revision 1.82 2001/05/04 16:36:00 rgb
  36314. + * Fix skb_cow() call for 2.4.4. (SS)
  36315. + *
  36316. + * Revision 1.81 2001/05/02 14:46:53 rgb
  36317. + * Fix typo for compiler directive to pull IPH back.
  36318. + *
  36319. + * Revision 1.80 2001/04/30 19:46:34 rgb
  36320. + * Update for 2.4.4. We now receive the skb with skb->data pointing to
  36321. + * h.raw.
  36322. + *
  36323. + * Revision 1.79 2001/04/23 15:01:15 rgb
  36324. + * Added spin_lock() check to prevent double-locking for multiple
  36325. + * transforms and hence kernel lock-ups with SMP kernels.
  36326. + * Minor spin_unlock() adjustments to unlock before non-dependant prints
  36327. + * and IPSEC device stats updates.
  36328. + *
  36329. + * Revision 1.78 2001/04/21 23:04:24 rgb
  36330. + * Check if soft expire has already been sent before sending another to
  36331. + * prevent ACQUIRE flooding.
  36332. + *
  36333. + * Revision 1.77 2001/03/16 07:35:20 rgb
  36334. + * Ditch extra #if 1 around now permanent policy checking code.
  36335. + *
  36336. + * Revision 1.76 2001/02/27 22:24:54 rgb
  36337. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  36338. + * Check for satoa() return codes.
  36339. + *
  36340. + * Revision 1.75 2001/02/19 22:28:30 rgb
  36341. + * Minor change to virtual device discovery code to assert which I/F has
  36342. + * been found.
  36343. + *
  36344. + * Revision 1.74 2000/11/25 03:50:36 rgb
  36345. + * Oops fix by minor re-arrangement of code to avoid accessing a freed tdb.
  36346. + *
  36347. + * Revision 1.73 2000/11/09 20:52:15 rgb
  36348. + * More spinlock shuffling, locking earlier and unlocking later in rcv to
  36349. + * include ipcomp and prevent races, renaming some tdb variables that got
  36350. + * forgotten, moving some unlocks to include tdbs and adding a missing
  36351. + * unlock. Thanks to Svenning for some of these.
  36352. + *
  36353. + * Revision 1.72 2000/11/09 20:11:22 rgb
  36354. + * Minor shuffles to fix non-standard kernel config option selection.
  36355. + *
  36356. + * Revision 1.71 2000/11/06 04:36:18 rgb
  36357. + * Ditched spin_lock_irqsave in favour of spin_lock.
  36358. + * Minor initial protocol check rewrite.
  36359. + * Clean up debug printing.
  36360. + * Clean up tdb handling on ipcomp.
  36361. + * Fixed transport mode null pointer de-reference without ipcomp.
  36362. + * Add Svenning's adaptive content compression.
  36363. + * Disabled registration of ipcomp handler.
  36364. + *
  36365. + * Revision 1.70 2000/10/30 23:41:43 henry
  36366. + * Hans-Joerg Hoexer's null-pointer fix
  36367. + *
  36368. + * Revision 1.69 2000/10/10 18:54:16 rgb
  36369. + * Added a fix for incoming policy check with ipcomp enabled but
  36370. + * uncompressible.
  36371. + *
  36372. + * Revision 1.68 2000/09/22 17:53:12 rgb
  36373. + * Fixed ipcomp tdb pointers update for policy checking.
  36374. + *
  36375. + * Revision 1.67 2000/09/21 03:40:58 rgb
  36376. + * Added more debugging to try and track down the cpi outward copy problem.
  36377. + *
  36378. + * Revision 1.66 2000/09/20 04:00:10 rgb
  36379. + * Changed static functions to DEBUG_NO_STATIC to reveal function names for
  36380. + * debugging oopsen.
  36381. + *
  36382. + * Revision 1.65 2000/09/19 07:07:16 rgb
  36383. + * Added debugging to inbound policy check for ipcomp.
  36384. + * Added missing spin_unlocks (thanks Svenning!).
  36385. + * Fixed misplaced tdbnext pointers causing mismatched ipip policy check.
  36386. + * Protect ipcomp policy check following ipip decap with sysctl switch.
  36387. + *
  36388. + * Revision 1.64 2000/09/18 21:27:29 rgb
  36389. + * 2.0 fixes.
  36390. + *
  36391. + * Revision 1.63 2000/09/18 02:35:50 rgb
  36392. + * Added policy checking to ipcomp and re-enabled policy checking by
  36393. + * default.
  36394. + * Optimised satoa calls.
  36395. + *
  36396. + * Revision 1.62 2000/09/17 21:02:32 rgb
  36397. + * Clean up debugging, removing slow timestamp debug code.
  36398. + *
  36399. + * Revision 1.61 2000/09/16 01:07:55 rgb
  36400. + * Fixed erroneous ref from struct ipcomp to struct ipcomphdr.
  36401. + *
  36402. + * Revision 1.60 2000/09/15 11:37:01 rgb
  36403. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  36404. + * IPCOMP zlib deflate code.
  36405. + *
  36406. + * Revision 1.59 2000/09/15 04:56:20 rgb
  36407. + * Remove redundant satoa() call, reformat comment.
  36408. + *
  36409. + * Revision 1.58 2000/09/13 08:00:52 rgb
  36410. + * Flick on inbound policy checking.
  36411. + *
  36412. + * Revision 1.57 2000/09/12 03:22:19 rgb
  36413. + * Converted inbound_policy_check to sysctl.
  36414. + * Re-enabled policy backcheck.
  36415. + * Moved policy checks to top and within tdb lock.
  36416. + *
  36417. + * Revision 1.56 2000/09/08 19:12:56 rgb
  36418. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  36419. + *
  36420. + * Revision 1.55 2000/08/28 18:15:46 rgb
  36421. + * Added MB's nf-debug reset patch.
  36422. + *
  36423. + * Revision 1.54 2000/08/27 01:41:26 rgb
  36424. + * More minor tweaks to the bad padding debug code.
  36425. + *
  36426. + * Revision 1.53 2000/08/24 16:54:16 rgb
  36427. + * Added KLIPS_PRINTMORE macro to continue lines without KERN_INFO level
  36428. + * info.
  36429. + * Tidied up device reporting at the start of ipsec_rcv.
  36430. + * Tidied up bad padding debugging and processing.
  36431. + *
  36432. + * Revision 1.52 2000/08/20 21:36:03 rgb
  36433. + * Activated pfkey_expire() calls.
  36434. + * Added a hard/soft expiry parameter to pfkey_expire().
  36435. + * Added sanity checking to avoid propagating zero or smaller-length skbs
  36436. + * from a bogus decryption.
  36437. + * Re-arranged the order of soft and hard expiry to conform to RFC2367.
  36438. + * Clean up references to CONFIG_IPSEC_PFKEYv2.
  36439. + *
  36440. + * Revision 1.51 2000/08/18 21:23:30 rgb
  36441. + * Improve bad padding warning so that the printk buffer doesn't get
  36442. + * trampled.
  36443. + *
  36444. + * Revision 1.50 2000/08/01 14:51:51 rgb
  36445. + * Removed _all_ remaining traces of DES.
  36446. + *
  36447. + * Revision 1.49 2000/07/28 13:50:53 rgb
  36448. + * Changed enet_statistics to net_device_stats and added back compatibility
  36449. + * for pre-2.1.19.
  36450. + *
  36451. + * Revision 1.48 2000/05/10 19:14:40 rgb
  36452. + * Only check usetime against soft and hard limits if the tdb has been
  36453. + * used.
  36454. + * Cast output of ntohl so that the broken prototype doesn't make our
  36455. + * compile noisy.
  36456. + *
  36457. + * Revision 1.47 2000/05/09 17:45:43 rgb
  36458. + * Fix replay bitmap corruption bug upon receipt of bogus packet
  36459. + * with correct SPI. This was a DoS.
  36460. + *
  36461. + * Revision 1.46 2000/03/27 02:31:58 rgb
  36462. + * Fixed authentication failure printout bug.
  36463. + *
  36464. + * Revision 1.45 2000/03/22 16:15:37 rgb
  36465. + * Fixed renaming of dev_get (MB).
  36466. + *
  36467. + * Revision 1.44 2000/03/16 08:17:24 rgb
  36468. + * Hardcode PF_KEYv2 support.
  36469. + * Fixed minor bug checking AH header length.
  36470. + *
  36471. + * Revision 1.43 2000/03/14 12:26:59 rgb
  36472. + * Added skb->nfct support for clearing netfilter conntrack bits (MB).
  36473. + *
  36474. + * Revision 1.42 2000/01/26 10:04:04 rgb
  36475. + * Fixed inbound policy checking on transport mode bug.
  36476. + * Fixed noisy 2.0 printk arguments.
  36477. + *
  36478. + * Revision 1.41 2000/01/24 20:58:02 rgb
  36479. + * Improve debugging/reporting support for (disabled) inbound
  36480. + * policy checking.
  36481. + *
  36482. + * Revision 1.40 2000/01/22 23:20:10 rgb
  36483. + * Fixed up inboud policy checking code.
  36484. + * Cleaned out unused crud.
  36485. + *
  36486. + * Revision 1.39 2000/01/21 06:15:29 rgb
  36487. + * Added sanity checks on skb_push(), skb_pull() to prevent panics.
  36488. + * Fixed cut-and-paste debug_tunnel to debug_rcv.
  36489. + * Added inbound policy checking code, disabled.
  36490. + * Simplified output code by updating ipp to post-IPIP decapsulation.
  36491. + *
  36492. + * elided pre-2000 comments. Use "cvs log"
  36493. + *
  36494. + *
  36495. + * Local Variables:
  36496. + * c-set-style: linux
  36497. + * End:
  36498. + *
  36499. + */
  36500. --- /dev/null Tue Mar 11 13:02:56 2003
  36501. +++ linux/net/ipsec/ipsec_sa.c Mon Feb 9 13:51:03 2004
  36502. @@ -0,0 +1,1870 @@
  36503. +/*
  36504. + * Common routines for IPsec SA maintenance routines.
  36505. + *
  36506. + * Copyright (C) 1996, 1997 John Ioannidis.
  36507. + * Copyright (C) 1998, 1999, 2000, 2001, 2002 Richard Guy Briggs.
  36508. + *
  36509. + * This program is free software; you can redistribute it and/or modify it
  36510. + * under the terms of the GNU General Public License as published by the
  36511. + * Free Software Foundation; either version 2 of the License, or (at your
  36512. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  36513. + *
  36514. + * This program is distributed in the hope that it will be useful, but
  36515. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  36516. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  36517. + * for more details.
  36518. + *
  36519. + * RCSID $Id: ipsec_sa.c,v 1.30.2.2 2006/10/06 21:39:26 paul Exp $
  36520. + *
  36521. + * This is the file formerly known as "ipsec_xform.h"
  36522. + *
  36523. + */
  36524. +
  36525. +#ifndef AUTOCONF_INCLUDED
  36526. +#include <linux/config.h>
  36527. +#endif
  36528. +#include <linux/version.h>
  36529. +#include <linux/kernel.h> /* printk() */
  36530. +
  36531. +#include "openswan/ipsec_param.h"
  36532. +
  36533. +#ifdef MALLOC_SLAB
  36534. +# include <linux/slab.h> /* kmalloc() */
  36535. +#else /* MALLOC_SLAB */
  36536. +# include <linux/malloc.h> /* kmalloc() */
  36537. +#endif /* MALLOC_SLAB */
  36538. +#include <linux/vmalloc.h> /* vmalloc() */
  36539. +#include <linux/errno.h> /* error codes */
  36540. +#include <linux/types.h> /* size_t */
  36541. +#include <linux/interrupt.h> /* mark_bh */
  36542. +
  36543. +#include <linux/netdevice.h> /* struct device, and other headers */
  36544. +#include <linux/etherdevice.h> /* eth_type_trans */
  36545. +#include <linux/ip.h> /* struct iphdr */
  36546. +#include <linux/skbuff.h>
  36547. +#include <openswan.h>
  36548. +#ifdef SPINLOCK
  36549. +#ifdef SPINLOCK_23
  36550. +#include <linux/spinlock.h> /* *lock* */
  36551. +#else /* SPINLOCK_23 */
  36552. +#include <asm/spinlock.h> /* *lock* */
  36553. +#endif /* SPINLOCK_23 */
  36554. +#endif /* SPINLOCK */
  36555. +
  36556. +#include <net/ip.h>
  36557. +
  36558. +#include "openswan/radij.h"
  36559. +
  36560. +#include "openswan/ipsec_stats.h"
  36561. +#include "openswan/ipsec_life.h"
  36562. +#include "openswan/ipsec_sa.h"
  36563. +#include "openswan/ipsec_xform.h"
  36564. +
  36565. +#include "openswan/ipsec_encap.h"
  36566. +#include "openswan/ipsec_radij.h"
  36567. +#include "openswan/ipsec_xform.h"
  36568. +#include "openswan/ipsec_ipe4.h"
  36569. +#include "openswan/ipsec_ah.h"
  36570. +#include "openswan/ipsec_esp.h"
  36571. +
  36572. +#include <pfkeyv2.h>
  36573. +#include <pfkey.h>
  36574. +
  36575. +#include "openswan/ipsec_proto.h"
  36576. +#include "openswan/ipsec_alg.h"
  36577. +
  36578. +
  36579. +#ifdef CONFIG_KLIPS_DEBUG
  36580. +int debug_xform = 0;
  36581. +#endif /* CONFIG_KLIPS_DEBUG */
  36582. +
  36583. +#define SENDERR(_x) do { error = -(_x); goto errlab; } while (0)
  36584. +
  36585. +struct ipsec_sa *ipsec_sadb_hash[SADB_HASHMOD];
  36586. +#ifdef SPINLOCK
  36587. +spinlock_t tdb_lock = SPIN_LOCK_UNLOCKED;
  36588. +#else /* SPINLOCK */
  36589. +spinlock_t tdb_lock;
  36590. +#endif /* SPINLOCK */
  36591. +
  36592. +struct ipsec_sadb ipsec_sadb;
  36593. +
  36594. +#if IPSEC_SA_REF_CODE
  36595. +
  36596. +/* the sub table must be narrower (or equal) in bits than the variable type
  36597. + in the main table to count the number of unused entries in it. */
  36598. +typedef struct {
  36599. + int testSizeOf_refSubTable :
  36600. + ((sizeof(IPsecRefTableUnusedCount) * 8) < IPSEC_SA_REF_SUBTABLE_IDX_WIDTH ? -1 : 1);
  36601. +} dummy;
  36602. +
  36603. +
  36604. +/* The field where the saref will be hosted in the skb must be wide enough to
  36605. + accomodate the information it needs to store. */
  36606. +typedef struct {
  36607. + int testSizeOf_refField :
  36608. + (IPSEC_SA_REF_HOST_FIELD_WIDTH < IPSEC_SA_REF_TABLE_IDX_WIDTH ? -1 : 1 );
  36609. +} dummy2;
  36610. +
  36611. +
  36612. +#define IPS_HASH(said) (((said)->spi + (said)->dst.u.v4.sin_addr.s_addr + (said)->proto) % SADB_HASHMOD)
  36613. +
  36614. +
  36615. +void
  36616. +ipsec_SAtest(void)
  36617. +{
  36618. + IPsecSAref_t SAref = 258;
  36619. + struct ipsec_sa ips;
  36620. + ips.ips_ref = 772;
  36621. +
  36622. + printk("klips_debug:ipsec_SAtest: "
  36623. + "IPSEC_SA_REF_SUBTABLE_IDX_WIDTH=%u\n"
  36624. + "IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES=%u\n"
  36625. + "IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES=%u\n"
  36626. + "IPSEC_SA_REF_HOST_FIELD_WIDTH=%lu\n"
  36627. + "IPSEC_SA_REF_TABLE_MASK=%x\n"
  36628. + "IPSEC_SA_REF_ENTRY_MASK=%x\n"
  36629. + "IPsecSAref2table(%d)=%u\n"
  36630. + "IPsecSAref2entry(%d)=%u\n"
  36631. + "IPsecSAref2NFmark(%d)=%u\n"
  36632. + "IPsecSAref2SA(%d)=%p\n"
  36633. + "IPsecSA2SAref(%p)=%d\n"
  36634. + ,
  36635. + IPSEC_SA_REF_SUBTABLE_IDX_WIDTH,
  36636. + IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES,
  36637. + IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES,
  36638. + (unsigned long) IPSEC_SA_REF_HOST_FIELD_WIDTH,
  36639. + IPSEC_SA_REF_TABLE_MASK,
  36640. + IPSEC_SA_REF_ENTRY_MASK,
  36641. + SAref, IPsecSAref2table(SAref),
  36642. + SAref, IPsecSAref2entry(SAref),
  36643. + SAref, IPsecSAref2NFmark(SAref),
  36644. + SAref, IPsecSAref2SA(SAref),
  36645. + (&ips), IPsecSA2SAref((&ips))
  36646. + );
  36647. + return;
  36648. +}
  36649. +
  36650. +int
  36651. +ipsec_SAref_recycle(void)
  36652. +{
  36653. + int table;
  36654. + int entry;
  36655. + int error = 0;
  36656. +
  36657. + ipsec_sadb.refFreeListHead = -1;
  36658. + ipsec_sadb.refFreeListTail = -1;
  36659. +
  36660. + if(ipsec_sadb.refFreeListCont == IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES * IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES) {
  36661. + KLIPS_PRINT(debug_xform,
  36662. + "klips_debug:ipsec_SAref_recycle: "
  36663. + "end of table reached, continuing at start..\n");
  36664. + ipsec_sadb.refFreeListCont = 0;
  36665. + }
  36666. +
  36667. + KLIPS_PRINT(debug_xform,
  36668. + "klips_debug:ipsec_SAref_recycle: "
  36669. + "recycling, continuing from SAref=%d (0p%p), table=%d, entry=%d.\n",
  36670. + ipsec_sadb.refFreeListCont,
  36671. + (ipsec_sadb.refTable[IPsecSAref2table(ipsec_sadb.refFreeListCont)] != NULL) ? IPsecSAref2SA(ipsec_sadb.refFreeListCont) : NULL,
  36672. + IPsecSAref2table(ipsec_sadb.refFreeListCont),
  36673. + IPsecSAref2entry(ipsec_sadb.refFreeListCont));
  36674. +
  36675. + for(table = IPsecSAref2table(ipsec_sadb.refFreeListCont);
  36676. + table < IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES;
  36677. + table++) {
  36678. + if(ipsec_sadb.refTable[table] == NULL) {
  36679. + error = ipsec_SArefSubTable_alloc(table);
  36680. + if(error) {
  36681. + return error;
  36682. + }
  36683. + }
  36684. + for(entry = IPsecSAref2entry(ipsec_sadb.refFreeListCont);
  36685. + entry < IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES;
  36686. + entry++) {
  36687. + if(ipsec_sadb.refTable[table]->entry[entry] == NULL) {
  36688. + ipsec_sadb.refFreeList[++ipsec_sadb.refFreeListTail] = IPsecSArefBuild(table, entry);
  36689. + if(ipsec_sadb.refFreeListTail == (IPSEC_SA_REF_FREELIST_NUM_ENTRIES - 1)) {
  36690. + ipsec_sadb.refFreeListHead = 0;
  36691. + ipsec_sadb.refFreeListCont = ipsec_sadb.refFreeList[ipsec_sadb.refFreeListTail] + 1;
  36692. + KLIPS_PRINT(debug_xform,
  36693. + "klips_debug:ipsec_SAref_recycle: "
  36694. + "SArefFreeList refilled.\n");
  36695. + return 0;
  36696. + }
  36697. + }
  36698. + }
  36699. + }
  36700. +
  36701. + if(ipsec_sadb.refFreeListTail == -1) {
  36702. + KLIPS_PRINT(debug_xform,
  36703. + "klips_debug:ipsec_SAref_recycle: "
  36704. + "out of room in the SArefTable.\n");
  36705. +
  36706. + return(-ENOSPC);
  36707. + }
  36708. +
  36709. + ipsec_sadb.refFreeListHead = 0;
  36710. + ipsec_sadb.refFreeListCont = ipsec_sadb.refFreeList[ipsec_sadb.refFreeListTail] + 1;
  36711. + KLIPS_PRINT(debug_xform,
  36712. + "klips_debug:ipsec_SAref_recycle: "
  36713. + "SArefFreeList partly refilled to %d of %d.\n",
  36714. + ipsec_sadb.refFreeListTail,
  36715. + IPSEC_SA_REF_FREELIST_NUM_ENTRIES);
  36716. + return 0;
  36717. +}
  36718. +
  36719. +int
  36720. +ipsec_SArefSubTable_alloc(unsigned table)
  36721. +{
  36722. + unsigned entry;
  36723. + struct IPsecSArefSubTable* SArefsub;
  36724. +
  36725. + KLIPS_PRINT(debug_xform,
  36726. + "klips_debug:ipsec_SArefSubTable_alloc: "
  36727. + "allocating %lu bytes for table %u of %u.\n",
  36728. + (unsigned long) (IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES * sizeof(struct ipsec_sa *)),
  36729. + table,
  36730. + IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES);
  36731. +
  36732. + /* allocate another sub-table */
  36733. + SArefsub = vmalloc(IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES * sizeof(struct ipsec_sa *));
  36734. + if(SArefsub == NULL) {
  36735. + KLIPS_PRINT(debug_xform,
  36736. + "klips_debug:ipsec_SArefSubTable_alloc: "
  36737. + "error allocating memory for table %u of %u!\n",
  36738. + table,
  36739. + IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES);
  36740. + return -ENOMEM;
  36741. + }
  36742. +
  36743. + /* add this sub-table to the main table */
  36744. + ipsec_sadb.refTable[table] = SArefsub;
  36745. +
  36746. + /* initialise each element to NULL */
  36747. + KLIPS_PRINT(debug_xform,
  36748. + "klips_debug:ipsec_SArefSubTable_alloc: "
  36749. + "initialising %u elements (2 ^ %u) of table %u.\n",
  36750. + IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES,
  36751. + IPSEC_SA_REF_SUBTABLE_IDX_WIDTH,
  36752. + table);
  36753. + for(entry = 0; entry < IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES; entry++) {
  36754. + SArefsub->entry[entry] = NULL;
  36755. + }
  36756. +
  36757. + return 0;
  36758. +}
  36759. +#endif /* IPSEC_SA_REF_CODE */
  36760. +
  36761. +int
  36762. +ipsec_saref_freelist_init(void)
  36763. +{
  36764. + int i;
  36765. +
  36766. + KLIPS_PRINT(debug_xform,
  36767. + "klips_debug:ipsec_saref_freelist_init: "
  36768. + "initialising %u elements of FreeList.\n",
  36769. + IPSEC_SA_REF_FREELIST_NUM_ENTRIES);
  36770. +
  36771. + for(i = 0; i < IPSEC_SA_REF_FREELIST_NUM_ENTRIES; i++) {
  36772. + ipsec_sadb.refFreeList[i] = IPSEC_SAREF_NULL;
  36773. + }
  36774. + ipsec_sadb.refFreeListHead = -1;
  36775. + ipsec_sadb.refFreeListCont = 0;
  36776. + ipsec_sadb.refFreeListTail = -1;
  36777. +
  36778. + return 0;
  36779. +}
  36780. +
  36781. +int
  36782. +ipsec_sadb_init(void)
  36783. +{
  36784. + int error = 0;
  36785. + unsigned i;
  36786. +
  36787. + for(i = 0; i < SADB_HASHMOD; i++) {
  36788. + ipsec_sadb_hash[i] = NULL;
  36789. + }
  36790. + /* parts above are for the old style SADB hash table */
  36791. +
  36792. +
  36793. +#if IPSEC_SA_REF_CODE
  36794. + /* initialise SA reference table */
  36795. +
  36796. + /* initialise the main table */
  36797. + KLIPS_PRINT(debug_xform,
  36798. + "klips_debug:ipsec_sadb_init: "
  36799. + "initialising main table of size %u (2 ^ %u).\n",
  36800. + IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES,
  36801. + IPSEC_SA_REF_MAINTABLE_IDX_WIDTH);
  36802. + {
  36803. + unsigned table;
  36804. + for(table = 0; table < IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES; table++) {
  36805. + ipsec_sadb.refTable[table] = NULL;
  36806. + }
  36807. + }
  36808. +
  36809. + /* allocate the first sub-table */
  36810. + error = ipsec_SArefSubTable_alloc(0);
  36811. + if(error) {
  36812. + return error;
  36813. + }
  36814. +
  36815. + error = ipsec_saref_freelist_init();
  36816. +#endif /* IPSEC_SA_REF_CODE */
  36817. + return error;
  36818. +}
  36819. +
  36820. +#if IPSEC_SA_REF_CODE
  36821. +IPsecSAref_t
  36822. +ipsec_SAref_alloc(int*error) /* pass in error var by pointer */
  36823. +{
  36824. + IPsecSAref_t SAref;
  36825. +
  36826. + KLIPS_PRINT(debug_xform,
  36827. + "klips_debug:ipsec_SAref_alloc: "
  36828. + "SAref requested... head=%d, cont=%d, tail=%d, listsize=%d.\n",
  36829. + ipsec_sadb.refFreeListHead,
  36830. + ipsec_sadb.refFreeListCont,
  36831. + ipsec_sadb.refFreeListTail,
  36832. + IPSEC_SA_REF_FREELIST_NUM_ENTRIES);
  36833. +
  36834. + if(ipsec_sadb.refFreeListHead == -1) {
  36835. + KLIPS_PRINT(debug_xform,
  36836. + "klips_debug:ipsec_SAref_alloc: "
  36837. + "FreeList empty, recycling...\n");
  36838. + *error = ipsec_SAref_recycle();
  36839. + if(*error) {
  36840. + return IPSEC_SAREF_NULL;
  36841. + }
  36842. + }
  36843. +
  36844. + SAref = ipsec_sadb.refFreeList[ipsec_sadb.refFreeListHead];
  36845. + if(SAref == IPSEC_SAREF_NULL) {
  36846. + KLIPS_PRINT(debug_xform,
  36847. + "klips_debug:ipsec_SAref_alloc: "
  36848. + "unexpected error, refFreeListHead = %d points to invalid entry.\n",
  36849. + ipsec_sadb.refFreeListHead);
  36850. + *error = -ESPIPE;
  36851. + return IPSEC_SAREF_NULL;
  36852. + }
  36853. +
  36854. + KLIPS_PRINT(debug_xform,
  36855. + "klips_debug:ipsec_SAref_alloc: "
  36856. + "allocating SAref=%d, table=%u, entry=%u of %u.\n",
  36857. + SAref,
  36858. + IPsecSAref2table(SAref),
  36859. + IPsecSAref2entry(SAref),
  36860. + IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES * IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES);
  36861. +
  36862. + ipsec_sadb.refFreeList[ipsec_sadb.refFreeListHead] = IPSEC_SAREF_NULL;
  36863. + ipsec_sadb.refFreeListHead++;
  36864. + if(ipsec_sadb.refFreeListHead > ipsec_sadb.refFreeListTail) {
  36865. + KLIPS_PRINT(debug_xform,
  36866. + "klips_debug:ipsec_SAref_alloc: "
  36867. + "last FreeList entry allocated, resetting list head to empty.\n");
  36868. + ipsec_sadb.refFreeListHead = -1;
  36869. + }
  36870. +
  36871. + return SAref;
  36872. +}
  36873. +#endif /* IPSEC_SA_REF_CODE */
  36874. +
  36875. +int
  36876. +ipsec_sa_print(struct ipsec_sa *ips)
  36877. +{
  36878. + char sa[SATOT_BUF];
  36879. + size_t sa_len;
  36880. +
  36881. + printk(KERN_INFO "klips_debug: SA:");
  36882. + if(ips == NULL) {
  36883. + printk("NULL\n");
  36884. + return -ENOENT;
  36885. + }
  36886. + printk(" ref=%d", ips->ips_ref);
  36887. + printk(" refcount=%d", atomic_read(&ips->ips_refcount));
  36888. + if(ips->ips_hnext != NULL) {
  36889. + printk(" hnext=0p%p", ips->ips_hnext);
  36890. + }
  36891. + if(ips->ips_inext != NULL) {
  36892. + printk(" inext=0p%p", ips->ips_inext);
  36893. + }
  36894. + if(ips->ips_onext != NULL) {
  36895. + printk(" onext=0p%p", ips->ips_onext);
  36896. + }
  36897. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  36898. + printk(" said=%s", sa_len ? sa : " (error)");
  36899. + if(ips->ips_seq) {
  36900. + printk(" seq=%u", ips->ips_seq);
  36901. + }
  36902. + if(ips->ips_pid) {
  36903. + printk(" pid=%u", ips->ips_pid);
  36904. + }
  36905. + if(ips->ips_authalg) {
  36906. + printk(" authalg=%u", ips->ips_authalg);
  36907. + }
  36908. + if(ips->ips_encalg) {
  36909. + printk(" encalg=%u", ips->ips_encalg);
  36910. + }
  36911. + printk(" XFORM=%s%s%s", IPS_XFORM_NAME(ips));
  36912. + if(ips->ips_replaywin) {
  36913. + printk(" ooowin=%u", ips->ips_replaywin);
  36914. + }
  36915. + if(ips->ips_flags) {
  36916. + printk(" flags=%u", ips->ips_flags);
  36917. + }
  36918. + if(ips->ips_addr_s) {
  36919. + char buf[SUBNETTOA_BUF];
  36920. + addrtoa(((struct sockaddr_in*)(ips->ips_addr_s))->sin_addr,
  36921. + 0, buf, sizeof(buf));
  36922. + printk(" src=%s", buf);
  36923. + }
  36924. + if(ips->ips_addr_d) {
  36925. + char buf[SUBNETTOA_BUF];
  36926. + addrtoa(((struct sockaddr_in*)(ips->ips_addr_s))->sin_addr,
  36927. + 0, buf, sizeof(buf));
  36928. + printk(" dst=%s", buf);
  36929. + }
  36930. + if(ips->ips_addr_p) {
  36931. + char buf[SUBNETTOA_BUF];
  36932. + addrtoa(((struct sockaddr_in*)(ips->ips_addr_p))->sin_addr,
  36933. + 0, buf, sizeof(buf));
  36934. + printk(" proxy=%s", buf);
  36935. + }
  36936. + if(ips->ips_key_bits_a) {
  36937. + printk(" key_bits_a=%u", ips->ips_key_bits_a);
  36938. + }
  36939. + if(ips->ips_key_bits_e) {
  36940. + printk(" key_bits_e=%u", ips->ips_key_bits_e);
  36941. + }
  36942. +
  36943. + printk("\n");
  36944. + return 0;
  36945. +}
  36946. +
  36947. +struct ipsec_sa*
  36948. +ipsec_sa_alloc(int*error) /* pass in error var by pointer */
  36949. +{
  36950. + struct ipsec_sa* ips;
  36951. +
  36952. + if((ips = kmalloc(sizeof(*ips), GFP_ATOMIC) ) == NULL) {
  36953. + KLIPS_PRINT(debug_xform,
  36954. + "klips_debug:ipsec_sa_alloc: "
  36955. + "memory allocation error\n");
  36956. + *error = -ENOMEM;
  36957. + return NULL;
  36958. + }
  36959. + memset((caddr_t)ips, 0, sizeof(*ips));
  36960. +#if IPSEC_SA_REF_CODE
  36961. + ips->ips_ref = ipsec_SAref_alloc(error); /* pass in error return by pointer */
  36962. + KLIPS_PRINT(debug_xform,
  36963. + "klips_debug:ipsec_sa_alloc: "
  36964. + "allocated %lu bytes for ipsec_sa struct=0p%p ref=%d.\n",
  36965. + (unsigned long) sizeof(*ips),
  36966. + ips,
  36967. + ips->ips_ref);
  36968. + if(ips->ips_ref == IPSEC_SAREF_NULL) {
  36969. + kfree(ips);
  36970. + KLIPS_PRINT(debug_xform,
  36971. + "klips_debug:ipsec_sa_alloc: "
  36972. + "SAref allocation error\n");
  36973. + return NULL;
  36974. + }
  36975. +
  36976. + atomic_inc(&ips->ips_refcount);
  36977. + IPsecSAref2SA(ips->ips_ref) = ips;
  36978. +#endif /* IPSEC_SA_REF_CODE */
  36979. +
  36980. + *error = 0;
  36981. + return(ips);
  36982. +}
  36983. +
  36984. +int
  36985. +ipsec_sa_free(struct ipsec_sa* ips)
  36986. +{
  36987. + return ipsec_sa_wipe(ips);
  36988. +}
  36989. +
  36990. +struct ipsec_sa *
  36991. +ipsec_sa_getbyid(ip_said *said)
  36992. +{
  36993. + int hashval;
  36994. + struct ipsec_sa *ips;
  36995. + char sa[SATOT_BUF];
  36996. + size_t sa_len;
  36997. +
  36998. + if(said == NULL) {
  36999. + KLIPS_PRINT(debug_xform,
  37000. + "klips_error:ipsec_sa_getbyid: "
  37001. + "null pointer passed in!\n");
  37002. + return NULL;
  37003. + }
  37004. +
  37005. + sa_len = satot(said, 0, sa, sizeof(sa));
  37006. +
  37007. + hashval = IPS_HASH(said);
  37008. +
  37009. + KLIPS_PRINT(debug_xform,
  37010. + "klips_debug:ipsec_sa_getbyid: "
  37011. + "linked entry in ipsec_sa table for hash=%d of SA:%s requested.\n",
  37012. + hashval,
  37013. + sa_len ? sa : " (error)");
  37014. +
  37015. + if((ips = ipsec_sadb_hash[hashval]) == NULL) {
  37016. + KLIPS_PRINT(debug_xform,
  37017. + "klips_debug:ipsec_sa_getbyid: "
  37018. + "no entries in ipsec_sa table for hash=%d of SA:%s.\n",
  37019. + hashval,
  37020. + sa_len ? sa : " (error)");
  37021. + return NULL;
  37022. + }
  37023. +
  37024. + for (; ips; ips = ips->ips_hnext) {
  37025. + if ((ips->ips_said.spi == said->spi) &&
  37026. + (ips->ips_said.dst.u.v4.sin_addr.s_addr == said->dst.u.v4.sin_addr.s_addr) &&
  37027. + (ips->ips_said.proto == said->proto)) {
  37028. + atomic_inc(&ips->ips_refcount);
  37029. + return ips;
  37030. + }
  37031. + }
  37032. +
  37033. + KLIPS_PRINT(debug_xform,
  37034. + "klips_debug:ipsec_sa_getbyid: "
  37035. + "no entry in linked list for hash=%d of SA:%s.\n",
  37036. + hashval,
  37037. + sa_len ? sa : " (error)");
  37038. + return NULL;
  37039. +}
  37040. +
  37041. +int
  37042. +ipsec_sa_put(struct ipsec_sa *ips)
  37043. +{
  37044. + char sa[SATOT_BUF];
  37045. + size_t sa_len;
  37046. +
  37047. + if(ips == NULL) {
  37048. + KLIPS_PRINT(debug_xform,
  37049. + "klips_error:ipsec_sa_put: "
  37050. + "null pointer passed in!\n");
  37051. + return -1;
  37052. + }
  37053. +
  37054. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37055. +
  37056. + KLIPS_PRINT(debug_xform,
  37057. + "klips_debug:ipsec_sa_put: "
  37058. + "ipsec_sa SA:%s, ref:%d reference count decremented.\n",
  37059. + sa_len ? sa : " (error)",
  37060. + ips->ips_ref);
  37061. +
  37062. + atomic_dec(&ips->ips_refcount);
  37063. +
  37064. + return 0;
  37065. +}
  37066. +
  37067. +/*
  37068. + The ipsec_sa table better *NOT* be locked before it is handed in, or SMP locks will happen
  37069. +*/
  37070. +int
  37071. +ipsec_sa_add(struct ipsec_sa *ips)
  37072. +{
  37073. + int error = 0;
  37074. + unsigned int hashval;
  37075. +
  37076. + if(ips == NULL) {
  37077. + KLIPS_PRINT(debug_xform,
  37078. + "klips_error:ipsec_sa_add: "
  37079. + "null pointer passed in!\n");
  37080. + return -ENODATA;
  37081. + }
  37082. + hashval = IPS_HASH(&ips->ips_said);
  37083. +
  37084. + atomic_inc(&ips->ips_refcount);
  37085. + spin_lock_bh(&tdb_lock);
  37086. +
  37087. + ips->ips_hnext = ipsec_sadb_hash[hashval];
  37088. + ipsec_sadb_hash[hashval] = ips;
  37089. +
  37090. + spin_unlock_bh(&tdb_lock);
  37091. +
  37092. + return error;
  37093. +}
  37094. +
  37095. +/*
  37096. + The ipsec_sa table better be locked before it is handed in, or races might happen
  37097. +*/
  37098. +int
  37099. +ipsec_sa_del(struct ipsec_sa *ips)
  37100. +{
  37101. + unsigned int hashval;
  37102. + struct ipsec_sa *ipstp;
  37103. + char sa[SATOT_BUF];
  37104. + size_t sa_len;
  37105. +
  37106. + if(ips == NULL) {
  37107. + KLIPS_PRINT(debug_xform,
  37108. + "klips_error:ipsec_sa_del: "
  37109. + "null pointer passed in!\n");
  37110. + return -ENODATA;
  37111. + }
  37112. +
  37113. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37114. + if(ips->ips_inext || ips->ips_onext) {
  37115. + KLIPS_PRINT(debug_xform,
  37116. + "klips_error:ipsec_sa_del: "
  37117. + "SA:%s still linked!\n",
  37118. + sa_len ? sa : " (error)");
  37119. + return -EMLINK;
  37120. + }
  37121. +
  37122. + hashval = IPS_HASH(&ips->ips_said);
  37123. +
  37124. + KLIPS_PRINT(debug_xform,
  37125. + "klips_debug:ipsec_sa_del: "
  37126. + "deleting SA:%s, hashval=%d.\n",
  37127. + sa_len ? sa : " (error)",
  37128. + hashval);
  37129. + if(ipsec_sadb_hash[hashval] == NULL) {
  37130. + KLIPS_PRINT(debug_xform,
  37131. + "klips_debug:ipsec_sa_del: "
  37132. + "no entries in ipsec_sa table for hash=%d of SA:%s.\n",
  37133. + hashval,
  37134. + sa_len ? sa : " (error)");
  37135. + return -ENOENT;
  37136. + }
  37137. +
  37138. + if (ips == ipsec_sadb_hash[hashval]) {
  37139. + ipsec_sadb_hash[hashval] = ipsec_sadb_hash[hashval]->ips_hnext;
  37140. + ips->ips_hnext = NULL;
  37141. + atomic_dec(&ips->ips_refcount);
  37142. + KLIPS_PRINT(debug_xform,
  37143. + "klips_debug:ipsec_sa_del: "
  37144. + "successfully deleted first ipsec_sa in chain.\n");
  37145. + return 0;
  37146. + } else {
  37147. + for (ipstp = ipsec_sadb_hash[hashval];
  37148. + ipstp;
  37149. + ipstp = ipstp->ips_hnext) {
  37150. + if (ipstp->ips_hnext == ips) {
  37151. + ipstp->ips_hnext = ips->ips_hnext;
  37152. + ips->ips_hnext = NULL;
  37153. + atomic_dec(&ips->ips_refcount);
  37154. + KLIPS_PRINT(debug_xform,
  37155. + "klips_debug:ipsec_sa_del: "
  37156. + "successfully deleted link in ipsec_sa chain.\n");
  37157. + return 0;
  37158. + }
  37159. + }
  37160. + }
  37161. +
  37162. + KLIPS_PRINT(debug_xform,
  37163. + "klips_debug:ipsec_sa_del: "
  37164. + "no entries in linked list for hash=%d of SA:%s.\n",
  37165. + hashval,
  37166. + sa_len ? sa : " (error)");
  37167. + return -ENOENT;
  37168. +}
  37169. +
  37170. +/*
  37171. + The ipsec_sa table better be locked before it is handed in, or races
  37172. + might happen
  37173. +*/
  37174. +int
  37175. +ipsec_sa_delchain(struct ipsec_sa *ips)
  37176. +{
  37177. + struct ipsec_sa *ipsdel;
  37178. + int error = 0;
  37179. + char sa[SATOT_BUF];
  37180. + size_t sa_len;
  37181. +
  37182. + if(ips == NULL) {
  37183. + KLIPS_PRINT(debug_xform,
  37184. + "klips_error:ipsec_sa_delchain: "
  37185. + "null pointer passed in!\n");
  37186. + return -ENODATA;
  37187. + }
  37188. +
  37189. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37190. + KLIPS_PRINT(debug_xform,
  37191. + "klips_debug:ipsec_sa_delchain: "
  37192. + "passed SA:%s\n",
  37193. + sa_len ? sa : " (error)");
  37194. + while(ips->ips_onext != NULL) {
  37195. + ips = ips->ips_onext;
  37196. + }
  37197. +
  37198. + while(ips) {
  37199. + /* XXX send a pfkey message up to advise of deleted ipsec_sa */
  37200. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37201. + KLIPS_PRINT(debug_xform,
  37202. + "klips_debug:ipsec_sa_delchain: "
  37203. + "unlinking and delting SA:%s",
  37204. + sa_len ? sa : " (error)");
  37205. + ipsdel = ips;
  37206. + ips = ips->ips_inext;
  37207. + if(ips != NULL) {
  37208. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37209. + KLIPS_PRINT(debug_xform,
  37210. + ", inext=%s",
  37211. + sa_len ? sa : " (error)");
  37212. + atomic_dec(&ipsdel->ips_refcount);
  37213. + ipsdel->ips_inext = NULL;
  37214. + atomic_dec(&ips->ips_refcount);
  37215. + ips->ips_onext = NULL;
  37216. + }
  37217. + KLIPS_PRINT(debug_xform,
  37218. + ".\n");
  37219. + if((error = ipsec_sa_del(ipsdel))) {
  37220. + KLIPS_PRINT(debug_xform,
  37221. + "klips_debug:ipsec_sa_delchain: "
  37222. + "ipsec_sa_del returned error %d.\n", -error);
  37223. + return error;
  37224. + }
  37225. + if((error = ipsec_sa_wipe(ipsdel))) {
  37226. + KLIPS_PRINT(debug_xform,
  37227. + "klips_debug:ipsec_sa_delchain: "
  37228. + "ipsec_sa_wipe returned error %d.\n", -error);
  37229. + return error;
  37230. + }
  37231. + }
  37232. + return error;
  37233. +}
  37234. +
  37235. +int
  37236. +ipsec_sadb_cleanup(__u8 proto)
  37237. +{
  37238. + unsigned i;
  37239. + int error = 0;
  37240. + struct ipsec_sa *ips, **ipsprev, *ipsdel;
  37241. + char sa[SATOT_BUF];
  37242. + size_t sa_len;
  37243. +
  37244. + KLIPS_PRINT(debug_xform,
  37245. + "klips_debug:ipsec_sadb_cleanup: "
  37246. + "cleaning up proto=%d.\n",
  37247. + proto);
  37248. +
  37249. + spin_lock_bh(&tdb_lock);
  37250. +
  37251. + for (i = 0; i < SADB_HASHMOD; i++) {
  37252. + ipsprev = &(ipsec_sadb_hash[i]);
  37253. + ips = ipsec_sadb_hash[i];
  37254. + if(ips != NULL) {
  37255. + atomic_inc(&ips->ips_refcount);
  37256. + }
  37257. + for(; ips != NULL;) {
  37258. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37259. + KLIPS_PRINT(debug_xform,
  37260. + "klips_debug:ipsec_sadb_cleanup: "
  37261. + "checking SA:%s, hash=%d, ref=%d",
  37262. + sa_len ? sa : " (error)",
  37263. + i,
  37264. + ips->ips_ref);
  37265. + ipsdel = ips;
  37266. + ips = ipsdel->ips_hnext;
  37267. + if(ips != NULL) {
  37268. + atomic_inc(&ips->ips_refcount);
  37269. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37270. + KLIPS_PRINT(debug_xform,
  37271. + ", hnext=%s",
  37272. + sa_len ? sa : " (error)");
  37273. + }
  37274. + if(*ipsprev != NULL) {
  37275. + sa_len = satot(&(*ipsprev)->ips_said, 0, sa, sizeof(sa));
  37276. + KLIPS_PRINT(debug_xform,
  37277. + ", *ipsprev=%s",
  37278. + sa_len ? sa : " (error)");
  37279. + if((*ipsprev)->ips_hnext) {
  37280. + sa_len = satot(&(*ipsprev)->ips_hnext->ips_said, 0, sa, sizeof(sa));
  37281. + KLIPS_PRINT(debug_xform,
  37282. + ", *ipsprev->ips_hnext=%s",
  37283. + sa_len ? sa : " (error)");
  37284. + }
  37285. + }
  37286. + KLIPS_PRINT(debug_xform,
  37287. + ".\n");
  37288. + if(proto == 0 || (proto == ipsdel->ips_said.proto)) {
  37289. + sa_len = satot(&ipsdel->ips_said, 0, sa, sizeof(sa));
  37290. + KLIPS_PRINT(debug_xform,
  37291. + "klips_debug:ipsec_sadb_cleanup: "
  37292. + "deleting SA chain:%s.\n",
  37293. + sa_len ? sa : " (error)");
  37294. + if((error = ipsec_sa_delchain(ipsdel))) {
  37295. + SENDERR(-error);
  37296. + }
  37297. + ipsprev = &(ipsec_sadb_hash[i]);
  37298. + ips = ipsec_sadb_hash[i];
  37299. +
  37300. + KLIPS_PRINT(debug_xform,
  37301. + "klips_debug:ipsec_sadb_cleanup: "
  37302. + "deleted SA chain:%s",
  37303. + sa_len ? sa : " (error)");
  37304. + if(ips != NULL) {
  37305. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37306. + KLIPS_PRINT(debug_xform,
  37307. + ", ipsec_sadb_hash[%d]=%s",
  37308. + i,
  37309. + sa_len ? sa : " (error)");
  37310. + }
  37311. + if(*ipsprev != NULL) {
  37312. + sa_len = satot(&(*ipsprev)->ips_said, 0, sa, sizeof(sa));
  37313. + KLIPS_PRINT(debug_xform,
  37314. + ", *ipsprev=%s",
  37315. + sa_len ? sa : " (error)");
  37316. + if((*ipsprev)->ips_hnext != NULL) {
  37317. + sa_len = satot(&(*ipsprev)->ips_hnext->ips_said, 0, sa, sizeof(sa));
  37318. + KLIPS_PRINT(debug_xform,
  37319. + ", *ipsprev->ips_hnext=%s",
  37320. + sa_len ? sa : " (error)");
  37321. + }
  37322. + }
  37323. + KLIPS_PRINT(debug_xform,
  37324. + ".\n");
  37325. + } else {
  37326. + ipsprev = &ipsdel;
  37327. + }
  37328. + if(ipsdel != NULL) {
  37329. + ipsec_sa_put(ipsdel);
  37330. + }
  37331. + }
  37332. + }
  37333. + errlab:
  37334. +
  37335. + spin_unlock_bh(&tdb_lock);
  37336. +
  37337. +
  37338. +#if IPSEC_SA_REF_CODE
  37339. + /* clean up SA reference table */
  37340. +
  37341. + /* go through the ref table and clean out all the SAs */
  37342. + KLIPS_PRINT(debug_xform,
  37343. + "klips_debug:ipsec_sadb_cleanup: "
  37344. + "removing SAref entries and tables.");
  37345. + {
  37346. + unsigned table, entry;
  37347. + for(table = 0; table < IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES; table++) {
  37348. + KLIPS_PRINT(debug_xform,
  37349. + "klips_debug:ipsec_sadb_cleanup: "
  37350. + "cleaning SAref table=%u.\n",
  37351. + table);
  37352. + if(ipsec_sadb.refTable[table] == NULL) {
  37353. + printk("\n");
  37354. + KLIPS_PRINT(debug_xform,
  37355. + "klips_debug:ipsec_sadb_cleanup: "
  37356. + "cleaned %u used refTables.\n",
  37357. + table);
  37358. + break;
  37359. + }
  37360. + for(entry = 0; entry < IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES; entry++) {
  37361. + if(ipsec_sadb.refTable[table]->entry[entry] != NULL) {
  37362. + ipsec_sa_delchain(ipsec_sadb.refTable[table]->entry[entry]);
  37363. + ipsec_sadb.refTable[table]->entry[entry] = NULL;
  37364. + }
  37365. + }
  37366. + }
  37367. + }
  37368. +#endif /* IPSEC_SA_REF_CODE */
  37369. +
  37370. + return(error);
  37371. +}
  37372. +
  37373. +int
  37374. +ipsec_sadb_free(void)
  37375. +{
  37376. + int error = 0;
  37377. +
  37378. + KLIPS_PRINT(debug_xform,
  37379. + "klips_debug:ipsec_sadb_free: "
  37380. + "freeing SArefTable memory.\n");
  37381. +
  37382. + /* clean up SA reference table */
  37383. +
  37384. + /* go through the ref table and clean out all the SAs if any are
  37385. + left and free table memory */
  37386. + KLIPS_PRINT(debug_xform,
  37387. + "klips_debug:ipsec_sadb_free: "
  37388. + "removing SAref entries and tables.\n");
  37389. + {
  37390. + unsigned table, entry;
  37391. + for(table = 0; table < IPSEC_SA_REF_MAINTABLE_NUM_ENTRIES; table++) {
  37392. + KLIPS_PRINT(debug_xform,
  37393. + "klips_debug:ipsec_sadb_free: "
  37394. + "removing SAref table=%u.\n",
  37395. + table);
  37396. + if(ipsec_sadb.refTable[table] == NULL) {
  37397. + KLIPS_PRINT(debug_xform,
  37398. + "klips_debug:ipsec_sadb_free: "
  37399. + "removed %u used refTables.\n",
  37400. + table);
  37401. + break;
  37402. + }
  37403. + for(entry = 0; entry < IPSEC_SA_REF_SUBTABLE_NUM_ENTRIES; entry++) {
  37404. + if(ipsec_sadb.refTable[table]->entry[entry] != NULL) {
  37405. + ipsec_sa_delchain(ipsec_sadb.refTable[table]->entry[entry]);
  37406. + ipsec_sadb.refTable[table]->entry[entry] = NULL;
  37407. + }
  37408. + }
  37409. + vfree(ipsec_sadb.refTable[table]);
  37410. + ipsec_sadb.refTable[table] = NULL;
  37411. + }
  37412. + }
  37413. +
  37414. + return(error);
  37415. +}
  37416. +
  37417. +int
  37418. +ipsec_sa_wipe(struct ipsec_sa *ips)
  37419. +{
  37420. + if(ips == NULL) {
  37421. + return -ENODATA;
  37422. + }
  37423. +
  37424. + /* if(atomic_dec_and_test(ips)) {
  37425. + }; */
  37426. +
  37427. +#if IPSEC_SA_REF_CODE
  37428. + /* remove me from the SArefTable */
  37429. + {
  37430. + char sa[SATOT_BUF];
  37431. + size_t sa_len;
  37432. + sa_len = satot(&ips->ips_said, 0, sa, sizeof(sa));
  37433. + KLIPS_PRINT(debug_xform,
  37434. + "klips_debug:ipsec_sa_wipe: "
  37435. + "removing SA=%s(0p%p), SAref=%d, table=%d(0p%p), entry=%d from the refTable.\n",
  37436. + sa_len ? sa : " (error)",
  37437. + ips,
  37438. + ips->ips_ref,
  37439. + IPsecSAref2table(IPsecSA2SAref(ips)),
  37440. + ipsec_sadb.refTable[IPsecSAref2table(IPsecSA2SAref(ips))],
  37441. + IPsecSAref2entry(IPsecSA2SAref(ips)));
  37442. + }
  37443. + if(ips->ips_ref == IPSEC_SAREF_NULL) {
  37444. + KLIPS_PRINT(debug_xform,
  37445. + "klips_debug:ipsec_sa_wipe: "
  37446. + "why does this SA not have a valid SAref?.\n");
  37447. + }
  37448. + ipsec_sadb.refTable[IPsecSAref2table(IPsecSA2SAref(ips))]->entry[IPsecSAref2entry(IPsecSA2SAref(ips))] = NULL;
  37449. + ips->ips_ref = IPSEC_SAREF_NULL;
  37450. + ipsec_sa_put(ips);
  37451. +#endif /* IPSEC_SA_REF_CODE */
  37452. +
  37453. + /* paranoid clean up */
  37454. + if(ips->ips_addr_s != NULL) {
  37455. + memset((caddr_t)(ips->ips_addr_s), 0, ips->ips_addr_s_size);
  37456. + kfree(ips->ips_addr_s);
  37457. + }
  37458. + ips->ips_addr_s = NULL;
  37459. +
  37460. + if(ips->ips_addr_d != NULL) {
  37461. + memset((caddr_t)(ips->ips_addr_d), 0, ips->ips_addr_d_size);
  37462. + kfree(ips->ips_addr_d);
  37463. + }
  37464. + ips->ips_addr_d = NULL;
  37465. +
  37466. + if(ips->ips_addr_p != NULL) {
  37467. + memset((caddr_t)(ips->ips_addr_p), 0, ips->ips_addr_p_size);
  37468. + kfree(ips->ips_addr_p);
  37469. + }
  37470. + ips->ips_addr_p = NULL;
  37471. +
  37472. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  37473. + if(ips->ips_natt_oa) {
  37474. + memset((caddr_t)(ips->ips_natt_oa), 0, ips->ips_natt_oa_size);
  37475. + kfree(ips->ips_natt_oa);
  37476. + }
  37477. + ips->ips_natt_oa = NULL;
  37478. +#endif
  37479. +
  37480. + if(ips->ips_key_a != NULL) {
  37481. + memset((caddr_t)(ips->ips_key_a), 0, ips->ips_key_a_size);
  37482. + kfree(ips->ips_key_a);
  37483. + }
  37484. + ips->ips_key_a = NULL;
  37485. +
  37486. + if(ips->ips_key_e != NULL) {
  37487. + if (ips->ips_alg_enc &&
  37488. + ips->ips_alg_enc->ixt_e_destroy_key)
  37489. + {
  37490. + ips->ips_alg_enc->ixt_e_destroy_key(ips->ips_alg_enc,
  37491. + ips->ips_key_e);
  37492. + } else
  37493. + {
  37494. + memset((caddr_t)(ips->ips_key_e), 0, ips->ips_key_e_size);
  37495. + kfree(ips->ips_key_e);
  37496. + }
  37497. + }
  37498. + ips->ips_key_e = NULL;
  37499. +
  37500. + if(ips->ips_iv != NULL) {
  37501. + memset((caddr_t)(ips->ips_iv), 0, ips->ips_iv_size);
  37502. + kfree(ips->ips_iv);
  37503. + }
  37504. + ips->ips_iv = NULL;
  37505. +
  37506. + if(ips->ips_ident_s.data != NULL) {
  37507. + memset((caddr_t)(ips->ips_ident_s.data),
  37508. + 0,
  37509. + ips->ips_ident_s.len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident));
  37510. + kfree(ips->ips_ident_s.data);
  37511. + }
  37512. + ips->ips_ident_s.data = NULL;
  37513. +
  37514. + if(ips->ips_ident_d.data != NULL) {
  37515. + memset((caddr_t)(ips->ips_ident_d.data),
  37516. + 0,
  37517. + ips->ips_ident_d.len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident));
  37518. + kfree(ips->ips_ident_d.data);
  37519. + }
  37520. + ips->ips_ident_d.data = NULL;
  37521. +
  37522. + if (ips->ips_alg_enc||ips->ips_alg_auth) {
  37523. + ipsec_alg_sa_wipe(ips);
  37524. + }
  37525. +
  37526. + memset((caddr_t)ips, 0, sizeof(*ips));
  37527. + kfree(ips);
  37528. + ips = NULL;
  37529. +
  37530. + return 0;
  37531. +}
  37532. +
  37533. +extern int sysctl_ipsec_debug_verbose;
  37534. +
  37535. +int ipsec_sa_init(struct ipsec_sa *ipsp)
  37536. +{
  37537. + int i;
  37538. + int error = 0;
  37539. + char sa[SATOT_BUF];
  37540. + size_t sa_len;
  37541. + char ipaddr_txt[ADDRTOA_BUF];
  37542. + char ipaddr2_txt[ADDRTOA_BUF];
  37543. +#if defined (CONFIG_KLIPS_AUTH_HMAC_MD5) || defined (CONFIG_KLIPS_AUTH_HMAC_SHA1)
  37544. + unsigned char kb[AHMD596_BLKLEN];
  37545. +#endif
  37546. + struct ipsec_alg_enc *ixt_e = NULL;
  37547. + struct ipsec_alg_auth *ixt_a = NULL;
  37548. +
  37549. + if(ipsp == NULL) {
  37550. + KLIPS_PRINT(debug_pfkey,
  37551. + "ipsec_sa_init: "
  37552. + "ipsp is NULL, fatal\n");
  37553. + SENDERR(EINVAL);
  37554. + }
  37555. +
  37556. + sa_len = satot(&ipsp->ips_said, 0, sa, sizeof(sa));
  37557. +
  37558. + KLIPS_PRINT(debug_pfkey,
  37559. + "ipsec_sa_init: "
  37560. + "(pfkey defined) called for SA:%s\n",
  37561. + sa_len ? sa : " (error)");
  37562. +
  37563. + KLIPS_PRINT(debug_pfkey,
  37564. + "ipsec_sa_init: "
  37565. + "calling init routine of %s%s%s\n",
  37566. + IPS_XFORM_NAME(ipsp));
  37567. +
  37568. + switch(ipsp->ips_said.proto) {
  37569. +
  37570. +#ifdef CONFIG_KLIPS_IPIP
  37571. + case IPPROTO_IPIP: {
  37572. + addrtoa(((struct sockaddr_in*)(ipsp->ips_addr_s))->sin_addr,
  37573. + 0,
  37574. + ipaddr_txt, sizeof(ipaddr_txt));
  37575. + addrtoa(((struct sockaddr_in*)(ipsp->ips_addr_d))->sin_addr,
  37576. + 0,
  37577. + ipaddr2_txt, sizeof(ipaddr_txt));
  37578. + KLIPS_PRINT(debug_pfkey,
  37579. + "ipsec_sa_init: "
  37580. + "(pfkey defined) IPIP ipsec_sa set for %s->%s.\n",
  37581. + ipaddr_txt,
  37582. + ipaddr2_txt);
  37583. + }
  37584. + break;
  37585. +#endif /* !CONFIG_KLIPS_IPIP */
  37586. +
  37587. +#ifdef CONFIG_KLIPS_AH
  37588. + case IPPROTO_AH:
  37589. + switch(ipsp->ips_authalg) {
  37590. +# ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  37591. + case AH_MD5: {
  37592. + unsigned char *akp;
  37593. + unsigned int aks;
  37594. + MD5_CTX *ictx;
  37595. + MD5_CTX *octx;
  37596. +
  37597. + if(ipsp->ips_key_bits_a != (AHMD596_KLEN * 8)) {
  37598. + KLIPS_PRINT(debug_pfkey,
  37599. + "ipsec_sa_init: "
  37600. + "incorrect key size: %d bits -- must be %d bits\n"/*octets (bytes)\n"*/,
  37601. + ipsp->ips_key_bits_a, AHMD596_KLEN * 8);
  37602. + SENDERR(EINVAL);
  37603. + }
  37604. +
  37605. +# if KLIPS_DIVULGE_HMAC_KEY
  37606. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37607. + "ipsec_sa_init: "
  37608. + "hmac md5-96 key is 0x%08x %08x %08x %08x\n",
  37609. + ntohl(*(((__u32 *)ipsp->ips_key_a)+0)),
  37610. + ntohl(*(((__u32 *)ipsp->ips_key_a)+1)),
  37611. + ntohl(*(((__u32 *)ipsp->ips_key_a)+2)),
  37612. + ntohl(*(((__u32 *)ipsp->ips_key_a)+3)));
  37613. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37614. +
  37615. + ipsp->ips_auth_bits = AHMD596_ALEN * 8;
  37616. +
  37617. + /* save the pointer to the key material */
  37618. + akp = ipsp->ips_key_a;
  37619. + aks = ipsp->ips_key_a_size;
  37620. +
  37621. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37622. + "ipsec_sa_init: "
  37623. + "allocating %lu bytes for md5_ctx.\n",
  37624. + (unsigned long) sizeof(struct md5_ctx));
  37625. + if((ipsp->ips_key_a = (caddr_t)
  37626. + kmalloc(sizeof(struct md5_ctx), GFP_ATOMIC)) == NULL) {
  37627. + ipsp->ips_key_a = akp;
  37628. + SENDERR(ENOMEM);
  37629. + }
  37630. + ipsp->ips_key_a_size = sizeof(struct md5_ctx);
  37631. +
  37632. + for (i = 0; i < DIVUP(ipsp->ips_key_bits_a, 8); i++) {
  37633. + kb[i] = akp[i] ^ HMAC_IPAD;
  37634. + }
  37635. + for (; i < AHMD596_BLKLEN; i++) {
  37636. + kb[i] = HMAC_IPAD;
  37637. + }
  37638. +
  37639. + ictx = &(((struct md5_ctx*)(ipsp->ips_key_a))->ictx);
  37640. + osMD5Init(ictx);
  37641. + osMD5Update(ictx, kb, AHMD596_BLKLEN);
  37642. +
  37643. + for (i = 0; i < AHMD596_BLKLEN; i++) {
  37644. + kb[i] ^= (HMAC_IPAD ^ HMAC_OPAD);
  37645. + }
  37646. +
  37647. + octx = &(((struct md5_ctx*)(ipsp->ips_key_a))->octx);
  37648. + osMD5Init(octx);
  37649. + osMD5Update(octx, kb, AHMD596_BLKLEN);
  37650. +
  37651. +# if KLIPS_DIVULGE_HMAC_KEY
  37652. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37653. + "ipsec_sa_init: "
  37654. + "MD5 ictx=0x%08x %08x %08x %08x octx=0x%08x %08x %08x %08x\n",
  37655. + ((__u32*)ictx)[0],
  37656. + ((__u32*)ictx)[1],
  37657. + ((__u32*)ictx)[2],
  37658. + ((__u32*)ictx)[3],
  37659. + ((__u32*)octx)[0],
  37660. + ((__u32*)octx)[1],
  37661. + ((__u32*)octx)[2],
  37662. + ((__u32*)octx)[3] );
  37663. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37664. +
  37665. + /* zero key buffer -- paranoid */
  37666. + memset(akp, 0, aks);
  37667. + kfree(akp);
  37668. + }
  37669. + break;
  37670. +# endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  37671. +# ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  37672. + case AH_SHA: {
  37673. + unsigned char *akp;
  37674. + unsigned int aks;
  37675. + SHA1_CTX *ictx;
  37676. + SHA1_CTX *octx;
  37677. +
  37678. + if(ipsp->ips_key_bits_a != (AHSHA196_KLEN * 8)) {
  37679. + KLIPS_PRINT(debug_pfkey,
  37680. + "ipsec_sa_init: "
  37681. + "incorrect key size: %d bits -- must be %d bits\n"/*octets (bytes)\n"*/,
  37682. + ipsp->ips_key_bits_a, AHSHA196_KLEN * 8);
  37683. + SENDERR(EINVAL);
  37684. + }
  37685. +
  37686. +# if KLIPS_DIVULGE_HMAC_KEY
  37687. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37688. + "ipsec_sa_init: "
  37689. + "hmac sha1-96 key is 0x%08x %08x %08x %08x\n",
  37690. + ntohl(*(((__u32 *)ipsp->ips_key_a)+0)),
  37691. + ntohl(*(((__u32 *)ipsp->ips_key_a)+1)),
  37692. + ntohl(*(((__u32 *)ipsp->ips_key_a)+2)),
  37693. + ntohl(*(((__u32 *)ipsp->ips_key_a)+3)));
  37694. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37695. +
  37696. + ipsp->ips_auth_bits = AHSHA196_ALEN * 8;
  37697. +
  37698. + /* save the pointer to the key material */
  37699. + akp = ipsp->ips_key_a;
  37700. + aks = ipsp->ips_key_a_size;
  37701. +
  37702. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37703. + "ipsec_sa_init: "
  37704. + "allocating %lu bytes for sha1_ctx.\n",
  37705. + (unsigned long) sizeof(struct sha1_ctx));
  37706. + if((ipsp->ips_key_a = (caddr_t)
  37707. + kmalloc(sizeof(struct sha1_ctx), GFP_ATOMIC)) == NULL) {
  37708. + ipsp->ips_key_a = akp;
  37709. + SENDERR(ENOMEM);
  37710. + }
  37711. + ipsp->ips_key_a_size = sizeof(struct sha1_ctx);
  37712. +
  37713. + for (i = 0; i < DIVUP(ipsp->ips_key_bits_a, 8); i++) {
  37714. + kb[i] = akp[i] ^ HMAC_IPAD;
  37715. + }
  37716. + for (; i < AHMD596_BLKLEN; i++) {
  37717. + kb[i] = HMAC_IPAD;
  37718. + }
  37719. +
  37720. + ictx = &(((struct sha1_ctx*)(ipsp->ips_key_a))->ictx);
  37721. + SHA1Init(ictx);
  37722. + SHA1Update(ictx, kb, AHSHA196_BLKLEN);
  37723. +
  37724. + for (i = 0; i < AHSHA196_BLKLEN; i++) {
  37725. + kb[i] ^= (HMAC_IPAD ^ HMAC_OPAD);
  37726. + }
  37727. +
  37728. + octx = &(((struct sha1_ctx*)(ipsp->ips_key_a))->octx);
  37729. + SHA1Init(octx);
  37730. + SHA1Update(octx, kb, AHSHA196_BLKLEN);
  37731. +
  37732. +# if KLIPS_DIVULGE_HMAC_KEY
  37733. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37734. + "ipsec_sa_init: "
  37735. + "SHA1 ictx=0x%08x %08x %08x %08x octx=0x%08x %08x %08x %08x\n",
  37736. + ((__u32*)ictx)[0],
  37737. + ((__u32*)ictx)[1],
  37738. + ((__u32*)ictx)[2],
  37739. + ((__u32*)ictx)[3],
  37740. + ((__u32*)octx)[0],
  37741. + ((__u32*)octx)[1],
  37742. + ((__u32*)octx)[2],
  37743. + ((__u32*)octx)[3] );
  37744. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37745. + /* zero key buffer -- paranoid */
  37746. + memset(akp, 0, aks);
  37747. + kfree(akp);
  37748. + }
  37749. + break;
  37750. +# endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  37751. + default:
  37752. + KLIPS_PRINT(debug_pfkey,
  37753. + "ipsec_sa_init: "
  37754. + "authalg=%d support not available in the kernel",
  37755. + ipsp->ips_authalg);
  37756. + SENDERR(EINVAL);
  37757. + }
  37758. + break;
  37759. +#endif /* CONFIG_KLIPS_AH */
  37760. +
  37761. +#ifdef CONFIG_KLIPS_ESP
  37762. + case IPPROTO_ESP:
  37763. + {
  37764. +#if defined (CONFIG_KLIPS_AUTH_HMAC_MD5) || defined (CONFIG_KLIPS_AUTH_HMAC_SHA1)
  37765. + unsigned char *akp;
  37766. + unsigned int aks;
  37767. +#endif
  37768. +
  37769. + ipsec_alg_sa_init(ipsp);
  37770. + ixt_e=ipsp->ips_alg_enc;
  37771. +
  37772. + if (ixt_e == NULL) {
  37773. + if(printk_ratelimit()) {
  37774. + printk(KERN_INFO
  37775. + "ipsec_sa_init: "
  37776. + "encalg=%d support not available in the kernel",
  37777. + ipsp->ips_encalg);
  37778. + }
  37779. + SENDERR(ENOENT);
  37780. + }
  37781. +
  37782. + ipsp->ips_iv_size = ixt_e->ixt_common.ixt_support.ias_ivlen/8;
  37783. +
  37784. + /* Create IV */
  37785. + if (ipsp->ips_iv_size) {
  37786. + if((ipsp->ips_iv = (caddr_t)
  37787. + kmalloc(ipsp->ips_iv_size, GFP_ATOMIC)) == NULL) {
  37788. + SENDERR(ENOMEM);
  37789. + }
  37790. + prng_bytes(&ipsec_prng,
  37791. + (char *)ipsp->ips_iv,
  37792. + ipsp->ips_iv_size);
  37793. + ipsp->ips_iv_bits = ipsp->ips_iv_size * 8;
  37794. + }
  37795. +
  37796. + if ((error=ipsec_alg_enc_key_create(ipsp)) < 0)
  37797. + SENDERR(-error);
  37798. +
  37799. + if ((ixt_a=ipsp->ips_alg_auth)) {
  37800. + if ((error=ipsec_alg_auth_key_create(ipsp)) < 0)
  37801. + SENDERR(-error);
  37802. + } else
  37803. +
  37804. + switch(ipsp->ips_authalg) {
  37805. +# ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  37806. + case AH_MD5: {
  37807. + MD5_CTX *ictx;
  37808. + MD5_CTX *octx;
  37809. +
  37810. + if(ipsp->ips_key_bits_a != (AHMD596_KLEN * 8)) {
  37811. + KLIPS_PRINT(debug_pfkey,
  37812. + "ipsec_sa_init: "
  37813. + "incorrect authorisation key size: %d bits -- must be %d bits\n"/*octets (bytes)\n"*/,
  37814. + ipsp->ips_key_bits_a,
  37815. + AHMD596_KLEN * 8);
  37816. + SENDERR(EINVAL);
  37817. + }
  37818. +
  37819. +# if KLIPS_DIVULGE_HMAC_KEY
  37820. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37821. + "ipsec_sa_init: "
  37822. + "hmac md5-96 key is 0x%08x %08x %08x %08x\n",
  37823. + ntohl(*(((__u32 *)(ipsp->ips_key_a))+0)),
  37824. + ntohl(*(((__u32 *)(ipsp->ips_key_a))+1)),
  37825. + ntohl(*(((__u32 *)(ipsp->ips_key_a))+2)),
  37826. + ntohl(*(((__u32 *)(ipsp->ips_key_a))+3)));
  37827. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37828. + ipsp->ips_auth_bits = AHMD596_ALEN * 8;
  37829. +
  37830. + /* save the pointer to the key material */
  37831. + akp = ipsp->ips_key_a;
  37832. + aks = ipsp->ips_key_a_size;
  37833. +
  37834. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37835. + "ipsec_sa_init: "
  37836. + "allocating %lu bytes for md5_ctx.\n",
  37837. + (unsigned long) sizeof(struct md5_ctx));
  37838. + if((ipsp->ips_key_a = (caddr_t)
  37839. + kmalloc(sizeof(struct md5_ctx), GFP_ATOMIC)) == NULL) {
  37840. + ipsp->ips_key_a = akp;
  37841. + SENDERR(ENOMEM);
  37842. + }
  37843. + ipsp->ips_key_a_size = sizeof(struct md5_ctx);
  37844. +
  37845. + for (i = 0; i < DIVUP(ipsp->ips_key_bits_a, 8); i++) {
  37846. + kb[i] = akp[i] ^ HMAC_IPAD;
  37847. + }
  37848. + for (; i < AHMD596_BLKLEN; i++) {
  37849. + kb[i] = HMAC_IPAD;
  37850. + }
  37851. +
  37852. + ictx = &(((struct md5_ctx*)(ipsp->ips_key_a))->ictx);
  37853. + osMD5Init(ictx);
  37854. + osMD5Update(ictx, kb, AHMD596_BLKLEN);
  37855. +
  37856. + for (i = 0; i < AHMD596_BLKLEN; i++) {
  37857. + kb[i] ^= (HMAC_IPAD ^ HMAC_OPAD);
  37858. + }
  37859. +
  37860. + octx = &(((struct md5_ctx*)(ipsp->ips_key_a))->octx);
  37861. + osMD5Init(octx);
  37862. + osMD5Update(octx, kb, AHMD596_BLKLEN);
  37863. +
  37864. +# if KLIPS_DIVULGE_HMAC_KEY
  37865. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37866. + "ipsec_sa_init: "
  37867. + "MD5 ictx=0x%08x %08x %08x %08x octx=0x%08x %08x %08x %08x\n",
  37868. + ((__u32*)ictx)[0],
  37869. + ((__u32*)ictx)[1],
  37870. + ((__u32*)ictx)[2],
  37871. + ((__u32*)ictx)[3],
  37872. + ((__u32*)octx)[0],
  37873. + ((__u32*)octx)[1],
  37874. + ((__u32*)octx)[2],
  37875. + ((__u32*)octx)[3] );
  37876. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37877. + /* paranoid */
  37878. + memset(akp, 0, aks);
  37879. + kfree(akp);
  37880. + break;
  37881. + }
  37882. +# endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  37883. +# ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  37884. + case AH_SHA: {
  37885. + SHA1_CTX *ictx;
  37886. + SHA1_CTX *octx;
  37887. +
  37888. + if(ipsp->ips_key_bits_a != (AHSHA196_KLEN * 8)) {
  37889. + KLIPS_PRINT(debug_pfkey,
  37890. + "ipsec_sa_init: "
  37891. + "incorrect authorisation key size: %d bits -- must be %d bits\n"/*octets (bytes)\n"*/,
  37892. + ipsp->ips_key_bits_a,
  37893. + AHSHA196_KLEN * 8);
  37894. + SENDERR(EINVAL);
  37895. + }
  37896. +
  37897. +# if KLIPS_DIVULGE_HMAC_KEY
  37898. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37899. + "ipsec_sa_init: "
  37900. + "hmac sha1-96 key is 0x%08x %08x %08x %08x\n",
  37901. + ntohl(*(((__u32 *)ipsp->ips_key_a)+0)),
  37902. + ntohl(*(((__u32 *)ipsp->ips_key_a)+1)),
  37903. + ntohl(*(((__u32 *)ipsp->ips_key_a)+2)),
  37904. + ntohl(*(((__u32 *)ipsp->ips_key_a)+3)));
  37905. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37906. + ipsp->ips_auth_bits = AHSHA196_ALEN * 8;
  37907. +
  37908. + /* save the pointer to the key material */
  37909. + akp = ipsp->ips_key_a;
  37910. + aks = ipsp->ips_key_a_size;
  37911. +
  37912. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37913. + "ipsec_sa_init: "
  37914. + "allocating %lu bytes for sha1_ctx.\n",
  37915. + (unsigned long) sizeof(struct sha1_ctx));
  37916. + if((ipsp->ips_key_a = (caddr_t)
  37917. + kmalloc(sizeof(struct sha1_ctx), GFP_ATOMIC)) == NULL) {
  37918. + ipsp->ips_key_a = akp;
  37919. + SENDERR(ENOMEM);
  37920. + }
  37921. + ipsp->ips_key_a_size = sizeof(struct sha1_ctx);
  37922. +
  37923. + for (i = 0; i < DIVUP(ipsp->ips_key_bits_a, 8); i++) {
  37924. + kb[i] = akp[i] ^ HMAC_IPAD;
  37925. + }
  37926. + for (; i < AHMD596_BLKLEN; i++) {
  37927. + kb[i] = HMAC_IPAD;
  37928. + }
  37929. +
  37930. + ictx = &(((struct sha1_ctx*)(ipsp->ips_key_a))->ictx);
  37931. + SHA1Init(ictx);
  37932. + SHA1Update(ictx, kb, AHSHA196_BLKLEN);
  37933. +
  37934. + for (i = 0; i < AHSHA196_BLKLEN; i++) {
  37935. + kb[i] ^= (HMAC_IPAD ^ HMAC_OPAD);
  37936. + }
  37937. +
  37938. + octx = &((struct sha1_ctx*)(ipsp->ips_key_a))->octx;
  37939. + SHA1Init(octx);
  37940. + SHA1Update(octx, kb, AHSHA196_BLKLEN);
  37941. +
  37942. +# if KLIPS_DIVULGE_HMAC_KEY
  37943. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  37944. + "ipsec_sa_init: "
  37945. + "SHA1 ictx=0x%08x %08x %08x %08x octx=0x%08x %08x %08x %08x\n",
  37946. + ((__u32*)ictx)[0],
  37947. + ((__u32*)ictx)[1],
  37948. + ((__u32*)ictx)[2],
  37949. + ((__u32*)ictx)[3],
  37950. + ((__u32*)octx)[0],
  37951. + ((__u32*)octx)[1],
  37952. + ((__u32*)octx)[2],
  37953. + ((__u32*)octx)[3] );
  37954. +# endif /* KLIPS_DIVULGE_HMAC_KEY */
  37955. + memset(akp, 0, aks);
  37956. + kfree(akp);
  37957. + break;
  37958. + }
  37959. +# endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  37960. + case AH_NONE:
  37961. + break;
  37962. + default:
  37963. + KLIPS_PRINT(debug_pfkey,
  37964. + "ipsec_sa_init: "
  37965. + "authalg=%d support not available in the kernel.\n",
  37966. + ipsp->ips_authalg);
  37967. + SENDERR(EINVAL);
  37968. + }
  37969. + }
  37970. + break;
  37971. +#endif /* !CONFIG_KLIPS_ESP */
  37972. +#ifdef CONFIG_KLIPS_IPCOMP
  37973. + case IPPROTO_COMP:
  37974. + ipsp->ips_comp_adapt_tries = 0;
  37975. + ipsp->ips_comp_adapt_skip = 0;
  37976. + ipsp->ips_comp_ratio_cbytes = 0;
  37977. + ipsp->ips_comp_ratio_dbytes = 0;
  37978. + break;
  37979. +#endif /* CONFIG_KLIPS_IPCOMP */
  37980. + default:
  37981. + printk(KERN_ERR "KLIPS sa initialization: "
  37982. + "proto=%d unknown.\n",
  37983. + ipsp->ips_said.proto);
  37984. + SENDERR(EINVAL);
  37985. + }
  37986. +
  37987. + errlab:
  37988. + return(error);
  37989. +}
  37990. +
  37991. +
  37992. +
  37993. +/*
  37994. + * $Log: ipsec_sa.c,v $
  37995. + * Revision 1.30.2.2 2006/10/06 21:39:26 paul
  37996. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  37997. + * set. This is defined through autoconf.h which is included through the
  37998. + * linux kernel build macros.
  37999. + *
  38000. + * Revision 1.30.2.1 2006/04/20 16:33:07 mcr
  38001. + * remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  38002. + * Fix in-kernel module compilation. Sub-makefiles do not work.
  38003. + *
  38004. + * Revision 1.30 2005/05/24 01:02:35 mcr
  38005. + * some refactoring/simplification of situation where alg
  38006. + * is not found.
  38007. + *
  38008. + * Revision 1.29 2005/05/18 19:13:28 mcr
  38009. + * rename debug messages. make sure that algo not found is not
  38010. + * a debug message.
  38011. + *
  38012. + * Revision 1.28 2005/05/11 01:30:20 mcr
  38013. + * removed "poor-man"s OOP in favour of proper C structures.
  38014. + *
  38015. + * Revision 1.27 2005/04/29 05:10:22 mcr
  38016. + * removed from extraenous includes to make unit testing easier.
  38017. + *
  38018. + * Revision 1.26 2005/04/14 20:56:24 mcr
  38019. + * moved (pfkey_)ipsec_sa_init to ipsec_sa.c.
  38020. + *
  38021. + * Revision 1.25 2004/08/22 20:12:16 mcr
  38022. + * one more KLIPS_NAT->IPSEC_NAT.
  38023. + *
  38024. + * Revision 1.24 2004/07/10 19:11:18 mcr
  38025. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  38026. + *
  38027. + * Revision 1.23 2004/04/06 02:49:26 mcr
  38028. + * pullup of algo code from alg-branch.
  38029. + *
  38030. + * Revision 1.22.2.1 2003/12/22 15:25:52 jjo
  38031. + * . Merged algo-0.8.1-rc11-test1 into alg-branch
  38032. + *
  38033. + * Revision 1.22 2003/12/10 01:14:27 mcr
  38034. + * NAT-traversal patches to KLIPS.
  38035. + *
  38036. + * Revision 1.21 2003/10/31 02:27:55 mcr
  38037. + * pulled up port-selector patches and sa_id elimination.
  38038. + *
  38039. + * Revision 1.20.4.1 2003/10/29 01:30:41 mcr
  38040. + * elimited "struct sa_id".
  38041. + *
  38042. + * Revision 1.20 2003/02/06 01:50:34 rgb
  38043. + * Fixed initialisation bug for first sadb hash bucket that would only manifest itself on platforms where NULL != 0.
  38044. + *
  38045. + * Revision 1.19 2003/01/30 02:32:22 rgb
  38046. + *
  38047. + * Rename SAref table macro names for clarity.
  38048. + * Transmit error code through to caller from callee for better diagnosis of problems.
  38049. + * Convert IPsecSAref_t from signed to unsigned to fix apparent SAref exhaustion bug.
  38050. + *
  38051. + * Revision 1.18 2002/10/12 23:11:53 dhr
  38052. + *
  38053. + * [KenB + DHR] more 64-bit cleanup
  38054. + *
  38055. + * Revision 1.17 2002/10/07 18:31:43 rgb
  38056. + * Move field width sanity checks to ipsec_sa.c
  38057. + *
  38058. + * Revision 1.16 2002/09/20 15:41:02 rgb
  38059. + * Re-wrote most of the SAref code to eliminate Entry pointers.
  38060. + * Added SAref code compiler directive switch.
  38061. + * Added a saref test function for testing macros.
  38062. + * Switch from pfkey_alloc_ipsec_sa() to ipsec_sa_alloc().
  38063. + * Split ipsec_sadb_cleanup from new funciton ipsec_sadb_free to avoid problem
  38064. + * of freeing newly created structures when clearing the reftable upon startup
  38065. + * to start from a known state.
  38066. + * Place all ipsec sadb globals into one struct.
  38067. + * Rework saref freelist.
  38068. + * Added memory allocation debugging.
  38069. + *
  38070. + * Revision 1.15 2002/09/20 05:01:44 rgb
  38071. + * Update copyright date.
  38072. + *
  38073. + * Revision 1.14 2002/08/13 19:01:25 mcr
  38074. + * patches from kenb to permit compilation of FreeSWAN on ia64.
  38075. + * des library patched to use proper DES_LONG type for ia64.
  38076. + *
  38077. + * Revision 1.13 2002/07/29 03:06:20 mcr
  38078. + * get rid of variable not used warnings.
  38079. + *
  38080. + * Revision 1.12 2002/07/26 08:48:31 rgb
  38081. + * Added SA ref table code.
  38082. + *
  38083. + * Revision 1.11 2002/06/04 16:48:49 rgb
  38084. + * Tidied up pointer code for processor independance.
  38085. + *
  38086. + * Revision 1.10 2002/05/23 07:16:17 rgb
  38087. + * Added ipsec_sa_put() for releasing an ipsec_sa refcount.
  38088. + * Pointer clean-up.
  38089. + * Added refcount code.
  38090. + * Convert "usecount" to "refcount" to remove ambiguity.
  38091. + *
  38092. + * Revision 1.9 2002/05/14 02:34:49 rgb
  38093. + * Converted reference from ipsec_sa_put to ipsec_sa_add to avoid confusion
  38094. + * with "put" usage in the kernel.
  38095. + * Change all references to tdb, TDB or Tunnel Descriptor Block to ips,
  38096. + * ipsec_sa or ipsec_sa.
  38097. + * Added some preliminary refcount code.
  38098. + *
  38099. + * Revision 1.8 2002/04/24 07:55:32 mcr
  38100. + * #include patches and Makefiles for post-reorg compilation.
  38101. + *
  38102. + * Revision 1.7 2002/04/24 07:36:30 mcr
  38103. + * Moved from ./klips/net/ipsec/ipsec_sa.c,v
  38104. + *
  38105. + * Revision 1.6 2002/04/20 00:12:25 rgb
  38106. + * Added esp IV CBC attack fix, disabled.
  38107. + *
  38108. + * Revision 1.5 2002/01/29 17:17:56 mcr
  38109. + * moved include of ipsec_param.h to after include of linux/kernel.h
  38110. + * otherwise, it seems that some option that is set in ipsec_param.h
  38111. + * screws up something subtle in the include path to kernel.h, and
  38112. + * it complains on the snprintf() prototype.
  38113. + *
  38114. + * Revision 1.4 2002/01/29 04:00:52 mcr
  38115. + * more excise of kversions.h header.
  38116. + *
  38117. + * Revision 1.3 2002/01/29 02:13:18 mcr
  38118. + * introduction of ipsec_kversion.h means that include of
  38119. + * ipsec_param.h must preceed any decisions about what files to
  38120. + * include to deal with differences in kernel source.
  38121. + *
  38122. + * Revision 1.2 2001/11/26 09:16:15 rgb
  38123. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  38124. + *
  38125. + * Revision 1.1.2.2 2001/10/22 21:05:41 mcr
  38126. + * removed phony prototype for des_set_key.
  38127. + *
  38128. + * Revision 1.1.2.1 2001/09/25 02:24:57 mcr
  38129. + * struct tdb -> struct ipsec_sa.
  38130. + * sa(tdb) manipulation functions renamed and moved to ipsec_sa.c
  38131. + * ipsec_xform.c removed. header file still contains useful things.
  38132. + *
  38133. + *
  38134. + *
  38135. + * CLONED from ipsec_xform.c:
  38136. + * Revision 1.53 2001/09/08 21:13:34 rgb
  38137. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  38138. + *
  38139. + * Revision 1.52 2001/06/14 19:35:11 rgb
  38140. + * Update copyright date.
  38141. + *
  38142. + * Revision 1.51 2001/05/30 08:14:03 rgb
  38143. + * Removed vestiges of esp-null transforms.
  38144. + *
  38145. + * Revision 1.50 2001/05/03 19:43:18 rgb
  38146. + * Initialise error return variable.
  38147. + * Update SENDERR macro.
  38148. + * Fix sign of error return code for ipsec_tdbcleanup().
  38149. + * Use more appropriate return code for ipsec_tdbwipe().
  38150. + *
  38151. + * Revision 1.49 2001/04/19 18:56:17 rgb
  38152. + * Fixed tdb table locking comments.
  38153. + *
  38154. + * Revision 1.48 2001/02/27 22:24:55 rgb
  38155. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  38156. + * Check for satoa() return codes.
  38157. + *
  38158. + * Revision 1.47 2000/11/06 04:32:08 rgb
  38159. + * Ditched spin_lock_irqsave in favour of spin_lock_bh.
  38160. + *
  38161. + * Revision 1.46 2000/09/20 16:21:57 rgb
  38162. + * Cleaned up ident string alloc/free.
  38163. + *
  38164. + * Revision 1.45 2000/09/08 19:16:51 rgb
  38165. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  38166. + * Removed all references to CONFIG_IPSEC_PFKEYv2.
  38167. + *
  38168. + * Revision 1.44 2000/08/30 05:29:04 rgb
  38169. + * Compiler-define out no longer used tdb_init() in ipsec_xform.c.
  38170. + *
  38171. + * Revision 1.43 2000/08/18 21:30:41 rgb
  38172. + * Purged all tdb_spi, tdb_proto and tdb_dst macros. They are unclear.
  38173. + *
  38174. + * Revision 1.42 2000/08/01 14:51:51 rgb
  38175. + * Removed _all_ remaining traces of DES.
  38176. + *
  38177. + * Revision 1.41 2000/07/28 14:58:31 rgb
  38178. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  38179. + *
  38180. + * Revision 1.40 2000/06/28 05:50:11 rgb
  38181. + * Actually set iv_bits.
  38182. + *
  38183. + * Revision 1.39 2000/05/10 23:11:09 rgb
  38184. + * Added netlink debugging output.
  38185. + * Added a cast to quiet down the ntohl bug.
  38186. + *
  38187. + * Revision 1.38 2000/05/10 19:18:42 rgb
  38188. + * Cast output of ntohl so that the broken prototype doesn't make our
  38189. + * compile noisy.
  38190. + *
  38191. + * Revision 1.37 2000/03/16 14:04:59 rgb
  38192. + * Hardwired CONFIG_IPSEC_PFKEYv2 on.
  38193. + *
  38194. + * Revision 1.36 2000/01/26 10:11:28 rgb
  38195. + * Fixed spacing in error text causing run-in words.
  38196. + *
  38197. + * Revision 1.35 2000/01/21 06:17:16 rgb
  38198. + * Tidied up compiler directive indentation for readability.
  38199. + * Added ictx,octx vars for simplification.(kravietz)
  38200. + * Added macros for HMAC padding magic numbers.(kravietz)
  38201. + * Fixed missing key length reporting bug.
  38202. + * Fixed bug in tdbwipe to return immediately on NULL tdbp passed in.
  38203. + *
  38204. + * Revision 1.34 1999/12/08 00:04:19 rgb
  38205. + * Fixed SA direction overwriting bug for netlink users.
  38206. + *
  38207. + * Revision 1.33 1999/12/01 22:16:44 rgb
  38208. + * Minor formatting changes in ESP MD5 initialisation.
  38209. + *
  38210. + * Revision 1.32 1999/11/25 09:06:36 rgb
  38211. + * Fixed error return messages, should be returning negative numbers.
  38212. + * Implemented SENDERR macro for propagating error codes.
  38213. + * Added debug message and separate error code for algorithms not compiled
  38214. + * in.
  38215. + *
  38216. + * Revision 1.31 1999/11/23 23:06:26 rgb
  38217. + * Sort out pfkey and freeswan headers, putting them in a library path.
  38218. + *
  38219. + * Revision 1.30 1999/11/18 04:09:20 rgb
  38220. + * Replaced all kernel version macros to shorter, readable form.
  38221. + *
  38222. + * Revision 1.29 1999/11/17 15:53:40 rgb
  38223. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  38224. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  38225. + * klips/net/ipsec/Makefile.
  38226. + *
  38227. + * Revision 1.28 1999/10/18 20:04:01 rgb
  38228. + * Clean-out unused cruft.
  38229. + *
  38230. + * Revision 1.27 1999/10/03 19:01:03 rgb
  38231. + * Spinlock support for 2.3.xx and 2.0.xx kernels.
  38232. + *
  38233. + * Revision 1.26 1999/10/01 16:22:24 rgb
  38234. + * Switch from assignment init. to functional init. of spinlocks.
  38235. + *
  38236. + * Revision 1.25 1999/10/01 15:44:54 rgb
  38237. + * Move spinlock header include to 2.1> scope.
  38238. + *
  38239. + * Revision 1.24 1999/10/01 00:03:46 rgb
  38240. + * Added tdb structure locking.
  38241. + * Minor formatting changes.
  38242. + * Add function to initialize tdb hash table.
  38243. + *
  38244. + * Revision 1.23 1999/05/25 22:42:12 rgb
  38245. + * Add deltdbchain() debugging.
  38246. + *
  38247. + * Revision 1.22 1999/05/25 21:24:31 rgb
  38248. + * Add debugging statements to deltdbchain().
  38249. + *
  38250. + * Revision 1.21 1999/05/25 03:51:48 rgb
  38251. + * Refix error return code.
  38252. + *
  38253. + * Revision 1.20 1999/05/25 03:34:07 rgb
  38254. + * Fix error return for flush.
  38255. + *
  38256. + * Revision 1.19 1999/05/09 03:25:37 rgb
  38257. + * Fix bug introduced by 2.2 quick-and-dirty patch.
  38258. + *
  38259. + * Revision 1.18 1999/05/05 22:02:32 rgb
  38260. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  38261. + *
  38262. + * Revision 1.17 1999/04/29 15:20:16 rgb
  38263. + * Change gettdb parameter to a pointer to reduce stack loading and
  38264. + * facilitate parameter sanity checking.
  38265. + * Add sanity checking for null pointer arguments.
  38266. + * Add debugging instrumentation.
  38267. + * Add function deltdbchain() which will take care of unlinking,
  38268. + * zeroing and deleting a chain of tdbs.
  38269. + * Add a parameter to tdbcleanup to be able to delete a class of SAs.
  38270. + * tdbwipe now actually zeroes the tdb as well as any of its pointed
  38271. + * structures.
  38272. + *
  38273. + * Revision 1.16 1999/04/16 15:36:29 rgb
  38274. + * Fix cut-and-paste error causing a memory leak in IPIP TDB freeing.
  38275. + *
  38276. + * Revision 1.15 1999/04/11 00:29:01 henry
  38277. + * GPL boilerplate
  38278. + *
  38279. + * Revision 1.14 1999/04/06 04:54:28 rgb
  38280. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  38281. + * patch shell fixes.
  38282. + *
  38283. + * Revision 1.13 1999/02/19 18:23:01 rgb
  38284. + * Nix debug off compile warning.
  38285. + *
  38286. + * Revision 1.12 1999/02/17 16:52:16 rgb
  38287. + * Consolidate satoa()s for space and speed efficiency.
  38288. + * Convert DEBUG_IPSEC to KLIPS_PRINT
  38289. + * Clean out unused cruft.
  38290. + * Ditch NET_IPIP dependancy.
  38291. + * Loop for 3des key setting.
  38292. + *
  38293. + * Revision 1.11 1999/01/26 02:09:05 rgb
  38294. + * Remove ah/esp/IPIP switching on include files.
  38295. + * Removed CONFIG_IPSEC_ALGO_SWITCH macro.
  38296. + * Removed dead code.
  38297. + * Clean up debug code when switched off.
  38298. + * Remove references to INET_GET_PROTOCOL.
  38299. + * Added code exclusion macros to reduce code from unused algorithms.
  38300. + *
  38301. + * Revision 1.10 1999/01/22 06:28:55 rgb
  38302. + * Cruft clean-out.
  38303. + * Put random IV generation in kernel.
  38304. + * Added algorithm switch code.
  38305. + * Enhanced debugging.
  38306. + * 64-bit clean-up.
  38307. + *
  38308. + * Revision 1.9 1998/11/30 13:22:55 rgb
  38309. + * Rationalised all the klips kernel file headers. They are much shorter
  38310. + * now and won't conflict under RH5.2.
  38311. + *
  38312. + * Revision 1.8 1998/11/25 04:59:06 rgb
  38313. + * Add conditionals for no IPIP tunnel code.
  38314. + * Delete commented out code.
  38315. + *
  38316. + * Revision 1.7 1998/10/31 06:50:41 rgb
  38317. + * Convert xform ASCII names to no spaces.
  38318. + * Fixed up comments in #endif directives.
  38319. + *
  38320. + * Revision 1.6 1998/10/19 14:44:28 rgb
  38321. + * Added inclusion of freeswan.h.
  38322. + * sa_id structure implemented and used: now includes protocol.
  38323. + *
  38324. + * Revision 1.5 1998/10/09 04:32:19 rgb
  38325. + * Added 'klips_debug' prefix to all klips printk debug statements.
  38326. + *
  38327. + * Revision 1.4 1998/08/12 00:11:31 rgb
  38328. + * Added new xform functions to the xform table.
  38329. + * Fixed minor debug output spelling error.
  38330. + *
  38331. + * Revision 1.3 1998/07/09 17:45:31 rgb
  38332. + * Clarify algorithm not available message.
  38333. + *
  38334. + * Revision 1.2 1998/06/23 03:00:51 rgb
  38335. + * Check for presence of IPIP protocol if it is setup one way (we don't
  38336. + * know what has been set up the other way and can only assume it will be
  38337. + * symmetrical with the exception of keys).
  38338. + *
  38339. + * Revision 1.1 1998/06/18 21:27:51 henry
  38340. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  38341. + * kernel-build scripts happier in the presence of symlinks
  38342. + *
  38343. + * Revision 1.3 1998/06/11 05:54:59 rgb
  38344. + * Added transform version string pointer to xformsw initialisations.
  38345. + *
  38346. + * Revision 1.2 1998/04/21 21:28:57 rgb
  38347. + * Rearrange debug switches to change on the fly debug output from user
  38348. + * space. Only kernel changes checked in at this time. radij.c was also
  38349. + * changed to temporarily remove buggy debugging code in rj_delete causing
  38350. + * an OOPS and hence, netlink device open errors.
  38351. + *
  38352. + * Revision 1.1 1998/04/09 03:06:13 henry
  38353. + * sources moved up from linux/net/ipsec
  38354. + *
  38355. + * Revision 1.1.1.1 1998/04/08 05:35:02 henry
  38356. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  38357. + *
  38358. + * Revision 0.5 1997/06/03 04:24:48 ji
  38359. + * Added ESP-3DES-MD5-96
  38360. + *
  38361. + * Revision 0.4 1997/01/15 01:28:15 ji
  38362. + * Added new transforms.
  38363. + *
  38364. + * Revision 0.3 1996/11/20 14:39:04 ji
  38365. + * Minor cleanups.
  38366. + * Rationalized debugging code.
  38367. + *
  38368. + * Revision 0.2 1996/11/02 00:18:33 ji
  38369. + * First limited release.
  38370. + *
  38371. + *
  38372. + */
  38373. --- /dev/null Tue Mar 11 13:02:56 2003
  38374. +++ linux/net/ipsec/ipsec_sha1.c Mon Feb 9 13:51:03 2004
  38375. @@ -0,0 +1,219 @@
  38376. +/*
  38377. + * RCSID $Id: ipsec_sha1.c,v 1.9 2004/04/06 02:49:26 mcr Exp $
  38378. + */
  38379. +
  38380. +/*
  38381. + * The rest of the code is derived from sha1.c by Steve Reid, which is
  38382. + * public domain.
  38383. + * Minor cosmetic changes to accomodate it in the Linux kernel by ji.
  38384. + */
  38385. +
  38386. +#include <asm/byteorder.h>
  38387. +#include <linux/string.h>
  38388. +
  38389. +#include "openswan/ipsec_sha1.h"
  38390. +
  38391. +#if defined(rol)
  38392. +#undef rol
  38393. +#endif
  38394. +
  38395. +#define SHA1HANDSOFF
  38396. +
  38397. +#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
  38398. +
  38399. +/* blk0() and blk() perform the initial expand. */
  38400. +/* I got the idea of expanding during the round function from SSLeay */
  38401. +#ifdef __LITTLE_ENDIAN
  38402. +#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
  38403. + |(rol(block->l[i],8)&0x00FF00FF))
  38404. +#else
  38405. +#define blk0(i) block->l[i]
  38406. +#endif
  38407. +#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
  38408. + ^block->l[(i+2)&15]^block->l[i&15],1))
  38409. +
  38410. +/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
  38411. +#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
  38412. +#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
  38413. +#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
  38414. +#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
  38415. +#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
  38416. +
  38417. +
  38418. +/* Hash a single 512-bit block. This is the core of the algorithm. */
  38419. +
  38420. +void SHA1Transform(__u32 state[5], __u8 buffer[64])
  38421. +{
  38422. +__u32 a, b, c, d, e;
  38423. +typedef union {
  38424. + unsigned char c[64];
  38425. + __u32 l[16];
  38426. +} CHAR64LONG16;
  38427. +CHAR64LONG16* block;
  38428. +#ifdef SHA1HANDSOFF
  38429. +static unsigned char workspace[64];
  38430. + block = (CHAR64LONG16*)workspace;
  38431. + memcpy(block, buffer, 64);
  38432. +#else
  38433. + block = (CHAR64LONG16*)buffer;
  38434. +#endif
  38435. + /* Copy context->state[] to working vars */
  38436. + a = state[0];
  38437. + b = state[1];
  38438. + c = state[2];
  38439. + d = state[3];
  38440. + e = state[4];
  38441. + /* 4 rounds of 20 operations each. Loop unrolled. */
  38442. + R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
  38443. + R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
  38444. + R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
  38445. + R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
  38446. + R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
  38447. + R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
  38448. + R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
  38449. + R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
  38450. + R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
  38451. + R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
  38452. + R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
  38453. + R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
  38454. + R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
  38455. + R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
  38456. + R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
  38457. + R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
  38458. + R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
  38459. + R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
  38460. + R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
  38461. + R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
  38462. + /* Add the working vars back into context.state[] */
  38463. + state[0] += a;
  38464. + state[1] += b;
  38465. + state[2] += c;
  38466. + state[3] += d;
  38467. + state[4] += e;
  38468. + /* Wipe variables */
  38469. + a = b = c = d = e = 0;
  38470. +}
  38471. +
  38472. +
  38473. +/* SHA1Init - Initialize new context */
  38474. +
  38475. +void SHA1Init(void *vcontext)
  38476. +{
  38477. + SHA1_CTX* context = vcontext;
  38478. +
  38479. + /* SHA1 initialization constants */
  38480. + context->state[0] = 0x67452301;
  38481. + context->state[1] = 0xEFCDAB89;
  38482. + context->state[2] = 0x98BADCFE;
  38483. + context->state[3] = 0x10325476;
  38484. + context->state[4] = 0xC3D2E1F0;
  38485. + context->count[0] = context->count[1] = 0;
  38486. +}
  38487. +
  38488. +
  38489. +/* Run your data through this. */
  38490. +
  38491. +void SHA1Update(void *vcontext, unsigned char* data, __u32 len)
  38492. +{
  38493. + SHA1_CTX* context = vcontext;
  38494. + __u32 i, j;
  38495. +
  38496. + j = context->count[0];
  38497. + if ((context->count[0] += len << 3) < j)
  38498. + context->count[1]++;
  38499. + context->count[1] += (len>>29);
  38500. + j = (j >> 3) & 63;
  38501. + if ((j + len) > 63) {
  38502. + memcpy(&context->buffer[j], data, (i = 64-j));
  38503. + SHA1Transform(context->state, context->buffer);
  38504. + for ( ; i + 63 < len; i += 64) {
  38505. + SHA1Transform(context->state, &data[i]);
  38506. + }
  38507. + j = 0;
  38508. + }
  38509. + else i = 0;
  38510. + memcpy(&context->buffer[j], &data[i], len - i);
  38511. +}
  38512. +
  38513. +
  38514. +/* Add padding and return the message digest. */
  38515. +
  38516. +void SHA1Final(unsigned char digest[20], void *vcontext)
  38517. +{
  38518. + __u32 i, j;
  38519. + unsigned char finalcount[8];
  38520. + SHA1_CTX* context = vcontext;
  38521. +
  38522. + for (i = 0; i < 8; i++) {
  38523. + finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)]
  38524. + >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
  38525. + }
  38526. + SHA1Update(context, (unsigned char *)"\200", 1);
  38527. + while ((context->count[0] & 504) != 448) {
  38528. + SHA1Update(context, (unsigned char *)"\0", 1);
  38529. + }
  38530. + SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
  38531. + for (i = 0; i < 20; i++) {
  38532. + digest[i] = (unsigned char)
  38533. + ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
  38534. + }
  38535. + /* Wipe variables */
  38536. + i = j = 0;
  38537. + memset(context->buffer, 0, 64);
  38538. + memset(context->state, 0, 20);
  38539. + memset(context->count, 0, 8);
  38540. + memset(&finalcount, 0, 8);
  38541. +#ifdef SHA1HANDSOFF /* make SHA1Transform overwrite its own static vars */
  38542. + SHA1Transform(context->state, context->buffer);
  38543. +#endif
  38544. +}
  38545. +
  38546. +
  38547. +/*
  38548. + * $Log: ipsec_sha1.c,v $
  38549. + * Revision 1.9 2004/04/06 02:49:26 mcr
  38550. + * pullup of algo code from alg-branch.
  38551. + *
  38552. + * Revision 1.8 2002/09/10 01:45:14 mcr
  38553. + * changed type of MD5_CTX and SHA1_CTX to void * so that
  38554. + * the function prototypes would match, and could be placed
  38555. + * into a pointer to a function.
  38556. + *
  38557. + * Revision 1.7 2002/04/24 07:55:32 mcr
  38558. + * #include patches and Makefiles for post-reorg compilation.
  38559. + *
  38560. + * Revision 1.6 2002/04/24 07:36:30 mcr
  38561. + * Moved from ./klips/net/ipsec/ipsec_sha1.c,v
  38562. + *
  38563. + * Revision 1.5 1999/12/13 13:59:13 rgb
  38564. + * Quick fix to argument size to Update bugs.
  38565. + *
  38566. + * Revision 1.4 1999/04/11 00:29:00 henry
  38567. + * GPL boilerplate
  38568. + *
  38569. + * Revision 1.3 1999/04/06 04:54:27 rgb
  38570. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  38571. + * patch shell fixes.
  38572. + *
  38573. + * Revision 1.2 1999/01/22 06:55:50 rgb
  38574. + * 64-bit clean-up.
  38575. + *
  38576. + * Revision 1.1 1998/06/18 21:27:50 henry
  38577. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  38578. + * kernel-build scripts happier in the presence of symlinks
  38579. + *
  38580. + * Revision 1.2 1998/04/23 20:54:04 rgb
  38581. + * Fixed md5 and sha1 include file nesting issues, to be cleaned up when
  38582. + * verified.
  38583. + *
  38584. + * Revision 1.1 1998/04/09 03:06:11 henry
  38585. + * sources moved up from linux/net/ipsec
  38586. + *
  38587. + * Revision 1.1.1.1 1998/04/08 05:35:05 henry
  38588. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  38589. + *
  38590. + * Revision 0.4 1997/01/15 01:28:15 ji
  38591. + * New transform
  38592. + *
  38593. + *
  38594. + */
  38595. --- /dev/null Tue Mar 11 13:02:56 2003
  38596. +++ linux/net/ipsec/ipsec_snprintf.c Mon Feb 9 13:51:03 2004
  38597. @@ -0,0 +1,135 @@
  38598. +/*
  38599. + * @(#) ipsec_snprintf() function
  38600. + *
  38601. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs <rgb@freeswan.org>
  38602. + * 2001 Michael Richardson <mcr@freeswan.org>
  38603. + * Copyright (C) 2005 Michael Richardson <mcr@xelerance.com>
  38604. + *
  38605. + * This program is free software; you can redistribute it and/or modify it
  38606. + * under the terms of the GNU General Public License as published by the
  38607. + * Free Software Foundation; either version 2 of the License, or (at your
  38608. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  38609. + *
  38610. + * This program is distributed in the hope that it will be useful, but
  38611. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  38612. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  38613. + * for more details.
  38614. + *
  38615. + * Split out from ipsec_proc.c.
  38616. + */
  38617. +
  38618. +#ifndef AUTOCONF_INCLUDED
  38619. +#include <linux/config.h>
  38620. +#endif
  38621. +#include <linux/version.h>
  38622. +#define __NO_VERSION__
  38623. +#include <linux/module.h>
  38624. +#include <linux/kernel.h> /* printk() */
  38625. +
  38626. +#include "openswan/ipsec_kversion.h"
  38627. +#include "openswan/ipsec_param.h"
  38628. +
  38629. +#include <net/ip.h>
  38630. +
  38631. +#include "openswan/radij.h"
  38632. +
  38633. +#include "openswan/ipsec_life.h"
  38634. +#include "openswan/ipsec_stats.h"
  38635. +#include "openswan/ipsec_sa.h"
  38636. +
  38637. +#include "openswan/ipsec_encap.h"
  38638. +#include "openswan/ipsec_radij.h"
  38639. +#include "openswan/ipsec_xform.h"
  38640. +#include "openswan/ipsec_tunnel.h"
  38641. +#include "openswan/ipsec_xmit.h"
  38642. +
  38643. +#include "openswan/ipsec_rcv.h"
  38644. +#include "openswan/ipsec_ah.h"
  38645. +#include "openswan/ipsec_esp.h"
  38646. +#include "openswan/ipsec_kern24.h"
  38647. +
  38648. +#ifdef CONFIG_KLIPS_IPCOMP
  38649. +#include "openswan/ipcomp.h"
  38650. +#endif /* CONFIG_KLIPS_IPCOMP */
  38651. +
  38652. +#include "openswan/ipsec_proto.h"
  38653. +
  38654. +#include <pfkeyv2.h>
  38655. +#include <pfkey.h>
  38656. +
  38657. +/* ipsec_snprintf: like snprintf except
  38658. + * - size is signed and a negative value is treated as if it were 0
  38659. + * - the returned result is never negative --
  38660. + * an error generates a "?" or null output (depending on space).
  38661. + * (Our callers are too lazy to check for an error return.)
  38662. + *
  38663. + * @param buf String buffer
  38664. + * @param size Size of the string
  38665. + * @param fmt printf string
  38666. + * @param ... Variables to be displayed in fmt
  38667. + * @return int Return code
  38668. + */
  38669. +int ipsec_snprintf(char *buf, ssize_t size, const char *fmt, ...)
  38670. +{
  38671. + va_list args;
  38672. + int i;
  38673. + size_t possize = size < 0? 0 : size;
  38674. + va_start(args, fmt);
  38675. + i = vsnprintf(buf,possize,fmt,args);
  38676. + va_end(args);
  38677. + if (i < 0) {
  38678. + /* create empty output in place of error */
  38679. + i = 0;
  38680. + if (size > 0) {
  38681. + *buf = '\0';
  38682. + }
  38683. + }
  38684. + return i;
  38685. +}
  38686. +
  38687. +
  38688. +void ipsec_dmp_block(char *s, caddr_t bb, int len)
  38689. +{
  38690. + int i;
  38691. + unsigned char *b = bb;
  38692. +
  38693. + printk(KERN_INFO "klips_dmp: "
  38694. + "at %s, len=%d:\n", s, len);
  38695. +
  38696. + for(i = 0; i < len; i++ /*, c++*/) {
  38697. + if(!(i % 16)) {
  38698. + printk(KERN_INFO
  38699. + "klips_debug: @%03x:",
  38700. + i);
  38701. + }
  38702. + printk(" %02x", b[i]);
  38703. + if(!((i + 1) % 16)) {
  38704. + printk("\n");
  38705. + }
  38706. + }
  38707. + if(i % 16) {
  38708. + printk("\n");
  38709. + }
  38710. +}
  38711. +
  38712. +/*
  38713. + *
  38714. + * $Log: ipsec_snprintf.c,v $
  38715. + * Revision 1.3.2.1 2006/10/06 21:39:26 paul
  38716. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  38717. + * set. This is defined through autoconf.h which is included through the
  38718. + * linux kernel build macros.
  38719. + *
  38720. + * Revision 1.3 2005/04/29 05:10:22 mcr
  38721. + * removed from extraenous includes to make unit testing easier.
  38722. + *
  38723. + * Revision 1.2 2005/04/15 00:32:01 mcr
  38724. + * added ipsec_dmp_block routine.
  38725. + *
  38726. + *
  38727. + * Local Variables:
  38728. + * c-file-style: "linux"
  38729. + * End:
  38730. + *
  38731. + */
  38732. +
  38733. --- /dev/null Tue Mar 11 13:02:56 2003
  38734. +++ linux/net/ipsec/ipsec_tunnel.c Mon Feb 9 13:51:03 2004
  38735. @@ -0,0 +1,2878 @@
  38736. +/*
  38737. + * IPSEC Tunneling code. Heavily based on drivers/net/new_tunnel.c
  38738. + * Copyright (C) 1996, 1997 John Ioannidis.
  38739. + * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Richard Guy Briggs.
  38740. + *
  38741. + * This program is free software; you can redistribute it and/or modify it
  38742. + * under the terms of the GNU General Public License as published by the
  38743. + * Free Software Foundation; either version 2 of the License, or (at your
  38744. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  38745. + *
  38746. + * This program is distributed in the hope that it will be useful, but
  38747. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  38748. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  38749. + * for more details.
  38750. + */
  38751. +
  38752. +char ipsec_tunnel_c_version[] = "RCSID $Id: ipsec_tunnel.c,v 1.232.2.5 2006/10/06 21:39:26 paul Exp $";
  38753. +
  38754. +#define __NO_VERSION__
  38755. +#include <linux/module.h>
  38756. +#ifndef AUTOCONF_INCLUDED
  38757. +#include <linux/config.h>
  38758. +#endif /* for CONFIG_IP_FORWARD */
  38759. +#include <linux/version.h>
  38760. +#include <linux/kernel.h> /* printk() */
  38761. +
  38762. +#include "openswan/ipsec_param.h"
  38763. +
  38764. +#ifdef MALLOC_SLAB
  38765. +# include <linux/slab.h> /* kmalloc() */
  38766. +#else /* MALLOC_SLAB */
  38767. +# include <linux/malloc.h> /* kmalloc() */
  38768. +#endif /* MALLOC_SLAB */
  38769. +#include <linux/errno.h> /* error codes */
  38770. +#include <linux/types.h> /* size_t */
  38771. +#include <linux/interrupt.h> /* mark_bh */
  38772. +
  38773. +#include <net/tcp.h>
  38774. +#include <net/udp.h>
  38775. +#include <linux/skbuff.h>
  38776. +
  38777. +#include <linux/netdevice.h> /* struct device, struct net_device_stats, dev_queue_xmit() and other headers */
  38778. +#include <linux/etherdevice.h> /* eth_type_trans */
  38779. +#include <linux/ip.h> /* struct iphdr */
  38780. +#include <linux/skbuff.h>
  38781. +
  38782. +#include <openswan.h>
  38783. +
  38784. +#ifdef NET_21
  38785. +# include <linux/in6.h>
  38786. +# define ip_chk_addr inet_addr_type
  38787. +# define IS_MYADDR RTN_LOCAL
  38788. +# include <net/dst.h>
  38789. +# undef dev_kfree_skb
  38790. +# define dev_kfree_skb(a,b) kfree_skb(a)
  38791. +# define PHYSDEV_TYPE
  38792. +#endif /* NET_21 */
  38793. +
  38794. +#include <net/icmp.h> /* icmp_send() */
  38795. +#include <net/ip.h>
  38796. +#ifdef NETDEV_23
  38797. +# include <linux/netfilter_ipv4.h>
  38798. +#endif /* NETDEV_23 */
  38799. +
  38800. +#include <linux/if_arp.h>
  38801. +#include <net/arp.h>
  38802. +
  38803. +#include "openswan/ipsec_kversion.h"
  38804. +#include "openswan/radij.h"
  38805. +#include "openswan/ipsec_life.h"
  38806. +#include "openswan/ipsec_xform.h"
  38807. +#include "openswan/ipsec_eroute.h"
  38808. +#include "openswan/ipsec_encap.h"
  38809. +#include "openswan/ipsec_radij.h"
  38810. +#include "openswan/ipsec_sa.h"
  38811. +#include "openswan/ipsec_tunnel.h"
  38812. +#include "openswan/ipsec_xmit.h"
  38813. +#include "openswan/ipsec_ipe4.h"
  38814. +#include "openswan/ipsec_ah.h"
  38815. +#include "openswan/ipsec_esp.h"
  38816. +#include "openswan/ipsec_kern24.h"
  38817. +
  38818. +#include <pfkeyv2.h>
  38819. +#include <pfkey.h>
  38820. +
  38821. +#include "openswan/ipsec_proto.h"
  38822. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  38823. +#include <linux/udp.h>
  38824. +#endif
  38825. +
  38826. +static __u32 zeroes[64];
  38827. +
  38828. +#ifdef CONFIG_KLIPS_DEBUG
  38829. +int debug_tunnel = 0;
  38830. +#endif /* CONFIG_KLIPS_DEBUG */
  38831. +
  38832. +DEBUG_NO_STATIC int
  38833. +ipsec_tunnel_open(struct net_device *dev)
  38834. +{
  38835. + struct ipsecpriv *prv = dev->priv;
  38836. +
  38837. + /*
  38838. + * Can't open until attached.
  38839. + */
  38840. +
  38841. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  38842. + "klips_debug:ipsec_tunnel_open: "
  38843. + "dev = %s, prv->dev = %s\n",
  38844. + dev->name, prv->dev?prv->dev->name:"NONE");
  38845. +
  38846. + if (prv->dev == NULL)
  38847. + return -ENODEV;
  38848. +
  38849. + KLIPS_INC_USE;
  38850. + return 0;
  38851. +}
  38852. +
  38853. +DEBUG_NO_STATIC int
  38854. +ipsec_tunnel_close(struct net_device *dev)
  38855. +{
  38856. + KLIPS_DEC_USE;
  38857. + return 0;
  38858. +}
  38859. +
  38860. +#ifdef NETDEV_23
  38861. +static inline int ipsec_tunnel_xmit2(struct sk_buff *skb)
  38862. +{
  38863. +#ifdef NETDEV_25 /* 2.6 kernels */
  38864. + return dst_output(skb);
  38865. +#else
  38866. + return ip_send(skb);
  38867. +#endif
  38868. +}
  38869. +#endif /* NETDEV_23 */
  38870. +
  38871. +enum ipsec_xmit_value
  38872. +ipsec_tunnel_strip_hard_header(struct ipsec_xmit_state *ixs)
  38873. +{
  38874. + /* ixs->physdev->hard_header_len is unreliable and should not be used */
  38875. + ixs->hard_header_len = (unsigned char *)(ixs->iph) - ixs->skb->data;
  38876. +
  38877. + if(ixs->hard_header_len < 0) {
  38878. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  38879. + "klips_error:ipsec_xmit_strip_hard_header: "
  38880. + "Negative hard_header_len (%d)?!\n", ixs->hard_header_len);
  38881. + ixs->stats->tx_dropped++;
  38882. + return IPSEC_XMIT_BADHHLEN;
  38883. + }
  38884. +
  38885. + /* while ixs->physdev->hard_header_len is unreliable and
  38886. + * should not be trusted, it accurate and required for ATM, GRE and
  38887. + * some other interfaces to work. Thanks to Willy Tarreau
  38888. + * <willy@w.ods.org>.
  38889. + */
  38890. + if(ixs->hard_header_len == 0) { /* no hard header present */
  38891. + ixs->hard_header_stripped = 1;
  38892. + ixs->hard_header_len = ixs->physdev->hard_header_len;
  38893. + }
  38894. +
  38895. +#ifdef CONFIG_KLIPS_DEBUG
  38896. + if (debug_tunnel & DB_TN_XMIT) {
  38897. + int i;
  38898. + char c;
  38899. +
  38900. + printk(KERN_INFO "klips_debug:ipsec_xmit_strip_hard_header: "
  38901. + ">>> skb->len=%ld hard_header_len:%d",
  38902. + (unsigned long int)ixs->skb->len, ixs->hard_header_len);
  38903. + c = ' ';
  38904. + for (i=0; i < ixs->hard_header_len; i++) {
  38905. + printk("%c%02x", c, ixs->skb->data[i]);
  38906. + c = ':';
  38907. + }
  38908. + printk(" \n");
  38909. + }
  38910. +#endif /* CONFIG_KLIPS_DEBUG */
  38911. +
  38912. + KLIPS_IP_PRINT(debug_tunnel & DB_TN_XMIT, ixs->iph);
  38913. +
  38914. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  38915. + "klips_debug:ipsec_xmit_strip_hard_header: "
  38916. + "Original head,tailroom: %d,%d\n",
  38917. + skb_headroom(ixs->skb), skb_tailroom(ixs->skb));
  38918. +
  38919. + return IPSEC_XMIT_OK;
  38920. +}
  38921. +
  38922. +enum ipsec_xmit_value
  38923. +ipsec_tunnel_SAlookup(struct ipsec_xmit_state *ixs)
  38924. +{
  38925. + unsigned int bypass;
  38926. +
  38927. + bypass = FALSE;
  38928. +
  38929. + /*
  38930. + * First things first -- look us up in the erouting tables.
  38931. + */
  38932. + ixs->matcher.sen_len = sizeof (struct sockaddr_encap);
  38933. + ixs->matcher.sen_family = AF_ENCAP;
  38934. + ixs->matcher.sen_type = SENT_IP4;
  38935. + ixs->matcher.sen_ip_src.s_addr = ixs->iph->saddr;
  38936. + ixs->matcher.sen_ip_dst.s_addr = ixs->iph->daddr;
  38937. + ixs->matcher.sen_proto = ixs->iph->protocol;
  38938. + ipsec_extract_ports(ixs->iph, &ixs->matcher);
  38939. +
  38940. + /*
  38941. + * The spinlock is to prevent any other process from accessing or deleting
  38942. + * the eroute while we are using and updating it.
  38943. + */
  38944. + spin_lock(&eroute_lock);
  38945. +
  38946. + ixs->eroute = ipsec_findroute(&ixs->matcher);
  38947. +
  38948. + if(ixs->iph->protocol == IPPROTO_UDP) {
  38949. + struct udphdr *t = NULL;
  38950. +
  38951. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  38952. + "klips_debug:udp port check: "
  38953. + "fragoff: %d len: %d>%ld \n",
  38954. + ntohs(ixs->iph->frag_off) & IP_OFFSET,
  38955. + (ixs->skb->len - ixs->hard_header_len),
  38956. + (unsigned long int) ((ixs->iph->ihl << 2) + sizeof(struct udphdr)));
  38957. +
  38958. + if((ntohs(ixs->iph->frag_off) & IP_OFFSET) == 0 &&
  38959. + ((ixs->skb->len - ixs->hard_header_len) >=
  38960. + ((ixs->iph->ihl << 2) + sizeof(struct udphdr))))
  38961. + {
  38962. + t =((struct udphdr*)((caddr_t)ixs->iph+(ixs->iph->ihl<<2)));
  38963. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  38964. + "klips_debug:udp port in packet: "
  38965. + "port %d -> %d\n",
  38966. + ntohs(t->source), ntohs(t->dest));
  38967. + }
  38968. +
  38969. + ixs->sport=0; ixs->dport=0;
  38970. +
  38971. + if(ixs->skb->sk) {
  38972. +#ifdef NET_26
  38973. + struct udp_sock *us;
  38974. +
  38975. + us = (struct udp_sock *)ixs->skb->sk;
  38976. +
  38977. + ixs->sport = ntohs(us->inet.sport);
  38978. + ixs->dport = ntohs(us->inet.dport);
  38979. +#else
  38980. + ixs->sport = ntohs(ixs->skb->sk->sport);
  38981. + ixs->dport = ntohs(ixs->skb->sk->dport);
  38982. +#endif
  38983. +
  38984. + }
  38985. +
  38986. + if(t != NULL) {
  38987. + if(ixs->sport == 0) {
  38988. + ixs->sport = ntohs(t->source);
  38989. + }
  38990. + if(ixs->dport == 0) {
  38991. + ixs->dport = ntohs(t->dest);
  38992. + }
  38993. + }
  38994. + }
  38995. +
  38996. + /*
  38997. + * practically identical to above, but let's be careful about
  38998. + * tcp vs udp headers
  38999. + */
  39000. + if(ixs->iph->protocol == IPPROTO_TCP) {
  39001. + struct tcphdr *t = NULL;
  39002. +
  39003. + if((ntohs(ixs->iph->frag_off) & IP_OFFSET) == 0 &&
  39004. + ((ixs->skb->len - ixs->hard_header_len) >=
  39005. + ((ixs->iph->ihl << 2) + sizeof(struct tcphdr)))) {
  39006. + t =((struct tcphdr*)((caddr_t)ixs->iph+(ixs->iph->ihl<<2)));
  39007. + }
  39008. +
  39009. + ixs->sport=0; ixs->dport=0;
  39010. +
  39011. + if(ixs->skb->sk) {
  39012. +#ifdef NET_26
  39013. +#ifdef HAVE_INET_SK_SPORT
  39014. + ixs->sport = ntohs(inet_sk(ixs->skb->sk)->sport);
  39015. + ixs->dport = ntohs(inet_sk(ixs->skb->sk)->dport);
  39016. +#else
  39017. + struct tcp_tw_bucket *tw;
  39018. +
  39019. + tw = (struct tcp_tw_bucket *)ixs->skb->sk;
  39020. +
  39021. + ixs->sport = ntohs(tw->tw_sport);
  39022. + ixs->dport = ntohs(tw->tw_dport);
  39023. +#endif
  39024. +#else
  39025. + ixs->sport = ntohs(ixs->skb->sk->sport);
  39026. + ixs->dport = ntohs(ixs->skb->sk->dport);
  39027. +#endif
  39028. + }
  39029. +
  39030. + if(t != NULL) {
  39031. + if(ixs->sport == 0) {
  39032. + ixs->sport = ntohs(t->source);
  39033. + }
  39034. + if(ixs->dport == 0) {
  39035. + ixs->dport = ntohs(t->dest);
  39036. + }
  39037. + }
  39038. + }
  39039. +
  39040. + /* default to a %drop eroute */
  39041. + ixs->outgoing_said.proto = IPPROTO_INT;
  39042. + ixs->outgoing_said.spi = htonl(SPI_DROP);
  39043. + ixs->outgoing_said.dst.u.v4.sin_addr.s_addr = INADDR_ANY;
  39044. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39045. + "klips_debug:ipsec_xmit_SAlookup: "
  39046. + "checking for local udp/500 IKE packet "
  39047. + "saddr=%x, er=0p%p, daddr=%x, er_dst=%x, proto=%d sport=%d dport=%d\n",
  39048. + ntohl((unsigned int)ixs->iph->saddr),
  39049. + ixs->eroute,
  39050. + ntohl((unsigned int)ixs->iph->daddr),
  39051. + ixs->eroute ? ntohl((unsigned int)ixs->eroute->er_said.dst.u.v4.sin_addr.s_addr) : 0,
  39052. + ixs->iph->protocol,
  39053. + ixs->sport,
  39054. + ixs->dport);
  39055. +
  39056. + /*
  39057. + * cheat for now...are we udp/500? If so, let it through
  39058. + * without interference since it is most likely an IKE packet.
  39059. + */
  39060. +
  39061. + if (ip_chk_addr((unsigned long)ixs->iph->saddr) == IS_MYADDR
  39062. + && (ixs->eroute==NULL
  39063. + || ixs->iph->daddr == ixs->eroute->er_said.dst.u.v4.sin_addr.s_addr
  39064. + || INADDR_ANY == ixs->eroute->er_said.dst.u.v4.sin_addr.s_addr)
  39065. + && (ixs->iph->protocol == IPPROTO_UDP &&
  39066. + (ixs->sport == 500 || ixs->sport == 4500))) {
  39067. + /* Whatever the eroute, this is an IKE message
  39068. + * from us (i.e. not being forwarded).
  39069. + * Furthermore, if there is a tunnel eroute,
  39070. + * the destination is the peer for this eroute.
  39071. + * So %pass the packet: modify the default %drop.
  39072. + */
  39073. +
  39074. + ixs->outgoing_said.spi = htonl(SPI_PASS);
  39075. + if(!(ixs->skb->sk) && ((ntohs(ixs->iph->frag_off) & IP_MF) != 0)) {
  39076. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39077. + "klips_debug:ipsec_xmit_SAlookup: "
  39078. + "local UDP/500 (probably IKE) passthrough: base fragment, rest of fragments will probably get filtered.\n");
  39079. + }
  39080. + bypass = TRUE;
  39081. + }
  39082. +
  39083. +#ifdef KLIPS_EXCEPT_DNS53
  39084. + /*
  39085. + *
  39086. + * if we are udp/53 or tcp/53, also let it through a %trap or %hold,
  39087. + * since it is DNS, but *also* follow the %trap.
  39088. + *
  39089. + * we do not do this for tunnels, only %trap's and %hold's.
  39090. + *
  39091. + */
  39092. +
  39093. + if (ip_chk_addr((unsigned long)ixs->iph->saddr) == IS_MYADDR
  39094. + && (ixs->eroute==NULL
  39095. + || ixs->iph->daddr == ixs->eroute->er_said.dst.u.v4.sin_addr.s_addr
  39096. + || INADDR_ANY == ixs->eroute->er_said.dst.u.v4.sin_addr.s_addr)
  39097. + && ((ixs->iph->protocol == IPPROTO_UDP
  39098. + || ixs->iph->protocol == IPPROTO_TCP)
  39099. + && ixs->dport == 53)) {
  39100. +
  39101. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39102. + "klips_debug:ipsec_xmit_SAlookup: "
  39103. + "possible DNS packet\n");
  39104. +
  39105. + if(ixs->eroute)
  39106. + {
  39107. + if(ixs->eroute->er_said.spi == htonl(SPI_TRAP)
  39108. + || ixs->eroute->er_said.spi == htonl(SPI_HOLD))
  39109. + {
  39110. + ixs->outgoing_said.spi = htonl(SPI_PASSTRAP);
  39111. + bypass = TRUE;
  39112. + }
  39113. + }
  39114. + else
  39115. + {
  39116. + ixs->outgoing_said.spi = htonl(SPI_PASSTRAP);
  39117. + bypass = TRUE;
  39118. + }
  39119. +
  39120. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39121. + "klips_debug:ipsec_xmit_SAlookup: "
  39122. + "bypass = %d\n", bypass);
  39123. +
  39124. + if(bypass
  39125. + && !(ixs->skb->sk)
  39126. + && ((ntohs(ixs->iph->frag_off) & IP_MF) != 0))
  39127. + {
  39128. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39129. + "klips_debug:ipsec_xmit_SAlookup: "
  39130. + "local port 53 (probably DNS) passthrough:"
  39131. + "base fragment, rest of fragments will "
  39132. + "probably get filtered.\n");
  39133. + }
  39134. + }
  39135. +#endif
  39136. +
  39137. + if (bypass==FALSE && ixs->eroute) {
  39138. + ixs->eroute->er_count++;
  39139. + ixs->eroute->er_lasttime = jiffies/HZ;
  39140. + if(ixs->eroute->er_said.proto==IPPROTO_INT
  39141. + && ixs->eroute->er_said.spi==htonl(SPI_HOLD))
  39142. + {
  39143. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39144. + "klips_debug:ipsec_xmit_SAlookup: "
  39145. + "shunt SA of HOLD: skb stored in HOLD.\n");
  39146. + if(ixs->eroute->er_last != NULL) {
  39147. + kfree_skb(ixs->eroute->er_last);
  39148. + }
  39149. + ixs->eroute->er_last = ixs->skb;
  39150. + ixs->skb = NULL;
  39151. + ixs->stats->tx_dropped++;
  39152. + spin_unlock(&eroute_lock);
  39153. + return IPSEC_XMIT_STOLEN;
  39154. + }
  39155. + ixs->outgoing_said = ixs->eroute->er_said;
  39156. + ixs->eroute_pid = ixs->eroute->er_pid;
  39157. +
  39158. + /* Copy of the ident for the TRAP/TRAPSUBNET eroutes */
  39159. + if(ixs->outgoing_said.proto==IPPROTO_INT
  39160. + && (ixs->outgoing_said.spi==htonl(SPI_TRAP)
  39161. + || (ixs->outgoing_said.spi==htonl(SPI_TRAPSUBNET)))) {
  39162. + int len;
  39163. +
  39164. + ixs->ips.ips_ident_s.type = ixs->eroute->er_ident_s.type;
  39165. + ixs->ips.ips_ident_s.id = ixs->eroute->er_ident_s.id;
  39166. + ixs->ips.ips_ident_s.len = ixs->eroute->er_ident_s.len;
  39167. + if (ixs->ips.ips_ident_s.len)
  39168. + {
  39169. + len = ixs->ips.ips_ident_s.len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  39170. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39171. + "klips_debug:ipsec_xmit_SAlookup: "
  39172. + "allocating %d bytes for ident_s shunt SA of HOLD: skb stored in HOLD.\n",
  39173. + len);
  39174. + if ((ixs->ips.ips_ident_s.data = kmalloc(len, GFP_ATOMIC)) == NULL) {
  39175. + printk(KERN_WARNING "klips_debug:ipsec_xmit_SAlookup: "
  39176. + "Failed, tried to allocate %d bytes for source ident.\n",
  39177. + len);
  39178. + ixs->stats->tx_dropped++;
  39179. + spin_unlock(&eroute_lock);
  39180. + return IPSEC_XMIT_ERRMEMALLOC;
  39181. + }
  39182. + memcpy(ixs->ips.ips_ident_s.data, ixs->eroute->er_ident_s.data, len);
  39183. + }
  39184. + ixs->ips.ips_ident_d.type = ixs->eroute->er_ident_d.type;
  39185. + ixs->ips.ips_ident_d.id = ixs->eroute->er_ident_d.id;
  39186. + ixs->ips.ips_ident_d.len = ixs->eroute->er_ident_d.len;
  39187. + if (ixs->ips.ips_ident_d.len)
  39188. + {
  39189. + len = ixs->ips.ips_ident_d.len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  39190. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39191. + "klips_debug:ipsec_xmit_SAlookup: "
  39192. + "allocating %d bytes for ident_d shunt SA of HOLD: skb stored in HOLD.\n",
  39193. + len);
  39194. + if ((ixs->ips.ips_ident_d.data = kmalloc(len, GFP_ATOMIC)) == NULL) {
  39195. + printk(KERN_WARNING "klips_debug:ipsec_xmit_SAlookup: "
  39196. + "Failed, tried to allocate %d bytes for dest ident.\n",
  39197. + len);
  39198. + ixs->stats->tx_dropped++;
  39199. + spin_unlock(&eroute_lock);
  39200. + return IPSEC_XMIT_ERRMEMALLOC;
  39201. + }
  39202. + memcpy(ixs->ips.ips_ident_d.data, ixs->eroute->er_ident_d.data, len);
  39203. + }
  39204. + }
  39205. + }
  39206. +
  39207. + spin_unlock(&eroute_lock);
  39208. + return IPSEC_XMIT_OK;
  39209. +}
  39210. +
  39211. +
  39212. +enum ipsec_xmit_value
  39213. +ipsec_tunnel_restore_hard_header(struct ipsec_xmit_state*ixs)
  39214. +{
  39215. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  39216. + "klips_debug:ipsec_xmit_restore_hard_header: "
  39217. + "After recursive xforms -- head,tailroom: %d,%d\n",
  39218. + skb_headroom(ixs->skb),
  39219. + skb_tailroom(ixs->skb));
  39220. +
  39221. + if(ixs->saved_header) {
  39222. + if(skb_headroom(ixs->skb) < ixs->hard_header_len) {
  39223. + printk(KERN_WARNING
  39224. + "klips_error:ipsec_xmit_restore_hard_header: "
  39225. + "tried to skb_push hhlen=%d, %d available. This should never happen, please report.\n",
  39226. + ixs->hard_header_len,
  39227. + skb_headroom(ixs->skb));
  39228. + ixs->stats->tx_errors++;
  39229. + return IPSEC_XMIT_PUSHPULLERR;
  39230. +
  39231. + }
  39232. + skb_push(ixs->skb, ixs->hard_header_len);
  39233. + {
  39234. + int i;
  39235. + for (i = 0; i < ixs->hard_header_len; i++) {
  39236. + ixs->skb->data[i] = ixs->saved_header[i];
  39237. + }
  39238. + }
  39239. + }
  39240. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  39241. + if (ixs->natt_type && ixs->natt_head) {
  39242. + struct iphdr *ipp = ixs->skb->nh.iph;
  39243. + struct udphdr *udp;
  39244. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39245. + "klips_debug:ipsec_tunnel_start_xmit: "
  39246. + "encapsuling packet into UDP (NAT-Traversal) (%d %d)\n",
  39247. + ixs->natt_type, ixs->natt_head);
  39248. +
  39249. + ixs->iphlen = ipp->ihl << 2;
  39250. + ipp->tot_len =
  39251. + htons(ntohs(ipp->tot_len) + ixs->natt_head);
  39252. + if(skb_tailroom(ixs->skb) < ixs->natt_head) {
  39253. + printk(KERN_WARNING "klips_error:ipsec_tunnel_start_xmit: "
  39254. + "tried to skb_put %d, %d available. "
  39255. + "This should never happen, please report.\n",
  39256. + ixs->natt_head,
  39257. + skb_tailroom(ixs->skb));
  39258. + ixs->stats->tx_errors++;
  39259. + return IPSEC_XMIT_ESPUDP;
  39260. + }
  39261. + skb_put(ixs->skb, ixs->natt_head);
  39262. +
  39263. + udp = (struct udphdr *)((char *)ipp + ixs->iphlen);
  39264. +
  39265. + /* move ESP hdr after UDP hdr */
  39266. + memmove((void *)((char *)udp + ixs->natt_head),
  39267. + (void *)(udp),
  39268. + ntohs(ipp->tot_len) - ixs->iphlen - ixs->natt_head);
  39269. +
  39270. + /* clear UDP & Non-IKE Markers (if any) */
  39271. + memset(udp, 0, ixs->natt_head);
  39272. +
  39273. + /* fill UDP with usefull informations ;-) */
  39274. + udp->source = htons(ixs->natt_sport);
  39275. + udp->dest = htons(ixs->natt_dport);
  39276. + udp->len = htons(ntohs(ipp->tot_len) - ixs->iphlen);
  39277. +
  39278. + /* set protocol */
  39279. + ipp->protocol = IPPROTO_UDP;
  39280. +
  39281. + /* fix IP checksum */
  39282. + ipp->check = 0;
  39283. + ipp->check = ip_fast_csum((unsigned char *)ipp, ipp->ihl);
  39284. + }
  39285. +#endif
  39286. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  39287. + "klips_debug:ipsec_xmit_restore_hard_header: "
  39288. + "With hard_header, final head,tailroom: %d,%d\n",
  39289. + skb_headroom(ixs->skb),
  39290. + skb_tailroom(ixs->skb));
  39291. +
  39292. + return IPSEC_XMIT_OK;
  39293. +}
  39294. +
  39295. +enum ipsec_xmit_value
  39296. +ipsec_tunnel_send(struct ipsec_xmit_state*ixs)
  39297. +{
  39298. +#ifdef NETDEV_25
  39299. + struct flowi fl;
  39300. +#endif
  39301. +
  39302. +#ifdef NET_21 /* 2.2 and 2.4 kernels */
  39303. + /* new route/dst cache code from James Morris */
  39304. + ixs->skb->dev = ixs->physdev;
  39305. +#ifdef NETDEV_25
  39306. + memset (&fl, 0x0, sizeof (struct flowi));
  39307. + fl.oif = ixs->physdev->iflink;
  39308. + fl.nl_u.ip4_u.daddr = ixs->skb->nh.iph->daddr;
  39309. + fl.nl_u.ip4_u.saddr = ixs->pass ? 0 : ixs->skb->nh.iph->saddr;
  39310. + fl.nl_u.ip4_u.tos = RT_TOS(ixs->skb->nh.iph->tos);
  39311. + fl.proto = ixs->skb->nh.iph->protocol;
  39312. + if ((ixs->error = ip_route_output_key(&ixs->route, &fl))) {
  39313. +#else
  39314. + /*skb_orphan(ixs->skb);*/
  39315. + if((ixs->error = ip_route_output(&ixs->route,
  39316. + ixs->skb->nh.iph->daddr,
  39317. + ixs->pass ? 0 : ixs->skb->nh.iph->saddr,
  39318. + RT_TOS(ixs->skb->nh.iph->tos),
  39319. + /* mcr->rgb: should this be 0 instead? */
  39320. + ixs->physdev->iflink))) {
  39321. +#endif
  39322. + ixs->stats->tx_errors++;
  39323. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39324. + "klips_debug:ipsec_xmit_send: "
  39325. + "ip_route_output failed with error code %d, rt->u.dst.dev=%s, dropped\n",
  39326. + ixs->error,
  39327. + ixs->route->u.dst.dev->name);
  39328. + return IPSEC_XMIT_ROUTEERR;
  39329. + }
  39330. + if(ixs->dev == ixs->route->u.dst.dev) {
  39331. + ip_rt_put(ixs->route);
  39332. + /* This is recursion, drop it. */
  39333. + ixs->stats->tx_errors++;
  39334. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39335. + "klips_debug:ipsec_xmit_send: "
  39336. + "suspect recursion, dev=rt->u.dst.dev=%s, dropped\n",
  39337. + ixs->dev->name);
  39338. + return IPSEC_XMIT_RECURSDETECT;
  39339. + }
  39340. + dst_release(ixs->skb->dst);
  39341. + ixs->skb->dst = &ixs->route->u.dst;
  39342. + ixs->stats->tx_bytes += ixs->skb->len;
  39343. + if(ixs->skb->len < ixs->skb->nh.raw - ixs->skb->data) {
  39344. + ixs->stats->tx_errors++;
  39345. + printk(KERN_WARNING
  39346. + "klips_error:ipsec_xmit_send: "
  39347. + "tried to __skb_pull nh-data=%ld, %d available. This should never happen, please report.\n",
  39348. + (unsigned long)(ixs->skb->nh.raw - ixs->skb->data),
  39349. + ixs->skb->len);
  39350. + return IPSEC_XMIT_PUSHPULLERR;
  39351. + }
  39352. + __skb_pull(ixs->skb, ixs->skb->nh.raw - ixs->skb->data);
  39353. +#ifdef SKB_RESET_NFCT
  39354. + if(!ixs->pass) {
  39355. + nf_conntrack_put(ixs->skb->nfct);
  39356. + ixs->skb->nfct = NULL;
  39357. + }
  39358. +#if defined(CONFIG_NETFILTER_DEBUG) && defined(HAVE_SKB_NF_DEBUG)
  39359. + ixs->skb->nf_debug = 0;
  39360. +#endif /* CONFIG_NETFILTER_DEBUG */
  39361. +#endif /* SKB_RESET_NFCT */
  39362. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39363. + "klips_debug:ipsec_xmit_send: "
  39364. + "...done, calling ip_send() on device:%s\n",
  39365. + ixs->skb->dev ? ixs->skb->dev->name : "NULL");
  39366. + KLIPS_IP_PRINT(debug_tunnel & DB_TN_XMIT, ixs->skb->nh.iph);
  39367. +#ifdef NETDEV_23 /* 2.4 kernels */
  39368. + {
  39369. + int err;
  39370. +
  39371. + err = NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, ixs->skb, NULL, ixs->route->u.dst.dev,
  39372. + ipsec_tunnel_xmit2);
  39373. + if(err != NET_XMIT_SUCCESS && err != NET_XMIT_CN) {
  39374. + if(net_ratelimit())
  39375. + printk(KERN_ERR
  39376. + "klips_error:ipsec_xmit_send: "
  39377. + "ip_send() failed, err=%d\n",
  39378. + -err);
  39379. + ixs->stats->tx_errors++;
  39380. + ixs->stats->tx_aborted_errors++;
  39381. + ixs->skb = NULL;
  39382. + return IPSEC_XMIT_IPSENDFAILURE;
  39383. + }
  39384. + }
  39385. +#else /* NETDEV_23 */ /* 2.2 kernels */
  39386. + ip_send(ixs->skb);
  39387. +#endif /* NETDEV_23 */
  39388. +#else /* NET_21 */ /* 2.0 kernels */
  39389. + ixs->skb->arp = 1;
  39390. + /* ISDN/ASYNC PPP from Matjaz Godec. */
  39391. + /* skb->protocol = htons(ETH_P_IP); */
  39392. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39393. + "klips_debug:ipsec_xmit_send: "
  39394. + "...done, calling dev_queue_xmit() or ip_fragment().\n");
  39395. + IP_SEND(ixs->skb, ixs->physdev);
  39396. +#endif /* NET_21 */
  39397. + ixs->stats->tx_packets++;
  39398. +
  39399. + ixs->skb = NULL;
  39400. +
  39401. + return IPSEC_XMIT_OK;
  39402. +}
  39403. +
  39404. +void
  39405. +ipsec_tunnel_cleanup(struct ipsec_xmit_state*ixs)
  39406. +{
  39407. +#if defined(HAS_NETIF_QUEUE) || defined (HAVE_NETIF_QUEUE)
  39408. + netif_wake_queue(ixs->dev);
  39409. +#else /* defined(HAS_NETIF_QUEUE) || defined (HAVE_NETIF_QUEUE) */
  39410. + ixs->dev->tbusy = 0;
  39411. +#endif /* defined(HAS_NETIF_QUEUE) || defined (HAVE_NETIF_QUEUE) */
  39412. + if(ixs->saved_header) {
  39413. + kfree(ixs->saved_header);
  39414. + }
  39415. + if(ixs->skb) {
  39416. + dev_kfree_skb(ixs->skb, FREE_WRITE);
  39417. + }
  39418. + if(ixs->oskb) {
  39419. + dev_kfree_skb(ixs->oskb, FREE_WRITE);
  39420. + }
  39421. + if (ixs->ips.ips_ident_s.data) {
  39422. + kfree(ixs->ips.ips_ident_s.data);
  39423. + }
  39424. + if (ixs->ips.ips_ident_d.data) {
  39425. + kfree(ixs->ips.ips_ident_d.data);
  39426. + }
  39427. +}
  39428. +
  39429. +/*
  39430. + * This function assumes it is being called from dev_queue_xmit()
  39431. + * and that skb is filled properly by that function.
  39432. + */
  39433. +int
  39434. +ipsec_tunnel_start_xmit(struct sk_buff *skb, struct net_device *dev)
  39435. +{
  39436. + struct ipsec_xmit_state ixs_mem;
  39437. + struct ipsec_xmit_state *ixs = &ixs_mem;
  39438. + enum ipsec_xmit_value stat;
  39439. +
  39440. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  39441. + ixs->natt_type = 0, ixs->natt_head = 0;
  39442. + ixs->natt_sport = 0, ixs->natt_dport = 0;
  39443. +#endif
  39444. +
  39445. + memset((caddr_t)ixs, 0, sizeof(*ixs));
  39446. + ixs->oskb = NULL;
  39447. + ixs->saved_header = NULL; /* saved copy of the hard header */
  39448. + ixs->route = NULL;
  39449. + memset((caddr_t)&(ixs->ips), 0, sizeof(ixs->ips));
  39450. + ixs->dev = dev;
  39451. + ixs->skb = skb;
  39452. +
  39453. + stat = ipsec_xmit_sanity_check_dev(ixs);
  39454. + if(stat != IPSEC_XMIT_OK) {
  39455. + goto cleanup;
  39456. + }
  39457. +
  39458. + stat = ipsec_xmit_sanity_check_skb(ixs);
  39459. + if(stat != IPSEC_XMIT_OK) {
  39460. + goto cleanup;
  39461. + }
  39462. +
  39463. + stat = ipsec_tunnel_strip_hard_header(ixs);
  39464. + if(stat != IPSEC_XMIT_OK) {
  39465. + goto cleanup;
  39466. + }
  39467. +
  39468. + stat = ipsec_tunnel_SAlookup(ixs);
  39469. + if(stat != IPSEC_XMIT_OK) {
  39470. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39471. + "klips_debug:ipsec_tunnel_start_xmit: SAlookup failed: %d\n",
  39472. + stat);
  39473. + goto cleanup;
  39474. + }
  39475. +
  39476. + ixs->innersrc = ixs->iph->saddr;
  39477. + /* start encapsulation loop here XXX */
  39478. + do {
  39479. + stat = ipsec_xmit_encap_bundle(ixs);
  39480. + if(stat != IPSEC_XMIT_OK) {
  39481. + if(stat == IPSEC_XMIT_PASS) {
  39482. + goto bypass;
  39483. + }
  39484. +
  39485. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  39486. + "klips_debug:ipsec_tunnel_start_xmit: encap_bundle failed: %d\n",
  39487. + stat);
  39488. + goto cleanup;
  39489. + }
  39490. +
  39491. + ixs->matcher.sen_ip_src.s_addr = ixs->iph->saddr;
  39492. + ixs->matcher.sen_ip_dst.s_addr = ixs->iph->daddr;
  39493. + ixs->matcher.sen_proto = ixs->iph->protocol;
  39494. + ipsec_extract_ports(ixs->iph, &ixs->matcher);
  39495. +
  39496. + spin_lock(&eroute_lock);
  39497. + ixs->eroute = ipsec_findroute(&ixs->matcher);
  39498. + if(ixs->eroute) {
  39499. + ixs->outgoing_said = ixs->eroute->er_said;
  39500. + ixs->eroute_pid = ixs->eroute->er_pid;
  39501. + ixs->eroute->er_count++;
  39502. + ixs->eroute->er_lasttime = jiffies/HZ;
  39503. + }
  39504. + spin_unlock(&eroute_lock);
  39505. +
  39506. + KLIPS_PRINT((debug_tunnel & DB_TN_XMIT) &&
  39507. + /* ((ixs->orgdst != ixs->newdst) || (ixs->orgsrc != ixs->newsrc)) */
  39508. + (ixs->orgedst != ixs->outgoing_said.dst.u.v4.sin_addr.s_addr) &&
  39509. + ixs->outgoing_said.dst.u.v4.sin_addr.s_addr &&
  39510. + ixs->eroute,
  39511. + "klips_debug:ipsec_tunnel_start_xmit: "
  39512. + "We are recursing here.\n");
  39513. +
  39514. + } while(/*((ixs->orgdst != ixs->newdst) || (ixs->orgsrc != ixs->newsrc))*/
  39515. + (ixs->orgedst != ixs->outgoing_said.dst.u.v4.sin_addr.s_addr) &&
  39516. + ixs->outgoing_said.dst.u.v4.sin_addr.s_addr &&
  39517. + ixs->eroute);
  39518. +
  39519. + stat = ipsec_tunnel_restore_hard_header(ixs);
  39520. + if(stat != IPSEC_XMIT_OK) {
  39521. + goto cleanup;
  39522. + }
  39523. +
  39524. + bypass:
  39525. + stat = ipsec_tunnel_send(ixs);
  39526. +
  39527. + cleanup:
  39528. + ipsec_tunnel_cleanup(ixs);
  39529. +
  39530. + return 0;
  39531. +}
  39532. +
  39533. +DEBUG_NO_STATIC struct net_device_stats *
  39534. +ipsec_tunnel_get_stats(struct net_device *dev)
  39535. +{
  39536. + return &(((struct ipsecpriv *)(dev->priv))->mystats);
  39537. +}
  39538. +
  39539. +/*
  39540. + * Revectored calls.
  39541. + * For each of these calls, a field exists in our private structure.
  39542. + */
  39543. +
  39544. +DEBUG_NO_STATIC int
  39545. +ipsec_tunnel_hard_header(struct sk_buff *skb, struct net_device *dev,
  39546. + unsigned short type, void *daddr, void *saddr, unsigned len)
  39547. +{
  39548. + struct ipsecpriv *prv = dev->priv;
  39549. + struct net_device *tmp;
  39550. + int ret;
  39551. + struct net_device_stats *stats; /* This device's statistics */
  39552. +
  39553. + if(skb == NULL) {
  39554. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39555. + "klips_debug:ipsec_tunnel_hard_header: "
  39556. + "no skb...\n");
  39557. + return -ENODATA;
  39558. + }
  39559. +
  39560. + if(dev == NULL) {
  39561. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39562. + "klips_debug:ipsec_tunnel_hard_header: "
  39563. + "no device...\n");
  39564. + return -ENODEV;
  39565. + }
  39566. +
  39567. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39568. + "klips_debug:ipsec_tunnel_hard_header: "
  39569. + "skb->dev=%s dev=%s.\n",
  39570. + skb->dev ? skb->dev->name : "NULL",
  39571. + dev->name);
  39572. +
  39573. + if(prv == NULL) {
  39574. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39575. + "klips_debug:ipsec_tunnel_hard_header: "
  39576. + "no private space associated with dev=%s\n",
  39577. + dev->name ? dev->name : "NULL");
  39578. + return -ENODEV;
  39579. + }
  39580. +
  39581. + stats = (struct net_device_stats *) &(prv->mystats);
  39582. +
  39583. + if(prv->dev == NULL) {
  39584. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39585. + "klips_debug:ipsec_tunnel_hard_header: "
  39586. + "no physical device associated with dev=%s\n",
  39587. + dev->name ? dev->name : "NULL");
  39588. + stats->tx_dropped++;
  39589. + return -ENODEV;
  39590. + }
  39591. +
  39592. + /* check if we have to send a IPv6 packet. It might be a Router
  39593. + Solicitation, where the building of the packet happens in
  39594. + reverse order:
  39595. + 1. ll hdr,
  39596. + 2. IPv6 hdr,
  39597. + 3. ICMPv6 hdr
  39598. + -> skb->nh.raw is still uninitialized when this function is
  39599. + called!! If this is no IPv6 packet, we can print debugging
  39600. + messages, otherwise we skip all debugging messages and just
  39601. + build the ll header */
  39602. + if(type != ETH_P_IPV6) {
  39603. + /* execute this only, if we don't have to build the
  39604. + header for a IPv6 packet */
  39605. + if(!prv->hard_header) {
  39606. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39607. + "klips_debug:ipsec_tunnel_hard_header: "
  39608. + "physical device has been detached, packet dropped 0p%p->0p%p len=%d type=%d dev=%s->NULL ",
  39609. + saddr,
  39610. + daddr,
  39611. + len,
  39612. + type,
  39613. + dev->name);
  39614. +#ifdef NET_21
  39615. + KLIPS_PRINTMORE(debug_tunnel & DB_TN_REVEC,
  39616. + "ip=%08x->%08x\n",
  39617. + (__u32)ntohl(skb->nh.iph->saddr),
  39618. + (__u32)ntohl(skb->nh.iph->daddr) );
  39619. +#else /* NET_21 */
  39620. + KLIPS_PRINTMORE(debug_tunnel & DB_TN_REVEC,
  39621. + "ip=%08x->%08x\n",
  39622. + (__u32)ntohl(skb->ip_hdr->saddr),
  39623. + (__u32)ntohl(skb->ip_hdr->daddr) );
  39624. +#endif /* NET_21 */
  39625. + stats->tx_dropped++;
  39626. + return -ENODEV;
  39627. + }
  39628. +
  39629. +#define da ((struct net_device *)(prv->dev))->dev_addr
  39630. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39631. + "klips_debug:ipsec_tunnel_hard_header: "
  39632. + "Revectored 0p%p->0p%p len=%d type=%d dev=%s->%s dev_addr=%02x:%02x:%02x:%02x:%02x:%02x ",
  39633. + saddr,
  39634. + daddr,
  39635. + len,
  39636. + type,
  39637. + dev->name,
  39638. + prv->dev->name,
  39639. + da[0], da[1], da[2], da[3], da[4], da[5]);
  39640. +#ifdef NET_21
  39641. + KLIPS_PRINTMORE(debug_tunnel & DB_TN_REVEC,
  39642. + "ip=%08x->%08x\n",
  39643. + (__u32)ntohl(skb->nh.iph->saddr),
  39644. + (__u32)ntohl(skb->nh.iph->daddr) );
  39645. +#else /* NET_21 */
  39646. + KLIPS_PRINTMORE(debug_tunnel & DB_TN_REVEC,
  39647. + "ip=%08x->%08x\n",
  39648. + (__u32)ntohl(skb->ip_hdr->saddr),
  39649. + (__u32)ntohl(skb->ip_hdr->daddr) );
  39650. +#endif /* NET_21 */
  39651. + } else {
  39652. + KLIPS_PRINT(debug_tunnel,
  39653. + "klips_debug:ipsec_tunnel_hard_header: "
  39654. + "is IPv6 packet, skip debugging messages, only revector and build linklocal header.\n");
  39655. + }
  39656. + tmp = skb->dev;
  39657. + skb->dev = prv->dev;
  39658. + ret = prv->hard_header(skb, prv->dev, type, (void *)daddr, (void *)saddr, len);
  39659. + skb->dev = tmp;
  39660. + return ret;
  39661. +}
  39662. +
  39663. +DEBUG_NO_STATIC int
  39664. +#ifdef NET_21
  39665. +ipsec_tunnel_rebuild_header(struct sk_buff *skb)
  39666. +#else /* NET_21 */
  39667. +ipsec_tunnel_rebuild_header(void *buff, struct net_device *dev,
  39668. + unsigned long raddr, struct sk_buff *skb)
  39669. +#endif /* NET_21 */
  39670. +{
  39671. + struct ipsecpriv *prv = skb->dev->priv;
  39672. + struct net_device *tmp;
  39673. + int ret;
  39674. + struct net_device_stats *stats; /* This device's statistics */
  39675. +
  39676. + if(skb->dev == NULL) {
  39677. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39678. + "klips_debug:ipsec_tunnel_rebuild_header: "
  39679. + "no device...");
  39680. + return -ENODEV;
  39681. + }
  39682. +
  39683. + if(prv == NULL) {
  39684. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39685. + "klips_debug:ipsec_tunnel_rebuild_header: "
  39686. + "no private space associated with dev=%s",
  39687. + skb->dev->name ? skb->dev->name : "NULL");
  39688. + return -ENODEV;
  39689. + }
  39690. +
  39691. + stats = (struct net_device_stats *) &(prv->mystats);
  39692. +
  39693. + if(prv->dev == NULL) {
  39694. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39695. + "klips_debug:ipsec_tunnel_rebuild_header: "
  39696. + "no physical device associated with dev=%s",
  39697. + skb->dev->name ? skb->dev->name : "NULL");
  39698. + stats->tx_dropped++;
  39699. + return -ENODEV;
  39700. + }
  39701. +
  39702. + if(!prv->rebuild_header) {
  39703. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39704. + "klips_debug:ipsec_tunnel_rebuild_header: "
  39705. + "physical device has been detached, packet dropped skb->dev=%s->NULL ",
  39706. + skb->dev->name);
  39707. +#ifdef NET_21
  39708. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39709. + "ip=%08x->%08x\n",
  39710. + (__u32)ntohl(skb->nh.iph->saddr),
  39711. + (__u32)ntohl(skb->nh.iph->daddr) );
  39712. +#else /* NET_21 */
  39713. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39714. + "ip=%08x->%08x\n",
  39715. + (__u32)ntohl(skb->ip_hdr->saddr),
  39716. + (__u32)ntohl(skb->ip_hdr->daddr) );
  39717. +#endif /* NET_21 */
  39718. + stats->tx_dropped++;
  39719. + return -ENODEV;
  39720. + }
  39721. +
  39722. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39723. + "klips_debug:ipsec_tunnel: "
  39724. + "Revectored rebuild_header dev=%s->%s ",
  39725. + skb->dev->name, prv->dev->name);
  39726. +#ifdef NET_21
  39727. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39728. + "ip=%08x->%08x\n",
  39729. + (__u32)ntohl(skb->nh.iph->saddr),
  39730. + (__u32)ntohl(skb->nh.iph->daddr) );
  39731. +#else /* NET_21 */
  39732. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39733. + "ip=%08x->%08x\n",
  39734. + (__u32)ntohl(skb->ip_hdr->saddr),
  39735. + (__u32)ntohl(skb->ip_hdr->daddr) );
  39736. +#endif /* NET_21 */
  39737. + tmp = skb->dev;
  39738. + skb->dev = prv->dev;
  39739. +
  39740. +#ifdef NET_21
  39741. + ret = prv->rebuild_header(skb);
  39742. +#else /* NET_21 */
  39743. + ret = prv->rebuild_header(buff, prv->dev, raddr, skb);
  39744. +#endif /* NET_21 */
  39745. + skb->dev = tmp;
  39746. + return ret;
  39747. +}
  39748. +
  39749. +DEBUG_NO_STATIC int
  39750. +ipsec_tunnel_set_mac_address(struct net_device *dev, void *addr)
  39751. +{
  39752. + struct ipsecpriv *prv = dev->priv;
  39753. +
  39754. + struct net_device_stats *stats; /* This device's statistics */
  39755. +
  39756. + if(dev == NULL) {
  39757. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39758. + "klips_debug:ipsec_tunnel_set_mac_address: "
  39759. + "no device...");
  39760. + return -ENODEV;
  39761. + }
  39762. +
  39763. + if(prv == NULL) {
  39764. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39765. + "klips_debug:ipsec_tunnel_set_mac_address: "
  39766. + "no private space associated with dev=%s",
  39767. + dev->name ? dev->name : "NULL");
  39768. + return -ENODEV;
  39769. + }
  39770. +
  39771. + stats = (struct net_device_stats *) &(prv->mystats);
  39772. +
  39773. + if(prv->dev == NULL) {
  39774. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39775. + "klips_debug:ipsec_tunnel_set_mac_address: "
  39776. + "no physical device associated with dev=%s",
  39777. + dev->name ? dev->name : "NULL");
  39778. + stats->tx_dropped++;
  39779. + return -ENODEV;
  39780. + }
  39781. +
  39782. + if(!prv->set_mac_address) {
  39783. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39784. + "klips_debug:ipsec_tunnel_set_mac_address: "
  39785. + "physical device has been detached, cannot set - skb->dev=%s->NULL\n",
  39786. + dev->name);
  39787. + return -ENODEV;
  39788. + }
  39789. +
  39790. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39791. + "klips_debug:ipsec_tunnel_set_mac_address: "
  39792. + "Revectored dev=%s->%s addr=0p%p\n",
  39793. + dev->name, prv->dev->name, addr);
  39794. + return prv->set_mac_address(prv->dev, addr);
  39795. +
  39796. +}
  39797. +
  39798. +#ifndef NET_21
  39799. +DEBUG_NO_STATIC void
  39800. +ipsec_tunnel_cache_bind(struct hh_cache **hhp, struct net_device *dev,
  39801. + unsigned short htype, __u32 daddr)
  39802. +{
  39803. + struct ipsecpriv *prv = dev->priv;
  39804. +
  39805. + struct net_device_stats *stats; /* This device's statistics */
  39806. +
  39807. + if(dev == NULL) {
  39808. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39809. + "klips_debug:ipsec_tunnel_cache_bind: "
  39810. + "no device...");
  39811. + return;
  39812. + }
  39813. +
  39814. + if(prv == NULL) {
  39815. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39816. + "klips_debug:ipsec_tunnel_cache_bind: "
  39817. + "no private space associated with dev=%s",
  39818. + dev->name ? dev->name : "NULL");
  39819. + return;
  39820. + }
  39821. +
  39822. + stats = (struct net_device_stats *) &(prv->mystats);
  39823. +
  39824. + if(prv->dev == NULL) {
  39825. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39826. + "klips_debug:ipsec_tunnel_cache_bind: "
  39827. + "no physical device associated with dev=%s",
  39828. + dev->name ? dev->name : "NULL");
  39829. + stats->tx_dropped++;
  39830. + return;
  39831. + }
  39832. +
  39833. + if(!prv->header_cache_bind) {
  39834. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39835. + "klips_debug:ipsec_tunnel_cache_bind: "
  39836. + "physical device has been detached, cannot set - skb->dev=%s->NULL\n",
  39837. + dev->name);
  39838. + stats->tx_dropped++;
  39839. + return;
  39840. + }
  39841. +
  39842. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39843. + "klips_debug:ipsec_tunnel_cache_bind: "
  39844. + "Revectored \n");
  39845. + prv->header_cache_bind(hhp, prv->dev, htype, daddr);
  39846. + return;
  39847. +}
  39848. +#endif /* !NET_21 */
  39849. +
  39850. +
  39851. +DEBUG_NO_STATIC void
  39852. +ipsec_tunnel_cache_update(struct hh_cache *hh, struct net_device *dev, unsigned char * haddr)
  39853. +{
  39854. + struct ipsecpriv *prv = dev->priv;
  39855. +
  39856. + struct net_device_stats *stats; /* This device's statistics */
  39857. +
  39858. + if(dev == NULL) {
  39859. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39860. + "klips_debug:ipsec_tunnel_cache_update: "
  39861. + "no device...");
  39862. + return;
  39863. + }
  39864. +
  39865. + if(prv == NULL) {
  39866. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39867. + "klips_debug:ipsec_tunnel_cache_update: "
  39868. + "no private space associated with dev=%s",
  39869. + dev->name ? dev->name : "NULL");
  39870. + return;
  39871. + }
  39872. +
  39873. + stats = (struct net_device_stats *) &(prv->mystats);
  39874. +
  39875. + if(prv->dev == NULL) {
  39876. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39877. + "klips_debug:ipsec_tunnel_cache_update: "
  39878. + "no physical device associated with dev=%s",
  39879. + dev->name ? dev->name : "NULL");
  39880. + stats->tx_dropped++;
  39881. + return;
  39882. + }
  39883. +
  39884. + if(!prv->header_cache_update) {
  39885. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39886. + "klips_debug:ipsec_tunnel_cache_update: "
  39887. + "physical device has been detached, cannot set - skb->dev=%s->NULL\n",
  39888. + dev->name);
  39889. + return;
  39890. + }
  39891. +
  39892. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39893. + "klips_debug:ipsec_tunnel: "
  39894. + "Revectored cache_update\n");
  39895. + prv->header_cache_update(hh, prv->dev, haddr);
  39896. + return;
  39897. +}
  39898. +
  39899. +#ifdef NET_21
  39900. +DEBUG_NO_STATIC int
  39901. +ipsec_tunnel_neigh_setup(struct neighbour *n)
  39902. +{
  39903. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39904. + "klips_debug:ipsec_tunnel_neigh_setup:\n");
  39905. +
  39906. + if (n->nud_state == NUD_NONE) {
  39907. + n->ops = &arp_broken_ops;
  39908. + n->output = n->ops->output;
  39909. + }
  39910. + return 0;
  39911. +}
  39912. +
  39913. +DEBUG_NO_STATIC int
  39914. +ipsec_tunnel_neigh_setup_dev(struct net_device *dev, struct neigh_parms *p)
  39915. +{
  39916. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39917. + "klips_debug:ipsec_tunnel_neigh_setup_dev: "
  39918. + "setting up %s\n",
  39919. + dev ? dev->name : "NULL");
  39920. +
  39921. + if (p->tbl->family == AF_INET) {
  39922. + p->neigh_setup = ipsec_tunnel_neigh_setup;
  39923. + p->ucast_probes = 0;
  39924. + p->mcast_probes = 0;
  39925. + }
  39926. + return 0;
  39927. +}
  39928. +#endif /* NET_21 */
  39929. +
  39930. +/*
  39931. + * We call the attach routine to attach another device.
  39932. + */
  39933. +
  39934. +DEBUG_NO_STATIC int
  39935. +ipsec_tunnel_attach(struct net_device *dev, struct net_device *physdev)
  39936. +{
  39937. + int i;
  39938. + struct ipsecpriv *prv = dev->priv;
  39939. +
  39940. + if(dev == NULL) {
  39941. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39942. + "klips_debug:ipsec_tunnel_attach: "
  39943. + "no device...");
  39944. + return -ENODEV;
  39945. + }
  39946. +
  39947. + if(prv == NULL) {
  39948. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  39949. + "klips_debug:ipsec_tunnel_attach: "
  39950. + "no private space associated with dev=%s",
  39951. + dev->name ? dev->name : "NULL");
  39952. + return -ENODATA;
  39953. + }
  39954. +
  39955. + prv->dev = physdev;
  39956. + prv->hard_start_xmit = physdev->hard_start_xmit;
  39957. + prv->get_stats = physdev->get_stats;
  39958. +
  39959. + if (physdev->hard_header) {
  39960. + prv->hard_header = physdev->hard_header;
  39961. + dev->hard_header = ipsec_tunnel_hard_header;
  39962. + } else
  39963. + dev->hard_header = NULL;
  39964. +
  39965. + if (physdev->rebuild_header) {
  39966. + prv->rebuild_header = physdev->rebuild_header;
  39967. + dev->rebuild_header = ipsec_tunnel_rebuild_header;
  39968. + } else
  39969. + dev->rebuild_header = NULL;
  39970. +
  39971. + if (physdev->set_mac_address) {
  39972. + prv->set_mac_address = physdev->set_mac_address;
  39973. + dev->set_mac_address = ipsec_tunnel_set_mac_address;
  39974. + } else
  39975. + dev->set_mac_address = NULL;
  39976. +
  39977. +#ifndef NET_21
  39978. + if (physdev->header_cache_bind) {
  39979. + prv->header_cache_bind = physdev->header_cache_bind;
  39980. + dev->header_cache_bind = ipsec_tunnel_cache_bind;
  39981. + } else
  39982. + dev->header_cache_bind = NULL;
  39983. +#endif /* !NET_21 */
  39984. +
  39985. + if (physdev->header_cache_update) {
  39986. + prv->header_cache_update = physdev->header_cache_update;
  39987. + dev->header_cache_update = ipsec_tunnel_cache_update;
  39988. + } else
  39989. + dev->header_cache_update = NULL;
  39990. +
  39991. + dev->hard_header_len = physdev->hard_header_len;
  39992. +
  39993. +#ifdef NET_21
  39994. +/* prv->neigh_setup = physdev->neigh_setup; */
  39995. + dev->neigh_setup = ipsec_tunnel_neigh_setup_dev;
  39996. +#endif /* NET_21 */
  39997. + dev->mtu = 16260; /* 0xfff0; */ /* dev->mtu; */
  39998. + prv->mtu = physdev->mtu;
  39999. +
  40000. +#ifdef PHYSDEV_TYPE
  40001. + dev->type = physdev->type; /* ARPHRD_TUNNEL; */
  40002. +#endif /* PHYSDEV_TYPE */
  40003. +
  40004. + dev->addr_len = physdev->addr_len;
  40005. + for (i=0; i<dev->addr_len; i++) {
  40006. + dev->dev_addr[i] = physdev->dev_addr[i];
  40007. + }
  40008. +#ifdef CONFIG_KLIPS_DEBUG
  40009. + if(debug_tunnel & DB_TN_INIT) {
  40010. + printk(KERN_INFO "klips_debug:ipsec_tunnel_attach: "
  40011. + "physical device %s being attached has HW address: %2x",
  40012. + physdev->name, physdev->dev_addr[0]);
  40013. + for (i=1; i < physdev->addr_len; i++) {
  40014. + printk(":%02x", physdev->dev_addr[i]);
  40015. + }
  40016. + printk("\n");
  40017. + }
  40018. +#endif /* CONFIG_KLIPS_DEBUG */
  40019. +
  40020. + return 0;
  40021. +}
  40022. +
  40023. +/*
  40024. + * We call the detach routine to detach the ipsec tunnel from another device.
  40025. + */
  40026. +
  40027. +DEBUG_NO_STATIC int
  40028. +ipsec_tunnel_detach(struct net_device *dev)
  40029. +{
  40030. + int i;
  40031. + struct ipsecpriv *prv = dev->priv;
  40032. +
  40033. + if(dev == NULL) {
  40034. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  40035. + "klips_debug:ipsec_tunnel_detach: "
  40036. + "no device...");
  40037. + return -ENODEV;
  40038. + }
  40039. +
  40040. + if(prv == NULL) {
  40041. + KLIPS_PRINT(debug_tunnel & DB_TN_REVEC,
  40042. + "klips_debug:ipsec_tunnel_detach: "
  40043. + "no private space associated with dev=%s",
  40044. + dev->name ? dev->name : "NULL");
  40045. + return -ENODATA;
  40046. + }
  40047. +
  40048. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40049. + "klips_debug:ipsec_tunnel_detach: "
  40050. + "physical device %s being detached from virtual device %s\n",
  40051. + prv->dev ? prv->dev->name : "NULL",
  40052. + dev->name);
  40053. +
  40054. + ipsec_dev_put(prv->dev);
  40055. + prv->dev = NULL;
  40056. + prv->hard_start_xmit = NULL;
  40057. + prv->get_stats = NULL;
  40058. +
  40059. + prv->hard_header = NULL;
  40060. +#ifdef DETACH_AND_DOWN
  40061. + dev->hard_header = NULL;
  40062. +#endif /* DETACH_AND_DOWN */
  40063. +
  40064. + prv->rebuild_header = NULL;
  40065. +#ifdef DETACH_AND_DOWN
  40066. + dev->rebuild_header = NULL;
  40067. +#endif /* DETACH_AND_DOWN */
  40068. +
  40069. + prv->set_mac_address = NULL;
  40070. +#ifdef DETACH_AND_DOWN
  40071. + dev->set_mac_address = NULL;
  40072. +#endif /* DETACH_AND_DOWN */
  40073. +
  40074. +#ifndef NET_21
  40075. + prv->header_cache_bind = NULL;
  40076. +#ifdef DETACH_AND_DOWN
  40077. + dev->header_cache_bind = NULL;
  40078. +#endif /* DETACH_AND_DOWN */
  40079. +#endif /* !NET_21 */
  40080. +
  40081. + prv->header_cache_update = NULL;
  40082. +#ifdef DETACH_AND_DOWN
  40083. + dev->header_cache_update = NULL;
  40084. +#endif /* DETACH_AND_DOWN */
  40085. +
  40086. +#ifdef NET_21
  40087. +/* prv->neigh_setup = NULL; */
  40088. +#ifdef DETACH_AND_DOWN
  40089. + dev->neigh_setup = NULL;
  40090. +#endif /* DETACH_AND_DOWN */
  40091. +#endif /* NET_21 */
  40092. + dev->hard_header_len = 0;
  40093. +#ifdef DETACH_AND_DOWN
  40094. + dev->mtu = 0;
  40095. +#endif /* DETACH_AND_DOWN */
  40096. + prv->mtu = 0;
  40097. + for (i=0; i<MAX_ADDR_LEN; i++) {
  40098. + dev->dev_addr[i] = 0;
  40099. + }
  40100. + dev->addr_len = 0;
  40101. +#ifdef PHYSDEV_TYPE
  40102. + dev->type = ARPHRD_VOID; /* ARPHRD_TUNNEL; */
  40103. +#endif /* PHYSDEV_TYPE */
  40104. +
  40105. + return 0;
  40106. +}
  40107. +
  40108. +/*
  40109. + * We call the clear routine to detach all ipsec tunnels from other devices.
  40110. + */
  40111. +DEBUG_NO_STATIC int
  40112. +ipsec_tunnel_clear(void)
  40113. +{
  40114. + int i;
  40115. + struct net_device *ipsecdev = NULL, *prvdev;
  40116. + struct ipsecpriv *prv;
  40117. + int ret;
  40118. +
  40119. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40120. + "klips_debug:ipsec_tunnel_clear: .\n");
  40121. +
  40122. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  40123. + ipsecdev = ipsecdevices[i];
  40124. + if(ipsecdev != NULL) {
  40125. + if((prv = (struct ipsecpriv *)(ipsecdev->priv))) {
  40126. + prvdev = (struct net_device *)(prv->dev);
  40127. + if(prvdev) {
  40128. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40129. + "klips_debug:ipsec_tunnel_clear: "
  40130. + "physical device for device %s is %s\n",
  40131. + ipsecdev->name, prvdev->name);
  40132. + if((ret = ipsec_tunnel_detach(ipsecdev))) {
  40133. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40134. + "klips_debug:ipsec_tunnel_clear: "
  40135. + "error %d detatching device %s from device %s.\n",
  40136. + ret, ipsecdev->name, prvdev->name);
  40137. + return ret;
  40138. + }
  40139. + }
  40140. + }
  40141. + }
  40142. + }
  40143. + return 0;
  40144. +}
  40145. +
  40146. +DEBUG_NO_STATIC int
  40147. +ipsec_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  40148. +{
  40149. + struct ipsectunnelconf *cf = (struct ipsectunnelconf *)&ifr->ifr_data;
  40150. + struct ipsecpriv *prv = dev->priv;
  40151. + struct net_device *them; /* physical device */
  40152. +#ifdef CONFIG_IP_ALIAS
  40153. + char *colon;
  40154. + char realphysname[IFNAMSIZ];
  40155. +#endif /* CONFIG_IP_ALIAS */
  40156. +
  40157. + if(dev == NULL) {
  40158. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40159. + "klips_debug:ipsec_tunnel_ioctl: "
  40160. + "device not supplied.\n");
  40161. + return -ENODEV;
  40162. + }
  40163. +
  40164. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40165. + "klips_debug:ipsec_tunnel_ioctl: "
  40166. + "tncfg service call #%d for dev=%s\n",
  40167. + cmd,
  40168. + dev->name ? dev->name : "NULL");
  40169. + switch (cmd) {
  40170. + /* attach a virtual ipsec? device to a physical device */
  40171. + case IPSEC_SET_DEV:
  40172. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40173. + "klips_debug:ipsec_tunnel_ioctl: "
  40174. + "calling ipsec_tunnel_attatch...\n");
  40175. +#ifdef CONFIG_IP_ALIAS
  40176. + /* If this is an IP alias interface, get its real physical name */
  40177. + strncpy(realphysname, cf->cf_name, IFNAMSIZ);
  40178. + realphysname[IFNAMSIZ-1] = 0;
  40179. + colon = strchr(realphysname, ':');
  40180. + if (colon) *colon = 0;
  40181. + them = ipsec_dev_get(realphysname);
  40182. +#else /* CONFIG_IP_ALIAS */
  40183. + them = ipsec_dev_get(cf->cf_name);
  40184. +#endif /* CONFIG_IP_ALIAS */
  40185. +
  40186. + if (them == NULL) {
  40187. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40188. + "klips_debug:ipsec_tunnel_ioctl: "
  40189. + "physical device %s requested is null\n",
  40190. + cf->cf_name);
  40191. + return -ENXIO;
  40192. + }
  40193. +
  40194. +#if 0
  40195. + if (them->flags & IFF_UP) {
  40196. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40197. + "klips_debug:ipsec_tunnel_ioctl: "
  40198. + "physical device %s requested is not up.\n",
  40199. + cf->cf_name);
  40200. + ipsec_dev_put(them);
  40201. + return -ENXIO;
  40202. + }
  40203. +#endif
  40204. +
  40205. + if (prv && prv->dev) {
  40206. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40207. + "klips_debug:ipsec_tunnel_ioctl: "
  40208. + "virtual device is already connected to %s.\n",
  40209. + prv->dev->name ? prv->dev->name : "NULL");
  40210. + ipsec_dev_put(them);
  40211. + return -EBUSY;
  40212. + }
  40213. + return ipsec_tunnel_attach(dev, them);
  40214. +
  40215. + case IPSEC_DEL_DEV:
  40216. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40217. + "klips_debug:ipsec_tunnel_ioctl: "
  40218. + "calling ipsec_tunnel_detatch.\n");
  40219. + if (! prv->dev) {
  40220. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40221. + "klips_debug:ipsec_tunnel_ioctl: "
  40222. + "physical device not connected.\n");
  40223. + return -ENODEV;
  40224. + }
  40225. + return ipsec_tunnel_detach(dev);
  40226. +
  40227. + case IPSEC_CLR_DEV:
  40228. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40229. + "klips_debug:ipsec_tunnel_ioctl: "
  40230. + "calling ipsec_tunnel_clear.\n");
  40231. + return ipsec_tunnel_clear();
  40232. +
  40233. + default:
  40234. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40235. + "klips_debug:ipsec_tunnel_ioctl: "
  40236. + "unknown command %d.\n",
  40237. + cmd);
  40238. + return -EOPNOTSUPP;
  40239. + }
  40240. +}
  40241. +
  40242. +struct net_device *ipsec_get_device(int inst)
  40243. +{
  40244. + struct net_device *ipsec_dev;
  40245. +
  40246. + ipsec_dev = NULL;
  40247. +
  40248. + if(inst < IPSEC_NUM_IF) {
  40249. + ipsec_dev = ipsecdevices[inst];
  40250. + }
  40251. +
  40252. + return ipsec_dev;
  40253. +}
  40254. +
  40255. +int
  40256. +ipsec_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
  40257. +{
  40258. + struct net_device *dev = ptr;
  40259. + struct net_device *ipsec_dev;
  40260. + struct ipsecpriv *priv;
  40261. + int i;
  40262. +
  40263. + if (dev == NULL) {
  40264. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40265. + "klips_debug:ipsec_device_event: "
  40266. + "dev=NULL for event type %ld.\n",
  40267. + event);
  40268. + return(NOTIFY_DONE);
  40269. + }
  40270. +
  40271. + /* check for loopback devices */
  40272. + if (dev && (dev->flags & IFF_LOOPBACK)) {
  40273. + return(NOTIFY_DONE);
  40274. + }
  40275. +
  40276. + switch (event) {
  40277. + case NETDEV_DOWN:
  40278. + /* look very carefully at the scope of these compiler
  40279. + directives before changing anything... -- RGB */
  40280. +#ifdef NET_21
  40281. + case NETDEV_UNREGISTER:
  40282. + switch (event) {
  40283. + case NETDEV_DOWN:
  40284. +#endif /* NET_21 */
  40285. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40286. + "klips_debug:ipsec_device_event: "
  40287. + "NETDEV_DOWN dev=%s flags=%x\n",
  40288. + dev->name,
  40289. + dev->flags);
  40290. + if(strncmp(dev->name, "ipsec", strlen("ipsec")) == 0) {
  40291. + printk(KERN_CRIT "IPSEC EVENT: KLIPS device %s shut down.\n",
  40292. + dev->name);
  40293. + }
  40294. +#ifdef NET_21
  40295. + break;
  40296. + case NETDEV_UNREGISTER:
  40297. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40298. + "klips_debug:ipsec_device_event: "
  40299. + "NETDEV_UNREGISTER dev=%s flags=%x\n",
  40300. + dev->name,
  40301. + dev->flags);
  40302. + break;
  40303. + }
  40304. +#endif /* NET_21 */
  40305. +
  40306. + /* find the attached physical device and detach it. */
  40307. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  40308. + ipsec_dev = ipsecdevices[i];
  40309. +
  40310. + if(ipsec_dev) {
  40311. + priv = (struct ipsecpriv *)(ipsec_dev->priv);
  40312. + if(priv) {
  40313. + ;
  40314. + if(((struct net_device *)(priv->dev)) == dev) {
  40315. + /* dev_close(ipsec_dev); */
  40316. + /* return */ ipsec_tunnel_detach(ipsec_dev);
  40317. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40318. + "klips_debug:ipsec_device_event: "
  40319. + "device '%s' has been detached.\n",
  40320. + ipsec_dev->name);
  40321. + break;
  40322. + }
  40323. + } else {
  40324. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40325. + "klips_debug:ipsec_device_event: "
  40326. + "device '%s' has no private data space!\n",
  40327. + ipsec_dev->name);
  40328. + }
  40329. + }
  40330. + }
  40331. + break;
  40332. + case NETDEV_UP:
  40333. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40334. + "klips_debug:ipsec_device_event: "
  40335. + "NETDEV_UP dev=%s\n",
  40336. + dev->name);
  40337. + break;
  40338. +#ifdef NET_21
  40339. + case NETDEV_REBOOT:
  40340. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40341. + "klips_debug:ipsec_device_event: "
  40342. + "NETDEV_REBOOT dev=%s\n",
  40343. + dev->name);
  40344. + break;
  40345. + case NETDEV_CHANGE:
  40346. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40347. + "klips_debug:ipsec_device_event: "
  40348. + "NETDEV_CHANGE dev=%s flags=%x\n",
  40349. + dev->name,
  40350. + dev->flags);
  40351. + break;
  40352. + case NETDEV_REGISTER:
  40353. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40354. + "klips_debug:ipsec_device_event: "
  40355. + "NETDEV_REGISTER dev=%s\n",
  40356. + dev->name);
  40357. + break;
  40358. + case NETDEV_CHANGEMTU:
  40359. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40360. + "klips_debug:ipsec_device_event: "
  40361. + "NETDEV_CHANGEMTU dev=%s to mtu=%d\n",
  40362. + dev->name,
  40363. + dev->mtu);
  40364. + break;
  40365. + case NETDEV_CHANGEADDR:
  40366. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40367. + "klips_debug:ipsec_device_event: "
  40368. + "NETDEV_CHANGEADDR dev=%s\n",
  40369. + dev->name);
  40370. + break;
  40371. + case NETDEV_GOING_DOWN:
  40372. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40373. + "klips_debug:ipsec_device_event: "
  40374. + "NETDEV_GOING_DOWN dev=%s\n",
  40375. + dev->name);
  40376. + break;
  40377. + case NETDEV_CHANGENAME:
  40378. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40379. + "klips_debug:ipsec_device_event: "
  40380. + "NETDEV_CHANGENAME dev=%s\n",
  40381. + dev->name);
  40382. + break;
  40383. +#endif /* NET_21 */
  40384. + default:
  40385. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40386. + "klips_debug:ipsec_device_event: "
  40387. + "event type %ld unrecognised for dev=%s\n",
  40388. + event,
  40389. + dev->name);
  40390. + break;
  40391. + }
  40392. + return NOTIFY_DONE;
  40393. +}
  40394. +
  40395. +/*
  40396. + * Called when an ipsec tunnel device is initialized.
  40397. + * The ipsec tunnel device structure is passed to us.
  40398. + */
  40399. +
  40400. +int
  40401. +ipsec_tunnel_init(struct net_device *dev)
  40402. +{
  40403. + int i;
  40404. +
  40405. + KLIPS_PRINT(debug_tunnel,
  40406. + "klips_debug:ipsec_tunnel_init: "
  40407. + "allocating %lu bytes initialising device: %s\n",
  40408. + (unsigned long) sizeof(struct ipsecpriv),
  40409. + dev->name ? dev->name : "NULL");
  40410. +
  40411. + /* Add our tunnel functions to the device */
  40412. + dev->open = ipsec_tunnel_open;
  40413. + dev->stop = ipsec_tunnel_close;
  40414. + dev->hard_start_xmit = ipsec_tunnel_start_xmit;
  40415. + dev->get_stats = ipsec_tunnel_get_stats;
  40416. +
  40417. + dev->priv = kmalloc(sizeof(struct ipsecpriv), GFP_KERNEL);
  40418. + if (dev->priv == NULL)
  40419. + return -ENOMEM;
  40420. + memset((caddr_t)(dev->priv), 0, sizeof(struct ipsecpriv));
  40421. +
  40422. + for(i = 0; i < sizeof(zeroes); i++) {
  40423. + ((__u8*)(zeroes))[i] = 0;
  40424. + }
  40425. +
  40426. +#ifndef NET_21
  40427. + /* Initialize the tunnel device structure */
  40428. + for (i = 0; i < DEV_NUMBUFFS; i++)
  40429. + skb_queue_head_init(&dev->buffs[i]);
  40430. +#endif /* !NET_21 */
  40431. +
  40432. + dev->set_multicast_list = NULL;
  40433. + dev->do_ioctl = ipsec_tunnel_ioctl;
  40434. + dev->hard_header = NULL;
  40435. + dev->rebuild_header = NULL;
  40436. + dev->set_mac_address = NULL;
  40437. +#ifndef NET_21
  40438. + dev->header_cache_bind = NULL;
  40439. +#endif /* !NET_21 */
  40440. + dev->header_cache_update= NULL;
  40441. +
  40442. +#ifdef NET_21
  40443. +/* prv->neigh_setup = NULL; */
  40444. + dev->neigh_setup = ipsec_tunnel_neigh_setup_dev;
  40445. +#endif /* NET_21 */
  40446. + dev->hard_header_len = 0;
  40447. + dev->mtu = 0;
  40448. + dev->addr_len = 0;
  40449. + dev->type = ARPHRD_VOID; /* ARPHRD_TUNNEL; */ /* ARPHRD_ETHER; */
  40450. + dev->tx_queue_len = 10; /* Small queue */
  40451. + memset((caddr_t)(dev->broadcast),0xFF, ETH_ALEN); /* what if this is not attached to ethernet? */
  40452. +
  40453. + /* New-style flags. */
  40454. + dev->flags = IFF_NOARP /* 0 */ /* Petr Novak */;
  40455. +
  40456. +#if 0
  40457. +#ifdef NET_21
  40458. + dev_init_buffers(dev);
  40459. +#else /* NET_21 */
  40460. + dev->family = AF_INET;
  40461. + dev->pa_addr = 0;
  40462. + dev->pa_brdaddr = 0;
  40463. + dev->pa_mask = 0;
  40464. + dev->pa_alen = 4;
  40465. +#endif /* NET_21 */
  40466. +#endif
  40467. +
  40468. + /* We're done. Have I forgotten anything? */
  40469. + return 0;
  40470. +}
  40471. +
  40472. +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  40473. +/* Module specific interface (but it links with the rest of IPSEC) */
  40474. +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  40475. +
  40476. +int
  40477. +ipsec_tunnel_probe(struct net_device *dev)
  40478. +{
  40479. + ipsec_tunnel_init(dev);
  40480. + return 0;
  40481. +}
  40482. +
  40483. +struct net_device *ipsecdevices[IPSEC_NUM_IF];
  40484. +
  40485. +int
  40486. +ipsec_tunnel_init_devices(void)
  40487. +{
  40488. + int i;
  40489. + char name[IFNAMSIZ];
  40490. + struct net_device *dev_ipsec;
  40491. +
  40492. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40493. + "klips_debug:ipsec_tunnel_init_devices: "
  40494. + "creating and registering IPSEC_NUM_IF=%u devices, allocating %lu per device, IFNAMSIZ=%u.\n",
  40495. + IPSEC_NUM_IF,
  40496. + (unsigned long) (sizeof(struct net_device) + IFNAMSIZ),
  40497. + IFNAMSIZ);
  40498. +
  40499. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  40500. + sprintf(name, IPSEC_DEV_FORMAT, i);
  40501. + dev_ipsec = (struct net_device*)kmalloc(sizeof(struct net_device), GFP_KERNEL);
  40502. + if (dev_ipsec == NULL) {
  40503. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40504. + "klips_debug:ipsec_tunnel_init_devices: "
  40505. + "failed to allocate memory for device %s, quitting device init.\n",
  40506. + name);
  40507. + return -ENOMEM;
  40508. + }
  40509. + memset((caddr_t)dev_ipsec, 0, sizeof(struct net_device));
  40510. +#ifdef NETDEV_23
  40511. + strncpy(dev_ipsec->name, name, sizeof(dev_ipsec->name));
  40512. +#else /* NETDEV_23 */
  40513. + dev_ipsec->name = (char*)kmalloc(IFNAMSIZ, GFP_KERNEL);
  40514. + if (dev_ipsec->name == NULL) {
  40515. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40516. + "klips_debug:ipsec_tunnel_init_devices: "
  40517. + "failed to allocate memory for device %s name, quitting device init.\n",
  40518. + name);
  40519. + return -ENOMEM;
  40520. + }
  40521. + memset((caddr_t)dev_ipsec->name, 0, IFNAMSIZ);
  40522. + strncpy(dev_ipsec->name, name, IFNAMSIZ);
  40523. +#endif /* NETDEV_23 */
  40524. + dev_ipsec->next = NULL;
  40525. + dev_ipsec->init = &ipsec_tunnel_probe;
  40526. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40527. + "klips_debug:ipsec_tunnel_init_devices: "
  40528. + "registering device %s\n",
  40529. + dev_ipsec->name);
  40530. +
  40531. + /* reference and hold the device reference */
  40532. + dev_hold(dev_ipsec);
  40533. + ipsecdevices[i]=dev_ipsec;
  40534. +
  40535. + if (register_netdev(dev_ipsec) != 0) {
  40536. + KLIPS_PRINT(1 || debug_tunnel & DB_TN_INIT,
  40537. + "klips_debug:ipsec_tunnel_init_devices: "
  40538. + "registering device %s failed, quitting device init.\n",
  40539. + dev_ipsec->name);
  40540. + return -EIO;
  40541. + } else {
  40542. + KLIPS_PRINT(debug_tunnel & DB_TN_INIT,
  40543. + "klips_debug:ipsec_tunnel_init_devices: "
  40544. + "registering device %s succeeded, continuing...\n",
  40545. + dev_ipsec->name);
  40546. + }
  40547. + }
  40548. + return 0;
  40549. +}
  40550. +
  40551. +/* void */
  40552. +int
  40553. +ipsec_tunnel_cleanup_devices(void)
  40554. +{
  40555. + int error = 0;
  40556. + int i;
  40557. + struct net_device *dev_ipsec;
  40558. +
  40559. + for(i = 0; i < IPSEC_NUM_IF; i++) {
  40560. + dev_ipsec = ipsecdevices[i];
  40561. + if(dev_ipsec == NULL) {
  40562. + continue;
  40563. + }
  40564. +
  40565. + /* release reference */
  40566. + ipsecdevices[i]=NULL;
  40567. + ipsec_dev_put(dev_ipsec);
  40568. +
  40569. + KLIPS_PRINT(debug_tunnel, "Unregistering %s (refcnt=%d)\n",
  40570. + dev_ipsec->name,
  40571. + atomic_read(&dev_ipsec->refcnt));
  40572. + unregister_netdev(dev_ipsec);
  40573. + KLIPS_PRINT(debug_tunnel, "Unregisted %s\n", dev_ipsec->name);
  40574. +#ifndef NETDEV_23
  40575. + kfree(dev_ipsec->name);
  40576. + dev_ipsec->name=NULL;
  40577. +#endif /* !NETDEV_23 */
  40578. + kfree(dev_ipsec->priv);
  40579. + dev_ipsec->priv=NULL;
  40580. + }
  40581. + return error;
  40582. +}
  40583. +
  40584. +/*
  40585. + * $Log: ipsec_tunnel.c,v $
  40586. + * Revision 1.232.2.5 2006/10/06 21:39:26 paul
  40587. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  40588. + * set. This is defined through autoconf.h which is included through the
  40589. + * linux kernel build macros.
  40590. + *
  40591. + * Revision 1.232.2.4 2006/03/28 20:58:19 ken
  40592. + * Fix for KLIPS on 2.6.16 - need to include <net/arp.h> now
  40593. + *
  40594. + * Revision 1.232.2.3 2006/02/15 05:14:12 paul
  40595. + * 568: uninitialized struct in ipsec_tunnel.c coud break routing under 2.6 kernels
  40596. + * ipsec_tunnel_send() calls the entry point function of routing subsystem
  40597. + * (ip_route_output_key()) using a not fully initialized struct of type
  40598. + * struct flowi.
  40599. + * This will cause a failure in routing packets through an ipsec interface
  40600. + * when patches for multipath routing from http://www.ssi.bg/~ja/
  40601. + * are applied.
  40602. + *
  40603. + * Revision 1.232.2.2 2005/11/22 04:11:52 ken
  40604. + * Backport fixes for 2.6.14 kernels from HEAD
  40605. + *
  40606. + * Revision 1.232.2.1 2005/09/21 22:57:43 paul
  40607. + * pulled up compile fix for 2.6.13
  40608. + *
  40609. + * Revision 1.232 2005/06/04 16:06:06 mcr
  40610. + * better patch for nat-t rcv-device code.
  40611. + *
  40612. + * Revision 1.231 2005/05/21 03:28:51 mcr
  40613. + * make sure that port-500 hole is used for port-4500 as well.
  40614. + *
  40615. + * Revision 1.230 2005/05/11 01:42:04 mcr
  40616. + * removal of debugging showed useless/wrong variables used.
  40617. + *
  40618. + * Revision 1.229 2005/04/29 05:10:22 mcr
  40619. + * removed from extraenous includes to make unit testing easier.
  40620. + *
  40621. + * Revision 1.228 2005/01/26 00:50:35 mcr
  40622. + * adjustment of confusion of CONFIG_IPSEC_NAT vs CONFIG_KLIPS_NAT,
  40623. + * and make sure that NAT_TRAVERSAL is set as well to match
  40624. + * userspace compiles of code.
  40625. + *
  40626. + * Revision 1.227 2004/12/10 21:16:08 ken
  40627. + * 64bit fixes from Opteron port of KLIPS 2.6
  40628. + *
  40629. + * Revision 1.226 2004/12/04 07:11:23 mcr
  40630. + * fix for snmp SIOCPRIVATE use of snmpd.
  40631. + * http://bugs.xelerance.com/view.php?id=144
  40632. + *
  40633. + * Revision 1.225 2004/12/03 21:25:57 mcr
  40634. + * compile time fixes for running on 2.6.
  40635. + * still experimental.
  40636. + *
  40637. + * Revision 1.224 2004/08/14 03:28:24 mcr
  40638. + * fixed log comment to remove warning about embedded comment.
  40639. + *
  40640. + * Revision 1.223 2004/08/04 15:57:07 mcr
  40641. + * moved des .h files to include/des/ *
  40642. + * included 2.6 protocol specific things
  40643. + * started at NAT-T support, but it will require a kernel patch.
  40644. + *
  40645. + * Revision 1.222 2004/08/03 18:19:08 mcr
  40646. + * in 2.6, use "net_device" instead of #define device->net_device.
  40647. + * this probably breaks 2.0 compiles.
  40648. + *
  40649. + * Revision 1.221 2004/07/10 19:11:18 mcr
  40650. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  40651. + *
  40652. + * Revision 1.220 2004/04/06 02:49:26 mcr
  40653. + * pullup of algo code from alg-branch.
  40654. + *
  40655. + * Revision 1.219 2004/02/03 03:13:17 mcr
  40656. + * minor edits for readability, and error reporting.
  40657. + *
  40658. + * Revision 1.218 2004/01/27 20:29:20 mcr
  40659. + * fix for unregister_netdev() problem for underlying eth0.
  40660. + *
  40661. + * Revision 1.217 2003/12/10 01:14:27 mcr
  40662. + * NAT-traversal patches to KLIPS.
  40663. + *
  40664. + * Revision 1.216 2003/12/04 23:01:17 mcr
  40665. + * removed ipsec_netlink.h
  40666. + *
  40667. + * Revision 1.215 2003/12/04 16:35:16 ken
  40668. + * Fix for ATM devices where physdev->hard_header_len *is* correct
  40669. + *
  40670. + * Revision 1.214 2003/11/25 23:52:37 mcr
  40671. + * fix typo in patch - ixs-> needed.
  40672. + *
  40673. + * Revision 1.213 2003/11/24 18:25:49 mcr
  40674. + * patch from willy@w.ods.org to fix problems with ATM interfaces.
  40675. + *
  40676. + * Revision 1.212 2003/10/31 02:27:55 mcr
  40677. + * pulled up port-selector patches and sa_id elimination.
  40678. + *
  40679. + * Revision 1.211.2.2 2003/10/29 01:30:41 mcr
  40680. + * elimited "struct sa_id".
  40681. + *
  40682. + * Revision 1.211.2.1 2003/09/21 13:59:56 mcr
  40683. + * pre-liminary X.509 patch - does not yet pass tests.
  40684. + *
  40685. + * Revision 1.211 2003/09/10 16:46:30 mcr
  40686. + * patches for 2.4 backport/2.6 existence.
  40687. + *
  40688. + * Revision 1.210 2003/07/31 22:47:16 mcr
  40689. + * preliminary (untested by FS-team) 2.5 patches.
  40690. + *
  40691. + * Revision 1.209 2003/06/22 21:28:43 mcr
  40692. + * inability to unload module was caused by calls to dev_get
  40693. + * (ipsec_dev_get), to gather a device from a name. There is
  40694. + * simply no reason to look the devices up - they should be kept
  40695. + * in a nice array, ready for use.
  40696. + *
  40697. + * Revision 1.208 2003/06/22 21:25:07 mcr
  40698. + * all staticly counted ipsecXXX device support removed.
  40699. + *
  40700. + * Revision 1.207 2003/04/02 20:15:37 mcr
  40701. + * fix for PR#204 - do not clear connection tracking info if we
  40702. + * the packet is being sent in the clear.
  40703. + *
  40704. + * Revision 1.206 2003/02/12 19:32:51 rgb
  40705. + * Refactored file to:
  40706. + * ipsec_xmit.c
  40707. + * ipsec_xmit.h
  40708. + * ipsec_mast.c
  40709. + *
  40710. + * Revision 1.205 2003/02/06 17:47:00 rgb
  40711. + *
  40712. + * Remove unused ipsec_tunnel_lock() and ipsec_tunnel_unlock() code.
  40713. + * Refactor ipsec_tunnel_start_xmit() further into:
  40714. + * ipsec_xmit_sanity_check_dev()
  40715. + * ipsec_xmit_sanity_check_skb()
  40716. + * ipsec_xmit_strip_hard_header()
  40717. + * ipsec_xmit_restore_hard_header()
  40718. + * ipsec_xmit_send()
  40719. + * ipsec_xmit_cleanup()
  40720. + * and start a skeletal ipsec_mast_start_xmit() .
  40721. + *
  40722. + * Revision 1.204 2003/02/06 06:43:46 rgb
  40723. + *
  40724. + * Refactor ipsec_tunnel_start_xmit, bringing out:
  40725. + * ipsec_xmit_SAlookup
  40726. + * ipsec_xmit_encap_once
  40727. + * ipsec_xmit_encap_bundle
  40728. + *
  40729. + * Revision 1.203 2003/02/06 02:21:34 rgb
  40730. + *
  40731. + * Moved "struct auth_alg" from ipsec_rcv.c to ipsec_ah.h .
  40732. + * Changed "struct ah" to "struct ahhdr" and "struct esp" to "struct esphdr".
  40733. + * Removed "#ifdef INBOUND_POLICY_CHECK_eroute" dead code.
  40734. + *
  40735. + * Revision 1.202 2003/01/03 07:38:01 rgb
  40736. + *
  40737. + * Start to refactor ipsec_tunnel_start_xmit() by putting local variables
  40738. + * into struct ipsec_xmit_state and renaming a few variables to give more
  40739. + * unique or searchable names.
  40740. + *
  40741. + * Revision 1.201 2003/01/03 00:31:28 rgb
  40742. + *
  40743. + * Clean up memset usage, including fixing 2 places where keys were not
  40744. + * properly wiped.
  40745. + *
  40746. + * Revision 1.200 2002/12/06 02:24:02 mcr
  40747. + * patches for compiling against SUSE 8.1 kernels. Requires
  40748. + * an additional -DSUSE_LINUX_2_4_19_IS_STUPID.
  40749. + *
  40750. + * Revision 1.199 2002/10/12 23:11:53 dhr
  40751. + *
  40752. + * [KenB + DHR] more 64-bit cleanup
  40753. + *
  40754. + * Revision 1.198 2002/10/05 05:02:58 dhr
  40755. + *
  40756. + * C labels go on statements
  40757. + *
  40758. + * Revision 1.197 2002/09/20 05:01:50 rgb
  40759. + * Added compiler directive to switch on IP options and fix IP options bug.
  40760. + * Make ip->ihl treatment consistent using shifts rather than multiplications.
  40761. + * Check for large enough packet before accessing udp header for IKE bypass.
  40762. + * Added memory allocation debugging.
  40763. + * Fixed potential memory allocation failure-induced oops.
  40764. + *
  40765. + * Revision 1.196 2002/07/24 18:44:54 rgb
  40766. + * Type fiddling to tame ia64 compiler.
  40767. + *
  40768. + * Revision 1.195 2002/07/23 03:36:07 rgb
  40769. + * Fixed 2.2 device initialisation hang.
  40770. + *
  40771. + * Revision 1.194 2002/05/27 21:40:34 rgb
  40772. + * Set unused ipsec devices to ARPHRD_VOID to avoid confusing iproute2.
  40773. + * Cleaned up intermediate step to dynamic device allocation.
  40774. + *
  40775. + * Revision 1.193 2002/05/27 19:31:36 rgb
  40776. + * Convert to dynamic ipsec device allocation.
  40777. + * Remove final vistiges of tdb references via IPSEC_KLIPS1_COMPAT.
  40778. + *
  40779. + * Revision 1.192 2002/05/23 07:14:28 rgb
  40780. + * Added refcount code.
  40781. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  40782. + *
  40783. + * Revision 1.191 2002/05/14 02:34:37 rgb
  40784. + * Change all references to tdb, TDB or Tunnel Descriptor Block to ips,
  40785. + * ipsec_sa or ipsec_sa.
  40786. + *
  40787. + * Revision 1.190 2002/04/24 07:55:32 mcr
  40788. + * #include patches and Makefiles for post-reorg compilation.
  40789. + *
  40790. + * Revision 1.189 2002/04/24 07:36:32 mcr
  40791. + * Moved from ./klips/net/ipsec/ipsec_tunnel.c,v
  40792. + *
  40793. + * Revision 1.188 2002/04/20 00:12:25 rgb
  40794. + * Added esp IV CBC attack fix, disabled.
  40795. + *
  40796. + * Revision 1.187 2002/03/23 19:55:17 rgb
  40797. + * Fix for 2.2 local IKE fragmentation blackhole. Still won't work if
  40798. + * iptraf or another pcap app is running.
  40799. + *
  40800. + * Revision 1.186 2002/03/19 03:26:22 rgb
  40801. + * Applied DHR's tunnel patch to streamline IKE/specialSA processing.
  40802. + *
  40803. + * Revision 1.185 2002/02/20 04:13:05 rgb
  40804. + * Send back ICMP_PKT_FILTERED upon %reject.
  40805. + *
  40806. + * Revision 1.184 2002/01/29 17:17:56 mcr
  40807. + * moved include of ipsec_param.h to after include of linux/kernel.h
  40808. + * otherwise, it seems that some option that is set in ipsec_param.h
  40809. + * screws up something subtle in the include path to kernel.h, and
  40810. + * it complains on the snprintf() prototype.
  40811. + *
  40812. + * Revision 1.183 2002/01/29 04:00:53 mcr
  40813. + * more excise of kversions.h header.
  40814. + *
  40815. + * Revision 1.182 2002/01/29 02:13:18 mcr
  40816. + * introduction of ipsec_kversion.h means that include of
  40817. + * ipsec_param.h must preceed any decisions about what files to
  40818. + * include to deal with differences in kernel source.
  40819. + *
  40820. + * Revision 1.181 2002/01/07 20:00:33 rgb
  40821. + * Added IKE destination port debugging.
  40822. + *
  40823. + * Revision 1.180 2001/12/21 21:49:54 rgb
  40824. + * Fixed bug as a result of moving IKE bypass above %trap/%hold code.
  40825. + *
  40826. + * Revision 1.179 2001/12/19 21:08:14 rgb
  40827. + * Added transport protocol ports to ipsec_print_ip().
  40828. + * Update eroute info for non-SA targets.
  40829. + * Added obey DF code disabled.
  40830. + * Fixed formatting bugs in ipsec_tunnel_hard_header().
  40831. + *
  40832. + * Revision 1.178 2001/12/05 09:36:10 rgb
  40833. + * Moved the UDP/500 IKE check just above the %hold/%trap checks to avoid
  40834. + * IKE packets being stolen by the %hold (and returned to the sending KMd
  40835. + * in an ACQUIRE, ironically ;-).
  40836. + *
  40837. + * Revision 1.177 2001/11/26 09:23:50 rgb
  40838. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  40839. + *
  40840. + * Revision 1.170.2.1 2001/09/25 02:28:27 mcr
  40841. + * struct tdb -> struct ipsec_sa.
  40842. + * lifetime checks moved to common routines.
  40843. + * cleaned up includes.
  40844. + *
  40845. + * Revision 1.170.2.2 2001/10/22 21:08:01 mcr
  40846. + * include des.h, removed phony prototypes and fixed calling
  40847. + * conventions to match real prototypes.
  40848. + *
  40849. + * Revision 1.176 2001/11/09 18:32:31 rgb
  40850. + * Added Hans Schultz' fragmented UDP/500 IKE socket port selector.
  40851. + *
  40852. + * Revision 1.175 2001/11/06 20:47:00 rgb
  40853. + * Added Eric Espie's TRAPSUBNET fix, minus spin-lock-bh dabbling.
  40854. + *
  40855. + * Revision 1.174 2001/11/06 19:50:43 rgb
  40856. + * Moved IP_SEND, ICMP_SEND, DEV_QUEUE_XMIT macros to ipsec_tunnel.h for
  40857. + * use also by pfkey_v2_parser.c
  40858. + *
  40859. + * Revision 1.173 2001/10/29 21:53:44 henry
  40860. + * tone down the device-down message slightly, until we can make it smarter
  40861. + *
  40862. + * Revision 1.172 2001/10/26 04:59:37 rgb
  40863. + * Added a critical level syslog message if an ipsec device goes down.
  40864. + *
  40865. + * Revision 1.171 2001/10/18 04:45:21 rgb
  40866. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  40867. + * lib/freeswan.h version macros moved to lib/kversions.h.
  40868. + * Other compiler directive cleanups.
  40869. + *
  40870. + * Revision 1.170 2001/09/25 00:09:50 rgb
  40871. + * Added NetCelo's TRAPSUBNET code to convert a new type TRAPSUBNET into a
  40872. + * HOLD.
  40873. + *
  40874. + * Revision 1.169 2001/09/15 16:24:05 rgb
  40875. + * Re-inject first and last HOLD packet when an eroute REPLACE is done.
  40876. + *
  40877. + * Revision 1.168 2001/09/14 16:58:37 rgb
  40878. + * Added support for storing the first and last packets through a HOLD.
  40879. + *
  40880. + * Revision 1.167 2001/09/08 21:13:33 rgb
  40881. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  40882. + *
  40883. + * Revision 1.166 2001/08/27 19:47:59 rgb
  40884. + * Clear tdb before usage.
  40885. + * Added comment: clear IF before calling routing?
  40886. + *
  40887. + * Revision 1.165 2001/07/03 01:23:53 rgb
  40888. + * Send back ICMP iff DF set, !ICMP, offset==0, sysctl_icmp, iph->tot_len >
  40889. + * emtu, and don't drop.
  40890. + *
  40891. + * Revision 1.164 2001/06/14 19:35:10 rgb
  40892. + * Update copyright date.
  40893. + *
  40894. + * Revision 1.163 2001/06/06 20:28:51 rgb
  40895. + * Added sanity checks for NULL skbs and devices.
  40896. + * Added more debugging output to various functions.
  40897. + * Removed redundant dev->priv argument to ipsec_tunnel_{at,de}tach().
  40898. + * Renamed ipsec_tunnel_attach() virtual and physical device arguments.
  40899. + * Corrected neigh_setup() device function assignment.
  40900. + * Keep valid pointers to ipsec_tunnel_*() on detach.
  40901. + * Set dev->type to the originally-initiallised value.
  40902. + *
  40903. + * Revision 1.162 2001/06/01 07:28:04 rgb
  40904. + * Added sanity checks for detached devices. Don't down virtual devices
  40905. + * to prevent packets going out in the clear if the detached device comes
  40906. + * back up.
  40907. + *
  40908. + * Revision 1.161 2001/05/30 08:14:52 rgb
  40909. + * Removed vestiges of esp-null transforms.
  40910. + * NetDev Notifier instrumentation to track down disappearing devices.
  40911. + *
  40912. + * Revision 1.160 2001/05/29 05:15:12 rgb
  40913. + * Added SS' PMTU patch which notifies sender if packet doesn't fit
  40914. + * physical MTU (if it wasn't ICMP) and then drops it.
  40915. + *
  40916. + * Revision 1.159 2001/05/27 06:12:12 rgb
  40917. + * Added structures for pid, packet count and last access time to eroute.
  40918. + * Added packet count to beginning of /proc/net/ipsec_eroute.
  40919. + *
  40920. + * Revision 1.158 2001/05/24 05:39:33 rgb
  40921. + * Applied source zeroing to 2.2 ip_route_output() call as well to enable
  40922. + * PASS eroutes for opportunism.
  40923. + *
  40924. + * Revision 1.157 2001/05/23 22:35:28 rgb
  40925. + * 2.4 source override simplification.
  40926. + *
  40927. + * Revision 1.156 2001/05/23 21:41:31 rgb
  40928. + * Added error return code printing on ip_route_output().
  40929. + *
  40930. + * Revision 1.155 2001/05/23 05:09:13 rgb
  40931. + * Fixed incorrect ip_route_output() failure message.
  40932. + *
  40933. + * Revision 1.154 2001/05/21 14:53:31 rgb
  40934. + * Added debug statement for case when ip_route_output() fails, causing
  40935. + * packet to be dropped, but log looked ok.
  40936. + *
  40937. + * Revision 1.153 2001/05/19 02:37:54 rgb
  40938. + * Fixed missing comment termination.
  40939. + *
  40940. + * Revision 1.152 2001/05/19 02:35:50 rgb
  40941. + * Debug code optimisation for non-debug speed.
  40942. + * Kernel version compiler define comments.
  40943. + * 2.2 and 2.4 kernel ip_send device and ip debug output added.
  40944. + *
  40945. + * Revision 1.151 2001/05/18 16:17:35 rgb
  40946. + * Changed reference from "magic" to "shunt" SAs.
  40947. + *
  40948. + * Revision 1.150 2001/05/18 16:12:19 rgb
  40949. + * Changed UDP/500 bypass test from 3 nested ifs to one anded if.
  40950. + *
  40951. + * Revision 1.149 2001/05/16 04:39:33 rgb
  40952. + * Add default == eroute.dest to IKE bypass conditions for magic eroutes.
  40953. + *
  40954. + * Revision 1.148 2001/05/05 03:31:41 rgb
  40955. + * IP frag debugging updates and enhancements.
  40956. + *
  40957. + * Revision 1.147 2001/05/03 19:41:40 rgb
  40958. + * Added SS' skb_cow fix for 2.4.4.
  40959. + *
  40960. + * Revision 1.146 2001/04/30 19:28:16 rgb
  40961. + * Update for 2.4.4. ip_select_ident() now has 3 args.
  40962. + *
  40963. + * Revision 1.145 2001/04/23 14:56:10 rgb
  40964. + * Added spin_lock() check to prevent double-locking for multiple
  40965. + * transforms and hence kernel lock-ups with SMP kernels.
  40966. + *
  40967. + * Revision 1.144 2001/04/21 23:04:45 rgb
  40968. + * Define out skb->used for 2.4 kernels.
  40969. + * Check if soft expire has already been sent before sending another to
  40970. + * prevent ACQUIRE flooding.
  40971. + *
  40972. + * Revision 1.143 2001/03/16 07:37:21 rgb
  40973. + * Added comments to all #endifs.
  40974. + *
  40975. + * Revision 1.142 2001/02/28 05:03:27 rgb
  40976. + * Clean up and rationalise startup messages.
  40977. + *
  40978. + * Revision 1.141 2001/02/27 22:24:54 rgb
  40979. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  40980. + * Check for satoa() return codes.
  40981. + *
  40982. + * Revision 1.140 2001/02/27 06:40:12 rgb
  40983. + * Fixed TRAP->HOLD eroute byte order.
  40984. + *
  40985. + * Revision 1.139 2001/02/26 20:38:59 rgb
  40986. + * Added compiler defines for 2.4.x-specific code.
  40987. + *
  40988. + * Revision 1.138 2001/02/26 19:57:27 rgb
  40989. + * Implement magic SAs %drop, %reject, %trap, %hold, %pass as part
  40990. + * of the new SPD and to support opportunistic.
  40991. + * Drop sysctl_ipsec_{no_eroute_pass,opportunistic}, replaced by magic SAs.
  40992. + *
  40993. + * Revision 1.137 2001/02/19 22:29:49 rgb
  40994. + * Fixes for presence of active ipv6 segments which share ipsec physical
  40995. + * device (gg).
  40996. + *
  40997. + * Revision 1.136 2001/01/29 22:30:38 rgb
  40998. + * Fixed minor acquire debug printing bug.
  40999. + *
  41000. + * Revision 1.135 2001/01/29 22:19:45 rgb
  41001. + * Zero source address for 2.4 bypass route lookup.
  41002. + *
  41003. + * Revision 1.134 2001/01/23 20:19:49 rgb
  41004. + * 2.4 fix to remove removed is_clone member.
  41005. + *
  41006. + * Revision 1.133 2000/12/09 22:08:35 rgb
  41007. + * Fix NET_23 bug, should be NETDEV_23.
  41008. + *
  41009. + * Revision 1.132 2000/12/01 06:54:50 rgb
  41010. + * Fix for new 2.4 IP TTL default variable name.
  41011. + *
  41012. + * Revision 1.131 2000/11/09 20:52:15 rgb
  41013. + * More spinlock shuffling, locking earlier and unlocking later in rcv to
  41014. + * include ipcomp and prevent races, renaming some tdb variables that got
  41015. + * forgotten, moving some unlocks to include tdbs and adding a missing
  41016. + * unlock. Thanks to Svenning for some of these.
  41017. + *
  41018. + * Revision 1.130 2000/11/09 20:11:22 rgb
  41019. + * Minor shuffles to fix non-standard kernel config option selection.
  41020. + *
  41021. + * Revision 1.129 2000/11/06 04:32:49 rgb
  41022. + * Clean up debug printing.
  41023. + * Copy skb->protocol for all kernel versions.
  41024. + * Ditched spin_lock_irqsave in favour of spin_lock.
  41025. + * Disabled TTL decrement, done in ip_forward.
  41026. + * Added debug printing before pfkey_acquire().
  41027. + * Fixed printk-deltdbchain-spin_lock races (Svenning).
  41028. + * Use defaultTTL for 2.1+ kernels.
  41029. + * Add Svenning's adaptive content compression.
  41030. + * Fix up debug display arguments.
  41031. + *
  41032. + * Revision 1.128 2000/09/28 00:58:57 rgb
  41033. + * Moved the IKE passthrough check after the eroute lookup so we can pass
  41034. + * IKE through intermediate tunnels.
  41035. + *
  41036. + * Revision 1.127 2000/09/22 17:52:11 rgb
  41037. + * Fixed misleading ipcomp debug output.
  41038. + *
  41039. + * Revision 1.126 2000/09/22 04:22:56 rgb
  41040. + * Fixed dumb spi->cpi conversion error.
  41041. + *
  41042. + * Revision 1.125 2000/09/21 04:34:48 rgb
  41043. + * A few debug-specific things should be hidden under
  41044. + * CONFIG_IPSEC_DEBUG.(MB)
  41045. + * Improved ip_send() error handling.(MB)
  41046. + *
  41047. + * Revision 1.124 2000/09/21 03:40:58 rgb
  41048. + * Added more debugging to try and track down the cpi outward copy problem.
  41049. + *
  41050. + * Revision 1.123 2000/09/19 07:08:49 rgb
  41051. + * Added debugging to outgoing compression report.
  41052. + *
  41053. + * Revision 1.122 2000/09/18 19:21:26 henry
  41054. + * RGB-supplied fix for RH5.2 problem
  41055. + *
  41056. + * Revision 1.121 2000/09/17 21:05:09 rgb
  41057. + * Added tdb to skb_compress call to write in cpi.
  41058. + *
  41059. + * Revision 1.120 2000/09/17 16:57:16 rgb
  41060. + * Added Svenning's patch to remove restriction of ipcomp to innermost
  41061. + * transform.
  41062. + *
  41063. + * Revision 1.119 2000/09/15 11:37:01 rgb
  41064. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  41065. + * IPCOMP zlib deflate code.
  41066. + *
  41067. + * Revision 1.118 2000/09/15 04:57:16 rgb
  41068. + * Moved debug output after sanity check.
  41069. + * Added tos copy sysctl.
  41070. + *
  41071. + * Revision 1.117 2000/09/12 03:22:51 rgb
  41072. + * Converted ipsec_icmp, no_eroute_pass, opportunistic and #if0 debugs to
  41073. + * sysctl.
  41074. + *
  41075. + * Revision 1.116 2000/09/08 19:18:19 rgb
  41076. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  41077. + * Added outgoing opportunistic hook, ifdef'ed out.
  41078. + *
  41079. + * Revision 1.115 2000/08/30 05:27:29 rgb
  41080. + * Removed all the rest of the references to tdb_spi, tdb_proto, tdb_dst.
  41081. + * Kill remainder of tdb_xform, tdb_xdata, xformsw.
  41082. + *
  41083. + * Revision 1.114 2000/08/28 18:15:46 rgb
  41084. + * Added MB's nf-debug reset patch.
  41085. + *
  41086. + * Revision 1.113 2000/08/27 02:26:40 rgb
  41087. + * Send all no-eroute-bypass, pluto-bypass and passthrough packets through
  41088. + * fragmentation machinery for 2.0, 2.2 and 2.4 kernels.
  41089. + *
  41090. + * Revision 1.112 2000/08/20 21:37:33 rgb
  41091. + * Activated pfkey_expire() calls.
  41092. + * Added a hard/soft expiry parameter to pfkey_expire(). (Momchil)
  41093. + * Re-arranged the order of soft and hard expiry to conform to RFC2367.
  41094. + * Clean up references to CONFIG_IPSEC_PFKEYv2.
  41095. + *
  41096. + * Revision 1.111 2000/08/01 14:51:51 rgb
  41097. + * Removed _all_ remaining traces of DES.
  41098. + *
  41099. + * Revision 1.110 2000/07/28 14:58:31 rgb
  41100. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  41101. + *
  41102. + * Revision 1.109 2000/07/28 13:50:54 rgb
  41103. + * Changed enet_statistics to net_device_stats and added back compatibility
  41104. + * for pre-2.1.19.
  41105. + *
  41106. + * Revision 1.108 2000/05/16 03:03:11 rgb
  41107. + * Updates for 2.3.99pre8 from MB.
  41108. + *
  41109. + * Revision 1.107 2000/05/10 23:08:21 rgb
  41110. + * Print a debug warning about bogus packets received by the outgoing
  41111. + * processing machinery only when klipsdebug is not set to none.
  41112. + * Comment out the device initialisation informational messages.
  41113. + *
  41114. + * Revision 1.106 2000/05/10 19:17:14 rgb
  41115. + * Define an IP_SEND macro, intending to have all packet passthroughs
  41116. + * use fragmentation. This didn't quite work, but is a step in the
  41117. + * right direction.
  41118. + * Added buffer allocation debugging statements.
  41119. + * Added configure option to shut off no eroute passthrough.
  41120. + * Only check usetime against soft and hard limits if the tdb has been
  41121. + * used.
  41122. + * Cast output of ntohl so that the broken prototype doesn't make our
  41123. + * compile noisy.
  41124. + *
  41125. + * Revision 1.105 2000/03/22 16:15:37 rgb
  41126. + * Fixed renaming of dev_get (MB).
  41127. + *
  41128. + * Revision 1.104 2000/03/16 14:04:15 rgb
  41129. + * Indented headers for readability.
  41130. + * Fixed debug scope to enable compilation with debug off.
  41131. + * Added macros for ip_chk_addr and IS_MYADDR for identifying self.
  41132. + *
  41133. + * Revision 1.103 2000/03/16 07:11:07 rgb
  41134. + * Hardcode PF_KEYv2 support.
  41135. + * Fixed bug which allowed UDP/500 packet from another machine
  41136. + * through in the clear.
  41137. + * Added disabled skb->protocol fix for ISDN/ASYNC PPP from Matjaz Godec.
  41138. + *
  41139. + * Revision 1.102 2000/03/14 12:26:59 rgb
  41140. + * Added skb->nfct support for clearing netfilter conntrack bits (MB).
  41141. + *
  41142. + * Revision 1.101 2000/02/14 21:05:22 rgb
  41143. + * Added MB's netif_queue fix for kernels 2.3.43+.
  41144. + *
  41145. + * Revision 1.100 2000/01/26 10:04:57 rgb
  41146. + * Fixed noisy 2.0 printk arguments.
  41147. + *
  41148. + * Revision 1.99 2000/01/21 06:16:25 rgb
  41149. + * Added sanity checks on skb_push(), skb_pull() to prevent panics.
  41150. + * Switched to AF_ENCAP macro.
  41151. + * Shortened debug output per packet and re-arranging debug_tunnel
  41152. + * bitmap flags, while retaining necessary information to avoid
  41153. + * trampling the kernel print ring buffer.
  41154. + * Reformatted recursion switch code.
  41155. + * Changed all references to tdb_proto to tdb_said.proto for clarity.
  41156. + *
  41157. + * Revision 1.98 2000/01/13 08:09:31 rgb
  41158. + * Shuffled debug_tunnel switches to focus output.
  41159. + * Fixed outgoing recursion bug, limiting to recursing only if the remote
  41160. + * SG changes and if it is valid, ie. not passthrough.
  41161. + * Clarified a number of debug messages.
  41162. + *
  41163. + * Revision 1.97 2000/01/10 16:37:16 rgb
  41164. + * MB support for new ip_select_ident() upon disappearance of
  41165. + * ip_id_count in 2.3.36+.
  41166. + *
  41167. + * Revision 1.96 1999/12/31 14:59:08 rgb
  41168. + * MB fix to use new skb_copy_expand in kernel 2.3.35.
  41169. + *
  41170. + * Revision 1.95 1999/12/29 21:15:44 rgb
  41171. + * Fix tncfg to aliased device bug.
  41172. + *
  41173. + * Revision 1.94 1999/12/22 04:26:06 rgb
  41174. + * Converted all 'static' functions to 'DEBUG_NO_STATIC' to enable
  41175. + * debugging by providing external labels to all functions with debugging
  41176. + * turned on.
  41177. + *
  41178. + * Revision 1.93 1999/12/13 13:30:14 rgb
  41179. + * Changed MTU reports and HW address reporting back to debug only.
  41180. + *
  41181. + * Revision 1.92 1999/12/07 18:57:56 rgb
  41182. + * Fix PFKEY symbol compile error (SADB_*) without pfkey enabled.
  41183. + *
  41184. + * Revision 1.91 1999/12/01 22:15:36 rgb
  41185. + * Add checks for LARVAL and DEAD SAs.
  41186. + * Change state of SA from MATURE to DYING when a soft lifetime is
  41187. + * reached and print debug warning.
  41188. + *
  41189. + * Revision 1.90 1999/11/23 23:04:04 rgb
  41190. + * Use provided macro ADDRTOA_BUF instead of hardcoded value.
  41191. + * Sort out pfkey and freeswan headers, putting them in a library path.
  41192. + *
  41193. + * Revision 1.89 1999/11/18 18:50:59 rgb
  41194. + * Changed all device registrations for static linking to
  41195. + * dynamic to reduce the number and size of patches.
  41196. + *
  41197. + * Revision 1.88 1999/11/18 04:09:19 rgb
  41198. + * Replaced all kernel version macros to shorter, readable form.
  41199. + *
  41200. + * Revision 1.87 1999/11/17 15:53:40 rgb
  41201. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  41202. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  41203. + * klips/net/ipsec/Makefile.
  41204. + *
  41205. + * Revision 1.86 1999/10/16 18:25:37 rgb
  41206. + * Moved SA lifetime expiry checks before packet processing.
  41207. + * Expire SA on replay counter rollover.
  41208. + *
  41209. + * Revision 1.85 1999/10/16 04:24:31 rgb
  41210. + * Add stats for time since last packet.
  41211. + *
  41212. + * Revision 1.84 1999/10/16 00:30:47 rgb
  41213. + * Added SA lifetime counting.
  41214. + *
  41215. + * Revision 1.83 1999/10/15 22:15:57 rgb
  41216. + * Clean out cruft.
  41217. + * Add debugging.
  41218. + *
  41219. + * Revision 1.82 1999/10/08 18:26:19 rgb
  41220. + * Fix 2.0.3x outgoing fragmented packet memory leak.
  41221. + *
  41222. + * Revision 1.81 1999/10/05 02:38:54 rgb
  41223. + * Lower the default mtu of virtual devices to 16260.
  41224. + *
  41225. + * Revision 1.80 1999/10/03 18:56:41 rgb
  41226. + * Spinlock support for 2.3.xx.
  41227. + * Don't forget to undo spinlocks on error!
  41228. + * Check for valid eroute before copying the structure.
  41229. + *
  41230. + * Revision 1.79 1999/10/01 15:44:53 rgb
  41231. + * Move spinlock header include to 2.1> scope.
  41232. + *
  41233. + * Revision 1.78 1999/10/01 00:02:43 rgb
  41234. + * Added tdb structure locking.
  41235. + * Added eroute structure locking.
  41236. + *
  41237. + * Revision 1.77 1999/09/30 02:52:29 rgb
  41238. + * Add Marc Boucher's Copy-On-Write code (same as ipsec_rcv.c).
  41239. + *
  41240. + * Revision 1.76 1999/09/25 19:31:27 rgb
  41241. + * Refine MSS hack to affect SYN, but not SYN+ACK packets.
  41242. + *
  41243. + * Revision 1.75 1999/09/24 22:52:38 rgb
  41244. + * Fix two things broken in 2.0.38 by trying to fix network notifiers.
  41245. + *
  41246. + * Revision 1.74 1999/09/24 00:30:37 rgb
  41247. + * Add test for changed source as well as destination to check for
  41248. + * recursion.
  41249. + *
  41250. + * Revision 1.73 1999/09/23 20:52:24 rgb
  41251. + * Add James Morris' MSS hack patch, disabled.
  41252. + *
  41253. + * Revision 1.72 1999/09/23 20:22:40 rgb
  41254. + * Enable, tidy and fix network notifier code.
  41255. + *
  41256. + * Revision 1.71 1999/09/23 18:09:05 rgb
  41257. + * Clean up 2.2.x fragmenting traces.
  41258. + * Disable dev->type switching, forcing ARPHRD_TUNNEL.
  41259. + *
  41260. + * Revision 1.70 1999/09/22 14:14:24 rgb
  41261. + * Add sanity checks for revectored calls to prevent calling a downed I/F.
  41262. + *
  41263. + * Revision 1.69 1999/09/21 15:00:57 rgb
  41264. + * Add Marc Boucher's packet size check.
  41265. + * Flesh out network device notifier code.
  41266. + *
  41267. + * Revision 1.68 1999/09/18 11:39:57 rgb
  41268. + * Start to add (disabled) netdevice notifier code.
  41269. + *
  41270. + * Revision 1.67 1999/09/17 23:44:40 rgb
  41271. + * Add a comment warning potential code hackers to stay away from mac.raw.
  41272. + *
  41273. + * Revision 1.66 1999/09/17 18:04:02 rgb
  41274. + * Add fix for unpredictable hard_header_len for ISDN folks (thanks MB).
  41275. + * Ditch TTL decrement in 2.2 (MB).
  41276. + *
  41277. + * Revision 1.65 1999/09/15 23:15:35 henry
  41278. + * Marc Boucher's PPP fixes
  41279. + *
  41280. + * Revision 1.64 1999/09/07 13:40:53 rgb
  41281. + * Ditch unreliable references to skb->mac.raw.
  41282. + *
  41283. + * Revision 1.63 1999/08/28 11:33:09 rgb
  41284. + * Check for null skb->mac pointer.
  41285. + *
  41286. + * Revision 1.62 1999/08/28 02:02:30 rgb
  41287. + * Add Marc Boucher's fix for properly dealing with skb->sk.
  41288. + *
  41289. + * Revision 1.61 1999/08/27 05:23:05 rgb
  41290. + * Clean up skb->data/raw/nh/h manipulation.
  41291. + * Add Marc Boucher's mods to aid tcpdump.
  41292. + * Add sanity checks to skb->raw/nh/h pointer copies in skb_copy_expand.
  41293. + * Re-order hard_header stripping -- might be able to remove it...
  41294. + *
  41295. + * Revision 1.60 1999/08/26 20:01:02 rgb
  41296. + * Tidy up compiler directives and macros.
  41297. + * Re-enable ICMP for tunnels where inner_dst != outer_dst.
  41298. + * Remove unnecessary skb->dev = physdev assignment affecting 2.2.x.
  41299. + *
  41300. + * Revision 1.59 1999/08/25 15:44:41 rgb
  41301. + * Clean up from 2.2.x instrumenting for compilation under 2.0.36.
  41302. + *
  41303. + * Revision 1.58 1999/08/25 15:00:54 rgb
  41304. + * Add dst cache code for 2.2.xx.
  41305. + * Add sanity check for skb packet header pointers.
  41306. + * Add/modify debugging instrumentation to *_start_xmit, *_hard_header and
  41307. + * *_rebuild_header.
  41308. + * Add neigh_* cache code.
  41309. + * Change dev->type back to ARPHRD_TUNNEL.
  41310. + *
  41311. + * Revision 1.57 1999/08/17 21:50:23 rgb
  41312. + * Fixed minor debug output bugs.
  41313. + * Regrouped error recovery exit code.
  41314. + * Added compiler directives to remove unwanted code and symbols.
  41315. + * Shut off ICMP messages: to be refined to only send ICMP to remote systems.
  41316. + * Add debugging code for output function addresses.
  41317. + * Fix minor bug in (possibly unused) header_cache_bind function.
  41318. + * Add device neighbour caching code.
  41319. + * Change dev->type from ARPHRD_TUNNEL to physdev->type.
  41320. + *
  41321. + * Revision 1.56 1999/08/03 17:22:56 rgb
  41322. + * Debug output clarification using KERN_* macros. Other inactive changes
  41323. + * added.
  41324. + *
  41325. + * Revision 1.55 1999/08/03 16:58:46 rgb
  41326. + * Fix skb_copy_expand size bug. Was getting incorrect size.
  41327. + *
  41328. + * Revision 1.54 1999/07/14 19:32:38 rgb
  41329. + * Fix oversize packet crash and ssh stalling in 2.2.x kernels.
  41330. + *
  41331. + * Revision 1.53 1999/06/10 15:44:02 rgb
  41332. + * Minor reformatting and clean-up.
  41333. + *
  41334. + * Revision 1.52 1999/05/09 03:25:36 rgb
  41335. + * Fix bug introduced by 2.2 quick-and-dirty patch.
  41336. + *
  41337. + * Revision 1.51 1999/05/08 21:24:59 rgb
  41338. + * Add casting to silence the 2.2.x compile.
  41339. + *
  41340. + * Revision 1.50 1999/05/05 22:02:32 rgb
  41341. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  41342. + *
  41343. + * Revision 1.49 1999/04/29 15:18:52 rgb
  41344. + * Change gettdb parameter to a pointer to reduce stack loading and
  41345. + * facilitate parameter sanity checking.
  41346. + * Fix undetected bug that might have tried to access a null pointer.
  41347. + * Eliminate unnessessary usage of tdb_xform member to further switch
  41348. + * away from the transform switch to the algorithm switch.
  41349. + * Add return values to init and cleanup functions.
  41350. + *
  41351. + * Revision 1.48 1999/04/16 15:38:00 rgb
  41352. + * Minor rearrangement of freeing code to avoid memory leaks with impossible or
  41353. + * rare situations.
  41354. + *
  41355. + * Revision 1.47 1999/04/15 15:37:25 rgb
  41356. + * Forward check changes from POST1_00 branch.
  41357. + *
  41358. + * Revision 1.32.2.4 1999/04/13 21:00:18 rgb
  41359. + * Ditch 'things I wish I had known before...'.
  41360. + *
  41361. + * Revision 1.32.2.3 1999/04/13 20:34:38 rgb
  41362. + * Free skb after fragmentation.
  41363. + * Use stats more effectively.
  41364. + * Add I/F to mtu notch-down reporting.
  41365. + *
  41366. + * Revision 1.32.2.2 1999/04/02 04:26:14 rgb
  41367. + * Backcheck from HEAD, pre1.0.
  41368. + *
  41369. + * Revision 1.46 1999/04/11 00:29:00 henry
  41370. + * GPL boilerplate
  41371. + *
  41372. + * Revision 1.45 1999/04/07 15:42:01 rgb
  41373. + * Fix mtu/ping bug AGAIN!
  41374. + *
  41375. + * Revision 1.44 1999/04/06 04:54:27 rgb
  41376. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  41377. + * patch shell fixes.
  41378. + *
  41379. + * Revision 1.43 1999/04/04 03:57:07 rgb
  41380. + * ip_fragment() doesn't free the supplied skb. Freed.
  41381. + *
  41382. + * Revision 1.42 1999/04/01 23:27:15 rgb
  41383. + * Preload size of virtual mtu.
  41384. + *
  41385. + * Revision 1.41 1999/04/01 09:31:23 rgb
  41386. + * Invert meaning of ICMP PMTUD config option and clarify.
  41387. + * Code clean-up.
  41388. + *
  41389. + * Revision 1.40 1999/04/01 04:37:17 rgb
  41390. + * SSH stalling bug fix.
  41391. + *
  41392. + * Revision 1.39 1999/03/31 23:44:28 rgb
  41393. + * Don't send ICMP on DF and frag_off.
  41394. + *
  41395. + * Revision 1.38 1999/03/31 15:20:10 rgb
  41396. + * Quiet down debugging.
  41397. + *
  41398. + * Revision 1.37 1999/03/31 08:30:31 rgb
  41399. + * Add switch to shut off ICMP PMTUD packets.
  41400. + *
  41401. + * Revision 1.36 1999/03/31 05:44:47 rgb
  41402. + * Keep PMTU reduction private.
  41403. + *
  41404. + * Revision 1.35 1999/03/27 15:13:02 rgb
  41405. + * PMTU/fragmentation bug fix.
  41406. + *
  41407. + * Revision 1.34 1999/03/17 21:19:26 rgb
  41408. + * Fix kmalloc nonatomic bug.
  41409. + *
  41410. + * Revision 1.33 1999/03/17 15:38:42 rgb
  41411. + * Code clean-up.
  41412. + * ESP_NULL IV bug fix.
  41413. + *
  41414. + * Revision 1.32 1999/03/01 20:44:25 rgb
  41415. + * Code clean-up.
  41416. + * Memory leak bug fix.
  41417. + *
  41418. + * Revision 1.31 1999/02/27 00:02:09 rgb
  41419. + * Tune to report the MTU reduction once, rather than after every recursion
  41420. + * through the encapsulating code, preventing tcp stream stalling.
  41421. + *
  41422. + * Revision 1.30 1999/02/24 20:21:01 rgb
  41423. + * Reformat debug printk's.
  41424. + * Fix recursive encapsulation, dynamic MTU bugs and add debugging code.
  41425. + * Clean-up.
  41426. + *
  41427. + * Revision 1.29 1999/02/22 17:08:14 rgb
  41428. + * Fix recursive encapsulation code.
  41429. + *
  41430. + * Revision 1.28 1999/02/19 18:27:02 rgb
  41431. + * Improve DF, fragmentation and PMTU behaviour and add dynamic MTU discovery.
  41432. + *
  41433. + * Revision 1.27 1999/02/17 16:51:37 rgb
  41434. + * Clean out unused cruft.
  41435. + * Temporarily tone down volume of debug output.
  41436. + * Temporarily shut off fragment rejection.
  41437. + * Disabled temporary failed recursive encapsulation loop.
  41438. + *
  41439. + * Revision 1.26 1999/02/12 21:21:26 rgb
  41440. + * Move KLIPS_PRINT to ipsec_netlink.h for accessibility.
  41441. + *
  41442. + * Revision 1.25 1999/02/11 19:38:27 rgb
  41443. + * More clean-up.
  41444. + * Add sanity checking for skb_copy_expand() to prevent kernel panics on
  41445. + * skb_put() values out of range.
  41446. + * Fix head/tailroom calculation causing skb_put() out-of-range values.
  41447. + * Fix return values to prevent 'nonatomic alloc_skb' warnings.
  41448. + * Allocate new skb iff needed.
  41449. + * Added more debug statements.
  41450. + * Make headroom depend on structure, not hard-coded values.
  41451. + *
  41452. + * Revision 1.24 1999/02/10 23:20:33 rgb
  41453. + * Shut up annoying 'statement has no effect' compiler warnings with
  41454. + * debugging compiled out.
  41455. + *
  41456. + * Revision 1.23 1999/02/10 22:36:30 rgb
  41457. + * Clean-up obsolete, unused and messy code.
  41458. + * Converted most IPSEC_DEBUG statements to KLIPS_PRINT macros.
  41459. + * Rename ipsec_tunnel_do_xmit to ipsec_tunnel_start_xmit and eliminated
  41460. + * original ipsec_tunnel_start_xmit.
  41461. + * Send all packet with different inner and outer destinations directly to
  41462. + * the attached physical device, rather than back through ip_forward,
  41463. + * preventing disappearing routes problems.
  41464. + * Do sanity checking before investing too much CPU in allocating new
  41465. + * structures.
  41466. + * Fail on IP header options: We cannot process them yet.
  41467. + * Add some helpful comments.
  41468. + * Use virtual device for parameters instead of physical device.
  41469. + *
  41470. + * Revision 1.22 1999/02/10 03:03:02 rgb
  41471. + * Duh. Fixed the TTL bug: forgot to update the checksum.
  41472. + *
  41473. + * Revision 1.21 1999/02/09 23:17:53 rgb
  41474. + * Add structure members to ipsec_print_ip debug function.
  41475. + * Temporarily fix TTL bug preventing tunnel mode from functioning.
  41476. + *
  41477. + * Revision 1.20 1999/02/09 00:14:25 rgb
  41478. + * Add KLIPSPRINT macro. (Not used yet, though.)
  41479. + * Delete old ip_tunnel code (BADCODE).
  41480. + * Decrement TTL in outgoing packet.
  41481. + * Set TTL on new IPIP_TUNNEL to default, not existing packet TTL.
  41482. + * Delete ethernet only feature and fix hard-coded hard_header_len.
  41483. + *
  41484. + * Revision 1.19 1999/01/29 17:56:22 rgb
  41485. + * 64-bit re-fix submitted by Peter Onion.
  41486. + *
  41487. + * Revision 1.18 1999/01/28 22:43:24 rgb
  41488. + * Fixed bug in ipsec_print_ip that caused an OOPS, found by P.Onion.
  41489. + *
  41490. + * Revision 1.17 1999/01/26 02:08:16 rgb
  41491. + * Removed CONFIG_IPSEC_ALGO_SWITCH macro.
  41492. + * Removed dead code.
  41493. + *
  41494. + * Revision 1.16 1999/01/22 06:25:26 rgb
  41495. + * Cruft clean-out.
  41496. + * Added algorithm switch code.
  41497. + * 64-bit clean-up.
  41498. + * Passthrough on IPIP protocol, spi 0x0 fix.
  41499. + * Enhanced debugging.
  41500. + *
  41501. + * Revision 1.15 1998/12/01 13:22:04 rgb
  41502. + * Added support for debug printing of version info.
  41503. + *
  41504. + * Revision 1.14 1998/11/30 13:22:55 rgb
  41505. + * Rationalised all the klips kernel file headers. They are much shorter
  41506. + * now and won't conflict under RH5.2.
  41507. + *
  41508. + * Revision 1.13 1998/11/17 21:13:52 rgb
  41509. + * Put IKE port bypass debug output in user-switched debug statements.
  41510. + *
  41511. + * Revision 1.12 1998/11/13 13:20:25 rgb
  41512. + * Fixed ntohs bug in udp/500 hole for IKE.
  41513. + *
  41514. + * Revision 1.11 1998/11/10 08:01:19 rgb
  41515. + * Kill tcp/500 hole, keep udp/500 hole.
  41516. + *
  41517. + * Revision 1.10 1998/11/09 21:29:26 rgb
  41518. + * If no eroute is found, discard packet and incr. tx_error.
  41519. + *
  41520. + * Revision 1.9 1998/10/31 06:50:00 rgb
  41521. + * Add tcp/udp/500 bypass.
  41522. + * Fixed up comments in #endif directives.
  41523. + *
  41524. + * Revision 1.8 1998/10/27 00:34:31 rgb
  41525. + * Reformat debug output of IP headers.
  41526. + * Newlines added before calls to ipsec_print_ip.
  41527. + *
  41528. + * Revision 1.7 1998/10/19 14:44:28 rgb
  41529. + * Added inclusion of freeswan.h.
  41530. + * sa_id structure implemented and used: now includes protocol.
  41531. + *
  41532. + * Revision 1.6 1998/10/09 04:31:35 rgb
  41533. + * Added 'klips_debug' prefix to all klips printk debug statements.
  41534. + *
  41535. + * Revision 1.5 1998/08/28 03:09:51 rgb
  41536. + * Prevent kernel log spam with default route through ipsec.
  41537. + *
  41538. + * Revision 1.4 1998/08/05 22:23:09 rgb
  41539. + * Change setdev return code to ENXIO for a non-existant physical device.
  41540. + *
  41541. + * Revision 1.3 1998/07/29 20:41:11 rgb
  41542. + * Add ipsec_tunnel_clear to clear all tunnel attachments.
  41543. + *
  41544. + * Revision 1.2 1998/06/25 20:00:33 rgb
  41545. + * Clean up #endif comments.
  41546. + * Rename dev_ipsec to dev_ipsec0 for consistency.
  41547. + * Document ipsec device fields.
  41548. + * Make ipsec_tunnel_probe visible from rest of kernel for static linking.
  41549. + * Get debugging report for *every* ipsec device initialisation.
  41550. + * Comment out redundant code.
  41551. + *
  41552. + * Revision 1.1 1998/06/18 21:27:50 henry
  41553. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  41554. + * kernel-build scripts happier in the presence of symlinks
  41555. + *
  41556. + * Revision 1.8 1998/06/14 23:49:40 rgb
  41557. + * Clarify version reporting on module loading.
  41558. + *
  41559. + * Revision 1.7 1998/05/27 23:19:20 rgb
  41560. + * Added version reporting.
  41561. + *
  41562. + * Revision 1.6 1998/05/18 21:56:23 rgb
  41563. + * Clean up for numerical consistency of output and cleaning up debug code.
  41564. + *
  41565. + * Revision 1.5 1998/05/12 02:44:23 rgb
  41566. + * Clarifying 'no e-route to host' message.
  41567. + *
  41568. + * Revision 1.4 1998/04/30 15:34:35 rgb
  41569. + * Enclosed most remaining debugging statements in #ifdef's to make it quieter.
  41570. + *
  41571. + * Revision 1.3 1998/04/21 21:28:54 rgb
  41572. + * Rearrange debug switches to change on the fly debug output from user
  41573. + * space. Only kernel changes checked in at this time. radij.c was also
  41574. + * changed to temporarily remove buggy debugging code in rj_delete causing
  41575. + * an OOPS and hence, netlink device open errors.
  41576. + *
  41577. + * Revision 1.2 1998/04/12 22:03:24 rgb
  41578. + * Updated ESP-3DES-HMAC-MD5-96,
  41579. + * ESP-DES-HMAC-MD5-96,
  41580. + * AH-HMAC-MD5-96,
  41581. + * AH-HMAC-SHA1-96 since Henry started freeswan cvs repository
  41582. + * from old standards (RFC182[5-9] to new (as of March 1998) drafts.
  41583. + *
  41584. + * Fixed eroute references in /proc/net/ipsec*.
  41585. + *
  41586. + * Started to patch module unloading memory leaks in ipsec_netlink and
  41587. + * radij tree unloading.
  41588. + *
  41589. + * Revision 1.1 1998/04/09 03:06:12 henry
  41590. + * sources moved up from linux/net/ipsec
  41591. + *
  41592. + * Revision 1.1.1.1 1998/04/08 05:35:04 henry
  41593. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  41594. + *
  41595. + * Revision 0.5 1997/06/03 04:24:48 ji
  41596. + * Added transport mode.
  41597. + * Changed the way routing is done.
  41598. + * Lots of bug fixes.
  41599. + *
  41600. + * Revision 0.4 1997/01/15 01:28:15 ji
  41601. + * No changes.
  41602. + *
  41603. + * Revision 0.3 1996/11/20 14:39:04 ji
  41604. + * Minor cleanups.
  41605. + * Rationalized debugging code.
  41606. + *
  41607. + * Revision 0.2 1996/11/02 00:18:33 ji
  41608. + * First limited release.
  41609. + *
  41610. + * Local Variables:
  41611. + * c-style: linux
  41612. + * End:
  41613. + */
  41614. --- /dev/null Tue Mar 11 13:02:56 2003
  41615. +++ linux/net/ipsec/ipsec_xform.c Mon Feb 9 13:51:03 2004
  41616. @@ -0,0 +1,360 @@
  41617. +/*
  41618. + * Common routines for IPSEC transformations.
  41619. + * Copyright (C) 1996, 1997 John Ioannidis.
  41620. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  41621. + *
  41622. + * This program is free software; you can redistribute it and/or modify it
  41623. + * under the terms of the GNU General Public License as published by the
  41624. + * Free Software Foundation; either version 2 of the License, or (at your
  41625. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  41626. + *
  41627. + * This program is distributed in the hope that it will be useful, but
  41628. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  41629. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  41630. + * for more details.
  41631. + *
  41632. + * RCSID $Id: ipsec_xform.c,v 1.65.2.1 2006/10/06 21:39:26 paul Exp $
  41633. + */
  41634. +
  41635. +#ifndef AUTOCONF_INCLUDED
  41636. +#include <linux/config.h>
  41637. +#endif
  41638. +#include <linux/version.h>
  41639. +#include <linux/kernel.h> /* printk() */
  41640. +
  41641. +#include "freeswan/ipsec_param.h"
  41642. +
  41643. +#ifdef MALLOC_SLAB
  41644. +# include <linux/slab.h> /* kmalloc() */
  41645. +#else /* MALLOC_SLAB */
  41646. +# include <linux/malloc.h> /* kmalloc() */
  41647. +#endif /* MALLOC_SLAB */
  41648. +#include <linux/errno.h> /* error codes */
  41649. +#include <linux/types.h> /* size_t */
  41650. +#include <linux/interrupt.h> /* mark_bh */
  41651. +
  41652. +#include <linux/netdevice.h> /* struct device, and other headers */
  41653. +#include <linux/etherdevice.h> /* eth_type_trans */
  41654. +#include <linux/ip.h> /* struct iphdr */
  41655. +#include <linux/skbuff.h>
  41656. +#include <linux/random.h> /* get_random_bytes() */
  41657. +#include <freeswan.h>
  41658. +#ifdef SPINLOCK
  41659. +# ifdef SPINLOCK_23
  41660. +# include <linux/spinlock.h> /* *lock* */
  41661. +# else /* SPINLOCK_23 */
  41662. +# include <asm/spinlock.h> /* *lock* */
  41663. +# endif /* SPINLOCK_23 */
  41664. +#endif /* SPINLOCK */
  41665. +
  41666. +#include <net/ip.h>
  41667. +
  41668. +#include "freeswan/radij.h"
  41669. +#include "freeswan/ipsec_encap.h"
  41670. +#include "freeswan/ipsec_radij.h"
  41671. +#include "freeswan/ipsec_xform.h"
  41672. +#include "freeswan/ipsec_ipe4.h"
  41673. +#include "freeswan/ipsec_ah.h"
  41674. +#include "freeswan/ipsec_esp.h"
  41675. +
  41676. +#include <pfkeyv2.h>
  41677. +#include <pfkey.h>
  41678. +
  41679. +#ifdef CONFIG_KLIPS_DEBUG
  41680. +int debug_xform = 0;
  41681. +#endif /* CONFIG_KLIPS_DEBUG */
  41682. +
  41683. +#ifdef SPINLOCK
  41684. +spinlock_t tdb_lock = SPIN_LOCK_UNLOCKED;
  41685. +#else /* SPINLOCK */
  41686. +spinlock_t tdb_lock;
  41687. +#endif /* SPINLOCK */
  41688. +
  41689. +/*
  41690. + * $Log: ipsec_xform.c,v $
  41691. + * Revision 1.65.2.1 2006/10/06 21:39:26 paul
  41692. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  41693. + * set. This is defined through autoconf.h which is included through the
  41694. + * linux kernel build macros.
  41695. + *
  41696. + * Revision 1.65 2005/04/29 05:10:22 mcr
  41697. + * removed from extraenous includes to make unit testing easier.
  41698. + *
  41699. + * Revision 1.64 2004/07/10 19:11:18 mcr
  41700. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  41701. + *
  41702. + * Revision 1.63 2003/10/31 02:27:55 mcr
  41703. + * pulled up port-selector patches and sa_id elimination.
  41704. + *
  41705. + * Revision 1.62.30.1 2003/10/29 01:30:41 mcr
  41706. + * elimited "struct sa_id".
  41707. + *
  41708. + * Revision 1.62 2002/05/14 02:34:21 rgb
  41709. + * Delete stale code.
  41710. + *
  41711. + * Revision 1.61 2002/04/24 07:55:32 mcr
  41712. + * #include patches and Makefiles for post-reorg compilation.
  41713. + *
  41714. + * Revision 1.60 2002/04/24 07:36:33 mcr
  41715. + * Moved from ./klips/net/ipsec/ipsec_xform.c,v
  41716. + *
  41717. + * Revision 1.59 2002/03/29 15:01:36 rgb
  41718. + * Delete decommissioned code.
  41719. + *
  41720. + * Revision 1.58 2002/01/29 17:17:57 mcr
  41721. + * moved include of ipsec_param.h to after include of linux/kernel.h
  41722. + * otherwise, it seems that some option that is set in ipsec_param.h
  41723. + * screws up something subtle in the include path to kernel.h, and
  41724. + * it complains on the snprintf() prototype.
  41725. + *
  41726. + * Revision 1.57 2002/01/29 04:00:53 mcr
  41727. + * more excise of kversions.h header.
  41728. + *
  41729. + * Revision 1.56 2001/11/27 05:17:22 mcr
  41730. + * turn off the worst of the per-packet debugging.
  41731. + *
  41732. + * Revision 1.55 2001/11/26 09:23:50 rgb
  41733. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  41734. + *
  41735. + * Revision 1.54 2001/10/18 04:45:21 rgb
  41736. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  41737. + * lib/freeswan.h version macros moved to lib/kversions.h.
  41738. + * Other compiler directive cleanups.
  41739. + *
  41740. + * Revision 1.53 2001/09/08 21:13:34 rgb
  41741. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  41742. + *
  41743. + * Revision 1.52 2001/06/14 19:35:11 rgb
  41744. + * Update copyright date.
  41745. + *
  41746. + * Revision 1.51 2001/05/30 08:14:03 rgb
  41747. + * Removed vestiges of esp-null transforms.
  41748. + *
  41749. + * Revision 1.50 2001/05/03 19:43:18 rgb
  41750. + * Initialise error return variable.
  41751. + * Update SENDERR macro.
  41752. + * Fix sign of error return code for ipsec_tdbcleanup().
  41753. + * Use more appropriate return code for ipsec_tdbwipe().
  41754. + *
  41755. + * Revision 1.49 2001/04/19 18:56:17 rgb
  41756. + * Fixed tdb table locking comments.
  41757. + *
  41758. + * Revision 1.48 2001/02/27 22:24:55 rgb
  41759. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  41760. + * Check for satoa() return codes.
  41761. + *
  41762. + * Revision 1.47 2000/11/06 04:32:08 rgb
  41763. + * Ditched spin_lock_irqsave in favour of spin_lock_bh.
  41764. + *
  41765. + * Revision 1.46 2000/09/20 16:21:57 rgb
  41766. + * Cleaned up ident string alloc/free.
  41767. + *
  41768. + * Revision 1.45 2000/09/08 19:16:51 rgb
  41769. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  41770. + * Removed all references to CONFIG_IPSEC_PFKEYv2.
  41771. + *
  41772. + * Revision 1.44 2000/08/30 05:29:04 rgb
  41773. + * Compiler-define out no longer used tdb_init() in ipsec_xform.c.
  41774. + *
  41775. + * Revision 1.43 2000/08/18 21:30:41 rgb
  41776. + * Purged all tdb_spi, tdb_proto and tdb_dst macros. They are unclear.
  41777. + *
  41778. + * Revision 1.42 2000/08/01 14:51:51 rgb
  41779. + * Removed _all_ remaining traces of DES.
  41780. + *
  41781. + * Revision 1.41 2000/07/28 14:58:31 rgb
  41782. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  41783. + *
  41784. + * Revision 1.40 2000/06/28 05:50:11 rgb
  41785. + * Actually set iv_bits.
  41786. + *
  41787. + * Revision 1.39 2000/05/10 23:11:09 rgb
  41788. + * Added netlink debugging output.
  41789. + * Added a cast to quiet down the ntohl bug.
  41790. + *
  41791. + * Revision 1.38 2000/05/10 19:18:42 rgb
  41792. + * Cast output of ntohl so that the broken prototype doesn't make our
  41793. + * compile noisy.
  41794. + *
  41795. + * Revision 1.37 2000/03/16 14:04:59 rgb
  41796. + * Hardwired CONFIG_IPSEC_PFKEYv2 on.
  41797. + *
  41798. + * Revision 1.36 2000/01/26 10:11:28 rgb
  41799. + * Fixed spacing in error text causing run-in words.
  41800. + *
  41801. + * Revision 1.35 2000/01/21 06:17:16 rgb
  41802. + * Tidied up compiler directive indentation for readability.
  41803. + * Added ictx,octx vars for simplification.(kravietz)
  41804. + * Added macros for HMAC padding magic numbers.(kravietz)
  41805. + * Fixed missing key length reporting bug.
  41806. + * Fixed bug in tdbwipe to return immediately on NULL tdbp passed in.
  41807. + *
  41808. + * Revision 1.34 1999/12/08 00:04:19 rgb
  41809. + * Fixed SA direction overwriting bug for netlink users.
  41810. + *
  41811. + * Revision 1.33 1999/12/01 22:16:44 rgb
  41812. + * Minor formatting changes in ESP MD5 initialisation.
  41813. + *
  41814. + * Revision 1.32 1999/11/25 09:06:36 rgb
  41815. + * Fixed error return messages, should be returning negative numbers.
  41816. + * Implemented SENDERR macro for propagating error codes.
  41817. + * Added debug message and separate error code for algorithms not compiled
  41818. + * in.
  41819. + *
  41820. + * Revision 1.31 1999/11/23 23:06:26 rgb
  41821. + * Sort out pfkey and freeswan headers, putting them in a library path.
  41822. + *
  41823. + * Revision 1.30 1999/11/18 04:09:20 rgb
  41824. + * Replaced all kernel version macros to shorter, readable form.
  41825. + *
  41826. + * Revision 1.29 1999/11/17 15:53:40 rgb
  41827. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  41828. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  41829. + * klips/net/ipsec/Makefile.
  41830. + *
  41831. + * Revision 1.28 1999/10/18 20:04:01 rgb
  41832. + * Clean-out unused cruft.
  41833. + *
  41834. + * Revision 1.27 1999/10/03 19:01:03 rgb
  41835. + * Spinlock support for 2.3.xx and 2.0.xx kernels.
  41836. + *
  41837. + * Revision 1.26 1999/10/01 16:22:24 rgb
  41838. + * Switch from assignment init. to functional init. of spinlocks.
  41839. + *
  41840. + * Revision 1.25 1999/10/01 15:44:54 rgb
  41841. + * Move spinlock header include to 2.1> scope.
  41842. + *
  41843. + * Revision 1.24 1999/10/01 00:03:46 rgb
  41844. + * Added tdb structure locking.
  41845. + * Minor formatting changes.
  41846. + * Add function to initialize tdb hash table.
  41847. + *
  41848. + * Revision 1.23 1999/05/25 22:42:12 rgb
  41849. + * Add deltdbchain() debugging.
  41850. + *
  41851. + * Revision 1.22 1999/05/25 21:24:31 rgb
  41852. + * Add debugging statements to deltdbchain().
  41853. + *
  41854. + * Revision 1.21 1999/05/25 03:51:48 rgb
  41855. + * Refix error return code.
  41856. + *
  41857. + * Revision 1.20 1999/05/25 03:34:07 rgb
  41858. + * Fix error return for flush.
  41859. + *
  41860. + * Revision 1.19 1999/05/09 03:25:37 rgb
  41861. + * Fix bug introduced by 2.2 quick-and-dirty patch.
  41862. + *
  41863. + * Revision 1.18 1999/05/05 22:02:32 rgb
  41864. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  41865. + *
  41866. + * Revision 1.17 1999/04/29 15:20:16 rgb
  41867. + * Change gettdb parameter to a pointer to reduce stack loading and
  41868. + * facilitate parameter sanity checking.
  41869. + * Add sanity checking for null pointer arguments.
  41870. + * Add debugging instrumentation.
  41871. + * Add function deltdbchain() which will take care of unlinking,
  41872. + * zeroing and deleting a chain of tdbs.
  41873. + * Add a parameter to tdbcleanup to be able to delete a class of SAs.
  41874. + * tdbwipe now actually zeroes the tdb as well as any of its pointed
  41875. + * structures.
  41876. + *
  41877. + * Revision 1.16 1999/04/16 15:36:29 rgb
  41878. + * Fix cut-and-paste error causing a memory leak in IPIP TDB freeing.
  41879. + *
  41880. + * Revision 1.15 1999/04/11 00:29:01 henry
  41881. + * GPL boilerplate
  41882. + *
  41883. + * Revision 1.14 1999/04/06 04:54:28 rgb
  41884. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  41885. + * patch shell fixes.
  41886. + *
  41887. + * Revision 1.13 1999/02/19 18:23:01 rgb
  41888. + * Nix debug off compile warning.
  41889. + *
  41890. + * Revision 1.12 1999/02/17 16:52:16 rgb
  41891. + * Consolidate satoa()s for space and speed efficiency.
  41892. + * Convert DEBUG_IPSEC to KLIPS_PRINT
  41893. + * Clean out unused cruft.
  41894. + * Ditch NET_IPIP dependancy.
  41895. + * Loop for 3des key setting.
  41896. + *
  41897. + * Revision 1.11 1999/01/26 02:09:05 rgb
  41898. + * Remove ah/esp/IPIP switching on include files.
  41899. + * Removed CONFIG_IPSEC_ALGO_SWITCH macro.
  41900. + * Removed dead code.
  41901. + * Clean up debug code when switched off.
  41902. + * Remove references to INET_GET_PROTOCOL.
  41903. + * Added code exclusion macros to reduce code from unused algorithms.
  41904. + *
  41905. + * Revision 1.10 1999/01/22 06:28:55 rgb
  41906. + * Cruft clean-out.
  41907. + * Put random IV generation in kernel.
  41908. + * Added algorithm switch code.
  41909. + * Enhanced debugging.
  41910. + * 64-bit clean-up.
  41911. + *
  41912. + * Revision 1.9 1998/11/30 13:22:55 rgb
  41913. + * Rationalised all the klips kernel file headers. They are much shorter
  41914. + * now and won't conflict under RH5.2.
  41915. + *
  41916. + * Revision 1.8 1998/11/25 04:59:06 rgb
  41917. + * Add conditionals for no IPIP tunnel code.
  41918. + * Delete commented out code.
  41919. + *
  41920. + * Revision 1.7 1998/10/31 06:50:41 rgb
  41921. + * Convert xform ASCII names to no spaces.
  41922. + * Fixed up comments in #endif directives.
  41923. + *
  41924. + * Revision 1.6 1998/10/19 14:44:28 rgb
  41925. + * Added inclusion of freeswan.h.
  41926. + * sa_id structure implemented and used: now includes protocol.
  41927. + *
  41928. + * Revision 1.5 1998/10/09 04:32:19 rgb
  41929. + * Added 'klips_debug' prefix to all klips printk debug statements.
  41930. + *
  41931. + * Revision 1.4 1998/08/12 00:11:31 rgb
  41932. + * Added new xform functions to the xform table.
  41933. + * Fixed minor debug output spelling error.
  41934. + *
  41935. + * Revision 1.3 1998/07/09 17:45:31 rgb
  41936. + * Clarify algorithm not available message.
  41937. + *
  41938. + * Revision 1.2 1998/06/23 03:00:51 rgb
  41939. + * Check for presence of IPIP protocol if it is setup one way (we don't
  41940. + * know what has been set up the other way and can only assume it will be
  41941. + * symmetrical with the exception of keys).
  41942. + *
  41943. + * Revision 1.1 1998/06/18 21:27:51 henry
  41944. + * move sources from klips/src to klips/net/ipsec, to keep stupid
  41945. + * kernel-build scripts happier in the presence of symlinks
  41946. + *
  41947. + * Revision 1.3 1998/06/11 05:54:59 rgb
  41948. + * Added transform version string pointer to xformsw initialisations.
  41949. + *
  41950. + * Revision 1.2 1998/04/21 21:28:57 rgb
  41951. + * Rearrange debug switches to change on the fly debug output from user
  41952. + * space. Only kernel changes checked in at this time. radij.c was also
  41953. + * changed to temporarily remove buggy debugging code in rj_delete causing
  41954. + * an OOPS and hence, netlink device open errors.
  41955. + *
  41956. + * Revision 1.1 1998/04/09 03:06:13 henry
  41957. + * sources moved up from linux/net/ipsec
  41958. + *
  41959. + * Revision 1.1.1.1 1998/04/08 05:35:02 henry
  41960. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  41961. + *
  41962. + * Revision 0.5 1997/06/03 04:24:48 ji
  41963. + * Added ESP-3DES-MD5-96
  41964. + *
  41965. + * Revision 0.4 1997/01/15 01:28:15 ji
  41966. + * Added new transforms.
  41967. + *
  41968. + * Revision 0.3 1996/11/20 14:39:04 ji
  41969. + * Minor cleanups.
  41970. + * Rationalized debugging code.
  41971. + *
  41972. + * Revision 0.2 1996/11/02 00:18:33 ji
  41973. + * First limited release.
  41974. + *
  41975. + *
  41976. + */
  41977. --- /dev/null Tue Mar 11 13:02:56 2003
  41978. +++ linux/net/ipsec/ipsec_xmit.c Mon Feb 9 13:51:03 2004
  41979. @@ -0,0 +1,1855 @@
  41980. +/*
  41981. + * IPSEC Transmit code.
  41982. + * Copyright (C) 1996, 1997 John Ioannidis.
  41983. + * Copyright (C) 1998-2003 Richard Guy Briggs.
  41984. + * Copyright (C) 2004-2005 Michael Richardson <mcr@xelerance.com>
  41985. + *
  41986. + * This program is free software; you can redistribute it and/or modify it
  41987. + * under the terms of the GNU General Public License as published by the
  41988. + * Free Software Foundation; either version 2 of the License, or (at your
  41989. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  41990. + *
  41991. + * This program is distributed in the hope that it will be useful, but
  41992. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  41993. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  41994. + * for more details.
  41995. + */
  41996. +
  41997. +char ipsec_xmit_c_version[] = "RCSID $Id: ipsec_xmit.c,v 1.20.2.9 2007/07/06 17:18:43 paul Exp $";
  41998. +
  41999. +#define __NO_VERSION__
  42000. +#include <linux/module.h>
  42001. +#ifndef AUTOCONF_INCLUDED
  42002. +#include <linux/config.h>
  42003. +#endif /* for CONFIG_IP_FORWARD */
  42004. +#include <linux/version.h>
  42005. +#include <linux/kernel.h> /* printk() */
  42006. +
  42007. +#include "openswan/ipsec_param.h"
  42008. +
  42009. +#ifdef MALLOC_SLAB
  42010. +# include <linux/slab.h> /* kmalloc() */
  42011. +#else /* MALLOC_SLAB */
  42012. +# include <linux/malloc.h> /* kmalloc() */
  42013. +#endif /* MALLOC_SLAB */
  42014. +#include <linux/errno.h> /* error codes */
  42015. +#include <linux/types.h> /* size_t */
  42016. +#include <linux/interrupt.h> /* mark_bh */
  42017. +
  42018. +#include <linux/netdevice.h> /* struct device, struct net_device_stats, dev_queue_xmit() and other headers */
  42019. +#include <linux/etherdevice.h> /* eth_type_trans */
  42020. +#include <linux/ip.h> /* struct iphdr */
  42021. +#include <linux/tcp.h> /* struct tcphdr */
  42022. +#include <linux/udp.h> /* struct udphdr */
  42023. +#include <linux/skbuff.h>
  42024. +#include <asm/uaccess.h>
  42025. +#include <asm/checksum.h>
  42026. +#include <openswan.h>
  42027. +#ifdef NET_21
  42028. +# define MSS_HACK_ /* experimental */
  42029. +# include <linux/in6.h>
  42030. +# include <net/dst.h>
  42031. +# define proto_priv cb
  42032. +#endif /* NET_21 */
  42033. +
  42034. +#include <net/icmp.h> /* icmp_send() */
  42035. +#include <net/ip.h>
  42036. +#ifdef NETDEV_23
  42037. +# include <linux/netfilter_ipv4.h>
  42038. +#endif /* NETDEV_23 */
  42039. +
  42040. +#include <linux/if_arp.h>
  42041. +#ifdef MSS_HACK
  42042. +# include <net/tcp.h> /* TCP options */
  42043. +#endif /* MSS_HACK */
  42044. +
  42045. +#include "openswan/radij.h"
  42046. +#include "openswan/ipsec_life.h"
  42047. +#include "openswan/ipsec_xform.h"
  42048. +#include "openswan/ipsec_eroute.h"
  42049. +#include "openswan/ipsec_encap.h"
  42050. +#include "openswan/ipsec_radij.h"
  42051. +#include "openswan/ipsec_xmit.h"
  42052. +#include "openswan/ipsec_sa.h"
  42053. +#include "openswan/ipsec_tunnel.h"
  42054. +#include "openswan/ipsec_ipe4.h"
  42055. +#include "openswan/ipsec_ah.h"
  42056. +#include "openswan/ipsec_esp.h"
  42057. +
  42058. +#ifdef CONFIG_KLIPS_IPCOMP
  42059. +#include "openswan/ipcomp.h"
  42060. +#endif /* CONFIG_KLIPS_IPCOMP */
  42061. +
  42062. +#include <pfkeyv2.h>
  42063. +#include <pfkey.h>
  42064. +
  42065. +#include "openswan/ipsec_proto.h"
  42066. +#include "openswan/ipsec_alg.h"
  42067. +
  42068. +
  42069. +/*
  42070. + * Stupid kernel API differences in APIs. Not only do some
  42071. + * kernels not have ip_select_ident, but some have differing APIs,
  42072. + * and SuSE has one with one parameter, but no way of checking to
  42073. + * see what is really what.
  42074. + */
  42075. +
  42076. +#ifdef SUSE_LINUX_2_4_19_IS_STUPID
  42077. +#define KLIPS_IP_SELECT_IDENT(iph, skb) ip_select_ident(iph)
  42078. +#else
  42079. +
  42080. +/* simplest case, nothing */
  42081. +#if !defined(IP_SELECT_IDENT)
  42082. +#define KLIPS_IP_SELECT_IDENT(iph, skb) do { iph->id = htons(ip_id_count++); } while(0)
  42083. +#endif
  42084. +
  42085. +/* kernels > 2.3.37-ish */
  42086. +#if defined(IP_SELECT_IDENT) && !defined(IP_SELECT_IDENT_NEW)
  42087. +#define KLIPS_IP_SELECT_IDENT(iph, skb) ip_select_ident(iph, skb->dst)
  42088. +#endif
  42089. +
  42090. +/* kernels > 2.4.2 */
  42091. +#if defined(IP_SELECT_IDENT) && defined(IP_SELECT_IDENT_NEW)
  42092. +#define KLIPS_IP_SELECT_IDENT(iph, skb) ip_select_ident(iph, skb->dst, NULL)
  42093. +#endif
  42094. +
  42095. +#endif /* SUSE_LINUX_2_4_19_IS_STUPID */
  42096. +
  42097. +
  42098. +
  42099. +#if defined(CONFIG_KLIPS_AH)
  42100. +static __u32 zeroes[64];
  42101. +#endif
  42102. +
  42103. +#ifdef CONFIG_KLIPS_DEBUG
  42104. +int sysctl_ipsec_debug_verbose = 0;
  42105. +#endif /* CONFIG_KLIPS_DEBUG */
  42106. +
  42107. +int ipsec_xmit_trap_count = 0;
  42108. +int ipsec_xmit_trap_sendcount = 0;
  42109. +
  42110. +int sysctl_ipsec_icmp = 0;
  42111. +int sysctl_ipsec_tos = 0;
  42112. +
  42113. +#ifdef CONFIG_KLIPS_DEBUG
  42114. +#define dmp(_x,_y,_z) if(debug_tunnel) ipsec_dmp_block(_x,_y,_z)
  42115. +#else /* CONFIG_KLIPS_DEBUG */
  42116. +#define dmp(_x, _y, _z)
  42117. +#endif /* CONFIG_KLIPS_DEBUG */
  42118. +
  42119. +
  42120. +#if !defined(SKB_COPY_EXPAND) || defined(KLIPS_UNIT_TESTS)
  42121. +/*
  42122. + * This is mostly skbuff.c:skb_copy().
  42123. + */
  42124. +struct sk_buff *
  42125. +skb_copy_expand(const struct sk_buff *skb, int headroom,
  42126. + int tailroom, int priority)
  42127. +{
  42128. + struct sk_buff *n;
  42129. + unsigned long offset;
  42130. +
  42131. + /*
  42132. + * Do sanity checking
  42133. + */
  42134. + if((headroom < 0) || (tailroom < 0) || ((headroom+tailroom) < 0)) {
  42135. + printk(KERN_WARNING
  42136. + "klips_error:skb_copy_expand: "
  42137. + "Illegal negative head,tailroom %d,%d\n",
  42138. + headroom,
  42139. + tailroom);
  42140. + return NULL;
  42141. + }
  42142. + /*
  42143. + * Allocate the copy buffer
  42144. + */
  42145. +
  42146. +#ifndef NET_21
  42147. + IS_SKB(skb);
  42148. +#endif /* !NET_21 */
  42149. +
  42150. +
  42151. + n=alloc_skb(skb->end - skb->head + headroom + tailroom, priority);
  42152. +
  42153. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  42154. + "klips_debug:skb_copy_expand: "
  42155. + "allocating %d bytes, head=0p%p data=0p%p tail=0p%p end=0p%p end-head=%d tail-data=%d\n",
  42156. + skb->end - skb->head + headroom + tailroom,
  42157. + skb->head,
  42158. + skb->data,
  42159. + skb->tail,
  42160. + skb->end,
  42161. + skb->end - skb->head,
  42162. + skb->tail - skb->data);
  42163. +
  42164. + if(n==NULL)
  42165. + return NULL;
  42166. +
  42167. + /*
  42168. + * Shift between the two data areas in bytes
  42169. + */
  42170. +
  42171. + /* Set the data pointer */
  42172. + skb_reserve(n,skb->data-skb->head+headroom);
  42173. + /* Set the tail pointer and length */
  42174. + if(skb_tailroom(n) < skb->len) {
  42175. + printk(KERN_WARNING "klips_error:skb_copy_expand: "
  42176. + "tried to skb_put %ld, %d available. This should never happen, please report.\n",
  42177. + (unsigned long int)skb->len,
  42178. + skb_tailroom(n));
  42179. + ipsec_kfree_skb(n);
  42180. + return NULL;
  42181. + }
  42182. + skb_put(n,skb->len);
  42183. +
  42184. + offset=n->head + headroom - skb->head;
  42185. +
  42186. + /* Copy the bytes */
  42187. + memcpy(n->head + headroom, skb->head,skb->end-skb->head);
  42188. +#ifdef NET_21
  42189. + n->csum=skb->csum;
  42190. + n->priority=skb->priority;
  42191. + n->dst=dst_clone(skb->dst);
  42192. + if(skb->nh.raw)
  42193. + n->nh.raw=skb->nh.raw+offset;
  42194. +#ifndef NETDEV_23
  42195. + n->is_clone=0;
  42196. +#endif /* NETDEV_23 */
  42197. + atomic_set(&n->users, 1);
  42198. + n->destructor = NULL;
  42199. +#ifdef HAVE_SOCK_SECURITY
  42200. + n->security=skb->security;
  42201. +#endif
  42202. +#else /* NET_21 */
  42203. + n->link3=NULL;
  42204. + n->when=skb->when;
  42205. + if(skb->ip_hdr)
  42206. + n->ip_hdr=(struct iphdr *)(((char *)skb->ip_hdr)+offset);
  42207. + n->saddr=skb->saddr;
  42208. + n->daddr=skb->daddr;
  42209. + n->raddr=skb->raddr;
  42210. + n->seq=skb->seq;
  42211. + n->end_seq=skb->end_seq;
  42212. + n->ack_seq=skb->ack_seq;
  42213. + n->acked=skb->acked;
  42214. + n->free=1;
  42215. + n->arp=skb->arp;
  42216. + n->tries=0;
  42217. + n->lock=0;
  42218. + n->users=0;
  42219. +#endif /* NET_21 */
  42220. + n->protocol=skb->protocol;
  42221. + n->list=NULL;
  42222. + n->sk=NULL;
  42223. + n->dev=skb->dev;
  42224. + if(skb->h.raw)
  42225. + n->h.raw=skb->h.raw+offset;
  42226. + if(skb->mac.raw)
  42227. + n->mac.raw=skb->mac.raw+offset;
  42228. + memcpy(n->proto_priv, skb->proto_priv, sizeof(skb->proto_priv));
  42229. +#ifndef NETDEV_23
  42230. + n->used=skb->used;
  42231. +#endif /* !NETDEV_23 */
  42232. + n->pkt_type=skb->pkt_type;
  42233. + n->stamp=skb->stamp;
  42234. +
  42235. +#ifndef NET_21
  42236. + IS_SKB(n);
  42237. +#endif /* !NET_21 */
  42238. + return n;
  42239. +}
  42240. +#endif /* !SKB_COPY_EXPAND */
  42241. +
  42242. +#ifdef CONFIG_KLIPS_DEBUG
  42243. +void
  42244. +ipsec_print_ip(struct iphdr *ip)
  42245. +{
  42246. + char buf[ADDRTOA_BUF];
  42247. +
  42248. + printk(KERN_INFO "klips_debug: IP:");
  42249. + printk(" ihl:%d", ip->ihl << 2);
  42250. + printk(" ver:%d", ip->version);
  42251. + printk(" tos:%d", ip->tos);
  42252. + printk(" tlen:%d", ntohs(ip->tot_len));
  42253. + printk(" id:%d", ntohs(ip->id));
  42254. + printk(" %s%s%sfrag_off:%d",
  42255. + ip->frag_off & __constant_htons(IP_CE) ? "CE " : "",
  42256. + ip->frag_off & __constant_htons(IP_DF) ? "DF " : "",
  42257. + ip->frag_off & __constant_htons(IP_MF) ? "MF " : "",
  42258. + (ntohs(ip->frag_off) & IP_OFFSET) << 3);
  42259. + printk(" ttl:%d", ip->ttl);
  42260. + printk(" proto:%d", ip->protocol);
  42261. + if(ip->protocol == IPPROTO_UDP)
  42262. + printk(" (UDP)");
  42263. + if(ip->protocol == IPPROTO_TCP)
  42264. + printk(" (TCP)");
  42265. + if(ip->protocol == IPPROTO_ICMP)
  42266. + printk(" (ICMP)");
  42267. + if(ip->protocol == IPPROTO_ESP)
  42268. + printk(" (ESP)");
  42269. + if(ip->protocol == IPPROTO_AH)
  42270. + printk(" (AH)");
  42271. + if(ip->protocol == IPPROTO_COMP)
  42272. + printk(" (COMP)");
  42273. + printk(" chk:%d", ntohs(ip->check));
  42274. + addrtoa(*((struct in_addr*)(&ip->saddr)), 0, buf, sizeof(buf));
  42275. + printk(" saddr:%s", buf);
  42276. + if(ip->protocol == IPPROTO_UDP)
  42277. + printk(":%d",
  42278. + ntohs(((struct udphdr*)((caddr_t)ip + (ip->ihl << 2)))->source));
  42279. + if(ip->protocol == IPPROTO_TCP)
  42280. + printk(":%d",
  42281. + ntohs(((struct tcphdr*)((caddr_t)ip + (ip->ihl << 2)))->source));
  42282. + addrtoa(*((struct in_addr*)(&ip->daddr)), 0, buf, sizeof(buf));
  42283. + printk(" daddr:%s", buf);
  42284. + if(ip->protocol == IPPROTO_UDP)
  42285. + printk(":%d",
  42286. + ntohs(((struct udphdr*)((caddr_t)ip + (ip->ihl << 2)))->dest));
  42287. + if(ip->protocol == IPPROTO_TCP)
  42288. + printk(":%d",
  42289. + ntohs(((struct tcphdr*)((caddr_t)ip + (ip->ihl << 2)))->dest));
  42290. + if(ip->protocol == IPPROTO_ICMP)
  42291. + printk(" type:code=%d:%d",
  42292. + ((struct icmphdr*)((caddr_t)ip + (ip->ihl << 2)))->type,
  42293. + ((struct icmphdr*)((caddr_t)ip + (ip->ihl << 2)))->code);
  42294. + printk("\n");
  42295. +
  42296. + if(sysctl_ipsec_debug_verbose) {
  42297. + __u8 *c;
  42298. + int len = ntohs(ip->tot_len) - ip->ihl*4;
  42299. +
  42300. + c = ((__u8*)ip) + ip->ihl*4;
  42301. + ipsec_dmp_block("ip_print", c, len);
  42302. + }
  42303. +}
  42304. +#endif /* CONFIG_KLIPS_DEBUG */
  42305. +
  42306. +#ifdef MSS_HACK
  42307. +/*
  42308. + * Issues:
  42309. + * 1) Fragments arriving in the tunnel should probably be rejected.
  42310. + * 2) How does this affect syncookies, mss_cache, dst cache ?
  42311. + * 3) Path MTU discovery handling needs to be reviewed. For example,
  42312. + * if we receive an ICMP 'packet too big' message from an intermediate
  42313. + * router specifying it's next hop MTU, our stack may process this and
  42314. + * adjust the MSS without taking our AH/ESP overheads into account.
  42315. + */
  42316. +
  42317. +
  42318. +/*
  42319. + * Recaclulate checksum using differences between changed datum,
  42320. + * borrowed from netfilter.
  42321. + */
  42322. +DEBUG_NO_STATIC u_int16_t
  42323. +ipsec_fast_csum(u_int32_t oldvalinv, u_int32_t newval, u_int16_t oldcheck)
  42324. +{
  42325. + u_int32_t diffs[] = { oldvalinv, newval };
  42326. + return csum_fold(csum_partial((char *)diffs, sizeof(diffs),
  42327. + oldcheck^0xFFFF));
  42328. +}
  42329. +
  42330. +/*
  42331. + * Determine effective MSS.
  42332. + *
  42333. + * Note that we assume that there is always an MSS option for our own
  42334. + * SYN segments, which is mentioned in tcp_syn_build_options(), kernel 2.2.x.
  42335. + * This could change, and we should probably parse TCP options instead.
  42336. + *
  42337. + */
  42338. +DEBUG_NO_STATIC u_int8_t
  42339. +ipsec_adjust_mss(struct sk_buff *skb, struct tcphdr *tcph, u_int16_t mtu)
  42340. +{
  42341. + u_int16_t oldmss, newmss;
  42342. + u_int32_t *mssp;
  42343. + struct sock *sk = skb->sk;
  42344. +
  42345. + newmss = tcp_sync_mss(sk, mtu);
  42346. + printk(KERN_INFO "klips: setting mss to %u\n", newmss);
  42347. + mssp = (u_int32_t *)tcph + sizeof(struct tcphdr) / sizeof(u_int32_t);
  42348. + oldmss = ntohl(*mssp) & 0x0000FFFF;
  42349. + *mssp = htonl((TCPOPT_MSS << 24) | (TCPOLEN_MSS << 16) | newmss);
  42350. + tcph->check = ipsec_fast_csum(htons(~oldmss),
  42351. + htons(newmss), tcph->check);
  42352. + return 1;
  42353. +}
  42354. +#endif /* MSS_HACK */
  42355. +
  42356. +/*
  42357. + * Sanity checks
  42358. + */
  42359. +enum ipsec_xmit_value
  42360. +ipsec_xmit_sanity_check_dev(struct ipsec_xmit_state *ixs)
  42361. +{
  42362. +
  42363. + if (ixs->dev == NULL) {
  42364. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  42365. + "klips_error:ipsec_xmit_sanity_check_dev: "
  42366. + "No device associated with skb!\n" );
  42367. + return IPSEC_XMIT_NODEV;
  42368. + }
  42369. +
  42370. + ixs->prv = ixs->dev->priv;
  42371. + if (ixs->prv == NULL) {
  42372. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  42373. + "klips_error:ipsec_xmit_sanity_check_dev: "
  42374. + "Device has no private structure!\n" );
  42375. + return IPSEC_XMIT_NOPRIVDEV;
  42376. + }
  42377. +
  42378. + ixs->physdev = ixs->prv->dev;
  42379. + if (ixs->physdev == NULL) {
  42380. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  42381. + "klips_error:ipsec_xmit_sanity_check_dev: "
  42382. + "Device is not attached to physical device!\n" );
  42383. + return IPSEC_XMIT_NOPHYSDEV;
  42384. + }
  42385. +
  42386. + ixs->physmtu = ixs->physdev->mtu;
  42387. + ixs->cur_mtu = ixs->physdev->mtu;
  42388. + ixs->stats = (struct net_device_stats *) &(ixs->prv->mystats);
  42389. +
  42390. + return IPSEC_XMIT_OK;
  42391. +}
  42392. +
  42393. +enum ipsec_xmit_value
  42394. +ipsec_xmit_sanity_check_skb(struct ipsec_xmit_state *ixs)
  42395. +{
  42396. + /*
  42397. + * Return if there is nothing to do. (Does this ever happen?) XXX
  42398. + */
  42399. + if (ixs->skb == NULL) {
  42400. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  42401. + "klips_error:ipsec_xmit_sanity_check_skb: "
  42402. + "Nothing to do!\n" );
  42403. + return IPSEC_XMIT_NOSKB;
  42404. + }
  42405. +
  42406. + /* if skb was cloned (most likely due to a packet sniffer such as
  42407. + tcpdump being momentarily attached to the interface), make
  42408. + a copy of our own to modify */
  42409. + if(skb_cloned(ixs->skb)) {
  42410. + if
  42411. +#ifdef SKB_COW_NEW
  42412. + (skb_cow(ixs->skb, skb_headroom(ixs->skb)) != 0)
  42413. +#else /* SKB_COW_NEW */
  42414. + ((ixs->skb = skb_cow(ixs->skb, skb_headroom(ixs->skb))) == NULL)
  42415. +#endif /* SKB_COW_NEW */
  42416. + {
  42417. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  42418. + "klips_error:ipsec_xmit_sanity_check_skb: "
  42419. + "skb_cow failed to allocate buffer, dropping.\n" );
  42420. + ixs->stats->tx_dropped++;
  42421. + return IPSEC_XMIT_ERRSKBALLOC;
  42422. + }
  42423. + }
  42424. +
  42425. + ixs->iph = ixs->skb->nh.iph;
  42426. +
  42427. + /* sanity check for IP version as we can't handle IPv6 right now */
  42428. + if (ixs->iph->version != 4) {
  42429. + KLIPS_PRINT(debug_tunnel,
  42430. + "klips_debug:ipsec_xmit_sanity_check_skb: "
  42431. + "found IP Version %d but cannot process other IP versions than v4.\n",
  42432. + ixs->iph->version); /* XXX */
  42433. + ixs->stats->tx_dropped++;
  42434. + return IPSEC_XMIT_NOIPV6;
  42435. + }
  42436. +
  42437. +#if IPSEC_DISALLOW_IPOPTIONS
  42438. + if ((ixs->iph->ihl << 2) != sizeof (struct iphdr)) {
  42439. + KLIPS_PRINT(debug_tunnel,
  42440. + "klips_debug:ipsec_xmit_sanity_check_skb: "
  42441. + "cannot process IP header options yet. May be mal-formed packet.\n"); /* XXX */
  42442. + ixs->stats->tx_dropped++;
  42443. + return IPSEC_XMIT_NOIPOPTIONS;
  42444. + }
  42445. +#endif /* IPSEC_DISALLOW_IPOPTIONS */
  42446. +
  42447. +#ifndef NET_21
  42448. + if (ixs->iph->ttl <= 0) {
  42449. + /* Tell the sender its packet died... */
  42450. + ICMP_SEND(ixs->skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, 0, ixs->physdev);
  42451. +
  42452. + KLIPS_PRINT(debug_tunnel, "klips_debug:ipsec_xmit_sanity_check_skb: "
  42453. + "TTL=0, too many hops!\n");
  42454. + ixs->stats->tx_dropped++;
  42455. + return IPSEC_XMIT_TTLEXPIRED;
  42456. + }
  42457. +#endif /* !NET_21 */
  42458. +
  42459. + return IPSEC_XMIT_OK;
  42460. +}
  42461. +
  42462. +enum ipsec_xmit_value
  42463. +ipsec_xmit_encap_once(struct ipsec_xmit_state *ixs)
  42464. +{
  42465. +#ifdef CONFIG_KLIPS_ESP
  42466. + struct esphdr *espp;
  42467. + unsigned char *idat, *pad;
  42468. + int authlen = 0, padlen = 0, i;
  42469. +#endif /* !CONFIG_KLIPS_ESP */
  42470. +#ifdef CONFIG_KLIPS_AH
  42471. + struct iphdr ipo;
  42472. + struct ahhdr *ahp;
  42473. +#endif /* CONFIG_KLIPS_AH */
  42474. +#if defined(CONFIG_KLIPS_AUTH_HMAC_MD5) || defined(CONFIG_KLIPS_AUTH_HMAC_SHA1)
  42475. + union {
  42476. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  42477. + MD5_CTX md5;
  42478. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  42479. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  42480. + SHA1_CTX sha1;
  42481. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  42482. + } tctx;
  42483. + __u8 hash[AH_AMAX];
  42484. +#endif /* defined(CONFIG_KLIPS_AUTH_HMAC_MD5) || defined(CONFIG_KLIPS_AUTH_HMACn_SHA1) */
  42485. + int headroom = 0, tailroom = 0, ilen = 0, len = 0;
  42486. + unsigned char *dat;
  42487. + int blocksize = 8; /* XXX: should be inside ixs --jjo */
  42488. + struct ipsec_alg_enc *ixt_e = NULL;
  42489. + struct ipsec_alg_auth *ixt_a = NULL;
  42490. +
  42491. + ixs->iphlen = ixs->iph->ihl << 2;
  42492. + ixs->pyldsz = ntohs(ixs->iph->tot_len) - ixs->iphlen;
  42493. + ixs->sa_len = satot(&ixs->ipsp->ips_said, 0, ixs->sa_txt, SATOT_BUF);
  42494. + KLIPS_PRINT(debug_tunnel & DB_TN_OXFS,
  42495. + "klips_debug:ipsec_xmit_encap_once: "
  42496. + "calling output for <%s%s%s>, SA:%s\n",
  42497. + IPS_XFORM_NAME(ixs->ipsp),
  42498. + ixs->sa_len ? ixs->sa_txt : " (error)");
  42499. +
  42500. + switch(ixs->ipsp->ips_said.proto) {
  42501. +#ifdef CONFIG_KLIPS_AH
  42502. + case IPPROTO_AH:
  42503. + headroom += sizeof(struct ahhdr);
  42504. + break;
  42505. +#endif /* CONFIG_KLIPS_AH */
  42506. +
  42507. +#ifdef CONFIG_KLIPS_ESP
  42508. + case IPPROTO_ESP:
  42509. + ixt_e=ixs->ipsp->ips_alg_enc;
  42510. + if (ixt_e) {
  42511. + blocksize = ixt_e->ixt_common.ixt_blocksize;
  42512. + headroom += ESP_HEADER_LEN + ixt_e->ixt_common.ixt_support.ias_ivlen/8;
  42513. + } else {
  42514. + ixs->stats->tx_errors++;
  42515. + return IPSEC_XMIT_ESP_BADALG;
  42516. + }
  42517. +
  42518. + ixt_a=ixs->ipsp->ips_alg_auth;
  42519. + if (ixt_a) {
  42520. + tailroom += AHHMAC_HASHLEN;
  42521. + authlen = AHHMAC_HASHLEN;
  42522. + } else
  42523. + switch(ixs->ipsp->ips_authalg) {
  42524. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  42525. + case AH_MD5:
  42526. + authlen = AHHMAC_HASHLEN;
  42527. + break;
  42528. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  42529. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  42530. + case AH_SHA:
  42531. + authlen = AHHMAC_HASHLEN;
  42532. + break;
  42533. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  42534. + case AH_NONE:
  42535. + break;
  42536. + default:
  42537. + ixs->stats->tx_errors++;
  42538. + return IPSEC_XMIT_ESP_BADALG;
  42539. + }
  42540. + tailroom += blocksize != 1 ?
  42541. + ((blocksize - ((ixs->pyldsz + 2) % blocksize)) % blocksize) + 2 :
  42542. + ((4 - ((ixs->pyldsz + 2) % 4)) % 4) + 2;
  42543. + tailroom += authlen;
  42544. + break;
  42545. +#endif /* CONFIG_KLIPS_ESP */
  42546. +
  42547. +#ifdef CONFIG_KLIPS_IPIP
  42548. + case IPPROTO_IPIP:
  42549. + headroom += sizeof(struct iphdr);
  42550. + ixs->iphlen = sizeof(struct iphdr);
  42551. + break;
  42552. +#endif /* !CONFIG_KLIPS_IPIP */
  42553. +
  42554. +#ifdef CONFIG_KLIPS_IPCOMP
  42555. + case IPPROTO_COMP:
  42556. + break;
  42557. +#endif /* CONFIG_KLIPS_IPCOMP */
  42558. +
  42559. + default:
  42560. + ixs->stats->tx_errors++;
  42561. + return IPSEC_XMIT_BADPROTO;
  42562. + }
  42563. +
  42564. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  42565. + "klips_debug:ipsec_xmit_encap_once: "
  42566. + "pushing %d bytes, putting %d, proto %d.\n",
  42567. + headroom, tailroom, ixs->ipsp->ips_said.proto);
  42568. + if(skb_headroom(ixs->skb) < headroom) {
  42569. + printk(KERN_WARNING
  42570. + "klips_error:ipsec_xmit_encap_once: "
  42571. + "tried to skb_push headroom=%d, %d available. This should never happen, please report.\n",
  42572. + headroom, skb_headroom(ixs->skb));
  42573. + ixs->stats->tx_errors++;
  42574. + return IPSEC_XMIT_ESP_PUSHPULLERR;
  42575. + }
  42576. +
  42577. + dat = skb_push(ixs->skb, headroom);
  42578. + ilen = ixs->skb->len - tailroom;
  42579. + if(skb_tailroom(ixs->skb) < tailroom) {
  42580. + printk(KERN_WARNING
  42581. + "klips_error:ipsec_xmit_encap_once: "
  42582. + "tried to skb_put %d, %d available. This should never happen, please report.\n",
  42583. + tailroom, skb_tailroom(ixs->skb));
  42584. + ixs->stats->tx_errors++;
  42585. + return IPSEC_XMIT_ESP_PUSHPULLERR;
  42586. + }
  42587. + skb_put(ixs->skb, tailroom);
  42588. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  42589. + "klips_debug:ipsec_xmit_encap_once: "
  42590. + "head,tailroom: %d,%d before xform.\n",
  42591. + skb_headroom(ixs->skb), skb_tailroom(ixs->skb));
  42592. + len = ixs->skb->len;
  42593. + if(len > 0xfff0) {
  42594. + printk(KERN_WARNING "klips_error:ipsec_xmit_encap_once: "
  42595. + "tot_len (%d) > 65520. This should never happen, please report.\n",
  42596. + len);
  42597. + ixs->stats->tx_errors++;
  42598. + return IPSEC_XMIT_BADLEN;
  42599. + }
  42600. + memmove((void *)dat, (void *)(dat + headroom), ixs->iphlen);
  42601. + ixs->iph = (struct iphdr *)dat;
  42602. + ixs->iph->tot_len = htons(ixs->skb->len);
  42603. +
  42604. + switch(ixs->ipsp->ips_said.proto) {
  42605. +#ifdef CONFIG_KLIPS_ESP
  42606. + case IPPROTO_ESP:
  42607. + espp = (struct esphdr *)(dat + ixs->iphlen);
  42608. + espp->esp_spi = ixs->ipsp->ips_said.spi;
  42609. + espp->esp_rpl = htonl(++(ixs->ipsp->ips_replaywin_lastseq));
  42610. +
  42611. + if (!ixt_e) {
  42612. + ixs->stats->tx_errors++;
  42613. + return IPSEC_XMIT_ESP_BADALG;
  42614. + }
  42615. +
  42616. + idat = dat + ixs->iphlen + headroom;
  42617. + ilen = len - (ixs->iphlen + headroom + authlen);
  42618. +
  42619. + /* Self-describing padding */
  42620. + pad = &dat[len - tailroom];
  42621. + padlen = tailroom - 2 - authlen;
  42622. + for (i = 0; i < padlen; i++) {
  42623. + pad[i] = i + 1;
  42624. + }
  42625. + dat[len - authlen - 2] = padlen;
  42626. +
  42627. + dat[len - authlen - 1] = ixs->iph->protocol;
  42628. + ixs->iph->protocol = IPPROTO_ESP;
  42629. +#ifdef CONFIG_KLIPS_DEBUG
  42630. + if(debug_tunnel & DB_TN_ENCAP) {
  42631. + dmp("pre-encrypt", dat, len);
  42632. + }
  42633. +#endif
  42634. +
  42635. + /*
  42636. + * Do all operations here:
  42637. + * copy IV->ESP, encrypt, update ips IV
  42638. + *
  42639. + */
  42640. + {
  42641. + int ret;
  42642. + memcpy(espp->esp_iv,
  42643. + ixs->ipsp->ips_iv,
  42644. + ixs->ipsp->ips_iv_size);
  42645. + ret=ipsec_alg_esp_encrypt(ixs->ipsp,
  42646. + idat, ilen, espp->esp_iv,
  42647. + IPSEC_ALG_ENCRYPT);
  42648. +
  42649. + prng_bytes(&ipsec_prng,
  42650. + (char *)ixs->ipsp->ips_iv,
  42651. + ixs->ipsp->ips_iv_size);
  42652. + }
  42653. +
  42654. + if (ixt_a) {
  42655. + ipsec_alg_sa_esp_hash(ixs->ipsp,
  42656. + (caddr_t)espp, len - ixs->iphlen - authlen,
  42657. + &(dat[len - authlen]), authlen);
  42658. +
  42659. + } else
  42660. + switch(ixs->ipsp->ips_authalg) {
  42661. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  42662. + case AH_MD5:
  42663. + dmp("espp", (char*)espp, len - ixs->iphlen - authlen);
  42664. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  42665. + dmp("ictx", (char*)&tctx.md5, sizeof(tctx.md5));
  42666. + osMD5Update(&tctx.md5, (caddr_t)espp, len - ixs->iphlen - authlen);
  42667. + dmp("ictx+dat", (char*)&tctx.md5, sizeof(tctx.md5));
  42668. + osMD5Final(hash, &tctx.md5);
  42669. + dmp("ictx hash", (char*)&hash, sizeof(hash));
  42670. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->octx;
  42671. + dmp("octx", (char*)&tctx.md5, sizeof(tctx.md5));
  42672. + osMD5Update(&tctx.md5, hash, AHMD596_ALEN);
  42673. + dmp("octx+hash", (char*)&tctx.md5, sizeof(tctx.md5));
  42674. + osMD5Final(hash, &tctx.md5);
  42675. + dmp("octx hash", (char*)&hash, sizeof(hash));
  42676. + memcpy(&(dat[len - authlen]), hash, authlen);
  42677. +
  42678. + /* paranoid */
  42679. + memset((caddr_t)&tctx.md5, 0, sizeof(tctx.md5));
  42680. + memset((caddr_t)hash, 0, sizeof(*hash));
  42681. + break;
  42682. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  42683. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  42684. + case AH_SHA:
  42685. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  42686. + SHA1Update(&tctx.sha1, (caddr_t)espp, len - ixs->iphlen - authlen);
  42687. + SHA1Final(hash, &tctx.sha1);
  42688. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->octx;
  42689. + SHA1Update(&tctx.sha1, hash, AHSHA196_ALEN);
  42690. + SHA1Final(hash, &tctx.sha1);
  42691. + memcpy(&(dat[len - authlen]), hash, authlen);
  42692. +
  42693. + /* paranoid */
  42694. + memset((caddr_t)&tctx.sha1, 0, sizeof(tctx.sha1));
  42695. + memset((caddr_t)hash, 0, sizeof(*hash));
  42696. + break;
  42697. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  42698. + case AH_NONE:
  42699. + break;
  42700. + default:
  42701. + ixs->stats->tx_errors++;
  42702. + return IPSEC_XMIT_AH_BADALG;
  42703. + }
  42704. +#ifdef NET_21
  42705. + ixs->skb->h.raw = (unsigned char*)espp;
  42706. +#endif /* NET_21 */
  42707. + break;
  42708. +#endif /* !CONFIG_KLIPS_ESP */
  42709. +#ifdef CONFIG_KLIPS_AH
  42710. + case IPPROTO_AH:
  42711. + ahp = (struct ahhdr *)(dat + ixs->iphlen);
  42712. + ahp->ah_spi = ixs->ipsp->ips_said.spi;
  42713. + ahp->ah_rpl = htonl(++(ixs->ipsp->ips_replaywin_lastseq));
  42714. + ahp->ah_rv = 0;
  42715. + ahp->ah_nh = ixs->iph->protocol;
  42716. + ahp->ah_hl = (headroom >> 2) - sizeof(__u64)/sizeof(__u32);
  42717. + ixs->iph->protocol = IPPROTO_AH;
  42718. + dmp("ahp", (char*)ahp, sizeof(*ahp));
  42719. +
  42720. + ipo = *ixs->iph;
  42721. + ipo.tos = 0;
  42722. + ipo.frag_off = 0;
  42723. + ipo.ttl = 0;
  42724. + ipo.check = 0;
  42725. + dmp("ipo", (char*)&ipo, sizeof(ipo));
  42726. +
  42727. + switch(ixs->ipsp->ips_authalg) {
  42728. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  42729. + case AH_MD5:
  42730. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  42731. + dmp("ictx", (char*)&tctx.md5, sizeof(tctx.md5));
  42732. + osMD5Update(&tctx.md5, (unsigned char *)&ipo, sizeof (struct iphdr));
  42733. + dmp("ictx+ipo", (char*)&tctx.md5, sizeof(tctx.md5));
  42734. + osMD5Update(&tctx.md5, (unsigned char *)ahp, headroom - sizeof(ahp->ah_data));
  42735. + dmp("ictx+ahp", (char*)&tctx.md5, sizeof(tctx.md5));
  42736. + osMD5Update(&tctx.md5, (unsigned char *)zeroes, AHHMAC_HASHLEN);
  42737. + dmp("ictx+zeroes", (char*)&tctx.md5, sizeof(tctx.md5));
  42738. + osMD5Update(&tctx.md5, dat + ixs->iphlen + headroom, len - ixs->iphlen - headroom);
  42739. + dmp("ictx+dat", (char*)&tctx.md5, sizeof(tctx.md5));
  42740. + osMD5Final(hash, &tctx.md5);
  42741. + dmp("ictx hash", (char*)&hash, sizeof(hash));
  42742. + tctx.md5 = ((struct md5_ctx*)(ixs->ipsp->ips_key_a))->octx;
  42743. + dmp("octx", (char*)&tctx.md5, sizeof(tctx.md5));
  42744. + osMD5Update(&tctx.md5, hash, AHMD596_ALEN);
  42745. + dmp("octx+hash", (char*)&tctx.md5, sizeof(tctx.md5));
  42746. + osMD5Final(hash, &tctx.md5);
  42747. + dmp("octx hash", (char*)&hash, sizeof(hash));
  42748. +
  42749. + memcpy(ahp->ah_data, hash, AHHMAC_HASHLEN);
  42750. +
  42751. + /* paranoid */
  42752. + memset((caddr_t)&tctx.md5, 0, sizeof(tctx.md5));
  42753. + memset((caddr_t)hash, 0, sizeof(*hash));
  42754. + break;
  42755. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  42756. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  42757. + case AH_SHA:
  42758. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->ictx;
  42759. + SHA1Update(&tctx.sha1, (unsigned char *)&ipo, sizeof (struct iphdr));
  42760. + SHA1Update(&tctx.sha1, (unsigned char *)ahp, headroom - sizeof(ahp->ah_data));
  42761. + SHA1Update(&tctx.sha1, (unsigned char *)zeroes, AHHMAC_HASHLEN);
  42762. + SHA1Update(&tctx.sha1, dat + ixs->iphlen + headroom, len - ixs->iphlen - headroom);
  42763. + SHA1Final(hash, &tctx.sha1);
  42764. + tctx.sha1 = ((struct sha1_ctx*)(ixs->ipsp->ips_key_a))->octx;
  42765. + SHA1Update(&tctx.sha1, hash, AHSHA196_ALEN);
  42766. + SHA1Final(hash, &tctx.sha1);
  42767. +
  42768. + memcpy(ahp->ah_data, hash, AHHMAC_HASHLEN);
  42769. +
  42770. + /* paranoid */
  42771. + memset((caddr_t)&tctx.sha1, 0, sizeof(tctx.sha1));
  42772. + memset((caddr_t)hash, 0, sizeof(*hash));
  42773. + break;
  42774. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  42775. + default:
  42776. + ixs->stats->tx_errors++;
  42777. + return IPSEC_XMIT_AH_BADALG;
  42778. + }
  42779. +#ifdef NET_21
  42780. + ixs->skb->h.raw = (unsigned char*)ahp;
  42781. +#endif /* NET_21 */
  42782. + break;
  42783. +#endif /* CONFIG_KLIPS_AH */
  42784. +#ifdef CONFIG_KLIPS_IPIP
  42785. + case IPPROTO_IPIP:
  42786. + ixs->iph->version = 4;
  42787. + switch(sysctl_ipsec_tos) {
  42788. + case 0:
  42789. +#ifdef NET_21
  42790. + ixs->iph->tos = ixs->skb->nh.iph->tos;
  42791. +#else /* NET_21 */
  42792. + ixs->iph->tos = ixs->skb->ip_hdr->tos;
  42793. +#endif /* NET_21 */
  42794. + break;
  42795. + case 1:
  42796. + ixs->iph->tos = 0;
  42797. + break;
  42798. + default:
  42799. + break;
  42800. + }
  42801. + ixs->iph->ttl = SYSCTL_IPSEC_DEFAULT_TTL;
  42802. + ixs->iph->frag_off = 0;
  42803. + ixs->iph->saddr = ((struct sockaddr_in*)(ixs->ipsp->ips_addr_s))->sin_addr.s_addr;
  42804. + ixs->iph->daddr = ((struct sockaddr_in*)(ixs->ipsp->ips_addr_d))->sin_addr.s_addr;
  42805. + ixs->iph->protocol = IPPROTO_IPIP;
  42806. + ixs->iph->ihl = sizeof(struct iphdr) >> 2;
  42807. +
  42808. + KLIPS_IP_SELECT_IDENT(ixs->iph, ixs->skb);
  42809. +
  42810. + ixs->newdst = (__u32)ixs->iph->daddr;
  42811. + ixs->newsrc = (__u32)ixs->iph->saddr;
  42812. +
  42813. +#ifdef NET_21
  42814. + ixs->skb->h.ipiph = ixs->skb->nh.iph;
  42815. +#endif /* NET_21 */
  42816. + break;
  42817. +#endif /* !CONFIG_KLIPS_IPIP */
  42818. +#ifdef CONFIG_KLIPS_IPCOMP
  42819. + case IPPROTO_COMP:
  42820. + {
  42821. + unsigned int flags = 0;
  42822. +#ifdef CONFIG_KLIPS_DEBUG
  42823. + unsigned int old_tot_len = ntohs(ixs->iph->tot_len);
  42824. +#endif /* CONFIG_KLIPS_DEBUG */
  42825. + ixs->ipsp->ips_comp_ratio_dbytes += ntohs(ixs->iph->tot_len);
  42826. +
  42827. + ixs->skb = skb_compress(ixs->skb, ixs->ipsp, &flags);
  42828. +
  42829. +#ifdef NET_21
  42830. + ixs->iph = ixs->skb->nh.iph;
  42831. +#else /* NET_21 */
  42832. + ixs->iph = ixs->skb->ip_hdr;
  42833. +#endif /* NET_21 */
  42834. +
  42835. + ixs->ipsp->ips_comp_ratio_cbytes += ntohs(ixs->iph->tot_len);
  42836. +
  42837. +#ifdef CONFIG_KLIPS_DEBUG
  42838. + if (debug_tunnel & DB_TN_CROUT)
  42839. + {
  42840. + if (old_tot_len > ntohs(ixs->iph->tot_len))
  42841. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  42842. + "klips_debug:ipsec_xmit_encap_once: "
  42843. + "packet shrunk from %d to %d bytes after compression, cpi=%04x (should be from spi=%08x, spi&0xffff=%04x.\n",
  42844. + old_tot_len, ntohs(ixs->iph->tot_len),
  42845. + ntohs(((struct ipcomphdr*)(((char*)ixs->iph) + ((ixs->iph->ihl) << 2)))->ipcomp_cpi),
  42846. + ntohl(ixs->ipsp->ips_said.spi),
  42847. + (__u16)(ntohl(ixs->ipsp->ips_said.spi) & 0x0000ffff));
  42848. + else
  42849. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  42850. + "klips_debug:ipsec_xmit_encap_once: "
  42851. + "packet did not compress (flags = %d).\n",
  42852. + flags);
  42853. + }
  42854. +#endif /* CONFIG_KLIPS_DEBUG */
  42855. + }
  42856. + break;
  42857. +#endif /* CONFIG_KLIPS_IPCOMP */
  42858. + default:
  42859. + ixs->stats->tx_errors++;
  42860. + return IPSEC_XMIT_BADPROTO;
  42861. + }
  42862. +
  42863. +#ifdef NET_21
  42864. + ixs->skb->nh.raw = ixs->skb->data;
  42865. +#else /* NET_21 */
  42866. + ixs->skb->ip_hdr = ixs->skb->h.iph = (struct iphdr *) ixs->skb->data;
  42867. +#endif /* NET_21 */
  42868. + ixs->iph->check = 0;
  42869. + ixs->iph->check = ip_fast_csum((unsigned char *)ixs->iph, ixs->iph->ihl);
  42870. +
  42871. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  42872. + "klips_debug:ipsec_xmit_encap_once: "
  42873. + "after <%s%s%s>, SA:%s:\n",
  42874. + IPS_XFORM_NAME(ixs->ipsp),
  42875. + ixs->sa_len ? ixs->sa_txt : " (error)");
  42876. + KLIPS_IP_PRINT(debug_tunnel & DB_TN_XMIT, ixs->iph);
  42877. +
  42878. + ixs->ipsp->ips_life.ipl_bytes.ipl_count += len;
  42879. + ixs->ipsp->ips_life.ipl_bytes.ipl_last = len;
  42880. +
  42881. + if(!ixs->ipsp->ips_life.ipl_usetime.ipl_count) {
  42882. + ixs->ipsp->ips_life.ipl_usetime.ipl_count = jiffies / HZ;
  42883. + }
  42884. + ixs->ipsp->ips_life.ipl_usetime.ipl_last = jiffies / HZ;
  42885. + ixs->ipsp->ips_life.ipl_packets.ipl_count++;
  42886. +
  42887. + ixs->ipsp = ixs->ipsp->ips_onext;
  42888. +
  42889. + return IPSEC_XMIT_OK;
  42890. +}
  42891. +
  42892. +/*
  42893. + * If the IP packet (iph) is a carrying TCP/UDP, then set the encaps
  42894. + * source and destination ports to those from the TCP/UDP header.
  42895. + */
  42896. +void ipsec_extract_ports(struct iphdr * iph, struct sockaddr_encap * er)
  42897. +{
  42898. + struct udphdr *udp;
  42899. +
  42900. + switch (iph->protocol) {
  42901. + case IPPROTO_UDP:
  42902. + case IPPROTO_TCP:
  42903. + /*
  42904. + * The ports are at the same offsets in a TCP and UDP
  42905. + * header so hack it ...
  42906. + */
  42907. + udp = (struct udphdr*)(((char*)iph)+(iph->ihl<<2));
  42908. + er->sen_sport = udp->source;
  42909. + er->sen_dport = udp->dest;
  42910. + break;
  42911. + default:
  42912. + er->sen_sport = 0;
  42913. + er->sen_dport = 0;
  42914. + break;
  42915. + }
  42916. +}
  42917. +
  42918. +/*
  42919. + * A TRAP eroute is installed and we want to replace it with a HOLD
  42920. + * eroute.
  42921. + */
  42922. +static int create_hold_eroute(struct eroute *origtrap,
  42923. + struct sk_buff * skb, struct iphdr * iph,
  42924. + uint32_t eroute_pid)
  42925. +{
  42926. + struct eroute hold_eroute;
  42927. + ip_said hold_said;
  42928. + struct sk_buff *first, *last;
  42929. + int error;
  42930. +
  42931. + first = last = NULL;
  42932. + memset((caddr_t)&hold_eroute, 0, sizeof(hold_eroute));
  42933. + memset((caddr_t)&hold_said, 0, sizeof(hold_said));
  42934. +
  42935. + hold_said.proto = IPPROTO_INT;
  42936. + hold_said.spi = htonl(SPI_HOLD);
  42937. + hold_said.dst.u.v4.sin_addr.s_addr = INADDR_ANY;
  42938. +
  42939. + hold_eroute.er_eaddr.sen_len = sizeof(struct sockaddr_encap);
  42940. + hold_eroute.er_emask.sen_len = sizeof(struct sockaddr_encap);
  42941. + hold_eroute.er_eaddr.sen_family = AF_ENCAP;
  42942. + hold_eroute.er_emask.sen_family = AF_ENCAP;
  42943. + hold_eroute.er_eaddr.sen_type = SENT_IP4;
  42944. + hold_eroute.er_emask.sen_type = 255;
  42945. +
  42946. + hold_eroute.er_eaddr.sen_ip_src.s_addr = iph->saddr;
  42947. + hold_eroute.er_eaddr.sen_ip_dst.s_addr = iph->daddr;
  42948. + hold_eroute.er_emask.sen_ip_src.s_addr = INADDR_BROADCAST;
  42949. + hold_eroute.er_emask.sen_ip_dst.s_addr = INADDR_BROADCAST;
  42950. + hold_eroute.er_emask.sen_sport = 0;
  42951. + hold_eroute.er_emask.sen_dport = 0;
  42952. + hold_eroute.er_pid = eroute_pid;
  42953. + hold_eroute.er_count = 0;
  42954. + hold_eroute.er_lasttime = jiffies/HZ;
  42955. +
  42956. + /*
  42957. + * if it wasn't captured by a wildcard, then don't record it as
  42958. + * a wildcard.
  42959. + */
  42960. + if(origtrap->er_eaddr.sen_proto != 0) {
  42961. + hold_eroute.er_eaddr.sen_proto = iph->protocol;
  42962. +
  42963. + if((iph->protocol == IPPROTO_TCP ||
  42964. + iph->protocol == IPPROTO_UDP) &&
  42965. + (origtrap->er_eaddr.sen_sport != 0 ||
  42966. + origtrap->er_eaddr.sen_dport != 0)) {
  42967. +
  42968. + if(origtrap->er_eaddr.sen_sport != 0)
  42969. + hold_eroute.er_emask.sen_sport = ~0;
  42970. +
  42971. + if(origtrap->er_eaddr.sen_dport != 0)
  42972. + hold_eroute.er_emask.sen_dport = ~0;
  42973. +
  42974. + ipsec_extract_ports(iph, &hold_eroute.er_eaddr);
  42975. + }
  42976. + }
  42977. +
  42978. +#ifdef CONFIG_KLIPS_DEBUG
  42979. + if (debug_pfkey) {
  42980. + char buf1[64], buf2[64];
  42981. + subnettoa(hold_eroute.er_eaddr.sen_ip_src,
  42982. + hold_eroute.er_emask.sen_ip_src, 0, buf1, sizeof(buf1));
  42983. + subnettoa(hold_eroute.er_eaddr.sen_ip_dst,
  42984. + hold_eroute.er_emask.sen_ip_dst, 0, buf2, sizeof(buf2));
  42985. + KLIPS_PRINT(debug_pfkey,
  42986. + "klips_debug:ipsec_tunnel_start_xmit: "
  42987. + "calling breakeroute and makeroute for %s:%d->%s:%d %d HOLD eroute.\n",
  42988. + buf1, ntohs(hold_eroute.er_eaddr.sen_sport),
  42989. + buf2, ntohs(hold_eroute.er_eaddr.sen_dport),
  42990. + hold_eroute.er_eaddr.sen_proto);
  42991. + }
  42992. +#endif /* CONFIG_KLIPS_DEBUG */
  42993. +
  42994. + if (ipsec_breakroute(&(hold_eroute.er_eaddr), &(hold_eroute.er_emask),
  42995. + &first, &last)) {
  42996. + KLIPS_PRINT(debug_pfkey,
  42997. + "klips_debug:ipsec_tunnel_start_xmit: "
  42998. + "HOLD breakeroute found nothing.\n");
  42999. + } else {
  43000. + KLIPS_PRINT(debug_pfkey,
  43001. + "klips_debug:ipsec_tunnel_start_xmit: "
  43002. + "HOLD breakroute deleted %u.%u.%u.%u:%u -> %u.%u.%u.%u:%u %u\n",
  43003. + NIPQUAD(hold_eroute.er_eaddr.sen_ip_src),
  43004. + ntohs(hold_eroute.er_eaddr.sen_sport),
  43005. + NIPQUAD(hold_eroute.er_eaddr.sen_ip_dst),
  43006. + ntohs(hold_eroute.er_eaddr.sen_dport),
  43007. + hold_eroute.er_eaddr.sen_proto);
  43008. + }
  43009. + if (first != NULL)
  43010. + kfree_skb(first);
  43011. + if (last != NULL)
  43012. + kfree_skb(last);
  43013. +
  43014. + error = ipsec_makeroute(&(hold_eroute.er_eaddr),
  43015. + &(hold_eroute.er_emask),
  43016. + hold_said, eroute_pid, skb, NULL, NULL);
  43017. + if (error) {
  43018. + KLIPS_PRINT(debug_pfkey,
  43019. + "klips_debug:ipsec_tunnel_start_xmit: "
  43020. + "HOLD makeroute returned %d, failed.\n", error);
  43021. + } else {
  43022. + KLIPS_PRINT(debug_pfkey,
  43023. + "klips_debug:ipsec_tunnel_start_xmit: "
  43024. + "HOLD makeroute call successful.\n");
  43025. + }
  43026. + return (error == 0);
  43027. +}
  43028. +
  43029. +/*
  43030. + * upon entry to this function, ixs->skb should be setup
  43031. + * as follows:
  43032. + *
  43033. + * data = beginning of IP packet <- differs from ipsec_rcv().
  43034. + * nh.raw = beginning of IP packet.
  43035. + * h.raw = data after the IP packet.
  43036. + *
  43037. + */
  43038. +enum ipsec_xmit_value
  43039. +ipsec_xmit_encap_bundle(struct ipsec_xmit_state *ixs)
  43040. +{
  43041. + struct ipsec_alg_enc *ixt_e = NULL;
  43042. + struct ipsec_alg_auth *ixt_a = NULL;
  43043. + int blocksize = 8;
  43044. + enum ipsec_xmit_value bundle_stat = IPSEC_XMIT_OK;
  43045. +
  43046. + ixs->newdst = ixs->orgdst = ixs->iph->daddr;
  43047. + ixs->newsrc = ixs->orgsrc = ixs->iph->saddr;
  43048. + ixs->orgedst = ixs->outgoing_said.dst.u.v4.sin_addr.s_addr;
  43049. + ixs->iphlen = ixs->iph->ihl << 2;
  43050. + ixs->pyldsz = ntohs(ixs->iph->tot_len) - ixs->iphlen;
  43051. + ixs->max_headroom = ixs->max_tailroom = 0;
  43052. +
  43053. + if (ixs->outgoing_said.proto == IPPROTO_INT) {
  43054. + switch (ntohl(ixs->outgoing_said.spi)) {
  43055. + case SPI_DROP:
  43056. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43057. + "klips_debug:ipsec_xmit_encap_bundle: "
  43058. + "shunt SA of DROP or no eroute: dropping.\n");
  43059. + ixs->stats->tx_dropped++;
  43060. + break;
  43061. +
  43062. + case SPI_REJECT:
  43063. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43064. + "klips_debug:ipsec_xmit_encap_bundle: "
  43065. + "shunt SA of REJECT: notifying and dropping.\n");
  43066. + ICMP_SEND(ixs->skb,
  43067. + ICMP_DEST_UNREACH,
  43068. + ICMP_PKT_FILTERED,
  43069. + 0,
  43070. + ixs->physdev);
  43071. + ixs->stats->tx_dropped++;
  43072. + break;
  43073. +
  43074. + case SPI_PASS:
  43075. +#ifdef NET_21
  43076. + ixs->pass = 1;
  43077. +#endif /* NET_21 */
  43078. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43079. + "klips_debug:ipsec_xmit_encap_bundle: "
  43080. + "PASS: calling dev_queue_xmit\n");
  43081. + return IPSEC_XMIT_PASS;
  43082. + goto cleanup;
  43083. +
  43084. + case SPI_HOLD:
  43085. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43086. + "klips_debug:ipsec_xmit_encap_bundle: "
  43087. + "shunt SA of HOLD: this does not make sense here, dropping.\n");
  43088. + ixs->stats->tx_dropped++;
  43089. + break;
  43090. +
  43091. + case SPI_TRAP:
  43092. + case SPI_TRAPSUBNET:
  43093. + {
  43094. + struct sockaddr_in src, dst;
  43095. +#ifdef CONFIG_KLIPS_DEBUG
  43096. + char bufsrc[ADDRTOA_BUF], bufdst[ADDRTOA_BUF];
  43097. +#endif /* CONFIG_KLIPS_DEBUG */
  43098. +
  43099. + /* Signal all listening KMds with a PF_KEY ACQUIRE */
  43100. +
  43101. + memset(&src, 0, sizeof(src));
  43102. + memset(&dst, 0, sizeof(dst));
  43103. + src.sin_family = AF_INET;
  43104. + dst.sin_family = AF_INET;
  43105. + src.sin_addr.s_addr = ixs->iph->saddr;
  43106. + dst.sin_addr.s_addr = ixs->iph->daddr;
  43107. +
  43108. + ixs->ips.ips_transport_protocol = 0;
  43109. + src.sin_port = 0;
  43110. + dst.sin_port = 0;
  43111. +
  43112. + if(ixs->eroute->er_eaddr.sen_proto != 0) {
  43113. + ixs->ips.ips_transport_protocol = ixs->iph->protocol;
  43114. +
  43115. + if(ixs->eroute->er_eaddr.sen_sport != 0) {
  43116. + src.sin_port =
  43117. + (ixs->iph->protocol == IPPROTO_UDP
  43118. + ? ((struct udphdr*) (((caddr_t)ixs->iph) + (ixs->iph->ihl << 2)))->source
  43119. + : (ixs->iph->protocol == IPPROTO_TCP
  43120. + ? ((struct tcphdr*)((caddr_t)ixs->iph + (ixs->iph->ihl << 2)))->source
  43121. + : 0));
  43122. + }
  43123. + if(ixs->eroute->er_eaddr.sen_dport != 0) {
  43124. + dst.sin_port =
  43125. + (ixs->iph->protocol == IPPROTO_UDP
  43126. + ? ((struct udphdr*) (((caddr_t)ixs->iph) + (ixs->iph->ihl << 2)))->dest
  43127. + : (ixs->iph->protocol == IPPROTO_TCP
  43128. + ? ((struct tcphdr*)((caddr_t)ixs->iph + (ixs->iph->ihl << 2)))->dest
  43129. + : 0));
  43130. + }
  43131. + }
  43132. +
  43133. + ixs->ips.ips_addr_s = (struct sockaddr*)(&src);
  43134. + ixs->ips.ips_addr_d = (struct sockaddr*)(&dst);
  43135. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43136. + "klips_debug:ipsec_xmit_encap_bundle: "
  43137. + "SADB_ACQUIRE sent with src=%s:%d, dst=%s:%d, proto=%d.\n",
  43138. + addrtoa(((struct sockaddr_in*)(ixs->ips.ips_addr_s))->sin_addr, 0, bufsrc, sizeof(bufsrc)) <= ADDRTOA_BUF ? bufsrc : "BAD_ADDR",
  43139. + ntohs(((struct sockaddr_in*)(ixs->ips.ips_addr_s))->sin_port),
  43140. + addrtoa(((struct sockaddr_in*)(ixs->ips.ips_addr_d))->sin_addr, 0, bufdst, sizeof(bufdst)) <= ADDRTOA_BUF ? bufdst : "BAD_ADDR",
  43141. + ntohs(((struct sockaddr_in*)(ixs->ips.ips_addr_d))->sin_port),
  43142. + ixs->ips.ips_said.proto);
  43143. +
  43144. + /* increment count of total traps needed */
  43145. + ipsec_xmit_trap_count++;
  43146. +
  43147. + if (pfkey_acquire(&ixs->ips) == 0) {
  43148. +
  43149. + /* note that we succeeded */
  43150. + ipsec_xmit_trap_sendcount++;
  43151. +
  43152. + if (ixs->outgoing_said.spi==htonl(SPI_TRAPSUBNET)) {
  43153. + /*
  43154. + * The spinlock is to prevent any other
  43155. + * process from accessing or deleting
  43156. + * the eroute while we are using and
  43157. + * updating it.
  43158. + */
  43159. + spin_lock(&eroute_lock);
  43160. + ixs->eroute = ipsec_findroute(&ixs->matcher);
  43161. + if(ixs->eroute) {
  43162. + ixs->eroute->er_said.spi = htonl(SPI_HOLD);
  43163. + ixs->eroute->er_first = ixs->skb;
  43164. + ixs->skb = NULL;
  43165. + }
  43166. + spin_unlock(&eroute_lock);
  43167. + } else if (create_hold_eroute(ixs->eroute,
  43168. + ixs->skb,
  43169. + ixs->iph,
  43170. + ixs->eroute_pid)) {
  43171. + ixs->skb = NULL;
  43172. + }
  43173. + /* whether or not the above succeeded, we continue */
  43174. +
  43175. + }
  43176. + ixs->stats->tx_dropped++;
  43177. + }
  43178. + default:
  43179. + /* XXX what do we do with an unknown shunt spi? */
  43180. + break;
  43181. + } /* switch (ntohl(ixs->outgoing_said.spi)) */
  43182. + return IPSEC_XMIT_STOLEN;
  43183. + } /* if (ixs->outgoing_said.proto == IPPROTO_INT) */
  43184. +
  43185. + /*
  43186. + The spinlock is to prevent any other process from
  43187. + accessing or deleting the ipsec_sa hash table or any of the
  43188. + ipsec_sa s while we are using and updating them.
  43189. +
  43190. + This is not optimal, but was relatively straightforward
  43191. + at the time. A better way to do it has been planned for
  43192. + more than a year, to lock the hash table and put reference
  43193. + counts on each ipsec_sa instead. This is not likely to happen
  43194. + in KLIPS1 unless a volunteer contributes it, but will be
  43195. + designed into KLIPS2.
  43196. + */
  43197. + spin_lock(&tdb_lock);
  43198. +
  43199. + ixs->ipsp = ipsec_sa_getbyid(&ixs->outgoing_said);
  43200. + ixs->sa_len = satot(&ixs->outgoing_said, 0, ixs->sa_txt, sizeof(ixs->sa_txt));
  43201. +
  43202. + if (ixs->ipsp == NULL) {
  43203. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43204. + "klips_debug:ipsec_xmit_encap_bundle: "
  43205. + "no ipsec_sa for SA%s: outgoing packet with no SA, dropped.\n",
  43206. + ixs->sa_len ? ixs->sa_txt : " (error)");
  43207. + if(ixs->stats) {
  43208. + ixs->stats->tx_dropped++;
  43209. + }
  43210. + bundle_stat = IPSEC_XMIT_SAIDNOTFOUND;
  43211. + goto cleanup;
  43212. + }
  43213. +
  43214. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43215. + "klips_debug:ipsec_xmit_encap_bundle: "
  43216. + "found ipsec_sa -- SA:<%s%s%s> %s\n",
  43217. + IPS_XFORM_NAME(ixs->ipsp),
  43218. + ixs->sa_len ? ixs->sa_txt : " (error)");
  43219. +
  43220. + /*
  43221. + * How much headroom do we need to be able to apply
  43222. + * all the grouped transforms?
  43223. + */
  43224. + ixs->ipsq = ixs->ipsp; /* save the head of the ipsec_sa chain */
  43225. + while (ixs->ipsp) {
  43226. + ixs->sa_len = satot(&ixs->ipsp->ips_said, 0, ixs->sa_txt, sizeof(ixs->sa_txt));
  43227. + if(ixs->sa_len == 0) {
  43228. + strcpy(ixs->sa_txt, "(error)");
  43229. + }
  43230. +
  43231. + /* If it is in larval state, drop the packet, we cannot process yet. */
  43232. + if(ixs->ipsp->ips_state == SADB_SASTATE_LARVAL) {
  43233. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43234. + "klips_debug:ipsec_xmit_encap_bundle: "
  43235. + "ipsec_sa in larval state for SA:<%s%s%s> %s, cannot be used yet, dropping packet.\n",
  43236. + IPS_XFORM_NAME(ixs->ipsp),
  43237. + ixs->sa_len ? ixs->sa_txt : " (error)");
  43238. + if(ixs->stats) {
  43239. + ixs->stats->tx_errors++;
  43240. + }
  43241. + bundle_stat = IPSEC_XMIT_SAIDNOTLIVE;
  43242. + goto cleanup;
  43243. + }
  43244. +
  43245. + if(ixs->ipsp->ips_state == SADB_SASTATE_DEAD) {
  43246. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43247. + "klips_debug:ipsec_xmit_encap_bundle: "
  43248. + "ipsec_sa in dead state for SA:<%s%s%s> %s, can no longer be used, dropping packet.\n",
  43249. + IPS_XFORM_NAME(ixs->ipsp),
  43250. + ixs->sa_len ? ixs->sa_txt : " (error)");
  43251. + ixs->stats->tx_errors++;
  43252. + bundle_stat = IPSEC_XMIT_SAIDNOTLIVE;
  43253. + goto cleanup;
  43254. + }
  43255. +
  43256. + /* If the replay window counter == -1, expire SA, it will roll */
  43257. + if(ixs->ipsp->ips_replaywin && ixs->ipsp->ips_replaywin_lastseq == -1) {
  43258. + pfkey_expire(ixs->ipsp, 1);
  43259. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43260. + "klips_debug:ipsec_xmit_encap_bundle: "
  43261. + "replay window counter rolled for SA:<%s%s%s> %s, packet dropped, expiring SA.\n",
  43262. + IPS_XFORM_NAME(ixs->ipsp),
  43263. + ixs->sa_len ? ixs->sa_txt : " (error)");
  43264. + ipsec_sa_delchain(ixs->ipsp);
  43265. + ixs->stats->tx_errors++;
  43266. + bundle_stat = IPSEC_XMIT_REPLAYROLLED;
  43267. + goto cleanup;
  43268. + }
  43269. +
  43270. + /*
  43271. + * if this is the first time we are using this SA, mark start time,
  43272. + * and offset hard/soft counters by "now" for later checking.
  43273. + */
  43274. +#if 0
  43275. + if(ixs->ipsp->ips_life.ipl_usetime.count == 0) {
  43276. + ixs->ipsp->ips_life.ipl_usetime.count = jiffies;
  43277. + ixs->ipsp->ips_life.ipl_usetime.hard += jiffies;
  43278. + ixs->ipsp->ips_life.ipl_usetime.soft += jiffies;
  43279. + }
  43280. +#endif
  43281. +
  43282. +
  43283. + if(ipsec_lifetime_check(&ixs->ipsp->ips_life.ipl_bytes, "bytes", ixs->sa_txt,
  43284. + ipsec_life_countbased, ipsec_outgoing, ixs->ipsp) == ipsec_life_harddied ||
  43285. + ipsec_lifetime_check(&ixs->ipsp->ips_life.ipl_addtime, "addtime",ixs->sa_txt,
  43286. + ipsec_life_timebased, ipsec_outgoing, ixs->ipsp) == ipsec_life_harddied ||
  43287. + ipsec_lifetime_check(&ixs->ipsp->ips_life.ipl_usetime, "usetime",ixs->sa_txt,
  43288. + ipsec_life_timebased, ipsec_outgoing, ixs->ipsp) == ipsec_life_harddied ||
  43289. + ipsec_lifetime_check(&ixs->ipsp->ips_life.ipl_packets, "packets",ixs->sa_txt,
  43290. + ipsec_life_countbased, ipsec_outgoing, ixs->ipsp) == ipsec_life_harddied) {
  43291. +
  43292. + ipsec_sa_delchain(ixs->ipsp);
  43293. + ixs->stats->tx_errors++;
  43294. + bundle_stat = IPSEC_XMIT_LIFETIMEFAILED;
  43295. + goto cleanup;
  43296. + }
  43297. +
  43298. +
  43299. + ixs->headroom = ixs->tailroom = 0;
  43300. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43301. + "klips_debug:ipsec_xmit_encap_bundle: "
  43302. + "calling room for <%s%s%s>, SA:%s\n",
  43303. + IPS_XFORM_NAME(ixs->ipsp),
  43304. + ixs->sa_len ? ixs->sa_txt : " (error)");
  43305. + switch(ixs->ipsp->ips_said.proto) {
  43306. +#ifdef CONFIG_KLIPS_AH
  43307. + case IPPROTO_AH:
  43308. + ixs->headroom += sizeof(struct ahhdr);
  43309. + break;
  43310. +#endif /* CONFIG_KLIPS_AH */
  43311. +#ifdef CONFIG_KLIPS_ESP
  43312. + case IPPROTO_ESP:
  43313. + ixt_e=ixs->ipsp->ips_alg_enc;
  43314. + if (ixt_e) {
  43315. + blocksize = ixt_e->ixt_common.ixt_blocksize;
  43316. + ixs->headroom += ESP_HEADER_LEN + ixt_e->ixt_common.ixt_support.ias_ivlen/8;
  43317. + }
  43318. + else {
  43319. + ixs->stats->tx_errors++;
  43320. + bundle_stat = IPSEC_XMIT_ESP_BADALG;
  43321. + goto cleanup;
  43322. + }
  43323. +
  43324. + if ((ixt_a=ixs->ipsp->ips_alg_auth)) {
  43325. + ixs->tailroom += AHHMAC_HASHLEN;
  43326. + } else
  43327. + switch(ixs->ipsp->ips_authalg) {
  43328. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  43329. + case AH_MD5:
  43330. + ixs->tailroom += AHHMAC_HASHLEN;
  43331. + break;
  43332. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  43333. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  43334. + case AH_SHA:
  43335. + ixs->tailroom += AHHMAC_HASHLEN;
  43336. + break;
  43337. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  43338. + case AH_NONE:
  43339. + break;
  43340. + default:
  43341. + ixs->stats->tx_errors++;
  43342. + bundle_stat = IPSEC_XMIT_AH_BADALG;
  43343. + goto cleanup;
  43344. + }
  43345. + ixs->tailroom += blocksize != 1 ?
  43346. + ((blocksize - ((ixs->pyldsz + 2) % blocksize)) % blocksize) + 2 :
  43347. + ((4 - ((ixs->pyldsz + 2) % 4)) % 4) + 2;
  43348. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  43349. + if ((ixs->ipsp->ips_natt_type) && (!ixs->natt_type)) {
  43350. + ixs->natt_type = ixs->ipsp->ips_natt_type;
  43351. + ixs->natt_sport = ixs->ipsp->ips_natt_sport;
  43352. + ixs->natt_dport = ixs->ipsp->ips_natt_dport;
  43353. + switch (ixs->natt_type) {
  43354. + case ESPINUDP_WITH_NON_IKE:
  43355. + ixs->natt_head = sizeof(struct udphdr)+(2*sizeof(__u32));
  43356. + break;
  43357. +
  43358. + case ESPINUDP_WITH_NON_ESP:
  43359. + ixs->natt_head = sizeof(struct udphdr);
  43360. + break;
  43361. +
  43362. + default:
  43363. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT
  43364. + , "klips_xmit: invalid nat-t type %d"
  43365. + , ixs->natt_type);
  43366. + bundle_stat = IPSEC_XMIT_ESPUDP_BADTYPE;
  43367. + goto cleanup;
  43368. +
  43369. + break;
  43370. + }
  43371. + ixs->tailroom += ixs->natt_head;
  43372. + }
  43373. +#endif
  43374. + break;
  43375. +#endif /* !CONFIG_KLIPS_ESP */
  43376. +#ifdef CONFIG_KLIPS_IPIP
  43377. + case IPPROTO_IPIP:
  43378. + ixs->headroom += sizeof(struct iphdr);
  43379. + break;
  43380. +#endif /* !CONFIG_KLIPS_IPIP */
  43381. + case IPPROTO_COMP:
  43382. +#ifdef CONFIG_KLIPS_IPCOMP
  43383. + /*
  43384. + We can't predict how much the packet will
  43385. + shrink without doing the actual compression.
  43386. + We could do it here, if we were the first
  43387. + encapsulation in the chain. That might save
  43388. + us a skb_copy_expand, since we might fit
  43389. + into the existing skb then. However, this
  43390. + would be a bit unclean (and this hack has
  43391. + bit us once), so we better not do it. After
  43392. + all, the skb_copy_expand is cheap in
  43393. + comparison to the actual compression.
  43394. + At least we know the packet will not grow.
  43395. + */
  43396. + break;
  43397. +#endif /* CONFIG_KLIPS_IPCOMP */
  43398. + default:
  43399. + ixs->stats->tx_errors++;
  43400. + bundle_stat = IPSEC_XMIT_BADPROTO;
  43401. + goto cleanup;
  43402. + }
  43403. + ixs->ipsp = ixs->ipsp->ips_onext;
  43404. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43405. + "klips_debug:ipsec_xmit_encap_bundle: "
  43406. + "Required head,tailroom: %d,%d\n",
  43407. + ixs->headroom, ixs->tailroom);
  43408. + ixs->max_headroom += ixs->headroom;
  43409. + ixs->max_tailroom += ixs->tailroom;
  43410. + ixs->pyldsz += (ixs->headroom + ixs->tailroom);
  43411. + }
  43412. + ixs->ipsp = ixs->ipsq; /* restore the head of the ipsec_sa chain */
  43413. +
  43414. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43415. + "klips_debug:ipsec_xmit_encap_bundle: "
  43416. + "existing head,tailroom: %d,%d before applying xforms with head,tailroom: %d,%d .\n",
  43417. + skb_headroom(ixs->skb), skb_tailroom(ixs->skb),
  43418. + ixs->max_headroom, ixs->max_tailroom);
  43419. +
  43420. + ixs->tot_headroom += ixs->max_headroom;
  43421. + ixs->tot_tailroom += ixs->max_tailroom;
  43422. +
  43423. + ixs->mtudiff = ixs->cur_mtu + ixs->tot_headroom + ixs->tot_tailroom - ixs->physmtu;
  43424. +
  43425. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43426. + "klips_debug:ipsec_xmit_encap_bundle: "
  43427. + "mtu:%d physmtu:%d tothr:%d tottr:%d mtudiff:%d ippkttotlen:%d\n",
  43428. + ixs->cur_mtu, ixs->physmtu,
  43429. + ixs->tot_headroom, ixs->tot_tailroom, ixs->mtudiff, ntohs(ixs->iph->tot_len));
  43430. + if(ixs->mtudiff > 0) {
  43431. + int newmtu = ixs->physmtu - (ixs->tot_headroom + ((ixs->tot_tailroom + 2) & ~7) + 5);
  43432. +
  43433. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43434. + "klips_info:ipsec_xmit_encap_bundle: "
  43435. + "dev %s mtu of %d decreased by %d to %d\n",
  43436. + ixs->dev ? ixs->dev->name : "ifX",
  43437. + ixs->cur_mtu,
  43438. + ixs->cur_mtu - newmtu,
  43439. + newmtu);
  43440. + ixs->cur_mtu = newmtu;
  43441. +
  43442. + /* this would seem to adjust the MTU of the route as well */
  43443. +#if 0
  43444. + ixs->skb->dst->pmtu = ixs->prv->mtu; /* RGB */
  43445. +#endif /* 0 */
  43446. + }
  43447. +
  43448. + /*
  43449. + If the sender is doing PMTU discovery, and the
  43450. + packet doesn't fit within ixs->prv->mtu, notify him
  43451. + (unless it was an ICMP packet, or it was not the
  43452. + zero-offset packet) and send it anyways.
  43453. +
  43454. + Note: buggy firewall configuration may prevent the
  43455. + ICMP packet from getting back.
  43456. + */
  43457. + if(sysctl_ipsec_icmp
  43458. + && ixs->cur_mtu < ntohs(ixs->iph->tot_len)
  43459. + && (ixs->iph->frag_off & __constant_htons(IP_DF)) ) {
  43460. + int notify = ixs->iph->protocol != IPPROTO_ICMP
  43461. + && (ixs->iph->frag_off & __constant_htons(IP_OFFSET)) == 0;
  43462. +
  43463. +#ifdef IPSEC_obey_DF
  43464. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43465. + "klips_debug:ipsec_xmit_encap_bundle: "
  43466. + "fragmentation needed and DF set; %sdropping packet\n",
  43467. + notify ? "sending ICMP and " : "");
  43468. + if (notify)
  43469. + ICMP_SEND(ixs->skb,
  43470. + ICMP_DEST_UNREACH,
  43471. + ICMP_FRAG_NEEDED,
  43472. + ixs->cur_mtu,
  43473. + ixs->physdev);
  43474. + ixs->stats->tx_errors++;
  43475. + bundle_stat = IPSEC_XMIT_CANNOTFRAG;
  43476. + goto cleanup;
  43477. +#else /* IPSEC_obey_DF */
  43478. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43479. + "klips_debug:ipsec_xmit_encap_bundle: "
  43480. + "fragmentation needed and DF set; %spassing packet\n",
  43481. + notify ? "sending ICMP and " : "");
  43482. + if (notify)
  43483. + ICMP_SEND(ixs->skb,
  43484. + ICMP_DEST_UNREACH,
  43485. + ICMP_FRAG_NEEDED,
  43486. + ixs->cur_mtu,
  43487. + ixs->physdev);
  43488. +#endif /* IPSEC_obey_DF */
  43489. + }
  43490. +
  43491. +#ifdef MSS_HACK
  43492. + /*
  43493. + * If this is a transport mode TCP packet with
  43494. + * SYN set, determine an effective MSS based on
  43495. + * AH/ESP overheads determined above.
  43496. + */
  43497. + if (ixs->iph->protocol == IPPROTO_TCP
  43498. + && ixs->outgoing_said.proto != IPPROTO_IPIP) {
  43499. + struct tcphdr *tcph = ixs->skb->h.th;
  43500. + if (tcph->syn && !tcph->ack) {
  43501. + if(!ipsec_adjust_mss(ixs->skb, tcph, ixs->cur_mtu)) {
  43502. + printk(KERN_WARNING
  43503. + "klips_warning:ipsec_xmit_encap_bundle: "
  43504. + "ipsec_adjust_mss() failed\n");
  43505. + ixs->stats->tx_errors++;
  43506. + bundle_stat = IPSEC_XMIT_MSSERR;
  43507. + goto cleanup;
  43508. + }
  43509. + }
  43510. + }
  43511. +#endif /* MSS_HACK */
  43512. +
  43513. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  43514. + if ((ixs->natt_type) && (ixs->outgoing_said.proto != IPPROTO_IPIP)) {
  43515. + /**
  43516. + * NAT-Traversal and Transport Mode:
  43517. + * we need to correct TCP/UDP checksum
  43518. + *
  43519. + * If we've got NAT-OA, we can fix checksum without recalculation.
  43520. + * If we don't we can zero udp checksum.
  43521. + */
  43522. + __u32 natt_oa = ixs->ipsp->ips_natt_oa ?
  43523. + ((struct sockaddr_in*)(ixs->ipsp->ips_natt_oa))->sin_addr.s_addr : 0;
  43524. + __u16 pkt_len = ixs->skb->tail - (unsigned char *)ixs->iph;
  43525. + __u16 data_len = pkt_len - (ixs->iph->ihl << 2);
  43526. + switch (ixs->iph->protocol) {
  43527. + case IPPROTO_TCP:
  43528. + if (data_len >= sizeof(struct tcphdr)) {
  43529. + struct tcphdr *tcp = (struct tcphdr *)((__u32 *)ixs->iph+ixs->iph->ihl);
  43530. + if (natt_oa) {
  43531. + __u32 buff[2] = { ~ixs->iph->daddr, natt_oa };
  43532. + KLIPS_PRINT(debug_tunnel,
  43533. + "klips_debug:ipsec_tunnel_start_xmit: "
  43534. + "NAT-T & TRANSPORT: "
  43535. + "fix TCP checksum using NAT-OA\n");
  43536. + tcp->check = csum_fold(
  43537. + csum_partial((unsigned char *)buff, sizeof(buff),
  43538. + tcp->check^0xffff));
  43539. + }
  43540. + else {
  43541. + KLIPS_PRINT(debug_tunnel,
  43542. + "klips_debug:ipsec_tunnel_start_xmit: "
  43543. + "NAT-T & TRANSPORT: do not recalc TCP checksum\n");
  43544. + }
  43545. + }
  43546. + else {
  43547. + KLIPS_PRINT(debug_tunnel,
  43548. + "klips_debug:ipsec_tunnel_start_xmit: "
  43549. + "NAT-T & TRANSPORT: can't fix TCP checksum\n");
  43550. + }
  43551. + break;
  43552. + case IPPROTO_UDP:
  43553. + if (data_len >= sizeof(struct udphdr)) {
  43554. + struct udphdr *udp = (struct udphdr *)((__u32 *)ixs->iph+ixs->iph->ihl);
  43555. + if (udp->check == 0) {
  43556. + KLIPS_PRINT(debug_tunnel,
  43557. + "klips_debug:ipsec_tunnel_start_xmit: "
  43558. + "NAT-T & TRANSPORT: UDP checksum already 0\n");
  43559. + }
  43560. + else if (natt_oa) {
  43561. + __u32 buff[2] = { ~ixs->iph->daddr, natt_oa };
  43562. + KLIPS_PRINT(debug_tunnel,
  43563. + "klips_debug:ipsec_tunnel_start_xmit: "
  43564. + "NAT-T & TRANSPORT: "
  43565. + "fix UDP checksum using NAT-OA\n");
  43566. + udp->check = csum_fold(
  43567. + csum_partial((unsigned char *)buff, sizeof(buff),
  43568. + udp->check^0xffff));
  43569. + }
  43570. + else {
  43571. + KLIPS_PRINT(debug_tunnel,
  43572. + "klips_debug:ipsec_tunnel_start_xmit: "
  43573. + "NAT-T & TRANSPORT: zero UDP checksum\n");
  43574. + udp->check = 0;
  43575. + }
  43576. + }
  43577. + else {
  43578. + KLIPS_PRINT(debug_tunnel,
  43579. + "klips_debug:ipsec_tunnel_start_xmit: "
  43580. + "NAT-T & TRANSPORT: can't fix UDP checksum\n");
  43581. + }
  43582. + break;
  43583. + default:
  43584. + KLIPS_PRINT(debug_tunnel,
  43585. + "klips_debug:ipsec_tunnel_start_xmit: "
  43586. + "NAT-T & TRANSPORT: non TCP/UDP packet -- do nothing\n");
  43587. + break;
  43588. + }
  43589. + }
  43590. +#endif /* CONFIG_IPSEC_NAT_TRAVERSAL */
  43591. +
  43592. + if(!ixs->hard_header_stripped && ixs->hard_header_len>0) {
  43593. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43594. + "klips_debug:ipsec_xmit_encap_bundle: "
  43595. + "allocating %d bytes for hardheader.\n",
  43596. + ixs->hard_header_len);
  43597. + if((ixs->saved_header = kmalloc(ixs->hard_header_len, GFP_ATOMIC)) == NULL) {
  43598. + printk(KERN_WARNING "klips_debug:ipsec_xmit_encap_bundle: "
  43599. + "Failed, tried to allocate %d bytes for temp hard_header.\n",
  43600. + ixs->hard_header_len);
  43601. + ixs->stats->tx_errors++;
  43602. + bundle_stat = IPSEC_XMIT_ERRMEMALLOC;
  43603. + goto cleanup;
  43604. + }
  43605. + {
  43606. + int i;
  43607. + for (i = 0; i < ixs->hard_header_len; i++) {
  43608. + ixs->saved_header[i] = ixs->skb->data[i];
  43609. + }
  43610. + }
  43611. + if(ixs->skb->len < ixs->hard_header_len) {
  43612. + printk(KERN_WARNING "klips_error:ipsec_xmit_encap_bundle: "
  43613. + "tried to skb_pull hhlen=%d, %d available. This should never happen, please report.\n",
  43614. + ixs->hard_header_len, (int)(ixs->skb->len));
  43615. + ixs->stats->tx_errors++;
  43616. + bundle_stat = IPSEC_XMIT_ESP_PUSHPULLERR;
  43617. + goto cleanup;
  43618. + }
  43619. + skb_pull(ixs->skb, ixs->hard_header_len);
  43620. + ixs->hard_header_stripped = 1;
  43621. +
  43622. +/* ixs->iph = (struct iphdr *) (ixs->skb->data); */
  43623. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43624. + "klips_debug:ipsec_xmit_encap_bundle: "
  43625. + "head,tailroom: %d,%d after hard_header stripped.\n",
  43626. + skb_headroom(ixs->skb), skb_tailroom(ixs->skb));
  43627. + KLIPS_IP_PRINT(debug_tunnel & DB_TN_CROUT, ixs->iph);
  43628. + } else {
  43629. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43630. + "klips_debug:ipsec_xmit_encap_bundle: "
  43631. + "hard header already stripped.\n");
  43632. + }
  43633. +
  43634. + ixs->ll_headroom = (ixs->hard_header_len + 15) & ~15;
  43635. +
  43636. + if ((skb_headroom(ixs->skb) >= ixs->max_headroom + 2 * ixs->ll_headroom) &&
  43637. + (skb_tailroom(ixs->skb) >= ixs->max_tailroom)
  43638. +#ifndef NET_21
  43639. + && ixs->skb->free
  43640. +#endif /* !NET_21 */
  43641. + ) {
  43642. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43643. + "klips_debug:ipsec_xmit_encap_bundle: "
  43644. + "data fits in existing skb\n");
  43645. + } else {
  43646. + struct sk_buff* tskb;
  43647. +
  43648. + if(!ixs->oskb) {
  43649. + ixs->oskb = ixs->skb;
  43650. + }
  43651. +
  43652. + tskb = skb_copy_expand(ixs->skb,
  43653. + /* The need for 2 * link layer length here remains unexplained...RGB */
  43654. + ixs->max_headroom + 2 * ixs->ll_headroom,
  43655. + ixs->max_tailroom,
  43656. + GFP_ATOMIC);
  43657. +
  43658. + if(tskb && ixs->skb->sk) {
  43659. + skb_set_owner_w(tskb, ixs->skb->sk);
  43660. + }
  43661. +
  43662. + if(ixs->skb != ixs->oskb) {
  43663. + ipsec_kfree_skb(ixs->skb);
  43664. + }
  43665. + ixs->skb = tskb;
  43666. + if (!ixs->skb) {
  43667. + printk(KERN_WARNING
  43668. + "klips_debug:ipsec_xmit_encap_bundle: "
  43669. + "Failed, tried to allocate %d head and %d tailroom\n",
  43670. + ixs->max_headroom, ixs->max_tailroom);
  43671. + ixs->stats->tx_errors++;
  43672. + bundle_stat = IPSEC_XMIT_ERRSKBALLOC;
  43673. + goto cleanup;
  43674. + }
  43675. + KLIPS_PRINT(debug_tunnel & DB_TN_CROUT,
  43676. + "klips_debug:ipsec_xmit_encap_bundle: "
  43677. + "head,tailroom: %d,%d after allocation\n",
  43678. + skb_headroom(ixs->skb), skb_tailroom(ixs->skb));
  43679. + }
  43680. +#ifdef CONFIG_KLIPS_DEBUG
  43681. + if(debug_tunnel & DB_TN_ENCAP) {
  43682. + ipsec_print_ip(ixs->iph);
  43683. + }
  43684. +#endif
  43685. +
  43686. + /*
  43687. + * Apply grouped transforms to packet
  43688. + */
  43689. + while (ixs->ipsp) {
  43690. + enum ipsec_xmit_value encap_stat = IPSEC_XMIT_OK;
  43691. +
  43692. + encap_stat = ipsec_xmit_encap_once(ixs);
  43693. +#ifdef CONFIG_KLIPS_DEBUG
  43694. + if(debug_tunnel & DB_TN_ENCAP) {
  43695. + ipsec_print_ip(ixs->iph);
  43696. + }
  43697. +#endif
  43698. +
  43699. + if(encap_stat != IPSEC_XMIT_OK) {
  43700. + KLIPS_PRINT(debug_tunnel & DB_TN_XMIT,
  43701. + "klips_debug:ipsec_xmit_encap_bundle: encap_once failed: %d\n",
  43702. + encap_stat);
  43703. +
  43704. + bundle_stat = IPSEC_XMIT_ENCAPFAIL;
  43705. + goto cleanup;
  43706. + }
  43707. + }
  43708. +
  43709. + /* we are done with this SA */
  43710. + ipsec_sa_put(ixs->ipsp);
  43711. +
  43712. + /* end encapsulation loop here XXX */
  43713. + cleanup:
  43714. + spin_unlock(&tdb_lock);
  43715. + return bundle_stat;
  43716. +}
  43717. +
  43718. +/*
  43719. + * $Log: ipsec_xmit.c,v $
  43720. + * Revision 1.20.2.9 2007/07/06 17:18:43 paul
  43721. + * Fix for authentication field on sent packets has size equals to zero when
  43722. + * using custom auth algorithms. This is bug #811. Patch by "iamscared".
  43723. + *
  43724. + * Revision 1.20.2.8 2006/10/06 21:39:26 paul
  43725. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  43726. + * set. This is defined through autoconf.h which is included through the
  43727. + * linux kernel build macros.
  43728. + *
  43729. + * Revision 1.20.2.7 2006/08/24 03:02:01 paul
  43730. + * Compile fixes for when CONFIG_KLIPS_DEBUG is not set. (bug #642)
  43731. + *
  43732. + * Revision 1.20.2.6 2006/07/07 22:09:49 paul
  43733. + * From: Bart Trojanowski <bart@xelerance.com>
  43734. + * Removing a left over '#else' that split another '#if/#endif' block in two.
  43735. + *
  43736. + * Revision 1.20.2.5 2006/07/07 15:43:17 paul
  43737. + * From: Bart Trojanowski <bart@xelerance.com>
  43738. + * improved protocol detection in ipsec_print_ip() -- a debug aid.
  43739. + *
  43740. + * Revision 1.20.2.4 2006/04/20 16:33:07 mcr
  43741. + * remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  43742. + * Fix in-kernel module compilation. Sub-makefiles do not work.
  43743. + *
  43744. + * Revision 1.20.2.3 2005/11/29 21:52:57 ken
  43745. + * Fix for #518 MTU issues
  43746. + *
  43747. + * Revision 1.20.2.2 2005/11/27 21:41:03 paul
  43748. + * Pull down TTL fixes from head. this fixes "Unknown symbol sysctl_ip_default_ttl"in for klips as module.
  43749. + *
  43750. + * Revision 1.20.2.1 2005/08/27 23:40:00 paul
  43751. + * recommited HAVE_SOCK_SECURITY fixes for linux 2.6.13
  43752. + *
  43753. + * Revision 1.20 2005/07/12 15:39:27 paul
  43754. + * include asm/uaccess.h for VERIFY_WRITE
  43755. + *
  43756. + * Revision 1.19 2005/05/24 01:02:35 mcr
  43757. + * some refactoring/simplification of situation where alg
  43758. + * is not found.
  43759. + *
  43760. + * Revision 1.18 2005/05/23 23:52:33 mcr
  43761. + * adjust comments, add additional debugging.
  43762. + *
  43763. + * Revision 1.17 2005/05/23 22:57:23 mcr
  43764. + * removed explicit 3DES support.
  43765. + *
  43766. + * Revision 1.16 2005/05/21 03:29:15 mcr
  43767. + * fixed warning about unused zeroes if AH is off.
  43768. + *
  43769. + * Revision 1.15 2005/05/20 16:47:59 mcr
  43770. + * include asm/checksum.h to get ip_fast_csum macro.
  43771. + *
  43772. + * Revision 1.14 2005/05/11 01:43:03 mcr
  43773. + * removed "poor-man"s OOP in favour of proper C structures.
  43774. + *
  43775. + * Revision 1.13 2005/04/29 05:10:22 mcr
  43776. + * removed from extraenous includes to make unit testing easier.
  43777. + *
  43778. + * Revision 1.12 2005/04/15 01:28:34 mcr
  43779. + * use ipsec_dmp_block.
  43780. + *
  43781. + * Revision 1.11 2005/01/26 00:50:35 mcr
  43782. + * adjustment of confusion of CONFIG_IPSEC_NAT vs CONFIG_KLIPS_NAT,
  43783. + * and make sure that NAT_TRAVERSAL is set as well to match
  43784. + * userspace compiles of code.
  43785. + *
  43786. + * Revision 1.10 2004/09/13 17:55:21 ken
  43787. + * MD5* -> osMD5*
  43788. + *
  43789. + * Revision 1.9 2004/07/10 19:11:18 mcr
  43790. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  43791. + *
  43792. + * Revision 1.8 2004/04/06 02:49:26 mcr
  43793. + * pullup of algo code from alg-branch.
  43794. + *
  43795. + * Revision 1.7 2004/02/03 03:13:41 mcr
  43796. + * mark invalid encapsulation states.
  43797. + *
  43798. + * Revision 1.6.2.1 2003/12/22 15:25:52 jjo
  43799. + * Merged algo-0.8.1-rc11-test1 into alg-branch
  43800. + *
  43801. + * Revision 1.6 2003/12/10 01:14:27 mcr
  43802. + * NAT-traversal patches to KLIPS.
  43803. + *
  43804. + * Revision 1.5 2003/10/31 02:27:55 mcr
  43805. + * pulled up port-selector patches and sa_id elimination.
  43806. + *
  43807. + * Revision 1.4.4.2 2003/10/29 01:37:39 mcr
  43808. + * when creating %hold from %trap, only make the %hold as
  43809. + * specific as the %trap was - so if the protocol and ports
  43810. + * were wildcards, then the %hold will be too.
  43811. + *
  43812. + * Revision 1.4.4.1 2003/09/21 13:59:56 mcr
  43813. + * pre-liminary X.509 patch - does not yet pass tests.
  43814. + *
  43815. + * Revision 1.4 2003/06/20 02:28:10 mcr
  43816. + * misstype of variable name, not detected by module build.
  43817. + *
  43818. + * Revision 1.3 2003/06/20 01:42:21 mcr
  43819. + * added counters to measure how many ACQUIREs we send to pluto,
  43820. + * and how many are successfully sent.
  43821. + *
  43822. + * Revision 1.2 2003/04/03 17:38:35 rgb
  43823. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  43824. + * Normalised coding style.
  43825. + * Simplified logic and reduced duplication of code.
  43826. + *
  43827. + * Revision 1.1 2003/02/12 19:31:23 rgb
  43828. + * Refactored from ipsec_tunnel.c
  43829. + *
  43830. + * Local Variables:
  43831. + * c-file-style: "linux"
  43832. + * End:
  43833. + *
  43834. + */
  43835. --- /dev/null Tue Mar 11 13:02:56 2003
  43836. +++ linux/net/ipsec/match586.S Mon Feb 9 13:51:03 2004
  43837. @@ -0,0 +1,357 @@
  43838. +/* match.s -- Pentium-optimized version of longest_match()
  43839. + * Written for zlib 1.1.2
  43840. + * Copyright (C) 1998 Brian Raiter <breadbox@muppetlabs.com>
  43841. + *
  43842. + * This is free software; you can redistribute it and/or modify it
  43843. + * under the terms of the GNU General Public License.
  43844. + */
  43845. +
  43846. +#ifndef NO_UNDERLINE
  43847. +#define match_init _ipcomp_match_init
  43848. +#define longest_match _ipcomp_longest_match
  43849. +#else
  43850. +#define match_init ipcomp_match_init
  43851. +#define longest_match ipcomp_longest_match
  43852. +#endif
  43853. +
  43854. +#define MAX_MATCH (258)
  43855. +#define MIN_MATCH (3)
  43856. +#define MIN_LOOKAHEAD (MAX_MATCH + MIN_MATCH + 1)
  43857. +#define MAX_MATCH_8 ((MAX_MATCH + 7) & ~7)
  43858. +
  43859. +/* stack frame offsets */
  43860. +
  43861. +#define wmask 0 /* local copy of s->wmask */
  43862. +#define window 4 /* local copy of s->window */
  43863. +#define windowbestlen 8 /* s->window + bestlen */
  43864. +#define chainlenscanend 12 /* high word: current chain len */
  43865. + /* low word: last bytes sought */
  43866. +#define scanstart 16 /* first two bytes of string */
  43867. +#define scanalign 20 /* dword-misalignment of string */
  43868. +#define nicematch 24 /* a good enough match size */
  43869. +#define bestlen 28 /* size of best match so far */
  43870. +#define scan 32 /* ptr to string wanting match */
  43871. +
  43872. +#define LocalVarsSize (36)
  43873. +/* saved ebx 36 */
  43874. +/* saved edi 40 */
  43875. +/* saved esi 44 */
  43876. +/* saved ebp 48 */
  43877. +/* return address 52 */
  43878. +#define deflatestate 56 /* the function arguments */
  43879. +#define curmatch 60
  43880. +
  43881. +/* Offsets for fields in the deflate_state structure. These numbers
  43882. + * are calculated from the definition of deflate_state, with the
  43883. + * assumption that the compiler will dword-align the fields. (Thus,
  43884. + * changing the definition of deflate_state could easily cause this
  43885. + * program to crash horribly, without so much as a warning at
  43886. + * compile time. Sigh.)
  43887. + */
  43888. +#define dsWSize 36
  43889. +#define dsWMask 44
  43890. +#define dsWindow 48
  43891. +#define dsPrev 56
  43892. +#define dsMatchLen 88
  43893. +#define dsPrevMatch 92
  43894. +#define dsStrStart 100
  43895. +#define dsMatchStart 104
  43896. +#define dsLookahead 108
  43897. +#define dsPrevLen 112
  43898. +#define dsMaxChainLen 116
  43899. +#define dsGoodMatch 132
  43900. +#define dsNiceMatch 136
  43901. +
  43902. +
  43903. +.file "match.S"
  43904. +
  43905. +.globl match_init, longest_match
  43906. +
  43907. +.text
  43908. +
  43909. +/* uInt longest_match(deflate_state *deflatestate, IPos curmatch) */
  43910. +
  43911. +longest_match:
  43912. +
  43913. +/* Save registers that the compiler may be using, and adjust %esp to */
  43914. +/* make room for our stack frame. */
  43915. +
  43916. + pushl %ebp
  43917. + pushl %edi
  43918. + pushl %esi
  43919. + pushl %ebx
  43920. + subl $LocalVarsSize, %esp
  43921. +
  43922. +/* Retrieve the function arguments. %ecx will hold cur_match */
  43923. +/* throughout the entire function. %edx will hold the pointer to the */
  43924. +/* deflate_state structure during the function's setup (before */
  43925. +/* entering the main loop). */
  43926. +
  43927. + movl deflatestate(%esp), %edx
  43928. + movl curmatch(%esp), %ecx
  43929. +
  43930. +/* if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; */
  43931. +
  43932. + movl dsNiceMatch(%edx), %eax
  43933. + movl dsLookahead(%edx), %ebx
  43934. + cmpl %eax, %ebx
  43935. + jl LookaheadLess
  43936. + movl %eax, %ebx
  43937. +LookaheadLess: movl %ebx, nicematch(%esp)
  43938. +
  43939. +/* register Bytef *scan = s->window + s->strstart; */
  43940. +
  43941. + movl dsWindow(%edx), %esi
  43942. + movl %esi, window(%esp)
  43943. + movl dsStrStart(%edx), %ebp
  43944. + lea (%esi,%ebp), %edi
  43945. + movl %edi, scan(%esp)
  43946. +
  43947. +/* Determine how many bytes the scan ptr is off from being */
  43948. +/* dword-aligned. */
  43949. +
  43950. + movl %edi, %eax
  43951. + negl %eax
  43952. + andl $3, %eax
  43953. + movl %eax, scanalign(%esp)
  43954. +
  43955. +/* IPos limit = s->strstart > (IPos)MAX_DIST(s) ? */
  43956. +/* s->strstart - (IPos)MAX_DIST(s) : NIL; */
  43957. +
  43958. + movl dsWSize(%edx), %eax
  43959. + subl $MIN_LOOKAHEAD, %eax
  43960. + subl %eax, %ebp
  43961. + jg LimitPositive
  43962. + xorl %ebp, %ebp
  43963. +LimitPositive:
  43964. +
  43965. +/* unsigned chain_length = s->max_chain_length; */
  43966. +/* if (s->prev_length >= s->good_match) { */
  43967. +/* chain_length >>= 2; */
  43968. +/* } */
  43969. +
  43970. + movl dsPrevLen(%edx), %eax
  43971. + movl dsGoodMatch(%edx), %ebx
  43972. + cmpl %ebx, %eax
  43973. + movl dsMaxChainLen(%edx), %ebx
  43974. + jl LastMatchGood
  43975. + shrl $2, %ebx
  43976. +LastMatchGood:
  43977. +
  43978. +/* chainlen is decremented once beforehand so that the function can */
  43979. +/* use the sign flag instead of the zero flag for the exit test. */
  43980. +/* It is then shifted into the high word, to make room for the scanend */
  43981. +/* scanend value, which it will always accompany. */
  43982. +
  43983. + decl %ebx
  43984. + shll $16, %ebx
  43985. +
  43986. +/* int best_len = s->prev_length; */
  43987. +
  43988. + movl dsPrevLen(%edx), %eax
  43989. + movl %eax, bestlen(%esp)
  43990. +
  43991. +/* Store the sum of s->window + best_len in %esi locally, and in %esi. */
  43992. +
  43993. + addl %eax, %esi
  43994. + movl %esi, windowbestlen(%esp)
  43995. +
  43996. +/* register ush scan_start = *(ushf*)scan; */
  43997. +/* register ush scan_end = *(ushf*)(scan+best_len-1); */
  43998. +
  43999. + movw (%edi), %bx
  44000. + movw %bx, scanstart(%esp)
  44001. + movw -1(%edi,%eax), %bx
  44002. + movl %ebx, chainlenscanend(%esp)
  44003. +
  44004. +/* Posf *prev = s->prev; */
  44005. +/* uInt wmask = s->w_mask; */
  44006. +
  44007. + movl dsPrev(%edx), %edi
  44008. + movl dsWMask(%edx), %edx
  44009. + mov %edx, wmask(%esp)
  44010. +
  44011. +/* Jump into the main loop. */
  44012. +
  44013. + jmp LoopEntry
  44014. +
  44015. +.balign 16
  44016. +
  44017. +/* do {
  44018. + * match = s->window + cur_match;
  44019. + * if (*(ushf*)(match+best_len-1) != scan_end ||
  44020. + * *(ushf*)match != scan_start) continue;
  44021. + * [...]
  44022. + * } while ((cur_match = prev[cur_match & wmask]) > limit
  44023. + * && --chain_length != 0);
  44024. + *
  44025. + * Here is the inner loop of the function. The function will spend the
  44026. + * majority of its time in this loop, and majority of that time will
  44027. + * be spent in the first ten instructions.
  44028. + *
  44029. + * Within this loop:
  44030. + * %ebx = chainlenscanend - i.e., ((chainlen << 16) | scanend)
  44031. + * %ecx = curmatch
  44032. + * %edx = curmatch & wmask
  44033. + * %esi = windowbestlen - i.e., (window + bestlen)
  44034. + * %edi = prev
  44035. + * %ebp = limit
  44036. + *
  44037. + * Two optimization notes on the choice of instructions:
  44038. + *
  44039. + * The first instruction uses a 16-bit address, which costs an extra,
  44040. + * unpairable cycle. This is cheaper than doing a 32-bit access and
  44041. + * zeroing the high word, due to the 3-cycle misalignment penalty which
  44042. + * would occur half the time. This also turns out to be cheaper than
  44043. + * doing two separate 8-bit accesses, as the memory is so rarely in the
  44044. + * L1 cache.
  44045. + *
  44046. + * The window buffer, however, apparently spends a lot of time in the
  44047. + * cache, and so it is faster to retrieve the word at the end of the
  44048. + * match string with two 8-bit loads. The instructions that test the
  44049. + * word at the beginning of the match string, however, are executed
  44050. + * much less frequently, and there it was cheaper to use 16-bit
  44051. + * instructions, which avoided the necessity of saving off and
  44052. + * subsequently reloading one of the other registers.
  44053. + */
  44054. +LookupLoop:
  44055. + /* 1 U & V */
  44056. + movw (%edi,%edx,2), %cx /* 2 U pipe */
  44057. + movl wmask(%esp), %edx /* 2 V pipe */
  44058. + cmpl %ebp, %ecx /* 3 U pipe */
  44059. + jbe LeaveNow /* 3 V pipe */
  44060. + subl $0x00010000, %ebx /* 4 U pipe */
  44061. + js LeaveNow /* 4 V pipe */
  44062. +LoopEntry: movb -1(%esi,%ecx), %al /* 5 U pipe */
  44063. + andl %ecx, %edx /* 5 V pipe */
  44064. + cmpb %bl, %al /* 6 U pipe */
  44065. + jnz LookupLoop /* 6 V pipe */
  44066. + movb (%esi,%ecx), %ah
  44067. + cmpb %bh, %ah
  44068. + jnz LookupLoop
  44069. + movl window(%esp), %eax
  44070. + movw (%eax,%ecx), %ax
  44071. + cmpw scanstart(%esp), %ax
  44072. + jnz LookupLoop
  44073. +
  44074. +/* Store the current value of chainlen. */
  44075. +
  44076. + movl %ebx, chainlenscanend(%esp)
  44077. +
  44078. +/* Point %edi to the string under scrutiny, and %esi to the string we */
  44079. +/* are hoping to match it up with. In actuality, %esi and %edi are */
  44080. +/* both pointed (MAX_MATCH_8 - scanalign) bytes ahead, and %edx is */
  44081. +/* initialized to -(MAX_MATCH_8 - scanalign). */
  44082. +
  44083. + movl window(%esp), %esi
  44084. + movl scan(%esp), %edi
  44085. + addl %ecx, %esi
  44086. + movl scanalign(%esp), %eax
  44087. + movl $(-MAX_MATCH_8), %edx
  44088. + lea MAX_MATCH_8(%edi,%eax), %edi
  44089. + lea MAX_MATCH_8(%esi,%eax), %esi
  44090. +
  44091. +/* Test the strings for equality, 8 bytes at a time. At the end,
  44092. + * adjust %edx so that it is offset to the exact byte that mismatched.
  44093. + *
  44094. + * We already know at this point that the first three bytes of the
  44095. + * strings match each other, and they can be safely passed over before
  44096. + * starting the compare loop. So what this code does is skip over 0-3
  44097. + * bytes, as much as necessary in order to dword-align the %edi
  44098. + * pointer. (%esi will still be misaligned three times out of four.)
  44099. + *
  44100. + * It should be confessed that this loop usually does not represent
  44101. + * much of the total running time. Replacing it with a more
  44102. + * straightforward "rep cmpsb" would not drastically degrade
  44103. + * performance.
  44104. + */
  44105. +LoopCmps:
  44106. + movl (%esi,%edx), %eax
  44107. + movl (%edi,%edx), %ebx
  44108. + xorl %ebx, %eax
  44109. + jnz LeaveLoopCmps
  44110. + movl 4(%esi,%edx), %eax
  44111. + movl 4(%edi,%edx), %ebx
  44112. + xorl %ebx, %eax
  44113. + jnz LeaveLoopCmps4
  44114. + addl $8, %edx
  44115. + jnz LoopCmps
  44116. + jmp LenMaximum
  44117. +LeaveLoopCmps4: addl $4, %edx
  44118. +LeaveLoopCmps: testl $0x0000FFFF, %eax
  44119. + jnz LenLower
  44120. + addl $2, %edx
  44121. + shrl $16, %eax
  44122. +LenLower: subb $1, %al
  44123. + adcl $0, %edx
  44124. +
  44125. +/* Calculate the length of the match. If it is longer than MAX_MATCH, */
  44126. +/* then automatically accept it as the best possible match and leave. */
  44127. +
  44128. + lea (%edi,%edx), %eax
  44129. + movl scan(%esp), %edi
  44130. + subl %edi, %eax
  44131. + cmpl $MAX_MATCH, %eax
  44132. + jge LenMaximum
  44133. +
  44134. +/* If the length of the match is not longer than the best match we */
  44135. +/* have so far, then forget it and return to the lookup loop. */
  44136. +
  44137. + movl deflatestate(%esp), %edx
  44138. + movl bestlen(%esp), %ebx
  44139. + cmpl %ebx, %eax
  44140. + jg LongerMatch
  44141. + movl chainlenscanend(%esp), %ebx
  44142. + movl windowbestlen(%esp), %esi
  44143. + movl dsPrev(%edx), %edi
  44144. + movl wmask(%esp), %edx
  44145. + andl %ecx, %edx
  44146. + jmp LookupLoop
  44147. +
  44148. +/* s->match_start = cur_match; */
  44149. +/* best_len = len; */
  44150. +/* if (len >= nice_match) break; */
  44151. +/* scan_end = *(ushf*)(scan+best_len-1); */
  44152. +
  44153. +LongerMatch: movl nicematch(%esp), %ebx
  44154. + movl %eax, bestlen(%esp)
  44155. + movl %ecx, dsMatchStart(%edx)
  44156. + cmpl %ebx, %eax
  44157. + jge LeaveNow
  44158. + movl window(%esp), %esi
  44159. + addl %eax, %esi
  44160. + movl %esi, windowbestlen(%esp)
  44161. + movl chainlenscanend(%esp), %ebx
  44162. + movw -1(%edi,%eax), %bx
  44163. + movl dsPrev(%edx), %edi
  44164. + movl %ebx, chainlenscanend(%esp)
  44165. + movl wmask(%esp), %edx
  44166. + andl %ecx, %edx
  44167. + jmp LookupLoop
  44168. +
  44169. +/* Accept the current string, with the maximum possible length. */
  44170. +
  44171. +LenMaximum: movl deflatestate(%esp), %edx
  44172. + movl $MAX_MATCH, bestlen(%esp)
  44173. + movl %ecx, dsMatchStart(%edx)
  44174. +
  44175. +/* if ((uInt)best_len <= s->lookahead) return (uInt)best_len; */
  44176. +/* return s->lookahead; */
  44177. +
  44178. +LeaveNow:
  44179. + movl deflatestate(%esp), %edx
  44180. + movl bestlen(%esp), %ebx
  44181. + movl dsLookahead(%edx), %eax
  44182. + cmpl %eax, %ebx
  44183. + jg LookaheadRet
  44184. + movl %ebx, %eax
  44185. +LookaheadRet:
  44186. +
  44187. +/* Restore the stack and return from whence we came. */
  44188. +
  44189. + addl $LocalVarsSize, %esp
  44190. + popl %ebx
  44191. + popl %esi
  44192. + popl %edi
  44193. + popl %ebp
  44194. +match_init: ret
  44195. --- /dev/null Tue Mar 11 13:02:56 2003
  44196. +++ linux/net/ipsec/match686.S Mon Feb 9 13:51:03 2004
  44197. @@ -0,0 +1,330 @@
  44198. +/* match.s -- Pentium-Pro-optimized version of longest_match()
  44199. + * Written for zlib 1.1.2
  44200. + * Copyright (C) 1998 Brian Raiter <breadbox@muppetlabs.com>
  44201. + *
  44202. + * This is free software; you can redistribute it and/or modify it
  44203. + * under the terms of the GNU General Public License.
  44204. + */
  44205. +
  44206. +#ifndef NO_UNDERLINE
  44207. +#define match_init _ipcomp_match_init
  44208. +#define longest_match _ipcomp_longest_match
  44209. +#else
  44210. +#define match_init ipcomp_match_init
  44211. +#define longest_match ipcomp_longest_match
  44212. +#endif
  44213. +
  44214. +#define MAX_MATCH (258)
  44215. +#define MIN_MATCH (3)
  44216. +#define MIN_LOOKAHEAD (MAX_MATCH + MIN_MATCH + 1)
  44217. +#define MAX_MATCH_8 ((MAX_MATCH + 7) & ~7)
  44218. +
  44219. +/* stack frame offsets */
  44220. +
  44221. +#define chainlenwmask 0 /* high word: current chain len */
  44222. + /* low word: s->wmask */
  44223. +#define window 4 /* local copy of s->window */
  44224. +#define windowbestlen 8 /* s->window + bestlen */
  44225. +#define scanstart 16 /* first two bytes of string */
  44226. +#define scanend 12 /* last two bytes of string */
  44227. +#define scanalign 20 /* dword-misalignment of string */
  44228. +#define nicematch 24 /* a good enough match size */
  44229. +#define bestlen 28 /* size of best match so far */
  44230. +#define scan 32 /* ptr to string wanting match */
  44231. +
  44232. +#define LocalVarsSize (36)
  44233. +/* saved ebx 36 */
  44234. +/* saved edi 40 */
  44235. +/* saved esi 44 */
  44236. +/* saved ebp 48 */
  44237. +/* return address 52 */
  44238. +#define deflatestate 56 /* the function arguments */
  44239. +#define curmatch 60
  44240. +
  44241. +/* Offsets for fields in the deflate_state structure. These numbers
  44242. + * are calculated from the definition of deflate_state, with the
  44243. + * assumption that the compiler will dword-align the fields. (Thus,
  44244. + * changing the definition of deflate_state could easily cause this
  44245. + * program to crash horribly, without so much as a warning at
  44246. + * compile time. Sigh.)
  44247. + */
  44248. +#define dsWSize 36
  44249. +#define dsWMask 44
  44250. +#define dsWindow 48
  44251. +#define dsPrev 56
  44252. +#define dsMatchLen 88
  44253. +#define dsPrevMatch 92
  44254. +#define dsStrStart 100
  44255. +#define dsMatchStart 104
  44256. +#define dsLookahead 108
  44257. +#define dsPrevLen 112
  44258. +#define dsMaxChainLen 116
  44259. +#define dsGoodMatch 132
  44260. +#define dsNiceMatch 136
  44261. +
  44262. +
  44263. +.file "match.S"
  44264. +
  44265. +.globl match_init, longest_match
  44266. +
  44267. +.text
  44268. +
  44269. +/* uInt longest_match(deflate_state *deflatestate, IPos curmatch) */
  44270. +
  44271. +longest_match:
  44272. +
  44273. +/* Save registers that the compiler may be using, and adjust %esp to */
  44274. +/* make room for our stack frame. */
  44275. +
  44276. + pushl %ebp
  44277. + pushl %edi
  44278. + pushl %esi
  44279. + pushl %ebx
  44280. + subl $LocalVarsSize, %esp
  44281. +
  44282. +/* Retrieve the function arguments. %ecx will hold cur_match */
  44283. +/* throughout the entire function. %edx will hold the pointer to the */
  44284. +/* deflate_state structure during the function's setup (before */
  44285. +/* entering the main loop). */
  44286. +
  44287. + movl deflatestate(%esp), %edx
  44288. + movl curmatch(%esp), %ecx
  44289. +
  44290. +/* uInt wmask = s->w_mask; */
  44291. +/* unsigned chain_length = s->max_chain_length; */
  44292. +/* if (s->prev_length >= s->good_match) { */
  44293. +/* chain_length >>= 2; */
  44294. +/* } */
  44295. +
  44296. + movl dsPrevLen(%edx), %eax
  44297. + movl dsGoodMatch(%edx), %ebx
  44298. + cmpl %ebx, %eax
  44299. + movl dsWMask(%edx), %eax
  44300. + movl dsMaxChainLen(%edx), %ebx
  44301. + jl LastMatchGood
  44302. + shrl $2, %ebx
  44303. +LastMatchGood:
  44304. +
  44305. +/* chainlen is decremented once beforehand so that the function can */
  44306. +/* use the sign flag instead of the zero flag for the exit test. */
  44307. +/* It is then shifted into the high word, to make room for the wmask */
  44308. +/* value, which it will always accompany. */
  44309. +
  44310. + decl %ebx
  44311. + shll $16, %ebx
  44312. + orl %eax, %ebx
  44313. + movl %ebx, chainlenwmask(%esp)
  44314. +
  44315. +/* if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; */
  44316. +
  44317. + movl dsNiceMatch(%edx), %eax
  44318. + movl dsLookahead(%edx), %ebx
  44319. + cmpl %eax, %ebx
  44320. + jl LookaheadLess
  44321. + movl %eax, %ebx
  44322. +LookaheadLess: movl %ebx, nicematch(%esp)
  44323. +
  44324. +/* register Bytef *scan = s->window + s->strstart; */
  44325. +
  44326. + movl dsWindow(%edx), %esi
  44327. + movl %esi, window(%esp)
  44328. + movl dsStrStart(%edx), %ebp
  44329. + lea (%esi,%ebp), %edi
  44330. + movl %edi, scan(%esp)
  44331. +
  44332. +/* Determine how many bytes the scan ptr is off from being */
  44333. +/* dword-aligned. */
  44334. +
  44335. + movl %edi, %eax
  44336. + negl %eax
  44337. + andl $3, %eax
  44338. + movl %eax, scanalign(%esp)
  44339. +
  44340. +/* IPos limit = s->strstart > (IPos)MAX_DIST(s) ? */
  44341. +/* s->strstart - (IPos)MAX_DIST(s) : NIL; */
  44342. +
  44343. + movl dsWSize(%edx), %eax
  44344. + subl $MIN_LOOKAHEAD, %eax
  44345. + subl %eax, %ebp
  44346. + jg LimitPositive
  44347. + xorl %ebp, %ebp
  44348. +LimitPositive:
  44349. +
  44350. +/* int best_len = s->prev_length; */
  44351. +
  44352. + movl dsPrevLen(%edx), %eax
  44353. + movl %eax, bestlen(%esp)
  44354. +
  44355. +/* Store the sum of s->window + best_len in %esi locally, and in %esi. */
  44356. +
  44357. + addl %eax, %esi
  44358. + movl %esi, windowbestlen(%esp)
  44359. +
  44360. +/* register ush scan_start = *(ushf*)scan; */
  44361. +/* register ush scan_end = *(ushf*)(scan+best_len-1); */
  44362. +/* Posf *prev = s->prev; */
  44363. +
  44364. + movzwl (%edi), %ebx
  44365. + movl %ebx, scanstart(%esp)
  44366. + movzwl -1(%edi,%eax), %ebx
  44367. + movl %ebx, scanend(%esp)
  44368. + movl dsPrev(%edx), %edi
  44369. +
  44370. +/* Jump into the main loop. */
  44371. +
  44372. + movl chainlenwmask(%esp), %edx
  44373. + jmp LoopEntry
  44374. +
  44375. +.balign 16
  44376. +
  44377. +/* do {
  44378. + * match = s->window + cur_match;
  44379. + * if (*(ushf*)(match+best_len-1) != scan_end ||
  44380. + * *(ushf*)match != scan_start) continue;
  44381. + * [...]
  44382. + * } while ((cur_match = prev[cur_match & wmask]) > limit
  44383. + * && --chain_length != 0);
  44384. + *
  44385. + * Here is the inner loop of the function. The function will spend the
  44386. + * majority of its time in this loop, and majority of that time will
  44387. + * be spent in the first ten instructions.
  44388. + *
  44389. + * Within this loop:
  44390. + * %ebx = scanend
  44391. + * %ecx = curmatch
  44392. + * %edx = chainlenwmask - i.e., ((chainlen << 16) | wmask)
  44393. + * %esi = windowbestlen - i.e., (window + bestlen)
  44394. + * %edi = prev
  44395. + * %ebp = limit
  44396. + */
  44397. +LookupLoop:
  44398. + andl %edx, %ecx
  44399. + movzwl (%edi,%ecx,2), %ecx
  44400. + cmpl %ebp, %ecx
  44401. + jbe LeaveNow
  44402. + subl $0x00010000, %edx
  44403. + js LeaveNow
  44404. +LoopEntry: movzwl -1(%esi,%ecx), %eax
  44405. + cmpl %ebx, %eax
  44406. + jnz LookupLoop
  44407. + movl window(%esp), %eax
  44408. + movzwl (%eax,%ecx), %eax
  44409. + cmpl scanstart(%esp), %eax
  44410. + jnz LookupLoop
  44411. +
  44412. +/* Store the current value of chainlen. */
  44413. +
  44414. + movl %edx, chainlenwmask(%esp)
  44415. +
  44416. +/* Point %edi to the string under scrutiny, and %esi to the string we */
  44417. +/* are hoping to match it up with. In actuality, %esi and %edi are */
  44418. +/* both pointed (MAX_MATCH_8 - scanalign) bytes ahead, and %edx is */
  44419. +/* initialized to -(MAX_MATCH_8 - scanalign). */
  44420. +
  44421. + movl window(%esp), %esi
  44422. + movl scan(%esp), %edi
  44423. + addl %ecx, %esi
  44424. + movl scanalign(%esp), %eax
  44425. + movl $(-MAX_MATCH_8), %edx
  44426. + lea MAX_MATCH_8(%edi,%eax), %edi
  44427. + lea MAX_MATCH_8(%esi,%eax), %esi
  44428. +
  44429. +/* Test the strings for equality, 8 bytes at a time. At the end,
  44430. + * adjust %edx so that it is offset to the exact byte that mismatched.
  44431. + *
  44432. + * We already know at this point that the first three bytes of the
  44433. + * strings match each other, and they can be safely passed over before
  44434. + * starting the compare loop. So what this code does is skip over 0-3
  44435. + * bytes, as much as necessary in order to dword-align the %edi
  44436. + * pointer. (%esi will still be misaligned three times out of four.)
  44437. + *
  44438. + * It should be confessed that this loop usually does not represent
  44439. + * much of the total running time. Replacing it with a more
  44440. + * straightforward "rep cmpsb" would not drastically degrade
  44441. + * performance.
  44442. + */
  44443. +LoopCmps:
  44444. + movl (%esi,%edx), %eax
  44445. + xorl (%edi,%edx), %eax
  44446. + jnz LeaveLoopCmps
  44447. + movl 4(%esi,%edx), %eax
  44448. + xorl 4(%edi,%edx), %eax
  44449. + jnz LeaveLoopCmps4
  44450. + addl $8, %edx
  44451. + jnz LoopCmps
  44452. + jmp LenMaximum
  44453. +LeaveLoopCmps4: addl $4, %edx
  44454. +LeaveLoopCmps: testl $0x0000FFFF, %eax
  44455. + jnz LenLower
  44456. + addl $2, %edx
  44457. + shrl $16, %eax
  44458. +LenLower: subb $1, %al
  44459. + adcl $0, %edx
  44460. +
  44461. +/* Calculate the length of the match. If it is longer than MAX_MATCH, */
  44462. +/* then automatically accept it as the best possible match and leave. */
  44463. +
  44464. + lea (%edi,%edx), %eax
  44465. + movl scan(%esp), %edi
  44466. + subl %edi, %eax
  44467. + cmpl $MAX_MATCH, %eax
  44468. + jge LenMaximum
  44469. +
  44470. +/* If the length of the match is not longer than the best match we */
  44471. +/* have so far, then forget it and return to the lookup loop. */
  44472. +
  44473. + movl deflatestate(%esp), %edx
  44474. + movl bestlen(%esp), %ebx
  44475. + cmpl %ebx, %eax
  44476. + jg LongerMatch
  44477. + movl windowbestlen(%esp), %esi
  44478. + movl dsPrev(%edx), %edi
  44479. + movl scanend(%esp), %ebx
  44480. + movl chainlenwmask(%esp), %edx
  44481. + jmp LookupLoop
  44482. +
  44483. +/* s->match_start = cur_match; */
  44484. +/* best_len = len; */
  44485. +/* if (len >= nice_match) break; */
  44486. +/* scan_end = *(ushf*)(scan+best_len-1); */
  44487. +
  44488. +LongerMatch: movl nicematch(%esp), %ebx
  44489. + movl %eax, bestlen(%esp)
  44490. + movl %ecx, dsMatchStart(%edx)
  44491. + cmpl %ebx, %eax
  44492. + jge LeaveNow
  44493. + movl window(%esp), %esi
  44494. + addl %eax, %esi
  44495. + movl %esi, windowbestlen(%esp)
  44496. + movzwl -1(%edi,%eax), %ebx
  44497. + movl dsPrev(%edx), %edi
  44498. + movl %ebx, scanend(%esp)
  44499. + movl chainlenwmask(%esp), %edx
  44500. + jmp LookupLoop
  44501. +
  44502. +/* Accept the current string, with the maximum possible length. */
  44503. +
  44504. +LenMaximum: movl deflatestate(%esp), %edx
  44505. + movl $MAX_MATCH, bestlen(%esp)
  44506. + movl %ecx, dsMatchStart(%edx)
  44507. +
  44508. +/* if ((uInt)best_len <= s->lookahead) return (uInt)best_len; */
  44509. +/* return s->lookahead; */
  44510. +
  44511. +LeaveNow:
  44512. + movl deflatestate(%esp), %edx
  44513. + movl bestlen(%esp), %ebx
  44514. + movl dsLookahead(%edx), %eax
  44515. + cmpl %eax, %ebx
  44516. + jg LookaheadRet
  44517. + movl %ebx, %eax
  44518. +LookaheadRet:
  44519. +
  44520. +/* Restore the stack and return from whence we came. */
  44521. +
  44522. + addl $LocalVarsSize, %esp
  44523. + popl %ebx
  44524. + popl %esi
  44525. + popl %edi
  44526. + popl %ebp
  44527. +match_init: ret
  44528. --- /dev/null Tue Mar 11 13:02:56 2003
  44529. +++ linux/net/ipsec/null/ipsec_alg_null.c Mon Feb 9 13:51:03 2004
  44530. @@ -0,0 +1,145 @@
  44531. +/*
  44532. + * ipsec_alg NULL cipher stubs
  44533. + *
  44534. + * Author: JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
  44535. + *
  44536. + * $Id: ipsec_alg_null.c,v 1.1.2.1 2006/10/11 18:14:33 paul Exp $
  44537. + *
  44538. + * This program is free software; you can redistribute it and/or modify it
  44539. + * under the terms of the GNU General Public License as published by the
  44540. + * Free Software Foundation; either version 2 of the License, or (at your
  44541. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  44542. + *
  44543. + * This program is distributed in the hope that it will be useful, but
  44544. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  44545. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  44546. + * for more details.
  44547. + *
  44548. + */
  44549. +#include <linux/config.h>
  44550. +#include <linux/version.h>
  44551. +
  44552. +/*
  44553. + * special case: ipsec core modular with this static algo inside:
  44554. + * must avoid MODULE magic for this file
  44555. + */
  44556. +#if defined(CONFIG_KLIPS_MODULE) && defined(CONFIG_KLIPS_ENC_NULL)
  44557. +#undef MODULE
  44558. +#endif
  44559. +
  44560. +#include <linux/module.h>
  44561. +#include <linux/init.h>
  44562. +
  44563. +#include <linux/kernel.h> /* printk() */
  44564. +#include <linux/errno.h> /* error codes */
  44565. +#include <linux/types.h> /* size_t */
  44566. +#include <linux/string.h>
  44567. +
  44568. +/* Check if __exit is defined, if not null it */
  44569. +#ifndef __exit
  44570. +#define __exit
  44571. +#endif
  44572. +
  44573. +/* Low freeswan header coupling */
  44574. +#include "openswan/ipsec_alg.h"
  44575. +
  44576. +#define ESP_NULL 11 /* from ipsec drafts */
  44577. +#define ESP_NULL_BLK_LEN 1
  44578. +
  44579. +MODULE_AUTHOR("JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>");
  44580. +static int debug_null=0;
  44581. +static int test_null=0;
  44582. +#ifdef module_param
  44583. +module_param(debug_null, int, 0600);
  44584. +module_param(test_null, int, 0600);
  44585. +#else
  44586. +MODULE_PARM(debug_null, "i");
  44587. +MODULE_PARM(test_null, "i");
  44588. +#endif
  44589. +
  44590. +typedef int null_context;
  44591. +
  44592. +struct null_eks{
  44593. + null_context null_ctx;
  44594. +};
  44595. +static int _null_set_key(struct ipsec_alg_enc *alg,
  44596. + __u8 * key_e, const __u8 * key,
  44597. + size_t keysize) {
  44598. + null_context *ctx=&((struct null_eks*)key_e)->null_ctx;
  44599. + if (debug_null > 0)
  44600. + printk(KERN_DEBUG "klips_debug:_null_set_key:"
  44601. + "key_e=%p key=%p keysize=%d\n",
  44602. + key_e, key, keysize);
  44603. + *ctx = 1;
  44604. + return 0;
  44605. +}
  44606. +static int _null_cbc_encrypt(struct ipsec_alg_enc *alg,
  44607. + __u8 * key_e, __u8 * in, int ilen, const __u8 * iv,
  44608. + int encrypt) {
  44609. + null_context *ctx=&((struct null_eks*)key_e)->null_ctx;
  44610. + if (debug_null > 0)
  44611. + printk(KERN_DEBUG "klips_debug:_null_cbc_encrypt:"
  44612. + "key_e=%p in=%p ilen=%d iv=%p encrypt=%d\n",
  44613. + key_e, in, ilen, iv, encrypt);
  44614. + (*ctx)++;
  44615. + return ilen;
  44616. +}
  44617. +static struct ipsec_alg_enc ipsec_alg_NULL = {
  44618. + ixt_common: { ixt_version: IPSEC_ALG_VERSION,
  44619. + ixt_refcnt: ATOMIC_INIT(0),
  44620. + ixt_name: "null",
  44621. + ixt_blocksize: ESP_NULL_BLK_LEN,
  44622. + ixt_support: {
  44623. + ias_exttype: IPSEC_ALG_TYPE_ENCRYPT,
  44624. + ias_id: ESP_NULL,
  44625. + ias_ivlen: 0,
  44626. + ias_keyminbits: 0,
  44627. + ias_keymaxbits: 0,
  44628. + },
  44629. + },
  44630. +#if defined(CONFIG_KLIPS_ENC_NULL_MODULE)
  44631. + ixt_module: THIS_MODULE,
  44632. +#endif
  44633. + ixt_e_keylen: 0,
  44634. + ixt_e_ctx_size: sizeof(null_context),
  44635. + ixt_e_set_key: _null_set_key,
  44636. + ixt_e_cbc_encrypt:_null_cbc_encrypt,
  44637. +};
  44638. +
  44639. +#if defined(CONFIG_KLIPS_ENC_NULL_MODULE)
  44640. +IPSEC_ALG_MODULE_INIT_MOD( ipsec_null_init )
  44641. +#else
  44642. +IPSEC_ALG_MODULE_INIT_STATIC( ipsec_null_init )
  44643. +#endif
  44644. +{
  44645. + int ret, test_ret;
  44646. + ret=register_ipsec_alg_enc(&ipsec_alg_NULL);
  44647. + printk("ipsec_null_init(alg_type=%d alg_id=%d name=%s): ret=%d\n",
  44648. + ipsec_alg_NULL.ixt_common.ixt_support.ias_exttype,
  44649. + ipsec_alg_NULL.ixt_common.ixt_support.ias_id,
  44650. + ipsec_alg_NULL.ixt_common.ixt_name,
  44651. + ret);
  44652. + if (ret==0 && test_null) {
  44653. + test_ret=ipsec_alg_test(
  44654. + ipsec_alg_NULL.ixt_common.ixt_support.ias_exttype,
  44655. + ipsec_alg_NULL.ixt_common.ixt_support.ias_id,
  44656. + test_null);
  44657. + printk("ipsec_null_init(alg_type=%d alg_id=%d): test_ret=%d\n",
  44658. + ipsec_alg_NULL.ixt_common.ixt_support.ias_exttype,
  44659. + ipsec_alg_NULL.ixt_common.ixt_support.ias_id,
  44660. + test_ret);
  44661. + }
  44662. + return ret;
  44663. +}
  44664. +#if defined(CONFIG_KLIPS_ENC_NULL_MODULE)
  44665. +IPSEC_ALG_MODULE_EXIT_MOD( ipsec_null_fini )
  44666. +#else
  44667. +IPSEC_ALG_MODULE_EXIT_STATIC( ipsec_null_fini )
  44668. +#endif
  44669. +{
  44670. + unregister_ipsec_alg_enc(&ipsec_alg_NULL);
  44671. + return;
  44672. +}
  44673. +#ifdef MODULE_LICENSE
  44674. +MODULE_LICENSE("GPL");
  44675. +#endif
  44676. --- /dev/null Tue Mar 11 13:02:56 2003
  44677. +++ linux/net/ipsec/pfkey_v2.c Mon Feb 9 13:51:03 2004
  44678. @@ -0,0 +1,2022 @@
  44679. +/*
  44680. + * @(#) RFC2367 PF_KEYv2 Key management API domain socket I/F
  44681. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs.
  44682. + *
  44683. + * This program is free software; you can redistribute it and/or modify it
  44684. + * under the terms of the GNU General Public License as published by the
  44685. + * Free Software Foundation; either version 2 of the License, or (at your
  44686. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  44687. + *
  44688. + * This program is distributed in the hope that it will be useful, but
  44689. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  44690. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  44691. + * for more details.
  44692. + *
  44693. + * RCSID $Id: pfkey_v2.c,v 1.97.2.12 2006/11/24 05:43:29 paul Exp $
  44694. + */
  44695. +
  44696. +/*
  44697. + * Template from /usr/src/linux-2.0.36/net/unix/af_unix.c.
  44698. + * Hints from /usr/src/linux-2.0.36/net/ipv4/udp.c.
  44699. + */
  44700. +
  44701. +#define __NO_VERSION__
  44702. +#include <linux/module.h>
  44703. +#include <linux/version.h>
  44704. +#ifndef AUTOCONF_INCLUDED
  44705. +#include <linux/config.h>
  44706. +#endif
  44707. +#include <linux/kernel.h>
  44708. +
  44709. +#include "openswan/ipsec_param.h"
  44710. +
  44711. +#include <linux/major.h>
  44712. +#include <linux/signal.h>
  44713. +#include <linux/sched.h>
  44714. +#include <linux/errno.h>
  44715. +#include <linux/string.h>
  44716. +#include <linux/stat.h>
  44717. +#include <linux/socket.h>
  44718. +#include <linux/un.h>
  44719. +#include <linux/fcntl.h>
  44720. +#include <linux/termios.h>
  44721. +#include <linux/socket.h>
  44722. +#include <linux/sockios.h>
  44723. +#include <linux/net.h> /* struct socket */
  44724. +#include <linux/in.h>
  44725. +#include <linux/fs.h>
  44726. +#ifdef MALLOC_SLAB
  44727. +# include <linux/slab.h> /* kmalloc() */
  44728. +#else /* MALLOC_SLAB */
  44729. +# include <linux/malloc.h> /* kmalloc() */
  44730. +#endif /* MALLOC_SLAB */
  44731. +#include <asm/segment.h>
  44732. +#include <linux/skbuff.h>
  44733. +#include <linux/netdevice.h>
  44734. +#include <net/sock.h> /* struct sock */
  44735. +#include <net/protocol.h>
  44736. +/* #include <net/tcp.h> */
  44737. +#include <net/af_unix.h>
  44738. +#ifdef CONFIG_PROC_FS
  44739. +# include <linux/proc_fs.h>
  44740. +#endif /* CONFIG_PROC_FS */
  44741. +
  44742. +#include <linux/types.h>
  44743. +
  44744. +#include <openswan.h>
  44745. +
  44746. +#include "openswan/radij.h"
  44747. +#include "openswan/ipsec_encap.h"
  44748. +#include "openswan/ipsec_sa.h"
  44749. +
  44750. +#include <pfkeyv2.h>
  44751. +#include <pfkey.h>
  44752. +
  44753. +#include "openswan/ipsec_proto.h"
  44754. +#include "openswan/ipsec_kern24.h"
  44755. +
  44756. +#ifdef CONFIG_KLIPS_DEBUG
  44757. +int debug_pfkey = 0;
  44758. +extern int sysctl_ipsec_debug_verbose;
  44759. +#endif /* CONFIG_KLIPS_DEBUG */
  44760. +
  44761. +#define SENDERR(_x) do { error = -(_x); goto errlab; } while (0)
  44762. +
  44763. +#ifndef SOCKOPS_WRAPPED
  44764. +#define SOCKOPS_WRAPPED(name) name
  44765. +#endif /* SOCKOPS_WRAPPED */
  44766. +
  44767. +#ifdef NET_26
  44768. +static rwlock_t pfkey_sock_lock = RW_LOCK_UNLOCKED;
  44769. +HLIST_HEAD(pfkey_sock_list);
  44770. +static DECLARE_WAIT_QUEUE_HEAD(pfkey_sock_wait);
  44771. +static atomic_t pfkey_sock_users = ATOMIC_INIT(0);
  44772. +#else
  44773. +struct sock *pfkey_sock_list = NULL;
  44774. +#endif
  44775. +
  44776. +struct supported_list *pfkey_supported_list[SADB_SATYPE_MAX+1];
  44777. +
  44778. +struct socket_list *pfkey_open_sockets = NULL;
  44779. +struct socket_list *pfkey_registered_sockets[SADB_SATYPE_MAX+1];
  44780. +
  44781. +int pfkey_msg_interp(struct sock *, struct sadb_msg *, struct sadb_msg **);
  44782. +
  44783. +DEBUG_NO_STATIC int pfkey_create(struct socket *sock, int protocol);
  44784. +DEBUG_NO_STATIC int pfkey_shutdown(struct socket *sock, int mode);
  44785. +DEBUG_NO_STATIC int pfkey_release(struct socket *sock);
  44786. +
  44787. +#ifdef NET_26
  44788. +DEBUG_NO_STATIC int pfkey_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len);
  44789. +DEBUG_NO_STATIC int pfkey_recvmsg(struct kiocb *kiocb, struct socket *sock, struct msghdr *msg
  44790. + , size_t size, int flags);
  44791. +#else
  44792. +DEBUG_NO_STATIC int pfkey_sendmsg(struct socket *sock, struct msghdr *msg, int len, struct scm_cookie *scm);
  44793. +DEBUG_NO_STATIC int pfkey_recvmsg(struct socket *sock, struct msghdr *msg, int size, int flags, struct scm_cookie *scm);
  44794. +#endif
  44795. +
  44796. +struct net_proto_family pfkey_family_ops = {
  44797. +#ifdef NETDEV_23
  44798. + .family = PF_KEY,
  44799. + .create = pfkey_create,
  44800. +#ifdef NET_26
  44801. + .owner = THIS_MODULE,
  44802. +#endif
  44803. +#else
  44804. + PF_KEY,
  44805. + pfkey_create
  44806. +#endif
  44807. +};
  44808. +
  44809. +struct proto_ops SOCKOPS_WRAPPED(pfkey_ops) = {
  44810. +#ifdef NETDEV_23
  44811. + family: PF_KEY,
  44812. +#ifdef NET_26
  44813. + owner: THIS_MODULE,
  44814. +#endif
  44815. + release: pfkey_release,
  44816. + bind: sock_no_bind,
  44817. + connect: sock_no_connect,
  44818. + socketpair: sock_no_socketpair,
  44819. + accept: sock_no_accept,
  44820. + getname: sock_no_getname,
  44821. + poll: datagram_poll,
  44822. + ioctl: sock_no_ioctl,
  44823. + listen: sock_no_listen,
  44824. + shutdown: pfkey_shutdown,
  44825. + setsockopt: sock_no_setsockopt,
  44826. + getsockopt: sock_no_getsockopt,
  44827. + sendmsg: pfkey_sendmsg,
  44828. + recvmsg: pfkey_recvmsg,
  44829. + mmap: sock_no_mmap,
  44830. +#else /* NETDEV_23 */
  44831. + PF_KEY,
  44832. + sock_no_dup,
  44833. + pfkey_release,
  44834. + sock_no_bind,
  44835. + sock_no_connect,
  44836. + sock_no_socketpair,
  44837. + sock_no_accept,
  44838. + sock_no_getname,
  44839. + datagram_poll,
  44840. + sock_no_ioctl,
  44841. + sock_no_listen,
  44842. + pfkey_shutdown,
  44843. + sock_no_setsockopt,
  44844. + sock_no_getsockopt,
  44845. + sock_no_fcntl,
  44846. + pfkey_sendmsg,
  44847. + pfkey_recvmsg
  44848. +#endif /* NETDEV_23 */
  44849. +};
  44850. +
  44851. +#ifdef NETDEV_23
  44852. +#include <linux/smp_lock.h>
  44853. +SOCKOPS_WRAP(pfkey, PF_KEY);
  44854. +#endif /* NETDEV_23 */
  44855. +
  44856. +#ifdef NET_26
  44857. +static void pfkey_sock_list_grab(void)
  44858. +{
  44859. + write_lock_bh(&pfkey_sock_lock);
  44860. +
  44861. + if (atomic_read(&pfkey_sock_users)) {
  44862. + DECLARE_WAITQUEUE(wait, current);
  44863. +
  44864. + add_wait_queue_exclusive(&pfkey_sock_wait, &wait);
  44865. + for(;;) {
  44866. + set_current_state(TASK_UNINTERRUPTIBLE);
  44867. + if (atomic_read(&pfkey_sock_users) == 0)
  44868. + break;
  44869. + write_unlock_bh(&pfkey_sock_lock);
  44870. + schedule();
  44871. + write_lock_bh(&pfkey_sock_lock);
  44872. + }
  44873. +
  44874. + __set_current_state(TASK_RUNNING);
  44875. + remove_wait_queue(&pfkey_sock_wait, &wait);
  44876. + }
  44877. +}
  44878. +
  44879. +static __inline__ void pfkey_sock_list_ungrab(void)
  44880. +{
  44881. + write_unlock_bh(&pfkey_sock_lock);
  44882. + wake_up(&pfkey_sock_wait);
  44883. +}
  44884. +
  44885. +static __inline__ void pfkey_lock_sock_list(void)
  44886. +{
  44887. + /* read_lock() synchronizes us to pfkey_table_grab */
  44888. +
  44889. + read_lock(&pfkey_sock_lock);
  44890. + atomic_inc(&pfkey_sock_users);
  44891. + read_unlock(&pfkey_sock_lock);
  44892. +}
  44893. +
  44894. +static __inline__ void pfkey_unlock_sock_list(void)
  44895. +{
  44896. + if (atomic_dec_and_test(&pfkey_sock_users))
  44897. + wake_up(&pfkey_sock_wait);
  44898. +}
  44899. +#endif
  44900. +
  44901. +int
  44902. +pfkey_list_remove_socket(struct socket *socketp, struct socket_list **sockets)
  44903. +{
  44904. + struct socket_list *socket_listp,*prev;
  44905. +
  44906. + if(!socketp) {
  44907. + KLIPS_PRINT(debug_pfkey,
  44908. + "klips_debug:pfkey_list_remove_socket: "
  44909. + "NULL socketp handed in, failed.\n");
  44910. + return -EINVAL;
  44911. + }
  44912. +
  44913. + if(!sockets) {
  44914. + KLIPS_PRINT(debug_pfkey,
  44915. + "klips_debug:pfkey_list_remove_socket: "
  44916. + "NULL sockets list handed in, failed.\n");
  44917. + return -EINVAL;
  44918. + }
  44919. +
  44920. + socket_listp = *sockets;
  44921. + prev = NULL;
  44922. +
  44923. + KLIPS_PRINT(debug_pfkey,
  44924. + "klips_debug:pfkey_list_remove_socket: "
  44925. + "removing sock=0p%p\n",
  44926. + socketp);
  44927. +
  44928. + while(socket_listp != NULL) {
  44929. + if(socket_listp->socketp == socketp) {
  44930. + if(prev != NULL) {
  44931. + prev->next = socket_listp->next;
  44932. + } else {
  44933. + *sockets = socket_listp->next;
  44934. + }
  44935. +
  44936. + kfree((void*)socket_listp);
  44937. +
  44938. + break;
  44939. + }
  44940. + prev = socket_listp;
  44941. + socket_listp = socket_listp->next;
  44942. + }
  44943. +
  44944. + return 0;
  44945. +}
  44946. +
  44947. +int
  44948. +pfkey_list_insert_socket(struct socket *socketp, struct socket_list **sockets)
  44949. +{
  44950. + struct socket_list *socket_listp;
  44951. +
  44952. + if(!socketp) {
  44953. + KLIPS_PRINT(debug_pfkey,
  44954. + "klips_debug:pfkey_list_insert_socket: "
  44955. + "NULL socketp handed in, failed.\n");
  44956. + return -EINVAL;
  44957. + }
  44958. +
  44959. + if(!sockets) {
  44960. + KLIPS_PRINT(debug_pfkey,
  44961. + "klips_debug:pfkey_list_insert_socket: "
  44962. + "NULL sockets list handed in, failed.\n");
  44963. + return -EINVAL;
  44964. + }
  44965. +
  44966. + KLIPS_PRINT(debug_pfkey,
  44967. + "klips_debug:pfkey_list_insert_socket: "
  44968. + "allocating %lu bytes for socketp=0p%p\n",
  44969. + (unsigned long) sizeof(struct socket_list),
  44970. + socketp);
  44971. +
  44972. + if((socket_listp = (struct socket_list *)kmalloc(sizeof(struct socket_list), GFP_KERNEL)) == NULL) {
  44973. + KLIPS_PRINT(debug_pfkey,
  44974. + "klips_debug:pfkey_list_insert_socket: "
  44975. + "memory allocation error.\n");
  44976. + return -ENOMEM;
  44977. + }
  44978. +
  44979. + socket_listp->socketp = socketp;
  44980. + socket_listp->next = *sockets;
  44981. + *sockets = socket_listp;
  44982. +
  44983. + return 0;
  44984. +}
  44985. +
  44986. +int
  44987. +pfkey_list_remove_supported(struct ipsec_alg_supported *supported, struct supported_list **supported_list)
  44988. +{
  44989. + struct supported_list *supported_listp = *supported_list, *prev = NULL;
  44990. +
  44991. + if(!supported) {
  44992. + KLIPS_PRINT(debug_pfkey,
  44993. + "klips_debug:pfkey_list_remove_supported: "
  44994. + "NULL supported handed in, failed.\n");
  44995. + return -EINVAL;
  44996. + }
  44997. +
  44998. + if(!supported_list) {
  44999. + KLIPS_PRINT(debug_pfkey,
  45000. + "klips_debug:pfkey_list_remove_supported: "
  45001. + "NULL supported_list handed in, failed.\n");
  45002. + return -EINVAL;
  45003. + }
  45004. +
  45005. + KLIPS_PRINT(debug_pfkey,
  45006. + "klips_debug:pfkey_list_remove_supported: "
  45007. + "removing supported=0p%p\n",
  45008. + supported);
  45009. +
  45010. + while(supported_listp != NULL) {
  45011. + if(supported_listp->supportedp == supported) {
  45012. + if(prev != NULL) {
  45013. + prev->next = supported_listp->next;
  45014. + } else {
  45015. + *supported_list = supported_listp->next;
  45016. + }
  45017. +
  45018. + kfree((void*)supported_listp);
  45019. +
  45020. + break;
  45021. + }
  45022. + prev = supported_listp;
  45023. + supported_listp = supported_listp->next;
  45024. + }
  45025. +
  45026. + return 0;
  45027. +}
  45028. +
  45029. +int
  45030. +pfkey_list_insert_supported(struct ipsec_alg_supported *supported
  45031. + , struct supported_list **supported_list)
  45032. +{
  45033. + struct supported_list *supported_listp;
  45034. +
  45035. + if(!supported) {
  45036. + KLIPS_PRINT(debug_pfkey,
  45037. + "klips_debug:pfkey_list_insert_supported: "
  45038. + "NULL supported handed in, failed.\n");
  45039. + return -EINVAL;
  45040. + }
  45041. +
  45042. + if(!supported_list) {
  45043. + KLIPS_PRINT(debug_pfkey,
  45044. + "klips_debug:pfkey_list_insert_supported: "
  45045. + "NULL supported_list handed in, failed.\n");
  45046. + return -EINVAL;
  45047. + }
  45048. +
  45049. + KLIPS_PRINT(debug_pfkey,
  45050. + "klips_debug:pfkey_list_insert_supported: "
  45051. + "allocating %lu bytes for incoming, supported=0p%p, supported_list=0p%p\n",
  45052. + (unsigned long) sizeof(struct supported_list),
  45053. + supported,
  45054. + supported_list);
  45055. +
  45056. + supported_listp = (struct supported_list *)kmalloc(sizeof(struct supported_list), GFP_KERNEL);
  45057. +
  45058. + if(supported_listp == NULL)
  45059. + {
  45060. + KLIPS_PRINT(debug_pfkey,
  45061. + "klips_debug:pfkey_list_insert_supported: "
  45062. + "memory allocation error.\n");
  45063. + return -ENOMEM;
  45064. + }
  45065. +
  45066. + supported_listp->supportedp = supported;
  45067. + supported_listp->next = *supported_list;
  45068. + *supported_list = supported_listp;
  45069. + KLIPS_PRINT(debug_pfkey,
  45070. + "klips_debug:pfkey_list_insert_supported: "
  45071. + "outgoing, supported=0p%p, supported_list=0p%p\n",
  45072. + supported,
  45073. + supported_list);
  45074. +
  45075. + return 0;
  45076. +}
  45077. +
  45078. +#ifdef NET_26
  45079. +DEBUG_NO_STATIC void
  45080. +pfkey_insert_socket(struct sock *sk)
  45081. +{
  45082. + KLIPS_PRINT(debug_pfkey,
  45083. + "klips_debug:pfkey_insert_socket: "
  45084. + "sk=0p%p\n",
  45085. + sk);
  45086. + pfkey_sock_list_grab();
  45087. + sk_add_node(sk, &pfkey_sock_list);
  45088. + pfkey_sock_list_ungrab();
  45089. +}
  45090. +
  45091. +DEBUG_NO_STATIC void
  45092. +pfkey_remove_socket(struct sock *sk)
  45093. +{
  45094. + KLIPS_PRINT(debug_pfkey,
  45095. + "klips_debug:pfkey_remove_socket: 0p%p\n", sk);
  45096. + pfkey_sock_list_grab();
  45097. + sk_del_node_init(sk);
  45098. + pfkey_sock_list_ungrab();
  45099. + return;
  45100. +}
  45101. +#else
  45102. +
  45103. +DEBUG_NO_STATIC void
  45104. +pfkey_insert_socket(struct sock *sk)
  45105. +{
  45106. + KLIPS_PRINT(debug_pfkey,
  45107. + "klips_debug:pfkey_insert_socket: "
  45108. + "sk=0p%p\n",
  45109. + sk);
  45110. + cli();
  45111. + sk->next=pfkey_sock_list;
  45112. + pfkey_sock_list=sk;
  45113. + sti();
  45114. +}
  45115. +DEBUG_NO_STATIC void
  45116. +pfkey_remove_socket(struct sock *sk)
  45117. +{
  45118. + struct sock **s;
  45119. +
  45120. + s = NULL;
  45121. + KLIPS_PRINT(debug_pfkey,
  45122. + "klips_debug:pfkey_remove_socket: .\n");
  45123. +
  45124. + cli();
  45125. + s=&pfkey_sock_list;
  45126. +
  45127. + while(*s!=NULL) {
  45128. + if(*s==sk) {
  45129. + *s=sk->next;
  45130. + sk->next=NULL;
  45131. + sti();
  45132. + KLIPS_PRINT(debug_pfkey,
  45133. + "klips_debug:pfkey_remove_socket: "
  45134. + "succeeded.\n");
  45135. + return;
  45136. + }
  45137. + s=&((*s)->next);
  45138. + }
  45139. + sti();
  45140. +
  45141. + KLIPS_PRINT(debug_pfkey,
  45142. + "klips_debug:pfkey_remove_socket: "
  45143. + "not found.\n");
  45144. + return;
  45145. +}
  45146. +#endif
  45147. +
  45148. +DEBUG_NO_STATIC void
  45149. +pfkey_destroy_socket(struct sock *sk)
  45150. +{
  45151. + struct sk_buff *skb;
  45152. +
  45153. + KLIPS_PRINT(debug_pfkey,
  45154. + "klips_debug:pfkey_destroy_socket: 0p%p\n",sk);
  45155. + pfkey_remove_socket(sk);
  45156. +
  45157. + KLIPS_PRINT(debug_pfkey,
  45158. + "klips_debug:pfkey_destroy_socket: "
  45159. + "pfkey_remove_socket called, sk=0p%p\n",sk);
  45160. +
  45161. + KLIPS_PRINT(debug_pfkey,
  45162. + "klips_debug:pfkey_destroy_socket: "
  45163. + "sk(0p%p)->(&0p%p)receive_queue.{next=0p%p,prev=0p%p}.\n",
  45164. + sk,
  45165. + &(sk->sk_receive_queue),
  45166. + sk->sk_receive_queue.next,
  45167. + sk->sk_receive_queue.prev);
  45168. +
  45169. + while(sk && ((skb=skb_dequeue(&(sk->sk_receive_queue)))!=NULL)) {
  45170. +#ifdef CONFIG_KLIPS_DEBUG
  45171. + if(debug_pfkey && sysctl_ipsec_debug_verbose) {
  45172. + KLIPS_PRINT(debug_pfkey,
  45173. + "klips_debug:pfkey_destroy_socket: "
  45174. + "skb=0p%p dequeued.\n", skb);
  45175. + printk(KERN_INFO "klips_debug:pfkey_destroy_socket: "
  45176. + "pfkey_skb contents:");
  45177. + printk(" next:0p%p", skb->next);
  45178. + printk(" prev:0p%p", skb->prev);
  45179. + printk(" sk:0p%p", skb->sk);
  45180. + printk(" dev:0p%p", skb->dev);
  45181. + if(skb->dev) {
  45182. + if(skb->dev->name) {
  45183. + printk(" dev->name:%s", skb->dev->name);
  45184. + } else {
  45185. + printk(" dev->name:NULL?");
  45186. + }
  45187. + } else {
  45188. + printk(" dev:NULL");
  45189. + }
  45190. + printk(" h:0p%p", skb->h.raw);
  45191. + printk(" nh:0p%p", skb->nh.raw);
  45192. + printk(" mac:0p%p", skb->mac.raw);
  45193. + printk(" dst:0p%p", skb->dst);
  45194. + if(sysctl_ipsec_debug_verbose) {
  45195. + int i;
  45196. +
  45197. + printk(" cb");
  45198. + for(i=0; i<48; i++) {
  45199. + printk(":%2x", skb->cb[i]);
  45200. + }
  45201. + }
  45202. + printk(" len:%d", skb->len);
  45203. + printk(" csum:%d", skb->csum);
  45204. +#ifndef NETDEV_23
  45205. + printk(" used:%d", skb->used);
  45206. + printk(" is_clone:%d", skb->is_clone);
  45207. +#endif /* NETDEV_23 */
  45208. + printk(" cloned:%d", skb->cloned);
  45209. + printk(" pkt_type:%d", skb->pkt_type);
  45210. + printk(" ip_summed:%d", skb->ip_summed);
  45211. + printk(" priority:%d", skb->priority);
  45212. + printk(" protocol:%d", skb->protocol);
  45213. +#ifdef HAVE_SOCK_SECURITY
  45214. + printk(" security:%d", skb->security);
  45215. +#endif
  45216. + printk(" truesize:%d", skb->truesize);
  45217. + printk(" head:0p%p", skb->head);
  45218. + printk(" data:0p%p", skb->data);
  45219. + printk(" tail:0p%p", skb->tail);
  45220. + printk(" end:0p%p", skb->end);
  45221. + if(sysctl_ipsec_debug_verbose) {
  45222. + unsigned char* i;
  45223. + printk(" data");
  45224. + for(i = skb->head; i < skb->end; i++) {
  45225. + printk(":%2x", (unsigned char)(*(i)));
  45226. + }
  45227. + }
  45228. + printk(" destructor:0p%p", skb->destructor);
  45229. + printk("\n");
  45230. + }
  45231. +#endif /* CONFIG_KLIPS_DEBUG */
  45232. + KLIPS_PRINT(debug_pfkey,
  45233. + "klips_debug:pfkey_destroy_socket: "
  45234. + "skb=0p%p freed.\n",
  45235. + skb);
  45236. + ipsec_kfree_skb(skb);
  45237. + }
  45238. +
  45239. +#ifdef NET_26
  45240. + sock_set_flag(sk, SOCK_DEAD);
  45241. +#else
  45242. + sk->dead = 1;
  45243. +#endif
  45244. + sk_free(sk);
  45245. +
  45246. + KLIPS_PRINT(debug_pfkey,
  45247. + "klips_debug:pfkey_destroy_socket: destroyed.\n");
  45248. +}
  45249. +
  45250. +int
  45251. +pfkey_upmsg(struct socket *sock, struct sadb_msg *pfkey_msg)
  45252. +{
  45253. + int error = 0;
  45254. + struct sk_buff * skb = NULL;
  45255. + struct sock *sk;
  45256. +
  45257. + if(sock == NULL) {
  45258. + KLIPS_PRINT(debug_pfkey,
  45259. + "klips_debug:pfkey_upmsg: "
  45260. + "NULL socket passed in.\n");
  45261. + return -EINVAL;
  45262. + }
  45263. +
  45264. + if(pfkey_msg == NULL) {
  45265. + KLIPS_PRINT(debug_pfkey,
  45266. + "klips_debug:pfkey_upmsg: "
  45267. + "NULL pfkey_msg passed in.\n");
  45268. + return -EINVAL;
  45269. + }
  45270. +
  45271. + sk = sock->sk;
  45272. +
  45273. + if(sk == NULL) {
  45274. + KLIPS_PRINT(debug_pfkey,
  45275. + "klips_debug:pfkey_upmsg: "
  45276. + "NULL sock passed in.\n");
  45277. + return -EINVAL;
  45278. + }
  45279. +
  45280. + KLIPS_PRINT(debug_pfkey,
  45281. + "klips_debug:pfkey_upmsg: "
  45282. + "allocating %d bytes...\n",
  45283. + (int)(pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN));
  45284. + if(!(skb = alloc_skb(pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN, GFP_ATOMIC) )) {
  45285. + KLIPS_PRINT(debug_pfkey,
  45286. + "klips_debug:pfkey_upmsg: "
  45287. + "no buffers left to send up a message.\n");
  45288. + return -ENOBUFS;
  45289. + }
  45290. + KLIPS_PRINT(debug_pfkey,
  45291. + "klips_debug:pfkey_upmsg: "
  45292. + "...allocated at 0p%p.\n",
  45293. + skb);
  45294. +
  45295. + skb->dev = NULL;
  45296. +
  45297. + if(skb_tailroom(skb) < pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN) {
  45298. + printk(KERN_WARNING "klips_error:pfkey_upmsg: "
  45299. + "tried to skb_put %ld, %d available. This should never happen, please report.\n",
  45300. + (unsigned long int)pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN,
  45301. + skb_tailroom(skb));
  45302. + ipsec_kfree_skb(skb);
  45303. + return -ENOBUFS;
  45304. + }
  45305. + skb->h.raw = skb_put(skb, pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN);
  45306. + memcpy(skb->h.raw, pfkey_msg, pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN);
  45307. +
  45308. + if((error = sock_queue_rcv_skb(sk, skb)) < 0) {
  45309. + skb->sk=NULL;
  45310. + KLIPS_PRINT(debug_pfkey,
  45311. + "klips_debug:pfkey_upmsg: "
  45312. + "error=%d calling sock_queue_rcv_skb with skb=0p%p.\n",
  45313. + error,
  45314. + skb);
  45315. + ipsec_kfree_skb(skb);
  45316. + return error;
  45317. + }
  45318. + return error;
  45319. +}
  45320. +
  45321. +#ifdef NET_26_12_SKALLOC
  45322. +static struct proto key_proto = {
  45323. + .name = "KEY",
  45324. + .owner = THIS_MODULE,
  45325. + .obj_size = sizeof(struct sock),
  45326. +
  45327. +};
  45328. +#endif
  45329. +
  45330. +DEBUG_NO_STATIC int
  45331. +pfkey_create(struct socket *sock, int protocol)
  45332. +{
  45333. + struct sock *sk;
  45334. +
  45335. + if(sock == NULL) {
  45336. + KLIPS_PRINT(debug_pfkey,
  45337. + "klips_debug:pfkey_create: "
  45338. + "socket NULL.\n");
  45339. + return -EINVAL;
  45340. + }
  45341. +
  45342. + KLIPS_PRINT(debug_pfkey,
  45343. + "klips_debug:pfkey_create: "
  45344. + "sock=0p%p type:%d state:%d flags:%ld protocol:%d\n",
  45345. + sock,
  45346. + sock->type,
  45347. + (unsigned int)(sock->state),
  45348. + sock->flags, protocol);
  45349. +
  45350. + if(sock->type != SOCK_RAW) {
  45351. + KLIPS_PRINT(debug_pfkey,
  45352. + "klips_debug:pfkey_create: "
  45353. + "only SOCK_RAW supported.\n");
  45354. + return -ESOCKTNOSUPPORT;
  45355. + }
  45356. +
  45357. + if(protocol != PF_KEY_V2) {
  45358. + KLIPS_PRINT(debug_pfkey,
  45359. + "klips_debug:pfkey_create: "
  45360. + "protocol not PF_KEY_V2.\n");
  45361. + return -EPROTONOSUPPORT;
  45362. + }
  45363. +
  45364. + if((current->uid != 0)) {
  45365. + KLIPS_PRINT(debug_pfkey,
  45366. + "klips_debug:pfkey_create: "
  45367. + "must be root to open pfkey sockets.\n");
  45368. + return -EACCES;
  45369. + }
  45370. +
  45371. + sock->state = SS_UNCONNECTED;
  45372. +
  45373. + KLIPS_INC_USE;
  45374. +
  45375. +#ifdef NET_26
  45376. +#ifdef NET_26_12_SKALLOC
  45377. + sk=(struct sock *)sk_alloc(PF_KEY, GFP_KERNEL, &key_proto, 1);
  45378. +#else
  45379. + sk=(struct sock *)sk_alloc(PF_KEY, GFP_KERNEL, 1, NULL);
  45380. +#endif
  45381. +#else
  45382. + /* 2.4 interface */
  45383. + sk=(struct sock *)sk_alloc(PF_KEY, GFP_KERNEL, 1);
  45384. +#endif
  45385. +
  45386. + if(sk == NULL)
  45387. + {
  45388. + KLIPS_PRINT(debug_pfkey,
  45389. + "klips_debug:pfkey_create: "
  45390. + "Out of memory trying to allocate.\n");
  45391. + KLIPS_DEC_USE;
  45392. + return -ENOMEM;
  45393. + }
  45394. +
  45395. + sock_init_data(sock, sk);
  45396. +
  45397. + sk->sk_destruct = NULL;
  45398. + sk->sk_reuse = 1;
  45399. + sock->ops = &pfkey_ops;
  45400. +
  45401. + sk->sk_family = PF_KEY;
  45402. +/* sk->num = protocol; */
  45403. + sk->sk_protocol = protocol;
  45404. + key_pid(sk) = current->pid;
  45405. + KLIPS_PRINT(debug_pfkey,
  45406. + "klips_debug:pfkey_create: "
  45407. + "sock->fasync_list=0p%p sk->sleep=0p%p.\n",
  45408. + sock->fasync_list,
  45409. + sk->sk_sleep);
  45410. +
  45411. + pfkey_insert_socket(sk);
  45412. + pfkey_list_insert_socket(sock, &pfkey_open_sockets);
  45413. +
  45414. + KLIPS_PRINT(debug_pfkey,
  45415. + "klips_debug:pfkey_create: "
  45416. + "Socket sock=0p%p sk=0p%p initialised.\n", sock, sk);
  45417. + return 0;
  45418. +}
  45419. +
  45420. +DEBUG_NO_STATIC int
  45421. +#ifdef NETDEV_23
  45422. +pfkey_release(struct socket *sock)
  45423. +#else /* NETDEV_23 */
  45424. +pfkey_release(struct socket *sock, struct socket *peersock)
  45425. +#endif /* NETDEV_23 */
  45426. +{
  45427. + struct sock *sk;
  45428. + int i;
  45429. +
  45430. + if(sock==NULL) {
  45431. + KLIPS_PRINT(debug_pfkey,
  45432. + "klips_debug:pfkey_release: "
  45433. + "No socket attached.\n");
  45434. + return 0; /* -EINVAL; */
  45435. + }
  45436. +
  45437. + sk=sock->sk;
  45438. +
  45439. + /* May not have data attached */
  45440. + if(sk==NULL) {
  45441. + KLIPS_PRINT(debug_pfkey,
  45442. + "klips_debug:pfkey_release: "
  45443. + "No sk attached to sock=0p%p.\n", sock);
  45444. + return 0; /* -EINVAL; */
  45445. + }
  45446. +
  45447. + KLIPS_PRINT(debug_pfkey,
  45448. + "klips_debug:pfkey_release: "
  45449. + "sock=0p%p sk=0p%p\n", sock, sk);
  45450. +
  45451. + if(sock_flag(sk, SOCK_DEAD))
  45452. + if(sk->sk_state_change) {
  45453. + sk->sk_state_change(sk);
  45454. + }
  45455. +
  45456. + sock->sk = NULL;
  45457. +
  45458. + /* Try to flush out this socket. Throw out buffers at least */
  45459. + pfkey_destroy_socket(sk);
  45460. + pfkey_list_remove_socket(sock, &pfkey_open_sockets);
  45461. + for(i = SADB_SATYPE_UNSPEC; i <= SADB_SATYPE_MAX; i++) {
  45462. + pfkey_list_remove_socket(sock, &(pfkey_registered_sockets[i]));
  45463. + }
  45464. +
  45465. + KLIPS_DEC_USE;
  45466. + KLIPS_PRINT(debug_pfkey,
  45467. + "klips_debug:pfkey_release: "
  45468. + "succeeded.\n");
  45469. +
  45470. + return 0;
  45471. +}
  45472. +
  45473. +DEBUG_NO_STATIC int
  45474. +pfkey_shutdown(struct socket *sock, int mode)
  45475. +{
  45476. + struct sock *sk;
  45477. +
  45478. + if(sock == NULL) {
  45479. + KLIPS_PRINT(debug_pfkey,
  45480. + "klips_debug:pfkey_shutdown: "
  45481. + "NULL socket passed in.\n");
  45482. + return -EINVAL;
  45483. + }
  45484. +
  45485. + sk=sock->sk;
  45486. +
  45487. + if(sk == NULL) {
  45488. + KLIPS_PRINT(debug_pfkey,
  45489. + "klips_debug:pfkey_shutdown: "
  45490. + "No sock attached to socket.\n");
  45491. + return -EINVAL;
  45492. + }
  45493. +
  45494. + KLIPS_PRINT(debug_pfkey,
  45495. + "klips_debug:pfkey_shutdown: "
  45496. + "mode=%x.\n", mode);
  45497. + mode++;
  45498. +
  45499. + if(mode&SEND_SHUTDOWN) {
  45500. + sk->sk_shutdown|=SEND_SHUTDOWN;
  45501. + sk->sk_state_change(sk);
  45502. + }
  45503. +
  45504. + if(mode&RCV_SHUTDOWN) {
  45505. + sk->sk_shutdown|=RCV_SHUTDOWN;
  45506. + sk->sk_state_change(sk);
  45507. + }
  45508. + return 0;
  45509. +}
  45510. +
  45511. +/*
  45512. + * Send PF_KEY data down.
  45513. + */
  45514. +
  45515. +DEBUG_NO_STATIC int
  45516. +#ifdef NET_26
  45517. +pfkey_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, size_t len)
  45518. +#else
  45519. +pfkey_sendmsg(struct socket *sock, struct msghdr *msg, int len, struct scm_cookie *scm)
  45520. +#endif
  45521. +{
  45522. + struct sock *sk;
  45523. + int error = 0;
  45524. + struct sadb_msg *pfkey_msg = NULL, *pfkey_reply = NULL;
  45525. +
  45526. + if(sock == NULL) {
  45527. + KLIPS_PRINT(debug_pfkey,
  45528. + "klips_debug:pfkey_sendmsg: "
  45529. + "Null socket passed in.\n");
  45530. + SENDERR(EINVAL);
  45531. + }
  45532. +
  45533. + sk = sock->sk;
  45534. +
  45535. + if(sk == NULL) {
  45536. + KLIPS_PRINT(debug_pfkey,
  45537. + "klips_debug:pfkey_sendmsg: "
  45538. + "Null sock passed in.\n");
  45539. + SENDERR(EINVAL);
  45540. + }
  45541. +
  45542. + if(msg == NULL) {
  45543. + KLIPS_PRINT(debug_pfkey,
  45544. + "klips_debug:pfkey_sendmsg: "
  45545. + "Null msghdr passed in.\n");
  45546. + SENDERR(EINVAL);
  45547. + }
  45548. +
  45549. + KLIPS_PRINT(debug_pfkey,
  45550. + "klips_debug:pfkey_sendmsg: .\n");
  45551. + if(sk->sk_err) {
  45552. + error = sock_error(sk);
  45553. + KLIPS_PRINT(debug_pfkey,
  45554. + "klips_debug:pfkey_sendmsg: "
  45555. + "sk->err is non-zero, returns %d.\n",
  45556. + error);
  45557. + SENDERR(-error);
  45558. + }
  45559. +
  45560. + if((current->uid != 0)) {
  45561. + KLIPS_PRINT(debug_pfkey,
  45562. + "klips_debug:pfkey_sendmsg: "
  45563. + "must be root to send messages to pfkey sockets.\n");
  45564. + SENDERR(EACCES);
  45565. + }
  45566. +
  45567. + if(msg->msg_control)
  45568. + {
  45569. + KLIPS_PRINT(debug_pfkey,
  45570. + "klips_debug:pfkey_sendmsg: "
  45571. + "can't set flags or set msg_control.\n");
  45572. + SENDERR(EINVAL);
  45573. + }
  45574. +
  45575. + if(sk->sk_shutdown & SEND_SHUTDOWN) {
  45576. + KLIPS_PRINT(debug_pfkey,
  45577. + "klips_debug:pfkey_sendmsg: "
  45578. + "shutdown.\n");
  45579. + send_sig(SIGPIPE, current, 0);
  45580. + SENDERR(EPIPE);
  45581. + }
  45582. +
  45583. + if(len < sizeof(struct sadb_msg)) {
  45584. + KLIPS_PRINT(debug_pfkey,
  45585. + "klips_debug:pfkey_sendmsg: "
  45586. + "bogus msg len of %d, too small.\n", (int)len);
  45587. + SENDERR(EMSGSIZE);
  45588. + }
  45589. +
  45590. + KLIPS_PRINT(debug_pfkey,
  45591. + "klips_debug:pfkey_sendmsg: "
  45592. + "allocating %d bytes for downward message.\n",
  45593. + (int)len);
  45594. + if((pfkey_msg = (struct sadb_msg*)kmalloc(len, GFP_KERNEL)) == NULL) {
  45595. + KLIPS_PRINT(debug_pfkey,
  45596. + "klips_debug:pfkey_sendmsg: "
  45597. + "memory allocation error.\n");
  45598. + SENDERR(ENOBUFS);
  45599. + }
  45600. +
  45601. + memcpy_fromiovec((void *)pfkey_msg, msg->msg_iov, len);
  45602. +
  45603. + if(pfkey_msg->sadb_msg_version != PF_KEY_V2) {
  45604. + KLIPS_PRINT(1 || debug_pfkey,
  45605. + "klips_debug:pfkey_sendmsg: "
  45606. + "not PF_KEY_V2 msg, found %d, should be %d.\n",
  45607. + pfkey_msg->sadb_msg_version,
  45608. + PF_KEY_V2);
  45609. + kfree((void*)pfkey_msg);
  45610. + return -EINVAL;
  45611. + }
  45612. +
  45613. + if(len != pfkey_msg->sadb_msg_len * IPSEC_PFKEYv2_ALIGN) {
  45614. + KLIPS_PRINT(debug_pfkey,
  45615. + "klips_debug:pfkey_sendmsg: "
  45616. + "bogus msg len of %d, not %d byte aligned.\n",
  45617. + (int)len, (int)IPSEC_PFKEYv2_ALIGN);
  45618. + SENDERR(EMSGSIZE);
  45619. + }
  45620. +
  45621. +#if 0
  45622. + /* This check is questionable, since a downward message could be
  45623. + the result of an ACQUIRE either from kernel (PID==0) or
  45624. + userspace (some other PID). */
  45625. + /* check PID */
  45626. + if(pfkey_msg->sadb_msg_pid != current->pid) {
  45627. + KLIPS_PRINT(debug_pfkey,
  45628. + "klips_debug:pfkey_sendmsg: "
  45629. + "pid (%d) does not equal sending process pid (%d).\n",
  45630. + pfkey_msg->sadb_msg_pid, current->pid);
  45631. + SENDERR(EINVAL);
  45632. + }
  45633. +#endif
  45634. +
  45635. + if(pfkey_msg->sadb_msg_reserved) {
  45636. + KLIPS_PRINT(debug_pfkey,
  45637. + "klips_debug:pfkey_sendmsg: "
  45638. + "reserved field must be zero, set to %d.\n",
  45639. + pfkey_msg->sadb_msg_reserved);
  45640. + SENDERR(EINVAL);
  45641. + }
  45642. +
  45643. + if((pfkey_msg->sadb_msg_type > SADB_MAX) || (!pfkey_msg->sadb_msg_type)){
  45644. + KLIPS_PRINT(debug_pfkey,
  45645. + "klips_debug:pfkey_sendmsg: "
  45646. + "msg type too large or small:%d.\n",
  45647. + pfkey_msg->sadb_msg_type);
  45648. + SENDERR(EINVAL);
  45649. + }
  45650. +
  45651. + KLIPS_PRINT(debug_pfkey,
  45652. + "klips_debug:pfkey_sendmsg: "
  45653. + "msg sent for parsing.\n");
  45654. +
  45655. + if((error = pfkey_msg_interp(sk, pfkey_msg, &pfkey_reply))) {
  45656. + struct socket_list *pfkey_socketsp;
  45657. +
  45658. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_sendmsg: "
  45659. + "pfkey_msg_parse returns %d.\n",
  45660. + error);
  45661. +
  45662. + if((pfkey_reply = (struct sadb_msg*)kmalloc(sizeof(struct sadb_msg), GFP_KERNEL)) == NULL) {
  45663. + KLIPS_PRINT(debug_pfkey,
  45664. + "klips_debug:pfkey_sendmsg: "
  45665. + "memory allocation error.\n");
  45666. + SENDERR(ENOBUFS);
  45667. + }
  45668. + memcpy((void*)pfkey_reply, (void*)pfkey_msg, sizeof(struct sadb_msg));
  45669. + pfkey_reply->sadb_msg_errno = -error;
  45670. + pfkey_reply->sadb_msg_len = sizeof(struct sadb_msg) / IPSEC_PFKEYv2_ALIGN;
  45671. +
  45672. + for(pfkey_socketsp = pfkey_open_sockets;
  45673. + pfkey_socketsp;
  45674. + pfkey_socketsp = pfkey_socketsp->next) {
  45675. + int error_upmsg = 0;
  45676. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_sendmsg: "
  45677. + "sending up error=%d message=0p%p to socket=0p%p.\n",
  45678. + error,
  45679. + pfkey_reply,
  45680. + pfkey_socketsp->socketp);
  45681. + if((error_upmsg = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  45682. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_sendmsg: "
  45683. + "sending up error message to socket=0p%p failed with error=%d.\n",
  45684. + pfkey_socketsp->socketp,
  45685. + error_upmsg);
  45686. + /* pfkey_msg_free(&pfkey_reply); */
  45687. + /* SENDERR(-error); */
  45688. + }
  45689. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_sendmsg: "
  45690. + "sending up error message to socket=0p%p succeeded.\n",
  45691. + pfkey_socketsp->socketp);
  45692. + }
  45693. +
  45694. + pfkey_msg_free(&pfkey_reply);
  45695. +
  45696. + SENDERR(-error);
  45697. + }
  45698. +
  45699. + errlab:
  45700. + if (pfkey_msg) {
  45701. + kfree((void*)pfkey_msg);
  45702. + }
  45703. +
  45704. + if(error) {
  45705. + return error;
  45706. + } else {
  45707. + return len;
  45708. + }
  45709. +}
  45710. +
  45711. +/*
  45712. + * Receive PF_KEY data up.
  45713. + */
  45714. +
  45715. +DEBUG_NO_STATIC int
  45716. +#ifdef NET_26
  45717. +pfkey_recvmsg(struct kiocb *kiocb
  45718. + , struct socket *sock
  45719. + , struct msghdr *msg
  45720. + , size_t size
  45721. + , int flags)
  45722. +#else
  45723. +pfkey_recvmsg(struct socket *sock
  45724. + , struct msghdr *msg
  45725. + , int size, int flags
  45726. + , struct scm_cookie *scm)
  45727. +#endif
  45728. +{
  45729. + struct sock *sk;
  45730. + int noblock = flags & MSG_DONTWAIT;
  45731. + struct sk_buff *skb;
  45732. + int error;
  45733. +
  45734. + if(sock == NULL) {
  45735. + KLIPS_PRINT(debug_pfkey,
  45736. + "klips_debug:pfkey_recvmsg: "
  45737. + "Null socket passed in.\n");
  45738. + return -EINVAL;
  45739. + }
  45740. +
  45741. + sk = sock->sk;
  45742. +
  45743. + if(sk == NULL) {
  45744. + KLIPS_PRINT(debug_pfkey,
  45745. + "klips_debug:pfkey_recvmsg: "
  45746. + "Null sock passed in for sock=0p%p.\n", sock);
  45747. + return -EINVAL;
  45748. + }
  45749. +
  45750. + if(msg == NULL) {
  45751. + KLIPS_PRINT(debug_pfkey,
  45752. + "klips_debug:pfkey_recvmsg: "
  45753. + "Null msghdr passed in for sock=0p%p, sk=0p%p.\n",
  45754. + sock, sk);
  45755. + return -EINVAL;
  45756. + }
  45757. +
  45758. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  45759. + "klips_debug:pfkey_recvmsg: sock=0p%p sk=0p%p msg=0p%p size=%d.\n",
  45760. + sock, sk, msg, (int)size);
  45761. + if(flags & ~MSG_PEEK) {
  45762. + KLIPS_PRINT(debug_pfkey,
  45763. + "klips_debug:pfkey_sendmsg: "
  45764. + "flags (%d) other than MSG_PEEK not supported.\n",
  45765. + flags);
  45766. + return -EOPNOTSUPP;
  45767. + }
  45768. +
  45769. + msg->msg_namelen = 0; /* sizeof(*ska); */
  45770. +
  45771. + if(sk->sk_err) {
  45772. + KLIPS_PRINT(debug_pfkey,
  45773. + "klips_debug:pfkey_sendmsg: "
  45774. + "sk->sk_err=%d.\n", sk->sk_err);
  45775. + return sock_error(sk);
  45776. + }
  45777. +
  45778. + if((skb = skb_recv_datagram(sk, flags, noblock, &error) ) == NULL) {
  45779. + return error;
  45780. + }
  45781. +
  45782. + if(size > skb->len) {
  45783. + size = skb->len;
  45784. + }
  45785. + else if(size <skb->len) {
  45786. + msg->msg_flags |= MSG_TRUNC;
  45787. + }
  45788. +
  45789. + skb_copy_datagram_iovec(skb, 0, msg->msg_iov, size);
  45790. +#ifdef HAVE_TSTAMP
  45791. + sk->sk_stamp.tv_sec = skb->tstamp.off_sec;
  45792. + sk->sk_stamp.tv_usec = skb->tstamp.off_usec;
  45793. +#else
  45794. + sk->sk_stamp=skb->stamp;
  45795. +#endif
  45796. +
  45797. + skb_free_datagram(sk, skb);
  45798. + return size;
  45799. +}
  45800. +
  45801. +#ifdef CONFIG_PROC_FS
  45802. +#ifndef PROC_FS_2325
  45803. +DEBUG_NO_STATIC
  45804. +#endif /* PROC_FS_2325 */
  45805. +int
  45806. +pfkey_get_info(char *buffer, char **start, off_t offset, int length
  45807. +#ifndef PROC_NO_DUMMY
  45808. +, int dummy
  45809. +#endif /* !PROC_NO_DUMMY */
  45810. +)
  45811. +{
  45812. + const int max_content = length > 0? length-1 : 0; /* limit of useful snprintf output */
  45813. +#ifdef NET_26
  45814. + struct hlist_node *node;
  45815. +#endif
  45816. + off_t begin=0;
  45817. + int len=0;
  45818. + struct sock *sk;
  45819. +
  45820. +#ifdef CONFIG_KLIPS_DEBUG
  45821. + if(!sysctl_ipsec_debug_verbose) {
  45822. +#endif /* CONFIG_KLIPS_DEBUG */
  45823. + len += ipsec_snprintf(buffer, length,
  45824. + " sock pid socket next prev e n p sndbf Flags Type St\n");
  45825. +#ifdef CONFIG_KLIPS_DEBUG
  45826. + } else {
  45827. + len += ipsec_snprintf(buffer, length,
  45828. + " sock pid d sleep socket next prev e r z n p sndbf stamp Flags Type St\n");
  45829. + }
  45830. +#endif /* CONFIG_KLIPS_DEBUG */
  45831. +
  45832. + sk_for_each(sk, node, &pfkey_sock_list) {
  45833. +
  45834. +#ifdef CONFIG_KLIPS_DEBUG
  45835. + if(!sysctl_ipsec_debug_verbose) {
  45836. +#endif /* CONFIG_KLIPS_DEBUG */
  45837. + len += ipsec_snprintf(buffer+len, length-len,
  45838. + "%8p %5d %8p %d %d %5d %08lX %8X %2X\n",
  45839. + sk,
  45840. + key_pid(sk),
  45841. + sk->sk_socket,
  45842. + sk->sk_err,
  45843. + sk->sk_protocol,
  45844. + sk->sk_sndbuf,
  45845. + sk->sk_socket->flags,
  45846. + sk->sk_socket->type,
  45847. + sk->sk_socket->state);
  45848. +#ifdef CONFIG_KLIPS_DEBUG
  45849. + } else {
  45850. + len += ipsec_snprintf(buffer+len, length-len,
  45851. + "%8p %5d %d %8p %8p %d %d %d %d %5d %d.%06d %08lX %8X %2X\n",
  45852. + sk,
  45853. + key_pid(sk),
  45854. + sock_flag(sk, SOCK_DEAD),
  45855. + sk->sk_sleep,
  45856. + sk->sk_socket,
  45857. + sk->sk_err,
  45858. + sk->sk_reuse,
  45859. +#ifdef HAVE_SOCK_ZAPPED
  45860. + sock_flag(sk, SOCK_ZAPPED),
  45861. +#else
  45862. + sk->sk_zapped,
  45863. +#endif
  45864. + sk->sk_protocol,
  45865. + sk->sk_sndbuf,
  45866. + (unsigned int)sk->sk_stamp.tv_sec,
  45867. + (unsigned int)sk->sk_stamp.tv_usec,
  45868. + sk->sk_socket->flags,
  45869. + sk->sk_socket->type,
  45870. + sk->sk_socket->state);
  45871. + }
  45872. +#endif /* CONFIG_KLIPS_DEBUG */
  45873. +
  45874. + if (len >= max_content) {
  45875. + /* we've done all that can fit -- stop loop */
  45876. + len = max_content; /* truncate crap */
  45877. + break;
  45878. + } else {
  45879. + const off_t pos = begin + len; /* file position of end of what we've generated */
  45880. +
  45881. + if (pos <= offset) {
  45882. + /* all is before first interesting character:
  45883. + * discard, but note where we are.
  45884. + */
  45885. + len = 0;
  45886. + begin = pos;
  45887. + }
  45888. + }
  45889. + }
  45890. +
  45891. + *start = buffer + (offset - begin); /* Start of wanted data */
  45892. + return len - (offset - begin);
  45893. +}
  45894. +
  45895. +#ifndef PROC_FS_2325
  45896. +DEBUG_NO_STATIC
  45897. +#endif /* PROC_FS_2325 */
  45898. +int
  45899. +pfkey_supported_get_info(char *buffer, char **start, off_t offset, int length
  45900. +#ifndef PROC_NO_DUMMY
  45901. +, int dummy
  45902. +#endif /* !PROC_NO_DUMMY */
  45903. +)
  45904. +{
  45905. + /* limit of useful snprintf output */
  45906. + const int max_content = length > 0? length-1 : 0;
  45907. + off_t begin=0;
  45908. + int len=0;
  45909. + int satype;
  45910. + struct supported_list *ps;
  45911. +
  45912. + len += ipsec_snprintf(buffer, length,
  45913. + "satype exttype alg_id ivlen minbits maxbits name\n");
  45914. +
  45915. + for(satype = SADB_SATYPE_UNSPEC; satype <= SADB_SATYPE_MAX; satype++) {
  45916. + ps = pfkey_supported_list[satype];
  45917. + while(ps) {
  45918. + struct ipsec_alg_supported *alg = ps->supportedp;
  45919. + unsigned char *n = alg->ias_name;
  45920. + if(n == NULL) n = "unknown";
  45921. +
  45922. + len += ipsec_snprintf(buffer+len, length-len,
  45923. + " %2d %2d %2d %3d %3d %3d %20s\n",
  45924. + satype,
  45925. + alg->ias_exttype,
  45926. + alg->ias_id,
  45927. + alg->ias_ivlen,
  45928. + alg->ias_keyminbits,
  45929. + alg->ias_keymaxbits,
  45930. + n);
  45931. +
  45932. + if (len >= max_content) {
  45933. + /* we've done all that can fit -- stop loop */
  45934. + len = max_content; /* truncate crap */
  45935. + break;
  45936. + } else {
  45937. + const off_t pos = begin + len; /* file position of end of what we've generated */
  45938. +
  45939. + if (pos <= offset) {
  45940. + /* all is before first interesting character:
  45941. + * discard, but note where we are.
  45942. + */
  45943. + len = 0;
  45944. + begin = pos;
  45945. + }
  45946. + }
  45947. +
  45948. + ps = ps->next;
  45949. + }
  45950. + }
  45951. + *start = buffer + (offset - begin); /* Start of wanted data */
  45952. + return len - (offset - begin);
  45953. +}
  45954. +
  45955. +#ifndef PROC_FS_2325
  45956. +DEBUG_NO_STATIC
  45957. +#endif /* PROC_FS_2325 */
  45958. +int
  45959. +pfkey_registered_get_info(char *buffer, char **start, off_t offset, int length
  45960. +#ifndef PROC_NO_DUMMY
  45961. +, int dummy
  45962. +#endif /* !PROC_NO_DUMMY */
  45963. +)
  45964. +{
  45965. + const int max_content = length > 0? length-1 : 0; /* limit of useful snprintf output */
  45966. + off_t begin=0;
  45967. + int len=0;
  45968. + int satype;
  45969. + struct socket_list *pfkey_sockets;
  45970. +
  45971. + len += ipsec_snprintf(buffer, length,
  45972. + "satype socket pid sk\n");
  45973. +
  45974. + for(satype = SADB_SATYPE_UNSPEC; satype <= SADB_SATYPE_MAX; satype++) {
  45975. + pfkey_sockets = pfkey_registered_sockets[satype];
  45976. + while(pfkey_sockets) {
  45977. + len += ipsec_snprintf(buffer+len, length-len,
  45978. + " %2d %8p %5d %8p\n",
  45979. + satype,
  45980. + pfkey_sockets->socketp,
  45981. + key_pid(pfkey_sockets->socketp->sk),
  45982. + pfkey_sockets->socketp->sk);
  45983. +
  45984. + if (len >= max_content) {
  45985. + /* we've done all that can fit -- stop loop (could stop two) */
  45986. + len = max_content; /* truncate crap */
  45987. + break;
  45988. + } else {
  45989. + const off_t pos = begin + len; /* file position of end of what we've generated */
  45990. +
  45991. + if (pos <= offset) {
  45992. + /* all is before first interesting character:
  45993. + * discard, but note where we are.
  45994. + */
  45995. + len = 0;
  45996. + begin = pos;
  45997. + }
  45998. + }
  45999. +
  46000. + pfkey_sockets = pfkey_sockets->next;
  46001. + }
  46002. + }
  46003. + *start = buffer + (offset - begin); /* Start of wanted data */
  46004. + return len - (offset - begin);
  46005. +}
  46006. +
  46007. +#ifndef PROC_FS_2325
  46008. +struct proc_dir_entry proc_net_pfkey =
  46009. +{
  46010. + 0,
  46011. + 6, "pf_key",
  46012. + S_IFREG | S_IRUGO, 1, 0, 0,
  46013. + 0, &proc_net_inode_operations,
  46014. + pfkey_get_info
  46015. +};
  46016. +struct proc_dir_entry proc_net_pfkey_supported =
  46017. +{
  46018. + 0,
  46019. + 16, "pf_key_supported",
  46020. + S_IFREG | S_IRUGO, 1, 0, 0,
  46021. + 0, &proc_net_inode_operations,
  46022. + pfkey_supported_get_info
  46023. +};
  46024. +struct proc_dir_entry proc_net_pfkey_registered =
  46025. +{
  46026. + 0,
  46027. + 17, "pf_key_registered",
  46028. + S_IFREG | S_IRUGO, 1, 0, 0,
  46029. + 0, &proc_net_inode_operations,
  46030. + pfkey_registered_get_info
  46031. +};
  46032. +#endif /* !PROC_FS_2325 */
  46033. +#endif /* CONFIG_PROC_FS */
  46034. +
  46035. +DEBUG_NO_STATIC int
  46036. +supported_add_all(int satype, struct ipsec_alg_supported supported[], int size)
  46037. +{
  46038. + int i;
  46039. + int error = 0;
  46040. +
  46041. + KLIPS_PRINT(debug_pfkey,
  46042. + "klips_debug:init_pfkey: "
  46043. + "sizeof(supported_init_<satype=%d>)[%d]/sizeof(struct ipsec_alg_supported)[%d]=%d.\n",
  46044. + satype,
  46045. + size,
  46046. + (int)sizeof(struct ipsec_alg_supported),
  46047. + (int)(size/sizeof(struct ipsec_alg_supported)));
  46048. +
  46049. + for(i = 0; i < size / sizeof(struct ipsec_alg_supported); i++) {
  46050. +
  46051. + unsigned char *n = supported[i].ias_name;
  46052. + if(n == NULL) n="unknown";
  46053. +
  46054. + KLIPS_PRINT(debug_pfkey,
  46055. + "klips_debug:init_pfkey: "
  46056. + "i=%d inserting satype=%d exttype=%d id=%d ivlen=%d minbits=%d maxbits=%d name=%s.\n",
  46057. + i,
  46058. + satype,
  46059. + supported[i].ias_exttype,
  46060. + supported[i].ias_id,
  46061. + supported[i].ias_ivlen,
  46062. + supported[i].ias_keyminbits,
  46063. + supported[i].ias_keymaxbits,
  46064. + n);
  46065. +
  46066. + error |= pfkey_list_insert_supported(&(supported[i]),
  46067. + &(pfkey_supported_list[satype]));
  46068. + }
  46069. + return error;
  46070. +}
  46071. +
  46072. +DEBUG_NO_STATIC int
  46073. +supported_remove_all(int satype)
  46074. +{
  46075. + int error = 0;
  46076. + struct ipsec_alg_supported*supportedp;
  46077. +
  46078. + while(pfkey_supported_list[satype]) {
  46079. + unsigned char *n;
  46080. + supportedp = pfkey_supported_list[satype]->supportedp;
  46081. +
  46082. + n = supportedp->ias_name;
  46083. + if(n == NULL) n="unknown";
  46084. +
  46085. + KLIPS_PRINT(debug_pfkey,
  46086. + "klips_debug:init_pfkey: "
  46087. + "removing satype=%d exttype=%d id=%d ivlen=%d minbits=%d maxbits=%d name=%s.\n",
  46088. + satype,
  46089. + supportedp->ias_exttype,
  46090. + supportedp->ias_id,
  46091. + supportedp->ias_ivlen,
  46092. + supportedp->ias_keyminbits,
  46093. + supportedp->ias_keymaxbits, n);
  46094. +
  46095. + error |= pfkey_list_remove_supported(supportedp,
  46096. + &(pfkey_supported_list[satype]));
  46097. + }
  46098. + return error;
  46099. +}
  46100. +
  46101. +int
  46102. +pfkey_init(void)
  46103. +{
  46104. + int error = 0;
  46105. + int i;
  46106. +
  46107. + static struct ipsec_alg_supported supported_init_ah[] = {
  46108. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  46109. + {SADB_EXT_SUPPORTED_AUTH, SADB_AALG_MD5HMAC, 0, 128, 128},
  46110. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  46111. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  46112. + {SADB_EXT_SUPPORTED_AUTH, SADB_AALG_SHA1HMAC, 0, 160, 160}
  46113. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  46114. + };
  46115. + static struct ipsec_alg_supported supported_init_esp[] = {
  46116. +#ifdef CONFIG_KLIPS_AUTH_HMAC_MD5
  46117. + {SADB_EXT_SUPPORTED_AUTH, SADB_AALG_MD5HMAC, 0, 128, 128},
  46118. +#endif /* CONFIG_KLIPS_AUTH_HMAC_MD5 */
  46119. +#ifdef CONFIG_KLIPS_AUTH_HMAC_SHA1
  46120. + {SADB_EXT_SUPPORTED_AUTH, SADB_AALG_SHA1HMAC, 0, 160, 160},
  46121. +#endif /* CONFIG_KLIPS_AUTH_HMAC_SHA1 */
  46122. +#ifdef CONFIG_KLIPS_ENC_3DES
  46123. + {SADB_EXT_SUPPORTED_ENCRYPT, SADB_EALG_3DESCBC, 64, 168, 168},
  46124. +#endif /* CONFIG_KLIPS_ENC_3DES */
  46125. + };
  46126. + static struct ipsec_alg_supported supported_init_ipip[] = {
  46127. + {SADB_EXT_SUPPORTED_ENCRYPT, SADB_X_TALG_IPv4_in_IPv4, 0, 32, 32}
  46128. +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  46129. + , {SADB_EXT_SUPPORTED_ENCRYPT, SADB_X_TALG_IPv6_in_IPv4, 0, 128, 32}
  46130. + , {SADB_EXT_SUPPORTED_ENCRYPT, SADB_X_TALG_IPv4_in_IPv6, 0, 32, 128}
  46131. + , {SADB_EXT_SUPPORTED_ENCRYPT, SADB_X_TALG_IPv6_in_IPv6, 0, 128, 128}
  46132. +#endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
  46133. + };
  46134. +#ifdef CONFIG_KLIPS_IPCOMP
  46135. + static struct ipsec_alg_supported supported_init_ipcomp[] = {
  46136. + {SADB_EXT_SUPPORTED_ENCRYPT, SADB_X_CALG_DEFLATE, 0, 1, 1}
  46137. + };
  46138. +#endif /* CONFIG_KLIPS_IPCOMP */
  46139. +
  46140. +#if 0
  46141. + printk(KERN_INFO
  46142. + "klips_info:pfkey_init: "
  46143. + "FreeS/WAN: initialising PF_KEYv2 domain sockets.\n");
  46144. +#endif
  46145. +
  46146. + for(i = SADB_SATYPE_UNSPEC; i <= SADB_SATYPE_MAX; i++) {
  46147. + pfkey_registered_sockets[i] = NULL;
  46148. + pfkey_supported_list[i] = NULL;
  46149. + }
  46150. +
  46151. + error |= supported_add_all(SADB_SATYPE_AH, supported_init_ah, sizeof(supported_init_ah));
  46152. + error |= supported_add_all(SADB_SATYPE_ESP, supported_init_esp, sizeof(supported_init_esp));
  46153. +#ifdef CONFIG_KLIPS_IPCOMP
  46154. + error |= supported_add_all(SADB_X_SATYPE_COMP, supported_init_ipcomp, sizeof(supported_init_ipcomp));
  46155. +#endif /* CONFIG_KLIPS_IPCOMP */
  46156. + error |= supported_add_all(SADB_X_SATYPE_IPIP, supported_init_ipip, sizeof(supported_init_ipip));
  46157. +
  46158. + error |= sock_register(&pfkey_family_ops);
  46159. +
  46160. +#ifdef CONFIG_PROC_FS
  46161. +# ifndef PROC_FS_2325
  46162. +# ifdef PROC_FS_21
  46163. + error |= proc_register(proc_net, &proc_net_pfkey);
  46164. + error |= proc_register(proc_net, &proc_net_pfkey_supported);
  46165. + error |= proc_register(proc_net, &proc_net_pfkey_registered);
  46166. +# else /* PROC_FS_21 */
  46167. + error |= proc_register_dynamic(&proc_net, &proc_net_pfkey);
  46168. + error |= proc_register_dynamic(&proc_net, &proc_net_pfkey_supported);
  46169. + error |= proc_register_dynamic(&proc_net, &proc_net_pfkey_registered);
  46170. +# endif /* PROC_FS_21 */
  46171. +# else /* !PROC_FS_2325 */
  46172. + proc_net_create ("pf_key", 0, pfkey_get_info);
  46173. + proc_net_create ("pf_key_supported", 0, pfkey_supported_get_info);
  46174. + proc_net_create ("pf_key_registered", 0, pfkey_registered_get_info);
  46175. +# endif /* !PROC_FS_2325 */
  46176. +#endif /* CONFIG_PROC_FS */
  46177. +
  46178. + return error;
  46179. +}
  46180. +
  46181. +int
  46182. +pfkey_cleanup(void)
  46183. +{
  46184. + int error = 0;
  46185. +
  46186. + printk(KERN_INFO "klips_info:pfkey_cleanup: "
  46187. + "shutting down PF_KEY domain sockets.\n");
  46188. + sock_unregister(PF_KEY);
  46189. +
  46190. + error |= supported_remove_all(SADB_SATYPE_AH);
  46191. + error |= supported_remove_all(SADB_SATYPE_ESP);
  46192. +#ifdef CONFIG_KLIPS_IPCOMP
  46193. + error |= supported_remove_all(SADB_X_SATYPE_COMP);
  46194. +#endif /* CONFIG_KLIPS_IPCOMP */
  46195. + error |= supported_remove_all(SADB_X_SATYPE_IPIP);
  46196. +
  46197. +#ifdef CONFIG_PROC_FS
  46198. +# ifndef PROC_FS_2325
  46199. + if (proc_net_unregister(proc_net_pfkey.low_ino) != 0)
  46200. + printk("klips_debug:pfkey_cleanup: "
  46201. + "cannot unregister /proc/net/pf_key\n");
  46202. + if (proc_net_unregister(proc_net_pfkey_supported.low_ino) != 0)
  46203. + printk("klips_debug:pfkey_cleanup: "
  46204. + "cannot unregister /proc/net/pf_key_supported\n");
  46205. + if (proc_net_unregister(proc_net_pfkey_registered.low_ino) != 0)
  46206. + printk("klips_debug:pfkey_cleanup: "
  46207. + "cannot unregister /proc/net/pf_key_registered\n");
  46208. +# else /* !PROC_FS_2325 */
  46209. + proc_net_remove ("pf_key");
  46210. + proc_net_remove ("pf_key_supported");
  46211. + proc_net_remove ("pf_key_registered");
  46212. +# endif /* !PROC_FS_2325 */
  46213. +#endif /* CONFIG_PROC_FS */
  46214. +
  46215. + /* other module unloading cleanup happens here */
  46216. + return error;
  46217. +}
  46218. +
  46219. +#ifdef MODULE
  46220. +#if 0
  46221. +int
  46222. +init_module(void)
  46223. +{
  46224. + pfkey_init();
  46225. + return 0;
  46226. +}
  46227. +
  46228. +void
  46229. +cleanup_module(void)
  46230. +{
  46231. + pfkey_cleanup();
  46232. +}
  46233. +#endif /* 0 */
  46234. +#else /* MODULE */
  46235. +struct net_protocol;
  46236. +void pfkey_proto_init(struct net_protocol *pro)
  46237. +{
  46238. + pfkey_init();
  46239. +}
  46240. +#endif /* MODULE */
  46241. +
  46242. +/*
  46243. + * $Log: pfkey_v2.c,v $
  46244. + * Revision 1.97.2.12 2006/11/24 05:43:29 paul
  46245. + * kernels after 2.6.18 do not return a code from unregister_socket()
  46246. + * backport from git 41e54a2684dc809d7952e816860ea646a3194a72
  46247. + *
  46248. + * Revision 1.97.2.11 2006/11/15 16:05:57 paul
  46249. + * fix for compiling on 2.4. kernels by Matthias Haas.
  46250. + *
  46251. + * Revision 1.97.2.10 2006/10/10 20:43:28 paul
  46252. + * Add family/create/owner for pfkey_family_ops. This fixes bug #671
  46253. + *
  46254. + * Revision 1.97.2.9 2006/10/06 21:39:26 paul
  46255. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  46256. + * set. This is defined through autoconf.h which is included through the
  46257. + * linux kernel build macros.
  46258. + *
  46259. + * Revision 1.97.2.8 2006/07/10 15:56:11 paul
  46260. + * Fix for bug #642 by Bart.
  46261. + *
  46262. + * Revision 1.97.2.7 2006/04/04 11:34:19 ken
  46263. + * Backport SMP fixes + #ifdef cleanup from #public
  46264. + *
  46265. + * Revision 1.97.2.6 2006/02/15 05:00:20 paul
  46266. + * Fix for crasher on 2.6.12+ with klips (mostly seen on redhat kernels)
  46267. + *
  46268. + * Revision 1.97.2.5 2005/11/22 04:11:52 ken
  46269. + * Backport fixes for 2.6.14 kernels from HEAD
  46270. + *
  46271. + * Revision 1.97.2.4 2005/09/14 16:40:45 mcr
  46272. + * pull up of compilation on 2.4
  46273. + *
  46274. + * Revision 1.97.2.3 2005/09/06 02:10:03 mcr
  46275. + * pulled up possible SMP-related compilation fix
  46276. + *
  46277. + * Revision 1.97.2.2 2005/08/28 01:21:12 paul
  46278. + * Undid Ken's gcc4 fix in version 1.94 since it breaks linking KLIPS on
  46279. + * SMP kernels.
  46280. + *
  46281. + * Revision 1.97.2.1 2005/08/27 23:40:00 paul
  46282. + * recommited HAVE_SOCK_SECURITY fixes for linux 2.6.13
  46283. + *
  46284. + * Revision 1.102 2005/09/14 16:37:23 mcr
  46285. + * fix to compile on 2.4.
  46286. + *
  46287. + * Revision 1.101 2005/09/06 01:42:25 mcr
  46288. + * removed additional SOCKOPS_WRAPPED code
  46289. + *
  46290. + * Revision 1.100 2005/08/30 18:10:15 mcr
  46291. + * remove SOCKOPS_WRAPPED() code, add proper locking to the
  46292. + * pfkey code. (cross fingers)
  46293. + *
  46294. + * Revision 1.99 2005/08/28 01:53:37 paul
  46295. + * Undid Ken's gcc4 fix in version 1.94 since it breaks linking KLIPS on SMP kernels.
  46296. + *
  46297. + * Revision 1.98 2005/08/27 23:07:21 paul
  46298. + * Somewhere between 2.6.12 and 2.6.13rc7 the unused security memnber in sk_buff
  46299. + * has been removed. This patch should fix compilation for both cases.
  46300. + *
  46301. + * Revision 1.97 2005/07/20 00:33:36 mcr
  46302. + * fixed typo in #ifdef for SKALLOC.
  46303. + *
  46304. + * Revision 1.96 2005/07/19 20:02:15 mcr
  46305. + * sk_alloc() interface change.
  46306. + *
  46307. + * Revision 1.95 2005/07/09 00:40:06 ken
  46308. + * Fix for GCC4 - it doesn't like the potential for duplicate declaration
  46309. + *
  46310. + * Revision 1.94 2005/07/09 00:14:04 ken
  46311. + * Casts for 64bit cleanliness
  46312. + *
  46313. + * Revision 1.93 2005/07/08 16:20:05 mcr
  46314. + * fix for 2.6.12 disapperance of sk_zapped field -> sock_flags.
  46315. + *
  46316. + * Revision 1.92 2005/05/21 03:29:39 mcr
  46317. + * fixed missing prototype definition.
  46318. + *
  46319. + * Revision 1.91 2005/05/11 01:43:45 mcr
  46320. + * removed "poor-man"s OOP in favour of proper C structures.
  46321. + *
  46322. + * Revision 1.90 2005/05/02 18:42:47 mcr
  46323. + * fix for cut&paste error with pfkey_v2.c "supported_name"
  46324. + *
  46325. + * Revision 1.89 2005/05/01 03:12:31 mcr
  46326. + * print name if it is available.
  46327. + *
  46328. + * Revision 1.88 2005/04/29 05:10:22 mcr
  46329. + * removed from extraenous includes to make unit testing easier.
  46330. + *
  46331. + * Revision 1.87 2005/04/15 19:57:10 mcr
  46332. + * make sure that address has 0p so that it will
  46333. + * sanitized.
  46334. + *
  46335. + * Revision 1.86 2005/04/08 18:28:36 mcr
  46336. + * some minor #ifdef simplification in pursuit of a possible bug.
  46337. + *
  46338. + * Revision 1.85 2004/12/03 21:25:57 mcr
  46339. + * compile time fixes for running on 2.6.
  46340. + * still experimental.
  46341. + *
  46342. + * Revision 1.84 2004/08/17 03:27:23 mcr
  46343. + * klips 2.6 edits.
  46344. + *
  46345. + * Revision 1.83 2004/08/04 15:57:07 mcr
  46346. + * moved des .h files to include/des/ *
  46347. + * included 2.6 protocol specific things
  46348. + * started at NAT-T support, but it will require a kernel patch.
  46349. + *
  46350. + * Revision 1.82 2004/07/10 19:11:18 mcr
  46351. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  46352. + *
  46353. + * Revision 1.81 2004/04/25 21:23:11 ken
  46354. + * Pull in dhr's changes from FreeS/WAN 2.06
  46355. + *
  46356. + * Revision 1.80 2004/04/06 02:49:26 mcr
  46357. + * pullup of algo code from alg-branch.
  46358. + *
  46359. + * Revision 1.79.4.1 2003/12/22 15:25:52 jjo
  46360. + * . Merged algo-0.8.1-rc11-test1 into alg-branch
  46361. + *
  46362. + * Revision 1.79 2003/10/31 02:27:55 mcr
  46363. + * pulled up port-selector patches and sa_id elimination.
  46364. + *
  46365. + * Revision 1.78.4.1 2003/10/29 01:30:41 mcr
  46366. + * elimited "struct sa_id".
  46367. + *
  46368. + * Revision 1.78 2003/04/03 17:38:09 rgb
  46369. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  46370. + *
  46371. + * Revision 1.77 2002/10/17 16:49:36 mcr
  46372. + * sock->ops should reference the unwrapped options so that
  46373. + * we get hacked in locking on SMP systems.
  46374. + *
  46375. + * Revision 1.76 2002/10/12 23:11:53 dhr
  46376. + *
  46377. + * [KenB + DHR] more 64-bit cleanup
  46378. + *
  46379. + * Revision 1.75 2002/09/20 05:01:57 rgb
  46380. + * Added memory allocation debugging.
  46381. + *
  46382. + * Revision 1.74 2002/09/19 02:42:50 mcr
  46383. + * do not define the pfkey_ops function for now.
  46384. + *
  46385. + * Revision 1.73 2002/09/17 17:29:23 mcr
  46386. + * #if 0 out some dead code - pfkey_ops is never used as written.
  46387. + *
  46388. + * Revision 1.72 2002/07/24 18:44:54 rgb
  46389. + * Type fiddling to tame ia64 compiler.
  46390. + *
  46391. + * Revision 1.71 2002/05/23 07:14:11 rgb
  46392. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  46393. + *
  46394. + * Revision 1.70 2002/04/24 07:55:32 mcr
  46395. + * #include patches and Makefiles for post-reorg compilation.
  46396. + *
  46397. + * Revision 1.69 2002/04/24 07:36:33 mcr
  46398. + * Moved from ./klips/net/ipsec/pfkey_v2.c,v
  46399. + *
  46400. + * Revision 1.68 2002/03/08 01:15:17 mcr
  46401. + * put some internal structure only debug messages behind
  46402. + * && sysctl_ipsec_debug_verbose.
  46403. + *
  46404. + * Revision 1.67 2002/01/29 17:17:57 mcr
  46405. + * moved include of ipsec_param.h to after include of linux/kernel.h
  46406. + * otherwise, it seems that some option that is set in ipsec_param.h
  46407. + * screws up something subtle in the include path to kernel.h, and
  46408. + * it complains on the snprintf() prototype.
  46409. + *
  46410. + * Revision 1.66 2002/01/29 04:00:54 mcr
  46411. + * more excise of kversions.h header.
  46412. + *
  46413. + * Revision 1.65 2002/01/29 02:13:18 mcr
  46414. + * introduction of ipsec_kversion.h means that include of
  46415. + * ipsec_param.h must preceed any decisions about what files to
  46416. + * include to deal with differences in kernel source.
  46417. + *
  46418. + * Revision 1.64 2001/11/26 09:23:51 rgb
  46419. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  46420. + *
  46421. + * Revision 1.61.2.1 2001/09/25 02:28:44 mcr
  46422. + * cleaned up includes.
  46423. + *
  46424. + * Revision 1.63 2001/11/12 19:38:00 rgb
  46425. + * Continue trying other sockets even if one fails and return only original
  46426. + * error.
  46427. + *
  46428. + * Revision 1.62 2001/10/18 04:45:22 rgb
  46429. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  46430. + * lib/freeswan.h version macros moved to lib/kversions.h.
  46431. + * Other compiler directive cleanups.
  46432. + *
  46433. + * Revision 1.61 2001/09/20 15:32:59 rgb
  46434. + * Min/max cleanup.
  46435. + *
  46436. + * Revision 1.60 2001/06/14 19:35:12 rgb
  46437. + * Update copyright date.
  46438. + *
  46439. + * Revision 1.59 2001/06/13 15:35:48 rgb
  46440. + * Fixed #endif comments.
  46441. + *
  46442. + * Revision 1.58 2001/05/04 16:37:24 rgb
  46443. + * Remove erroneous checking of return codes for proc_net_* in 2.4.
  46444. + *
  46445. + * Revision 1.57 2001/05/03 19:43:36 rgb
  46446. + * Initialise error return variable.
  46447. + * Check error return codes in startup and shutdown.
  46448. + * Standardise on SENDERR() macro.
  46449. + *
  46450. + * Revision 1.56 2001/04/21 23:05:07 rgb
  46451. + * Define out skb->used for 2.4 kernels.
  46452. + *
  46453. + * Revision 1.55 2001/02/28 05:03:28 rgb
  46454. + * Clean up and rationalise startup messages.
  46455. + *
  46456. + * Revision 1.54 2001/02/27 22:24:55 rgb
  46457. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  46458. + * Check for satoa() return codes.
  46459. + *
  46460. + * Revision 1.53 2001/02/27 06:48:18 rgb
  46461. + * Fixed pfkey socket unregister log message to reflect type and function.
  46462. + *
  46463. + * Revision 1.52 2001/02/26 22:34:38 rgb
  46464. + * Fix error return code that was getting overwritten by the error return
  46465. + * code of an upmsg.
  46466. + *
  46467. + * Revision 1.51 2001/01/30 23:42:47 rgb
  46468. + * Allow pfkey msgs from pid other than user context required for ACQUIRE
  46469. + * and subsequent ADD or UDATE.
  46470. + *
  46471. + * Revision 1.50 2001/01/23 20:22:59 rgb
  46472. + * 2.4 fix to remove removed is_clone member.
  46473. + *
  46474. + * Revision 1.49 2000/11/06 04:33:47 rgb
  46475. + * Changed non-exported functions to DEBUG_NO_STATIC.
  46476. + *
  46477. + * Revision 1.48 2000/09/29 19:47:41 rgb
  46478. + * Update copyright.
  46479. + *
  46480. + * Revision 1.47 2000/09/22 04:23:04 rgb
  46481. + * Added more debugging to pfkey_upmsg() call from pfkey_sendmsg() error.
  46482. + *
  46483. + * Revision 1.46 2000/09/21 04:20:44 rgb
  46484. + * Fixed array size off-by-one error. (Thanks Svenning!)
  46485. + *
  46486. + * Revision 1.45 2000/09/20 04:01:26 rgb
  46487. + * Changed static functions to DEBUG_NO_STATIC for revealing function names
  46488. + * in oopsen.
  46489. + *
  46490. + * Revision 1.44 2000/09/19 00:33:17 rgb
  46491. + * 2.0 fixes.
  46492. + *
  46493. + * Revision 1.43 2000/09/16 01:28:13 rgb
  46494. + * Fixed use of 0 in p format warning.
  46495. + *
  46496. + * Revision 1.42 2000/09/16 01:09:41 rgb
  46497. + * Fixed debug format warning for pointers that was expecting ints.
  46498. + *
  46499. + * Revision 1.41 2000/09/13 15:54:00 rgb
  46500. + * Rewrote pfkey_get_info(), added pfkey_{supported,registered}_get_info().
  46501. + * Moved supported algos add and remove to functions.
  46502. + *
  46503. + * Revision 1.40 2000/09/12 18:49:28 rgb
  46504. + * Added IPIP tunnel and IPCOMP register support.
  46505. + *
  46506. + * Revision 1.39 2000/09/12 03:23:49 rgb
  46507. + * Converted #if0 debugs to sysctl.
  46508. + * Removed debug_pfkey initialisations that prevented no_debug loading or
  46509. + * linking.
  46510. + *
  46511. + * Revision 1.38 2000/09/09 06:38:02 rgb
  46512. + * Return positive errno in pfkey_reply error message.
  46513. + *
  46514. + * Revision 1.37 2000/09/08 19:19:09 rgb
  46515. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  46516. + * Clean-up of long-unused crud...
  46517. + * Create pfkey error message on on failure.
  46518. + * Give pfkey_list_{insert,remove}_{socket,supported}() some error
  46519. + * checking.
  46520. + *
  46521. + * Revision 1.36 2000/09/01 18:49:38 rgb
  46522. + * Reap experimental NET_21_ bits.
  46523. + * Turned registered sockets list into an array of one list per satype.
  46524. + * Remove references to deprecated sklist_{insert,remove}_socket.
  46525. + * Removed leaking socket debugging code.
  46526. + * Removed duplicate pfkey_insert_socket in pfkey_create.
  46527. + * Removed all references to pfkey msg->msg_name, since it is not used for
  46528. + * pfkey.
  46529. + * Added a supported algorithms array lists, one per satype and registered
  46530. + * existing algorithms.
  46531. + * Fixed pfkey_list_{insert,remove}_{socket,support}() to allow change to
  46532. + * list.
  46533. + * Only send pfkey_expire() messages to sockets registered for that satype.
  46534. + *
  46535. + * Revision 1.35 2000/08/24 17:03:00 rgb
  46536. + * Corrected message size error return code for PF_KEYv2.
  46537. + * Removed downward error prohibition.
  46538. + *
  46539. + * Revision 1.34 2000/08/21 16:32:26 rgb
  46540. + * Re-formatted for cosmetic consistency and readability.
  46541. + *
  46542. + * Revision 1.33 2000/08/20 21:38:24 rgb
  46543. + * Added a pfkey_reply parameter to pfkey_msg_interp(). (Momchil)
  46544. + * Extended the upward message initiation of pfkey_sendmsg(). (Momchil)
  46545. + *
  46546. + * Revision 1.32 2000/07/28 14:58:31 rgb
  46547. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  46548. + *
  46549. + * Revision 1.31 2000/05/16 03:04:00 rgb
  46550. + * Updates for 2.3.99pre8 from MB.
  46551. + *
  46552. + * Revision 1.30 2000/05/10 19:22:21 rgb
  46553. + * Use sklist private functions for 2.3.xx compatibility.
  46554. + *
  46555. + * Revision 1.29 2000/03/22 16:17:03 rgb
  46556. + * Fixed SOCKOPS_WRAPPED macro for SMP (MB).
  46557. + *
  46558. + * Revision 1.28 2000/02/21 19:30:45 rgb
  46559. + * Removed references to pkt_bridged for 2.3.47 compatibility.
  46560. + *
  46561. + * Revision 1.27 2000/02/14 21:07:00 rgb
  46562. + * Fixed /proc/net/pf-key legend spacing.
  46563. + *
  46564. + * Revision 1.26 2000/01/22 03:46:59 rgb
  46565. + * Fixed pfkey error return mechanism so that we are able to free the
  46566. + * local copy of the pfkey_msg, plugging a memory leak and silencing
  46567. + * the bad object free complaints.
  46568. + *
  46569. + * Revision 1.25 2000/01/21 06:19:44 rgb
  46570. + * Moved pfkey_list_remove_socket() calls to before MOD_USE_DEC_COUNT.
  46571. + * Added debugging to pfkey_upmsg.
  46572. + *
  46573. + * Revision 1.24 2000/01/10 16:38:23 rgb
  46574. + * MB fixups for 2.3.x.
  46575. + *
  46576. + * Revision 1.23 1999/12/09 23:22:16 rgb
  46577. + * Added more instrumentation for debugging 2.0 socket
  46578. + * selection/reading.
  46579. + * Removed erroneous 2.0 wait==NULL check bug in select.
  46580. + *
  46581. + * Revision 1.22 1999/12/08 20:32:16 rgb
  46582. + * Tidied up 2.0.xx support, after major pfkey work, eliminating
  46583. + * msg->msg_name twiddling in the process, since it is not defined
  46584. + * for PF_KEYv2.
  46585. + *
  46586. + * Revision 1.21 1999/12/01 22:17:19 rgb
  46587. + * Set skb->dev to zero on new skb in case it is a reused skb.
  46588. + * Added check for skb_put overflow and freeing to avoid upmsg on error.
  46589. + * Added check for wrong pfkey version and freeing to avoid upmsg on
  46590. + * error.
  46591. + * Shut off content dumping in pfkey_destroy.
  46592. + * Added debugging message for size of buffer allocated for upmsg.
  46593. + *
  46594. + * Revision 1.20 1999/11/27 12:11:00 rgb
  46595. + * Minor clean-up, enabling quiet operation of pfkey if desired.
  46596. + *
  46597. + * Revision 1.19 1999/11/25 19:04:21 rgb
  46598. + * Update proc_fs code for pfkey to use dynamic registration.
  46599. + *
  46600. + * Revision 1.18 1999/11/25 09:07:17 rgb
  46601. + * Implemented SENDERR macro for propagating error codes.
  46602. + * Fixed error return code bug.
  46603. + *
  46604. + * Revision 1.17 1999/11/23 23:07:20 rgb
  46605. + * Change name of pfkey_msg_parser to pfkey_msg_interp since it no longer
  46606. + * parses. (PJO)
  46607. + * Sort out pfkey and freeswan headers, putting them in a library path.
  46608. + *
  46609. + * Revision 1.16 1999/11/20 22:00:22 rgb
  46610. + * Moved socketlist type declarations and prototypes for shared use.
  46611. + * Renamed reformatted and generically extended for use by other socket
  46612. + * lists pfkey_{del,add}_open_socket to pfkey_list_{remove,insert}_socket.
  46613. + *
  46614. + * Revision 1.15 1999/11/18 04:15:09 rgb
  46615. + * Make pfkey_data_ready temporarily available for 2.2.x testing.
  46616. + * Clean up pfkey_destroy_socket() debugging statements.
  46617. + * Add Peter Onion's code to send messages up to all listening sockets.
  46618. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  46619. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  46620. + * klips/net/ipsec/Makefile.
  46621. + * Replaced all kernel version macros to shorter, readable form.
  46622. + * Added CONFIG_PROC_FS compiler directives in case it is shut off.
  46623. + *
  46624. + * Revision 1.14 1999/11/17 16:01:00 rgb
  46625. + * Make pfkey_data_ready temporarily available for 2.2.x testing.
  46626. + * Clean up pfkey_destroy_socket() debugging statements.
  46627. + * Add Peter Onion's code to send messages up to all listening sockets.
  46628. + * Changed #include "../../../lib/freeswan.h" to #include <freeswan.h>
  46629. + * which works due to -Ilibfreeswan in the klips/net/ipsec/Makefile.
  46630. + *
  46631. + * Revision 1.13 1999/10/27 19:59:51 rgb
  46632. + * Removed af_unix comments that are no longer relevant.
  46633. + * Added debug prink statements.
  46634. + * Added to the /proc output in pfkey_get_info.
  46635. + * Made most functions non-static to enable oops tracing.
  46636. + * Re-enable skb dequeueing and freeing.
  46637. + * Fix skb_alloc() and skb_put() size bug in pfkey_upmsg().
  46638. + *
  46639. + * Revision 1.12 1999/10/26 17:05:42 rgb
  46640. + * Complete re-ordering based on proto_ops structure order.
  46641. + * Separated out proto_ops structures for 2.0.x and 2.2.x for clarity.
  46642. + * Simplification to use built-in socket ops where possible for 2.2.x.
  46643. + * Add shorter macros for compiler directives to visually clean-up.
  46644. + * Add lots of sk skb dequeueing debugging statements.
  46645. + * Added to the /proc output in pfkey_get_info.
  46646. + *
  46647. + * Revision 1.11 1999/09/30 02:55:10 rgb
  46648. + * Bogus skb detection.
  46649. + * Fix incorrect /proc/net/ipsec-eroute printk message.
  46650. + *
  46651. + * Revision 1.10 1999/09/21 15:22:13 rgb
  46652. + * Temporary fix while I figure out the right way to destroy sockets.
  46653. + *
  46654. + * Revision 1.9 1999/07/08 19:19:44 rgb
  46655. + * Fix pointer format warning.
  46656. + * Fix missing member error under 2.0.xx kernels.
  46657. + *
  46658. + * Revision 1.8 1999/06/13 07:24:04 rgb
  46659. + * Add more debugging.
  46660. + *
  46661. + * Revision 1.7 1999/06/10 05:24:17 rgb
  46662. + * Clarified compiler directives.
  46663. + * Renamed variables to reduce confusion.
  46664. + * Used sklist_*_socket() kernel functions to simplify 2.2.x socket support.
  46665. + * Added lots of sanity checking.
  46666. + *
  46667. + * Revision 1.6 1999/06/03 18:59:50 rgb
  46668. + * More updates to 2.2.x socket support. Almost works, oops at end of call.
  46669. + *
  46670. + * Revision 1.5 1999/05/25 22:44:05 rgb
  46671. + * Start fixing 2.2 sockets.
  46672. + *
  46673. + * Revision 1.4 1999/04/29 15:21:34 rgb
  46674. + * Move log to the end of the file.
  46675. + * Eliminate min/max redefinition in #include <net/tcp.h>.
  46676. + * Correct path for pfkey #includes
  46677. + * Standardise an error return method.
  46678. + * Add debugging instrumentation.
  46679. + * Move message type checking to pfkey_msg_parse().
  46680. + * Add check for errno incorrectly set.
  46681. + * Add check for valid PID.
  46682. + * Add check for reserved illegally set.
  46683. + * Add check for message out of bounds.
  46684. + *
  46685. + * Revision 1.3 1999/04/15 17:58:07 rgb
  46686. + * Add RCSID labels.
  46687. + *
  46688. + * Revision 1.2 1999/04/15 15:37:26 rgb
  46689. + * Forward check changes from POST1_00 branch.
  46690. + *
  46691. + * Revision 1.1.2.2 1999/04/13 20:37:12 rgb
  46692. + * Header Title correction.
  46693. + *
  46694. + * Revision 1.1.2.1 1999/03/26 20:58:55 rgb
  46695. + * Add pfkeyv2 support to KLIPS.
  46696. + *
  46697. + *
  46698. + * RFC 2367
  46699. + * PF_KEY_v2 Key Management API
  46700. + */
  46701. --- /dev/null Tue Mar 11 13:02:56 2003
  46702. +++ linux/net/ipsec/pfkey_v2_build.c Mon Feb 9 13:51:03 2004
  46703. @@ -0,0 +1,1581 @@
  46704. +/*
  46705. + * RFC2367 PF_KEYv2 Key management API message parser
  46706. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs.
  46707. + *
  46708. + * This program is free software; you can redistribute it and/or modify it
  46709. + * under the terms of the GNU General Public License as published by the
  46710. + * Free Software Foundation; either version 2 of the License, or (at your
  46711. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  46712. + *
  46713. + * This program is distributed in the hope that it will be useful, but
  46714. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  46715. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  46716. + * for more details.
  46717. + *
  46718. + * RCSID $Id: pfkey_v2_build.c,v 1.51.8.1 2006/05/01 14:36:39 mcr Exp $
  46719. + */
  46720. +
  46721. +/*
  46722. + * Template from klips/net/ipsec/ipsec/ipsec_parser.c.
  46723. + */
  46724. +
  46725. +char pfkey_v2_build_c_version[] = "$Id: pfkey_v2_build.c,v 1.51.8.1 2006/05/01 14:36:39 mcr Exp $";
  46726. +
  46727. +/*
  46728. + * Some ugly stuff to allow consistent debugging code for use in the
  46729. + * kernel and in user space
  46730. +*/
  46731. +
  46732. +#ifdef __KERNEL__
  46733. +
  46734. +# include <linux/kernel.h> /* for printk */
  46735. +
  46736. +# include "openswan/ipsec_kversion.h" /* for malloc switch */
  46737. +# ifdef MALLOC_SLAB
  46738. +# include <linux/slab.h> /* kmalloc() */
  46739. +# else /* MALLOC_SLAB */
  46740. +# include <linux/malloc.h> /* kmalloc() */
  46741. +# endif /* MALLOC_SLAB */
  46742. +# include <linux/errno.h> /* error codes */
  46743. +# include <linux/types.h> /* size_t */
  46744. +# include <linux/interrupt.h> /* mark_bh */
  46745. +
  46746. +# include <linux/netdevice.h> /* struct device, and other headers */
  46747. +# include <linux/etherdevice.h> /* eth_type_trans */
  46748. +# include <linux/ip.h> /* struct iphdr */
  46749. +# if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  46750. +# include <linux/ipv6.h> /* struct ipv6hdr */
  46751. +# endif /* if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
  46752. +
  46753. +# define MALLOC(size) kmalloc(size, GFP_ATOMIC)
  46754. +# define FREE(obj) kfree(obj)
  46755. +# include <openswan.h>
  46756. +#else /* __KERNEL__ */
  46757. +
  46758. +# include <sys/types.h>
  46759. +# include <linux/types.h>
  46760. +# include <linux/errno.h>
  46761. +# include <malloc.h>
  46762. +# include <string.h> /* memset */
  46763. +
  46764. +# include <openswan.h>
  46765. +
  46766. +#endif /* __KERNEL__ */
  46767. +
  46768. +#include <pfkeyv2.h>
  46769. +#include <pfkey.h>
  46770. +
  46771. +#ifdef __KERNEL__
  46772. +#include "openswan/radij.h" /* rd_nodes */
  46773. +#include "openswan/ipsec_encap.h" /* sockaddr_encap */
  46774. +#endif /* __KERNEL__ */
  46775. +
  46776. +
  46777. +#include "openswan/ipsec_sa.h" /* IPSEC_SAREF_NULL, IPSEC_SA_REF_TABLE_IDX_WIDTH */
  46778. +#include "openswan/pfkey_debug.h"
  46779. +
  46780. +
  46781. +#define SENDERR(_x) do { error = -(_x); goto errlab; } while (0)
  46782. +
  46783. +void
  46784. +pfkey_extensions_init(struct sadb_ext *extensions[SADB_EXT_MAX + 1])
  46785. +{
  46786. + int i;
  46787. +
  46788. + for (i = 0; i != SADB_EXT_MAX + 1; i++) {
  46789. + extensions[i] = NULL;
  46790. + }
  46791. +}
  46792. +
  46793. +void
  46794. +pfkey_extensions_free(struct sadb_ext *extensions[SADB_EXT_MAX + 1])
  46795. +{
  46796. + int i;
  46797. +
  46798. + if(!extensions) {
  46799. + return;
  46800. + }
  46801. +
  46802. + if(extensions[0]) {
  46803. + memset(extensions[0], 0, sizeof(struct sadb_msg));
  46804. + FREE(extensions[0]);
  46805. + extensions[0] = NULL;
  46806. + }
  46807. +
  46808. + for (i = 1; i != SADB_EXT_MAX + 1; i++) {
  46809. + if(extensions[i]) {
  46810. + memset(extensions[i], 0, extensions[i]->sadb_ext_len * IPSEC_PFKEYv2_ALIGN);
  46811. + FREE(extensions[i]);
  46812. + extensions[i] = NULL;
  46813. + }
  46814. + }
  46815. +}
  46816. +
  46817. +void
  46818. +pfkey_msg_free(struct sadb_msg **pfkey_msg)
  46819. +{
  46820. + if(*pfkey_msg) {
  46821. + memset(*pfkey_msg, 0, (*pfkey_msg)->sadb_msg_len * IPSEC_PFKEYv2_ALIGN);
  46822. + FREE(*pfkey_msg);
  46823. + *pfkey_msg = NULL;
  46824. + }
  46825. +}
  46826. +
  46827. +/* Default extension builders taken from the KLIPS code */
  46828. +
  46829. +int
  46830. +pfkey_msg_hdr_build(struct sadb_ext** pfkey_ext,
  46831. + uint8_t msg_type,
  46832. + uint8_t satype,
  46833. + uint8_t msg_errno,
  46834. + uint32_t seq,
  46835. + uint32_t pid)
  46836. +{
  46837. + int error = 0;
  46838. + struct sadb_msg *pfkey_msg = (struct sadb_msg *)*pfkey_ext;
  46839. +
  46840. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46841. + "pfkey_msg_hdr_build:\n");
  46842. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46843. + "pfkey_msg_hdr_build: "
  46844. + "on_entry &pfkey_ext=0p%p pfkey_ext=0p%p *pfkey_ext=0p%p.\n",
  46845. + &pfkey_ext,
  46846. + pfkey_ext,
  46847. + *pfkey_ext);
  46848. + /* sanity checks... */
  46849. + if(pfkey_msg) {
  46850. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46851. + "pfkey_msg_hdr_build: "
  46852. + "why is pfkey_msg already pointing to something?\n");
  46853. + SENDERR(EINVAL);
  46854. + }
  46855. +
  46856. + if(!msg_type) {
  46857. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46858. + "pfkey_msg_hdr_build: "
  46859. + "msg type not set, must be non-zero..\n");
  46860. + SENDERR(EINVAL);
  46861. + }
  46862. +
  46863. + if(msg_type > SADB_MAX) {
  46864. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46865. + "pfkey_msg_hdr_build: "
  46866. + "msg type too large:%d.\n",
  46867. + msg_type);
  46868. + SENDERR(EINVAL);
  46869. + }
  46870. +
  46871. + if(satype > SADB_SATYPE_MAX) {
  46872. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46873. + "pfkey_msg_hdr_build: "
  46874. + "satype %d > max %d\n",
  46875. + satype, SADB_SATYPE_MAX);
  46876. + SENDERR(EINVAL);
  46877. + }
  46878. +
  46879. + pfkey_msg = (struct sadb_msg*)MALLOC(sizeof(struct sadb_msg));
  46880. + *pfkey_ext = (struct sadb_ext*)pfkey_msg;
  46881. +
  46882. + if(pfkey_msg == NULL) {
  46883. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46884. + "pfkey_msg_hdr_build: "
  46885. + "memory allocation failed\n");
  46886. + SENDERR(ENOMEM);
  46887. + }
  46888. + memset(pfkey_msg, 0, sizeof(struct sadb_msg));
  46889. +
  46890. + pfkey_msg->sadb_msg_len = sizeof(struct sadb_msg) / IPSEC_PFKEYv2_ALIGN;
  46891. +
  46892. + pfkey_msg->sadb_msg_type = msg_type;
  46893. + pfkey_msg->sadb_msg_satype = satype;
  46894. +
  46895. + pfkey_msg->sadb_msg_version = PF_KEY_V2;
  46896. + pfkey_msg->sadb_msg_errno = msg_errno;
  46897. + pfkey_msg->sadb_msg_reserved = 0;
  46898. + pfkey_msg->sadb_msg_seq = seq;
  46899. + pfkey_msg->sadb_msg_pid = pid;
  46900. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46901. + "pfkey_msg_hdr_build: "
  46902. + "on_exit &pfkey_ext=0p%p pfkey_ext=0p%p *pfkey_ext=0p%p.\n",
  46903. + &pfkey_ext,
  46904. + pfkey_ext,
  46905. + *pfkey_ext);
  46906. +errlab:
  46907. + return error;
  46908. +}
  46909. +
  46910. +int
  46911. +pfkey_sa_ref_build(struct sadb_ext ** pfkey_ext,
  46912. + uint16_t exttype,
  46913. + uint32_t spi,
  46914. + uint8_t replay_window,
  46915. + uint8_t sa_state,
  46916. + uint8_t auth,
  46917. + uint8_t encrypt,
  46918. + uint32_t flags,
  46919. + uint32_t/*IPsecSAref_t*/ ref)
  46920. +{
  46921. + int error = 0;
  46922. + struct sadb_sa *pfkey_sa = (struct sadb_sa *)*pfkey_ext;
  46923. +
  46924. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46925. + "pfkey_sa_build: "
  46926. + "spi=%08x replay=%d sa_state=%d auth=%d encrypt=%d flags=%d\n",
  46927. + ntohl(spi), /* in network order */
  46928. + replay_window,
  46929. + sa_state,
  46930. + auth,
  46931. + encrypt,
  46932. + flags);
  46933. + /* sanity checks... */
  46934. + if(pfkey_sa) {
  46935. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46936. + "pfkey_sa_build: "
  46937. + "why is pfkey_sa already pointing to something?\n");
  46938. + SENDERR(EINVAL);
  46939. + }
  46940. +
  46941. + if(exttype != SADB_EXT_SA &&
  46942. + exttype != SADB_X_EXT_SA2) {
  46943. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46944. + "pfkey_sa_build: "
  46945. + "invalid exttype=%d.\n",
  46946. + exttype);
  46947. + SENDERR(EINVAL);
  46948. + }
  46949. +
  46950. + if(replay_window > 64) {
  46951. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46952. + "pfkey_sa_build: "
  46953. + "replay window size: %d -- must be 0 <= size <= 64\n",
  46954. + replay_window);
  46955. + SENDERR(EINVAL);
  46956. + }
  46957. +
  46958. + if(auth > SADB_AALG_MAX) {
  46959. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46960. + "pfkey_sa_build: "
  46961. + "auth=%d > SADB_AALG_MAX=%d.\n",
  46962. + auth,
  46963. + SADB_AALG_MAX);
  46964. + SENDERR(EINVAL);
  46965. + }
  46966. +
  46967. +#if SADB_EALG_MAX < 255
  46968. + if(encrypt > SADB_EALG_MAX) {
  46969. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46970. + "pfkey_sa_build: "
  46971. + "encrypt=%d > SADB_EALG_MAX=%d.\n",
  46972. + encrypt,
  46973. + SADB_EALG_MAX);
  46974. + SENDERR(EINVAL);
  46975. + }
  46976. +#endif
  46977. +
  46978. + if(sa_state > SADB_SASTATE_MAX) {
  46979. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46980. + "pfkey_sa_build: "
  46981. + "sa_state=%d exceeds MAX=%d.\n",
  46982. + sa_state,
  46983. + SADB_SASTATE_MAX);
  46984. + SENDERR(EINVAL);
  46985. + }
  46986. +
  46987. + if(sa_state == SADB_SASTATE_DEAD) {
  46988. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46989. + "pfkey_sa_build: "
  46990. + "sa_state=%d is DEAD=%d is not allowed.\n",
  46991. + sa_state,
  46992. + SADB_SASTATE_DEAD);
  46993. + SENDERR(EINVAL);
  46994. + }
  46995. +
  46996. + if((IPSEC_SAREF_NULL != ref) && (ref >= (1 << IPSEC_SA_REF_TABLE_IDX_WIDTH))) {
  46997. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  46998. + "pfkey_sa_build: "
  46999. + "SAref=%d must be (SAref == IPSEC_SAREF_NULL(%d) || SAref < IPSEC_SA_REF_TABLE_NUM_ENTRIES(%d)).\n",
  47000. + ref,
  47001. + IPSEC_SAREF_NULL,
  47002. + IPSEC_SA_REF_TABLE_NUM_ENTRIES);
  47003. + SENDERR(EINVAL);
  47004. + }
  47005. +
  47006. + pfkey_sa = (struct sadb_sa*)MALLOC(sizeof(struct sadb_sa));
  47007. + *pfkey_ext = (struct sadb_ext*)pfkey_sa;
  47008. +
  47009. + if(pfkey_sa == NULL) {
  47010. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47011. + "pfkey_sa_build: "
  47012. + "memory allocation failed\n");
  47013. + SENDERR(ENOMEM);
  47014. + }
  47015. + memset(pfkey_sa, 0, sizeof(struct sadb_sa));
  47016. +
  47017. + pfkey_sa->sadb_sa_len = sizeof(*pfkey_sa) / IPSEC_PFKEYv2_ALIGN;
  47018. + pfkey_sa->sadb_sa_exttype = exttype;
  47019. + pfkey_sa->sadb_sa_spi = spi;
  47020. + pfkey_sa->sadb_sa_replay = replay_window;
  47021. + pfkey_sa->sadb_sa_state = sa_state;
  47022. + pfkey_sa->sadb_sa_auth = auth;
  47023. + pfkey_sa->sadb_sa_encrypt = encrypt;
  47024. + pfkey_sa->sadb_sa_flags = flags;
  47025. + pfkey_sa->sadb_x_sa_ref = ref;
  47026. +
  47027. +errlab:
  47028. + return error;
  47029. +}
  47030. +
  47031. +int
  47032. +pfkey_sa_build(struct sadb_ext ** pfkey_ext,
  47033. + uint16_t exttype,
  47034. + uint32_t spi,
  47035. + uint8_t replay_window,
  47036. + uint8_t sa_state,
  47037. + uint8_t auth,
  47038. + uint8_t encrypt,
  47039. + uint32_t flags)
  47040. +{
  47041. + return pfkey_sa_ref_build(pfkey_ext,
  47042. + exttype,
  47043. + spi,
  47044. + replay_window,
  47045. + sa_state,
  47046. + auth,
  47047. + encrypt,
  47048. + flags,
  47049. + IPSEC_SAREF_NULL);
  47050. +}
  47051. +
  47052. +int
  47053. +pfkey_lifetime_build(struct sadb_ext ** pfkey_ext,
  47054. + uint16_t exttype,
  47055. + uint32_t allocations,
  47056. + uint64_t bytes,
  47057. + uint64_t addtime,
  47058. + uint64_t usetime,
  47059. + uint32_t packets)
  47060. +{
  47061. + int error = 0;
  47062. + struct sadb_lifetime *pfkey_lifetime = (struct sadb_lifetime *)*pfkey_ext;
  47063. +
  47064. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47065. + "pfkey_lifetime_build:\n");
  47066. + /* sanity checks... */
  47067. + if(pfkey_lifetime) {
  47068. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47069. + "pfkey_lifetime_build: "
  47070. + "why is pfkey_lifetime already pointing to something?\n");
  47071. + SENDERR(EINVAL);
  47072. + }
  47073. +
  47074. + if(exttype != SADB_EXT_LIFETIME_CURRENT &&
  47075. + exttype != SADB_EXT_LIFETIME_HARD &&
  47076. + exttype != SADB_EXT_LIFETIME_SOFT) {
  47077. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47078. + "pfkey_lifetime_build: "
  47079. + "invalid exttype=%d.\n",
  47080. + exttype);
  47081. + SENDERR(EINVAL);
  47082. + }
  47083. +
  47084. + pfkey_lifetime = (struct sadb_lifetime*)MALLOC(sizeof(struct sadb_lifetime));
  47085. + *pfkey_ext = (struct sadb_ext*) pfkey_lifetime;
  47086. +
  47087. + if(pfkey_lifetime == NULL) {
  47088. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47089. + "pfkey_lifetime_build: "
  47090. + "memory allocation failed\n");
  47091. + SENDERR(ENOMEM);
  47092. + }
  47093. + memset(pfkey_lifetime, 0, sizeof(struct sadb_lifetime));
  47094. +
  47095. + pfkey_lifetime->sadb_lifetime_len = sizeof(struct sadb_lifetime) / IPSEC_PFKEYv2_ALIGN;
  47096. + pfkey_lifetime->sadb_lifetime_exttype = exttype;
  47097. + pfkey_lifetime->sadb_lifetime_allocations = allocations;
  47098. + pfkey_lifetime->sadb_lifetime_bytes = bytes;
  47099. + pfkey_lifetime->sadb_lifetime_addtime = addtime;
  47100. + pfkey_lifetime->sadb_lifetime_usetime = usetime;
  47101. + pfkey_lifetime->sadb_x_lifetime_packets = packets;
  47102. +
  47103. +errlab:
  47104. + return error;
  47105. +}
  47106. +
  47107. +int
  47108. +pfkey_address_build(struct sadb_ext** pfkey_ext,
  47109. + uint16_t exttype,
  47110. + uint8_t proto,
  47111. + uint8_t prefixlen,
  47112. + struct sockaddr* address)
  47113. +{
  47114. + int error = 0;
  47115. + int saddr_len = 0;
  47116. + char ipaddr_txt[ADDRTOT_BUF + 6/*extra for port number*/];
  47117. + struct sadb_address *pfkey_address = (struct sadb_address *)*pfkey_ext;
  47118. +
  47119. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47120. + "pfkey_address_build: "
  47121. + "exttype=%d proto=%d prefixlen=%d\n",
  47122. + exttype,
  47123. + proto,
  47124. + prefixlen);
  47125. + /* sanity checks... */
  47126. + if(pfkey_address) {
  47127. + ERROR("pfkey_address_build: "
  47128. + "why is pfkey_address already pointing to something?\n");
  47129. + SENDERR(EINVAL);
  47130. + }
  47131. +
  47132. + if (!address) {
  47133. + ERROR("pfkey_address_build: " "address is NULL\n");
  47134. + SENDERR(EINVAL);
  47135. + }
  47136. +
  47137. + switch(exttype) {
  47138. + case SADB_EXT_ADDRESS_SRC:
  47139. + case SADB_EXT_ADDRESS_DST:
  47140. + case SADB_EXT_ADDRESS_PROXY:
  47141. + case SADB_X_EXT_ADDRESS_DST2:
  47142. + case SADB_X_EXT_ADDRESS_SRC_FLOW:
  47143. + case SADB_X_EXT_ADDRESS_DST_FLOW:
  47144. + case SADB_X_EXT_ADDRESS_SRC_MASK:
  47145. + case SADB_X_EXT_ADDRESS_DST_MASK:
  47146. +#ifdef NAT_TRAVERSAL
  47147. + case SADB_X_EXT_NAT_T_OA:
  47148. +#endif
  47149. + break;
  47150. + default:
  47151. + ERROR("pfkey_address_build: "
  47152. + "unrecognised ext_type=%d.\n",
  47153. + exttype);
  47154. + SENDERR(EINVAL);
  47155. + }
  47156. +
  47157. + switch(address->sa_family) {
  47158. + case AF_INET:
  47159. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47160. + "pfkey_address_build: "
  47161. + "found address family AF_INET.\n");
  47162. + saddr_len = sizeof(struct sockaddr_in);
  47163. + sprintf(ipaddr_txt, "%d.%d.%d.%d:%d"
  47164. + , (((struct sockaddr_in*)address)->sin_addr.s_addr >> 0) & 0xFF
  47165. + , (((struct sockaddr_in*)address)->sin_addr.s_addr >> 8) & 0xFF
  47166. + , (((struct sockaddr_in*)address)->sin_addr.s_addr >> 16) & 0xFF
  47167. + , (((struct sockaddr_in*)address)->sin_addr.s_addr >> 24) & 0xFF
  47168. + , ntohs(((struct sockaddr_in*)address)->sin_port));
  47169. + break;
  47170. + case AF_INET6:
  47171. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47172. + "pfkey_address_build: "
  47173. + "found address family AF_INET6.\n");
  47174. + saddr_len = sizeof(struct sockaddr_in6);
  47175. + sprintf(ipaddr_txt, "%x:%x:%x:%x:%x:%x:%x:%x-%x"
  47176. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[0])
  47177. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[1])
  47178. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[2])
  47179. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[3])
  47180. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[4])
  47181. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[5])
  47182. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[6])
  47183. + , ntohs(((struct sockaddr_in6*)address)->sin6_addr.s6_addr16[7])
  47184. + , ntohs(((struct sockaddr_in6*)address)->sin6_port));
  47185. + break;
  47186. + default:
  47187. + ERROR("pfkey_address_build: "
  47188. + "address->sa_family=%d not supported.\n",
  47189. + address->sa_family);
  47190. + SENDERR(EPFNOSUPPORT);
  47191. + }
  47192. +
  47193. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47194. + "pfkey_address_build: "
  47195. + "found address=%s.\n",
  47196. + ipaddr_txt);
  47197. + if(prefixlen != 0) {
  47198. + ERROR("pfkey_address_build: "
  47199. + "address prefixes not supported yet.\n");
  47200. + SENDERR(EAFNOSUPPORT); /* not supported yet */
  47201. + }
  47202. +
  47203. + /* allocate some memory for the extension */
  47204. + pfkey_address = (struct sadb_address*)
  47205. + MALLOC(ALIGN_N(sizeof(struct sadb_address) + saddr_len, IPSEC_PFKEYv2_ALIGN));
  47206. + *pfkey_ext = (struct sadb_ext*)pfkey_address;
  47207. +
  47208. + if(pfkey_address == NULL ) {
  47209. + ERROR("pfkey_lifetime_build: "
  47210. + "memory allocation failed\n");
  47211. + SENDERR(ENOMEM);
  47212. + }
  47213. + memset(pfkey_address,
  47214. + 0,
  47215. + ALIGN_N(sizeof(struct sadb_address) + saddr_len,
  47216. + IPSEC_PFKEYv2_ALIGN));
  47217. +
  47218. + pfkey_address->sadb_address_len = DIVUP(sizeof(struct sadb_address) + saddr_len,
  47219. + IPSEC_PFKEYv2_ALIGN);
  47220. +
  47221. + pfkey_address->sadb_address_exttype = exttype;
  47222. + pfkey_address->sadb_address_proto = proto;
  47223. + pfkey_address->sadb_address_prefixlen = prefixlen;
  47224. + pfkey_address->sadb_address_reserved = 0;
  47225. +
  47226. + memcpy((char*)pfkey_address + sizeof(struct sadb_address),
  47227. + address,
  47228. + saddr_len);
  47229. +
  47230. +#if 0
  47231. + for(i = 0; i < sizeof(struct sockaddr_in) - offsetof(struct sockaddr_in, sin_zero); i++) {
  47232. + pfkey_address_s_ska.sin_zero[i] = 0;
  47233. + }
  47234. +#endif
  47235. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47236. + "pfkey_address_build: "
  47237. + "successful created len: %d.\n", pfkey_address->sadb_address_len);
  47238. +
  47239. + errlab:
  47240. + return error;
  47241. +}
  47242. +
  47243. +int
  47244. +pfkey_key_build(struct sadb_ext** pfkey_ext,
  47245. + uint16_t exttype,
  47246. + uint16_t key_bits,
  47247. + char* key)
  47248. +{
  47249. + int error = 0;
  47250. + struct sadb_key *pfkey_key = (struct sadb_key *)*pfkey_ext;
  47251. +
  47252. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47253. + "pfkey_key_build:\n");
  47254. + /* sanity checks... */
  47255. + if(pfkey_key) {
  47256. + ERROR("pfkey_key_build: "
  47257. + "why is pfkey_key already pointing to something?\n");
  47258. + SENDERR(EINVAL);
  47259. + }
  47260. +
  47261. + if(!key_bits) {
  47262. + ERROR("pfkey_key_build: "
  47263. + "key_bits is zero, it must be non-zero.\n");
  47264. + SENDERR(EINVAL);
  47265. + }
  47266. +
  47267. + if( !((exttype == SADB_EXT_KEY_AUTH) || (exttype == SADB_EXT_KEY_ENCRYPT))) {
  47268. + ERROR("pfkey_key_build: "
  47269. + "unsupported extension type=%d.\n",
  47270. + exttype);
  47271. + SENDERR(EINVAL);
  47272. + }
  47273. +
  47274. + pfkey_key = (struct sadb_key*)
  47275. + MALLOC(sizeof(struct sadb_key) +
  47276. + DIVUP(key_bits, 64) * IPSEC_PFKEYv2_ALIGN);
  47277. +
  47278. + *pfkey_ext = (struct sadb_ext*)pfkey_key;
  47279. +
  47280. + if(pfkey_key == NULL) {
  47281. + ERROR("pfkey_key_build: "
  47282. + "memory allocation failed\n");
  47283. + SENDERR(ENOMEM);
  47284. + }
  47285. + memset(pfkey_key,
  47286. + 0,
  47287. + sizeof(struct sadb_key) +
  47288. + DIVUP(key_bits, 64) * IPSEC_PFKEYv2_ALIGN);
  47289. +
  47290. + pfkey_key->sadb_key_len = DIVUP(sizeof(struct sadb_key) * IPSEC_PFKEYv2_ALIGN + key_bits,
  47291. + 64);
  47292. + pfkey_key->sadb_key_exttype = exttype;
  47293. + pfkey_key->sadb_key_bits = key_bits;
  47294. + pfkey_key->sadb_key_reserved = 0;
  47295. + memcpy((char*)pfkey_key + sizeof(struct sadb_key),
  47296. + key,
  47297. + DIVUP(key_bits, 8));
  47298. +
  47299. +errlab:
  47300. + return error;
  47301. +}
  47302. +
  47303. +int
  47304. +pfkey_ident_build(struct sadb_ext** pfkey_ext,
  47305. + uint16_t exttype,
  47306. + uint16_t ident_type,
  47307. + uint64_t ident_id,
  47308. + uint8_t ident_len,
  47309. + char* ident_string)
  47310. +{
  47311. + int error = 0;
  47312. + struct sadb_ident *pfkey_ident = (struct sadb_ident *)*pfkey_ext;
  47313. + int data_len = ident_len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  47314. +
  47315. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47316. + "pfkey_ident_build:\n");
  47317. + /* sanity checks... */
  47318. + if(pfkey_ident) {
  47319. + ERROR("pfkey_ident_build: "
  47320. + "why is pfkey_ident already pointing to something?\n");
  47321. + SENDERR(EINVAL);
  47322. + }
  47323. +
  47324. + if( ! ((exttype == SADB_EXT_IDENTITY_SRC) ||
  47325. + (exttype == SADB_EXT_IDENTITY_DST))) {
  47326. + ERROR("pfkey_ident_build: "
  47327. + "unsupported extension type=%d.\n",
  47328. + exttype);
  47329. + SENDERR(EINVAL);
  47330. + }
  47331. +
  47332. + if((ident_type == SADB_IDENTTYPE_RESERVED)) {
  47333. + ERROR("pfkey_ident_build: "
  47334. + "ident_type must be non-zero.\n");
  47335. + SENDERR(EINVAL);
  47336. + }
  47337. +
  47338. + if(ident_type > SADB_IDENTTYPE_MAX) {
  47339. + ERROR("pfkey_ident_build: "
  47340. + "identtype=%d out of range.\n",
  47341. + ident_type);
  47342. + SENDERR(EINVAL);
  47343. + }
  47344. +
  47345. + if(((ident_type == SADB_IDENTTYPE_PREFIX) ||
  47346. + (ident_type == SADB_IDENTTYPE_FQDN)) &&
  47347. + !ident_string) {
  47348. + ERROR("pfkey_ident_build: "
  47349. + "string required to allocate size of extension.\n");
  47350. + SENDERR(EINVAL);
  47351. + }
  47352. +
  47353. +#if 0
  47354. + if((ident_type == SADB_IDENTTYPE_USERFQDN) ) {
  47355. + }
  47356. +#endif
  47357. +
  47358. + pfkey_ident = (struct sadb_ident*)
  47359. + MALLOC(ident_len * IPSEC_PFKEYv2_ALIGN);
  47360. +
  47361. + *pfkey_ext = (struct sadb_ext*)pfkey_ident;
  47362. +
  47363. + if(pfkey_ident == NULL) {
  47364. + ERROR("pfkey_ident_build: "
  47365. + "memory allocation failed\n");
  47366. + SENDERR(ENOMEM);
  47367. + }
  47368. + memset(pfkey_ident, 0, ident_len * IPSEC_PFKEYv2_ALIGN);
  47369. +
  47370. + pfkey_ident->sadb_ident_len = ident_len;
  47371. + pfkey_ident->sadb_ident_exttype = exttype;
  47372. + pfkey_ident->sadb_ident_type = ident_type;
  47373. + pfkey_ident->sadb_ident_reserved = 0;
  47374. + pfkey_ident->sadb_ident_id = ident_id;
  47375. + memcpy((char*)pfkey_ident + sizeof(struct sadb_ident),
  47376. + ident_string,
  47377. + data_len);
  47378. +
  47379. +errlab:
  47380. + return error;
  47381. +}
  47382. +
  47383. +int
  47384. +pfkey_sens_build(struct sadb_ext** pfkey_ext,
  47385. + uint32_t dpd,
  47386. + uint8_t sens_level,
  47387. + uint8_t sens_len,
  47388. + uint64_t* sens_bitmap,
  47389. + uint8_t integ_level,
  47390. + uint8_t integ_len,
  47391. + uint64_t* integ_bitmap)
  47392. +{
  47393. + int error = 0;
  47394. + struct sadb_sens *pfkey_sens = (struct sadb_sens *)*pfkey_ext;
  47395. + int i;
  47396. + uint64_t* bitmap;
  47397. +
  47398. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47399. + "pfkey_sens_build:\n");
  47400. + /* sanity checks... */
  47401. + if(pfkey_sens) {
  47402. + ERROR("pfkey_sens_build: "
  47403. + "why is pfkey_sens already pointing to something?\n");
  47404. + SENDERR(EINVAL);
  47405. + }
  47406. +
  47407. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47408. + "pfkey_sens_build: "
  47409. + "Sorry, I can't build exttype=%d yet.\n",
  47410. + (*pfkey_ext)->sadb_ext_type);
  47411. + SENDERR(EINVAL); /* don't process these yet */
  47412. +
  47413. + pfkey_sens = (struct sadb_sens*)
  47414. + MALLOC(sizeof(struct sadb_sens) +
  47415. + (sens_len + integ_len) * sizeof(uint64_t));
  47416. +
  47417. + *pfkey_ext = (struct sadb_ext*)pfkey_sens;
  47418. +
  47419. + if(pfkey_sens == NULL) {
  47420. + ERROR("pfkey_sens_build: "
  47421. + "memory allocation failed\n");
  47422. + SENDERR(ENOMEM);
  47423. + }
  47424. + memset(pfkey_sens,
  47425. + 0,
  47426. + sizeof(struct sadb_sens) +
  47427. + (sens_len + integ_len) * sizeof(uint64_t));
  47428. +
  47429. + pfkey_sens->sadb_sens_len = (sizeof(struct sadb_sens) +
  47430. + (sens_len + integ_len) * sizeof(uint64_t)) / IPSEC_PFKEYv2_ALIGN;
  47431. + pfkey_sens->sadb_sens_exttype = SADB_EXT_SENSITIVITY;
  47432. + pfkey_sens->sadb_sens_dpd = dpd;
  47433. + pfkey_sens->sadb_sens_sens_level = sens_level;
  47434. + pfkey_sens->sadb_sens_sens_len = sens_len;
  47435. + pfkey_sens->sadb_sens_integ_level = integ_level;
  47436. + pfkey_sens->sadb_sens_integ_len = integ_len;
  47437. + pfkey_sens->sadb_sens_reserved = 0;
  47438. +
  47439. + bitmap = (uint64_t*)((char*)pfkey_ext + sizeof(struct sadb_sens));
  47440. + for(i = 0; i < sens_len; i++) {
  47441. + *bitmap = sens_bitmap[i];
  47442. + bitmap++;
  47443. + }
  47444. + for(i = 0; i < integ_len; i++) {
  47445. + *bitmap = integ_bitmap[i];
  47446. + bitmap++;
  47447. + }
  47448. +
  47449. +errlab:
  47450. + return error;
  47451. +}
  47452. +
  47453. +int
  47454. +pfkey_prop_build(struct sadb_ext** pfkey_ext,
  47455. + uint8_t replay,
  47456. + unsigned int comb_num,
  47457. + struct sadb_comb* comb)
  47458. +{
  47459. + int error = 0;
  47460. + int i;
  47461. + struct sadb_prop *pfkey_prop = (struct sadb_prop *)*pfkey_ext;
  47462. + struct sadb_comb *combp;
  47463. +
  47464. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47465. + "pfkey_prop_build:\n");
  47466. + /* sanity checks... */
  47467. + if(pfkey_prop) {
  47468. + ERROR("pfkey_prop_build: "
  47469. + "why is pfkey_prop already pointing to something?\n");
  47470. + SENDERR(EINVAL);
  47471. + }
  47472. +
  47473. + pfkey_prop = (struct sadb_prop*)
  47474. + MALLOC(sizeof(struct sadb_prop) +
  47475. + comb_num * sizeof(struct sadb_comb));
  47476. +
  47477. + *pfkey_ext = (struct sadb_ext*)pfkey_prop;
  47478. +
  47479. + if(pfkey_prop == NULL) {
  47480. + ERROR("pfkey_prop_build: "
  47481. + "memory allocation failed\n");
  47482. + SENDERR(ENOMEM);
  47483. + }
  47484. + memset(pfkey_prop,
  47485. + 0,
  47486. + sizeof(struct sadb_prop) +
  47487. + comb_num * sizeof(struct sadb_comb));
  47488. +
  47489. + pfkey_prop->sadb_prop_len = (sizeof(struct sadb_prop) +
  47490. + comb_num * sizeof(struct sadb_comb)) / IPSEC_PFKEYv2_ALIGN;
  47491. +
  47492. + pfkey_prop->sadb_prop_exttype = SADB_EXT_PROPOSAL;
  47493. + pfkey_prop->sadb_prop_replay = replay;
  47494. +
  47495. + for(i=0; i<3; i++) {
  47496. + pfkey_prop->sadb_prop_reserved[i] = 0;
  47497. + }
  47498. +
  47499. + combp = (struct sadb_comb*)((char*)*pfkey_ext + sizeof(struct sadb_prop));
  47500. + for(i = 0; i < comb_num; i++) {
  47501. + memcpy (combp, &(comb[i]), sizeof(struct sadb_comb));
  47502. + combp++;
  47503. + }
  47504. +
  47505. +#if 0
  47506. + uint8_t sadb_comb_auth;
  47507. + uint8_t sadb_comb_encrypt;
  47508. + uint16_t sadb_comb_flags;
  47509. + uint16_t sadb_comb_auth_minbits;
  47510. + uint16_t sadb_comb_auth_maxbits;
  47511. + uint16_t sadb_comb_encrypt_minbits;
  47512. + uint16_t sadb_comb_encrypt_maxbits;
  47513. + uint32_t sadb_comb_reserved;
  47514. + uint32_t sadb_comb_soft_allocations;
  47515. + uint32_t sadb_comb_hard_allocations;
  47516. + uint64_t sadb_comb_soft_bytes;
  47517. + uint64_t sadb_comb_hard_bytes;
  47518. + uint64_t sadb_comb_soft_addtime;
  47519. + uint64_t sadb_comb_hard_addtime;
  47520. + uint64_t sadb_comb_soft_usetime;
  47521. + uint64_t sadb_comb_hard_usetime;
  47522. + uint32_t sadb_comb_soft_packets;
  47523. + uint32_t sadb_comb_hard_packets;
  47524. +#endif
  47525. +errlab:
  47526. + return error;
  47527. +}
  47528. +
  47529. +int
  47530. +pfkey_supported_build(struct sadb_ext** pfkey_ext,
  47531. + uint16_t exttype,
  47532. + unsigned int alg_num,
  47533. + struct sadb_alg* alg)
  47534. +{
  47535. + int error = 0;
  47536. + unsigned int i;
  47537. + struct sadb_supported *pfkey_supported = (struct sadb_supported *)*pfkey_ext;
  47538. + struct sadb_alg *pfkey_alg;
  47539. +
  47540. + /* sanity checks... */
  47541. + if(pfkey_supported) {
  47542. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47543. + "pfkey_supported_build: "
  47544. + "why is pfkey_supported already pointing to something?\n");
  47545. + SENDERR(EINVAL);
  47546. + }
  47547. +
  47548. + if( !((exttype == SADB_EXT_SUPPORTED_AUTH) || (exttype == SADB_EXT_SUPPORTED_ENCRYPT))) {
  47549. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47550. + "pfkey_supported_build: "
  47551. + "unsupported extension type=%d.\n",
  47552. + exttype);
  47553. + SENDERR(EINVAL);
  47554. + }
  47555. +
  47556. + pfkey_supported = (struct sadb_supported*)
  47557. + MALLOC(sizeof(struct sadb_supported) +
  47558. + alg_num *
  47559. + sizeof(struct sadb_alg));
  47560. +
  47561. + *pfkey_ext = (struct sadb_ext*)pfkey_supported;
  47562. +
  47563. + if(pfkey_supported == NULL) {
  47564. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47565. + "pfkey_supported_build: "
  47566. + "memory allocation failed\n");
  47567. + SENDERR(ENOMEM);
  47568. + }
  47569. + memset(pfkey_supported,
  47570. + 0,
  47571. + sizeof(struct sadb_supported) +
  47572. + alg_num *
  47573. + sizeof(struct sadb_alg));
  47574. +
  47575. + pfkey_supported->sadb_supported_len = (sizeof(struct sadb_supported) +
  47576. + alg_num *
  47577. + sizeof(struct sadb_alg)) /
  47578. + IPSEC_PFKEYv2_ALIGN;
  47579. + pfkey_supported->sadb_supported_exttype = exttype;
  47580. + pfkey_supported->sadb_supported_reserved = 0;
  47581. +
  47582. + pfkey_alg = (struct sadb_alg*)((char*)pfkey_supported + sizeof(struct sadb_supported));
  47583. + for(i = 0; i < alg_num; i++) {
  47584. + memcpy (pfkey_alg, &(alg[i]), sizeof(struct sadb_alg));
  47585. + pfkey_alg->sadb_alg_reserved = 0;
  47586. + pfkey_alg++;
  47587. + }
  47588. +
  47589. +#if 0
  47590. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47591. + "pfkey_supported_build: "
  47592. + "Sorry, I can't build exttype=%d yet.\n",
  47593. + (*pfkey_ext)->sadb_ext_type);
  47594. + SENDERR(EINVAL); /* don't process these yet */
  47595. +
  47596. + uint8_t sadb_alg_id;
  47597. + uint8_t sadb_alg_ivlen;
  47598. + uint16_t sadb_alg_minbits;
  47599. + uint16_t sadb_alg_maxbits;
  47600. + uint16_t sadb_alg_reserved;
  47601. +#endif
  47602. +errlab:
  47603. + return error;
  47604. +}
  47605. +
  47606. +int
  47607. +pfkey_spirange_build(struct sadb_ext** pfkey_ext,
  47608. + uint16_t exttype,
  47609. + uint32_t min, /* in network order */
  47610. + uint32_t max) /* in network order */
  47611. +{
  47612. + int error = 0;
  47613. + struct sadb_spirange *pfkey_spirange = (struct sadb_spirange *)*pfkey_ext;
  47614. +
  47615. + /* sanity checks... */
  47616. + if(pfkey_spirange) {
  47617. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47618. + "pfkey_spirange_build: "
  47619. + "why is pfkey_spirange already pointing to something?\n");
  47620. + SENDERR(EINVAL);
  47621. + }
  47622. +
  47623. + if(ntohl(max) < ntohl(min)) {
  47624. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47625. + "pfkey_spirange_build: "
  47626. + "minspi=%08x must be < maxspi=%08x.\n",
  47627. + ntohl(min),
  47628. + ntohl(max));
  47629. + SENDERR(EINVAL);
  47630. + }
  47631. +
  47632. + if(ntohl(min) <= 255) {
  47633. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47634. + "pfkey_spirange_build: "
  47635. + "minspi=%08x must be > 255.\n",
  47636. + ntohl(min));
  47637. + SENDERR(EEXIST);
  47638. + }
  47639. +
  47640. + pfkey_spirange = (struct sadb_spirange*)
  47641. + MALLOC(sizeof(struct sadb_spirange));
  47642. +
  47643. + *pfkey_ext = (struct sadb_ext*)pfkey_spirange;
  47644. +
  47645. + if(pfkey_spirange == NULL) {
  47646. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47647. + "pfkey_spirange_build: "
  47648. + "memory allocation failed\n");
  47649. + SENDERR(ENOMEM);
  47650. + }
  47651. + memset(pfkey_spirange,
  47652. + 0,
  47653. + sizeof(struct sadb_spirange));
  47654. +
  47655. + pfkey_spirange->sadb_spirange_len = sizeof(struct sadb_spirange) / IPSEC_PFKEYv2_ALIGN;
  47656. +
  47657. + pfkey_spirange->sadb_spirange_exttype = SADB_EXT_SPIRANGE;
  47658. + pfkey_spirange->sadb_spirange_min = min;
  47659. + pfkey_spirange->sadb_spirange_max = max;
  47660. + pfkey_spirange->sadb_spirange_reserved = 0;
  47661. + errlab:
  47662. + return error;
  47663. +}
  47664. +
  47665. +int
  47666. +pfkey_x_kmprivate_build(struct sadb_ext** pfkey_ext)
  47667. +{
  47668. + int error = 0;
  47669. + struct sadb_x_kmprivate *pfkey_x_kmprivate = (struct sadb_x_kmprivate *)*pfkey_ext;
  47670. +
  47671. + /* sanity checks... */
  47672. + if(pfkey_x_kmprivate) {
  47673. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47674. + "pfkey_x_kmprivate_build: "
  47675. + "why is pfkey_x_kmprivate already pointing to something?\n");
  47676. + SENDERR(EINVAL);
  47677. + }
  47678. +
  47679. + pfkey_x_kmprivate->sadb_x_kmprivate_reserved = 0;
  47680. +
  47681. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47682. + "pfkey_x_kmprivate_build: "
  47683. + "Sorry, I can't build exttype=%d yet.\n",
  47684. + (*pfkey_ext)->sadb_ext_type);
  47685. + SENDERR(EINVAL); /* don't process these yet */
  47686. +
  47687. + pfkey_x_kmprivate = (struct sadb_x_kmprivate*)
  47688. + MALLOC(sizeof(struct sadb_x_kmprivate));
  47689. +
  47690. + *pfkey_ext = (struct sadb_ext*)pfkey_x_kmprivate;
  47691. +
  47692. + if(pfkey_x_kmprivate == NULL) {
  47693. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47694. + "pfkey_x_kmprivate_build: "
  47695. + "memory allocation failed\n");
  47696. + SENDERR(ENOMEM);
  47697. + }
  47698. + memset(pfkey_x_kmprivate,
  47699. + 0,
  47700. + sizeof(struct sadb_x_kmprivate));
  47701. +
  47702. + pfkey_x_kmprivate->sadb_x_kmprivate_len =
  47703. + sizeof(struct sadb_x_kmprivate) / IPSEC_PFKEYv2_ALIGN;
  47704. +
  47705. + pfkey_x_kmprivate->sadb_x_kmprivate_exttype = SADB_X_EXT_KMPRIVATE;
  47706. + pfkey_x_kmprivate->sadb_x_kmprivate_reserved = 0;
  47707. +errlab:
  47708. + return error;
  47709. +}
  47710. +
  47711. +int
  47712. +pfkey_x_satype_build(struct sadb_ext** pfkey_ext,
  47713. + uint8_t satype)
  47714. +{
  47715. + int error = 0;
  47716. + int i;
  47717. + struct sadb_x_satype *pfkey_x_satype = (struct sadb_x_satype *)*pfkey_ext;
  47718. +
  47719. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47720. + "pfkey_x_satype_build:\n");
  47721. + /* sanity checks... */
  47722. + if(pfkey_x_satype) {
  47723. + ERROR("pfkey_x_satype_build: "
  47724. + "why is pfkey_x_satype already pointing to something?\n");
  47725. + SENDERR(EINVAL);
  47726. + }
  47727. +
  47728. + if(!satype) {
  47729. + ERROR("pfkey_x_satype_build: "
  47730. + "SA type not set, must be non-zero.\n");
  47731. + SENDERR(EINVAL);
  47732. + }
  47733. +
  47734. + if(satype > SADB_SATYPE_MAX) {
  47735. + ERROR("pfkey_x_satype_build: "
  47736. + "satype %d > max %d\n",
  47737. + satype, SADB_SATYPE_MAX);
  47738. + SENDERR(EINVAL);
  47739. + }
  47740. +
  47741. + pfkey_x_satype = (struct sadb_x_satype*)
  47742. + MALLOC(sizeof(struct sadb_x_satype));
  47743. +
  47744. + *pfkey_ext = (struct sadb_ext*)pfkey_x_satype;
  47745. + if(pfkey_x_satype == NULL) {
  47746. + ERROR("pfkey_x_satype_build: "
  47747. + "memory allocation failed\n");
  47748. + SENDERR(ENOMEM);
  47749. + }
  47750. + memset(pfkey_x_satype,
  47751. + 0,
  47752. + sizeof(struct sadb_x_satype));
  47753. +
  47754. + pfkey_x_satype->sadb_x_satype_len = sizeof(struct sadb_x_satype) / IPSEC_PFKEYv2_ALIGN;
  47755. +
  47756. + pfkey_x_satype->sadb_x_satype_exttype = SADB_X_EXT_SATYPE2;
  47757. + pfkey_x_satype->sadb_x_satype_satype = satype;
  47758. + for(i=0; i<3; i++) {
  47759. + pfkey_x_satype->sadb_x_satype_reserved[i] = 0;
  47760. + }
  47761. +
  47762. +errlab:
  47763. + return error;
  47764. +}
  47765. +
  47766. +int
  47767. +pfkey_x_debug_build(struct sadb_ext** pfkey_ext,
  47768. + uint32_t tunnel,
  47769. + uint32_t netlink,
  47770. + uint32_t xform,
  47771. + uint32_t eroute,
  47772. + uint32_t spi,
  47773. + uint32_t radij,
  47774. + uint32_t esp,
  47775. + uint32_t ah,
  47776. + uint32_t rcv,
  47777. + uint32_t pfkey,
  47778. + uint32_t ipcomp,
  47779. + uint32_t verbose)
  47780. +{
  47781. + int error = 0;
  47782. + int i;
  47783. + struct sadb_x_debug *pfkey_x_debug = (struct sadb_x_debug *)*pfkey_ext;
  47784. +
  47785. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47786. + "pfkey_x_debug_build:\n");
  47787. + /* sanity checks... */
  47788. + if(pfkey_x_debug) {
  47789. + ERROR("pfkey_x_debug_build: "
  47790. + "why is pfkey_x_debug already pointing to something?\n");
  47791. + SENDERR(EINVAL);
  47792. + }
  47793. +
  47794. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47795. + "pfkey_x_debug_build: "
  47796. + "tunnel=%x netlink=%x xform=%x eroute=%x spi=%x radij=%x esp=%x ah=%x rcv=%x pfkey=%x ipcomp=%x verbose=%x?\n",
  47797. + tunnel, netlink, xform, eroute, spi, radij, esp, ah, rcv, pfkey, ipcomp, verbose);
  47798. +
  47799. + pfkey_x_debug = (struct sadb_x_debug*)
  47800. + MALLOC(sizeof(struct sadb_x_debug));
  47801. +
  47802. + *pfkey_ext = (struct sadb_ext*)pfkey_x_debug;
  47803. +
  47804. + if(pfkey_x_debug == NULL) {
  47805. + ERROR("pfkey_x_debug_build: "
  47806. + "memory allocation failed\n");
  47807. + SENDERR(ENOMEM);
  47808. + }
  47809. +#if 0
  47810. + memset(pfkey_x_debug,
  47811. + 0,
  47812. + sizeof(struct sadb_x_debug));
  47813. +#endif
  47814. +
  47815. + pfkey_x_debug->sadb_x_debug_len = sizeof(struct sadb_x_debug) / IPSEC_PFKEYv2_ALIGN;
  47816. + pfkey_x_debug->sadb_x_debug_exttype = SADB_X_EXT_DEBUG;
  47817. +
  47818. + pfkey_x_debug->sadb_x_debug_tunnel = tunnel;
  47819. + pfkey_x_debug->sadb_x_debug_netlink = netlink;
  47820. + pfkey_x_debug->sadb_x_debug_xform = xform;
  47821. + pfkey_x_debug->sadb_x_debug_eroute = eroute;
  47822. + pfkey_x_debug->sadb_x_debug_spi = spi;
  47823. + pfkey_x_debug->sadb_x_debug_radij = radij;
  47824. + pfkey_x_debug->sadb_x_debug_esp = esp;
  47825. + pfkey_x_debug->sadb_x_debug_ah = ah;
  47826. + pfkey_x_debug->sadb_x_debug_rcv = rcv;
  47827. + pfkey_x_debug->sadb_x_debug_pfkey = pfkey;
  47828. + pfkey_x_debug->sadb_x_debug_ipcomp = ipcomp;
  47829. + pfkey_x_debug->sadb_x_debug_verbose = verbose;
  47830. +
  47831. + for(i=0; i<4; i++) {
  47832. + pfkey_x_debug->sadb_x_debug_reserved[i] = 0;
  47833. + }
  47834. +
  47835. +errlab:
  47836. + return error;
  47837. +}
  47838. +
  47839. +int
  47840. +pfkey_x_nat_t_type_build(struct sadb_ext** pfkey_ext,
  47841. + uint8_t type)
  47842. +{
  47843. + int error = 0;
  47844. + int i;
  47845. + struct sadb_x_nat_t_type *pfkey_x_nat_t_type = (struct sadb_x_nat_t_type *)*pfkey_ext;
  47846. +
  47847. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47848. + "pfkey_x_nat_t_type_build:\n");
  47849. + /* sanity checks... */
  47850. + if(pfkey_x_nat_t_type) {
  47851. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47852. + "pfkey_x_nat_t_type_build: "
  47853. + "why is pfkey_x_nat_t_type already pointing to something?\n");
  47854. + SENDERR(EINVAL);
  47855. + }
  47856. +
  47857. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47858. + "pfkey_x_nat_t_type_build: "
  47859. + "type=%d\n", type);
  47860. +
  47861. + pfkey_x_nat_t_type = (struct sadb_x_nat_t_type*)
  47862. + MALLOC(sizeof(struct sadb_x_nat_t_type));
  47863. +
  47864. + *pfkey_ext = (struct sadb_ext*)pfkey_x_nat_t_type;
  47865. +
  47866. + if(pfkey_x_nat_t_type == NULL) {
  47867. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47868. + "pfkey_x_nat_t_type_build: "
  47869. + "memory allocation failed\n");
  47870. + SENDERR(ENOMEM);
  47871. + }
  47872. +
  47873. + pfkey_x_nat_t_type->sadb_x_nat_t_type_len = sizeof(struct sadb_x_nat_t_type) / IPSEC_PFKEYv2_ALIGN;
  47874. + pfkey_x_nat_t_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
  47875. + pfkey_x_nat_t_type->sadb_x_nat_t_type_type = type;
  47876. + for(i=0; i<3; i++) {
  47877. + pfkey_x_nat_t_type->sadb_x_nat_t_type_reserved[i] = 0;
  47878. + }
  47879. +
  47880. +errlab:
  47881. + return error;
  47882. +}
  47883. +int
  47884. +pfkey_x_nat_t_port_build(struct sadb_ext** pfkey_ext,
  47885. + uint16_t exttype,
  47886. + uint16_t port)
  47887. +{
  47888. + int error = 0;
  47889. + struct sadb_x_nat_t_port *pfkey_x_nat_t_port = (struct sadb_x_nat_t_port *)*pfkey_ext;
  47890. +
  47891. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47892. + "pfkey_x_nat_t_port_build:\n");
  47893. + /* sanity checks... */
  47894. + if(pfkey_x_nat_t_port) {
  47895. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47896. + "pfkey_x_nat_t_port_build: "
  47897. + "why is pfkey_x_nat_t_port already pointing to something?\n");
  47898. + SENDERR(EINVAL);
  47899. + }
  47900. +
  47901. + switch(exttype) {
  47902. + case SADB_X_EXT_NAT_T_SPORT:
  47903. + case SADB_X_EXT_NAT_T_DPORT:
  47904. + break;
  47905. + default:
  47906. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47907. + "pfkey_nat_t_port_build: "
  47908. + "unrecognised ext_type=%d.\n",
  47909. + exttype);
  47910. + SENDERR(EINVAL);
  47911. + }
  47912. +
  47913. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47914. + "pfkey_x_nat_t_port_build: "
  47915. + "ext=%d, port=%d\n", exttype, port);
  47916. +
  47917. + pfkey_x_nat_t_port = (struct sadb_x_nat_t_port*)
  47918. + MALLOC(sizeof(struct sadb_x_nat_t_port));
  47919. +
  47920. + *pfkey_ext = (struct sadb_ext*)pfkey_x_nat_t_port;
  47921. +
  47922. + if(pfkey_x_nat_t_port == NULL) {
  47923. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47924. + "pfkey_x_nat_t_port_build: "
  47925. + "memory allocation failed\n");
  47926. + SENDERR(ENOMEM);
  47927. + }
  47928. +
  47929. + pfkey_x_nat_t_port->sadb_x_nat_t_port_len = sizeof(struct sadb_x_nat_t_port) / IPSEC_PFKEYv2_ALIGN;
  47930. + pfkey_x_nat_t_port->sadb_x_nat_t_port_exttype = exttype;
  47931. + pfkey_x_nat_t_port->sadb_x_nat_t_port_port = port;
  47932. + pfkey_x_nat_t_port->sadb_x_nat_t_port_reserved = 0;
  47933. +
  47934. +errlab:
  47935. + return error;
  47936. +}
  47937. +
  47938. +int pfkey_x_protocol_build(struct sadb_ext **pfkey_ext,
  47939. + uint8_t protocol)
  47940. +{
  47941. + int error = 0;
  47942. + struct sadb_protocol * p = (struct sadb_protocol *)*pfkey_ext;
  47943. + DEBUGGING(PF_KEY_DEBUG_BUILD,"pfkey_x_protocol_build: protocol=%u\n", protocol);
  47944. + /* sanity checks... */
  47945. + if (p != 0) {
  47946. + ERROR("pfkey_x_protocol_build: bogus protocol pointer\n");
  47947. + SENDERR(EINVAL);
  47948. + }
  47949. + if ((p = (struct sadb_protocol*)MALLOC(sizeof(*p))) == 0) {
  47950. + ERROR("pfkey_build: memory allocation failed\n");
  47951. + SENDERR(ENOMEM);
  47952. + }
  47953. + *pfkey_ext = (struct sadb_ext *)p;
  47954. + p->sadb_protocol_len = sizeof(*p) / sizeof(uint64_t);
  47955. + p->sadb_protocol_exttype = SADB_X_EXT_PROTOCOL;
  47956. + p->sadb_protocol_proto = protocol;
  47957. + p->sadb_protocol_flags = 0;
  47958. + p->sadb_protocol_reserved2 = 0;
  47959. + errlab:
  47960. + return error;
  47961. +}
  47962. +
  47963. +int
  47964. +pfkey_msg_build(struct sadb_msg **pfkey_msg, struct sadb_ext *extensions[], int dir)
  47965. +{
  47966. + int error = 0;
  47967. + unsigned ext;
  47968. + unsigned total_size;
  47969. + struct sadb_ext *pfkey_ext;
  47970. + int extensions_seen = 0;
  47971. +#ifndef __KERNEL__
  47972. + struct sadb_ext *extensions_check[SADB_EXT_MAX + 1];
  47973. +#endif
  47974. +
  47975. + if(!extensions[0]) {
  47976. + ERROR("pfkey_msg_build: "
  47977. + "extensions[0] must be specified (struct sadb_msg).\n");
  47978. + SENDERR(EINVAL);
  47979. + }
  47980. +
  47981. + /* figure out the total size for all the requested extensions */
  47982. + total_size = IPSEC_PFKEYv2_WORDS(sizeof(struct sadb_msg));
  47983. + for(ext = 1; ext <= SADB_EXT_MAX; ext++) {
  47984. + if(extensions[ext]) {
  47985. + total_size += (extensions[ext])->sadb_ext_len;
  47986. + }
  47987. + }
  47988. +
  47989. + /* allocate that much space */
  47990. + *pfkey_msg = (struct sadb_msg*)MALLOC(total_size * IPSEC_PFKEYv2_ALIGN);
  47991. + if(*pfkey_msg == NULL) {
  47992. + ERROR("pfkey_msg_build: "
  47993. + "memory allocation failed\n");
  47994. + SENDERR(ENOMEM);
  47995. + }
  47996. +
  47997. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  47998. + "pfkey_msg_build: "
  47999. + "pfkey_msg=0p%p allocated %lu bytes, &(extensions[0])=0p%p\n",
  48000. + *pfkey_msg,
  48001. + (unsigned long)(total_size * IPSEC_PFKEYv2_ALIGN),
  48002. + &(extensions[0]));
  48003. +
  48004. + memcpy(*pfkey_msg,
  48005. + extensions[0],
  48006. + sizeof(struct sadb_msg));
  48007. + (*pfkey_msg)->sadb_msg_len = total_size;
  48008. + (*pfkey_msg)->sadb_msg_reserved = 0;
  48009. + extensions_seen = 1 ;
  48010. +
  48011. + /*
  48012. + * point pfkey_ext to immediately after the space for the header,
  48013. + * i.e. at the first extension location.
  48014. + */
  48015. + pfkey_ext = (struct sadb_ext*)(((char*)(*pfkey_msg)) + sizeof(struct sadb_msg));
  48016. +
  48017. + for(ext = 1; ext <= SADB_EXT_MAX; ext++) {
  48018. + /* copy from extension[ext] to buffer */
  48019. + if(extensions[ext]) {
  48020. + /* Is this type of extension permitted for this type of message? */
  48021. + if(!(extensions_bitmaps[dir][EXT_BITS_PERM][(*pfkey_msg)->sadb_msg_type] &
  48022. + 1<<ext)) {
  48023. + ERROR("pfkey_msg_build: "
  48024. + "ext type %d not permitted, exts_perm=%08x, 1<<type=%08x\n",
  48025. + ext,
  48026. + extensions_bitmaps[dir][EXT_BITS_PERM][(*pfkey_msg)->sadb_msg_type],
  48027. + 1<<ext);
  48028. + SENDERR(EINVAL);
  48029. + }
  48030. +
  48031. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  48032. + "pfkey_msg_build: "
  48033. + "copying %lu bytes from extensions[%u] (type=%d)\n",
  48034. + (unsigned long)(extensions[ext]->sadb_ext_len * IPSEC_PFKEYv2_ALIGN),
  48035. + ext,
  48036. + extensions[ext]->sadb_ext_type);
  48037. +
  48038. + memcpy(pfkey_ext,
  48039. + extensions[ext],
  48040. + (extensions[ext])->sadb_ext_len * IPSEC_PFKEYv2_ALIGN);
  48041. + {
  48042. + char *pfkey_ext_c = (char *)pfkey_ext;
  48043. +
  48044. + pfkey_ext_c += (extensions[ext])->sadb_ext_len * IPSEC_PFKEYv2_ALIGN;
  48045. + pfkey_ext = (struct sadb_ext *)pfkey_ext_c;
  48046. + }
  48047. +
  48048. + /* Mark that we have seen this extension and remember the header location */
  48049. + extensions_seen |= ( 1 << ext );
  48050. + }
  48051. + }
  48052. +
  48053. + /* check required extensions */
  48054. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  48055. + "pfkey_msg_build: "
  48056. + "extensions permitted=%08x, seen=%08x, required=%08x.\n",
  48057. + extensions_bitmaps[dir][EXT_BITS_PERM][(*pfkey_msg)->sadb_msg_type],
  48058. + extensions_seen,
  48059. + extensions_bitmaps[dir][EXT_BITS_REQ][(*pfkey_msg)->sadb_msg_type]);
  48060. +
  48061. + if((extensions_seen &
  48062. + extensions_bitmaps[dir][EXT_BITS_REQ][(*pfkey_msg)->sadb_msg_type]) !=
  48063. + extensions_bitmaps[dir][EXT_BITS_REQ][(*pfkey_msg)->sadb_msg_type]) {
  48064. + DEBUGGING(PF_KEY_DEBUG_BUILD,
  48065. + "pfkey_msg_build: "
  48066. + "required extensions missing:%08x.\n",
  48067. + extensions_bitmaps[dir][EXT_BITS_REQ][(*pfkey_msg)->sadb_msg_type] -
  48068. + (extensions_seen &
  48069. + extensions_bitmaps[dir][EXT_BITS_REQ][(*pfkey_msg)->sadb_msg_type]) );
  48070. + SENDERR(EINVAL);
  48071. + }
  48072. +
  48073. +#ifndef __KERNEL__
  48074. +/*
  48075. + * this is silly, there is no need to reparse the message that we just built.
  48076. + *
  48077. + */
  48078. + if((error = pfkey_msg_parse(*pfkey_msg, NULL, extensions_check, dir))) {
  48079. + ERROR(
  48080. + "pfkey_msg_build: "
  48081. + "Trouble parsing newly built pfkey message, error=%d.\n",
  48082. + error);
  48083. + SENDERR(-error);
  48084. + }
  48085. +#endif
  48086. +
  48087. +errlab:
  48088. +
  48089. + return error;
  48090. +}
  48091. +
  48092. +/*
  48093. + * $Log: pfkey_v2_build.c,v $
  48094. + * Revision 1.51.8.1 2006/05/01 14:36:39 mcr
  48095. + * get rid of dead code.
  48096. + *
  48097. + * Revision 1.51 2004/10/03 01:26:36 mcr
  48098. + * fixes for gcc 3.4 compilation.
  48099. + *
  48100. + * Revision 1.50 2004/07/10 07:48:35 mcr
  48101. + * Moved from linux/lib/libfreeswan/pfkey_v2_build.c,v
  48102. + *
  48103. + * Revision 1.49 2004/04/12 02:59:06 mcr
  48104. + * erroneously moved pfkey_v2_build.c
  48105. + *
  48106. + * Revision 1.48 2004/04/09 18:00:40 mcr
  48107. + * Moved from linux/lib/libfreeswan/pfkey_v2_build.c,v
  48108. + *
  48109. + * Revision 1.47 2004/03/08 01:59:08 ken
  48110. + * freeswan.h -> openswan.h
  48111. + *
  48112. + * Revision 1.46 2003/12/10 01:20:19 mcr
  48113. + * NAT-traversal patches to KLIPS.
  48114. + *
  48115. + * Revision 1.45 2003/12/04 23:01:12 mcr
  48116. + * removed ipsec_netlink.h
  48117. + *
  48118. + * Revision 1.44 2003/10/31 02:27:12 mcr
  48119. + * pulled up port-selector patches and sa_id elimination.
  48120. + *
  48121. + * Revision 1.43.4.2 2003/10/29 01:11:32 mcr
  48122. + * added debugging for pfkey library.
  48123. + *
  48124. + * Revision 1.43.4.1 2003/09/21 13:59:44 mcr
  48125. + * pre-liminary X.509 patch - does not yet pass tests.
  48126. + *
  48127. + * Revision 1.43 2003/05/07 17:29:17 mcr
  48128. + * new function pfkey_debug_func added for us in debugging from
  48129. + * pfkey library.
  48130. + *
  48131. + * Revision 1.42 2003/01/30 02:32:09 rgb
  48132. + *
  48133. + * Rename SAref table macro names for clarity.
  48134. + * Convert IPsecSAref_t from signed to unsigned to fix apparent SAref exhaustion bug.
  48135. + *
  48136. + * Revision 1.41 2002/12/13 18:16:02 mcr
  48137. + * restored sa_ref code
  48138. + *
  48139. + * Revision 1.40 2002/12/13 18:06:52 mcr
  48140. + * temporarily removed sadb_x_sa_ref reference for 2.xx
  48141. + *
  48142. + * Revision 1.39 2002/12/13 17:43:28 mcr
  48143. + * commented out access to sadb_x_sa_ref for 2.xx branch
  48144. + *
  48145. + * Revision 1.38 2002/10/09 03:12:05 dhr
  48146. + *
  48147. + * [kenb+dhr] 64-bit fixes
  48148. + *
  48149. + * Revision 1.37 2002/09/20 15:40:39 rgb
  48150. + * Added new function pfkey_sa_ref_build() to accomodate saref parameter.
  48151. + *
  48152. + * Revision 1.36 2002/09/20 05:01:22 rgb
  48153. + * Generalise for platform independance: fix (ia64) using unsigned for sizes.
  48154. + *
  48155. + * Revision 1.35 2002/07/24 18:44:54 rgb
  48156. + * Type fiddling to tame ia64 compiler.
  48157. + *
  48158. + * Revision 1.34 2002/05/23 07:14:11 rgb
  48159. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  48160. + *
  48161. + * Revision 1.33 2002/04/24 07:55:32 mcr
  48162. + * #include patches and Makefiles for post-reorg compilation.
  48163. + *
  48164. + * Revision 1.32 2002/04/24 07:36:40 mcr
  48165. + * Moved from ./lib/pfkey_v2_build.c,v
  48166. + *
  48167. + * Revision 1.31 2002/01/29 22:25:35 rgb
  48168. + * Re-add ipsec_kversion.h to keep MALLOC happy.
  48169. + *
  48170. + * Revision 1.30 2002/01/29 01:59:09 mcr
  48171. + * removal of kversions.h - sources that needed it now use ipsec_param.h.
  48172. + * updating of IPv6 structures to match latest in6.h version.
  48173. + * removed dead code from openswan.h that also duplicated kversions.h
  48174. + * code.
  48175. + *
  48176. + * Revision 1.29 2001/12/19 21:06:09 rgb
  48177. + * Added port numbers to pfkey_address_build() debugging.
  48178. + *
  48179. + * Revision 1.28 2001/11/06 19:47:47 rgb
  48180. + * Added packet parameter to lifetime and comb structures.
  48181. + *
  48182. + * Revision 1.27 2001/10/18 04:45:24 rgb
  48183. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  48184. + * lib/openswan.h version macros moved to lib/kversions.h.
  48185. + * Other compiler directive cleanups.
  48186. + *
  48187. + * Revision 1.26 2001/09/08 21:13:34 rgb
  48188. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  48189. + *
  48190. + * Revision 1.25 2001/06/14 19:35:16 rgb
  48191. + * Update copyright date.
  48192. + *
  48193. + * Revision 1.24 2001/03/20 03:49:45 rgb
  48194. + * Ditch superfluous debug_pfkey declaration.
  48195. + * Move misplaced openswan.h inclusion for kernel case.
  48196. + *
  48197. + * Revision 1.23 2001/03/16 07:41:50 rgb
  48198. + * Put openswan.h include before pluto includes.
  48199. + *
  48200. + * Revision 1.22 2001/02/27 22:24:56 rgb
  48201. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  48202. + * Check for satoa() return codes.
  48203. + *
  48204. + * Revision 1.21 2000/11/17 18:10:30 rgb
  48205. + * Fixed bugs mostly relating to spirange, to treat all spi variables as
  48206. + * network byte order since this is the way PF_KEYv2 stored spis.
  48207. + *
  48208. + * Revision 1.20 2000/10/12 00:02:39 rgb
  48209. + * Removed 'format, ##' nonsense from debug macros for RH7.0.
  48210. + *
  48211. + * Revision 1.19 2000/10/10 20:10:20 rgb
  48212. + * Added support for debug_ipcomp and debug_verbose to klipsdebug.
  48213. + *
  48214. + * Revision 1.18 2000/09/12 18:59:54 rgb
  48215. + * Added Gerhard's IPv6 support to pfkey parts of libopenswan.
  48216. + *
  48217. + * Revision 1.17 2000/09/12 03:27:00 rgb
  48218. + * Moved DEBUGGING definition to compile kernel with debug off.
  48219. + *
  48220. + * Revision 1.16 2000/09/08 19:22:12 rgb
  48221. + * Fixed pfkey_prop_build() parameter to be only single indirection.
  48222. + * Fixed struct alg copy.
  48223. + *
  48224. + * Revision 1.15 2000/08/20 21:40:01 rgb
  48225. + * Added an address parameter sanity check to pfkey_address_build().
  48226. + *
  48227. + * Revision 1.14 2000/08/15 17:29:23 rgb
  48228. + * Fixes from SZI to untested pfkey_prop_build().
  48229. + *
  48230. + * Revision 1.13 2000/06/02 22:54:14 rgb
  48231. + * Added Gerhard Gessler's struct sockaddr_storage mods for IPv6 support.
  48232. + *
  48233. + * Revision 1.12 2000/05/10 19:24:01 rgb
  48234. + * Fleshed out sensitivity, proposal and supported extensions.
  48235. + *
  48236. + * Revision 1.11 2000/03/16 14:07:23 rgb
  48237. + * Renamed ALIGN macro to avoid fighting with others in kernel.
  48238. + *
  48239. + * Revision 1.10 2000/01/24 21:14:35 rgb
  48240. + * Added disabled pluto pfkey lib debug flag.
  48241. + *
  48242. + * Revision 1.9 2000/01/21 06:27:32 rgb
  48243. + * Added address cases for eroute flows.
  48244. + * Removed unused code.
  48245. + * Dropped unused argument to pfkey_x_satype_build().
  48246. + * Indented compiler directives for readability.
  48247. + * Added klipsdebug switching capability.
  48248. + * Fixed SADB_EXT_MAX bug not permitting last extension access.
  48249. + *
  48250. + * Revision 1.8 1999/12/29 21:17:41 rgb
  48251. + * Changed pfkey_msg_build() I/F to include a struct sadb_msg**
  48252. + * parameter for cleaner manipulation of extensions[] and to guard
  48253. + * against potential memory leaks.
  48254. + * Changed the I/F to pfkey_msg_free() for the same reason.
  48255. + *
  48256. + * Revision 1.7 1999/12/09 23:12:20 rgb
  48257. + * Removed unused cruft.
  48258. + * Added argument to pfkey_sa_build() to do eroutes.
  48259. + * Fixed exttype check in as yet unused pfkey_lifetime_build().
  48260. + *
  48261. + * Revision 1.6 1999/12/07 19:54:29 rgb
  48262. + * Removed static pluto debug flag.
  48263. + * Added functions for pfkey message and extensions initialisation
  48264. + * and cleanup.
  48265. + *
  48266. + * Revision 1.5 1999/12/01 22:20:06 rgb
  48267. + * Changed pfkey_sa_build to accept an SPI in network byte order.
  48268. + * Added <string.h> to quiet userspace compiler.
  48269. + * Moved pfkey_lib_debug variable into the library.
  48270. + * Removed SATYPE check from pfkey_msg_hdr_build so FLUSH will work.
  48271. + * Added extension assembly debugging.
  48272. + * Isolated assignment with brackets to be sure of scope.
  48273. + *
  48274. + * Revision 1.4 1999/11/27 11:57:35 rgb
  48275. + * Added ipv6 headers.
  48276. + * Remove over-zealous algorithm sanity checkers from pfkey_sa_build.
  48277. + * Debugging error messages added.
  48278. + * Fixed missing auth and encrypt assignment bug.
  48279. + * Add argument to pfkey_msg_parse() for direction.
  48280. + * Move parse-after-build check inside pfkey_msg_build().
  48281. + * Consolidated the 4 1-d extension bitmap arrays into one 4-d array.
  48282. + * Add CVS log entry to bottom of file.
  48283. + *
  48284. + */
  48285. --- /dev/null Tue Mar 11 13:02:56 2003
  48286. +++ linux/net/ipsec/pfkey_v2_debug.c Mon Feb 9 13:51:03 2004
  48287. @@ -0,0 +1,181 @@
  48288. +/*
  48289. + * @(#) pfkey version 2 debugging messages
  48290. + *
  48291. + * Copyright (C) 2001 Richard Guy Briggs <rgb@openswan.org>
  48292. + * and Michael Richardson <mcr@openswan.org>
  48293. + *
  48294. + * This program is free software; you can redistribute it and/or modify it
  48295. + * under the terms of the GNU General Public License as published by the
  48296. + * Free Software Foundation; either version 2 of the License, or (at your
  48297. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  48298. + *
  48299. + * This program is distributed in the hope that it will be useful, but
  48300. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  48301. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  48302. + * for more details.
  48303. + *
  48304. + * RCSID $Id: pfkey_v2_debug.c,v 1.11 2005/04/06 17:45:16 mcr Exp $
  48305. + *
  48306. + */
  48307. +
  48308. +#ifdef __KERNEL__
  48309. +
  48310. +# include <linux/kernel.h> /* for printk */
  48311. +
  48312. +# include "openswan/ipsec_kversion.h" /* for malloc switch */
  48313. +# ifdef MALLOC_SLAB
  48314. +# include <linux/slab.h> /* kmalloc() */
  48315. +# else /* MALLOC_SLAB */
  48316. +# include <linux/malloc.h> /* kmalloc() */
  48317. +# endif /* MALLOC_SLAB */
  48318. +# include <linux/errno.h> /* error codes */
  48319. +# include <linux/types.h> /* size_t */
  48320. +# include <linux/interrupt.h> /* mark_bh */
  48321. +
  48322. +# include <linux/netdevice.h> /* struct device, and other headers */
  48323. +# include <linux/etherdevice.h> /* eth_type_trans */
  48324. +extern int debug_pfkey;
  48325. +
  48326. +#else /* __KERNEL__ */
  48327. +
  48328. +# include <sys/types.h>
  48329. +# include <linux/types.h>
  48330. +# include <linux/errno.h>
  48331. +
  48332. +#endif /* __KERNEL__ */
  48333. +
  48334. +#include "openswan.h"
  48335. +#include "pfkeyv2.h"
  48336. +#include "pfkey.h"
  48337. +
  48338. +/*
  48339. + * This file provides ASCII translations of PF_KEY magic numbers.
  48340. + *
  48341. + */
  48342. +
  48343. +static char *pfkey_sadb_ext_strings[]={
  48344. + "reserved", /* SADB_EXT_RESERVED 0 */
  48345. + "security-association", /* SADB_EXT_SA 1 */
  48346. + "lifetime-current", /* SADB_EXT_LIFETIME_CURRENT 2 */
  48347. + "lifetime-hard", /* SADB_EXT_LIFETIME_HARD 3 */
  48348. + "lifetime-soft", /* SADB_EXT_LIFETIME_SOFT 4 */
  48349. + "source-address", /* SADB_EXT_ADDRESS_SRC 5 */
  48350. + "destination-address", /* SADB_EXT_ADDRESS_DST 6 */
  48351. + "proxy-address", /* SADB_EXT_ADDRESS_PROXY 7 */
  48352. + "authentication-key", /* SADB_EXT_KEY_AUTH 8 */
  48353. + "cipher-key", /* SADB_EXT_KEY_ENCRYPT 9 */
  48354. + "source-identity", /* SADB_EXT_IDENTITY_SRC 10 */
  48355. + "destination-identity", /* SADB_EXT_IDENTITY_DST 11 */
  48356. + "sensitivity-label", /* SADB_EXT_SENSITIVITY 12 */
  48357. + "proposal", /* SADB_EXT_PROPOSAL 13 */
  48358. + "supported-auth", /* SADB_EXT_SUPPORTED_AUTH 14 */
  48359. + "supported-cipher", /* SADB_EXT_SUPPORTED_ENCRYPT 15 */
  48360. + "spi-range", /* SADB_EXT_SPIRANGE 16 */
  48361. + "X-kmpprivate", /* SADB_X_EXT_KMPRIVATE 17 */
  48362. + "X-satype2", /* SADB_X_EXT_SATYPE2 18 */
  48363. + "X-security-association", /* SADB_X_EXT_SA2 19 */
  48364. + "X-destination-address2", /* SADB_X_EXT_ADDRESS_DST2 20 */
  48365. + "X-source-flow-address", /* SADB_X_EXT_ADDRESS_SRC_FLOW 21 */
  48366. + "X-dest-flow-address", /* SADB_X_EXT_ADDRESS_DST_FLOW 22 */
  48367. + "X-source-mask", /* SADB_X_EXT_ADDRESS_SRC_MASK 23 */
  48368. + "X-dest-mask", /* SADB_X_EXT_ADDRESS_DST_MASK 24 */
  48369. + "X-set-debug", /* SADB_X_EXT_DEBUG 25 */
  48370. + /* NAT_TRAVERSAL */
  48371. + "X-NAT-T-type", /* SADB_X_EXT_NAT_T_TYPE 26 */
  48372. + "X-NAT-T-sport", /* SADB_X_EXT_NAT_T_SPORT 27 */
  48373. + "X-NAT-T-dport", /* SADB_X_EXT_NAT_T_DPORT 28 */
  48374. + "X-NAT-T-OA", /* SADB_X_EXT_NAT_T_OA 29 */
  48375. +};
  48376. +
  48377. +const char *
  48378. +pfkey_v2_sadb_ext_string(int ext)
  48379. +{
  48380. + if(ext <= SADB_EXT_MAX) {
  48381. + return pfkey_sadb_ext_strings[ext];
  48382. + } else {
  48383. + return "unknown-ext";
  48384. + }
  48385. +}
  48386. +
  48387. +
  48388. +static char *pfkey_sadb_type_strings[]={
  48389. + "reserved", /* SADB_RESERVED */
  48390. + "getspi", /* SADB_GETSPI */
  48391. + "update", /* SADB_UPDATE */
  48392. + "add", /* SADB_ADD */
  48393. + "delete", /* SADB_DELETE */
  48394. + "get", /* SADB_GET */
  48395. + "acquire", /* SADB_ACQUIRE */
  48396. + "register", /* SADB_REGISTER */
  48397. + "expire", /* SADB_EXPIRE */
  48398. + "flush", /* SADB_FLUSH */
  48399. + "dump", /* SADB_DUMP */
  48400. + "x-promisc", /* SADB_X_PROMISC */
  48401. + "x-pchange", /* SADB_X_PCHANGE */
  48402. + "x-groupsa", /* SADB_X_GRPSA */
  48403. + "x-addflow(eroute)", /* SADB_X_ADDFLOW */
  48404. + "x-delflow(eroute)", /* SADB_X_DELFLOW */
  48405. + "x-debug", /* SADB_X_DEBUG */
  48406. +};
  48407. +
  48408. +const char *
  48409. +pfkey_v2_sadb_type_string(int sadb_type)
  48410. +{
  48411. + if(sadb_type <= SADB_MAX) {
  48412. + return pfkey_sadb_type_strings[sadb_type];
  48413. + } else {
  48414. + return "unknown-sadb-type";
  48415. + }
  48416. +}
  48417. +
  48418. +
  48419. +
  48420. +
  48421. +/*
  48422. + * $Log: pfkey_v2_debug.c,v $
  48423. + * Revision 1.11 2005/04/06 17:45:16 mcr
  48424. + * always include NAT-T names.
  48425. + *
  48426. + * Revision 1.10 2004/07/10 07:48:35 mcr
  48427. + * Moved from linux/lib/libfreeswan/pfkey_v2_debug.c,v
  48428. + *
  48429. + * Revision 1.9 2004/03/08 01:59:08 ken
  48430. + * freeswan.h -> openswan.h
  48431. + *
  48432. + * Revision 1.8 2003/12/10 01:20:19 mcr
  48433. + * NAT-traversal patches to KLIPS.
  48434. + *
  48435. + * Revision 1.7 2002/09/20 05:01:26 rgb
  48436. + * Fixed limit inclusion error in both type and ext string conversion.
  48437. + *
  48438. + * Revision 1.6 2002/04/24 07:55:32 mcr
  48439. + * #include patches and Makefiles for post-reorg compilation.
  48440. + *
  48441. + * Revision 1.5 2002/04/24 07:36:40 mcr
  48442. + * Moved from ./lib/pfkey_v2_debug.c,v
  48443. + *
  48444. + * Revision 1.4 2002/01/29 22:25:36 rgb
  48445. + * Re-add ipsec_kversion.h to keep MALLOC happy.
  48446. + *
  48447. + * Revision 1.3 2002/01/29 01:59:09 mcr
  48448. + * removal of kversions.h - sources that needed it now use ipsec_param.h.
  48449. + * updating of IPv6 structures to match latest in6.h version.
  48450. + * removed dead code from openswan.h that also duplicated kversions.h
  48451. + * code.
  48452. + *
  48453. + * Revision 1.2 2002/01/20 20:34:50 mcr
  48454. + * added pfkey_v2_sadb_type_string to decode sadb_type to string.
  48455. + *
  48456. + * Revision 1.1 2001/11/27 05:30:06 mcr
  48457. + * initial set of debug strings for pfkey debugging.
  48458. + * this will eventually only be included for debug builds.
  48459. + *
  48460. + * Revision 1.1 2001/09/21 04:12:03 mcr
  48461. + * first compilable version.
  48462. + *
  48463. + *
  48464. + * Local variables:
  48465. + * c-file-style: "linux"
  48466. + * End:
  48467. + *
  48468. + */
  48469. --- /dev/null Tue Mar 11 13:02:56 2003
  48470. +++ linux/net/ipsec/pfkey_v2_ext_bits.c Mon Feb 9 13:51:03 2004
  48471. @@ -0,0 +1,814 @@
  48472. +/*
  48473. + * RFC2367 PF_KEYv2 Key management API message parser
  48474. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs.
  48475. + *
  48476. + * This program is free software; you can redistribute it and/or modify it
  48477. + * under the terms of the GNU General Public License as published by the
  48478. + * Free Software Foundation; either version 2 of the License, or (at your
  48479. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  48480. + *
  48481. + * This program is distributed in the hope that it will be useful, but
  48482. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  48483. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  48484. + * for more details.
  48485. + *
  48486. + * RCSID $Id: pfkey_v2_ext_bits.c,v 1.22 2005/05/11 01:45:31 mcr Exp $
  48487. + */
  48488. +
  48489. +/*
  48490. + * Template from klips/net/ipsec/ipsec/ipsec_parse.c.
  48491. + */
  48492. +
  48493. +char pfkey_v2_ext_bits_c_version[] = "$Id: pfkey_v2_ext_bits.c,v 1.22 2005/05/11 01:45:31 mcr Exp $";
  48494. +
  48495. +/*
  48496. + * Some ugly stuff to allow consistent debugging code for use in the
  48497. + * kernel and in user space
  48498. +*/
  48499. +
  48500. +#ifdef __KERNEL__
  48501. +
  48502. +# include <linux/kernel.h> /* for printk */
  48503. +
  48504. +# include "openswan/ipsec_kversion.h" /* for malloc switch */
  48505. +# ifdef MALLOC_SLAB
  48506. +# include <linux/slab.h> /* kmalloc() */
  48507. +# else /* MALLOC_SLAB */
  48508. +# include <linux/malloc.h> /* kmalloc() */
  48509. +# endif /* MALLOC_SLAB */
  48510. +# include <linux/errno.h> /* error codes */
  48511. +# include <linux/types.h> /* size_t */
  48512. +# include <linux/interrupt.h> /* mark_bh */
  48513. +
  48514. +# include <linux/netdevice.h> /* struct device, and other headers */
  48515. +# include <linux/etherdevice.h> /* eth_type_trans */
  48516. +# include <linux/ip.h> /* struct iphdr */
  48517. +# if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  48518. +# include <linux/ipv6.h>
  48519. +# endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
  48520. +
  48521. +#else /* __KERNEL__ */
  48522. +
  48523. +# include <sys/types.h>
  48524. +# include <linux/types.h>
  48525. +# include <linux/errno.h>
  48526. +#endif
  48527. +
  48528. +#include <openswan.h>
  48529. +#include <pfkeyv2.h>
  48530. +#include <pfkey.h>
  48531. +
  48532. +unsigned int extensions_bitmaps[2/*in/out*/][2/*perm/req*/][SADB_EXTENSIONS_MAX] = {
  48533. +
  48534. +/* INBOUND EXTENSIONS */
  48535. +{
  48536. +
  48537. +/* PERMITTED IN */
  48538. +{
  48539. +/* SADB_RESERVED */
  48540. +0
  48541. +,
  48542. +/* SADB_GETSPI */
  48543. +1<<SADB_EXT_RESERVED
  48544. +| 1<<SADB_EXT_ADDRESS_SRC
  48545. +| 1<<SADB_EXT_ADDRESS_DST
  48546. +| 1<<SADB_EXT_ADDRESS_PROXY
  48547. +| 1<<SADB_EXT_SPIRANGE
  48548. +,
  48549. +/* SADB_UPDATE */
  48550. +1<<SADB_EXT_RESERVED
  48551. +| 1<<SADB_EXT_SA
  48552. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48553. +| 1<<SADB_EXT_LIFETIME_HARD
  48554. +| 1<<SADB_EXT_LIFETIME_SOFT
  48555. +| 1<<SADB_EXT_ADDRESS_SRC
  48556. +| 1<<SADB_EXT_ADDRESS_DST
  48557. +| 1<<SADB_EXT_ADDRESS_PROXY
  48558. +| 1<<SADB_EXT_KEY_AUTH
  48559. +| 1<<SADB_EXT_KEY_ENCRYPT
  48560. +| 1<<SADB_EXT_IDENTITY_SRC
  48561. +| 1<<SADB_EXT_IDENTITY_DST
  48562. +| 1<<SADB_EXT_SENSITIVITY
  48563. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48564. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48565. +,
  48566. +/* SADB_ADD */
  48567. +1<<SADB_EXT_RESERVED
  48568. +| 1<<SADB_EXT_SA
  48569. +| 1<<SADB_EXT_LIFETIME_HARD
  48570. +| 1<<SADB_EXT_LIFETIME_SOFT
  48571. +| 1<<SADB_EXT_ADDRESS_SRC
  48572. +| 1<<SADB_EXT_ADDRESS_DST
  48573. +| 1<<SADB_EXT_ADDRESS_PROXY
  48574. +| 1<<SADB_EXT_KEY_AUTH
  48575. +| 1<<SADB_EXT_KEY_ENCRYPT
  48576. +| 1<<SADB_EXT_IDENTITY_SRC
  48577. +| 1<<SADB_EXT_IDENTITY_DST
  48578. +| 1<<SADB_EXT_SENSITIVITY
  48579. +| 1<<SADB_X_EXT_NAT_T_TYPE
  48580. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48581. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48582. +| 1<<SADB_X_EXT_NAT_T_OA
  48583. +,
  48584. +/* SADB_DELETE */
  48585. +1<<SADB_EXT_RESERVED
  48586. +| 1<<SADB_EXT_SA
  48587. +| 1<<SADB_EXT_ADDRESS_SRC
  48588. +| 1<<SADB_EXT_ADDRESS_DST
  48589. +,
  48590. +/* SADB_GET */
  48591. +1<<SADB_EXT_RESERVED
  48592. +| 1<<SADB_EXT_SA
  48593. +| 1<<SADB_EXT_ADDRESS_SRC
  48594. +| 1<<SADB_EXT_ADDRESS_DST
  48595. +,
  48596. +/* SADB_ACQUIRE */
  48597. +1<<SADB_EXT_RESERVED
  48598. +| 1<<SADB_EXT_ADDRESS_SRC
  48599. +| 1<<SADB_EXT_ADDRESS_DST
  48600. +| 1<<SADB_EXT_ADDRESS_PROXY
  48601. +| 1<<SADB_EXT_IDENTITY_SRC
  48602. +| 1<<SADB_EXT_IDENTITY_DST
  48603. +| 1<<SADB_EXT_SENSITIVITY
  48604. +| 1<<SADB_EXT_PROPOSAL
  48605. +,
  48606. +/* SADB_REGISTER */
  48607. +1<<SADB_EXT_RESERVED
  48608. +,
  48609. +/* SADB_EXPIRE */
  48610. +0
  48611. +,
  48612. +/* SADB_FLUSH */
  48613. +1<<SADB_EXT_RESERVED
  48614. +,
  48615. +/* SADB_DUMP */
  48616. +1<<SADB_EXT_RESERVED
  48617. +,
  48618. +/* SADB_X_PROMISC */
  48619. +1<<SADB_EXT_RESERVED
  48620. +| 1<<SADB_EXT_SA
  48621. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48622. +| 1<<SADB_EXT_LIFETIME_HARD
  48623. +| 1<<SADB_EXT_LIFETIME_SOFT
  48624. +| 1<<SADB_EXT_ADDRESS_SRC
  48625. +| 1<<SADB_EXT_ADDRESS_DST
  48626. +| 1<<SADB_EXT_ADDRESS_PROXY
  48627. +| 1<<SADB_EXT_KEY_AUTH
  48628. +| 1<<SADB_EXT_KEY_ENCRYPT
  48629. +| 1<<SADB_EXT_IDENTITY_SRC
  48630. +| 1<<SADB_EXT_IDENTITY_DST
  48631. +| 1<<SADB_EXT_SENSITIVITY
  48632. +| 1<<SADB_EXT_PROPOSAL
  48633. +| 1<<SADB_EXT_SUPPORTED_AUTH
  48634. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  48635. +| 1<<SADB_EXT_SPIRANGE
  48636. +| 1<<SADB_X_EXT_KMPRIVATE
  48637. +| 1<<SADB_X_EXT_SATYPE2
  48638. +| 1<<SADB_X_EXT_SA2
  48639. +| 1<<SADB_X_EXT_ADDRESS_DST2
  48640. +,
  48641. +/* SADB_X_PCHANGE */
  48642. +1<<SADB_EXT_RESERVED
  48643. +| 1<<SADB_EXT_SA
  48644. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48645. +| 1<<SADB_EXT_LIFETIME_HARD
  48646. +| 1<<SADB_EXT_LIFETIME_SOFT
  48647. +| 1<<SADB_EXT_ADDRESS_SRC
  48648. +| 1<<SADB_EXT_ADDRESS_DST
  48649. +| 1<<SADB_EXT_ADDRESS_PROXY
  48650. +| 1<<SADB_EXT_KEY_AUTH
  48651. +| 1<<SADB_EXT_KEY_ENCRYPT
  48652. +| 1<<SADB_EXT_IDENTITY_SRC
  48653. +| 1<<SADB_EXT_IDENTITY_DST
  48654. +| 1<<SADB_EXT_SENSITIVITY
  48655. +| 1<<SADB_EXT_PROPOSAL
  48656. +| 1<<SADB_EXT_SUPPORTED_AUTH
  48657. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  48658. +| 1<<SADB_EXT_SPIRANGE
  48659. +| 1<<SADB_X_EXT_KMPRIVATE
  48660. +| 1<<SADB_X_EXT_SATYPE2
  48661. +| 1<<SADB_X_EXT_SA2
  48662. +| 1<<SADB_X_EXT_ADDRESS_DST2
  48663. +,
  48664. +/* SADB_X_GRPSA */
  48665. +1<<SADB_EXT_RESERVED
  48666. +| 1<<SADB_EXT_SA
  48667. +| 1<<SADB_EXT_ADDRESS_DST
  48668. +| 1<<SADB_X_EXT_SATYPE2
  48669. +| 1<<SADB_X_EXT_SA2
  48670. +| 1<<SADB_X_EXT_ADDRESS_DST2
  48671. +,
  48672. +/* SADB_X_ADDFLOW */
  48673. +1<<SADB_EXT_RESERVED
  48674. +| 1<<SADB_EXT_SA
  48675. +| 1<<SADB_EXT_ADDRESS_SRC
  48676. +| 1<<SADB_EXT_ADDRESS_DST
  48677. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  48678. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  48679. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  48680. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  48681. +| 1<<SADB_EXT_IDENTITY_SRC
  48682. +| 1<<SADB_EXT_IDENTITY_DST
  48683. +| 1<<SADB_X_EXT_PROTOCOL
  48684. +,
  48685. +/* SADB_X_DELFLOW */
  48686. +1<<SADB_EXT_RESERVED
  48687. +| 1<<SADB_EXT_SA
  48688. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  48689. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  48690. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  48691. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  48692. +| 1<<SADB_EXT_IDENTITY_SRC
  48693. +| 1<<SADB_EXT_IDENTITY_DST
  48694. +| 1<<SADB_X_EXT_PROTOCOL
  48695. +,
  48696. +/* SADB_X_DEBUG */
  48697. +1<<SADB_EXT_RESERVED
  48698. +| 1<<SADB_X_EXT_DEBUG
  48699. +,
  48700. +/* SADB_X_NAT_T_NEW_MAPPING */
  48701. +1<<SADB_EXT_RESERVED
  48702. +| 1<<SADB_EXT_SA
  48703. +| 1<<SADB_EXT_ADDRESS_SRC
  48704. +| 1<<SADB_EXT_ADDRESS_DST
  48705. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48706. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48707. +},
  48708. +
  48709. +/* REQUIRED IN */
  48710. +{
  48711. +/* SADB_RESERVED */
  48712. +0
  48713. +,
  48714. +/* SADB_GETSPI */
  48715. +1<<SADB_EXT_RESERVED
  48716. +| 1<<SADB_EXT_ADDRESS_SRC
  48717. +| 1<<SADB_EXT_ADDRESS_DST
  48718. +| 1<<SADB_EXT_SPIRANGE
  48719. +,
  48720. +/* SADB_UPDATE */
  48721. +1<<SADB_EXT_RESERVED
  48722. +| 1<<SADB_EXT_SA
  48723. +| 1<<SADB_EXT_ADDRESS_SRC
  48724. +| 1<<SADB_EXT_ADDRESS_DST
  48725. +/*| 1<<SADB_EXT_KEY_AUTH*/
  48726. +/*| 1<<SADB_EXT_KEY_ENCRYPT*/
  48727. +,
  48728. +/* SADB_ADD */
  48729. +1<<SADB_EXT_RESERVED
  48730. +| 1<<SADB_EXT_SA
  48731. +| 1<<SADB_EXT_ADDRESS_SRC
  48732. +| 1<<SADB_EXT_ADDRESS_DST
  48733. +/*| 1<<SADB_EXT_KEY_AUTH*/
  48734. +/*| 1<<SADB_EXT_KEY_ENCRYPT*/
  48735. +,
  48736. +/* SADB_DELETE */
  48737. +1<<SADB_EXT_RESERVED
  48738. +| 1<<SADB_EXT_SA
  48739. +| 1<<SADB_EXT_ADDRESS_SRC
  48740. +| 1<<SADB_EXT_ADDRESS_DST
  48741. +,
  48742. +/* SADB_GET */
  48743. +1<<SADB_EXT_RESERVED
  48744. +| 1<<SADB_EXT_SA
  48745. +| 1<<SADB_EXT_ADDRESS_SRC
  48746. +| 1<<SADB_EXT_ADDRESS_DST
  48747. +,
  48748. +/* SADB_ACQUIRE */
  48749. +1<<SADB_EXT_RESERVED
  48750. +| 1<<SADB_EXT_ADDRESS_SRC
  48751. +| 1<<SADB_EXT_ADDRESS_DST
  48752. +| 1<<SADB_EXT_PROPOSAL
  48753. +,
  48754. +/* SADB_REGISTER */
  48755. +1<<SADB_EXT_RESERVED
  48756. +,
  48757. +/* SADB_EXPIRE */
  48758. +0
  48759. +,
  48760. +/* SADB_FLUSH */
  48761. +1<<SADB_EXT_RESERVED
  48762. +,
  48763. +/* SADB_DUMP */
  48764. +1<<SADB_EXT_RESERVED
  48765. +,
  48766. +/* SADB_X_PROMISC */
  48767. +1<<SADB_EXT_RESERVED
  48768. +| 1<<SADB_EXT_SA
  48769. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48770. +| 1<<SADB_EXT_LIFETIME_HARD
  48771. +| 1<<SADB_EXT_LIFETIME_SOFT
  48772. +| 1<<SADB_EXT_ADDRESS_SRC
  48773. +| 1<<SADB_EXT_ADDRESS_DST
  48774. +| 1<<SADB_EXT_ADDRESS_PROXY
  48775. +| 1<<SADB_EXT_KEY_AUTH
  48776. +| 1<<SADB_EXT_KEY_ENCRYPT
  48777. +| 1<<SADB_EXT_IDENTITY_SRC
  48778. +| 1<<SADB_EXT_IDENTITY_DST
  48779. +| 1<<SADB_EXT_SENSITIVITY
  48780. +| 1<<SADB_EXT_PROPOSAL
  48781. +| 1<<SADB_EXT_SUPPORTED_AUTH
  48782. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  48783. +| 1<<SADB_EXT_SPIRANGE
  48784. +| 1<<SADB_X_EXT_KMPRIVATE
  48785. +| 1<<SADB_X_EXT_SATYPE2
  48786. +| 1<<SADB_X_EXT_SA2
  48787. +| 1<<SADB_X_EXT_ADDRESS_DST2
  48788. +,
  48789. +/* SADB_X_PCHANGE */
  48790. +1<<SADB_EXT_RESERVED
  48791. +| 1<<SADB_EXT_SA
  48792. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48793. +| 1<<SADB_EXT_LIFETIME_HARD
  48794. +| 1<<SADB_EXT_LIFETIME_SOFT
  48795. +| 1<<SADB_EXT_ADDRESS_SRC
  48796. +| 1<<SADB_EXT_ADDRESS_DST
  48797. +| 1<<SADB_EXT_ADDRESS_PROXY
  48798. +| 1<<SADB_EXT_KEY_AUTH
  48799. +| 1<<SADB_EXT_KEY_ENCRYPT
  48800. +| 1<<SADB_EXT_IDENTITY_SRC
  48801. +| 1<<SADB_EXT_IDENTITY_DST
  48802. +| 1<<SADB_EXT_SENSITIVITY
  48803. +| 1<<SADB_EXT_PROPOSAL
  48804. +| 1<<SADB_EXT_SUPPORTED_AUTH
  48805. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  48806. +| 1<<SADB_EXT_SPIRANGE
  48807. +| 1<<SADB_X_EXT_KMPRIVATE
  48808. +| 1<<SADB_X_EXT_SATYPE2
  48809. +| 1<<SADB_X_EXT_SA2
  48810. +| 1<<SADB_X_EXT_ADDRESS_DST2
  48811. +,
  48812. +/* SADB_X_GRPSA */
  48813. +1<<SADB_EXT_RESERVED
  48814. +| 1<<SADB_EXT_SA
  48815. +| 1<<SADB_EXT_ADDRESS_DST
  48816. +/*| 1<<SADB_X_EXT_SATYPE2*/
  48817. +/*| 1<<SADB_X_EXT_SA2*/
  48818. +/*| 1<<SADB_X_EXT_ADDRESS_DST2*/
  48819. +,
  48820. +/* SADB_X_ADDFLOW */
  48821. +1<<SADB_EXT_RESERVED
  48822. +| 1<<SADB_EXT_SA
  48823. +| 1<<SADB_EXT_ADDRESS_DST
  48824. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  48825. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  48826. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  48827. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  48828. +,
  48829. +/* SADB_X_DELFLOW */
  48830. +1<<SADB_EXT_RESERVED
  48831. +/*| 1<<SADB_EXT_SA*/
  48832. +#if 0 /* SADB_X_CLREROUTE doesn't need all these... */
  48833. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  48834. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  48835. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  48836. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  48837. +#endif
  48838. +,
  48839. +/* SADB_X_DEBUG */
  48840. +1<<SADB_EXT_RESERVED
  48841. +| 1<<SADB_X_EXT_DEBUG
  48842. +,
  48843. +/* SADB_X_NAT_T_NEW_MAPPING */
  48844. +1<<SADB_EXT_RESERVED
  48845. +| 1<<SADB_EXT_SA
  48846. +| 1<<SADB_EXT_ADDRESS_SRC
  48847. +| 1<<SADB_EXT_ADDRESS_DST
  48848. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48849. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48850. +}
  48851. +
  48852. +},
  48853. +
  48854. +/* OUTBOUND EXTENSIONS */
  48855. +{
  48856. +
  48857. +/* PERMITTED OUT */
  48858. +{
  48859. +/* SADB_RESERVED */
  48860. +0
  48861. +,
  48862. +/* SADB_GETSPI */
  48863. +1<<SADB_EXT_RESERVED
  48864. +| 1<<SADB_EXT_SA
  48865. +| 1<<SADB_EXT_ADDRESS_SRC
  48866. +| 1<<SADB_EXT_ADDRESS_DST
  48867. +,
  48868. +/* SADB_UPDATE */
  48869. +1<<SADB_EXT_RESERVED
  48870. +| 1<<SADB_EXT_SA
  48871. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48872. +| 1<<SADB_EXT_LIFETIME_HARD
  48873. +| 1<<SADB_EXT_LIFETIME_SOFT
  48874. +| 1<<SADB_EXT_ADDRESS_SRC
  48875. +| 1<<SADB_EXT_ADDRESS_DST
  48876. +| 1<<SADB_EXT_ADDRESS_PROXY
  48877. +| 1<<SADB_EXT_IDENTITY_SRC
  48878. +| 1<<SADB_EXT_IDENTITY_DST
  48879. +| 1<<SADB_EXT_SENSITIVITY
  48880. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48881. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48882. +,
  48883. +/* SADB_ADD */
  48884. +1<<SADB_EXT_RESERVED
  48885. +| 1<<SADB_EXT_SA
  48886. +| 1<<SADB_EXT_LIFETIME_HARD
  48887. +| 1<<SADB_EXT_LIFETIME_SOFT
  48888. +| 1<<SADB_EXT_ADDRESS_SRC
  48889. +| 1<<SADB_EXT_ADDRESS_DST
  48890. +| 1<<SADB_EXT_IDENTITY_SRC
  48891. +| 1<<SADB_EXT_IDENTITY_DST
  48892. +| 1<<SADB_EXT_SENSITIVITY
  48893. +| 1<<SADB_X_EXT_NAT_T_TYPE
  48894. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48895. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48896. +| 1<<SADB_X_EXT_NAT_T_OA
  48897. +,
  48898. +/* SADB_DELETE */
  48899. +1<<SADB_EXT_RESERVED
  48900. +| 1<<SADB_EXT_SA
  48901. +| 1<<SADB_EXT_ADDRESS_SRC
  48902. +| 1<<SADB_EXT_ADDRESS_DST
  48903. +,
  48904. +/* SADB_GET */
  48905. +1<<SADB_EXT_RESERVED
  48906. +| 1<<SADB_EXT_SA
  48907. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48908. +| 1<<SADB_EXT_LIFETIME_HARD
  48909. +| 1<<SADB_EXT_LIFETIME_SOFT
  48910. +| 1<<SADB_EXT_ADDRESS_SRC
  48911. +| 1<<SADB_EXT_ADDRESS_DST
  48912. +| 1<<SADB_EXT_ADDRESS_PROXY
  48913. +| 1<<SADB_EXT_KEY_AUTH
  48914. +| 1<<SADB_EXT_KEY_ENCRYPT
  48915. +| 1<<SADB_EXT_IDENTITY_SRC
  48916. +| 1<<SADB_EXT_IDENTITY_DST
  48917. +| 1<<SADB_EXT_SENSITIVITY
  48918. +| 1<<SADB_X_EXT_NAT_T_TYPE
  48919. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48920. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48921. +| 1<<SADB_X_EXT_NAT_T_OA
  48922. +,
  48923. +/* SADB_ACQUIRE */
  48924. +1<<SADB_EXT_RESERVED
  48925. +| 1<<SADB_EXT_ADDRESS_SRC
  48926. +| 1<<SADB_EXT_ADDRESS_DST
  48927. +| 1<<SADB_EXT_ADDRESS_PROXY
  48928. +| 1<<SADB_EXT_IDENTITY_SRC
  48929. +| 1<<SADB_EXT_IDENTITY_DST
  48930. +| 1<<SADB_EXT_SENSITIVITY
  48931. +| 1<<SADB_EXT_PROPOSAL
  48932. +,
  48933. +/* SADB_REGISTER */
  48934. +1<<SADB_EXT_RESERVED
  48935. +| 1<<SADB_EXT_SUPPORTED_AUTH
  48936. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  48937. +,
  48938. +/* SADB_EXPIRE */
  48939. +1<<SADB_EXT_RESERVED
  48940. +| 1<<SADB_EXT_SA
  48941. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48942. +| 1<<SADB_EXT_LIFETIME_HARD
  48943. +| 1<<SADB_EXT_LIFETIME_SOFT
  48944. +| 1<<SADB_EXT_ADDRESS_SRC
  48945. +| 1<<SADB_EXT_ADDRESS_DST
  48946. +,
  48947. +/* SADB_FLUSH */
  48948. +1<<SADB_EXT_RESERVED
  48949. +,
  48950. +/* SADB_DUMP */
  48951. +1<<SADB_EXT_RESERVED
  48952. +| 1<<SADB_EXT_SA
  48953. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48954. +| 1<<SADB_EXT_LIFETIME_HARD
  48955. +| 1<<SADB_EXT_LIFETIME_SOFT
  48956. +| 1<<SADB_EXT_ADDRESS_SRC
  48957. +| 1<<SADB_EXT_ADDRESS_DST
  48958. +| 1<<SADB_EXT_ADDRESS_PROXY
  48959. +| 1<<SADB_EXT_KEY_AUTH
  48960. +| 1<<SADB_EXT_KEY_ENCRYPT
  48961. +| 1<<SADB_EXT_IDENTITY_SRC
  48962. +| 1<<SADB_EXT_IDENTITY_DST
  48963. +| 1<<SADB_EXT_SENSITIVITY
  48964. +| 1<<SADB_X_EXT_NAT_T_TYPE
  48965. +| 1<<SADB_X_EXT_NAT_T_SPORT
  48966. +| 1<<SADB_X_EXT_NAT_T_DPORT
  48967. +| 1<<SADB_X_EXT_NAT_T_OA
  48968. +,
  48969. +/* SADB_X_PROMISC */
  48970. +1<<SADB_EXT_RESERVED
  48971. +| 1<<SADB_EXT_SA
  48972. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48973. +| 1<<SADB_EXT_LIFETIME_HARD
  48974. +| 1<<SADB_EXT_LIFETIME_SOFT
  48975. +| 1<<SADB_EXT_ADDRESS_SRC
  48976. +| 1<<SADB_EXT_ADDRESS_DST
  48977. +| 1<<SADB_EXT_ADDRESS_PROXY
  48978. +| 1<<SADB_EXT_KEY_AUTH
  48979. +| 1<<SADB_EXT_KEY_ENCRYPT
  48980. +| 1<<SADB_EXT_IDENTITY_SRC
  48981. +| 1<<SADB_EXT_IDENTITY_DST
  48982. +| 1<<SADB_EXT_SENSITIVITY
  48983. +| 1<<SADB_EXT_PROPOSAL
  48984. +| 1<<SADB_EXT_SUPPORTED_AUTH
  48985. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  48986. +| 1<<SADB_EXT_SPIRANGE
  48987. +| 1<<SADB_X_EXT_KMPRIVATE
  48988. +| 1<<SADB_X_EXT_SATYPE2
  48989. +| 1<<SADB_X_EXT_SA2
  48990. +| 1<<SADB_X_EXT_ADDRESS_DST2
  48991. +,
  48992. +/* SADB_X_PCHANGE */
  48993. +1<<SADB_EXT_RESERVED
  48994. +| 1<<SADB_EXT_SA
  48995. +| 1<<SADB_EXT_LIFETIME_CURRENT
  48996. +| 1<<SADB_EXT_LIFETIME_HARD
  48997. +| 1<<SADB_EXT_LIFETIME_SOFT
  48998. +| 1<<SADB_EXT_ADDRESS_SRC
  48999. +| 1<<SADB_EXT_ADDRESS_DST
  49000. +| 1<<SADB_EXT_ADDRESS_PROXY
  49001. +| 1<<SADB_EXT_KEY_AUTH
  49002. +| 1<<SADB_EXT_KEY_ENCRYPT
  49003. +| 1<<SADB_EXT_IDENTITY_SRC
  49004. +| 1<<SADB_EXT_IDENTITY_DST
  49005. +| 1<<SADB_EXT_SENSITIVITY
  49006. +| 1<<SADB_EXT_PROPOSAL
  49007. +| 1<<SADB_EXT_SUPPORTED_AUTH
  49008. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  49009. +| 1<<SADB_EXT_SPIRANGE
  49010. +| 1<<SADB_X_EXT_KMPRIVATE
  49011. +| 1<<SADB_X_EXT_SATYPE2
  49012. +| 1<<SADB_X_EXT_SA2
  49013. +| 1<<SADB_X_EXT_ADDRESS_DST2
  49014. +,
  49015. +/* SADB_X_GRPSA */
  49016. +1<<SADB_EXT_RESERVED
  49017. +| 1<<SADB_EXT_SA
  49018. +| 1<<SADB_EXT_ADDRESS_DST
  49019. +| 1<<SADB_X_EXT_SATYPE2
  49020. +| 1<<SADB_X_EXT_SA2
  49021. +| 1<<SADB_X_EXT_ADDRESS_DST2
  49022. +,
  49023. +/* SADB_X_ADDFLOW */
  49024. +1<<SADB_EXT_RESERVED
  49025. +| 1<<SADB_EXT_SA
  49026. +| 1<<SADB_EXT_ADDRESS_SRC
  49027. +| 1<<SADB_EXT_ADDRESS_DST
  49028. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  49029. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  49030. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  49031. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  49032. +| 1<<SADB_X_EXT_PROTOCOL
  49033. +,
  49034. +/* SADB_X_DELFLOW */
  49035. +1<<SADB_EXT_RESERVED
  49036. +| 1<<SADB_EXT_SA
  49037. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  49038. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  49039. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  49040. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  49041. +| 1<<SADB_X_EXT_PROTOCOL
  49042. +,
  49043. +/* SADB_X_DEBUG */
  49044. +1<<SADB_EXT_RESERVED
  49045. +| 1<<SADB_X_EXT_DEBUG
  49046. +,
  49047. +/* SADB_X_NAT_T_NEW_MAPPING */
  49048. +1<<SADB_EXT_RESERVED
  49049. +| 1<<SADB_EXT_SA
  49050. +| 1<<SADB_EXT_ADDRESS_SRC
  49051. +| 1<<SADB_EXT_ADDRESS_DST
  49052. +| 1<<SADB_X_EXT_NAT_T_SPORT
  49053. +| 1<<SADB_X_EXT_NAT_T_DPORT
  49054. +},
  49055. +
  49056. +/* REQUIRED OUT */
  49057. +{
  49058. +/* SADB_RESERVED */
  49059. +0
  49060. +,
  49061. +/* SADB_GETSPI */
  49062. +1<<SADB_EXT_RESERVED
  49063. +| 1<<SADB_EXT_SA
  49064. +| 1<<SADB_EXT_ADDRESS_SRC
  49065. +| 1<<SADB_EXT_ADDRESS_DST
  49066. +,
  49067. +/* SADB_UPDATE */
  49068. +1<<SADB_EXT_RESERVED
  49069. +| 1<<SADB_EXT_SA
  49070. +| 1<<SADB_EXT_ADDRESS_SRC
  49071. +| 1<<SADB_EXT_ADDRESS_DST
  49072. +,
  49073. +/* SADB_ADD */
  49074. +1<<SADB_EXT_RESERVED
  49075. +| 1<<SADB_EXT_SA
  49076. +| 1<<SADB_EXT_ADDRESS_SRC
  49077. +| 1<<SADB_EXT_ADDRESS_DST
  49078. +,
  49079. +/* SADB_DELETE */
  49080. +1<<SADB_EXT_RESERVED
  49081. +| 1<<SADB_EXT_SA
  49082. +| 1<<SADB_EXT_ADDRESS_SRC
  49083. +| 1<<SADB_EXT_ADDRESS_DST
  49084. +,
  49085. +/* SADB_GET */
  49086. +1<<SADB_EXT_RESERVED
  49087. +| 1<<SADB_EXT_SA
  49088. +| 1<<SADB_EXT_ADDRESS_SRC
  49089. +| 1<<SADB_EXT_ADDRESS_DST
  49090. +/* | 1<<SADB_EXT_KEY_AUTH */
  49091. +/* | 1<<SADB_EXT_KEY_ENCRYPT */
  49092. +,
  49093. +/* SADB_ACQUIRE */
  49094. +1<<SADB_EXT_RESERVED
  49095. +| 1<<SADB_EXT_ADDRESS_SRC
  49096. +| 1<<SADB_EXT_ADDRESS_DST
  49097. +| 1<<SADB_EXT_PROPOSAL
  49098. +,
  49099. +/* SADB_REGISTER */
  49100. +1<<SADB_EXT_RESERVED
  49101. +/* | 1<<SADB_EXT_SUPPORTED_AUTH
  49102. + | 1<<SADB_EXT_SUPPORTED_ENCRYPT */
  49103. +,
  49104. +/* SADB_EXPIRE */
  49105. +1<<SADB_EXT_RESERVED
  49106. +| 1<<SADB_EXT_SA
  49107. +| 1<<SADB_EXT_LIFETIME_CURRENT
  49108. +/* | 1<<SADB_EXT_LIFETIME_HARD
  49109. + | 1<<SADB_EXT_LIFETIME_SOFT */
  49110. +| 1<<SADB_EXT_ADDRESS_SRC
  49111. +| 1<<SADB_EXT_ADDRESS_DST
  49112. +,
  49113. +/* SADB_FLUSH */
  49114. +1<<SADB_EXT_RESERVED
  49115. +,
  49116. +/* SADB_DUMP */
  49117. +1<<SADB_EXT_RESERVED
  49118. +| 1<<SADB_EXT_SA
  49119. +| 1<<SADB_EXT_ADDRESS_SRC
  49120. +| 1<<SADB_EXT_ADDRESS_DST
  49121. +| 1<<SADB_EXT_KEY_AUTH
  49122. +| 1<<SADB_EXT_KEY_ENCRYPT
  49123. +,
  49124. +/* SADB_X_PROMISC */
  49125. +1<<SADB_EXT_RESERVED
  49126. +| 1<<SADB_EXT_SA
  49127. +| 1<<SADB_EXT_LIFETIME_CURRENT
  49128. +| 1<<SADB_EXT_LIFETIME_HARD
  49129. +| 1<<SADB_EXT_LIFETIME_SOFT
  49130. +| 1<<SADB_EXT_ADDRESS_SRC
  49131. +| 1<<SADB_EXT_ADDRESS_DST
  49132. +| 1<<SADB_EXT_ADDRESS_PROXY
  49133. +| 1<<SADB_EXT_KEY_AUTH
  49134. +| 1<<SADB_EXT_KEY_ENCRYPT
  49135. +| 1<<SADB_EXT_IDENTITY_SRC
  49136. +| 1<<SADB_EXT_IDENTITY_DST
  49137. +| 1<<SADB_EXT_SENSITIVITY
  49138. +| 1<<SADB_EXT_PROPOSAL
  49139. +| 1<<SADB_EXT_SUPPORTED_AUTH
  49140. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  49141. +| 1<<SADB_EXT_SPIRANGE
  49142. +| 1<<SADB_X_EXT_KMPRIVATE
  49143. +| 1<<SADB_X_EXT_SATYPE2
  49144. +| 1<<SADB_X_EXT_SA2
  49145. +| 1<<SADB_X_EXT_ADDRESS_DST2
  49146. +,
  49147. +/* SADB_X_PCHANGE */
  49148. +1<<SADB_EXT_RESERVED
  49149. +| 1<<SADB_EXT_SA
  49150. +| 1<<SADB_EXT_LIFETIME_CURRENT
  49151. +| 1<<SADB_EXT_LIFETIME_HARD
  49152. +| 1<<SADB_EXT_LIFETIME_SOFT
  49153. +| 1<<SADB_EXT_ADDRESS_SRC
  49154. +| 1<<SADB_EXT_ADDRESS_DST
  49155. +| 1<<SADB_EXT_ADDRESS_PROXY
  49156. +| 1<<SADB_EXT_KEY_AUTH
  49157. +| 1<<SADB_EXT_KEY_ENCRYPT
  49158. +| 1<<SADB_EXT_IDENTITY_SRC
  49159. +| 1<<SADB_EXT_IDENTITY_DST
  49160. +| 1<<SADB_EXT_SENSITIVITY
  49161. +| 1<<SADB_EXT_PROPOSAL
  49162. +| 1<<SADB_EXT_SUPPORTED_AUTH
  49163. +| 1<<SADB_EXT_SUPPORTED_ENCRYPT
  49164. +| 1<<SADB_EXT_SPIRANGE
  49165. +| 1<<SADB_X_EXT_KMPRIVATE
  49166. +| 1<<SADB_X_EXT_SATYPE2
  49167. +| 1<<SADB_X_EXT_SA2
  49168. +| 1<<SADB_X_EXT_ADDRESS_DST2
  49169. +,
  49170. +/* SADB_X_GRPSA */
  49171. +1<<SADB_EXT_RESERVED
  49172. +| 1<<SADB_EXT_SA
  49173. +| 1<<SADB_EXT_ADDRESS_DST
  49174. +,
  49175. +/* SADB_X_ADDFLOW */
  49176. +1<<SADB_EXT_RESERVED
  49177. +| 1<<SADB_EXT_SA
  49178. +| 1<<SADB_EXT_ADDRESS_DST
  49179. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  49180. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  49181. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  49182. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  49183. +,
  49184. +/* SADB_X_DELFLOW */
  49185. +1<<SADB_EXT_RESERVED
  49186. +/*| 1<<SADB_EXT_SA*/
  49187. +| 1<<SADB_X_EXT_ADDRESS_SRC_FLOW
  49188. +| 1<<SADB_X_EXT_ADDRESS_DST_FLOW
  49189. +| 1<<SADB_X_EXT_ADDRESS_SRC_MASK
  49190. +| 1<<SADB_X_EXT_ADDRESS_DST_MASK
  49191. +,
  49192. +/* SADB_X_DEBUG */
  49193. +1<<SADB_EXT_RESERVED
  49194. +| 1<<SADB_X_EXT_DEBUG
  49195. +,
  49196. +/* SADB_X_NAT_T_NEW_MAPPING */
  49197. +1<<SADB_EXT_RESERVED
  49198. +| 1<<SADB_EXT_SA
  49199. +| 1<<SADB_EXT_ADDRESS_SRC
  49200. +| 1<<SADB_EXT_ADDRESS_DST
  49201. +| 1<<SADB_X_EXT_NAT_T_SPORT
  49202. +| 1<<SADB_X_EXT_NAT_T_DPORT
  49203. +}
  49204. +}
  49205. +};
  49206. +
  49207. +/*
  49208. + * $Log: pfkey_v2_ext_bits.c,v $
  49209. + * Revision 1.22 2005/05/11 01:45:31 mcr
  49210. + * make pfkey.h standalone.
  49211. + *
  49212. + * Revision 1.21 2004/07/10 07:48:36 mcr
  49213. + * Moved from linux/lib/libfreeswan/pfkey_v2_ext_bits.c,v
  49214. + *
  49215. + * Revision 1.20 2004/03/08 01:59:08 ken
  49216. + * freeswan.h -> openswan.h
  49217. + *
  49218. + * Revision 1.19 2003/12/22 21:38:13 mcr
  49219. + * removed extraenous #endif.
  49220. + *
  49221. + * Revision 1.18 2003/12/22 19:34:41 mcr
  49222. + * added 0.6c NAT-T patch.
  49223. + *
  49224. + * Revision 1.17 2003/12/10 01:20:19 mcr
  49225. + * NAT-traversal patches to KLIPS.
  49226. + *
  49227. + * Revision 1.16 2003/10/31 02:27:12 mcr
  49228. + * pulled up port-selector patches and sa_id elimination.
  49229. + *
  49230. + * Revision 1.15.30.1 2003/09/21 13:59:44 mcr
  49231. + * pre-liminary X.509 patch - does not yet pass tests.
  49232. + *
  49233. + * Revision 1.15 2002/04/24 07:55:32 mcr
  49234. + * #include patches and Makefiles for post-reorg compilation.
  49235. + *
  49236. + * Revision 1.14 2002/04/24 07:36:40 mcr
  49237. + * Moved from ./lib/pfkey_v2_ext_bits.c,v
  49238. + *
  49239. + * Revision 1.13 2002/01/29 22:25:36 rgb
  49240. + * Re-add ipsec_kversion.h to keep MALLOC happy.
  49241. + *
  49242. + * Revision 1.12 2002/01/29 01:59:10 mcr
  49243. + * removal of kversions.h - sources that needed it now use ipsec_param.h.
  49244. + * updating of IPv6 structures to match latest in6.h version.
  49245. + * removed dead code from openswan.h that also duplicated kversions.h
  49246. + * code.
  49247. + *
  49248. + * Revision 1.11 2001/10/18 04:45:24 rgb
  49249. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  49250. + * lib/openswan.h version macros moved to lib/kversions.h.
  49251. + * Other compiler directive cleanups.
  49252. + *
  49253. + * Revision 1.10 2001/09/08 21:13:35 rgb
  49254. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  49255. + *
  49256. + * Revision 1.9 2001/06/14 19:35:16 rgb
  49257. + * Update copyright date.
  49258. + *
  49259. + * Revision 1.8 2001/03/26 23:07:36 rgb
  49260. + * Remove requirement for auth and enc key from UPDATE.
  49261. + *
  49262. + * Revision 1.7 2000/09/12 22:35:37 rgb
  49263. + * Restructured to remove unused extensions from CLEARFLOW messages.
  49264. + *
  49265. + * Revision 1.6 2000/09/09 06:39:01 rgb
  49266. + * Added comments for clarity.
  49267. + *
  49268. + * Revision 1.5 2000/06/02 22:54:14 rgb
  49269. + * Added Gerhard Gessler's struct sockaddr_storage mods for IPv6 support.
  49270. + *
  49271. + * Revision 1.4 2000/01/21 06:27:56 rgb
  49272. + * Added address cases for eroute flows.
  49273. + * Added comments for each message type.
  49274. + * Added klipsdebug switching capability.
  49275. + * Fixed GRPSA bitfields.
  49276. + *
  49277. + * Revision 1.3 1999/12/01 22:20:27 rgb
  49278. + * Remove requirement for a proxy address in an incoming getspi message.
  49279. + *
  49280. + * Revision 1.2 1999/11/27 11:57:06 rgb
  49281. + * Consolidated the 4 1-d extension bitmap arrays into one 4-d array.
  49282. + * Add CVS log entry to bottom of file.
  49283. + * Cleaned out unused bits.
  49284. + *
  49285. + */
  49286. --- /dev/null Tue Mar 11 13:02:56 2003
  49287. +++ linux/net/ipsec/pfkey_v2_ext_process.c Mon Feb 9 13:51:03 2004
  49288. @@ -0,0 +1,951 @@
  49289. +/*
  49290. + * @(#) RFC2367 PF_KEYv2 Key management API message parser
  49291. + * Copyright (C) 1998-2003 Richard Guy Briggs.
  49292. + * Copyright (C) 2004 Michael Richardson <mcr@xelerance.com>
  49293. + *
  49294. + * This program is free software; you can redistribute it and/or modify it
  49295. + * under the terms of the GNU General Public License as published by the
  49296. + * Free Software Foundation; either version 2 of the License, or (at your
  49297. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  49298. + *
  49299. + * This program is distributed in the hope that it will be useful, but
  49300. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  49301. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  49302. + * for more details.
  49303. + *
  49304. + * RCSID $Id: pfkey_v2_ext_process.c,v 1.20.2.2 2006/10/06 21:39:26 paul Exp $
  49305. + */
  49306. +
  49307. +/*
  49308. + * Template from klips/net/ipsec/ipsec/ipsec_netlink.c.
  49309. + */
  49310. +
  49311. +char pfkey_v2_ext_process_c_version[] = "$Id: pfkey_v2_ext_process.c,v 1.20.2.2 2006/10/06 21:39:26 paul Exp $";
  49312. +
  49313. +#ifndef AUTOCONF_INCLUDED
  49314. +#include <linux/config.h>
  49315. +#endif
  49316. +#include <linux/version.h>
  49317. +#include <linux/kernel.h> /* printk() */
  49318. +
  49319. +#include "openswan/ipsec_param.h"
  49320. +
  49321. +#ifdef MALLOC_SLAB
  49322. +# include <linux/slab.h> /* kmalloc() */
  49323. +#else /* MALLOC_SLAB */
  49324. +# include <linux/malloc.h> /* kmalloc() */
  49325. +#endif /* MALLOC_SLAB */
  49326. +#include <linux/errno.h> /* error codes */
  49327. +#include <linux/types.h> /* size_t */
  49328. +#include <linux/interrupt.h> /* mark_bh */
  49329. +
  49330. +#include <linux/netdevice.h> /* struct device, and other headers */
  49331. +#include <linux/etherdevice.h> /* eth_type_trans */
  49332. +#include <linux/ip.h> /* struct iphdr */
  49333. +#include <linux/skbuff.h>
  49334. +
  49335. +#include <openswan.h>
  49336. +
  49337. +#include <crypto/des.h>
  49338. +
  49339. +#ifdef SPINLOCK
  49340. +# ifdef SPINLOCK_23
  49341. +# include <linux/spinlock.h> /* *lock* */
  49342. +# else /* SPINLOCK_23 */
  49343. +# include <asm/spinlock.h> /* *lock* */
  49344. +# endif /* SPINLOCK_23 */
  49345. +#endif /* SPINLOCK */
  49346. +#ifdef NET_21
  49347. +# include <linux/in6.h>
  49348. +# define ip_chk_addr inet_addr_type
  49349. +# define IS_MYADDR RTN_LOCAL
  49350. +#endif
  49351. +
  49352. +#include <net/ip.h>
  49353. +#ifdef NETLINK_SOCK
  49354. +# include <linux/netlink.h>
  49355. +#else
  49356. +# include <net/netlink.h>
  49357. +#endif
  49358. +
  49359. +#include <linux/random.h> /* get_random_bytes() */
  49360. +
  49361. +#include "openswan/radij.h"
  49362. +#include "openswan/ipsec_encap.h"
  49363. +#include "openswan/ipsec_sa.h"
  49364. +
  49365. +#include "openswan/ipsec_radij.h"
  49366. +#include "openswan/ipsec_xform.h"
  49367. +#include "openswan/ipsec_ah.h"
  49368. +#include "openswan/ipsec_esp.h"
  49369. +#include "openswan/ipsec_tunnel.h"
  49370. +#include "openswan/ipsec_rcv.h"
  49371. +#include "openswan/ipcomp.h"
  49372. +
  49373. +#include <pfkeyv2.h>
  49374. +#include <pfkey.h>
  49375. +
  49376. +#include "openswan/ipsec_proto.h"
  49377. +#include "openswan/ipsec_alg.h"
  49378. +
  49379. +#define SENDERR(_x) do { error = -(_x); goto errlab; } while (0)
  49380. +
  49381. +int
  49382. +pfkey_sa_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49383. +{
  49384. + struct sadb_sa *pfkey_sa = (struct sadb_sa *)pfkey_ext;
  49385. + int error = 0;
  49386. + struct ipsec_sa* ipsp;
  49387. +
  49388. + KLIPS_PRINT(debug_pfkey,
  49389. + "klips_debug:pfkey_sa_process: .\n");
  49390. +
  49391. + if(!extr || !extr->ips) {
  49392. + KLIPS_PRINT(debug_pfkey,
  49393. + "klips_debug:pfkey_sa_process: "
  49394. + "extr or extr->ips is NULL, fatal\n");
  49395. + SENDERR(EINVAL);
  49396. + }
  49397. +
  49398. + switch(pfkey_ext->sadb_ext_type) {
  49399. + case SADB_EXT_SA:
  49400. + ipsp = extr->ips;
  49401. + break;
  49402. + case SADB_X_EXT_SA2:
  49403. + if(extr->ips2 == NULL) {
  49404. + extr->ips2 = ipsec_sa_alloc(&error); /* pass error var by pointer */
  49405. + }
  49406. + if(extr->ips2 == NULL) {
  49407. + SENDERR(-error);
  49408. + }
  49409. + ipsp = extr->ips2;
  49410. + break;
  49411. + default:
  49412. + KLIPS_PRINT(debug_pfkey,
  49413. + "klips_debug:pfkey_sa_process: "
  49414. + "invalid exttype=%d.\n",
  49415. + pfkey_ext->sadb_ext_type);
  49416. + SENDERR(EINVAL);
  49417. + }
  49418. +
  49419. + ipsp->ips_said.spi = pfkey_sa->sadb_sa_spi;
  49420. + ipsp->ips_replaywin = pfkey_sa->sadb_sa_replay;
  49421. + ipsp->ips_state = pfkey_sa->sadb_sa_state;
  49422. + ipsp->ips_flags = pfkey_sa->sadb_sa_flags;
  49423. + ipsp->ips_replaywin_lastseq = ipsp->ips_replaywin_bitmap = 0;
  49424. + ipsp->ips_ref_rel = pfkey_sa->sadb_x_sa_ref;
  49425. +
  49426. + switch(ipsp->ips_said.proto) {
  49427. + case IPPROTO_AH:
  49428. + ipsp->ips_authalg = pfkey_sa->sadb_sa_auth;
  49429. + ipsp->ips_encalg = SADB_EALG_NONE;
  49430. + break;
  49431. + case IPPROTO_ESP:
  49432. + ipsp->ips_authalg = pfkey_sa->sadb_sa_auth;
  49433. + ipsp->ips_encalg = pfkey_sa->sadb_sa_encrypt;
  49434. + ipsec_alg_sa_init(ipsp);
  49435. + break;
  49436. + case IPPROTO_IPIP:
  49437. + ipsp->ips_authalg = AH_NONE;
  49438. + ipsp->ips_encalg = ESP_NONE;
  49439. + break;
  49440. +#ifdef CONFIG_KLIPS_IPCOMP
  49441. + case IPPROTO_COMP:
  49442. + ipsp->ips_authalg = AH_NONE;
  49443. + ipsp->ips_encalg = pfkey_sa->sadb_sa_encrypt;
  49444. + break;
  49445. +#endif /* CONFIG_KLIPS_IPCOMP */
  49446. + case IPPROTO_INT:
  49447. + ipsp->ips_authalg = AH_NONE;
  49448. + ipsp->ips_encalg = ESP_NONE;
  49449. + break;
  49450. + case 0:
  49451. + break;
  49452. + default:
  49453. + KLIPS_PRINT(debug_pfkey,
  49454. + "klips_debug:pfkey_sa_process: "
  49455. + "unknown proto=%d.\n",
  49456. + ipsp->ips_said.proto);
  49457. + SENDERR(EINVAL);
  49458. + }
  49459. +
  49460. +errlab:
  49461. + return error;
  49462. +}
  49463. +
  49464. +int
  49465. +pfkey_lifetime_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49466. +{
  49467. + int error = 0;
  49468. + struct sadb_lifetime *pfkey_lifetime = (struct sadb_lifetime *)pfkey_ext;
  49469. +
  49470. + KLIPS_PRINT(debug_pfkey,
  49471. + "klips_debug:pfkey_lifetime_process: .\n");
  49472. +
  49473. + if(!extr || !extr->ips) {
  49474. + KLIPS_PRINT(debug_pfkey,
  49475. + "klips_debug:pfkey_lifetime_process: "
  49476. + "extr or extr->ips is NULL, fatal\n");
  49477. + SENDERR(EINVAL);
  49478. + }
  49479. +
  49480. + switch(pfkey_lifetime->sadb_lifetime_exttype) {
  49481. + case SADB_EXT_LIFETIME_CURRENT:
  49482. + KLIPS_PRINT(debug_pfkey,
  49483. + "klips_debug:pfkey_lifetime_process: "
  49484. + "lifetime_current not supported yet.\n");
  49485. + SENDERR(EINVAL);
  49486. + break;
  49487. + case SADB_EXT_LIFETIME_HARD:
  49488. + ipsec_lifetime_update_hard(&extr->ips->ips_life.ipl_allocations,
  49489. + pfkey_lifetime->sadb_lifetime_allocations);
  49490. +
  49491. + ipsec_lifetime_update_hard(&extr->ips->ips_life.ipl_bytes,
  49492. + pfkey_lifetime->sadb_lifetime_bytes);
  49493. +
  49494. + ipsec_lifetime_update_hard(&extr->ips->ips_life.ipl_addtime,
  49495. + pfkey_lifetime->sadb_lifetime_addtime);
  49496. +
  49497. + ipsec_lifetime_update_hard(&extr->ips->ips_life.ipl_usetime,
  49498. + pfkey_lifetime->sadb_lifetime_usetime);
  49499. +
  49500. + break;
  49501. +
  49502. + case SADB_EXT_LIFETIME_SOFT:
  49503. + ipsec_lifetime_update_soft(&extr->ips->ips_life.ipl_allocations,
  49504. + pfkey_lifetime->sadb_lifetime_allocations);
  49505. +
  49506. + ipsec_lifetime_update_soft(&extr->ips->ips_life.ipl_bytes,
  49507. + pfkey_lifetime->sadb_lifetime_bytes);
  49508. +
  49509. + ipsec_lifetime_update_soft(&extr->ips->ips_life.ipl_addtime,
  49510. + pfkey_lifetime->sadb_lifetime_addtime);
  49511. +
  49512. + ipsec_lifetime_update_soft(&extr->ips->ips_life.ipl_usetime,
  49513. + pfkey_lifetime->sadb_lifetime_usetime);
  49514. +
  49515. + break;
  49516. + default:
  49517. + KLIPS_PRINT(debug_pfkey,
  49518. + "klips_debug:pfkey_lifetime_process: "
  49519. + "invalid exttype=%d.\n",
  49520. + pfkey_ext->sadb_ext_type);
  49521. + SENDERR(EINVAL);
  49522. + }
  49523. +
  49524. +errlab:
  49525. + return error;
  49526. +}
  49527. +
  49528. +int
  49529. +pfkey_address_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49530. +{
  49531. + int error = 0;
  49532. + int saddr_len = 0;
  49533. + char ipaddr_txt[ADDRTOA_BUF];
  49534. + unsigned char **sap;
  49535. + unsigned short * portp = 0;
  49536. + struct sadb_address *pfkey_address = (struct sadb_address *)pfkey_ext;
  49537. + struct sockaddr* s = (struct sockaddr*)((char*)pfkey_address + sizeof(*pfkey_address));
  49538. + struct ipsec_sa* ipsp;
  49539. +
  49540. + KLIPS_PRINT(debug_pfkey,
  49541. + "klips_debug:pfkey_address_process:\n");
  49542. +
  49543. + if(!extr || !extr->ips) {
  49544. + KLIPS_PRINT(debug_pfkey,
  49545. + "klips_debug:pfkey_address_process: "
  49546. + "extr or extr->ips is NULL, fatal\n");
  49547. + SENDERR(EINVAL);
  49548. + }
  49549. +
  49550. + switch(s->sa_family) {
  49551. + case AF_INET:
  49552. + saddr_len = sizeof(struct sockaddr_in);
  49553. + addrtoa(((struct sockaddr_in*)s)->sin_addr, 0, ipaddr_txt, sizeof(ipaddr_txt));
  49554. + KLIPS_PRINT(debug_pfkey,
  49555. + "klips_debug:pfkey_address_process: "
  49556. + "found address family=%d, AF_INET, %s.\n",
  49557. + s->sa_family,
  49558. + ipaddr_txt);
  49559. + break;
  49560. +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  49561. + case AF_INET6:
  49562. + saddr_len = sizeof(struct sockaddr_in6);
  49563. + break;
  49564. +#endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
  49565. + default:
  49566. + KLIPS_PRINT(debug_pfkey,
  49567. + "klips_debug:pfkey_address_process: "
  49568. + "s->sa_family=%d not supported.\n",
  49569. + s->sa_family);
  49570. + SENDERR(EPFNOSUPPORT);
  49571. + }
  49572. +
  49573. + switch(pfkey_address->sadb_address_exttype) {
  49574. + case SADB_EXT_ADDRESS_SRC:
  49575. + KLIPS_PRINT(debug_pfkey,
  49576. + "klips_debug:pfkey_address_process: "
  49577. + "found src address.\n");
  49578. + sap = (unsigned char **)&(extr->ips->ips_addr_s);
  49579. + extr->ips->ips_addr_s_size = saddr_len;
  49580. + break;
  49581. + case SADB_EXT_ADDRESS_DST:
  49582. + KLIPS_PRINT(debug_pfkey,
  49583. + "klips_debug:pfkey_address_process: "
  49584. + "found dst address.\n");
  49585. + sap = (unsigned char **)&(extr->ips->ips_addr_d);
  49586. + extr->ips->ips_addr_d_size = saddr_len;
  49587. + break;
  49588. + case SADB_EXT_ADDRESS_PROXY:
  49589. + KLIPS_PRINT(debug_pfkey,
  49590. + "klips_debug:pfkey_address_process: "
  49591. + "found proxy address.\n");
  49592. + sap = (unsigned char **)&(extr->ips->ips_addr_p);
  49593. + extr->ips->ips_addr_p_size = saddr_len;
  49594. + break;
  49595. + case SADB_X_EXT_ADDRESS_DST2:
  49596. + KLIPS_PRINT(debug_pfkey,
  49597. + "klips_debug:pfkey_address_process: "
  49598. + "found 2nd dst address.\n");
  49599. + if(extr->ips2 == NULL) {
  49600. + extr->ips2 = ipsec_sa_alloc(&error); /* pass error var by pointer */
  49601. + }
  49602. + if(extr->ips2 == NULL) {
  49603. + SENDERR(-error);
  49604. + }
  49605. + sap = (unsigned char **)&(extr->ips2->ips_addr_d);
  49606. + extr->ips2->ips_addr_d_size = saddr_len;
  49607. + break;
  49608. + case SADB_X_EXT_ADDRESS_SRC_FLOW:
  49609. + KLIPS_PRINT(debug_pfkey,
  49610. + "klips_debug:pfkey_address_process: "
  49611. + "found src flow address.\n");
  49612. + if(pfkey_alloc_eroute(&(extr->eroute)) == ENOMEM) {
  49613. + SENDERR(ENOMEM);
  49614. + }
  49615. + sap = (unsigned char **)&(extr->eroute->er_eaddr.sen_ip_src);
  49616. + portp = &(extr->eroute->er_eaddr.sen_sport);
  49617. + break;
  49618. + case SADB_X_EXT_ADDRESS_DST_FLOW:
  49619. + KLIPS_PRINT(debug_pfkey,
  49620. + "klips_debug:pfkey_address_process: "
  49621. + "found dst flow address.\n");
  49622. + if(pfkey_alloc_eroute(&(extr->eroute)) == ENOMEM) {
  49623. + SENDERR(ENOMEM);
  49624. + }
  49625. + sap = (unsigned char **)&(extr->eroute->er_eaddr.sen_ip_dst);
  49626. + portp = &(extr->eroute->er_eaddr.sen_dport);
  49627. + break;
  49628. + case SADB_X_EXT_ADDRESS_SRC_MASK:
  49629. + KLIPS_PRINT(debug_pfkey,
  49630. + "klips_debug:pfkey_address_process: "
  49631. + "found src mask address.\n");
  49632. + if(pfkey_alloc_eroute(&(extr->eroute)) == ENOMEM) {
  49633. + SENDERR(ENOMEM);
  49634. + }
  49635. + sap = (unsigned char **)&(extr->eroute->er_emask.sen_ip_src);
  49636. + portp = &(extr->eroute->er_emask.sen_sport);
  49637. + break;
  49638. + case SADB_X_EXT_ADDRESS_DST_MASK:
  49639. + KLIPS_PRINT(debug_pfkey,
  49640. + "klips_debug:pfkey_address_process: "
  49641. + "found dst mask address.\n");
  49642. + if(pfkey_alloc_eroute(&(extr->eroute)) == ENOMEM) {
  49643. + SENDERR(ENOMEM);
  49644. + }
  49645. + sap = (unsigned char **)&(extr->eroute->er_emask.sen_ip_dst);
  49646. + portp = &(extr->eroute->er_emask.sen_dport);
  49647. + break;
  49648. +#ifdef NAT_TRAVERSAL
  49649. + case SADB_X_EXT_NAT_T_OA:
  49650. + KLIPS_PRINT(debug_pfkey,
  49651. + "klips_debug:pfkey_address_process: "
  49652. + "found NAT-OA address.\n");
  49653. + sap = (unsigned char **)&(extr->ips->ips_natt_oa);
  49654. + extr->ips->ips_natt_oa_size = saddr_len;
  49655. + break;
  49656. +#endif
  49657. + default:
  49658. + KLIPS_PRINT(debug_pfkey,
  49659. + "klips_debug:pfkey_address_process: "
  49660. + "unrecognised ext_type=%d.\n",
  49661. + pfkey_address->sadb_address_exttype);
  49662. + SENDERR(EINVAL);
  49663. + }
  49664. +
  49665. + switch(pfkey_address->sadb_address_exttype) {
  49666. + case SADB_EXT_ADDRESS_SRC:
  49667. + case SADB_EXT_ADDRESS_DST:
  49668. + case SADB_EXT_ADDRESS_PROXY:
  49669. + case SADB_X_EXT_ADDRESS_DST2:
  49670. +#ifdef NAT_TRAVERSAL
  49671. + case SADB_X_EXT_NAT_T_OA:
  49672. +#endif
  49673. + KLIPS_PRINT(debug_pfkey,
  49674. + "klips_debug:pfkey_address_process: "
  49675. + "allocating %d bytes for saddr.\n",
  49676. + saddr_len);
  49677. + if(!(*sap = kmalloc(saddr_len, GFP_KERNEL))) {
  49678. + SENDERR(ENOMEM);
  49679. + }
  49680. + memcpy(*sap, s, saddr_len);
  49681. + break;
  49682. + default:
  49683. + if(s->sa_family != AF_INET) {
  49684. + KLIPS_PRINT(debug_pfkey,
  49685. + "klips_debug:pfkey_address_process: "
  49686. + "s->sa_family=%d not supported.\n",
  49687. + s->sa_family);
  49688. + SENDERR(EPFNOSUPPORT);
  49689. + }
  49690. + {
  49691. + unsigned long *ulsap = (unsigned long *)sap;
  49692. + *ulsap = ((struct sockaddr_in*)s)->sin_addr.s_addr;
  49693. + }
  49694. +
  49695. + if (portp != 0)
  49696. + *portp = ((struct sockaddr_in*)s)->sin_port;
  49697. +#ifdef CONFIG_KLIPS_DEBUG
  49698. + if(extr->eroute) {
  49699. + char buf1[64], buf2[64];
  49700. + if (debug_pfkey) {
  49701. + subnettoa(extr->eroute->er_eaddr.sen_ip_src,
  49702. + extr->eroute->er_emask.sen_ip_src, 0, buf1, sizeof(buf1));
  49703. + subnettoa(extr->eroute->er_eaddr.sen_ip_dst,
  49704. + extr->eroute->er_emask.sen_ip_dst, 0, buf2, sizeof(buf2));
  49705. + KLIPS_PRINT(debug_pfkey,
  49706. + "klips_debug:pfkey_address_parse: "
  49707. + "extr->eroute set to %s:%d->%s:%d\n",
  49708. + buf1,
  49709. + ntohs(extr->eroute->er_eaddr.sen_sport),
  49710. + buf2,
  49711. + ntohs(extr->eroute->er_eaddr.sen_dport));
  49712. + }
  49713. + }
  49714. +#endif /* CONFIG_KLIPS_DEBUG */
  49715. + }
  49716. +
  49717. + ipsp = extr->ips;
  49718. + switch(pfkey_address->sadb_address_exttype) {
  49719. + case SADB_X_EXT_ADDRESS_DST2:
  49720. + ipsp = extr->ips2;
  49721. + case SADB_EXT_ADDRESS_DST:
  49722. + if(s->sa_family == AF_INET) {
  49723. + ipsp->ips_said.dst.u.v4.sin_addr.s_addr = ((struct sockaddr_in*)(ipsp->ips_addr_d))->sin_addr.s_addr;
  49724. + ipsp->ips_said.dst.u.v4.sin_family = AF_INET;
  49725. + addrtoa(((struct sockaddr_in*)(ipsp->ips_addr_d))->sin_addr,
  49726. + 0,
  49727. + ipaddr_txt,
  49728. + sizeof(ipaddr_txt));
  49729. + KLIPS_PRINT(debug_pfkey,
  49730. + "klips_debug:pfkey_address_process: "
  49731. + "ips_said.dst set to %s.\n",
  49732. + ipaddr_txt);
  49733. + } else {
  49734. + KLIPS_PRINT(debug_pfkey,
  49735. + "klips_debug:pfkey_address_process: "
  49736. + "uh, ips_said.dst doesn't do address family=%d yet, said will be invalid.\n",
  49737. + s->sa_family);
  49738. + }
  49739. + default:
  49740. + break;
  49741. + }
  49742. +
  49743. + /* XXX check if port!=0 */
  49744. +
  49745. + KLIPS_PRINT(debug_pfkey,
  49746. + "klips_debug:pfkey_address_process: successful.\n");
  49747. + errlab:
  49748. + return error;
  49749. +}
  49750. +
  49751. +int
  49752. +pfkey_key_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49753. +{
  49754. + int error = 0;
  49755. + struct sadb_key *pfkey_key = (struct sadb_key *)pfkey_ext;
  49756. +
  49757. + KLIPS_PRINT(debug_pfkey,
  49758. + "klips_debug:pfkey_key_process: .\n");
  49759. +
  49760. + if(!extr || !extr->ips) {
  49761. + KLIPS_PRINT(debug_pfkey,
  49762. + "klips_debug:pfkey_key_process: "
  49763. + "extr or extr->ips is NULL, fatal\n");
  49764. + SENDERR(EINVAL);
  49765. + }
  49766. +
  49767. + switch(pfkey_key->sadb_key_exttype) {
  49768. + case SADB_EXT_KEY_AUTH:
  49769. + KLIPS_PRINT(debug_pfkey,
  49770. + "klips_debug:pfkey_key_process: "
  49771. + "allocating %d bytes for authkey.\n",
  49772. + DIVUP(pfkey_key->sadb_key_bits, 8));
  49773. + if(!(extr->ips->ips_key_a = kmalloc(DIVUP(pfkey_key->sadb_key_bits, 8), GFP_KERNEL))) {
  49774. + KLIPS_PRINT(debug_pfkey,
  49775. + "klips_debug:pfkey_key_process: "
  49776. + "memory allocation error.\n");
  49777. + SENDERR(ENOMEM);
  49778. + }
  49779. + extr->ips->ips_key_bits_a = pfkey_key->sadb_key_bits;
  49780. + extr->ips->ips_key_a_size = DIVUP(pfkey_key->sadb_key_bits, 8);
  49781. + memcpy(extr->ips->ips_key_a,
  49782. + (char*)pfkey_key + sizeof(struct sadb_key),
  49783. + extr->ips->ips_key_a_size);
  49784. + break;
  49785. + case SADB_EXT_KEY_ENCRYPT: /* Key(s) */
  49786. + KLIPS_PRINT(debug_pfkey,
  49787. + "klips_debug:pfkey_key_process: "
  49788. + "allocating %d bytes for enckey.\n",
  49789. + DIVUP(pfkey_key->sadb_key_bits, 8));
  49790. + if(!(extr->ips->ips_key_e = kmalloc(DIVUP(pfkey_key->sadb_key_bits, 8), GFP_KERNEL))) {
  49791. + KLIPS_PRINT(debug_pfkey,
  49792. + "klips_debug:pfkey_key_process: "
  49793. + "memory allocation error.\n");
  49794. + SENDERR(ENOMEM);
  49795. + }
  49796. + extr->ips->ips_key_bits_e = pfkey_key->sadb_key_bits;
  49797. + extr->ips->ips_key_e_size = DIVUP(pfkey_key->sadb_key_bits, 8);
  49798. + memcpy(extr->ips->ips_key_e,
  49799. + (char*)pfkey_key + sizeof(struct sadb_key),
  49800. + extr->ips->ips_key_e_size);
  49801. + break;
  49802. + default:
  49803. + SENDERR(EINVAL);
  49804. + }
  49805. +
  49806. + KLIPS_PRINT(debug_pfkey,
  49807. + "klips_debug:pfkey_key_process: "
  49808. + "success.\n");
  49809. +errlab:
  49810. + return error;
  49811. +}
  49812. +
  49813. +int
  49814. +pfkey_ident_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49815. +{
  49816. + int error = 0;
  49817. + struct sadb_ident *pfkey_ident = (struct sadb_ident *)pfkey_ext;
  49818. + int data_len;
  49819. +
  49820. + KLIPS_PRINT(debug_pfkey,
  49821. + "klips_debug:pfkey_ident_process: .\n");
  49822. +
  49823. + if(!extr || !extr->ips) {
  49824. + KLIPS_PRINT(debug_pfkey,
  49825. + "klips_debug:pfkey_ident_process: "
  49826. + "extr or extr->ips is NULL, fatal\n");
  49827. + SENDERR(EINVAL);
  49828. + }
  49829. +
  49830. + switch(pfkey_ident->sadb_ident_exttype) {
  49831. + case SADB_EXT_IDENTITY_SRC:
  49832. + data_len = pfkey_ident->sadb_ident_len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  49833. +
  49834. + extr->ips->ips_ident_s.type = pfkey_ident->sadb_ident_type;
  49835. + extr->ips->ips_ident_s.id = pfkey_ident->sadb_ident_id;
  49836. + extr->ips->ips_ident_s.len = pfkey_ident->sadb_ident_len;
  49837. + if(data_len) {
  49838. + KLIPS_PRINT(debug_pfkey,
  49839. + "klips_debug:pfkey_ident_process: "
  49840. + "allocating %d bytes for ident_s.\n",
  49841. + data_len);
  49842. + if(!(extr->ips->ips_ident_s.data
  49843. + = kmalloc(data_len, GFP_KERNEL))) {
  49844. + SENDERR(ENOMEM);
  49845. + }
  49846. + memcpy(extr->ips->ips_ident_s.data,
  49847. + (char*)pfkey_ident + sizeof(struct sadb_ident),
  49848. + data_len);
  49849. + } else {
  49850. + extr->ips->ips_ident_s.data = NULL;
  49851. + }
  49852. + break;
  49853. + case SADB_EXT_IDENTITY_DST: /* Identity(ies) */
  49854. + data_len = pfkey_ident->sadb_ident_len * IPSEC_PFKEYv2_ALIGN - sizeof(struct sadb_ident);
  49855. +
  49856. + extr->ips->ips_ident_d.type = pfkey_ident->sadb_ident_type;
  49857. + extr->ips->ips_ident_d.id = pfkey_ident->sadb_ident_id;
  49858. + extr->ips->ips_ident_d.len = pfkey_ident->sadb_ident_len;
  49859. + if(data_len) {
  49860. + KLIPS_PRINT(debug_pfkey,
  49861. + "klips_debug:pfkey_ident_process: "
  49862. + "allocating %d bytes for ident_d.\n",
  49863. + data_len);
  49864. + if(!(extr->ips->ips_ident_d.data
  49865. + = kmalloc(data_len, GFP_KERNEL))) {
  49866. + SENDERR(ENOMEM);
  49867. + }
  49868. + memcpy(extr->ips->ips_ident_d.data,
  49869. + (char*)pfkey_ident + sizeof(struct sadb_ident),
  49870. + data_len);
  49871. + } else {
  49872. + extr->ips->ips_ident_d.data = NULL;
  49873. + }
  49874. + break;
  49875. + default:
  49876. + SENDERR(EINVAL);
  49877. + }
  49878. +errlab:
  49879. + return error;
  49880. +}
  49881. +
  49882. +int
  49883. +pfkey_sens_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49884. +{
  49885. + int error = 0;
  49886. +
  49887. + KLIPS_PRINT(debug_pfkey,
  49888. + "klips_debug:pfkey_sens_process: "
  49889. + "Sorry, I can't process exttype=%d yet.\n",
  49890. + pfkey_ext->sadb_ext_type);
  49891. + SENDERR(EINVAL); /* don't process these yet */
  49892. + errlab:
  49893. + return error;
  49894. +}
  49895. +
  49896. +int
  49897. +pfkey_prop_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49898. +{
  49899. + int error = 0;
  49900. +
  49901. + KLIPS_PRINT(debug_pfkey,
  49902. + "klips_debug:pfkey_prop_process: "
  49903. + "Sorry, I can't process exttype=%d yet.\n",
  49904. + pfkey_ext->sadb_ext_type);
  49905. + SENDERR(EINVAL); /* don't process these yet */
  49906. +
  49907. + errlab:
  49908. + return error;
  49909. +}
  49910. +
  49911. +int
  49912. +pfkey_supported_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49913. +{
  49914. + int error = 0;
  49915. +
  49916. + KLIPS_PRINT(debug_pfkey,
  49917. + "klips_debug:pfkey_supported_process: "
  49918. + "Sorry, I can't process exttype=%d yet.\n",
  49919. + pfkey_ext->sadb_ext_type);
  49920. + SENDERR(EINVAL); /* don't process these yet */
  49921. +
  49922. +errlab:
  49923. + return error;
  49924. +}
  49925. +
  49926. +int
  49927. +pfkey_spirange_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49928. +{
  49929. + int error = 0;
  49930. +
  49931. + KLIPS_PRINT(debug_pfkey,
  49932. + "klips_debug:pfkey_spirange_process: .\n");
  49933. +/* errlab: */
  49934. + return error;
  49935. +}
  49936. +
  49937. +int
  49938. +pfkey_x_kmprivate_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49939. +{
  49940. + int error = 0;
  49941. +
  49942. + KLIPS_PRINT(debug_pfkey,
  49943. + "klips_debug:pfkey_x_kmprivate_process: "
  49944. + "Sorry, I can't process exttype=%d yet.\n",
  49945. + pfkey_ext->sadb_ext_type);
  49946. + SENDERR(EINVAL); /* don't process these yet */
  49947. +
  49948. +errlab:
  49949. + return error;
  49950. +}
  49951. +
  49952. +int
  49953. +pfkey_x_satype_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49954. +{
  49955. + int error = 0;
  49956. + struct sadb_x_satype *pfkey_x_satype = (struct sadb_x_satype *)pfkey_ext;
  49957. +
  49958. + KLIPS_PRINT(debug_pfkey,
  49959. + "klips_debug:pfkey_x_satype_process: .\n");
  49960. +
  49961. + if(!extr || !extr->ips) {
  49962. + KLIPS_PRINT(debug_pfkey,
  49963. + "klips_debug:pfkey_x_satype_process: "
  49964. + "extr or extr->ips is NULL, fatal\n");
  49965. + SENDERR(EINVAL);
  49966. + }
  49967. +
  49968. + if(extr->ips2 == NULL) {
  49969. + extr->ips2 = ipsec_sa_alloc(&error); /* pass error var by pointer */
  49970. + }
  49971. + if(extr->ips2 == NULL) {
  49972. + SENDERR(-error);
  49973. + }
  49974. + if(!(extr->ips2->ips_said.proto = satype2proto(pfkey_x_satype->sadb_x_satype_satype))) {
  49975. + KLIPS_PRINT(debug_pfkey,
  49976. + "klips_debug:pfkey_x_satype_process: "
  49977. + "proto lookup from satype=%d failed.\n",
  49978. + pfkey_x_satype->sadb_x_satype_satype);
  49979. + SENDERR(EINVAL);
  49980. + }
  49981. + KLIPS_PRINT(debug_pfkey,
  49982. + "klips_debug:pfkey_x_satype_process: "
  49983. + "protocol==%d decoded from satype==%d(%s).\n",
  49984. + extr->ips2->ips_said.proto,
  49985. + pfkey_x_satype->sadb_x_satype_satype,
  49986. + satype2name(pfkey_x_satype->sadb_x_satype_satype));
  49987. +
  49988. +errlab:
  49989. + return error;
  49990. +}
  49991. +
  49992. +
  49993. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  49994. +int
  49995. +pfkey_x_nat_t_type_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  49996. +{
  49997. + int error = 0;
  49998. + struct sadb_x_nat_t_type *pfkey_x_nat_t_type = (struct sadb_x_nat_t_type *)pfkey_ext;
  49999. +
  50000. + if(!pfkey_x_nat_t_type) {
  50001. + printk("klips_debug:pfkey_x_nat_t_type_process: "
  50002. + "null pointer passed in\n");
  50003. + SENDERR(EINVAL);
  50004. + }
  50005. +
  50006. + KLIPS_PRINT(debug_pfkey,
  50007. + "klips_debug:pfkey_x_nat_t_type_process: %d.\n",
  50008. + pfkey_x_nat_t_type->sadb_x_nat_t_type_type);
  50009. +
  50010. + if(!extr || !extr->ips) {
  50011. + KLIPS_PRINT(debug_pfkey,
  50012. + "klips_debug:pfkey_nat_t_type_process: "
  50013. + "extr or extr->ips is NULL, fatal\n");
  50014. + SENDERR(EINVAL);
  50015. + }
  50016. +
  50017. + switch(pfkey_x_nat_t_type->sadb_x_nat_t_type_type) {
  50018. + case ESPINUDP_WITH_NON_IKE: /* with Non-IKE (older version) */
  50019. + case ESPINUDP_WITH_NON_ESP: /* with Non-ESP */
  50020. +
  50021. + extr->ips->ips_natt_type = pfkey_x_nat_t_type->sadb_x_nat_t_type_type;
  50022. + break;
  50023. + default:
  50024. + KLIPS_PRINT(debug_pfkey,
  50025. + "klips_debug:pfkey_x_nat_t_type_process: "
  50026. + "unknown type %d.\n",
  50027. + pfkey_x_nat_t_type->sadb_x_nat_t_type_type);
  50028. + SENDERR(EINVAL);
  50029. + break;
  50030. + }
  50031. +
  50032. +errlab:
  50033. + return error;
  50034. +}
  50035. +
  50036. +int
  50037. +pfkey_x_nat_t_port_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  50038. +{
  50039. + int error = 0;
  50040. + struct sadb_x_nat_t_port *pfkey_x_nat_t_port = (struct sadb_x_nat_t_port *)pfkey_ext;
  50041. +
  50042. + if(!pfkey_x_nat_t_port) {
  50043. + printk("klips_debug:pfkey_x_nat_t_port_process: "
  50044. + "null pointer passed in\n");
  50045. + SENDERR(EINVAL);
  50046. + }
  50047. +
  50048. + KLIPS_PRINT(debug_pfkey,
  50049. + "klips_debug:pfkey_x_nat_t_port_process: %d/%d.\n",
  50050. + pfkey_x_nat_t_port->sadb_x_nat_t_port_exttype,
  50051. + pfkey_x_nat_t_port->sadb_x_nat_t_port_port);
  50052. +
  50053. + if(!extr || !extr->ips) {
  50054. + KLIPS_PRINT(debug_pfkey,
  50055. + "klips_debug:pfkey_nat_t_type_process: "
  50056. + "extr or extr->ips is NULL, fatal\n");
  50057. + SENDERR(EINVAL);
  50058. + }
  50059. +
  50060. + switch(pfkey_x_nat_t_port->sadb_x_nat_t_port_exttype) {
  50061. + case SADB_X_EXT_NAT_T_SPORT:
  50062. + extr->ips->ips_natt_sport = pfkey_x_nat_t_port->sadb_x_nat_t_port_port;
  50063. + break;
  50064. + case SADB_X_EXT_NAT_T_DPORT:
  50065. + extr->ips->ips_natt_dport = pfkey_x_nat_t_port->sadb_x_nat_t_port_port;
  50066. + break;
  50067. + default:
  50068. + KLIPS_PRINT(debug_pfkey,
  50069. + "klips_debug:pfkey_x_nat_t_port_process: "
  50070. + "unknown exttype %d.\n",
  50071. + pfkey_x_nat_t_port->sadb_x_nat_t_port_exttype);
  50072. + SENDERR(EINVAL);
  50073. + break;
  50074. + }
  50075. +
  50076. +errlab:
  50077. + return error;
  50078. +}
  50079. +#endif
  50080. +
  50081. +int
  50082. +pfkey_x_debug_process(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr)
  50083. +{
  50084. + int error = 0;
  50085. + struct sadb_x_debug *pfkey_x_debug = (struct sadb_x_debug *)pfkey_ext;
  50086. +
  50087. + if(!pfkey_x_debug) {
  50088. + printk("klips_debug:pfkey_x_debug_process: "
  50089. + "null pointer passed in\n");
  50090. + SENDERR(EINVAL);
  50091. + }
  50092. +
  50093. + KLIPS_PRINT(debug_pfkey,
  50094. + "klips_debug:pfkey_x_debug_process: .\n");
  50095. +
  50096. +#ifdef CONFIG_KLIPS_DEBUG
  50097. + if(pfkey_x_debug->sadb_x_debug_netlink >>
  50098. + (sizeof(pfkey_x_debug->sadb_x_debug_netlink) * 8 - 1)) {
  50099. + pfkey_x_debug->sadb_x_debug_netlink &=
  50100. + ~(1 << (sizeof(pfkey_x_debug->sadb_x_debug_netlink) * 8 -1));
  50101. + debug_tunnel |= pfkey_x_debug->sadb_x_debug_tunnel;
  50102. + debug_netlink |= pfkey_x_debug->sadb_x_debug_netlink;
  50103. + debug_xform |= pfkey_x_debug->sadb_x_debug_xform;
  50104. + debug_eroute |= pfkey_x_debug->sadb_x_debug_eroute;
  50105. + debug_spi |= pfkey_x_debug->sadb_x_debug_spi;
  50106. + debug_radij |= pfkey_x_debug->sadb_x_debug_radij;
  50107. + debug_esp |= pfkey_x_debug->sadb_x_debug_esp;
  50108. + debug_ah |= pfkey_x_debug->sadb_x_debug_ah;
  50109. + debug_rcv |= pfkey_x_debug->sadb_x_debug_rcv;
  50110. + debug_pfkey |= pfkey_x_debug->sadb_x_debug_pfkey;
  50111. +#ifdef CONFIG_KLIPS_IPCOMP
  50112. + sysctl_ipsec_debug_ipcomp |= pfkey_x_debug->sadb_x_debug_ipcomp;
  50113. +#endif /* CONFIG_KLIPS_IPCOMP */
  50114. + sysctl_ipsec_debug_verbose |= pfkey_x_debug->sadb_x_debug_verbose;
  50115. + KLIPS_PRINT(debug_pfkey,
  50116. + "klips_debug:pfkey_x_debug_process: "
  50117. + "set\n");
  50118. + } else {
  50119. + KLIPS_PRINT(debug_pfkey,
  50120. + "klips_debug:pfkey_x_debug_process: "
  50121. + "unset\n");
  50122. + debug_tunnel &= pfkey_x_debug->sadb_x_debug_tunnel;
  50123. + debug_netlink &= pfkey_x_debug->sadb_x_debug_netlink;
  50124. + debug_xform &= pfkey_x_debug->sadb_x_debug_xform;
  50125. + debug_eroute &= pfkey_x_debug->sadb_x_debug_eroute;
  50126. + debug_spi &= pfkey_x_debug->sadb_x_debug_spi;
  50127. + debug_radij &= pfkey_x_debug->sadb_x_debug_radij;
  50128. + debug_esp &= pfkey_x_debug->sadb_x_debug_esp;
  50129. + debug_ah &= pfkey_x_debug->sadb_x_debug_ah;
  50130. + debug_rcv &= pfkey_x_debug->sadb_x_debug_rcv;
  50131. + debug_pfkey &= pfkey_x_debug->sadb_x_debug_pfkey;
  50132. +#ifdef CONFIG_KLIPS_IPCOMP
  50133. + sysctl_ipsec_debug_ipcomp &= pfkey_x_debug->sadb_x_debug_ipcomp;
  50134. +#endif /* CONFIG_KLIPS_IPCOMP */
  50135. + sysctl_ipsec_debug_verbose &= pfkey_x_debug->sadb_x_debug_verbose;
  50136. + }
  50137. +#else /* CONFIG_KLIPS_DEBUG */
  50138. + printk("klips_debug:pfkey_x_debug_process: "
  50139. + "debugging not enabled\n");
  50140. + SENDERR(EINVAL);
  50141. +#endif /* CONFIG_KLIPS_DEBUG */
  50142. +
  50143. +errlab:
  50144. + return error;
  50145. +}
  50146. +
  50147. +/*
  50148. + * $Log: pfkey_v2_ext_process.c,v $
  50149. + * Revision 1.20.2.2 2006/10/06 21:39:26 paul
  50150. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  50151. + * set. This is defined through autoconf.h which is included through the
  50152. + * linux kernel build macros.
  50153. + *
  50154. + * Revision 1.20.2.1 2006/04/20 16:33:07 mcr
  50155. + * remove all of CONFIG_KLIPS_ALG --- one can no longer build without it.
  50156. + * Fix in-kernel module compilation. Sub-makefiles do not work.
  50157. + *
  50158. + * Revision 1.20 2005/04/29 05:10:22 mcr
  50159. + * removed from extraenous includes to make unit testing easier.
  50160. + *
  50161. + * Revision 1.19 2004/12/04 07:14:18 mcr
  50162. + * resolution to gcc3-ism was wrong. fixed to assign correct
  50163. + * variable.
  50164. + *
  50165. + * Revision 1.18 2004/12/03 21:25:57 mcr
  50166. + * compile time fixes for running on 2.6.
  50167. + * still experimental.
  50168. + *
  50169. + * Revision 1.17 2004/08/21 00:45:04 mcr
  50170. + * CONFIG_KLIPS_NAT was wrong, also need to include udp.h.
  50171. + *
  50172. + * Revision 1.16 2004/07/10 19:11:18 mcr
  50173. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  50174. + *
  50175. + * Revision 1.15 2004/04/06 02:49:26 mcr
  50176. + * pullup of algo code from alg-branch.
  50177. + *
  50178. + * Revision 1.14 2004/02/03 03:13:59 mcr
  50179. + * no longer #ifdef out NON_ESP mode. That was a mistake.
  50180. + *
  50181. + * Revision 1.13 2003/12/15 18:13:12 mcr
  50182. + * when compiling with NAT traversal, don't assume that the
  50183. + * kernel has been patched, unless CONFIG_IPSEC_NAT_NON_ESP
  50184. + * is set.
  50185. + *
  50186. + * Revision 1.12.2.1 2003/12/22 15:25:52 jjo
  50187. + * Merged algo-0.8.1-rc11-test1 into alg-branch
  50188. + *
  50189. + * Revision 1.12 2003/12/10 01:14:27 mcr
  50190. + * NAT-traversal patches to KLIPS.
  50191. + *
  50192. + * Revision 1.11 2003/10/31 02:27:55 mcr
  50193. + * pulled up port-selector patches and sa_id elimination.
  50194. + *
  50195. + * Revision 1.10.4.2 2003/10/29 01:30:41 mcr
  50196. + * elimited "struct sa_id".
  50197. + *
  50198. + * Revision 1.10.4.1 2003/09/21 13:59:56 mcr
  50199. + * pre-liminary X.509 patch - does not yet pass tests.
  50200. + *
  50201. + * Revision 1.10 2003/02/06 01:51:41 rgb
  50202. + * Removed no longer relevant comment
  50203. + *
  50204. + * Revision 1.9 2003/01/30 02:32:44 rgb
  50205. + *
  50206. + * Transmit error code through to caller from callee for better diagnosis of problems.
  50207. + *
  50208. + * Revision 1.8 2002/12/13 22:42:22 mcr
  50209. + * restored sa_ref code
  50210. + *
  50211. + * Revision 1.7 2002/12/13 22:40:48 mcr
  50212. + * temporarily removed sadb_x_sa_ref reference for 2.xx
  50213. + *
  50214. + * Revision 1.6 2002/10/05 05:02:58 dhr
  50215. + *
  50216. + * C labels go on statements
  50217. + *
  50218. + * Revision 1.5 2002/09/20 15:41:08 rgb
  50219. + * Switch from pfkey_alloc_ipsec_sa() to ipsec_sa_alloc().
  50220. + * Added sadb_x_sa_ref to struct sadb_sa.
  50221. + *
  50222. + * Revision 1.4 2002/09/20 05:02:02 rgb
  50223. + * Added memory allocation debugging.
  50224. + *
  50225. + * Revision 1.3 2002/07/24 18:44:54 rgb
  50226. + * Type fiddling to tame ia64 compiler.
  50227. + *
  50228. + * Revision 1.2 2002/05/27 18:55:03 rgb
  50229. + * Remove final vistiges of tdb references via IPSEC_KLIPS1_COMPAT.
  50230. + *
  50231. + * Revision 1.1 2002/05/14 02:33:51 rgb
  50232. + * Moved all the extension processing functions to pfkey_v2_ext_process.c.
  50233. + *
  50234. + *
  50235. + * Local variables:
  50236. + * c-file-style: "linux"
  50237. + * End:
  50238. + *
  50239. + */
  50240. --- /dev/null Tue Mar 11 13:02:56 2003
  50241. +++ linux/net/ipsec/pfkey_v2_parse.c Mon Feb 9 13:51:03 2004
  50242. @@ -0,0 +1,1846 @@
  50243. +/*
  50244. + * RFC2367 PF_KEYv2 Key management API message parser
  50245. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs.
  50246. + *
  50247. + * This program is free software; you can redistribute it and/or modify it
  50248. + * under the terms of the GNU General Public License as published by the
  50249. + * Free Software Foundation; either version 2 of the License, or (at your
  50250. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  50251. + *
  50252. + * This program is distributed in the hope that it will be useful, but
  50253. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  50254. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  50255. + * for more details.
  50256. + *
  50257. + * RCSID $Id: pfkey_v2_parse.c,v 1.65 2005/04/06 17:46:05 mcr Exp $
  50258. + */
  50259. +
  50260. +/*
  50261. + * Template from klips/net/ipsec/ipsec/ipsec_parser.c.
  50262. + */
  50263. +
  50264. +char pfkey_v2_parse_c_version[] = "$Id: pfkey_v2_parse.c,v 1.65 2005/04/06 17:46:05 mcr Exp $";
  50265. +
  50266. +/*
  50267. + * Some ugly stuff to allow consistent debugging code for use in the
  50268. + * kernel and in user space
  50269. +*/
  50270. +
  50271. +#ifdef __KERNEL__
  50272. +
  50273. +# include <linux/kernel.h> /* for printk */
  50274. +
  50275. +#include "openswan/ipsec_kversion.h" /* for malloc switch */
  50276. +
  50277. +# ifdef MALLOC_SLAB
  50278. +# include <linux/slab.h> /* kmalloc() */
  50279. +# else /* MALLOC_SLAB */
  50280. +# include <linux/malloc.h> /* kmalloc() */
  50281. +# endif /* MALLOC_SLAB */
  50282. +# include <linux/errno.h> /* error codes */
  50283. +# include <linux/types.h> /* size_t */
  50284. +# include <linux/interrupt.h> /* mark_bh */
  50285. +
  50286. +# include <linux/netdevice.h> /* struct device, and other headers */
  50287. +# include <linux/etherdevice.h> /* eth_type_trans */
  50288. +# include <linux/ip.h> /* struct iphdr */
  50289. +# if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  50290. +# include <linux/ipv6.h> /* struct ipv6hdr */
  50291. +# endif /* if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
  50292. +extern int debug_pfkey;
  50293. +
  50294. +# include <openswan.h>
  50295. +
  50296. +#include "openswan/ipsec_encap.h"
  50297. +
  50298. +#else /* __KERNEL__ */
  50299. +
  50300. +# include <sys/types.h>
  50301. +# include <linux/types.h>
  50302. +# include <linux/errno.h>
  50303. +
  50304. +# include <openswan.h>
  50305. +# include "constants.h"
  50306. +# include "programs/pluto/defs.h" /* for PRINTF_LIKE */
  50307. +
  50308. +#endif /* __KERNEL__ */
  50309. +
  50310. +
  50311. +#include <pfkeyv2.h>
  50312. +#include <pfkey.h>
  50313. +
  50314. +#include "openswan/ipsec_sa.h" /* IPSEC_SAREF_NULL, IPSEC_SA_REF_TABLE_IDX_WIDTH */
  50315. +
  50316. +/*
  50317. + * how to handle debugging for pfkey.
  50318. + */
  50319. +#include <openswan/pfkey_debug.h>
  50320. +
  50321. +unsigned int pfkey_lib_debug = PF_KEY_DEBUG_PARSE_NONE;
  50322. +void (*pfkey_debug_func)(const char *message, ...) PRINTF_LIKE(1);
  50323. +void (*pfkey_error_func)(const char *message, ...) PRINTF_LIKE(1);
  50324. +
  50325. +
  50326. +#define SENDERR(_x) do { error = -(_x); goto errlab; } while (0)
  50327. +
  50328. +struct satype_tbl {
  50329. + uint8_t proto;
  50330. + uint8_t satype;
  50331. + char* name;
  50332. +} static satype_tbl[] = {
  50333. +#ifdef __KERNEL__
  50334. + { IPPROTO_ESP, SADB_SATYPE_ESP, "ESP" },
  50335. + { IPPROTO_AH, SADB_SATYPE_AH, "AH" },
  50336. + { IPPROTO_IPIP, SADB_X_SATYPE_IPIP, "IPIP" },
  50337. +#ifdef CONFIG_KLIPS_IPCOMP
  50338. + { IPPROTO_COMP, SADB_X_SATYPE_COMP, "COMP" },
  50339. +#endif /* CONFIG_KLIPS_IPCOMP */
  50340. + { IPPROTO_INT, SADB_X_SATYPE_INT, "INT" },
  50341. +#else /* __KERNEL__ */
  50342. + { SA_ESP, SADB_SATYPE_ESP, "ESP" },
  50343. + { SA_AH, SADB_SATYPE_AH, "AH" },
  50344. + { SA_IPIP, SADB_X_SATYPE_IPIP, "IPIP" },
  50345. + { SA_COMP, SADB_X_SATYPE_COMP, "COMP" },
  50346. + { SA_INT, SADB_X_SATYPE_INT, "INT" },
  50347. +#endif /* __KERNEL__ */
  50348. + { 0, 0, "UNKNOWN" }
  50349. +};
  50350. +
  50351. +uint8_t
  50352. +satype2proto(uint8_t satype)
  50353. +{
  50354. + int i =0;
  50355. +
  50356. + while(satype_tbl[i].satype != satype && satype_tbl[i].satype != 0) {
  50357. + i++;
  50358. + }
  50359. + return satype_tbl[i].proto;
  50360. +}
  50361. +
  50362. +uint8_t
  50363. +proto2satype(uint8_t proto)
  50364. +{
  50365. + int i = 0;
  50366. +
  50367. + while(satype_tbl[i].proto != proto && satype_tbl[i].proto != 0) {
  50368. + i++;
  50369. + }
  50370. + return satype_tbl[i].satype;
  50371. +}
  50372. +
  50373. +char*
  50374. +satype2name(uint8_t satype)
  50375. +{
  50376. + int i = 0;
  50377. +
  50378. + while(satype_tbl[i].satype != satype && satype_tbl[i].satype != 0) {
  50379. + i++;
  50380. + }
  50381. + return satype_tbl[i].name;
  50382. +}
  50383. +
  50384. +char*
  50385. +proto2name(uint8_t proto)
  50386. +{
  50387. + int i = 0;
  50388. +
  50389. + while(satype_tbl[i].proto != proto && satype_tbl[i].proto != 0) {
  50390. + i++;
  50391. + }
  50392. + return satype_tbl[i].name;
  50393. +}
  50394. +
  50395. +/* Default extension parsers taken from the KLIPS code */
  50396. +
  50397. +DEBUG_NO_STATIC int
  50398. +pfkey_sa_parse(struct sadb_ext *pfkey_ext)
  50399. +{
  50400. + int error = 0;
  50401. + struct sadb_sa *pfkey_sa = (struct sadb_sa *)pfkey_ext;
  50402. +#if 0
  50403. + struct sadb_sa sav2;
  50404. +#endif
  50405. +
  50406. + /* sanity checks... */
  50407. + if(!pfkey_sa) {
  50408. + ERROR("pfkey_sa_parse: "
  50409. + "NULL pointer passed in.\n");
  50410. + SENDERR(EINVAL);
  50411. + }
  50412. +
  50413. +#if 0
  50414. + /* check if this structure is short, and if so, fix it up.
  50415. + * XXX this is NOT the way to do things.
  50416. + */
  50417. + if(pfkey_sa->sadb_sa_len == sizeof(struct sadb_sa_v1)/IPSEC_PFKEYv2_ALIGN) {
  50418. +
  50419. + /* yes, so clear out a temporary structure, and copy first */
  50420. + memset(&sav2, 0, sizeof(sav2));
  50421. + memcpy(&sav2, pfkey_sa, sizeof(struct sadb_sa_v1));
  50422. + sav2.sadb_x_sa_ref=-1;
  50423. + sav2.sadb_sa_len = sizeof(struct sadb_sa) / IPSEC_PFKEYv2_ALIGN;
  50424. +
  50425. + pfkey_sa = &sav2;
  50426. + }
  50427. +#endif
  50428. +
  50429. +
  50430. + if(pfkey_sa->sadb_sa_len != sizeof(struct sadb_sa) / IPSEC_PFKEYv2_ALIGN) {
  50431. + ERROR(
  50432. + "pfkey_sa_parse: "
  50433. + "length wrong pfkey_sa->sadb_sa_len=%d sizeof(struct sadb_sa)=%d.\n",
  50434. + pfkey_sa->sadb_sa_len,
  50435. + (int)sizeof(struct sadb_sa));
  50436. + SENDERR(EINVAL);
  50437. + }
  50438. +
  50439. +#if SADB_EALG_MAX < 255
  50440. + if(pfkey_sa->sadb_sa_encrypt > SADB_EALG_MAX) {
  50441. + ERROR(
  50442. + "pfkey_sa_parse: "
  50443. + "pfkey_sa->sadb_sa_encrypt=%d > SADB_EALG_MAX=%d.\n",
  50444. + pfkey_sa->sadb_sa_encrypt,
  50445. + SADB_EALG_MAX);
  50446. + SENDERR(EINVAL);
  50447. + }
  50448. +#endif
  50449. +
  50450. +#if SADB_AALG_MAX < 255
  50451. + if(pfkey_sa->sadb_sa_auth > SADB_AALG_MAX) {
  50452. + ERROR(
  50453. + "pfkey_sa_parse: "
  50454. + "pfkey_sa->sadb_sa_auth=%d > SADB_AALG_MAX=%d.\n",
  50455. + pfkey_sa->sadb_sa_auth,
  50456. + SADB_AALG_MAX);
  50457. + SENDERR(EINVAL);
  50458. + }
  50459. +#endif
  50460. +
  50461. +#if SADB_SASTATE_MAX < 255
  50462. + if(pfkey_sa->sadb_sa_state > SADB_SASTATE_MAX) {
  50463. + ERROR(
  50464. + "pfkey_sa_parse: "
  50465. + "state=%d exceeds MAX=%d.\n",
  50466. + pfkey_sa->sadb_sa_state,
  50467. + SADB_SASTATE_MAX);
  50468. + SENDERR(EINVAL);
  50469. + }
  50470. +#endif
  50471. +
  50472. + if(pfkey_sa->sadb_sa_state == SADB_SASTATE_DEAD) {
  50473. + ERROR(
  50474. + "pfkey_sa_parse: "
  50475. + "state=%d is DEAD=%d.\n",
  50476. + pfkey_sa->sadb_sa_state,
  50477. + SADB_SASTATE_DEAD);
  50478. + SENDERR(EINVAL);
  50479. + }
  50480. +
  50481. + if(pfkey_sa->sadb_sa_replay > 64) {
  50482. + ERROR(
  50483. + "pfkey_sa_parse: "
  50484. + "replay window size: %d -- must be 0 <= size <= 64\n",
  50485. + pfkey_sa->sadb_sa_replay);
  50486. + SENDERR(EINVAL);
  50487. + }
  50488. +
  50489. + if(! ((pfkey_sa->sadb_sa_exttype == SADB_EXT_SA) ||
  50490. + (pfkey_sa->sadb_sa_exttype == SADB_X_EXT_SA2)))
  50491. + {
  50492. + ERROR(
  50493. + "pfkey_sa_parse: "
  50494. + "unknown exttype=%d, expecting SADB_EXT_SA=%d or SADB_X_EXT_SA2=%d.\n",
  50495. + pfkey_sa->sadb_sa_exttype,
  50496. + SADB_EXT_SA,
  50497. + SADB_X_EXT_SA2);
  50498. + SENDERR(EINVAL);
  50499. + }
  50500. +
  50501. + if((IPSEC_SAREF_NULL != pfkey_sa->sadb_x_sa_ref) && (pfkey_sa->sadb_x_sa_ref >= (1 << IPSEC_SA_REF_TABLE_IDX_WIDTH))) {
  50502. + ERROR(
  50503. + "pfkey_sa_parse: "
  50504. + "SAref=%d must be (SAref == IPSEC_SAREF_NULL(%d) || SAref < IPSEC_SA_REF_TABLE_NUM_ENTRIES(%d)).\n",
  50505. + pfkey_sa->sadb_x_sa_ref,
  50506. + IPSEC_SAREF_NULL,
  50507. + IPSEC_SA_REF_TABLE_NUM_ENTRIES);
  50508. + SENDERR(EINVAL);
  50509. + }
  50510. +
  50511. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  50512. + "pfkey_sa_parse: "
  50513. + "successfully found len=%d exttype=%d(%s) spi=%08lx replay=%d state=%d auth=%d encrypt=%d flags=%d ref=%d.\n",
  50514. + pfkey_sa->sadb_sa_len,
  50515. + pfkey_sa->sadb_sa_exttype,
  50516. + pfkey_v2_sadb_ext_string(pfkey_sa->sadb_sa_exttype),
  50517. + (long unsigned int)ntohl(pfkey_sa->sadb_sa_spi),
  50518. + pfkey_sa->sadb_sa_replay,
  50519. + pfkey_sa->sadb_sa_state,
  50520. + pfkey_sa->sadb_sa_auth,
  50521. + pfkey_sa->sadb_sa_encrypt,
  50522. + pfkey_sa->sadb_sa_flags,
  50523. + pfkey_sa->sadb_x_sa_ref);
  50524. +
  50525. + errlab:
  50526. + return error;
  50527. +}
  50528. +
  50529. +DEBUG_NO_STATIC int
  50530. +pfkey_lifetime_parse(struct sadb_ext *pfkey_ext)
  50531. +{
  50532. + int error = 0;
  50533. + struct sadb_lifetime *pfkey_lifetime = (struct sadb_lifetime *)pfkey_ext;
  50534. +
  50535. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  50536. + "pfkey_lifetime_parse:enter\n");
  50537. + /* sanity checks... */
  50538. + if(!pfkey_lifetime) {
  50539. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50540. + "pfkey_lifetime_parse: "
  50541. + "NULL pointer passed in.\n");
  50542. + SENDERR(EINVAL);
  50543. + }
  50544. +
  50545. + if(pfkey_lifetime->sadb_lifetime_len !=
  50546. + sizeof(struct sadb_lifetime) / IPSEC_PFKEYv2_ALIGN) {
  50547. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50548. + "pfkey_lifetime_parse: "
  50549. + "length wrong pfkey_lifetime->sadb_lifetime_len=%d sizeof(struct sadb_lifetime)=%d.\n",
  50550. + pfkey_lifetime->sadb_lifetime_len,
  50551. + (int)sizeof(struct sadb_lifetime));
  50552. + SENDERR(EINVAL);
  50553. + }
  50554. +
  50555. + if((pfkey_lifetime->sadb_lifetime_exttype != SADB_EXT_LIFETIME_HARD) &&
  50556. + (pfkey_lifetime->sadb_lifetime_exttype != SADB_EXT_LIFETIME_SOFT) &&
  50557. + (pfkey_lifetime->sadb_lifetime_exttype != SADB_EXT_LIFETIME_CURRENT)) {
  50558. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50559. + "pfkey_lifetime_parse: "
  50560. + "unexpected ext_type=%d.\n",
  50561. + pfkey_lifetime->sadb_lifetime_exttype);
  50562. + SENDERR(EINVAL);
  50563. + }
  50564. +
  50565. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  50566. + "pfkey_lifetime_parse: "
  50567. + "life_type=%d(%s) alloc=%u bytes=%u add=%u use=%u pkts=%u.\n",
  50568. + pfkey_lifetime->sadb_lifetime_exttype,
  50569. + pfkey_v2_sadb_ext_string(pfkey_lifetime->sadb_lifetime_exttype),
  50570. + pfkey_lifetime->sadb_lifetime_allocations,
  50571. + (unsigned)pfkey_lifetime->sadb_lifetime_bytes,
  50572. + (unsigned)pfkey_lifetime->sadb_lifetime_addtime,
  50573. + (unsigned)pfkey_lifetime->sadb_lifetime_usetime,
  50574. + pfkey_lifetime->sadb_x_lifetime_packets);
  50575. +errlab:
  50576. + return error;
  50577. +}
  50578. +
  50579. +DEBUG_NO_STATIC int
  50580. +pfkey_address_parse(struct sadb_ext *pfkey_ext)
  50581. +{
  50582. + int error = 0;
  50583. + int saddr_len = 0;
  50584. + struct sadb_address *pfkey_address = (struct sadb_address *)pfkey_ext;
  50585. + struct sockaddr* s = (struct sockaddr*)((char*)pfkey_address + sizeof(*pfkey_address));
  50586. + char ipaddr_txt[ADDRTOT_BUF];
  50587. +
  50588. + /* sanity checks... */
  50589. + if(!pfkey_address) {
  50590. + ERROR(
  50591. + "pfkey_address_parse: "
  50592. + "NULL pointer passed in.\n");
  50593. + SENDERR(EINVAL);
  50594. + }
  50595. +
  50596. + if(pfkey_address->sadb_address_len <
  50597. + (sizeof(struct sadb_address) + sizeof(struct sockaddr))/
  50598. + IPSEC_PFKEYv2_ALIGN) {
  50599. + ERROR("pfkey_address_parse: "
  50600. + "size wrong 1 ext_len=%d, adr_ext_len=%d, saddr_len=%d.\n",
  50601. + pfkey_address->sadb_address_len,
  50602. + (int)sizeof(struct sadb_address),
  50603. + (int)sizeof(struct sockaddr));
  50604. + SENDERR(EINVAL);
  50605. + }
  50606. +
  50607. + if(pfkey_address->sadb_address_reserved) {
  50608. + ERROR("pfkey_address_parse: "
  50609. + "res=%d, must be zero.\n",
  50610. + pfkey_address->sadb_address_reserved);
  50611. + SENDERR(EINVAL);
  50612. + }
  50613. +
  50614. + switch(pfkey_address->sadb_address_exttype) {
  50615. + case SADB_EXT_ADDRESS_SRC:
  50616. + case SADB_EXT_ADDRESS_DST:
  50617. + case SADB_EXT_ADDRESS_PROXY:
  50618. + case SADB_X_EXT_ADDRESS_DST2:
  50619. + case SADB_X_EXT_ADDRESS_SRC_FLOW:
  50620. + case SADB_X_EXT_ADDRESS_DST_FLOW:
  50621. + case SADB_X_EXT_ADDRESS_SRC_MASK:
  50622. + case SADB_X_EXT_ADDRESS_DST_MASK:
  50623. +#ifdef NAT_TRAVERSAL
  50624. + case SADB_X_EXT_NAT_T_OA:
  50625. +#endif
  50626. + break;
  50627. + default:
  50628. + ERROR(
  50629. + "pfkey_address_parse: "
  50630. + "unexpected ext_type=%d.\n",
  50631. + pfkey_address->sadb_address_exttype);
  50632. + SENDERR(ENOPKG);
  50633. + }
  50634. +
  50635. + switch(s->sa_family) {
  50636. + case AF_INET:
  50637. + saddr_len = sizeof(struct sockaddr_in);
  50638. + sprintf(ipaddr_txt, "%d.%d.%d.%d"
  50639. + , (((struct sockaddr_in*)s)->sin_addr.s_addr >> 0) & 0xFF
  50640. + , (((struct sockaddr_in*)s)->sin_addr.s_addr >> 8) & 0xFF
  50641. + , (((struct sockaddr_in*)s)->sin_addr.s_addr >> 16) & 0xFF
  50642. + , (((struct sockaddr_in*)s)->sin_addr.s_addr >> 24) & 0xFF);
  50643. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  50644. + "pfkey_address_parse: "
  50645. + "found exttype=%u(%s) family=%d(AF_INET) address=%s proto=%u port=%u.\n",
  50646. + pfkey_address->sadb_address_exttype,
  50647. + pfkey_v2_sadb_ext_string(pfkey_address->sadb_address_exttype),
  50648. + s->sa_family,
  50649. + ipaddr_txt,
  50650. + pfkey_address->sadb_address_proto,
  50651. + ntohs(((struct sockaddr_in*)s)->sin_port));
  50652. + break;
  50653. + case AF_INET6:
  50654. + saddr_len = sizeof(struct sockaddr_in6);
  50655. + sprintf(ipaddr_txt, "%x:%x:%x:%x:%x:%x:%x:%x"
  50656. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[0])
  50657. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[1])
  50658. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[2])
  50659. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[3])
  50660. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[4])
  50661. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[5])
  50662. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[6])
  50663. + , ntohs(((struct sockaddr_in6*)s)->sin6_addr.s6_addr16[7]));
  50664. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  50665. + "pfkey_address_parse: "
  50666. + "found exttype=%u(%s) family=%d(AF_INET6) address=%s proto=%u port=%u.\n",
  50667. + pfkey_address->sadb_address_exttype,
  50668. + pfkey_v2_sadb_ext_string(pfkey_address->sadb_address_exttype),
  50669. + s->sa_family,
  50670. + ipaddr_txt,
  50671. + pfkey_address->sadb_address_proto,
  50672. + ((struct sockaddr_in6*)s)->sin6_port);
  50673. + break;
  50674. + default:
  50675. + ERROR(
  50676. + "pfkey_address_parse: "
  50677. + "s->sa_family=%d not supported.\n",
  50678. + s->sa_family);
  50679. + SENDERR(EPFNOSUPPORT);
  50680. + }
  50681. +
  50682. + if(pfkey_address->sadb_address_len !=
  50683. + DIVUP(sizeof(struct sadb_address) + saddr_len, IPSEC_PFKEYv2_ALIGN)) {
  50684. + ERROR(
  50685. + "pfkey_address_parse: "
  50686. + "size wrong 2 ext_len=%d, adr_ext_len=%d, saddr_len=%d.\n",
  50687. + pfkey_address->sadb_address_len,
  50688. + (int)sizeof(struct sadb_address),
  50689. + saddr_len);
  50690. + SENDERR(EINVAL);
  50691. + }
  50692. +
  50693. + if(pfkey_address->sadb_address_prefixlen != 0) {
  50694. + ERROR(
  50695. + "pfkey_address_parse: "
  50696. + "address prefixes not supported yet.\n");
  50697. + SENDERR(EAFNOSUPPORT); /* not supported yet */
  50698. + }
  50699. +
  50700. + /* XXX check if port!=0 */
  50701. +
  50702. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  50703. + "pfkey_address_parse: successful.\n");
  50704. + errlab:
  50705. + return error;
  50706. +}
  50707. +
  50708. +DEBUG_NO_STATIC int
  50709. +pfkey_key_parse(struct sadb_ext *pfkey_ext)
  50710. +{
  50711. + int error = 0;
  50712. + struct sadb_key *pfkey_key = (struct sadb_key *)pfkey_ext;
  50713. +
  50714. + /* sanity checks... */
  50715. +
  50716. + if(!pfkey_key) {
  50717. + ERROR(
  50718. + "pfkey_key_parse: "
  50719. + "NULL pointer passed in.\n");
  50720. + SENDERR(EINVAL);
  50721. + }
  50722. +
  50723. + if(pfkey_key->sadb_key_len < sizeof(struct sadb_key) / IPSEC_PFKEYv2_ALIGN) {
  50724. + ERROR(
  50725. + "pfkey_key_parse: "
  50726. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  50727. + pfkey_key->sadb_key_len,
  50728. + (int)sizeof(struct sadb_key));
  50729. + SENDERR(EINVAL);
  50730. + }
  50731. +
  50732. + if(!pfkey_key->sadb_key_bits) {
  50733. + ERROR(
  50734. + "pfkey_key_parse: "
  50735. + "key length set to zero, must be non-zero.\n");
  50736. + SENDERR(EINVAL);
  50737. + }
  50738. +
  50739. + if(pfkey_key->sadb_key_len !=
  50740. + DIVUP(sizeof(struct sadb_key) * OCTETBITS + pfkey_key->sadb_key_bits,
  50741. + PFKEYBITS)) {
  50742. + ERROR(
  50743. + "pfkey_key_parse: "
  50744. + "key length=%d does not agree with extension length=%d.\n",
  50745. + pfkey_key->sadb_key_bits,
  50746. + pfkey_key->sadb_key_len);
  50747. + SENDERR(EINVAL);
  50748. + }
  50749. +
  50750. + if(pfkey_key->sadb_key_reserved) {
  50751. + ERROR(
  50752. + "pfkey_key_parse: "
  50753. + "res=%d, must be zero.\n",
  50754. + pfkey_key->sadb_key_reserved);
  50755. + SENDERR(EINVAL);
  50756. + }
  50757. +
  50758. + if(! ( (pfkey_key->sadb_key_exttype == SADB_EXT_KEY_AUTH) ||
  50759. + (pfkey_key->sadb_key_exttype == SADB_EXT_KEY_ENCRYPT))) {
  50760. + ERROR(
  50761. + "pfkey_key_parse: "
  50762. + "expecting extension type AUTH or ENCRYPT, got %d.\n",
  50763. + pfkey_key->sadb_key_exttype);
  50764. + SENDERR(EINVAL);
  50765. + }
  50766. +
  50767. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  50768. + "pfkey_key_parse: "
  50769. + "success, found len=%d exttype=%d(%s) bits=%d reserved=%d.\n",
  50770. + pfkey_key->sadb_key_len,
  50771. + pfkey_key->sadb_key_exttype,
  50772. + pfkey_v2_sadb_ext_string(pfkey_key->sadb_key_exttype),
  50773. + pfkey_key->sadb_key_bits,
  50774. + pfkey_key->sadb_key_reserved);
  50775. +
  50776. +errlab:
  50777. + return error;
  50778. +}
  50779. +
  50780. +DEBUG_NO_STATIC int
  50781. +pfkey_ident_parse(struct sadb_ext *pfkey_ext)
  50782. +{
  50783. + int error = 0;
  50784. + struct sadb_ident *pfkey_ident = (struct sadb_ident *)pfkey_ext;
  50785. +
  50786. + /* sanity checks... */
  50787. + if(pfkey_ident->sadb_ident_len < sizeof(struct sadb_ident) / IPSEC_PFKEYv2_ALIGN) {
  50788. + ERROR(
  50789. + "pfkey_ident_parse: "
  50790. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  50791. + pfkey_ident->sadb_ident_len,
  50792. + (int)sizeof(struct sadb_ident));
  50793. + SENDERR(EINVAL);
  50794. + }
  50795. +
  50796. + if(pfkey_ident->sadb_ident_type > SADB_IDENTTYPE_MAX) {
  50797. + ERROR(
  50798. + "pfkey_ident_parse: "
  50799. + "ident_type=%d out of range, must be less than %d.\n",
  50800. + pfkey_ident->sadb_ident_type,
  50801. + SADB_IDENTTYPE_MAX);
  50802. + SENDERR(EINVAL);
  50803. + }
  50804. +
  50805. + if(pfkey_ident->sadb_ident_reserved) {
  50806. + ERROR(
  50807. + "pfkey_ident_parse: "
  50808. + "res=%d, must be zero.\n",
  50809. + pfkey_ident->sadb_ident_reserved);
  50810. + SENDERR(EINVAL);
  50811. + }
  50812. +
  50813. + /* string terminator/padding must be zero */
  50814. + if(pfkey_ident->sadb_ident_len > sizeof(struct sadb_ident) / IPSEC_PFKEYv2_ALIGN) {
  50815. + if(*((char*)pfkey_ident + pfkey_ident->sadb_ident_len * IPSEC_PFKEYv2_ALIGN - 1)) {
  50816. + ERROR(
  50817. + "pfkey_ident_parse: "
  50818. + "string padding must be zero, last is 0x%02x.\n",
  50819. + *((char*)pfkey_ident +
  50820. + pfkey_ident->sadb_ident_len * IPSEC_PFKEYv2_ALIGN - 1));
  50821. + SENDERR(EINVAL);
  50822. + }
  50823. + }
  50824. +
  50825. + if( ! ((pfkey_ident->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC) ||
  50826. + (pfkey_ident->sadb_ident_exttype == SADB_EXT_IDENTITY_DST))) {
  50827. + ERROR(
  50828. + "pfkey_key_parse: "
  50829. + "expecting extension type IDENTITY_SRC or IDENTITY_DST, got %d.\n",
  50830. + pfkey_ident->sadb_ident_exttype);
  50831. + SENDERR(EINVAL);
  50832. + }
  50833. +
  50834. +errlab:
  50835. + return error;
  50836. +}
  50837. +
  50838. +DEBUG_NO_STATIC int
  50839. +pfkey_sens_parse(struct sadb_ext *pfkey_ext)
  50840. +{
  50841. + int error = 0;
  50842. + struct sadb_sens *pfkey_sens = (struct sadb_sens *)pfkey_ext;
  50843. +
  50844. + /* sanity checks... */
  50845. + if(pfkey_sens->sadb_sens_len < sizeof(struct sadb_sens) / IPSEC_PFKEYv2_ALIGN) {
  50846. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50847. + "pfkey_sens_parse: "
  50848. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  50849. + pfkey_sens->sadb_sens_len,
  50850. + (int)sizeof(struct sadb_sens));
  50851. + SENDERR(EINVAL);
  50852. + }
  50853. +
  50854. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50855. + "pfkey_sens_parse: "
  50856. + "Sorry, I can't parse exttype=%d yet.\n",
  50857. + pfkey_ext->sadb_ext_type);
  50858. +#if 0
  50859. + SENDERR(EINVAL); /* don't process these yet */
  50860. +#endif
  50861. +
  50862. +errlab:
  50863. + return error;
  50864. +}
  50865. +
  50866. +DEBUG_NO_STATIC int
  50867. +pfkey_prop_parse(struct sadb_ext *pfkey_ext)
  50868. +{
  50869. + int error = 0;
  50870. + int i, num_comb;
  50871. + struct sadb_prop *pfkey_prop = (struct sadb_prop *)pfkey_ext;
  50872. + struct sadb_comb *pfkey_comb = (struct sadb_comb *)((char*)pfkey_ext + sizeof(struct sadb_prop));
  50873. +
  50874. + /* sanity checks... */
  50875. + if((pfkey_prop->sadb_prop_len < sizeof(struct sadb_prop) / IPSEC_PFKEYv2_ALIGN) ||
  50876. + (((pfkey_prop->sadb_prop_len * IPSEC_PFKEYv2_ALIGN) - sizeof(struct sadb_prop)) % sizeof(struct sadb_comb))) {
  50877. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50878. + "pfkey_prop_parse: "
  50879. + "size wrong ext_len=%d, prop_ext_len=%d comb_ext_len=%d.\n",
  50880. + pfkey_prop->sadb_prop_len,
  50881. + (int)sizeof(struct sadb_prop),
  50882. + (int)sizeof(struct sadb_comb));
  50883. + SENDERR(EINVAL);
  50884. + }
  50885. +
  50886. + if(pfkey_prop->sadb_prop_replay > 64) {
  50887. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50888. + "pfkey_prop_parse: "
  50889. + "replay window size: %d -- must be 0 <= size <= 64\n",
  50890. + pfkey_prop->sadb_prop_replay);
  50891. + SENDERR(EINVAL);
  50892. + }
  50893. +
  50894. + for(i=0; i<3; i++) {
  50895. + if(pfkey_prop->sadb_prop_reserved[i]) {
  50896. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50897. + "pfkey_prop_parse: "
  50898. + "res[%d]=%d, must be zero.\n",
  50899. + i, pfkey_prop->sadb_prop_reserved[i]);
  50900. + SENDERR(EINVAL);
  50901. + }
  50902. + }
  50903. +
  50904. + num_comb = ((pfkey_prop->sadb_prop_len * IPSEC_PFKEYv2_ALIGN) - sizeof(struct sadb_prop)) / sizeof(struct sadb_comb);
  50905. +
  50906. + for(i = 0; i < num_comb; i++) {
  50907. + if(pfkey_comb->sadb_comb_auth > SADB_AALG_MAX) {
  50908. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50909. + "pfkey_prop_parse: "
  50910. + "pfkey_comb[%d]->sadb_comb_auth=%d > SADB_AALG_MAX=%d.\n",
  50911. + i,
  50912. + pfkey_comb->sadb_comb_auth,
  50913. + SADB_AALG_MAX);
  50914. + SENDERR(EINVAL);
  50915. + }
  50916. +
  50917. + if(pfkey_comb->sadb_comb_auth) {
  50918. + if(!pfkey_comb->sadb_comb_auth_minbits) {
  50919. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50920. + "pfkey_prop_parse: "
  50921. + "pfkey_comb[%d]->sadb_comb_auth_minbits=0, fatal.\n",
  50922. + i);
  50923. + SENDERR(EINVAL);
  50924. + }
  50925. + if(!pfkey_comb->sadb_comb_auth_maxbits) {
  50926. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50927. + "pfkey_prop_parse: "
  50928. + "pfkey_comb[%d]->sadb_comb_auth_maxbits=0, fatal.\n",
  50929. + i);
  50930. + SENDERR(EINVAL);
  50931. + }
  50932. + if(pfkey_comb->sadb_comb_auth_minbits > pfkey_comb->sadb_comb_auth_maxbits) {
  50933. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50934. + "pfkey_prop_parse: "
  50935. + "pfkey_comb[%d]->sadb_comb_auth_minbits=%d > maxbits=%d, fatal.\n",
  50936. + i,
  50937. + pfkey_comb->sadb_comb_auth_minbits,
  50938. + pfkey_comb->sadb_comb_auth_maxbits);
  50939. + SENDERR(EINVAL);
  50940. + }
  50941. + } else {
  50942. + if(pfkey_comb->sadb_comb_auth_minbits) {
  50943. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50944. + "pfkey_prop_parse: "
  50945. + "pfkey_comb[%d]->sadb_comb_auth_minbits=%d != 0, fatal.\n",
  50946. + i,
  50947. + pfkey_comb->sadb_comb_auth_minbits);
  50948. + SENDERR(EINVAL);
  50949. + }
  50950. + if(pfkey_comb->sadb_comb_auth_maxbits) {
  50951. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50952. + "pfkey_prop_parse: "
  50953. + "pfkey_comb[%d]->sadb_comb_auth_maxbits=%d != 0, fatal.\n",
  50954. + i,
  50955. + pfkey_comb->sadb_comb_auth_maxbits);
  50956. + SENDERR(EINVAL);
  50957. + }
  50958. + }
  50959. +
  50960. +#if SADB_EALG_MAX < 255
  50961. + if(pfkey_comb->sadb_comb_encrypt > SADB_EALG_MAX) {
  50962. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50963. + "pfkey_comb_parse: "
  50964. + "pfkey_comb[%d]->sadb_comb_encrypt=%d > SADB_EALG_MAX=%d.\n",
  50965. + i,
  50966. + pfkey_comb->sadb_comb_encrypt,
  50967. + SADB_EALG_MAX);
  50968. + SENDERR(EINVAL);
  50969. + }
  50970. +#endif
  50971. +
  50972. + if(pfkey_comb->sadb_comb_encrypt) {
  50973. + if(!pfkey_comb->sadb_comb_encrypt_minbits) {
  50974. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50975. + "pfkey_prop_parse: "
  50976. + "pfkey_comb[%d]->sadb_comb_encrypt_minbits=0, fatal.\n",
  50977. + i);
  50978. + SENDERR(EINVAL);
  50979. + }
  50980. + if(!pfkey_comb->sadb_comb_encrypt_maxbits) {
  50981. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50982. + "pfkey_prop_parse: "
  50983. + "pfkey_comb[%d]->sadb_comb_encrypt_maxbits=0, fatal.\n",
  50984. + i);
  50985. + SENDERR(EINVAL);
  50986. + }
  50987. + if(pfkey_comb->sadb_comb_encrypt_minbits > pfkey_comb->sadb_comb_encrypt_maxbits) {
  50988. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50989. + "pfkey_prop_parse: "
  50990. + "pfkey_comb[%d]->sadb_comb_encrypt_minbits=%d > maxbits=%d, fatal.\n",
  50991. + i,
  50992. + pfkey_comb->sadb_comb_encrypt_minbits,
  50993. + pfkey_comb->sadb_comb_encrypt_maxbits);
  50994. + SENDERR(EINVAL);
  50995. + }
  50996. + } else {
  50997. + if(pfkey_comb->sadb_comb_encrypt_minbits) {
  50998. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  50999. + "pfkey_prop_parse: "
  51000. + "pfkey_comb[%d]->sadb_comb_encrypt_minbits=%d != 0, fatal.\n",
  51001. + i,
  51002. + pfkey_comb->sadb_comb_encrypt_minbits);
  51003. + SENDERR(EINVAL);
  51004. + }
  51005. + if(pfkey_comb->sadb_comb_encrypt_maxbits) {
  51006. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51007. + "pfkey_prop_parse: "
  51008. + "pfkey_comb[%d]->sadb_comb_encrypt_maxbits=%d != 0, fatal.\n",
  51009. + i,
  51010. + pfkey_comb->sadb_comb_encrypt_maxbits);
  51011. + SENDERR(EINVAL);
  51012. + }
  51013. + }
  51014. +
  51015. + /* XXX do sanity check on flags */
  51016. +
  51017. + if(pfkey_comb->sadb_comb_hard_allocations && pfkey_comb->sadb_comb_soft_allocations > pfkey_comb->sadb_comb_hard_allocations) {
  51018. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51019. + "pfkey_prop_parse: "
  51020. + "pfkey_comb[%d]->sadb_comb_soft_allocations=%d > hard_allocations=%d, fatal.\n",
  51021. + i,
  51022. + pfkey_comb->sadb_comb_soft_allocations,
  51023. + pfkey_comb->sadb_comb_hard_allocations);
  51024. + SENDERR(EINVAL);
  51025. + }
  51026. +
  51027. + if(pfkey_comb->sadb_comb_hard_bytes && pfkey_comb->sadb_comb_soft_bytes > pfkey_comb->sadb_comb_hard_bytes) {
  51028. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51029. + "pfkey_prop_parse: "
  51030. + "pfkey_comb[%d]->sadb_comb_soft_bytes=%Ld > hard_bytes=%Ld, fatal.\n",
  51031. + i,
  51032. + (unsigned long long int)pfkey_comb->sadb_comb_soft_bytes,
  51033. + (unsigned long long int)pfkey_comb->sadb_comb_hard_bytes);
  51034. + SENDERR(EINVAL);
  51035. + }
  51036. +
  51037. + if(pfkey_comb->sadb_comb_hard_addtime && pfkey_comb->sadb_comb_soft_addtime > pfkey_comb->sadb_comb_hard_addtime) {
  51038. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51039. + "pfkey_prop_parse: "
  51040. + "pfkey_comb[%d]->sadb_comb_soft_addtime=%Ld > hard_addtime=%Ld, fatal.\n",
  51041. + i,
  51042. + (unsigned long long int)pfkey_comb->sadb_comb_soft_addtime,
  51043. + (unsigned long long int)pfkey_comb->sadb_comb_hard_addtime);
  51044. + SENDERR(EINVAL);
  51045. + }
  51046. +
  51047. + if(pfkey_comb->sadb_comb_hard_usetime && pfkey_comb->sadb_comb_soft_usetime > pfkey_comb->sadb_comb_hard_usetime) {
  51048. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51049. + "pfkey_prop_parse: "
  51050. + "pfkey_comb[%d]->sadb_comb_soft_usetime=%Ld > hard_usetime=%Ld, fatal.\n",
  51051. + i,
  51052. + (unsigned long long int)pfkey_comb->sadb_comb_soft_usetime,
  51053. + (unsigned long long int)pfkey_comb->sadb_comb_hard_usetime);
  51054. + SENDERR(EINVAL);
  51055. + }
  51056. +
  51057. + if(pfkey_comb->sadb_x_comb_hard_packets && pfkey_comb->sadb_x_comb_soft_packets > pfkey_comb->sadb_x_comb_hard_packets) {
  51058. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51059. + "pfkey_prop_parse: "
  51060. + "pfkey_comb[%d]->sadb_x_comb_soft_packets=%d > hard_packets=%d, fatal.\n",
  51061. + i,
  51062. + pfkey_comb->sadb_x_comb_soft_packets,
  51063. + pfkey_comb->sadb_x_comb_hard_packets);
  51064. + SENDERR(EINVAL);
  51065. + }
  51066. +
  51067. + if(pfkey_comb->sadb_comb_reserved) {
  51068. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51069. + "pfkey_prop_parse: "
  51070. + "comb[%d].res=%d, must be zero.\n",
  51071. + i,
  51072. + pfkey_comb->sadb_comb_reserved);
  51073. + SENDERR(EINVAL);
  51074. + }
  51075. + pfkey_comb++;
  51076. + }
  51077. +
  51078. +errlab:
  51079. + return error;
  51080. +}
  51081. +
  51082. +DEBUG_NO_STATIC int
  51083. +pfkey_supported_parse(struct sadb_ext *pfkey_ext)
  51084. +{
  51085. + int error = 0;
  51086. + unsigned int i, num_alg;
  51087. + struct sadb_supported *pfkey_supported = (struct sadb_supported *)pfkey_ext;
  51088. + struct sadb_alg *pfkey_alg = (struct sadb_alg*)((char*)pfkey_ext + sizeof(struct sadb_supported));
  51089. +
  51090. + /* sanity checks... */
  51091. + if((pfkey_supported->sadb_supported_len <
  51092. + sizeof(struct sadb_supported) / IPSEC_PFKEYv2_ALIGN) ||
  51093. + (((pfkey_supported->sadb_supported_len * IPSEC_PFKEYv2_ALIGN) -
  51094. + sizeof(struct sadb_supported)) % sizeof(struct sadb_alg))) {
  51095. +
  51096. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51097. + "pfkey_supported_parse: "
  51098. + "size wrong ext_len=%d, supported_ext_len=%d alg_ext_len=%d.\n",
  51099. + pfkey_supported->sadb_supported_len,
  51100. + (int)sizeof(struct sadb_supported),
  51101. + (int)sizeof(struct sadb_alg));
  51102. + SENDERR(EINVAL);
  51103. + }
  51104. +
  51105. + if(pfkey_supported->sadb_supported_reserved) {
  51106. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51107. + "pfkey_supported_parse: "
  51108. + "res=%d, must be zero.\n",
  51109. + pfkey_supported->sadb_supported_reserved);
  51110. + SENDERR(EINVAL);
  51111. + }
  51112. +
  51113. + num_alg = ((pfkey_supported->sadb_supported_len * IPSEC_PFKEYv2_ALIGN) - sizeof(struct sadb_supported)) / sizeof(struct sadb_alg);
  51114. +
  51115. + for(i = 0; i < num_alg; i++) {
  51116. + /* process algo description */
  51117. + if(pfkey_alg->sadb_alg_reserved) {
  51118. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51119. + "pfkey_supported_parse: "
  51120. + "alg[%d], id=%d, ivlen=%d, minbits=%d, maxbits=%d, res=%d, must be zero.\n",
  51121. + i,
  51122. + pfkey_alg->sadb_alg_id,
  51123. + pfkey_alg->sadb_alg_ivlen,
  51124. + pfkey_alg->sadb_alg_minbits,
  51125. + pfkey_alg->sadb_alg_maxbits,
  51126. + pfkey_alg->sadb_alg_reserved);
  51127. + SENDERR(EINVAL);
  51128. + }
  51129. +
  51130. + /* XXX can alg_id auth/enc be determined from info given?
  51131. + Yes, but OpenBSD's method does not iteroperate with rfc2367.
  51132. + rgb, 2000-04-06 */
  51133. +
  51134. + switch(pfkey_supported->sadb_supported_exttype) {
  51135. + case SADB_EXT_SUPPORTED_AUTH:
  51136. + if(pfkey_alg->sadb_alg_id > SADB_AALG_MAX) {
  51137. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51138. + "pfkey_supported_parse: "
  51139. + "alg[%d], alg_id=%d > SADB_AALG_MAX=%d, fatal.\n",
  51140. + i,
  51141. + pfkey_alg->sadb_alg_id,
  51142. + SADB_AALG_MAX);
  51143. + SENDERR(EINVAL);
  51144. + }
  51145. + break;
  51146. + case SADB_EXT_SUPPORTED_ENCRYPT:
  51147. +#if SADB_EALG_MAX < 255
  51148. + if(pfkey_alg->sadb_alg_id > SADB_EALG_MAX) {
  51149. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51150. + "pfkey_supported_parse: "
  51151. + "alg[%d], alg_id=%d > SADB_EALG_MAX=%d, fatal.\n",
  51152. + i,
  51153. + pfkey_alg->sadb_alg_id,
  51154. + SADB_EALG_MAX);
  51155. + SENDERR(EINVAL);
  51156. + }
  51157. +#endif
  51158. + break;
  51159. + default:
  51160. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51161. + "pfkey_supported_parse: "
  51162. + "alg[%d], alg_id=%d > SADB_EALG_MAX=%d, fatal.\n",
  51163. + i,
  51164. + pfkey_alg->sadb_alg_id,
  51165. + SADB_EALG_MAX);
  51166. + SENDERR(EINVAL);
  51167. + }
  51168. + pfkey_alg++;
  51169. + }
  51170. +
  51171. + errlab:
  51172. + return error;
  51173. +}
  51174. +
  51175. +DEBUG_NO_STATIC int
  51176. +pfkey_spirange_parse(struct sadb_ext *pfkey_ext)
  51177. +{
  51178. + int error = 0;
  51179. + struct sadb_spirange *pfkey_spirange = (struct sadb_spirange *)pfkey_ext;
  51180. +
  51181. + /* sanity checks... */
  51182. + if(pfkey_spirange->sadb_spirange_len !=
  51183. + sizeof(struct sadb_spirange) / IPSEC_PFKEYv2_ALIGN) {
  51184. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51185. + "pfkey_spirange_parse: "
  51186. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  51187. + pfkey_spirange->sadb_spirange_len,
  51188. + (int)sizeof(struct sadb_spirange));
  51189. + SENDERR(EINVAL);
  51190. + }
  51191. +
  51192. + if(pfkey_spirange->sadb_spirange_reserved) {
  51193. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51194. + "pfkey_spirange_parse: "
  51195. + "reserved=%d must be set to zero.\n",
  51196. + pfkey_spirange->sadb_spirange_reserved);
  51197. + SENDERR(EINVAL);
  51198. + }
  51199. +
  51200. + if(ntohl(pfkey_spirange->sadb_spirange_max) < ntohl(pfkey_spirange->sadb_spirange_min)) {
  51201. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51202. + "pfkey_spirange_parse: "
  51203. + "minspi=%08x must be < maxspi=%08x.\n",
  51204. + ntohl(pfkey_spirange->sadb_spirange_min),
  51205. + ntohl(pfkey_spirange->sadb_spirange_max));
  51206. + SENDERR(EINVAL);
  51207. + }
  51208. +
  51209. + if(ntohl(pfkey_spirange->sadb_spirange_min) <= 255) {
  51210. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51211. + "pfkey_spirange_parse: "
  51212. + "minspi=%08x must be > 255.\n",
  51213. + ntohl(pfkey_spirange->sadb_spirange_min));
  51214. + SENDERR(EEXIST);
  51215. + }
  51216. +
  51217. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  51218. + "pfkey_spirange_parse: "
  51219. + "ext_len=%u ext_type=%u(%s) min=%u max=%u res=%u.\n",
  51220. + pfkey_spirange->sadb_spirange_len,
  51221. + pfkey_spirange->sadb_spirange_exttype,
  51222. + pfkey_v2_sadb_ext_string(pfkey_spirange->sadb_spirange_exttype),
  51223. + pfkey_spirange->sadb_spirange_min,
  51224. + pfkey_spirange->sadb_spirange_max,
  51225. + pfkey_spirange->sadb_spirange_reserved);
  51226. + errlab:
  51227. + return error;
  51228. +}
  51229. +
  51230. +DEBUG_NO_STATIC int
  51231. +pfkey_x_kmprivate_parse(struct sadb_ext *pfkey_ext)
  51232. +{
  51233. + int error = 0;
  51234. + struct sadb_x_kmprivate *pfkey_x_kmprivate = (struct sadb_x_kmprivate *)pfkey_ext;
  51235. +
  51236. + /* sanity checks... */
  51237. + if(pfkey_x_kmprivate->sadb_x_kmprivate_len <
  51238. + sizeof(struct sadb_x_kmprivate) / IPSEC_PFKEYv2_ALIGN) {
  51239. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51240. + "pfkey_x_kmprivate_parse: "
  51241. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  51242. + pfkey_x_kmprivate->sadb_x_kmprivate_len,
  51243. + (int)sizeof(struct sadb_x_kmprivate));
  51244. + SENDERR(EINVAL);
  51245. + }
  51246. +
  51247. + if(pfkey_x_kmprivate->sadb_x_kmprivate_reserved) {
  51248. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51249. + "pfkey_x_kmprivate_parse: "
  51250. + "reserved=%d must be set to zero.\n",
  51251. + pfkey_x_kmprivate->sadb_x_kmprivate_reserved);
  51252. + SENDERR(EINVAL);
  51253. + }
  51254. +
  51255. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51256. + "pfkey_x_kmprivate_parse: "
  51257. + "Sorry, I can't parse exttype=%d yet.\n",
  51258. + pfkey_ext->sadb_ext_type);
  51259. + SENDERR(EINVAL); /* don't process these yet */
  51260. +
  51261. +errlab:
  51262. + return error;
  51263. +}
  51264. +
  51265. +DEBUG_NO_STATIC int
  51266. +pfkey_x_satype_parse(struct sadb_ext *pfkey_ext)
  51267. +{
  51268. + int error = 0;
  51269. + int i;
  51270. + struct sadb_x_satype *pfkey_x_satype = (struct sadb_x_satype *)pfkey_ext;
  51271. +
  51272. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  51273. + "pfkey_x_satype_parse: enter\n");
  51274. + /* sanity checks... */
  51275. + if(pfkey_x_satype->sadb_x_satype_len !=
  51276. + sizeof(struct sadb_x_satype) / IPSEC_PFKEYv2_ALIGN) {
  51277. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51278. + "pfkey_x_satype_parse: "
  51279. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  51280. + pfkey_x_satype->sadb_x_satype_len,
  51281. + (int)sizeof(struct sadb_x_satype));
  51282. + SENDERR(EINVAL);
  51283. + }
  51284. +
  51285. + if(!pfkey_x_satype->sadb_x_satype_satype) {
  51286. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51287. + "pfkey_x_satype_parse: "
  51288. + "satype is zero, must be non-zero.\n");
  51289. + SENDERR(EINVAL);
  51290. + }
  51291. +
  51292. + if(pfkey_x_satype->sadb_x_satype_satype > SADB_SATYPE_MAX) {
  51293. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51294. + "pfkey_x_satype_parse: "
  51295. + "satype %d > max %d, invalid.\n",
  51296. + pfkey_x_satype->sadb_x_satype_satype, SADB_SATYPE_MAX);
  51297. + SENDERR(EINVAL);
  51298. + }
  51299. +
  51300. + if(!(satype2proto(pfkey_x_satype->sadb_x_satype_satype))) {
  51301. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51302. + "pfkey_x_satype_parse: "
  51303. + "proto lookup from satype=%d failed.\n",
  51304. + pfkey_x_satype->sadb_x_satype_satype);
  51305. + SENDERR(EINVAL);
  51306. + }
  51307. +
  51308. + for(i = 0; i < 3; i++) {
  51309. + if(pfkey_x_satype->sadb_x_satype_reserved[i]) {
  51310. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51311. + "pfkey_x_satype_parse: "
  51312. + "reserved[%d]=%d must be set to zero.\n",
  51313. + i, pfkey_x_satype->sadb_x_satype_reserved[i]);
  51314. + SENDERR(EINVAL);
  51315. + }
  51316. + }
  51317. +
  51318. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  51319. + "pfkey_x_satype_parse: "
  51320. + "len=%u ext=%u(%s) satype=%u(%s) res=%u,%u,%u.\n",
  51321. + pfkey_x_satype->sadb_x_satype_len,
  51322. + pfkey_x_satype->sadb_x_satype_exttype,
  51323. + pfkey_v2_sadb_ext_string(pfkey_x_satype->sadb_x_satype_exttype),
  51324. + pfkey_x_satype->sadb_x_satype_satype,
  51325. + satype2name(pfkey_x_satype->sadb_x_satype_satype),
  51326. + pfkey_x_satype->sadb_x_satype_reserved[0],
  51327. + pfkey_x_satype->sadb_x_satype_reserved[1],
  51328. + pfkey_x_satype->sadb_x_satype_reserved[2]);
  51329. +errlab:
  51330. + return error;
  51331. +}
  51332. +
  51333. +DEBUG_NO_STATIC int
  51334. +pfkey_x_ext_debug_parse(struct sadb_ext *pfkey_ext)
  51335. +{
  51336. + int error = 0;
  51337. + int i;
  51338. + struct sadb_x_debug *pfkey_x_debug = (struct sadb_x_debug *)pfkey_ext;
  51339. +
  51340. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  51341. + "pfkey_x_debug_parse: enter\n");
  51342. + /* sanity checks... */
  51343. + if(pfkey_x_debug->sadb_x_debug_len !=
  51344. + sizeof(struct sadb_x_debug) / IPSEC_PFKEYv2_ALIGN) {
  51345. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51346. + "pfkey_x_debug_parse: "
  51347. + "size wrong ext_len=%d, key_ext_len=%d.\n",
  51348. + pfkey_x_debug->sadb_x_debug_len,
  51349. + (int)sizeof(struct sadb_x_debug));
  51350. + SENDERR(EINVAL);
  51351. + }
  51352. +
  51353. + for(i = 0; i < 4; i++) {
  51354. + if(pfkey_x_debug->sadb_x_debug_reserved[i]) {
  51355. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51356. + "pfkey_x_debug_parse: "
  51357. + "reserved[%d]=%d must be set to zero.\n",
  51358. + i, pfkey_x_debug->sadb_x_debug_reserved[i]);
  51359. + SENDERR(EINVAL);
  51360. + }
  51361. + }
  51362. +
  51363. +errlab:
  51364. + return error;
  51365. +}
  51366. +
  51367. +DEBUG_NO_STATIC int
  51368. +pfkey_x_ext_protocol_parse(struct sadb_ext *pfkey_ext)
  51369. +{
  51370. + int error = 0;
  51371. + struct sadb_protocol *p = (struct sadb_protocol *)pfkey_ext;
  51372. +
  51373. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM, "pfkey_x_protocol_parse:\n");
  51374. + /* sanity checks... */
  51375. +
  51376. + if (p->sadb_protocol_len != sizeof(*p)/IPSEC_PFKEYv2_ALIGN) {
  51377. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51378. + "pfkey_x_protocol_parse: size wrong ext_len=%d, key_ext_len=%d.\n",
  51379. + p->sadb_protocol_len, (int)sizeof(*p));
  51380. + SENDERR(EINVAL);
  51381. + }
  51382. +
  51383. + if (p->sadb_protocol_reserved2 != 0) {
  51384. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51385. + "pfkey_protocol_parse: res=%d, must be zero.\n",
  51386. + p->sadb_protocol_reserved2);
  51387. + SENDERR(EINVAL);
  51388. + }
  51389. +
  51390. + errlab:
  51391. + return error;
  51392. +}
  51393. +
  51394. +#ifdef NAT_TRAVERSAL
  51395. +DEBUG_NO_STATIC int
  51396. +pfkey_x_ext_nat_t_type_parse(struct sadb_ext *pfkey_ext)
  51397. +{
  51398. + return 0;
  51399. +}
  51400. +DEBUG_NO_STATIC int
  51401. +pfkey_x_ext_nat_t_port_parse(struct sadb_ext *pfkey_ext)
  51402. +{
  51403. + return 0;
  51404. +}
  51405. +#endif
  51406. +
  51407. +#define DEFINEPARSER(NAME) static struct pf_key_ext_parsers_def NAME##_def={NAME, #NAME};
  51408. +
  51409. +DEFINEPARSER(pfkey_sa_parse);
  51410. +DEFINEPARSER(pfkey_lifetime_parse);
  51411. +DEFINEPARSER(pfkey_address_parse);
  51412. +DEFINEPARSER(pfkey_key_parse);
  51413. +DEFINEPARSER(pfkey_ident_parse);
  51414. +DEFINEPARSER(pfkey_sens_parse);
  51415. +DEFINEPARSER(pfkey_prop_parse);
  51416. +DEFINEPARSER(pfkey_supported_parse);
  51417. +DEFINEPARSER(pfkey_spirange_parse);
  51418. +DEFINEPARSER(pfkey_x_kmprivate_parse);
  51419. +DEFINEPARSER(pfkey_x_satype_parse);
  51420. +DEFINEPARSER(pfkey_x_ext_debug_parse);
  51421. +DEFINEPARSER(pfkey_x_ext_protocol_parse);
  51422. +#ifdef NAT_TRAVERSAL
  51423. +DEFINEPARSER(pfkey_x_ext_nat_t_type_parse);
  51424. +DEFINEPARSER(pfkey_x_ext_nat_t_port_parse);
  51425. +#endif
  51426. +
  51427. +struct pf_key_ext_parsers_def *ext_default_parsers[]=
  51428. +{
  51429. + NULL, /* pfkey_msg_parse, */
  51430. + &pfkey_sa_parse_def,
  51431. + &pfkey_lifetime_parse_def,
  51432. + &pfkey_lifetime_parse_def,
  51433. + &pfkey_lifetime_parse_def,
  51434. + &pfkey_address_parse_def,
  51435. + &pfkey_address_parse_def,
  51436. + &pfkey_address_parse_def,
  51437. + &pfkey_key_parse_def,
  51438. + &pfkey_key_parse_def,
  51439. + &pfkey_ident_parse_def,
  51440. + &pfkey_ident_parse_def,
  51441. + &pfkey_sens_parse_def,
  51442. + &pfkey_prop_parse_def,
  51443. + &pfkey_supported_parse_def,
  51444. + &pfkey_supported_parse_def,
  51445. + &pfkey_spirange_parse_def,
  51446. + &pfkey_x_kmprivate_parse_def,
  51447. + &pfkey_x_satype_parse_def,
  51448. + &pfkey_sa_parse_def,
  51449. + &pfkey_address_parse_def,
  51450. + &pfkey_address_parse_def,
  51451. + &pfkey_address_parse_def,
  51452. + &pfkey_address_parse_def,
  51453. + &pfkey_address_parse_def,
  51454. + &pfkey_x_ext_debug_parse_def,
  51455. + &pfkey_x_ext_protocol_parse_def
  51456. +#ifdef NAT_TRAVERSAL
  51457. + ,
  51458. + &pfkey_x_ext_nat_t_type_parse_def,
  51459. + &pfkey_x_ext_nat_t_port_parse_def,
  51460. + &pfkey_x_ext_nat_t_port_parse_def,
  51461. + &pfkey_address_parse_def
  51462. +#endif
  51463. +};
  51464. +
  51465. +int
  51466. +pfkey_msg_parse(struct sadb_msg *pfkey_msg,
  51467. + struct pf_key_ext_parsers_def *ext_parsers[],
  51468. + struct sadb_ext *extensions[],
  51469. + int dir)
  51470. +{
  51471. + int error = 0;
  51472. + int remain;
  51473. + struct sadb_ext *pfkey_ext;
  51474. + int extensions_seen = 0;
  51475. +
  51476. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  51477. + "pfkey_msg_parse: "
  51478. + "parsing message ver=%d, type=%d(%s), errno=%d, satype=%d(%s), len=%d, res=%d, seq=%d, pid=%d.\n",
  51479. + pfkey_msg->sadb_msg_version,
  51480. + pfkey_msg->sadb_msg_type,
  51481. + pfkey_v2_sadb_type_string(pfkey_msg->sadb_msg_type),
  51482. + pfkey_msg->sadb_msg_errno,
  51483. + pfkey_msg->sadb_msg_satype,
  51484. + satype2name(pfkey_msg->sadb_msg_satype),
  51485. + pfkey_msg->sadb_msg_len,
  51486. + pfkey_msg->sadb_msg_reserved,
  51487. + pfkey_msg->sadb_msg_seq,
  51488. + pfkey_msg->sadb_msg_pid);
  51489. +
  51490. + if(ext_parsers == NULL) ext_parsers = ext_default_parsers;
  51491. +
  51492. + pfkey_extensions_init(extensions);
  51493. +
  51494. + remain = pfkey_msg->sadb_msg_len;
  51495. + remain -= sizeof(struct sadb_msg) / IPSEC_PFKEYv2_ALIGN;
  51496. +
  51497. + pfkey_ext = (struct sadb_ext*)((char*)pfkey_msg +
  51498. + sizeof(struct sadb_msg));
  51499. +
  51500. + extensions[0] = (struct sadb_ext *) pfkey_msg;
  51501. +
  51502. +
  51503. + if(pfkey_msg->sadb_msg_version != PF_KEY_V2) {
  51504. + ERROR("pfkey_msg_parse: "
  51505. + "not PF_KEY_V2 msg, found %d, should be %d.\n",
  51506. + pfkey_msg->sadb_msg_version,
  51507. + PF_KEY_V2);
  51508. + SENDERR(EINVAL);
  51509. + }
  51510. +
  51511. + if(!pfkey_msg->sadb_msg_type) {
  51512. + ERROR("pfkey_msg_parse: "
  51513. + "msg type not set, must be non-zero..\n");
  51514. + SENDERR(EINVAL);
  51515. + }
  51516. +
  51517. + if(pfkey_msg->sadb_msg_type > SADB_MAX) {
  51518. + ERROR("pfkey_msg_parse: "
  51519. + "msg type=%d > max=%d.\n",
  51520. + pfkey_msg->sadb_msg_type,
  51521. + SADB_MAX);
  51522. + SENDERR(EINVAL);
  51523. + }
  51524. +
  51525. + switch(pfkey_msg->sadb_msg_type) {
  51526. + case SADB_GETSPI:
  51527. + case SADB_UPDATE:
  51528. + case SADB_ADD:
  51529. + case SADB_DELETE:
  51530. + case SADB_GET:
  51531. + case SADB_X_GRPSA:
  51532. + case SADB_X_ADDFLOW:
  51533. + if(!satype2proto(pfkey_msg->sadb_msg_satype)) {
  51534. + ERROR("pfkey_msg_parse: "
  51535. + "satype %d conversion to proto failed for msg_type %d (%s).\n",
  51536. + pfkey_msg->sadb_msg_satype,
  51537. + pfkey_msg->sadb_msg_type,
  51538. + pfkey_v2_sadb_type_string(pfkey_msg->sadb_msg_type));
  51539. + SENDERR(EINVAL);
  51540. + } else {
  51541. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51542. + "pfkey_msg_parse: "
  51543. + "satype %d(%s) conversion to proto gives %d for msg_type %d(%s).\n",
  51544. + pfkey_msg->sadb_msg_satype,
  51545. + satype2name(pfkey_msg->sadb_msg_satype),
  51546. + satype2proto(pfkey_msg->sadb_msg_satype),
  51547. + pfkey_msg->sadb_msg_type,
  51548. + pfkey_v2_sadb_type_string(pfkey_msg->sadb_msg_type));
  51549. + }
  51550. + case SADB_ACQUIRE:
  51551. + case SADB_REGISTER:
  51552. + case SADB_EXPIRE:
  51553. + if(!pfkey_msg->sadb_msg_satype) {
  51554. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51555. + "pfkey_msg_parse: "
  51556. + "satype is zero, must be non-zero for msg_type %d(%s).\n",
  51557. + pfkey_msg->sadb_msg_type,
  51558. + pfkey_v2_sadb_type_string(pfkey_msg->sadb_msg_type));
  51559. + SENDERR(EINVAL);
  51560. + }
  51561. + default:
  51562. + break;
  51563. + }
  51564. +
  51565. + /* errno must not be set in downward messages */
  51566. + /* this is not entirely true... a response to an ACQUIRE could return an error */
  51567. + if((dir == EXT_BITS_IN) && (pfkey_msg->sadb_msg_type != SADB_ACQUIRE) && pfkey_msg->sadb_msg_errno) {
  51568. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51569. + "pfkey_msg_parse: "
  51570. + "errno set to %d.\n",
  51571. + pfkey_msg->sadb_msg_errno);
  51572. + SENDERR(EINVAL);
  51573. + }
  51574. +
  51575. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  51576. + "pfkey_msg_parse: "
  51577. + "remain=%d\n",
  51578. + remain
  51579. + );
  51580. +
  51581. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  51582. + "pfkey_msg_parse: "
  51583. + "extensions permitted=%08x, required=%08x.\n",
  51584. + extensions_bitmaps[dir][EXT_BITS_PERM][pfkey_msg->sadb_msg_type],
  51585. + extensions_bitmaps[dir][EXT_BITS_REQ][pfkey_msg->sadb_msg_type]);
  51586. +
  51587. + extensions_seen = 1;
  51588. +
  51589. + while( (remain * IPSEC_PFKEYv2_ALIGN) >= sizeof(struct sadb_ext) ) {
  51590. + /* Is there enough message left to support another extension header? */
  51591. + if(remain < pfkey_ext->sadb_ext_len) {
  51592. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51593. + "pfkey_msg_parse: "
  51594. + "remain %d less than ext len %d.\n",
  51595. + remain, pfkey_ext->sadb_ext_len);
  51596. + SENDERR(EINVAL);
  51597. + }
  51598. +
  51599. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  51600. + "pfkey_msg_parse: "
  51601. + "parsing ext type=%d(%s) remain=%d.\n",
  51602. + pfkey_ext->sadb_ext_type,
  51603. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type),
  51604. + remain);
  51605. +
  51606. + /* Is the extension header type valid? */
  51607. + if((pfkey_ext->sadb_ext_type > SADB_EXT_MAX) || (!pfkey_ext->sadb_ext_type)) {
  51608. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51609. + "pfkey_msg_parse: "
  51610. + "ext type %d(%s) invalid, SADB_EXT_MAX=%d.\n",
  51611. + pfkey_ext->sadb_ext_type,
  51612. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type),
  51613. + SADB_EXT_MAX);
  51614. + SENDERR(EINVAL);
  51615. + }
  51616. +
  51617. + /* Have we already seen this type of extension? */
  51618. + if((extensions_seen & ( 1 << pfkey_ext->sadb_ext_type )) != 0)
  51619. + {
  51620. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51621. + "pfkey_msg_parse: "
  51622. + "ext type %d(%s) already seen.\n",
  51623. + pfkey_ext->sadb_ext_type,
  51624. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type));
  51625. + SENDERR(EINVAL);
  51626. + }
  51627. +
  51628. + /* Do I even know about this type of extension? */
  51629. + if(ext_parsers[pfkey_ext->sadb_ext_type]==NULL) {
  51630. + ERROR("pfkey_msg_parse: "
  51631. + "ext type %d(%s) unknown, ignoring.\n",
  51632. + pfkey_ext->sadb_ext_type,
  51633. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type));
  51634. + goto next_ext;
  51635. + }
  51636. +
  51637. + /* Is this type of extension permitted for this type of message? */
  51638. + if(!(extensions_bitmaps[dir][EXT_BITS_PERM][pfkey_msg->sadb_msg_type] &
  51639. + 1<<pfkey_ext->sadb_ext_type)) {
  51640. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51641. + "pfkey_msg_parse: "
  51642. + "ext type %d(%s) not permitted, exts_perm_in=%08x, 1<<type=%08x\n",
  51643. + pfkey_ext->sadb_ext_type,
  51644. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type),
  51645. + extensions_bitmaps[dir][EXT_BITS_PERM][pfkey_msg->sadb_msg_type],
  51646. + 1<<pfkey_ext->sadb_ext_type);
  51647. + SENDERR(EINVAL);
  51648. + }
  51649. +
  51650. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  51651. + "pfkey_msg_parse: "
  51652. + "remain=%d ext_type=%d(%s) ext_len=%d parsing ext 0p%p with parser %s.\n",
  51653. + remain,
  51654. + pfkey_ext->sadb_ext_type,
  51655. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type),
  51656. + pfkey_ext->sadb_ext_len,
  51657. + pfkey_ext,
  51658. + ext_parsers[pfkey_ext->sadb_ext_type]->parser_name);
  51659. +
  51660. + /* Parse the extension */
  51661. + if((error =
  51662. + (*ext_parsers[pfkey_ext->sadb_ext_type]->parser)(pfkey_ext))) {
  51663. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51664. + "pfkey_msg_parse: "
  51665. + "extension parsing for type %d(%s) failed with error %d.\n",
  51666. + pfkey_ext->sadb_ext_type,
  51667. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type),
  51668. + error);
  51669. + SENDERR(-error);
  51670. + }
  51671. + DEBUGGING(PF_KEY_DEBUG_PARSE_FLOW,
  51672. + "pfkey_msg_parse: "
  51673. + "Extension %d(%s) parsed.\n",
  51674. + pfkey_ext->sadb_ext_type,
  51675. + pfkey_v2_sadb_ext_string(pfkey_ext->sadb_ext_type));
  51676. +
  51677. + /* Mark that we have seen this extension and remember the header location */
  51678. + extensions_seen |= ( 1 << pfkey_ext->sadb_ext_type );
  51679. + extensions[pfkey_ext->sadb_ext_type] = pfkey_ext;
  51680. +
  51681. + next_ext:
  51682. + /* Calculate how much message remains */
  51683. + remain -= pfkey_ext->sadb_ext_len;
  51684. +
  51685. + if(!remain) {
  51686. + break;
  51687. + }
  51688. + /* Find the next extension header */
  51689. + pfkey_ext = (struct sadb_ext*)((char*)pfkey_ext +
  51690. + pfkey_ext->sadb_ext_len * IPSEC_PFKEYv2_ALIGN);
  51691. + }
  51692. +
  51693. + if(remain) {
  51694. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51695. + "pfkey_msg_parse: "
  51696. + "unexpected remainder of %d.\n",
  51697. + remain);
  51698. + /* why is there still something remaining? */
  51699. + SENDERR(EINVAL);
  51700. + }
  51701. +
  51702. + /* check required extensions */
  51703. + DEBUGGING(PF_KEY_DEBUG_PARSE_STRUCT,
  51704. + "pfkey_msg_parse: "
  51705. + "extensions permitted=%08x, seen=%08x, required=%08x.\n",
  51706. + extensions_bitmaps[dir][EXT_BITS_PERM][pfkey_msg->sadb_msg_type],
  51707. + extensions_seen,
  51708. + extensions_bitmaps[dir][EXT_BITS_REQ][pfkey_msg->sadb_msg_type]);
  51709. +
  51710. + /* don't check further if it is an error return message since it
  51711. + may not have a body */
  51712. + if(pfkey_msg->sadb_msg_errno) {
  51713. + SENDERR(-error);
  51714. + }
  51715. +
  51716. + if((extensions_seen &
  51717. + extensions_bitmaps[dir][EXT_BITS_REQ][pfkey_msg->sadb_msg_type]) !=
  51718. + extensions_bitmaps[dir][EXT_BITS_REQ][pfkey_msg->sadb_msg_type]) {
  51719. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51720. + "pfkey_msg_parse: "
  51721. + "required extensions missing:%08x.\n",
  51722. + extensions_bitmaps[dir][EXT_BITS_REQ][pfkey_msg->sadb_msg_type] -
  51723. + (extensions_seen &
  51724. + extensions_bitmaps[dir][EXT_BITS_REQ][pfkey_msg->sadb_msg_type]));
  51725. + SENDERR(EINVAL);
  51726. + }
  51727. +
  51728. + if((dir == EXT_BITS_IN) && (pfkey_msg->sadb_msg_type == SADB_X_DELFLOW)
  51729. + && ((extensions_seen & SADB_X_EXT_ADDRESS_DELFLOW)
  51730. + != SADB_X_EXT_ADDRESS_DELFLOW)
  51731. + && (((extensions_seen & (1<<SADB_EXT_SA)) != (1<<SADB_EXT_SA))
  51732. + || ((((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_flags
  51733. + & SADB_X_SAFLAGS_CLEARFLOW)
  51734. + != SADB_X_SAFLAGS_CLEARFLOW))) {
  51735. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51736. + "pfkey_msg_parse: "
  51737. + "required SADB_X_DELFLOW extensions missing: either %08x must be present or %08x must be present with SADB_X_SAFLAGS_CLEARFLOW set.\n",
  51738. + SADB_X_EXT_ADDRESS_DELFLOW
  51739. + - (extensions_seen & SADB_X_EXT_ADDRESS_DELFLOW),
  51740. + (1<<SADB_EXT_SA) - (extensions_seen & (1<<SADB_EXT_SA)));
  51741. + SENDERR(EINVAL);
  51742. + }
  51743. +
  51744. + switch(pfkey_msg->sadb_msg_type) {
  51745. + case SADB_ADD:
  51746. + case SADB_UPDATE:
  51747. + /* check maturity */
  51748. + if(((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_state !=
  51749. + SADB_SASTATE_MATURE) {
  51750. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51751. + "pfkey_msg_parse: "
  51752. + "state=%d for add or update should be MATURE=%d.\n",
  51753. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_state,
  51754. + SADB_SASTATE_MATURE);
  51755. + SENDERR(EINVAL);
  51756. + }
  51757. +
  51758. + /* check AH and ESP */
  51759. + switch(((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype) {
  51760. + case SADB_SATYPE_AH:
  51761. + if(!(((struct sadb_sa*)extensions[SADB_EXT_SA]) &&
  51762. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_auth !=
  51763. + SADB_AALG_NONE)) {
  51764. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51765. + "pfkey_msg_parse: "
  51766. + "auth alg is zero, must be non-zero for AH SAs.\n");
  51767. + SENDERR(EINVAL);
  51768. + }
  51769. + if(((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_encrypt !=
  51770. + SADB_EALG_NONE) {
  51771. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51772. + "pfkey_msg_parse: "
  51773. + "AH handed encalg=%d, must be zero.\n",
  51774. + ((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_encrypt);
  51775. + SENDERR(EINVAL);
  51776. + }
  51777. + break;
  51778. + case SADB_SATYPE_ESP:
  51779. + if(!(((struct sadb_sa*)extensions[SADB_EXT_SA]) &&
  51780. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_encrypt !=
  51781. + SADB_EALG_NONE)) {
  51782. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51783. + "pfkey_msg_parse: "
  51784. + "encrypt alg=%d is zero, must be non-zero for ESP=%d SAs.\n",
  51785. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_encrypt,
  51786. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype);
  51787. + SENDERR(EINVAL);
  51788. + }
  51789. + if((((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_encrypt ==
  51790. + SADB_EALG_NULL) &&
  51791. + (((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_auth ==
  51792. + SADB_AALG_NONE) ) {
  51793. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51794. + "pfkey_msg_parse: "
  51795. + "ESP handed encNULL+authNONE, illegal combination.\n");
  51796. + SENDERR(EINVAL);
  51797. + }
  51798. + break;
  51799. + case SADB_X_SATYPE_COMP:
  51800. + if(!(((struct sadb_sa*)extensions[SADB_EXT_SA]) &&
  51801. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_encrypt !=
  51802. + SADB_EALG_NONE)) {
  51803. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51804. + "pfkey_msg_parse: "
  51805. + "encrypt alg=%d is zero, must be non-zero for COMP=%d SAs.\n",
  51806. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_encrypt,
  51807. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype);
  51808. + SENDERR(EINVAL);
  51809. + }
  51810. + if(((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_auth !=
  51811. + SADB_AALG_NONE) {
  51812. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51813. + "pfkey_msg_parse: "
  51814. + "COMP handed auth=%d, must be zero.\n",
  51815. + ((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_auth);
  51816. + SENDERR(EINVAL);
  51817. + }
  51818. + break;
  51819. + default:
  51820. + break;
  51821. + }
  51822. + if(ntohl(((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_spi) <= 255) {
  51823. + DEBUGGING(PF_KEY_DEBUG_PARSE_PROBLEM,
  51824. + "pfkey_msg_parse: "
  51825. + "spi=%08x must be > 255.\n",
  51826. + ntohl(((struct sadb_sa*)(extensions[SADB_EXT_SA]))->sadb_sa_spi));
  51827. + SENDERR(EINVAL);
  51828. + }
  51829. + default:
  51830. + break;
  51831. + }
  51832. +errlab:
  51833. +
  51834. + return error;
  51835. +}
  51836. +
  51837. +/*
  51838. + * $Log: pfkey_v2_parse.c,v $
  51839. + * Revision 1.65 2005/04/06 17:46:05 mcr
  51840. + * failure to recognize an extension is considered an error.
  51841. + * This could be a problem in the future, but we need some kind
  51842. + * of logging. This should be rate limited, probably.
  51843. + *
  51844. + * Revision 1.64 2005/01/26 00:50:35 mcr
  51845. + * adjustment of confusion of CONFIG_IPSEC_NAT vs CONFIG_KLIPS_NAT,
  51846. + * and make sure that NAT_TRAVERSAL is set as well to match
  51847. + * userspace compiles of code.
  51848. + *
  51849. + * Revision 1.63 2004/10/28 22:54:10 mcr
  51850. + * results from valgrind, thanks to: Harald Hoyer <harald@redhat.com>
  51851. + *
  51852. + * Revision 1.62 2004/10/03 01:26:36 mcr
  51853. + * fixes for gcc 3.4 compilation.
  51854. + *
  51855. + * Revision 1.61 2004/07/10 19:11:18 mcr
  51856. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  51857. + *
  51858. + * Revision 1.59 2004/04/18 03:03:49 mcr
  51859. + * renamed common include files from pluto directory.
  51860. + *
  51861. + * Revision 1.58 2004/03/08 01:59:08 ken
  51862. + * freeswan.h -> openswan.h
  51863. + *
  51864. + * Revision 1.57 2003/12/10 01:20:19 mcr
  51865. + * NAT-traversal patches to KLIPS.
  51866. + *
  51867. + * Revision 1.56 2003/12/04 23:01:12 mcr
  51868. + * removed ipsec_netlink.h
  51869. + *
  51870. + * Revision 1.55 2003/11/07 01:30:37 ken
  51871. + * Cast sizeof() to int to keep things 64bit clean
  51872. + *
  51873. + * Revision 1.54 2003/10/31 02:27:12 mcr
  51874. + * pulled up port-selector patches and sa_id elimination.
  51875. + *
  51876. + * Revision 1.53.20.2 2003/10/29 01:11:32 mcr
  51877. + * added debugging for pfkey library.
  51878. + *
  51879. + * Revision 1.53.20.1 2003/09/21 13:59:44 mcr
  51880. + * pre-liminary X.509 patch - does not yet pass tests.
  51881. + *
  51882. + * Revision 1.53 2003/01/30 02:32:09 rgb
  51883. + *
  51884. + * Rename SAref table macro names for clarity.
  51885. + * Convert IPsecSAref_t from signed to unsigned to fix apparent SAref exhaustion bug.
  51886. + *
  51887. + * Revision 1.52 2002/12/30 06:53:07 mcr
  51888. + * deal with short SA structures... #if 0 out for now. Probably
  51889. + * not quite the right way.
  51890. + *
  51891. + * Revision 1.51 2002/12/13 18:16:02 mcr
  51892. + * restored sa_ref code
  51893. + *
  51894. + * Revision 1.50 2002/12/13 18:06:52 mcr
  51895. + * temporarily removed sadb_x_sa_ref reference for 2.xx
  51896. + *
  51897. + * Revision 1.49 2002/10/05 05:02:58 dhr
  51898. + *
  51899. + * C labels go on statements
  51900. + *
  51901. + * Revision 1.48 2002/09/20 15:40:45 rgb
  51902. + * Added sadb_x_sa_ref to struct sadb_sa.
  51903. + *
  51904. + * Revision 1.47 2002/09/20 05:01:31 rgb
  51905. + * Fixed usage of pfkey_lib_debug.
  51906. + * Format for function declaration style consistency.
  51907. + * Added text labels to elucidate numeric values presented.
  51908. + * Re-organised debug output to reduce noise in output.
  51909. + *
  51910. + * Revision 1.46 2002/07/24 18:44:54 rgb
  51911. + * Type fiddling to tame ia64 compiler.
  51912. + *
  51913. + * Revision 1.45 2002/05/23 07:14:11 rgb
  51914. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  51915. + *
  51916. + * Revision 1.44 2002/04/24 07:55:32 mcr
  51917. + * #include patches and Makefiles for post-reorg compilation.
  51918. + *
  51919. + * Revision 1.43 2002/04/24 07:36:40 mcr
  51920. + * Moved from ./lib/pfkey_v2_parse.c,v
  51921. + *
  51922. + * Revision 1.42 2002/01/29 22:25:36 rgb
  51923. + * Re-add ipsec_kversion.h to keep MALLOC happy.
  51924. + *
  51925. + * Revision 1.41 2002/01/29 01:59:10 mcr
  51926. + * removal of kversions.h - sources that needed it now use ipsec_param.h.
  51927. + * updating of IPv6 structures to match latest in6.h version.
  51928. + * removed dead code from openswan.h that also duplicated kversions.h
  51929. + * code.
  51930. + *
  51931. + * Revision 1.40 2002/01/20 20:34:50 mcr
  51932. + * added pfkey_v2_sadb_type_string to decode sadb_type to string.
  51933. + *
  51934. + * Revision 1.39 2001/11/27 05:29:22 mcr
  51935. + * pfkey parses are now maintained by a structure
  51936. + * that includes their name for debug purposes.
  51937. + * DEBUGGING() macro changed so that it takes a debug
  51938. + * level so that pf_key() can use this to decode the
  51939. + * structures without innundanting humans.
  51940. + * Also uses pfkey_v2_sadb_ext_string() in messages.
  51941. + *
  51942. + * Revision 1.38 2001/11/06 19:47:47 rgb
  51943. + * Added packet parameter to lifetime and comb structures.
  51944. + *
  51945. + * Revision 1.37 2001/10/18 04:45:24 rgb
  51946. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  51947. + * lib/openswan.h version macros moved to lib/kversions.h.
  51948. + * Other compiler directive cleanups.
  51949. + *
  51950. + * Revision 1.36 2001/06/14 19:35:16 rgb
  51951. + * Update copyright date.
  51952. + *
  51953. + * Revision 1.35 2001/05/03 19:44:51 rgb
  51954. + * Standardise on SENDERR() macro.
  51955. + *
  51956. + * Revision 1.34 2001/03/16 07:41:51 rgb
  51957. + * Put openswan.h include before pluto includes.
  51958. + *
  51959. + * Revision 1.33 2001/02/27 07:13:51 rgb
  51960. + * Added satype2name() function.
  51961. + * Added text to default satype_tbl entry.
  51962. + * Added satype2name() conversions for most satype debug output.
  51963. + *
  51964. + * Revision 1.32 2001/02/26 20:01:09 rgb
  51965. + * Added internal IP protocol 61 for magic SAs.
  51966. + * Ditch unused sadb_satype2proto[], replaced by satype2proto().
  51967. + * Re-formatted debug output (split lines, consistent spacing).
  51968. + * Removed acquire, register and expire requirements for a known satype.
  51969. + * Changed message type checking to a switch structure.
  51970. + * Verify expected NULL auth for IPCOMP.
  51971. + * Enforced spi > 0x100 requirement, now that pass uses a magic SA for
  51972. + * appropriate message types.
  51973. + *
  51974. + * Revision 1.31 2000/12/01 07:09:00 rgb
  51975. + * Added ipcomp sanity check to require encalgo is set.
  51976. + *
  51977. + * Revision 1.30 2000/11/17 18:10:30 rgb
  51978. + * Fixed bugs mostly relating to spirange, to treat all spi variables as
  51979. + * network byte order since this is the way PF_KEYv2 stored spis.
  51980. + *
  51981. + * Revision 1.29 2000/10/12 00:02:39 rgb
  51982. + * Removed 'format, ##' nonsense from debug macros for RH7.0.
  51983. + *
  51984. + * Revision 1.28 2000/09/20 16:23:04 rgb
  51985. + * Remove over-paranoid extension check in the presence of sadb_msg_errno.
  51986. + *
  51987. + * Revision 1.27 2000/09/20 04:04:21 rgb
  51988. + * Changed static functions to DEBUG_NO_STATIC to reveal function names in
  51989. + * oopsen.
  51990. + *
  51991. + * Revision 1.26 2000/09/15 11:37:02 rgb
  51992. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  51993. + * IPCOMP zlib deflate code.
  51994. + *
  51995. + * Revision 1.25 2000/09/12 22:35:37 rgb
  51996. + * Restructured to remove unused extensions from CLEARFLOW messages.
  51997. + *
  51998. + * Revision 1.24 2000/09/12 18:59:54 rgb
  51999. + * Added Gerhard's IPv6 support to pfkey parts of libopenswan.
  52000. + *
  52001. + * Revision 1.23 2000/09/12 03:27:00 rgb
  52002. + * Moved DEBUGGING definition to compile kernel with debug off.
  52003. + *
  52004. + * Revision 1.22 2000/09/09 06:39:27 rgb
  52005. + * Restrict pfkey errno check to downward messages only.
  52006. + *
  52007. + * Revision 1.21 2000/09/08 19:22:34 rgb
  52008. + * Enabled pfkey_sens_parse().
  52009. + * Added check for errno on downward acquire messages only.
  52010. + *
  52011. + * Revision 1.20 2000/09/01 18:48:23 rgb
  52012. + * Fixed reserved check bug and added debug output in
  52013. + * pfkey_supported_parse().
  52014. + * Fixed debug output label bug in pfkey_ident_parse().
  52015. + *
  52016. + * Revision 1.19 2000/08/27 01:55:26 rgb
  52017. + * Define OCTETBITS and PFKEYBITS to avoid using 'magic' numbers in code.
  52018. + *
  52019. + * Revision 1.18 2000/08/24 17:00:36 rgb
  52020. + * Ignore unknown extensions instead of failing.
  52021. + *
  52022. + * Revision 1.17 2000/06/02 22:54:14 rgb
  52023. + * Added Gerhard Gessler's struct sockaddr_storage mods for IPv6 support.
  52024. + *
  52025. + * Revision 1.16 2000/05/10 19:25:11 rgb
  52026. + * Fleshed out proposal and supported extensions.
  52027. + *
  52028. + * Revision 1.15 2000/01/24 21:15:31 rgb
  52029. + * Added disabled pluto pfkey lib debug flag.
  52030. + * Added algo debugging reporting.
  52031. + *
  52032. + * Revision 1.14 2000/01/22 23:24:29 rgb
  52033. + * Added new functions proto2satype() and satype2proto() and lookup
  52034. + * table satype_tbl. Also added proto2name() since it was easy.
  52035. + *
  52036. + * Revision 1.13 2000/01/21 09:43:59 rgb
  52037. + * Cast ntohl(spi) as (unsigned long int) to shut up compiler.
  52038. + *
  52039. + * Revision 1.12 2000/01/21 06:28:19 rgb
  52040. + * Added address cases for eroute flows.
  52041. + * Indented compiler directives for readability.
  52042. + * Added klipsdebug switching capability.
  52043. + *
  52044. + * Revision 1.11 1999/12/29 21:14:59 rgb
  52045. + * Fixed debug text cut and paste typo.
  52046. + *
  52047. + * Revision 1.10 1999/12/10 17:45:24 rgb
  52048. + * Added address debugging.
  52049. + *
  52050. + * Revision 1.9 1999/12/09 23:11:42 rgb
  52051. + * Ditched <string.h> include since we no longer use memset().
  52052. + * Use new pfkey_extensions_init() instead of memset().
  52053. + * Added check for SATYPE in pfkey_msg_build().
  52054. + * Tidy up comments and debugging comments.
  52055. + *
  52056. + * Revision 1.8 1999/12/07 19:55:26 rgb
  52057. + * Removed unused first argument from extension parsers.
  52058. + * Removed static pluto debug flag.
  52059. + * Moved message type and state checking to pfkey_msg_parse().
  52060. + * Changed print[fk] type from lx to x to quiet compiler.
  52061. + * Removed redundant remain check.
  52062. + * Changed __u* types to uint* to avoid use of asm/types.h and
  52063. + * sys/types.h in userspace code.
  52064. + *
  52065. + * Revision 1.7 1999/12/01 22:20:51 rgb
  52066. + * Moved pfkey_lib_debug variable into the library.
  52067. + * Added pfkey version check into header parsing.
  52068. + * Added check for SATYPE only for those extensions that require a
  52069. + * non-zero value.
  52070. + *
  52071. + * Revision 1.6 1999/11/27 11:58:05 rgb
  52072. + * Added ipv6 headers.
  52073. + * Moved sadb_satype2proto protocol lookup table from
  52074. + * klips/net/ipsec/pfkey_v2_parser.c.
  52075. + * Enable lifetime_current checking.
  52076. + * Debugging error messages added.
  52077. + * Add argument to pfkey_msg_parse() for direction.
  52078. + * Consolidated the 4 1-d extension bitmap arrays into one 4-d array.
  52079. + * Add CVS log entry to bottom of file.
  52080. + * Moved auth and enc alg check to pfkey_msg_parse().
  52081. + * Enable accidentally disabled spirange parsing.
  52082. + * Moved protocol/algorithm checks from klips/net/ipsec/pfkey_v2_parser.c
  52083. + *
  52084. + * Local variables:
  52085. + * c-file-style: "linux"
  52086. + * End:
  52087. + *
  52088. + */
  52089. --- /dev/null Tue Mar 11 13:02:56 2003
  52090. +++ linux/net/ipsec/pfkey_v2_parser.c Mon Feb 9 13:51:03 2004
  52091. @@ -0,0 +1,3520 @@
  52092. +/*
  52093. + * @(#) RFC2367 PF_KEYv2 Key management API message parser
  52094. + * Copyright (C) 1999, 2000, 2001 Richard Guy Briggs <rgb@freeswan.org>
  52095. + *
  52096. + * This program is free software; you can redistribute it and/or modify it
  52097. + * under the terms of the GNU General Public License as published by the
  52098. + * Free Software Foundation; either version 2 of the License, or (at your
  52099. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  52100. + *
  52101. + * This program is distributed in the hope that it will be useful, but
  52102. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  52103. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  52104. + * for more details.
  52105. + *
  52106. + * RCSID $Id: pfkey_v2_parser.c,v 1.134.2.2 2006/10/06 21:39:26 paul Exp $
  52107. + */
  52108. +
  52109. +/*
  52110. + * Template from klips/net/ipsec/ipsec/ipsec_netlink.c.
  52111. + */
  52112. +
  52113. +char pfkey_v2_parser_c_version[] = "$Id: pfkey_v2_parser.c,v 1.134.2.2 2006/10/06 21:39:26 paul Exp $";
  52114. +
  52115. +#ifndef AUTOCONF_INCLUDED
  52116. +#include <linux/config.h>
  52117. +#endif
  52118. +#include <linux/version.h>
  52119. +#include <linux/kernel.h> /* printk() */
  52120. +
  52121. +#include "openswan/ipsec_param.h"
  52122. +
  52123. +#ifdef MALLOC_SLAB
  52124. +# include <linux/slab.h> /* kmalloc() */
  52125. +#else /* MALLOC_SLAB */
  52126. +# include <linux/malloc.h> /* kmalloc() */
  52127. +#endif /* MALLOC_SLAB */
  52128. +#include <linux/errno.h> /* error codes */
  52129. +#include <linux/types.h> /* size_t */
  52130. +#include <linux/interrupt.h> /* mark_bh */
  52131. +
  52132. +#include <linux/netdevice.h> /* struct device, and other headers */
  52133. +#include <linux/etherdevice.h> /* eth_type_trans */
  52134. +#include <linux/ip.h> /* struct iphdr */
  52135. +#include <linux/skbuff.h>
  52136. +
  52137. +#include <openswan.h>
  52138. +
  52139. +#include <crypto/des.h>
  52140. +
  52141. +#ifdef SPINLOCK
  52142. +# ifdef SPINLOCK_23
  52143. +# include <linux/spinlock.h> /* *lock* */
  52144. +# else /* SPINLOCK_23 */
  52145. +# include <asm/spinlock.h> /* *lock* */
  52146. +# endif /* SPINLOCK_23 */
  52147. +#endif /* SPINLOCK */
  52148. +
  52149. +#include <linux/in6.h>
  52150. +#include <net/route.h>
  52151. +
  52152. +#include <net/ip.h>
  52153. +#ifdef NETLINK_SOCK
  52154. +# include <linux/netlink.h>
  52155. +#else
  52156. +# include <net/netlink.h>
  52157. +#endif
  52158. +
  52159. +#include <linux/random.h> /* get_random_bytes() */
  52160. +
  52161. +#include "openswan/radij.h"
  52162. +#include "openswan/ipsec_encap.h"
  52163. +#include "openswan/ipsec_sa.h"
  52164. +
  52165. +#include "openswan/ipsec_radij.h"
  52166. +#include "openswan/ipsec_xform.h"
  52167. +#include "openswan/ipsec_ah.h"
  52168. +#include "openswan/ipsec_esp.h"
  52169. +#include "openswan/ipsec_tunnel.h"
  52170. +#include "openswan/ipsec_rcv.h"
  52171. +#include "openswan/ipcomp.h"
  52172. +
  52173. +#include <pfkeyv2.h>
  52174. +#include <pfkey.h>
  52175. +
  52176. +#include "openswan/ipsec_proto.h"
  52177. +#include "openswan/ipsec_alg.h"
  52178. +
  52179. +#include "openswan/ipsec_kern24.h"
  52180. +
  52181. +#define SENDERR(_x) do { error = -(_x); goto errlab; } while (0)
  52182. +
  52183. +struct sklist_t {
  52184. + struct socket *sk;
  52185. + struct sklist_t* next;
  52186. +} pfkey_sklist_head, *pfkey_sklist, *pfkey_sklist_prev;
  52187. +
  52188. +__u32 pfkey_msg_seq = 0;
  52189. +
  52190. +
  52191. +#if 0
  52192. +#define DUMP_SAID dump_said(&extr->ips->ips_said, __LINE__)
  52193. +#define DUMP_SAID2 dump_said(&extr.ips->ips_said, __LINE__)
  52194. +static void dump_said(ip_said *s, int line)
  52195. +{
  52196. + char msa[SATOT_BUF];
  52197. + size_t msa_len;
  52198. +
  52199. + msa_len = satot(s, 0, msa, sizeof(msa));
  52200. +
  52201. + printk("line: %d msa: %s\n", line, msa);
  52202. +}
  52203. +#endif
  52204. +
  52205. +
  52206. +int
  52207. +pfkey_alloc_eroute(struct eroute** eroute)
  52208. +{
  52209. + int error = 0;
  52210. + if(*eroute) {
  52211. + KLIPS_PRINT(debug_pfkey,
  52212. + "klips_debug:pfkey_alloc_eroute: "
  52213. + "eroute struct already allocated\n");
  52214. + SENDERR(EEXIST);
  52215. + }
  52216. +
  52217. + if((*eroute = kmalloc(sizeof(**eroute), GFP_ATOMIC) ) == NULL) {
  52218. + KLIPS_PRINT(debug_pfkey,
  52219. + "klips_debug:pfkey_alloc_eroute: "
  52220. + "memory allocation error\n");
  52221. + SENDERR(ENOMEM);
  52222. + }
  52223. +
  52224. + KLIPS_PRINT(debug_pfkey,
  52225. + "klips_debug:pfkey_alloc_eroute: "
  52226. + "allocating %lu bytes for an eroute at 0p%p\n",
  52227. + (unsigned long) sizeof(**eroute), *eroute);
  52228. +
  52229. + memset((caddr_t)*eroute, 0, sizeof(**eroute));
  52230. + (*eroute)->er_eaddr.sen_len =
  52231. + (*eroute)->er_emask.sen_len = sizeof(struct sockaddr_encap);
  52232. + (*eroute)->er_eaddr.sen_family =
  52233. + (*eroute)->er_emask.sen_family = AF_ENCAP;
  52234. + (*eroute)->er_eaddr.sen_type = SENT_IP4;
  52235. + (*eroute)->er_emask.sen_type = 255;
  52236. + (*eroute)->er_pid = 0;
  52237. + (*eroute)->er_count = 0;
  52238. + (*eroute)->er_lasttime = jiffies/HZ;
  52239. +
  52240. + errlab:
  52241. + return(error);
  52242. +}
  52243. +
  52244. +DEBUG_NO_STATIC int
  52245. +pfkey_x_protocol_process(struct sadb_ext *pfkey_ext,
  52246. + struct pfkey_extracted_data *extr)
  52247. +{
  52248. + int error = 0;
  52249. + struct sadb_protocol * p = (struct sadb_protocol *)pfkey_ext;
  52250. +
  52251. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_protocol_process: %p\n", extr);
  52252. +
  52253. + if (extr == 0) {
  52254. + KLIPS_PRINT(debug_pfkey,
  52255. + "klips_debug:pfkey_x_protocol_process:"
  52256. + "extr is NULL, fatal\n");
  52257. + SENDERR(EINVAL);
  52258. + }
  52259. + if (extr->eroute == 0) {
  52260. + KLIPS_PRINT(debug_pfkey,
  52261. + "klips_debug:pfkey_x_protocol_process:"
  52262. + "extr->eroute is NULL, fatal\n");
  52263. + SENDERR(EINVAL);
  52264. + }
  52265. +
  52266. + extr->eroute->er_eaddr.sen_proto = p->sadb_protocol_proto;
  52267. + extr->eroute->er_emask.sen_proto = p->sadb_protocol_proto ? ~0:0;
  52268. + KLIPS_PRINT(debug_pfkey,
  52269. + "klips_debug:pfkey_x_protocol_process: protocol = %d.\n",
  52270. + p->sadb_protocol_proto);
  52271. + errlab:
  52272. + return error;
  52273. +}
  52274. +
  52275. +DEBUG_NO_STATIC int
  52276. +pfkey_ipsec_sa_init(struct ipsec_sa *ipsp)
  52277. +{
  52278. +
  52279. + return ipsec_sa_init(ipsp);
  52280. +}
  52281. +
  52282. +int
  52283. +pfkey_safe_build(int error, struct sadb_ext *extensions[SADB_MAX+1])
  52284. +{
  52285. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_safe_build: "
  52286. + "error=%d\n",
  52287. + error);
  52288. + if (!error) {
  52289. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_safe_build:"
  52290. + "success.\n");
  52291. + return 1;
  52292. + } else {
  52293. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_safe_build:"
  52294. + "caught error %d\n",
  52295. + error);
  52296. + pfkey_extensions_free(extensions);
  52297. + return 0;
  52298. + }
  52299. +}
  52300. +
  52301. +
  52302. +DEBUG_NO_STATIC int
  52303. +pfkey_getspi_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  52304. +{
  52305. + int error = 0;
  52306. + ipsec_spi_t minspi = htonl(256), maxspi = htonl(-1L);
  52307. + int found_avail = 0;
  52308. + struct ipsec_sa *ipsq;
  52309. + char sa[SATOT_BUF];
  52310. + size_t sa_len;
  52311. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  52312. + struct sadb_msg *pfkey_reply = NULL;
  52313. + struct socket_list *pfkey_socketsp;
  52314. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  52315. +
  52316. + KLIPS_PRINT(debug_pfkey,
  52317. + "klips_debug:pfkey_getspi_parse: .\n");
  52318. +
  52319. + pfkey_extensions_init(extensions_reply);
  52320. +
  52321. + if(extr == NULL || extr->ips == NULL) {
  52322. + KLIPS_PRINT(debug_pfkey,
  52323. + "klips_debug:pfkey_getspi_parse: "
  52324. + "error, extr or extr->ipsec_sa pointer NULL\n");
  52325. + SENDERR(EINVAL);
  52326. + }
  52327. +
  52328. + if(extensions[SADB_EXT_SPIRANGE]) {
  52329. + minspi = ((struct sadb_spirange *)extensions[SADB_EXT_SPIRANGE])->sadb_spirange_min;
  52330. + maxspi = ((struct sadb_spirange *)extensions[SADB_EXT_SPIRANGE])->sadb_spirange_max;
  52331. + }
  52332. +
  52333. + if(maxspi == minspi) {
  52334. + extr->ips->ips_said.spi = maxspi;
  52335. + ipsq = ipsec_sa_getbyid(&(extr->ips->ips_said));
  52336. + if(ipsq != NULL) {
  52337. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  52338. + ipsec_sa_put(ipsq);
  52339. + KLIPS_PRINT(debug_pfkey,
  52340. + "klips_debug:pfkey_getspi_parse: "
  52341. + "EMT_GETSPI found an old ipsec_sa for SA: %s, delete it first.\n",
  52342. + sa_len ? sa : " (error)");
  52343. + SENDERR(EEXIST);
  52344. + } else {
  52345. + found_avail = 1;
  52346. + }
  52347. + } else {
  52348. + int i = 0;
  52349. + __u32 rand_val;
  52350. + __u32 spi_diff;
  52351. + while( ( i < (spi_diff = (ntohl(maxspi) - ntohl(minspi)))) && !found_avail ) {
  52352. + prng_bytes(&ipsec_prng, (char *) &(rand_val),
  52353. + ( (spi_diff < (2^8)) ? 1 :
  52354. + ( (spi_diff < (2^16)) ? 2 :
  52355. + ( (spi_diff < (2^24)) ? 3 :
  52356. + 4 ) ) ) );
  52357. + extr->ips->ips_said.spi = htonl(ntohl(minspi) +
  52358. + (rand_val %
  52359. + (spi_diff + 1)));
  52360. + i++;
  52361. + ipsq = ipsec_sa_getbyid(&(extr->ips->ips_said));
  52362. + if(ipsq == NULL) {
  52363. + found_avail = 1;
  52364. + } else {
  52365. + ipsec_sa_put(ipsq);
  52366. + }
  52367. + }
  52368. + }
  52369. +
  52370. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  52371. +
  52372. + if (!found_avail) {
  52373. + KLIPS_PRINT(debug_pfkey,
  52374. + "klips_debug:pfkey_getspi_parse: "
  52375. + "found an old ipsec_sa for SA: %s, delete it first.\n",
  52376. + sa_len ? sa : " (error)");
  52377. + SENDERR(EEXIST);
  52378. + }
  52379. +
  52380. + if(inet_addr_type((unsigned long)extr->ips->ips_said.dst.u.v4.sin_addr.s_addr) == RTN_LOCAL) {
  52381. + extr->ips->ips_flags |= EMT_INBOUND;
  52382. + }
  52383. +
  52384. + KLIPS_PRINT(debug_pfkey,
  52385. + "klips_debug:pfkey_getspi_parse: "
  52386. + "existing ipsec_sa not found (this is good) for SA: %s, %s-bound, allocating.\n",
  52387. + sa_len ? sa : " (error)",
  52388. + extr->ips->ips_flags & EMT_INBOUND ? "in" : "out");
  52389. +
  52390. + /* XXX extr->ips->ips_rcvif = &(enc_softc[em->em_if].enc_if);*/
  52391. + extr->ips->ips_rcvif = NULL;
  52392. + extr->ips->ips_life.ipl_addtime.ipl_count = jiffies/HZ;
  52393. +
  52394. + extr->ips->ips_state = SADB_SASTATE_LARVAL;
  52395. +
  52396. + if(!extr->ips->ips_life.ipl_allocations.ipl_count) {
  52397. + extr->ips->ips_life.ipl_allocations.ipl_count += 1;
  52398. + }
  52399. +
  52400. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  52401. + SADB_GETSPI,
  52402. + satype,
  52403. + 0,
  52404. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  52405. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  52406. + extensions_reply)
  52407. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  52408. + SADB_EXT_SA,
  52409. + extr->ips->ips_said.spi,
  52410. + 0,
  52411. + SADB_SASTATE_LARVAL,
  52412. + 0,
  52413. + 0,
  52414. + 0,
  52415. + extr->ips->ips_ref),
  52416. + extensions_reply)
  52417. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_SRC],
  52418. + SADB_EXT_ADDRESS_SRC,
  52419. + 0, /*extr->ips->ips_said.proto,*/
  52420. + 0,
  52421. + extr->ips->ips_addr_s),
  52422. + extensions_reply)
  52423. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  52424. + SADB_EXT_ADDRESS_DST,
  52425. + 0, /*extr->ips->ips_said.proto,*/
  52426. + 0,
  52427. + extr->ips->ips_addr_d),
  52428. + extensions_reply) )) {
  52429. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_getspi_parse: "
  52430. + "failed to build the getspi reply message extensions\n");
  52431. + goto errlab;
  52432. + }
  52433. +
  52434. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  52435. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_getspi_parse: "
  52436. + "failed to build the getspi reply message\n");
  52437. + SENDERR(-error);
  52438. + }
  52439. + for(pfkey_socketsp = pfkey_open_sockets;
  52440. + pfkey_socketsp;
  52441. + pfkey_socketsp = pfkey_socketsp->next) {
  52442. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  52443. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_getspi_parse: "
  52444. + "sending up getspi reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  52445. + satype,
  52446. + satype2name(satype),
  52447. + pfkey_socketsp->socketp,
  52448. + error);
  52449. + SENDERR(-error);
  52450. + }
  52451. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_getspi_parse: "
  52452. + "sending up getspi reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  52453. + satype,
  52454. + satype2name(satype),
  52455. + pfkey_socketsp->socketp);
  52456. + }
  52457. +
  52458. + if((error = ipsec_sa_add(extr->ips))) {
  52459. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_getspi_parse: "
  52460. + "failed to add the larval SA=%s with error=%d.\n",
  52461. + sa_len ? sa : " (error)",
  52462. + error);
  52463. + SENDERR(-error);
  52464. + }
  52465. + extr->ips = NULL;
  52466. +
  52467. + KLIPS_PRINT(debug_pfkey,
  52468. + "klips_debug:pfkey_getspi_parse: "
  52469. + "successful for SA: %s\n",
  52470. + sa_len ? sa : " (error)");
  52471. +
  52472. + errlab:
  52473. + if (pfkey_reply) {
  52474. + pfkey_msg_free(&pfkey_reply);
  52475. + }
  52476. + pfkey_extensions_free(extensions_reply);
  52477. + return error;
  52478. +}
  52479. +
  52480. +DEBUG_NO_STATIC int
  52481. +pfkey_update_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  52482. +{
  52483. + int error = 0;
  52484. + struct ipsec_sa* ipsq;
  52485. + char sa[SATOT_BUF];
  52486. + size_t sa_len;
  52487. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  52488. + struct sadb_msg *pfkey_reply = NULL;
  52489. + struct socket_list *pfkey_socketsp;
  52490. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  52491. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  52492. + struct ipsec_sa *nat_t_ips_saved = NULL;
  52493. +#endif
  52494. + KLIPS_PRINT(debug_pfkey,
  52495. + "klips_debug:pfkey_update_parse: .\n");
  52496. +
  52497. + pfkey_extensions_init(extensions_reply);
  52498. +
  52499. + if(((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_state != SADB_SASTATE_MATURE) {
  52500. + KLIPS_PRINT(debug_pfkey,
  52501. + "klips_debug:pfkey_update_parse: "
  52502. + "error, sa_state=%d must be MATURE=%d\n",
  52503. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_state,
  52504. + SADB_SASTATE_MATURE);
  52505. + SENDERR(EINVAL);
  52506. + }
  52507. +
  52508. + if(extr == NULL || extr->ips == NULL) {
  52509. + KLIPS_PRINT(debug_pfkey,
  52510. + "klips_debug:pfkey_update_parse: "
  52511. + "error, extr or extr->ips pointer NULL\n");
  52512. + SENDERR(EINVAL);
  52513. + }
  52514. +
  52515. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  52516. +
  52517. + spin_lock_bh(&tdb_lock);
  52518. +
  52519. + ipsq = ipsec_sa_getbyid(&(extr->ips->ips_said));
  52520. + if (ipsq == NULL) {
  52521. + spin_unlock_bh(&tdb_lock);
  52522. + KLIPS_PRINT(debug_pfkey,
  52523. + "klips_debug:pfkey_update_parse: "
  52524. + "reserved ipsec_sa for SA: %s not found. Call SADB_GETSPI first or call SADB_ADD instead.\n",
  52525. + sa_len ? sa : " (error)");
  52526. + SENDERR(ENOENT);
  52527. + }
  52528. +
  52529. + if(inet_addr_type((unsigned long)extr->ips->ips_said.dst.u.v4.sin_addr.s_addr) == RTN_LOCAL) {
  52530. + extr->ips->ips_flags |= EMT_INBOUND;
  52531. + }
  52532. +
  52533. + KLIPS_PRINT(debug_pfkey,
  52534. + "klips_debug:pfkey_update_parse: "
  52535. + "existing ipsec_sa found (this is good) for SA: %s, %s-bound, updating.\n",
  52536. + sa_len ? sa : " (error)",
  52537. + extr->ips->ips_flags & EMT_INBOUND ? "in" : "out");
  52538. +
  52539. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  52540. + if (extr->ips->ips_natt_sport || extr->ips->ips_natt_dport) {
  52541. + KLIPS_PRINT(debug_pfkey,
  52542. + "klips_debug:pfkey_update_parse: only updating NAT-T ports "
  52543. + "(%u:%u -> %u:%u)\n",
  52544. + ipsq->ips_natt_sport, ipsq->ips_natt_dport,
  52545. + extr->ips->ips_natt_sport, extr->ips->ips_natt_dport);
  52546. +
  52547. + if (extr->ips->ips_natt_sport) {
  52548. + ipsq->ips_natt_sport = extr->ips->ips_natt_sport;
  52549. + if (ipsq->ips_addr_s->sa_family == AF_INET) {
  52550. + ((struct sockaddr_in *)(ipsq->ips_addr_s))->sin_port = htons(extr->ips->ips_natt_sport);
  52551. + }
  52552. + }
  52553. +
  52554. + if (extr->ips->ips_natt_dport) {
  52555. + ipsq->ips_natt_dport = extr->ips->ips_natt_dport;
  52556. + if (ipsq->ips_addr_d->sa_family == AF_INET) {
  52557. + ((struct sockaddr_in *)(ipsq->ips_addr_d))->sin_port = htons(extr->ips->ips_natt_dport);
  52558. + }
  52559. + }
  52560. +
  52561. + nat_t_ips_saved = extr->ips;
  52562. + extr->ips = ipsq;
  52563. + }
  52564. + else {
  52565. +#endif
  52566. +
  52567. + /* XXX extr->ips->ips_rcvif = &(enc_softc[em->em_if].enc_if);*/
  52568. + extr->ips->ips_rcvif = NULL;
  52569. + if ((error = pfkey_ipsec_sa_init(extr->ips))) {
  52570. + ipsec_sa_put(ipsq);
  52571. + spin_unlock_bh(&tdb_lock);
  52572. + KLIPS_PRINT(debug_pfkey,
  52573. + "klips_debug:pfkey_update_parse: "
  52574. + "not successful for SA: %s, deleting.\n",
  52575. + sa_len ? sa : " (error)");
  52576. + SENDERR(-error);
  52577. + }
  52578. +
  52579. + extr->ips->ips_life.ipl_addtime.ipl_count = ipsq->ips_life.ipl_addtime.ipl_count;
  52580. + ipsec_sa_put(ipsq);
  52581. + if((error = ipsec_sa_delchain(ipsq))) {
  52582. + spin_unlock_bh(&tdb_lock);
  52583. + KLIPS_PRINT(debug_pfkey,
  52584. + "klips_debug:pfkey_update_parse: "
  52585. + "error=%d, trouble deleting intermediate ipsec_sa for SA=%s.\n",
  52586. + error,
  52587. + sa_len ? sa : " (error)");
  52588. + SENDERR(-error);
  52589. + }
  52590. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  52591. + }
  52592. +#endif
  52593. +
  52594. + spin_unlock_bh(&tdb_lock);
  52595. +
  52596. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  52597. + SADB_UPDATE,
  52598. + satype,
  52599. + 0,
  52600. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  52601. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  52602. + extensions_reply)
  52603. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  52604. + SADB_EXT_SA,
  52605. + extr->ips->ips_said.spi,
  52606. + extr->ips->ips_replaywin,
  52607. + extr->ips->ips_state,
  52608. + extr->ips->ips_authalg,
  52609. + extr->ips->ips_encalg,
  52610. + extr->ips->ips_flags,
  52611. + extr->ips->ips_ref),
  52612. + extensions_reply)
  52613. + /* The 3 lifetime extentions should only be sent if non-zero. */
  52614. + && (extensions[SADB_EXT_LIFETIME_HARD]
  52615. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_HARD],
  52616. + SADB_EXT_LIFETIME_HARD,
  52617. + extr->ips->ips_life.ipl_allocations.ipl_hard,
  52618. + extr->ips->ips_life.ipl_bytes.ipl_hard,
  52619. + extr->ips->ips_life.ipl_addtime.ipl_hard,
  52620. + extr->ips->ips_life.ipl_usetime.ipl_hard,
  52621. + extr->ips->ips_life.ipl_packets.ipl_hard),
  52622. + extensions_reply) : 1)
  52623. + && (extensions[SADB_EXT_LIFETIME_SOFT]
  52624. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_SOFT],
  52625. + SADB_EXT_LIFETIME_SOFT,
  52626. + extr->ips->ips_life.ipl_allocations.ipl_count,
  52627. + extr->ips->ips_life.ipl_bytes.ipl_count,
  52628. + extr->ips->ips_life.ipl_addtime.ipl_count,
  52629. + extr->ips->ips_life.ipl_usetime.ipl_count,
  52630. + extr->ips->ips_life.ipl_packets.ipl_count),
  52631. + extensions_reply) : 1)
  52632. + && (extr->ips->ips_life.ipl_allocations.ipl_count
  52633. + || extr->ips->ips_life.ipl_bytes.ipl_count
  52634. + || extr->ips->ips_life.ipl_addtime.ipl_count
  52635. + || extr->ips->ips_life.ipl_usetime.ipl_count
  52636. + || extr->ips->ips_life.ipl_packets.ipl_count
  52637. +
  52638. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_CURRENT],
  52639. + SADB_EXT_LIFETIME_CURRENT,
  52640. + extr->ips->ips_life.ipl_allocations.ipl_count,
  52641. + extr->ips->ips_life.ipl_bytes.ipl_count,
  52642. + extr->ips->ips_life.ipl_addtime.ipl_count,
  52643. + extr->ips->ips_life.ipl_usetime.ipl_count,
  52644. + extr->ips->ips_life.ipl_packets.ipl_count),
  52645. + extensions_reply) : 1)
  52646. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_SRC],
  52647. + SADB_EXT_ADDRESS_SRC,
  52648. + 0, /*extr->ips->ips_said.proto,*/
  52649. + 0,
  52650. + extr->ips->ips_addr_s),
  52651. + extensions_reply)
  52652. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  52653. + SADB_EXT_ADDRESS_DST,
  52654. + 0, /*extr->ips->ips_said.proto,*/
  52655. + 0,
  52656. + extr->ips->ips_addr_d),
  52657. + extensions_reply)
  52658. + && (extr->ips->ips_ident_s.data
  52659. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions_reply[SADB_EXT_IDENTITY_SRC],
  52660. + SADB_EXT_IDENTITY_SRC,
  52661. + extr->ips->ips_ident_s.type,
  52662. + extr->ips->ips_ident_s.id,
  52663. + extr->ips->ips_ident_s.len,
  52664. + extr->ips->ips_ident_s.data),
  52665. + extensions_reply) : 1)
  52666. + && (extr->ips->ips_ident_d.data
  52667. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions_reply[SADB_EXT_IDENTITY_DST],
  52668. + SADB_EXT_IDENTITY_DST,
  52669. + extr->ips->ips_ident_d.type,
  52670. + extr->ips->ips_ident_d.id,
  52671. + extr->ips->ips_ident_d.len,
  52672. + extr->ips->ips_ident_d.data),
  52673. + extensions_reply) : 1)
  52674. +#if 0
  52675. + /* FIXME: This won't work yet because I have not finished
  52676. + it. */
  52677. + && (extr->ips->ips_sens_
  52678. + ? pfkey_safe_build(error = pfkey_sens_build(&extensions_reply[SADB_EXT_SENSITIVITY],
  52679. + extr->ips->ips_sens_dpd,
  52680. + extr->ips->ips_sens_sens_level,
  52681. + extr->ips->ips_sens_sens_len,
  52682. + extr->ips->ips_sens_sens_bitmap,
  52683. + extr->ips->ips_sens_integ_level,
  52684. + extr->ips->ips_sens_integ_len,
  52685. + extr->ips->ips_sens_integ_bitmap),
  52686. + extensions_reply) : 1)
  52687. +#endif
  52688. + )) {
  52689. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_update_parse: "
  52690. + "failed to build the update reply message extensions\n");
  52691. + SENDERR(-error);
  52692. + }
  52693. +
  52694. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  52695. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_update_parse: "
  52696. + "failed to build the update reply message\n");
  52697. + SENDERR(-error);
  52698. + }
  52699. + for(pfkey_socketsp = pfkey_open_sockets;
  52700. + pfkey_socketsp;
  52701. + pfkey_socketsp = pfkey_socketsp->next) {
  52702. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  52703. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_update_parse: "
  52704. + "sending up update reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  52705. + satype,
  52706. + satype2name(satype),
  52707. + pfkey_socketsp->socketp,
  52708. + error);
  52709. + SENDERR(-error);
  52710. + }
  52711. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_update_parse: "
  52712. + "sending up update reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  52713. + satype,
  52714. + satype2name(satype),
  52715. + pfkey_socketsp->socketp);
  52716. + }
  52717. +
  52718. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  52719. + if (nat_t_ips_saved) {
  52720. + /**
  52721. + * As we _really_ update existing SA, we keep tdbq and need to delete
  52722. + * parsed ips (nat_t_ips_saved, was extr->ips).
  52723. + *
  52724. + * goto errlab with extr->ips = nat_t_ips_saved will free it.
  52725. + */
  52726. +
  52727. + extr->ips = nat_t_ips_saved;
  52728. +
  52729. + error = 0;
  52730. + KLIPS_PRINT(debug_pfkey,
  52731. + "klips_debug:pfkey_update_parse (NAT-T ports): "
  52732. + "successful for SA: %s\n",
  52733. + sa_len ? sa : " (error)");
  52734. +
  52735. + goto errlab;
  52736. + }
  52737. +#endif
  52738. +
  52739. + if((error = ipsec_sa_add(extr->ips))) {
  52740. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_update_parse: "
  52741. + "failed to update the mature SA=%s with error=%d.\n",
  52742. + sa_len ? sa : " (error)",
  52743. + error);
  52744. + SENDERR(-error);
  52745. + }
  52746. + extr->ips = NULL;
  52747. +
  52748. + KLIPS_PRINT(debug_pfkey,
  52749. + "klips_debug:pfkey_update_parse: "
  52750. + "successful for SA: %s\n",
  52751. + sa_len ? sa : " (error)");
  52752. +
  52753. + errlab:
  52754. + if (pfkey_reply) {
  52755. + pfkey_msg_free(&pfkey_reply);
  52756. + }
  52757. + pfkey_extensions_free(extensions_reply);
  52758. + return error;
  52759. +}
  52760. +
  52761. +DEBUG_NO_STATIC int
  52762. +pfkey_add_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  52763. +{
  52764. + int error = 0;
  52765. + struct ipsec_sa* ipsq;
  52766. + char sa[SATOT_BUF];
  52767. + size_t sa_len;
  52768. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  52769. + struct sadb_msg *pfkey_reply = NULL;
  52770. + struct socket_list *pfkey_socketsp;
  52771. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  52772. +
  52773. + KLIPS_PRINT(debug_pfkey,
  52774. + "klips_debug:pfkey_add_parse: .\n");
  52775. +
  52776. + pfkey_extensions_init(extensions_reply);
  52777. +
  52778. + if(((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_state != SADB_SASTATE_MATURE) {
  52779. + KLIPS_PRINT(debug_pfkey,
  52780. + "klips_debug:pfkey_add_parse: "
  52781. + "error, sa_state=%d must be MATURE=%d\n",
  52782. + ((struct sadb_sa*)extensions[SADB_EXT_SA])->sadb_sa_state,
  52783. + SADB_SASTATE_MATURE);
  52784. + SENDERR(EINVAL);
  52785. + }
  52786. +
  52787. + if(!extr || !extr->ips) {
  52788. + KLIPS_PRINT(debug_pfkey,
  52789. + "klips_debug:pfkey_add_parse: "
  52790. + "extr or extr->ips pointer NULL\n");
  52791. + SENDERR(EINVAL);
  52792. + }
  52793. +
  52794. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  52795. +
  52796. + ipsq = ipsec_sa_getbyid(&(extr->ips->ips_said));
  52797. + if(ipsq != NULL) {
  52798. + ipsec_sa_put(ipsq);
  52799. + KLIPS_PRINT(debug_pfkey,
  52800. + "klips_debug:pfkey_add_parse: "
  52801. + "found an old ipsec_sa for SA%s, delete it first.\n",
  52802. + sa_len ? sa : " (error)");
  52803. + SENDERR(EEXIST);
  52804. + }
  52805. +
  52806. + if(inet_addr_type((unsigned long)extr->ips->ips_said.dst.u.v4.sin_addr.s_addr) == RTN_LOCAL) {
  52807. + extr->ips->ips_flags |= EMT_INBOUND;
  52808. + }
  52809. +
  52810. + KLIPS_PRINT(debug_pfkey,
  52811. + "klips_debug:pfkey_add_parse: "
  52812. + "existing ipsec_sa not found (this is good) for SA%s, %s-bound, allocating.\n",
  52813. + sa_len ? sa : " (error)",
  52814. + extr->ips->ips_flags & EMT_INBOUND ? "in" : "out");
  52815. +
  52816. + /* XXX extr->ips->ips_rcvif = &(enc_softc[em->em_if].enc_if);*/
  52817. + extr->ips->ips_rcvif = NULL;
  52818. +
  52819. + if ((error = pfkey_ipsec_sa_init(extr->ips))) {
  52820. + KLIPS_PRINT(debug_pfkey,
  52821. + "klips_debug:pfkey_add_parse: "
  52822. + "not successful for SA: %s, deleting.\n",
  52823. + sa_len ? sa : " (error)");
  52824. + SENDERR(-error);
  52825. + }
  52826. +
  52827. + extr->ips->ips_life.ipl_addtime.ipl_count = jiffies / HZ;
  52828. + if(!extr->ips->ips_life.ipl_allocations.ipl_count) {
  52829. + extr->ips->ips_life.ipl_allocations.ipl_count += 1;
  52830. + }
  52831. +
  52832. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  52833. + SADB_ADD,
  52834. + satype,
  52835. + 0,
  52836. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  52837. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  52838. + extensions_reply)
  52839. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  52840. + SADB_EXT_SA,
  52841. + extr->ips->ips_said.spi,
  52842. + extr->ips->ips_replaywin,
  52843. + extr->ips->ips_state,
  52844. + extr->ips->ips_authalg,
  52845. + extr->ips->ips_encalg,
  52846. + extr->ips->ips_flags,
  52847. + extr->ips->ips_ref),
  52848. + extensions_reply)
  52849. + /* The 3 lifetime extentions should only be sent if non-zero. */
  52850. + && (extensions[SADB_EXT_LIFETIME_HARD]
  52851. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_HARD],
  52852. + SADB_EXT_LIFETIME_HARD,
  52853. + extr->ips->ips_life.ipl_allocations.ipl_hard,
  52854. + extr->ips->ips_life.ipl_bytes.ipl_hard,
  52855. + extr->ips->ips_life.ipl_addtime.ipl_hard,
  52856. + extr->ips->ips_life.ipl_usetime.ipl_hard,
  52857. + extr->ips->ips_life.ipl_packets.ipl_hard),
  52858. + extensions_reply) : 1)
  52859. + && (extensions[SADB_EXT_LIFETIME_SOFT]
  52860. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_SOFT],
  52861. + SADB_EXT_LIFETIME_SOFT,
  52862. + extr->ips->ips_life.ipl_allocations.ipl_soft,
  52863. + extr->ips->ips_life.ipl_bytes.ipl_soft,
  52864. + extr->ips->ips_life.ipl_addtime.ipl_soft,
  52865. + extr->ips->ips_life.ipl_usetime.ipl_soft,
  52866. + extr->ips->ips_life.ipl_packets.ipl_soft),
  52867. + extensions_reply) : 1)
  52868. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_SRC],
  52869. + SADB_EXT_ADDRESS_SRC,
  52870. + 0, /*extr->ips->ips_said.proto,*/
  52871. + 0,
  52872. + extr->ips->ips_addr_s),
  52873. + extensions_reply)
  52874. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  52875. + SADB_EXT_ADDRESS_DST,
  52876. + 0, /*extr->ips->ips_said.proto,*/
  52877. + 0,
  52878. + extr->ips->ips_addr_d),
  52879. + extensions_reply)
  52880. + && (extr->ips->ips_ident_s.data
  52881. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions_reply[SADB_EXT_IDENTITY_SRC],
  52882. + SADB_EXT_IDENTITY_SRC,
  52883. + extr->ips->ips_ident_s.type,
  52884. + extr->ips->ips_ident_s.id,
  52885. + extr->ips->ips_ident_s.len,
  52886. + extr->ips->ips_ident_s.data),
  52887. + extensions_reply) : 1)
  52888. + && (extr->ips->ips_ident_d.data
  52889. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions_reply[SADB_EXT_IDENTITY_DST],
  52890. + SADB_EXT_IDENTITY_DST,
  52891. + extr->ips->ips_ident_d.type,
  52892. + extr->ips->ips_ident_d.id,
  52893. + extr->ips->ips_ident_d.len,
  52894. + extr->ips->ips_ident_d.data),
  52895. + extensions_reply) : 1)
  52896. +#if 0
  52897. + /* FIXME: This won't work yet because I have not finished
  52898. + it. */
  52899. + && (extr->ips->ips_sens_
  52900. + ? pfkey_safe_build(error = pfkey_sens_build(&extensions_reply[SADB_EXT_SENSITIVITY],
  52901. + extr->ips->ips_sens_dpd,
  52902. + extr->ips->ips_sens_sens_level,
  52903. + extr->ips->ips_sens_sens_len,
  52904. + extr->ips->ips_sens_sens_bitmap,
  52905. + extr->ips->ips_sens_integ_level,
  52906. + extr->ips->ips_sens_integ_len,
  52907. + extr->ips->ips_sens_integ_bitmap),
  52908. + extensions_reply) : 1)
  52909. +#endif
  52910. + )) {
  52911. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_add_parse: "
  52912. + "failed to build the add reply message extensions\n");
  52913. + SENDERR(-error);
  52914. + }
  52915. +
  52916. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  52917. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_add_parse: "
  52918. + "failed to build the add reply message\n");
  52919. + SENDERR(-error);
  52920. + }
  52921. + for(pfkey_socketsp = pfkey_open_sockets;
  52922. + pfkey_socketsp;
  52923. + pfkey_socketsp = pfkey_socketsp->next) {
  52924. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  52925. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_add_parse: "
  52926. + "sending up add reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  52927. + satype,
  52928. + satype2name(satype),
  52929. + pfkey_socketsp->socketp,
  52930. + error);
  52931. + SENDERR(-error);
  52932. + }
  52933. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_add_parse: "
  52934. + "sending up add reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  52935. + satype,
  52936. + satype2name(satype),
  52937. + pfkey_socketsp->socketp);
  52938. + }
  52939. +
  52940. + if((error = ipsec_sa_add(extr->ips))) {
  52941. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_add_parse: "
  52942. + "failed to add the mature SA=%s with error=%d.\n",
  52943. + sa_len ? sa : " (error)",
  52944. + error);
  52945. + SENDERR(-error);
  52946. + }
  52947. + extr->ips = NULL;
  52948. +
  52949. + KLIPS_PRINT(debug_pfkey,
  52950. + "klips_debug:pfkey_add_parse: "
  52951. + "successful for SA: %s\n",
  52952. + sa_len ? sa : " (error)");
  52953. +
  52954. + errlab:
  52955. + if (pfkey_reply) {
  52956. + pfkey_msg_free(&pfkey_reply);
  52957. + }
  52958. + pfkey_extensions_free(extensions_reply);
  52959. + return error;
  52960. +}
  52961. +
  52962. +DEBUG_NO_STATIC int
  52963. +pfkey_delete_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  52964. +{
  52965. + struct ipsec_sa *ipsp;
  52966. + char sa[SATOT_BUF];
  52967. + size_t sa_len;
  52968. + int error = 0;
  52969. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  52970. + struct sadb_msg *pfkey_reply = NULL;
  52971. + struct socket_list *pfkey_socketsp;
  52972. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  52973. +
  52974. + KLIPS_PRINT(debug_pfkey,
  52975. + "klips_debug:pfkey_delete_parse: .\n");
  52976. +
  52977. + pfkey_extensions_init(extensions_reply);
  52978. +
  52979. + if(!extr || !extr->ips) {
  52980. + KLIPS_PRINT(debug_pfkey,
  52981. + "klips_debug:pfkey_delete_parse: "
  52982. + "extr or extr->ips pointer NULL, fatal\n");
  52983. + SENDERR(EINVAL);
  52984. + }
  52985. +
  52986. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  52987. +
  52988. + spin_lock_bh(&tdb_lock);
  52989. +
  52990. + ipsp = ipsec_sa_getbyid(&(extr->ips->ips_said));
  52991. + if (ipsp == NULL) {
  52992. + spin_unlock_bh(&tdb_lock);
  52993. + KLIPS_PRINT(debug_pfkey,
  52994. + "klips_debug:pfkey_delete_parse: "
  52995. + "ipsec_sa not found for SA:%s, could not delete.\n",
  52996. + sa_len ? sa : " (error)");
  52997. + SENDERR(ESRCH);
  52998. + }
  52999. +
  53000. + ipsec_sa_put(ipsp);
  53001. + if((error = ipsec_sa_delchain(ipsp))) {
  53002. + spin_unlock_bh(&tdb_lock);
  53003. + KLIPS_PRINT(debug_pfkey,
  53004. + "klips_debug:pfkey_delete_parse: "
  53005. + "error=%d returned trying to delete ipsec_sa for SA:%s.\n",
  53006. + error,
  53007. + sa_len ? sa : " (error)");
  53008. + SENDERR(-error);
  53009. + }
  53010. + spin_unlock_bh(&tdb_lock);
  53011. +
  53012. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  53013. + SADB_DELETE,
  53014. + satype,
  53015. + 0,
  53016. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  53017. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  53018. + extensions_reply)
  53019. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  53020. + SADB_EXT_SA,
  53021. + extr->ips->ips_said.spi,
  53022. + 0,
  53023. + 0,
  53024. + 0,
  53025. + 0,
  53026. + 0,
  53027. + extr->ips->ips_ref),
  53028. + extensions_reply)
  53029. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_SRC],
  53030. + SADB_EXT_ADDRESS_SRC,
  53031. + 0, /*extr->ips->ips_said.proto,*/
  53032. + 0,
  53033. + extr->ips->ips_addr_s),
  53034. + extensions_reply)
  53035. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  53036. + SADB_EXT_ADDRESS_DST,
  53037. + 0, /*extr->ips->ips_said.proto,*/
  53038. + 0,
  53039. + extr->ips->ips_addr_d),
  53040. + extensions_reply)
  53041. + )) {
  53042. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_delete_parse: "
  53043. + "failed to build the delete reply message extensions\n");
  53044. + SENDERR(-error);
  53045. + }
  53046. +
  53047. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  53048. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_delete_parse: "
  53049. + "failed to build the delete reply message\n");
  53050. + SENDERR(-error);
  53051. + }
  53052. + for(pfkey_socketsp = pfkey_open_sockets;
  53053. + pfkey_socketsp;
  53054. + pfkey_socketsp = pfkey_socketsp->next) {
  53055. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  53056. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_delete_parse: "
  53057. + "sending up delete reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  53058. + satype,
  53059. + satype2name(satype),
  53060. + pfkey_socketsp->socketp,
  53061. + error);
  53062. + SENDERR(-error);
  53063. + }
  53064. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_delete_parse: "
  53065. + "sending up delete reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  53066. + satype,
  53067. + satype2name(satype),
  53068. + pfkey_socketsp->socketp);
  53069. + }
  53070. +
  53071. + errlab:
  53072. + if (pfkey_reply) {
  53073. + pfkey_msg_free(&pfkey_reply);
  53074. + }
  53075. + pfkey_extensions_free(extensions_reply);
  53076. + return error;
  53077. +}
  53078. +
  53079. +DEBUG_NO_STATIC int
  53080. +pfkey_get_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53081. +{
  53082. + int error = 0;
  53083. + struct ipsec_sa *ipsp;
  53084. + char sa[SATOT_BUF];
  53085. + size_t sa_len;
  53086. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  53087. + struct sadb_msg *pfkey_reply = NULL;
  53088. +
  53089. + KLIPS_PRINT(debug_pfkey,
  53090. + "klips_debug:pfkey_get_parse: .\n");
  53091. +
  53092. + pfkey_extensions_init(extensions_reply);
  53093. +
  53094. + if(!extr || !extr->ips) {
  53095. + KLIPS_PRINT(debug_pfkey,
  53096. + "klips_debug:pfkey_get_parse: "
  53097. + "extr or extr->ips pointer NULL, fatal\n");
  53098. + SENDERR(EINVAL);
  53099. + }
  53100. +
  53101. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  53102. +
  53103. + spin_lock_bh(&tdb_lock);
  53104. +
  53105. + ipsp = ipsec_sa_getbyid(&(extr->ips->ips_said));
  53106. + if (ipsp == NULL) {
  53107. + spin_unlock_bh(&tdb_lock);
  53108. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_get_parse: "
  53109. + "ipsec_sa not found for SA=%s, could not get.\n",
  53110. + sa_len ? sa : " (error)");
  53111. + SENDERR(ESRCH);
  53112. + }
  53113. +
  53114. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  53115. + SADB_GET,
  53116. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype,
  53117. + 0,
  53118. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  53119. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  53120. + extensions_reply)
  53121. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  53122. + SADB_EXT_SA,
  53123. + extr->ips->ips_said.spi,
  53124. + extr->ips->ips_replaywin,
  53125. + extr->ips->ips_state,
  53126. + extr->ips->ips_authalg,
  53127. + extr->ips->ips_encalg,
  53128. + extr->ips->ips_flags,
  53129. + extr->ips->ips_ref),
  53130. + extensions_reply)
  53131. + /* The 3 lifetime extentions should only be sent if non-zero. */
  53132. + && (ipsp->ips_life.ipl_allocations.ipl_count
  53133. + || ipsp->ips_life.ipl_bytes.ipl_count
  53134. + || ipsp->ips_life.ipl_addtime.ipl_count
  53135. + || ipsp->ips_life.ipl_usetime.ipl_count
  53136. + || ipsp->ips_life.ipl_packets.ipl_count
  53137. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_CURRENT],
  53138. + SADB_EXT_LIFETIME_CURRENT,
  53139. + ipsp->ips_life.ipl_allocations.ipl_count,
  53140. + ipsp->ips_life.ipl_bytes.ipl_count,
  53141. + ipsp->ips_life.ipl_addtime.ipl_count,
  53142. + ipsp->ips_life.ipl_usetime.ipl_count,
  53143. + ipsp->ips_life.ipl_packets.ipl_count),
  53144. + extensions_reply) : 1)
  53145. + && (ipsp->ips_life.ipl_allocations.ipl_hard
  53146. + || ipsp->ips_life.ipl_bytes.ipl_hard
  53147. + || ipsp->ips_life.ipl_addtime.ipl_hard
  53148. + || ipsp->ips_life.ipl_usetime.ipl_hard
  53149. + || ipsp->ips_life.ipl_packets.ipl_hard
  53150. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_HARD],
  53151. + SADB_EXT_LIFETIME_HARD,
  53152. + ipsp->ips_life.ipl_allocations.ipl_hard,
  53153. + ipsp->ips_life.ipl_bytes.ipl_hard,
  53154. + ipsp->ips_life.ipl_addtime.ipl_hard,
  53155. + ipsp->ips_life.ipl_usetime.ipl_hard,
  53156. + ipsp->ips_life.ipl_packets.ipl_hard),
  53157. + extensions_reply) : 1)
  53158. + && (ipsp->ips_life.ipl_allocations.ipl_soft
  53159. + || ipsp->ips_life.ipl_bytes.ipl_soft
  53160. + || ipsp->ips_life.ipl_addtime.ipl_soft
  53161. + || ipsp->ips_life.ipl_usetime.ipl_soft
  53162. + || ipsp->ips_life.ipl_packets.ipl_soft
  53163. + ? pfkey_safe_build(error = pfkey_lifetime_build(&extensions_reply[SADB_EXT_LIFETIME_SOFT],
  53164. + SADB_EXT_LIFETIME_SOFT,
  53165. + ipsp->ips_life.ipl_allocations.ipl_soft,
  53166. + ipsp->ips_life.ipl_bytes.ipl_soft,
  53167. + ipsp->ips_life.ipl_addtime.ipl_soft,
  53168. + ipsp->ips_life.ipl_usetime.ipl_soft,
  53169. + ipsp->ips_life.ipl_packets.ipl_soft),
  53170. + extensions_reply) : 1)
  53171. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_SRC],
  53172. + SADB_EXT_ADDRESS_SRC,
  53173. + 0, /*extr->ips->ips_said.proto,*/
  53174. + 0,
  53175. + extr->ips->ips_addr_s),
  53176. + extensions_reply)
  53177. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  53178. + SADB_EXT_ADDRESS_DST,
  53179. + 0, /*extr->ips->ips_said.proto,*/
  53180. + 0,
  53181. + extr->ips->ips_addr_d),
  53182. + extensions_reply)
  53183. + && (extr->ips->ips_addr_p
  53184. + ? pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_PROXY],
  53185. + SADB_EXT_ADDRESS_PROXY,
  53186. + 0, /*extr->ips->ips_said.proto,*/
  53187. + 0,
  53188. + extr->ips->ips_addr_p),
  53189. + extensions_reply) : 1)
  53190. +#if 0
  53191. + /* FIXME: This won't work yet because the keys are not
  53192. + stored directly in the ipsec_sa. They are stored as
  53193. + contexts. */
  53194. + && (extr->ips->ips_key_a_size
  53195. + ? pfkey_safe_build(error = pfkey_key_build(&extensions_reply[SADB_EXT_KEY_AUTH],
  53196. + SADB_EXT_KEY_AUTH,
  53197. + extr->ips->ips_key_a_size * 8,
  53198. + extr->ips->ips_key_a),
  53199. + extensions_reply) : 1)
  53200. + /* FIXME: This won't work yet because the keys are not
  53201. + stored directly in the ipsec_sa. They are stored as
  53202. + key schedules. */
  53203. + && (extr->ips->ips_key_e_size
  53204. + ? pfkey_safe_build(error = pfkey_key_build(&extensions_reply[SADB_EXT_KEY_ENCRYPT],
  53205. + SADB_EXT_KEY_ENCRYPT,
  53206. + extr->ips->ips_key_e_size * 8,
  53207. + extr->ips->ips_key_e),
  53208. + extensions_reply) : 1)
  53209. +#endif
  53210. + && (extr->ips->ips_ident_s.data
  53211. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions_reply[SADB_EXT_IDENTITY_SRC],
  53212. + SADB_EXT_IDENTITY_SRC,
  53213. + extr->ips->ips_ident_s.type,
  53214. + extr->ips->ips_ident_s.id,
  53215. + extr->ips->ips_ident_s.len,
  53216. + extr->ips->ips_ident_s.data),
  53217. + extensions_reply) : 1)
  53218. + && (extr->ips->ips_ident_d.data
  53219. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions_reply[SADB_EXT_IDENTITY_DST],
  53220. + SADB_EXT_IDENTITY_DST,
  53221. + extr->ips->ips_ident_d.type,
  53222. + extr->ips->ips_ident_d.id,
  53223. + extr->ips->ips_ident_d.len,
  53224. + extr->ips->ips_ident_d.data),
  53225. + extensions_reply) : 1)
  53226. +#if 0
  53227. + /* FIXME: This won't work yet because I have not finished
  53228. + it. */
  53229. + && (extr->ips->ips_sens_
  53230. + ? pfkey_safe_build(error = pfkey_sens_build(&extensions_reply[SADB_EXT_SENSITIVITY],
  53231. + extr->ips->ips_sens_dpd,
  53232. + extr->ips->ips_sens_sens_level,
  53233. + extr->ips->ips_sens_sens_len,
  53234. + extr->ips->ips_sens_sens_bitmap,
  53235. + extr->ips->ips_sens_integ_level,
  53236. + extr->ips->ips_sens_integ_len,
  53237. + extr->ips->ips_sens_integ_bitmap),
  53238. + extensions_reply) : 1)
  53239. +#endif
  53240. + )) {
  53241. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_get_parse: "
  53242. + "failed to build the get reply message extensions\n");
  53243. + ipsec_sa_put(ipsp);
  53244. + spin_unlock_bh(&tdb_lock);
  53245. + SENDERR(-error);
  53246. + }
  53247. +
  53248. + ipsec_sa_put(ipsp);
  53249. + spin_unlock_bh(&tdb_lock);
  53250. +
  53251. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  53252. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_get_parse: "
  53253. + "failed to build the get reply message\n");
  53254. + SENDERR(-error);
  53255. + }
  53256. +
  53257. + if((error = pfkey_upmsg(sk->sk_socket, pfkey_reply))) {
  53258. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_get_parse: "
  53259. + "failed to send the get reply message\n");
  53260. + SENDERR(-error);
  53261. + }
  53262. +
  53263. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_get_parse: "
  53264. + "succeeded in sending get reply message.\n");
  53265. +
  53266. + errlab:
  53267. + if (pfkey_reply) {
  53268. + pfkey_msg_free(&pfkey_reply);
  53269. + }
  53270. + pfkey_extensions_free(extensions_reply);
  53271. + return error;
  53272. +}
  53273. +
  53274. +DEBUG_NO_STATIC int
  53275. +pfkey_acquire_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53276. +{
  53277. + int error = 0;
  53278. + struct socket_list *pfkey_socketsp;
  53279. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  53280. +
  53281. + KLIPS_PRINT(debug_pfkey,
  53282. + "klips_debug:pfkey_acquire_parse: .\n");
  53283. +
  53284. + /* XXX I don't know if we want an upper bound, since userspace may
  53285. + want to register itself for an satype > SADB_SATYPE_MAX. */
  53286. + if((satype == 0) || (satype > SADB_SATYPE_MAX)) {
  53287. + KLIPS_PRINT(debug_pfkey,
  53288. + "klips_debug:pfkey_acquire_parse: "
  53289. + "SATYPE=%d invalid.\n",
  53290. + satype);
  53291. + SENDERR(EINVAL);
  53292. + }
  53293. +
  53294. + if(!(pfkey_registered_sockets[satype])) {
  53295. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_acquire_parse: "
  53296. + "no sockets registered for SAtype=%d(%s).\n",
  53297. + satype,
  53298. + satype2name(satype));
  53299. + SENDERR(EPROTONOSUPPORT);
  53300. + }
  53301. +
  53302. + for(pfkey_socketsp = pfkey_registered_sockets[satype];
  53303. + pfkey_socketsp;
  53304. + pfkey_socketsp = pfkey_socketsp->next) {
  53305. + if((error = pfkey_upmsg(pfkey_socketsp->socketp,
  53306. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])))) {
  53307. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_acquire_parse: "
  53308. + "sending up acquire reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  53309. + satype,
  53310. + satype2name(satype),
  53311. + pfkey_socketsp->socketp,
  53312. + error);
  53313. + SENDERR(-error);
  53314. + }
  53315. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_acquire_parse: "
  53316. + "sending up acquire reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  53317. + satype,
  53318. + satype2name(satype),
  53319. + pfkey_socketsp->socketp);
  53320. + }
  53321. +
  53322. + errlab:
  53323. + return error;
  53324. +}
  53325. +
  53326. +DEBUG_NO_STATIC int
  53327. +pfkey_register_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53328. +{
  53329. + int error = 0;
  53330. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  53331. +
  53332. + KLIPS_PRINT(debug_pfkey,
  53333. + "klips_debug:pfkey_register_parse: .\n");
  53334. +
  53335. + /* XXX I don't know if we want an upper bound, since userspace may
  53336. + want to register itself for an satype > SADB_SATYPE_MAX. */
  53337. + if((satype == 0) || (satype > SADB_SATYPE_MAX)) {
  53338. + KLIPS_PRINT(debug_pfkey,
  53339. + "klips_debug:pfkey_register_parse: "
  53340. + "SATYPE=%d invalid.\n",
  53341. + satype);
  53342. + SENDERR(EINVAL);
  53343. + }
  53344. +
  53345. + if(!pfkey_list_insert_socket(sk->sk_socket,
  53346. + &(pfkey_registered_sockets[satype]))) {
  53347. + KLIPS_PRINT(debug_pfkey,
  53348. + "klips_debug:pfkey_register_parse: "
  53349. + "SATYPE=%02d(%s) successfully registered by KMd (pid=%d).\n",
  53350. + satype,
  53351. + satype2name(satype),
  53352. + key_pid(sk));
  53353. + };
  53354. +
  53355. + /* send up register msg with supported SATYPE algos */
  53356. +
  53357. + error=pfkey_register_reply(satype, (struct sadb_msg*)extensions[SADB_EXT_RESERVED]);
  53358. + errlab:
  53359. + return error;
  53360. +}
  53361. +
  53362. +int
  53363. +pfkey_register_reply(int satype, struct sadb_msg *sadb_msg)
  53364. +{
  53365. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  53366. + struct sadb_msg *pfkey_reply = NULL;
  53367. + struct socket_list *pfkey_socketsp;
  53368. + struct supported_list *pfkey_supported_listp;
  53369. + unsigned int alg_num_a = 0, alg_num_e = 0;
  53370. + struct sadb_alg *alg_a = NULL, *alg_e = NULL, *alg_ap = NULL, *alg_ep = NULL;
  53371. + int error = 0;
  53372. +
  53373. + pfkey_extensions_init(extensions_reply);
  53374. +
  53375. + if((satype == 0) || (satype > SADB_SATYPE_MAX)) {
  53376. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_register_reply: "
  53377. + "SAtype=%d unspecified or unknown.\n",
  53378. + satype);
  53379. + SENDERR(EINVAL);
  53380. + }
  53381. + if(!(pfkey_registered_sockets[satype])) {
  53382. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_register_reply: "
  53383. + "no sockets registered for SAtype=%d(%s).\n",
  53384. + satype,
  53385. + satype2name(satype));
  53386. + SENDERR(EPROTONOSUPPORT);
  53387. + }
  53388. + /* send up register msg with supported SATYPE algos */
  53389. + pfkey_supported_listp = pfkey_supported_list[satype];
  53390. + KLIPS_PRINT(debug_pfkey,
  53391. + "klips_debug:pfkey_register_reply: "
  53392. + "pfkey_supported_list[%d]=0p%p\n",
  53393. + satype,
  53394. + pfkey_supported_list[satype]);
  53395. + while(pfkey_supported_listp) {
  53396. + KLIPS_PRINT(debug_pfkey,
  53397. + "klips_debug:pfkey_register_reply: "
  53398. + "checking supported=0p%p\n",
  53399. + pfkey_supported_listp);
  53400. + if(pfkey_supported_listp->supportedp->ias_exttype == SADB_EXT_SUPPORTED_AUTH) {
  53401. + KLIPS_PRINT(debug_pfkey,
  53402. + "klips_debug:pfkey_register_reply: "
  53403. + "adding auth alg.\n");
  53404. + alg_num_a++;
  53405. + }
  53406. + if(pfkey_supported_listp->supportedp->ias_exttype == SADB_EXT_SUPPORTED_ENCRYPT) {
  53407. + KLIPS_PRINT(debug_pfkey,
  53408. + "klips_debug:pfkey_register_reply: "
  53409. + "adding encrypt alg.\n");
  53410. + alg_num_e++;
  53411. + }
  53412. + pfkey_supported_listp = pfkey_supported_listp->next;
  53413. + }
  53414. +
  53415. + if(alg_num_a) {
  53416. + KLIPS_PRINT(debug_pfkey,
  53417. + "klips_debug:pfkey_register_reply: "
  53418. + "allocating %lu bytes for auth algs.\n",
  53419. + (unsigned long) (alg_num_a * sizeof(struct sadb_alg)));
  53420. + if((alg_a = kmalloc(alg_num_a * sizeof(struct sadb_alg), GFP_ATOMIC) ) == NULL) {
  53421. + KLIPS_PRINT(debug_pfkey,
  53422. + "klips_debug:pfkey_register_reply: "
  53423. + "auth alg memory allocation error\n");
  53424. + SENDERR(ENOMEM);
  53425. + }
  53426. + alg_ap = alg_a;
  53427. + }
  53428. +
  53429. + if(alg_num_e) {
  53430. + KLIPS_PRINT(debug_pfkey,
  53431. + "klips_debug:pfkey_register_reply: "
  53432. + "allocating %lu bytes for enc algs.\n",
  53433. + (unsigned long) (alg_num_e * sizeof(struct sadb_alg)));
  53434. + if((alg_e = kmalloc(alg_num_e * sizeof(struct sadb_alg), GFP_ATOMIC) ) == NULL) {
  53435. + KLIPS_PRINT(debug_pfkey,
  53436. + "klips_debug:pfkey_register_reply: "
  53437. + "enc alg memory allocation error\n");
  53438. + SENDERR(ENOMEM);
  53439. + }
  53440. + alg_ep = alg_e;
  53441. + }
  53442. +
  53443. + pfkey_supported_listp = pfkey_supported_list[satype];
  53444. + while(pfkey_supported_listp) {
  53445. + if(alg_num_a) {
  53446. + if(pfkey_supported_listp->supportedp->ias_exttype == SADB_EXT_SUPPORTED_AUTH) {
  53447. + alg_ap->sadb_alg_id = pfkey_supported_listp->supportedp->ias_id;
  53448. + alg_ap->sadb_alg_ivlen = pfkey_supported_listp->supportedp->ias_ivlen;
  53449. + alg_ap->sadb_alg_minbits = pfkey_supported_listp->supportedp->ias_keyminbits;
  53450. + alg_ap->sadb_alg_maxbits = pfkey_supported_listp->supportedp->ias_keymaxbits;
  53451. + alg_ap->sadb_alg_reserved = 0;
  53452. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  53453. + "klips_debug:pfkey_register_reply: "
  53454. + "adding auth=0p%p\n",
  53455. + alg_ap);
  53456. + alg_ap++;
  53457. + }
  53458. + }
  53459. + if(alg_num_e) {
  53460. + if(pfkey_supported_listp->supportedp->ias_exttype == SADB_EXT_SUPPORTED_ENCRYPT) {
  53461. + alg_ep->sadb_alg_id = pfkey_supported_listp->supportedp->ias_id;
  53462. + alg_ep->sadb_alg_ivlen = pfkey_supported_listp->supportedp->ias_ivlen;
  53463. + alg_ep->sadb_alg_minbits = pfkey_supported_listp->supportedp->ias_keyminbits;
  53464. + alg_ep->sadb_alg_maxbits = pfkey_supported_listp->supportedp->ias_keymaxbits;
  53465. + alg_ep->sadb_alg_reserved = 0;
  53466. + KLIPS_PRINT(debug_pfkey && sysctl_ipsec_debug_verbose,
  53467. + "klips_debug:pfkey_register_reply: "
  53468. + "adding encrypt=0p%p\n",
  53469. + alg_ep);
  53470. + alg_ep++;
  53471. + }
  53472. + }
  53473. + KLIPS_PRINT(debug_pfkey,
  53474. + "klips_debug:pfkey_register_reply: "
  53475. + "found satype=%d(%s) exttype=%d id=%d ivlen=%d minbits=%d maxbits=%d.\n",
  53476. + satype,
  53477. + satype2name(satype),
  53478. + pfkey_supported_listp->supportedp->ias_exttype,
  53479. + pfkey_supported_listp->supportedp->ias_id,
  53480. + pfkey_supported_listp->supportedp->ias_ivlen,
  53481. + pfkey_supported_listp->supportedp->ias_keyminbits,
  53482. + pfkey_supported_listp->supportedp->ias_keymaxbits);
  53483. + pfkey_supported_listp = pfkey_supported_listp->next;
  53484. + }
  53485. +
  53486. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  53487. + SADB_REGISTER,
  53488. + satype,
  53489. + 0,
  53490. + sadb_msg? sadb_msg->sadb_msg_seq : ++pfkey_msg_seq,
  53491. + sadb_msg? sadb_msg->sadb_msg_pid: current->pid),
  53492. + extensions_reply) &&
  53493. + (alg_num_a ? pfkey_safe_build(error = pfkey_supported_build(&extensions_reply[SADB_EXT_SUPPORTED_AUTH],
  53494. + SADB_EXT_SUPPORTED_AUTH,
  53495. + alg_num_a,
  53496. + alg_a),
  53497. + extensions_reply) : 1) &&
  53498. + (alg_num_e ? pfkey_safe_build(error = pfkey_supported_build(&extensions_reply[SADB_EXT_SUPPORTED_ENCRYPT],
  53499. + SADB_EXT_SUPPORTED_ENCRYPT,
  53500. + alg_num_e,
  53501. + alg_e),
  53502. + extensions_reply) : 1))) {
  53503. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_register_reply: "
  53504. + "failed to build the register message extensions_reply\n");
  53505. + SENDERR(-error);
  53506. + }
  53507. +
  53508. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  53509. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_register_reply: "
  53510. + "failed to build the register message\n");
  53511. + SENDERR(-error);
  53512. + }
  53513. + /* this should go to all registered sockets for that satype only */
  53514. + for(pfkey_socketsp = pfkey_registered_sockets[satype];
  53515. + pfkey_socketsp;
  53516. + pfkey_socketsp = pfkey_socketsp->next) {
  53517. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  53518. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_register_reply: "
  53519. + "sending up acquire message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  53520. + satype,
  53521. + satype2name(satype),
  53522. + pfkey_socketsp->socketp,
  53523. + error);
  53524. + SENDERR(-error);
  53525. + }
  53526. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_register_reply: "
  53527. + "sending up register message for satype=%d(%s) to socket=0p%p succeeded.\n",
  53528. + satype,
  53529. + satype2name(satype),
  53530. + pfkey_socketsp->socketp);
  53531. + }
  53532. +
  53533. + errlab:
  53534. + if(alg_a) {
  53535. + kfree(alg_a);
  53536. + }
  53537. + if(alg_e) {
  53538. + kfree(alg_e);
  53539. + }
  53540. +
  53541. + if (pfkey_reply) {
  53542. + pfkey_msg_free(&pfkey_reply);
  53543. + }
  53544. + pfkey_extensions_free(extensions_reply);
  53545. + return error;
  53546. +}
  53547. +
  53548. +DEBUG_NO_STATIC int
  53549. +pfkey_expire_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53550. +{
  53551. + int error = 0;
  53552. + struct socket_list *pfkey_socketsp;
  53553. +#ifdef CONFIG_KLIPS_DEBUG
  53554. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  53555. +#endif /* CONFIG_KLIPS_DEBUG */
  53556. +
  53557. + KLIPS_PRINT(debug_pfkey,
  53558. + "klips_debug:pfkey_expire_parse: .\n");
  53559. +
  53560. + if(pfkey_open_sockets) {
  53561. + for(pfkey_socketsp = pfkey_open_sockets;
  53562. + pfkey_socketsp;
  53563. + pfkey_socketsp = pfkey_socketsp->next) {
  53564. + if((error = pfkey_upmsg(pfkey_socketsp->socketp,
  53565. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])))) {
  53566. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire_parse: "
  53567. + "sending up expire reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  53568. + satype,
  53569. + satype2name(satype),
  53570. + pfkey_socketsp->socketp,
  53571. + error);
  53572. + SENDERR(-error);
  53573. + }
  53574. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire_parse: "
  53575. + "sending up expire reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  53576. + satype,
  53577. + satype2name(satype),
  53578. + pfkey_socketsp->socketp);
  53579. + }
  53580. + }
  53581. +
  53582. + errlab:
  53583. + return error;
  53584. +}
  53585. +
  53586. +DEBUG_NO_STATIC int
  53587. +pfkey_flush_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53588. +{
  53589. + int error = 0;
  53590. + struct socket_list *pfkey_socketsp;
  53591. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  53592. + uint8_t proto = 0;
  53593. +
  53594. + KLIPS_PRINT(debug_pfkey,
  53595. + "klips_debug:pfkey_flush_parse: "
  53596. + "flushing type %d SAs\n",
  53597. + satype);
  53598. +
  53599. + if(satype && !(proto = satype2proto(satype))) {
  53600. + KLIPS_PRINT(debug_pfkey,
  53601. + "klips_debug:pfkey_flush_parse: "
  53602. + "satype %d lookup failed.\n",
  53603. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype);
  53604. + SENDERR(EINVAL);
  53605. + }
  53606. +
  53607. + if ((error = ipsec_sadb_cleanup(proto))) {
  53608. + SENDERR(-error);
  53609. + }
  53610. +
  53611. + if(pfkey_open_sockets) {
  53612. + for(pfkey_socketsp = pfkey_open_sockets;
  53613. + pfkey_socketsp;
  53614. + pfkey_socketsp = pfkey_socketsp->next) {
  53615. + if((error = pfkey_upmsg(pfkey_socketsp->socketp,
  53616. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])))) {
  53617. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_flush_parse: "
  53618. + "sending up flush reply message for satype=%d(%s) (proto=%d) to socket=0p%p failed with error=%d.\n",
  53619. + satype,
  53620. + satype2name(satype),
  53621. + proto,
  53622. + pfkey_socketsp->socketp,
  53623. + error);
  53624. + SENDERR(-error);
  53625. + }
  53626. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_flush_parse: "
  53627. + "sending up flush reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  53628. + satype,
  53629. + satype2name(satype),
  53630. + pfkey_socketsp->socketp);
  53631. + }
  53632. + }
  53633. +
  53634. + errlab:
  53635. + return error;
  53636. +}
  53637. +
  53638. +DEBUG_NO_STATIC int
  53639. +pfkey_dump_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53640. +{
  53641. + int error = 0;
  53642. +
  53643. + KLIPS_PRINT(debug_pfkey,
  53644. + "klips_debug:pfkey_dump_parse: .\n");
  53645. +
  53646. + SENDERR(ENOSYS);
  53647. + errlab:
  53648. + return error;
  53649. +}
  53650. +
  53651. +DEBUG_NO_STATIC int
  53652. +pfkey_x_promisc_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53653. +{
  53654. + int error = 0;
  53655. +
  53656. + KLIPS_PRINT(debug_pfkey,
  53657. + "klips_debug:pfkey_promisc_parse: .\n");
  53658. +
  53659. + SENDERR(ENOSYS);
  53660. + errlab:
  53661. + return error;
  53662. +}
  53663. +
  53664. +DEBUG_NO_STATIC int
  53665. +pfkey_x_pchange_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53666. +{
  53667. + int error = 0;
  53668. +
  53669. + KLIPS_PRINT(debug_pfkey,
  53670. + "klips_debug:pfkey_x_pchange_parse: .\n");
  53671. +
  53672. + SENDERR(ENOSYS);
  53673. + errlab:
  53674. + return error;
  53675. +}
  53676. +
  53677. +DEBUG_NO_STATIC int
  53678. +pfkey_x_grpsa_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53679. +{
  53680. + struct ipsec_sa *ips1p, *ips2p, *ipsp;
  53681. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  53682. + struct sadb_msg *pfkey_reply = NULL;
  53683. + struct socket_list *pfkey_socketsp;
  53684. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  53685. + char sa1[SATOT_BUF], sa2[SATOT_BUF];
  53686. + size_t sa_len1, sa_len2 = 0;
  53687. + int error = 0;
  53688. +
  53689. + KLIPS_PRINT(debug_pfkey,
  53690. + "klips_debug:pfkey_x_grpsa_parse: .\n");
  53691. +
  53692. + pfkey_extensions_init(extensions_reply);
  53693. +
  53694. + if(extr == NULL || extr->ips == NULL) {
  53695. + KLIPS_PRINT(debug_pfkey,
  53696. + "klips_debug:pfkey_x_grpsa_parse: "
  53697. + "extr or extr->ips is NULL, fatal.\n");
  53698. + SENDERR(EINVAL);
  53699. + }
  53700. +
  53701. + sa_len1 = satot(&extr->ips->ips_said, 0, sa1, sizeof(sa1));
  53702. + if(extr->ips2 != NULL) {
  53703. + sa_len2 = satot(&extr->ips2->ips_said, 0, sa2, sizeof(sa2));
  53704. + }
  53705. +
  53706. + spin_lock_bh(&tdb_lock);
  53707. +
  53708. + ips1p = ipsec_sa_getbyid(&(extr->ips->ips_said));
  53709. + if(ips1p == NULL) {
  53710. + spin_unlock_bh(&tdb_lock);
  53711. + KLIPS_PRINT(debug_pfkey,
  53712. + "klips_debug:pfkey_x_grpsa_parse: "
  53713. + "reserved ipsec_sa for SA1: %s not found. Call SADB_ADD/UPDATE first.\n",
  53714. + sa_len1 ? sa1 : " (error)");
  53715. + SENDERR(ENOENT);
  53716. + }
  53717. + if(extr->ips2) { /* GRPSA */
  53718. + ips2p = ipsec_sa_getbyid(&(extr->ips2->ips_said));
  53719. + if(ips2p == NULL) {
  53720. + ipsec_sa_put(ips1p);
  53721. + spin_unlock_bh(&tdb_lock);
  53722. + KLIPS_PRINT(debug_pfkey,
  53723. + "klips_debug:pfkey_x_grpsa_parse: "
  53724. + "reserved ipsec_sa for SA2: %s not found. Call SADB_ADD/UPDATE first.\n",
  53725. + sa_len2 ? sa2 : " (error)");
  53726. + SENDERR(ENOENT);
  53727. + }
  53728. +
  53729. + /* Is either one already linked? */
  53730. + if(ips1p->ips_onext) {
  53731. + ipsec_sa_put(ips1p);
  53732. + ipsec_sa_put(ips2p);
  53733. + spin_unlock_bh(&tdb_lock);
  53734. + KLIPS_PRINT(debug_pfkey,
  53735. + "klips_debug:pfkey_x_grpsa_parse: "
  53736. + "ipsec_sa for SA: %s is already linked.\n",
  53737. + sa_len1 ? sa1 : " (error)");
  53738. + SENDERR(EEXIST);
  53739. + }
  53740. + if(ips2p->ips_inext) {
  53741. + ipsec_sa_put(ips1p);
  53742. + ipsec_sa_put(ips2p);
  53743. + spin_unlock_bh(&tdb_lock);
  53744. + KLIPS_PRINT(debug_pfkey,
  53745. + "klips_debug:pfkey_x_grpsa_parse: "
  53746. + "ipsec_sa for SA: %s is already linked.\n",
  53747. + sa_len2 ? sa2 : " (error)");
  53748. + SENDERR(EEXIST);
  53749. + }
  53750. +
  53751. + /* Is extr->ips already linked to extr->ips2? */
  53752. + ipsp = ips2p;
  53753. + while(ipsp) {
  53754. + if(ipsp == ips1p) {
  53755. + ipsec_sa_put(ips1p);
  53756. + ipsec_sa_put(ips2p);
  53757. + spin_unlock_bh(&tdb_lock);
  53758. + KLIPS_PRINT(debug_pfkey,
  53759. + "klips_debug:pfkey_x_grpsa_parse: "
  53760. + "ipsec_sa for SA: %s is already linked to %s.\n",
  53761. + sa_len1 ? sa1 : " (error)",
  53762. + sa_len2 ? sa2 : " (error)");
  53763. + SENDERR(EEXIST);
  53764. + }
  53765. + ipsp = ipsp->ips_onext;
  53766. + }
  53767. +
  53768. + /* link 'em */
  53769. + KLIPS_PRINT(debug_pfkey,
  53770. + "klips_debug:pfkey_x_grpsa_parse: "
  53771. + "linking ipsec_sa SA: %s with %s.\n",
  53772. + sa_len1 ? sa1 : " (error)",
  53773. + sa_len2 ? sa2 : " (error)");
  53774. + ips1p->ips_onext = ips2p;
  53775. + ips2p->ips_inext = ips1p;
  53776. + } else { /* UNGRPSA */
  53777. + ipsec_sa_put(ips1p);
  53778. + KLIPS_PRINT(debug_pfkey,
  53779. + "klips_debug:pfkey_x_grpsa_parse: "
  53780. + "unlinking ipsec_sa SA: %s.\n",
  53781. + sa_len1 ? sa1 : " (error)");
  53782. + while(ips1p->ips_onext) {
  53783. + ips1p = ips1p->ips_onext;
  53784. + }
  53785. + while(ips1p->ips_inext) {
  53786. + ipsp = ips1p;
  53787. + ips1p = ips1p->ips_inext;
  53788. + ipsec_sa_put(ips1p);
  53789. + ipsp->ips_inext = NULL;
  53790. + ipsec_sa_put(ipsp);
  53791. + ips1p->ips_onext = NULL;
  53792. + }
  53793. + }
  53794. +
  53795. + spin_unlock_bh(&tdb_lock);
  53796. +
  53797. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  53798. + SADB_X_GRPSA,
  53799. + satype,
  53800. + 0,
  53801. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  53802. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  53803. + extensions_reply)
  53804. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  53805. + SADB_EXT_SA,
  53806. + extr->ips->ips_said.spi,
  53807. + extr->ips->ips_replaywin,
  53808. + extr->ips->ips_state,
  53809. + extr->ips->ips_authalg,
  53810. + extr->ips->ips_encalg,
  53811. + extr->ips->ips_flags,
  53812. + extr->ips->ips_ref),
  53813. + extensions_reply)
  53814. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  53815. + SADB_EXT_ADDRESS_DST,
  53816. + 0, /*extr->ips->ips_said.proto,*/
  53817. + 0,
  53818. + extr->ips->ips_addr_d),
  53819. + extensions_reply)
  53820. + && (extr->ips2
  53821. + ? (pfkey_safe_build(error = pfkey_x_satype_build(&extensions_reply[SADB_X_EXT_SATYPE2],
  53822. + ((struct sadb_x_satype*)extensions[SADB_X_EXT_SATYPE2])->sadb_x_satype_satype
  53823. + /* proto2satype(extr->ips2->ips_said.proto) */),
  53824. + extensions_reply)
  53825. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_X_EXT_SA2],
  53826. + SADB_X_EXT_SA2,
  53827. + extr->ips2->ips_said.spi,
  53828. + extr->ips2->ips_replaywin,
  53829. + extr->ips2->ips_state,
  53830. + extr->ips2->ips_authalg,
  53831. + extr->ips2->ips_encalg,
  53832. + extr->ips2->ips_flags,
  53833. + extr->ips2->ips_ref),
  53834. + extensions_reply)
  53835. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_DST2],
  53836. + SADB_X_EXT_ADDRESS_DST2,
  53837. + 0, /*extr->ips->ips_said.proto,*/
  53838. + 0,
  53839. + extr->ips2->ips_addr_d),
  53840. + extensions_reply) ) : 1 )
  53841. + )) {
  53842. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_grpsa_parse: "
  53843. + "failed to build the x_grpsa reply message extensions\n");
  53844. + SENDERR(-error);
  53845. + }
  53846. +
  53847. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  53848. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_grpsa_parse: "
  53849. + "failed to build the x_grpsa reply message\n");
  53850. + SENDERR(-error);
  53851. + }
  53852. +
  53853. + for(pfkey_socketsp = pfkey_open_sockets;
  53854. + pfkey_socketsp;
  53855. + pfkey_socketsp = pfkey_socketsp->next) {
  53856. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  53857. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_grpsa_parse: "
  53858. + "sending up x_grpsa reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  53859. + satype,
  53860. + satype2name(satype),
  53861. + pfkey_socketsp->socketp,
  53862. + error);
  53863. + SENDERR(-error);
  53864. + }
  53865. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_grpsa_parse: "
  53866. + "sending up x_grpsa reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  53867. + satype,
  53868. + satype2name(satype),
  53869. + pfkey_socketsp->socketp);
  53870. + }
  53871. +
  53872. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_grpsa_parse: "
  53873. + "succeeded in sending x_grpsa reply message.\n");
  53874. +
  53875. + errlab:
  53876. + if (pfkey_reply) {
  53877. + pfkey_msg_free(&pfkey_reply);
  53878. + }
  53879. + pfkey_extensions_free(extensions_reply);
  53880. + return error;
  53881. +}
  53882. +
  53883. +DEBUG_NO_STATIC int
  53884. +pfkey_x_addflow_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  53885. +{
  53886. + int error = 0;
  53887. +#ifdef CONFIG_KLIPS_DEBUG
  53888. + char buf1[64], buf2[64];
  53889. +#endif /* CONFIG_KLIPS_DEBUG */
  53890. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  53891. + struct sadb_msg *pfkey_reply = NULL;
  53892. + struct socket_list *pfkey_socketsp;
  53893. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  53894. + ip_address srcflow, dstflow, srcmask, dstmask;
  53895. +
  53896. + KLIPS_PRINT(debug_pfkey,
  53897. + "klips_debug:pfkey_x_addflow_parse: .\n");
  53898. +
  53899. + pfkey_extensions_init(extensions_reply);
  53900. +
  53901. + memset((caddr_t)&srcflow, 0, sizeof(srcflow));
  53902. + memset((caddr_t)&dstflow, 0, sizeof(dstflow));
  53903. + memset((caddr_t)&srcmask, 0, sizeof(srcmask));
  53904. + memset((caddr_t)&dstmask, 0, sizeof(dstmask));
  53905. +
  53906. + if(!extr || !(extr->ips) || !(extr->eroute)) {
  53907. + KLIPS_PRINT(debug_pfkey,
  53908. + "klips_debug:pfkey_x_addflow_parse: "
  53909. + "missing extr, ipsec_sa or eroute data.\n");
  53910. + SENDERR(EINVAL);
  53911. + }
  53912. +
  53913. + srcflow.u.v4.sin_family = AF_INET;
  53914. + dstflow.u.v4.sin_family = AF_INET;
  53915. + srcmask.u.v4.sin_family = AF_INET;
  53916. + dstmask.u.v4.sin_family = AF_INET;
  53917. + srcflow.u.v4.sin_addr = extr->eroute->er_eaddr.sen_ip_src;
  53918. + dstflow.u.v4.sin_addr = extr->eroute->er_eaddr.sen_ip_dst;
  53919. + srcmask.u.v4.sin_addr = extr->eroute->er_emask.sen_ip_src;
  53920. + dstmask.u.v4.sin_addr = extr->eroute->er_emask.sen_ip_dst;
  53921. +
  53922. +#ifdef CONFIG_KLIPS_DEBUG
  53923. + if (debug_pfkey) {
  53924. + subnettoa(extr->eroute->er_eaddr.sen_ip_src,
  53925. + extr->eroute->er_emask.sen_ip_src, 0, buf1, sizeof(buf1));
  53926. + subnettoa(extr->eroute->er_eaddr.sen_ip_dst,
  53927. + extr->eroute->er_emask.sen_ip_dst, 0, buf2, sizeof(buf2));
  53928. + KLIPS_PRINT(debug_pfkey,
  53929. + "klips_debug:pfkey_x_addflow_parse: "
  53930. + "calling breakeroute and/or makeroute for %s->%s\n",
  53931. + buf1, buf2);
  53932. + }
  53933. +#endif /* CONFIG_KLIPS_DEBUG */
  53934. +
  53935. + if(extr->ips->ips_flags & SADB_X_SAFLAGS_INFLOW) {
  53936. + struct ipsec_sa *ipsp, *ipsq;
  53937. + char sa[SATOT_BUF];
  53938. + size_t sa_len;
  53939. +
  53940. + ipsq = ipsec_sa_getbyid(&(extr->ips->ips_said));
  53941. + if(ipsq == NULL) {
  53942. + KLIPS_PRINT(debug_pfkey,
  53943. + "klips_debug:pfkey_x_addflow_parse: "
  53944. + "ipsec_sa not found, cannot set incoming policy.\n");
  53945. + SENDERR(ENOENT);
  53946. + }
  53947. +
  53948. + ipsp = ipsq;
  53949. + while(ipsp && ipsp->ips_said.proto != IPPROTO_IPIP) {
  53950. + ipsp = ipsp->ips_inext;
  53951. + }
  53952. +
  53953. + if(ipsp == NULL) {
  53954. + ipsec_sa_put(ipsq);
  53955. + KLIPS_PRINT(debug_pfkey,
  53956. + "klips_debug:pfkey_x_addflow_parse: "
  53957. + "SA chain does not have an IPIP SA, cannot set incoming policy.\n");
  53958. + SENDERR(ENOENT);
  53959. + }
  53960. +
  53961. + sa_len = satot(&extr->ips->ips_said, 0, sa, sizeof(sa));
  53962. +
  53963. + ipsp->ips_flags |= SADB_X_SAFLAGS_INFLOW;
  53964. + ipsp->ips_flow_s = srcflow;
  53965. + ipsp->ips_flow_d = dstflow;
  53966. + ipsp->ips_mask_s = srcmask;
  53967. + ipsp->ips_mask_d = dstmask;
  53968. +
  53969. + ipsec_sa_put(ipsq);
  53970. +
  53971. + KLIPS_PRINT(debug_pfkey,
  53972. + "klips_debug:pfkey_x_addflow_parse: "
  53973. + "inbound eroute, setting incoming policy information in IPIP ipsec_sa for SA: %s.\n",
  53974. + sa_len ? sa : " (error)");
  53975. + } else {
  53976. + struct sk_buff *first = NULL, *last = NULL;
  53977. +
  53978. + if(extr->ips->ips_flags & SADB_X_SAFLAGS_REPLACEFLOW) {
  53979. + KLIPS_PRINT(debug_pfkey,
  53980. + "klips_debug:pfkey_x_addflow_parse: "
  53981. + "REPLACEFLOW flag set, calling breakeroute.\n");
  53982. + if ((error = ipsec_breakroute(&(extr->eroute->er_eaddr),
  53983. + &(extr->eroute->er_emask),
  53984. + &first, &last))) {
  53985. + KLIPS_PRINT(debug_pfkey,
  53986. + "klips_debug:pfkey_x_addflow_parse: "
  53987. + "breakeroute returned %d. first=0p%p, last=0p%p\n",
  53988. + error,
  53989. + first,
  53990. + last);
  53991. + if(first != NULL) {
  53992. + ipsec_kfree_skb(first);
  53993. + }
  53994. + if(last != NULL) {
  53995. + ipsec_kfree_skb(last);
  53996. + }
  53997. + SENDERR(-error);
  53998. + }
  53999. + }
  54000. +
  54001. + KLIPS_PRINT(debug_pfkey,
  54002. + "klips_debug:pfkey_x_addflow_parse: "
  54003. + "calling makeroute.\n");
  54004. +
  54005. + if ((error = ipsec_makeroute(&(extr->eroute->er_eaddr),
  54006. + &(extr->eroute->er_emask),
  54007. + extr->ips->ips_said,
  54008. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid,
  54009. + NULL,
  54010. + &(extr->ips->ips_ident_s),
  54011. + &(extr->ips->ips_ident_d)))) {
  54012. + KLIPS_PRINT(debug_pfkey,
  54013. + "klips_debug:pfkey_x_addflow_parse: "
  54014. + "makeroute returned %d.\n", error);
  54015. + SENDERR(-error);
  54016. + }
  54017. + if(first != NULL) {
  54018. + KLIPS_PRINT(debug_eroute,
  54019. + "klips_debug:pfkey_x_addflow_parse: "
  54020. + "first=0p%p HOLD packet re-injected.\n",
  54021. + first);
  54022. + DEV_QUEUE_XMIT(first, first->dev, SOPRI_NORMAL);
  54023. + }
  54024. + if(last != NULL) {
  54025. + KLIPS_PRINT(debug_eroute,
  54026. + "klips_debug:pfkey_x_addflow_parse: "
  54027. + "last=0p%p HOLD packet re-injected.\n",
  54028. + last);
  54029. + DEV_QUEUE_XMIT(last, last->dev, SOPRI_NORMAL);
  54030. + }
  54031. + }
  54032. +
  54033. + KLIPS_PRINT(debug_pfkey,
  54034. + "klips_debug:pfkey_x_addflow_parse: "
  54035. + "makeroute call successful.\n");
  54036. +
  54037. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  54038. + SADB_X_ADDFLOW,
  54039. + satype,
  54040. + 0,
  54041. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  54042. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  54043. + extensions_reply)
  54044. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  54045. + SADB_EXT_SA,
  54046. + extr->ips->ips_said.spi,
  54047. + extr->ips->ips_replaywin,
  54048. + extr->ips->ips_state,
  54049. + extr->ips->ips_authalg,
  54050. + extr->ips->ips_encalg,
  54051. + extr->ips->ips_flags,
  54052. + extr->ips->ips_ref),
  54053. + extensions_reply)
  54054. + && (extensions[SADB_EXT_ADDRESS_SRC]
  54055. + ? pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_SRC],
  54056. + SADB_EXT_ADDRESS_SRC,
  54057. + 0, /*extr->ips->ips_said.proto,*/
  54058. + 0,
  54059. + extr->ips->ips_addr_s),
  54060. + extensions_reply) : 1)
  54061. + && (extensions[SADB_EXT_ADDRESS_DST]
  54062. + ? pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_EXT_ADDRESS_DST],
  54063. + SADB_EXT_ADDRESS_DST,
  54064. + 0, /*extr->ips->ips_said.proto,*/
  54065. + 0,
  54066. + extr->ips->ips_addr_d),
  54067. + extensions_reply) : 1)
  54068. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_SRC_FLOW],
  54069. + SADB_X_EXT_ADDRESS_SRC_FLOW,
  54070. + 0, /*extr->ips->ips_said.proto,*/
  54071. + 0,
  54072. + (struct sockaddr*)&srcflow),
  54073. + extensions_reply)
  54074. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_DST_FLOW],
  54075. + SADB_X_EXT_ADDRESS_DST_FLOW,
  54076. + 0, /*extr->ips->ips_said.proto,*/
  54077. + 0,
  54078. + (struct sockaddr*)&dstflow),
  54079. + extensions_reply)
  54080. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_SRC_MASK],
  54081. + SADB_X_EXT_ADDRESS_SRC_MASK,
  54082. + 0, /*extr->ips->ips_said.proto,*/
  54083. + 0,
  54084. + (struct sockaddr*)&srcmask),
  54085. + extensions_reply)
  54086. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_DST_MASK],
  54087. + SADB_X_EXT_ADDRESS_DST_MASK,
  54088. + 0, /*extr->ips->ips_said.proto,*/
  54089. + 0,
  54090. + (struct sockaddr*)&dstmask),
  54091. + extensions_reply)
  54092. + )) {
  54093. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_addflow_parse: "
  54094. + "failed to build the x_addflow reply message extensions\n");
  54095. + SENDERR(-error);
  54096. + }
  54097. +
  54098. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  54099. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_addflow_parse: "
  54100. + "failed to build the x_addflow reply message\n");
  54101. + SENDERR(-error);
  54102. + }
  54103. +
  54104. + for(pfkey_socketsp = pfkey_open_sockets;
  54105. + pfkey_socketsp;
  54106. + pfkey_socketsp = pfkey_socketsp->next) {
  54107. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  54108. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_addflow_parse: "
  54109. + "sending up x_addflow reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  54110. + satype,
  54111. + satype2name(satype),
  54112. + pfkey_socketsp->socketp,
  54113. + error);
  54114. + SENDERR(-error);
  54115. + }
  54116. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_addflow_parse: "
  54117. + "sending up x_addflow reply message for satype=%d(%s) (proto=%d) to socket=0p%p succeeded.\n",
  54118. + satype,
  54119. + satype2name(satype),
  54120. + extr->ips->ips_said.proto,
  54121. + pfkey_socketsp->socketp);
  54122. + }
  54123. +
  54124. + KLIPS_PRINT(debug_pfkey,
  54125. + "klips_debug:pfkey_x_addflow_parse: "
  54126. + "extr->ips cleaned up and freed.\n");
  54127. +
  54128. + errlab:
  54129. + if (pfkey_reply) {
  54130. + pfkey_msg_free(&pfkey_reply);
  54131. + }
  54132. + pfkey_extensions_free(extensions_reply);
  54133. + return error;
  54134. +}
  54135. +
  54136. +DEBUG_NO_STATIC int
  54137. +pfkey_x_delflow_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  54138. +{
  54139. + int error = 0;
  54140. +#ifdef CONFIG_KLIPS_DEBUG
  54141. + char buf1[64], buf2[64];
  54142. +#endif /* CONFIG_KLIPS_DEBUG */
  54143. + struct sadb_ext *extensions_reply[SADB_EXT_MAX+1];
  54144. + struct sadb_msg *pfkey_reply = NULL;
  54145. + struct socket_list *pfkey_socketsp;
  54146. + uint8_t satype = ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_satype;
  54147. + ip_address srcflow, dstflow, srcmask, dstmask;
  54148. +
  54149. + KLIPS_PRINT(debug_pfkey,
  54150. + "klips_debug:pfkey_x_delflow_parse: .\n");
  54151. +
  54152. + pfkey_extensions_init(extensions_reply);
  54153. +
  54154. + memset((caddr_t)&srcflow, 0, sizeof(srcflow));
  54155. + memset((caddr_t)&dstflow, 0, sizeof(dstflow));
  54156. + memset((caddr_t)&srcmask, 0, sizeof(srcmask));
  54157. + memset((caddr_t)&dstmask, 0, sizeof(dstmask));
  54158. +
  54159. + if(!extr || !(extr->ips)) {
  54160. + KLIPS_PRINT(debug_pfkey,
  54161. + "klips_debug:pfkey_x_delflow_parse: "
  54162. + "extr, or extr->ips is NULL, fatal\n");
  54163. + SENDERR(EINVAL);
  54164. + }
  54165. +
  54166. + if(extr->ips->ips_flags & SADB_X_SAFLAGS_CLEARFLOW) {
  54167. + KLIPS_PRINT(debug_pfkey,
  54168. + "klips_debug:pfkey_x_delflow_parse: "
  54169. + "CLEARFLOW flag set, calling cleareroutes.\n");
  54170. + if ((error = ipsec_cleareroutes()))
  54171. + KLIPS_PRINT(debug_pfkey,
  54172. + "klips_debug:pfkey_x_delflow_parse: "
  54173. + "cleareroutes returned %d.\n", error);
  54174. + SENDERR(-error);
  54175. + } else {
  54176. + struct sk_buff *first = NULL, *last = NULL;
  54177. +
  54178. + if(!(extr->eroute)) {
  54179. + KLIPS_PRINT(debug_pfkey,
  54180. + "klips_debug:pfkey_x_delflow_parse: "
  54181. + "extr->eroute is NULL, fatal.\n");
  54182. + SENDERR(EINVAL);
  54183. + }
  54184. +
  54185. + srcflow.u.v4.sin_family = AF_INET;
  54186. + dstflow.u.v4.sin_family = AF_INET;
  54187. + srcmask.u.v4.sin_family = AF_INET;
  54188. + dstmask.u.v4.sin_family = AF_INET;
  54189. + srcflow.u.v4.sin_addr = extr->eroute->er_eaddr.sen_ip_src;
  54190. + dstflow.u.v4.sin_addr = extr->eroute->er_eaddr.sen_ip_dst;
  54191. + srcmask.u.v4.sin_addr = extr->eroute->er_emask.sen_ip_src;
  54192. + dstmask.u.v4.sin_addr = extr->eroute->er_emask.sen_ip_dst;
  54193. +
  54194. +#ifdef CONFIG_KLIPS_DEBUG
  54195. + if (debug_pfkey) {
  54196. + subnettoa(extr->eroute->er_eaddr.sen_ip_src,
  54197. + extr->eroute->er_emask.sen_ip_src, 0, buf1, sizeof(buf1));
  54198. + subnettoa(extr->eroute->er_eaddr.sen_ip_dst,
  54199. + extr->eroute->er_emask.sen_ip_dst, 0, buf2, sizeof(buf2));
  54200. + KLIPS_PRINT(debug_pfkey,
  54201. + "klips_debug:pfkey_x_delflow_parse: "
  54202. + "calling breakeroute for %s->%s\n",
  54203. + buf1, buf2);
  54204. + }
  54205. +#endif /* CONFIG_KLIPS_DEBUG */
  54206. + error = ipsec_breakroute(&(extr->eroute->er_eaddr),
  54207. + &(extr->eroute->er_emask),
  54208. + &first, &last);
  54209. + if(error) {
  54210. + KLIPS_PRINT(debug_pfkey,
  54211. + "klips_debug:pfkey_x_delflow_parse: "
  54212. + "breakeroute returned %d. first=0p%p, last=0p%p\n",
  54213. + error,
  54214. + first,
  54215. + last);
  54216. + }
  54217. + if(first != NULL) {
  54218. + ipsec_kfree_skb(first);
  54219. + }
  54220. + if(last != NULL) {
  54221. + ipsec_kfree_skb(last);
  54222. + }
  54223. + if(error) {
  54224. + SENDERR(-error);
  54225. + }
  54226. + }
  54227. +
  54228. + if(!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions_reply[0],
  54229. + SADB_X_DELFLOW,
  54230. + satype,
  54231. + 0,
  54232. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_seq,
  54233. + ((struct sadb_msg*)extensions[SADB_EXT_RESERVED])->sadb_msg_pid),
  54234. + extensions_reply)
  54235. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions_reply[SADB_EXT_SA],
  54236. + SADB_EXT_SA,
  54237. + extr->ips->ips_said.spi,
  54238. + extr->ips->ips_replaywin,
  54239. + extr->ips->ips_state,
  54240. + extr->ips->ips_authalg,
  54241. + extr->ips->ips_encalg,
  54242. + extr->ips->ips_flags,
  54243. + extr->ips->ips_ref),
  54244. + extensions_reply)
  54245. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_SRC_FLOW],
  54246. + SADB_X_EXT_ADDRESS_SRC_FLOW,
  54247. + 0, /*extr->ips->ips_said.proto,*/
  54248. + 0,
  54249. + (struct sockaddr*)&srcflow),
  54250. + extensions_reply)
  54251. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_DST_FLOW],
  54252. + SADB_X_EXT_ADDRESS_DST_FLOW,
  54253. + 0, /*extr->ips->ips_said.proto,*/
  54254. + 0,
  54255. + (struct sockaddr*)&dstflow),
  54256. + extensions_reply)
  54257. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_SRC_MASK],
  54258. + SADB_X_EXT_ADDRESS_SRC_MASK,
  54259. + 0, /*extr->ips->ips_said.proto,*/
  54260. + 0,
  54261. + (struct sockaddr*)&srcmask),
  54262. + extensions_reply)
  54263. + && pfkey_safe_build(error = pfkey_address_build(&extensions_reply[SADB_X_EXT_ADDRESS_DST_MASK],
  54264. + SADB_X_EXT_ADDRESS_DST_MASK,
  54265. + 0, /*extr->ips->ips_said.proto,*/
  54266. + 0,
  54267. + (struct sockaddr*)&dstmask),
  54268. + extensions_reply)
  54269. + )) {
  54270. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_delflow_parse: "
  54271. + "failed to build the x_delflow reply message extensions\n");
  54272. + SENDERR(-error);
  54273. + }
  54274. +
  54275. + if((error = pfkey_msg_build(&pfkey_reply, extensions_reply, EXT_BITS_OUT))) {
  54276. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_delflow_parse: "
  54277. + "failed to build the x_delflow reply message\n");
  54278. + SENDERR(-error);
  54279. + }
  54280. +
  54281. + for(pfkey_socketsp = pfkey_open_sockets;
  54282. + pfkey_socketsp;
  54283. + pfkey_socketsp = pfkey_socketsp->next) {
  54284. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_reply))) {
  54285. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_delflow_parse: "
  54286. + "sending up x_delflow reply message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  54287. + satype,
  54288. + satype2name(satype),
  54289. + pfkey_socketsp->socketp,
  54290. + error);
  54291. + SENDERR(-error);
  54292. + }
  54293. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_x_delflow_parse: "
  54294. + "sending up x_delflow reply message for satype=%d(%s) to socket=0p%p succeeded.\n",
  54295. + satype,
  54296. + satype2name(satype),
  54297. + pfkey_socketsp->socketp);
  54298. + }
  54299. +
  54300. + KLIPS_PRINT(debug_pfkey,
  54301. + "klips_debug:pfkey_x_delflow_parse: "
  54302. + "extr->ips cleaned up and freed.\n");
  54303. +
  54304. + errlab:
  54305. + if (pfkey_reply) {
  54306. + pfkey_msg_free(&pfkey_reply);
  54307. + }
  54308. + pfkey_extensions_free(extensions_reply);
  54309. + return error;
  54310. +}
  54311. +
  54312. +DEBUG_NO_STATIC int
  54313. +pfkey_x_msg_debug_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  54314. +{
  54315. + int error = 0;
  54316. +
  54317. + KLIPS_PRINT(debug_pfkey,
  54318. + "klips_debug:pfkey_x_msg_debug_parse: .\n");
  54319. +
  54320. +/* errlab:*/
  54321. + return error;
  54322. +}
  54323. +
  54324. +/* pfkey_expire expects the ipsec_sa table to be locked before being called. */
  54325. +int
  54326. +pfkey_expire(struct ipsec_sa *ipsp, int hard)
  54327. +{
  54328. + struct sadb_ext *extensions[SADB_EXT_MAX+1];
  54329. + struct sadb_msg *pfkey_msg = NULL;
  54330. + struct socket_list *pfkey_socketsp;
  54331. + int error = 0;
  54332. + uint8_t satype;
  54333. +
  54334. + pfkey_extensions_init(extensions);
  54335. +
  54336. + if(!(satype = proto2satype(ipsp->ips_said.proto))) {
  54337. + KLIPS_PRINT(debug_pfkey,
  54338. + "klips_debug:pfkey_expire: "
  54339. + "satype lookup for protocol %d lookup failed.\n",
  54340. + ipsp->ips_said.proto);
  54341. + SENDERR(EINVAL);
  54342. + }
  54343. +
  54344. + if(!pfkey_open_sockets) {
  54345. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire: "
  54346. + "no sockets listening.\n");
  54347. + SENDERR(EPROTONOSUPPORT);
  54348. + }
  54349. +
  54350. + if (!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions[0],
  54351. + SADB_EXPIRE,
  54352. + satype,
  54353. + 0,
  54354. + ++pfkey_msg_seq,
  54355. + 0),
  54356. + extensions)
  54357. + && pfkey_safe_build(error = pfkey_sa_ref_build(&extensions[SADB_EXT_SA],
  54358. + SADB_EXT_SA,
  54359. + ipsp->ips_said.spi,
  54360. + ipsp->ips_replaywin,
  54361. + ipsp->ips_state,
  54362. + ipsp->ips_authalg,
  54363. + ipsp->ips_encalg,
  54364. + ipsp->ips_flags,
  54365. + ipsp->ips_ref),
  54366. + extensions)
  54367. + && pfkey_safe_build(error = pfkey_lifetime_build(&extensions[SADB_EXT_LIFETIME_CURRENT],
  54368. + SADB_EXT_LIFETIME_CURRENT,
  54369. + ipsp->ips_life.ipl_allocations.ipl_count,
  54370. + ipsp->ips_life.ipl_bytes.ipl_count,
  54371. + ipsp->ips_life.ipl_addtime.ipl_count,
  54372. + ipsp->ips_life.ipl_usetime.ipl_count,
  54373. + ipsp->ips_life.ipl_packets.ipl_count),
  54374. + extensions)
  54375. + && (hard ?
  54376. + pfkey_safe_build(error = pfkey_lifetime_build(&extensions[SADB_EXT_LIFETIME_HARD],
  54377. + SADB_EXT_LIFETIME_HARD,
  54378. + ipsp->ips_life.ipl_allocations.ipl_hard,
  54379. + ipsp->ips_life.ipl_bytes.ipl_hard,
  54380. + ipsp->ips_life.ipl_addtime.ipl_hard,
  54381. + ipsp->ips_life.ipl_usetime.ipl_hard,
  54382. + ipsp->ips_life.ipl_packets.ipl_hard),
  54383. + extensions)
  54384. + : pfkey_safe_build(error = pfkey_lifetime_build(&extensions[SADB_EXT_LIFETIME_SOFT],
  54385. + SADB_EXT_LIFETIME_SOFT,
  54386. + ipsp->ips_life.ipl_allocations.ipl_soft,
  54387. + ipsp->ips_life.ipl_bytes.ipl_soft,
  54388. + ipsp->ips_life.ipl_addtime.ipl_soft,
  54389. + ipsp->ips_life.ipl_usetime.ipl_soft,
  54390. + ipsp->ips_life.ipl_packets.ipl_soft),
  54391. + extensions))
  54392. + && pfkey_safe_build(error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_SRC],
  54393. + SADB_EXT_ADDRESS_SRC,
  54394. + 0, /* ipsp->ips_said.proto, */
  54395. + 0,
  54396. + ipsp->ips_addr_s),
  54397. + extensions)
  54398. + && pfkey_safe_build(error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_DST],
  54399. + SADB_EXT_ADDRESS_DST,
  54400. + 0, /* ipsp->ips_said.proto, */
  54401. + 0,
  54402. + ipsp->ips_addr_d),
  54403. + extensions))) {
  54404. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire: "
  54405. + "failed to build the expire message extensions\n");
  54406. + spin_unlock(&tdb_lock);
  54407. + goto errlab;
  54408. + }
  54409. +
  54410. + if ((error = pfkey_msg_build(&pfkey_msg, extensions, EXT_BITS_OUT))) {
  54411. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire: "
  54412. + "failed to build the expire message\n");
  54413. + SENDERR(-error);
  54414. + }
  54415. +
  54416. + for(pfkey_socketsp = pfkey_open_sockets;
  54417. + pfkey_socketsp;
  54418. + pfkey_socketsp = pfkey_socketsp->next) {
  54419. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_msg))) {
  54420. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire: "
  54421. + "sending up expire message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  54422. + satype,
  54423. + satype2name(satype),
  54424. + pfkey_socketsp->socketp,
  54425. + error);
  54426. + SENDERR(-error);
  54427. + }
  54428. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_expire: "
  54429. + "sending up expire message for satype=%d(%s) (proto=%d) to socket=0p%p succeeded.\n",
  54430. + satype,
  54431. + satype2name(satype),
  54432. + ipsp->ips_said.proto,
  54433. + pfkey_socketsp->socketp);
  54434. + }
  54435. +
  54436. + errlab:
  54437. + if (pfkey_msg) {
  54438. + pfkey_msg_free(&pfkey_msg);
  54439. + }
  54440. + pfkey_extensions_free(extensions);
  54441. + return error;
  54442. +}
  54443. +
  54444. +int
  54445. +pfkey_acquire(struct ipsec_sa *ipsp)
  54446. +{
  54447. + struct sadb_ext *extensions[SADB_EXT_MAX+1];
  54448. + struct sadb_msg *pfkey_msg = NULL;
  54449. + struct socket_list *pfkey_socketsp;
  54450. + int error = 0;
  54451. + struct sadb_comb comb[] = {
  54452. + /* auth; encrypt; flags; */
  54453. + /* auth_minbits; auth_maxbits; encrypt_minbits; encrypt_maxbits; */
  54454. + /* reserved; soft_allocations; hard_allocations; soft_bytes; hard_bytes; */
  54455. + /* soft_addtime; hard_addtime; soft_usetime; hard_usetime; */
  54456. + /* soft_packets; hard_packets; */
  54457. + { SADB_AALG_MD5HMAC, SADB_EALG_3DESCBC, SADB_SAFLAGS_PFS,
  54458. + 128, 128, 168, 168,
  54459. + 0, 0, 0, 0, 0,
  54460. + 57600, 86400, 57600, 86400,
  54461. + 0, 0 },
  54462. + { SADB_AALG_SHA1HMAC, SADB_EALG_3DESCBC, SADB_SAFLAGS_PFS,
  54463. + 160, 160, 168, 168,
  54464. + 0, 0, 0, 0, 0,
  54465. + 57600, 86400, 57600, 86400,
  54466. + 0, 0 }
  54467. + };
  54468. +
  54469. + /* XXX This should not be hard-coded. It should be taken from the spdb */
  54470. + uint8_t satype = SADB_SATYPE_ESP;
  54471. +
  54472. + pfkey_extensions_init(extensions);
  54473. +
  54474. + if((satype == 0) || (satype > SADB_SATYPE_MAX)) {
  54475. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_acquire: "
  54476. + "SAtype=%d unspecified or unknown.\n",
  54477. + satype);
  54478. + SENDERR(EINVAL);
  54479. + }
  54480. +
  54481. + if(!(pfkey_registered_sockets[satype])) {
  54482. + KLIPS_PRINT(1|debug_pfkey, "klips_debug:pfkey_acquire: "
  54483. + "no sockets registered for SAtype=%d(%s).\n",
  54484. + satype,
  54485. + satype2name(satype));
  54486. + SENDERR(EPROTONOSUPPORT);
  54487. + }
  54488. +
  54489. + if (!(pfkey_safe_build(error = pfkey_msg_hdr_build(&extensions[0],
  54490. + SADB_ACQUIRE,
  54491. + satype,
  54492. + 0,
  54493. + ++pfkey_msg_seq,
  54494. + 0),
  54495. + extensions)
  54496. + && pfkey_safe_build(error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_SRC],
  54497. + SADB_EXT_ADDRESS_SRC,
  54498. + ipsp->ips_transport_protocol,
  54499. + 0,
  54500. + ipsp->ips_addr_s),
  54501. + extensions)
  54502. + && pfkey_safe_build(error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_DST],
  54503. + SADB_EXT_ADDRESS_DST,
  54504. + ipsp->ips_transport_protocol,
  54505. + 0,
  54506. + ipsp->ips_addr_d),
  54507. + extensions)
  54508. +#if 0
  54509. + && (ipsp->ips_addr_p
  54510. + ? pfkey_safe_build(error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_PROXY],
  54511. + SADB_EXT_ADDRESS_PROXY,
  54512. + ipsp->ips_transport_protocol,
  54513. + 0,
  54514. + ipsp->ips_addr_p),
  54515. + extensions) : 1)
  54516. +#endif
  54517. + && (ipsp->ips_ident_s.type != SADB_IDENTTYPE_RESERVED
  54518. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions[SADB_EXT_IDENTITY_SRC],
  54519. + SADB_EXT_IDENTITY_SRC,
  54520. + ipsp->ips_ident_s.type,
  54521. + ipsp->ips_ident_s.id,
  54522. + ipsp->ips_ident_s.len,
  54523. + ipsp->ips_ident_s.data),
  54524. + extensions) : 1)
  54525. +
  54526. + && (ipsp->ips_ident_d.type != SADB_IDENTTYPE_RESERVED
  54527. + ? pfkey_safe_build(error = pfkey_ident_build(&extensions[SADB_EXT_IDENTITY_DST],
  54528. + SADB_EXT_IDENTITY_DST,
  54529. + ipsp->ips_ident_d.type,
  54530. + ipsp->ips_ident_d.id,
  54531. + ipsp->ips_ident_d.len,
  54532. + ipsp->ips_ident_d.data),
  54533. + extensions) : 1)
  54534. +#if 0
  54535. + /* FIXME: This won't work yet because I have not finished
  54536. + it. */
  54537. + && (ipsp->ips_sens_
  54538. + ? pfkey_safe_build(error = pfkey_sens_build(&extensions[SADB_EXT_SENSITIVITY],
  54539. + ipsp->ips_sens_dpd,
  54540. + ipsp->ips_sens_sens_level,
  54541. + ipsp->ips_sens_sens_len,
  54542. + ipsp->ips_sens_sens_bitmap,
  54543. + ipsp->ips_sens_integ_level,
  54544. + ipsp->ips_sens_integ_len,
  54545. + ipsp->ips_sens_integ_bitmap),
  54546. + extensions) : 1)
  54547. +#endif
  54548. + && pfkey_safe_build(error = pfkey_prop_build(&extensions[SADB_EXT_PROPOSAL],
  54549. + 64, /* replay */
  54550. + sizeof(comb)/sizeof(struct sadb_comb),
  54551. + &(comb[0])),
  54552. + extensions)
  54553. + )) {
  54554. + KLIPS_PRINT(1|debug_pfkey, "klips_debug:pfkey_acquire: "
  54555. + "failed to build the acquire message extensions\n");
  54556. + SENDERR(-error);
  54557. + }
  54558. +
  54559. + if ((error = pfkey_msg_build(&pfkey_msg, extensions, EXT_BITS_OUT))) {
  54560. + KLIPS_PRINT(1|debug_pfkey, "klips_debug:pfkey_acquire: "
  54561. + "failed to build the acquire message\n");
  54562. + SENDERR(-error);
  54563. + }
  54564. +
  54565. +#if KLIPS_PFKEY_ACQUIRE_LOSSAGE > 0
  54566. + if(sysctl_ipsec_regress_pfkey_lossage) {
  54567. + return(0);
  54568. + }
  54569. +#endif
  54570. +
  54571. + /* this should go to all registered sockets for that satype only */
  54572. + for(pfkey_socketsp = pfkey_registered_sockets[satype];
  54573. + pfkey_socketsp;
  54574. + pfkey_socketsp = pfkey_socketsp->next) {
  54575. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_msg))) {
  54576. + KLIPS_PRINT(1|debug_pfkey, "klips_debug:pfkey_acquire: "
  54577. + "sending up acquire message for satype=%d(%s) to socket=0p%p failed with error=%d.\n",
  54578. + satype,
  54579. + satype2name(satype),
  54580. + pfkey_socketsp->socketp,
  54581. + error);
  54582. + SENDERR(-error);
  54583. + }
  54584. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_acquire: "
  54585. + "sending up acquire message for satype=%d(%s) to socket=0p%p succeeded.\n",
  54586. + satype,
  54587. + satype2name(satype),
  54588. + pfkey_socketsp->socketp);
  54589. + }
  54590. +
  54591. + errlab:
  54592. + if (pfkey_msg) {
  54593. + pfkey_msg_free(&pfkey_msg);
  54594. + }
  54595. + pfkey_extensions_free(extensions);
  54596. + return error;
  54597. +}
  54598. +
  54599. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  54600. +int
  54601. +pfkey_nat_t_new_mapping(struct ipsec_sa *ipsp, struct sockaddr *ipaddr,
  54602. + __u16 sport)
  54603. +{
  54604. + struct sadb_ext *extensions[SADB_EXT_MAX+1];
  54605. + struct sadb_msg *pfkey_msg = NULL;
  54606. + struct socket_list *pfkey_socketsp;
  54607. + int error = 0;
  54608. + uint8_t satype = (ipsp->ips_said.proto==IPPROTO_ESP) ? SADB_SATYPE_ESP : 0;
  54609. +
  54610. + /* Construct SADB_X_NAT_T_NEW_MAPPING message */
  54611. +
  54612. + pfkey_extensions_init(extensions);
  54613. +
  54614. + if((satype == 0) || (satype > SADB_SATYPE_MAX)) {
  54615. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_nat_t_new_mapping: "
  54616. + "SAtype=%d unspecified or unknown.\n",
  54617. + satype);
  54618. + SENDERR(EINVAL);
  54619. + }
  54620. +
  54621. + if(!(pfkey_registered_sockets[satype])) {
  54622. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_nat_t_new_mapping: "
  54623. + "no sockets registered for SAtype=%d(%s).\n",
  54624. + satype,
  54625. + satype2name(satype));
  54626. + SENDERR(EPROTONOSUPPORT);
  54627. + }
  54628. +
  54629. + if (!(pfkey_safe_build
  54630. + (error = pfkey_msg_hdr_build(&extensions[0], SADB_X_NAT_T_NEW_MAPPING,
  54631. + satype, 0, ++pfkey_msg_seq, 0), extensions)
  54632. + /* SA */
  54633. + && pfkey_safe_build
  54634. + (error = pfkey_sa_build(&extensions[SADB_EXT_SA],
  54635. + SADB_EXT_SA, ipsp->ips_said.spi, 0, 0, 0, 0, 0), extensions)
  54636. + /* ADDRESS_SRC = old addr */
  54637. + && pfkey_safe_build
  54638. + (error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_SRC],
  54639. + SADB_EXT_ADDRESS_SRC, ipsp->ips_said.proto, 0, ipsp->ips_addr_s),
  54640. + extensions)
  54641. + /* NAT_T_SPORT = old port */
  54642. + && pfkey_safe_build
  54643. + (error = pfkey_x_nat_t_port_build(&extensions[SADB_X_EXT_NAT_T_SPORT],
  54644. + SADB_X_EXT_NAT_T_SPORT, ipsp->ips_natt_sport), extensions)
  54645. + /* ADDRESS_DST = new addr */
  54646. + && pfkey_safe_build
  54647. + (error = pfkey_address_build(&extensions[SADB_EXT_ADDRESS_DST],
  54648. + SADB_EXT_ADDRESS_DST, ipsp->ips_said.proto, 0, ipaddr), extensions)
  54649. + /* NAT_T_DPORT = new port */
  54650. + && pfkey_safe_build
  54651. + (error = pfkey_x_nat_t_port_build(&extensions[SADB_X_EXT_NAT_T_DPORT],
  54652. + SADB_X_EXT_NAT_T_DPORT, sport), extensions)
  54653. + )) {
  54654. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_nat_t_new_mapping: "
  54655. + "failed to build the nat_t_new_mapping message extensions\n");
  54656. + SENDERR(-error);
  54657. + }
  54658. +
  54659. + if ((error = pfkey_msg_build(&pfkey_msg, extensions, EXT_BITS_OUT))) {
  54660. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_nat_t_new_mapping: "
  54661. + "failed to build the nat_t_new_mapping message\n");
  54662. + SENDERR(-error);
  54663. + }
  54664. +
  54665. + /* this should go to all registered sockets for that satype only */
  54666. + for(pfkey_socketsp = pfkey_registered_sockets[satype];
  54667. + pfkey_socketsp;
  54668. + pfkey_socketsp = pfkey_socketsp->next) {
  54669. + if((error = pfkey_upmsg(pfkey_socketsp->socketp, pfkey_msg))) {
  54670. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_nat_t_new_mapping: "
  54671. + "sending up nat_t_new_mapping message for satype=%d(%s) to socket=%p failed with error=%d.\n",
  54672. + satype,
  54673. + satype2name(satype),
  54674. + pfkey_socketsp->socketp,
  54675. + error);
  54676. + SENDERR(-error);
  54677. + }
  54678. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_nat_t_new_mapping: "
  54679. + "sending up nat_t_new_mapping message for satype=%d(%s) to socket=%p succeeded.\n",
  54680. + satype,
  54681. + satype2name(satype),
  54682. + pfkey_socketsp->socketp);
  54683. + }
  54684. +
  54685. + errlab:
  54686. + if (pfkey_msg) {
  54687. + pfkey_msg_free(&pfkey_msg);
  54688. + }
  54689. + pfkey_extensions_free(extensions);
  54690. + return error;
  54691. +}
  54692. +
  54693. +DEBUG_NO_STATIC int
  54694. +pfkey_x_nat_t_new_mapping_parse(struct sock *sk, struct sadb_ext **extensions, struct pfkey_extracted_data* extr)
  54695. +{
  54696. + /* SADB_X_NAT_T_NEW_MAPPING not used in kernel */
  54697. + return -EINVAL;
  54698. +}
  54699. +#endif
  54700. +
  54701. +DEBUG_NO_STATIC int (*ext_processors[SADB_EXT_MAX+1])(struct sadb_ext *pfkey_ext, struct pfkey_extracted_data* extr) =
  54702. +{
  54703. + NULL, /* pfkey_msg_process, */
  54704. + pfkey_sa_process,
  54705. + pfkey_lifetime_process,
  54706. + pfkey_lifetime_process,
  54707. + pfkey_lifetime_process,
  54708. + pfkey_address_process,
  54709. + pfkey_address_process,
  54710. + pfkey_address_process,
  54711. + pfkey_key_process,
  54712. + pfkey_key_process,
  54713. + pfkey_ident_process,
  54714. + pfkey_ident_process,
  54715. + pfkey_sens_process,
  54716. + pfkey_prop_process,
  54717. + pfkey_supported_process,
  54718. + pfkey_supported_process,
  54719. + pfkey_spirange_process,
  54720. + pfkey_x_kmprivate_process,
  54721. + pfkey_x_satype_process,
  54722. + pfkey_sa_process,
  54723. + pfkey_address_process,
  54724. + pfkey_address_process,
  54725. + pfkey_address_process,
  54726. + pfkey_address_process,
  54727. + pfkey_address_process,
  54728. + pfkey_x_debug_process,
  54729. + pfkey_x_protocol_process
  54730. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  54731. + ,
  54732. + pfkey_x_nat_t_type_process,
  54733. + pfkey_x_nat_t_port_process,
  54734. + pfkey_x_nat_t_port_process,
  54735. + pfkey_address_process
  54736. +#endif
  54737. +};
  54738. +
  54739. +
  54740. +DEBUG_NO_STATIC int (*msg_parsers[SADB_MAX +1])(struct sock *sk, struct sadb_ext *extensions[], struct pfkey_extracted_data* extr)
  54741. + =
  54742. +{
  54743. + NULL, /* RESERVED */
  54744. + pfkey_getspi_parse,
  54745. + pfkey_update_parse,
  54746. + pfkey_add_parse,
  54747. + pfkey_delete_parse,
  54748. + pfkey_get_parse,
  54749. + pfkey_acquire_parse,
  54750. + pfkey_register_parse,
  54751. + pfkey_expire_parse,
  54752. + pfkey_flush_parse,
  54753. + pfkey_dump_parse,
  54754. + pfkey_x_promisc_parse,
  54755. + pfkey_x_pchange_parse,
  54756. + pfkey_x_grpsa_parse,
  54757. + pfkey_x_addflow_parse,
  54758. + pfkey_x_delflow_parse,
  54759. + pfkey_x_msg_debug_parse
  54760. +#ifdef CONFIG_IPSEC_NAT_TRAVERSAL
  54761. + , pfkey_x_nat_t_new_mapping_parse
  54762. +#endif
  54763. +};
  54764. +
  54765. +int
  54766. +pfkey_build_reply(struct sadb_msg *pfkey_msg,
  54767. + struct pfkey_extracted_data *extr,
  54768. + struct sadb_msg **pfkey_reply)
  54769. +{
  54770. + struct sadb_ext *extensions[SADB_EXT_MAX+1];
  54771. + int error = 0;
  54772. + int msg_type = pfkey_msg->sadb_msg_type;
  54773. + int seq = pfkey_msg->sadb_msg_seq;
  54774. +
  54775. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_build_reply: "
  54776. + "building reply with type: %d\n",
  54777. + msg_type);
  54778. + pfkey_extensions_init(extensions);
  54779. + if (!extr || !extr->ips) {
  54780. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_build_reply: "
  54781. + "bad ipsec_sa passed\n");
  54782. + return EINVAL;
  54783. + }
  54784. + error = pfkey_safe_build(pfkey_msg_hdr_build(&extensions[0],
  54785. + msg_type,
  54786. + proto2satype(extr->ips->ips_said.proto),
  54787. + 0,
  54788. + seq,
  54789. + pfkey_msg->sadb_msg_pid),
  54790. + extensions) &&
  54791. + (!(extensions_bitmaps[EXT_BITS_OUT][EXT_BITS_REQ][msg_type] &
  54792. + 1 << SADB_EXT_SA)
  54793. + || pfkey_safe_build(pfkey_sa_ref_build(&extensions[SADB_EXT_SA],
  54794. + SADB_EXT_SA,
  54795. + extr->ips->ips_said.spi,
  54796. + extr->ips->ips_replaywin,
  54797. + extr->ips->ips_state,
  54798. + extr->ips->ips_authalg,
  54799. + extr->ips->ips_encalg,
  54800. + extr->ips->ips_flags,
  54801. + extr->ips->ips_ref),
  54802. + extensions)) &&
  54803. + (!(extensions_bitmaps[EXT_BITS_OUT][EXT_BITS_REQ][msg_type] &
  54804. + 1 << SADB_EXT_LIFETIME_CURRENT)
  54805. + || pfkey_safe_build(pfkey_lifetime_build(&extensions
  54806. + [SADB_EXT_LIFETIME_CURRENT],
  54807. + SADB_EXT_LIFETIME_CURRENT,
  54808. + extr->ips->ips_life.ipl_allocations.ipl_count,
  54809. + extr->ips->ips_life.ipl_bytes.ipl_count,
  54810. + extr->ips->ips_life.ipl_addtime.ipl_count,
  54811. + extr->ips->ips_life.ipl_usetime.ipl_count,
  54812. + extr->ips->ips_life.ipl_packets.ipl_count),
  54813. + extensions)) &&
  54814. + (!(extensions_bitmaps[EXT_BITS_OUT][EXT_BITS_REQ][msg_type] &
  54815. + 1 << SADB_EXT_ADDRESS_SRC)
  54816. + || pfkey_safe_build(pfkey_address_build(&extensions[SADB_EXT_ADDRESS_SRC],
  54817. + SADB_EXT_ADDRESS_SRC,
  54818. + extr->ips->ips_said.proto,
  54819. + 0,
  54820. + extr->ips->ips_addr_s),
  54821. + extensions)) &&
  54822. + (!(extensions_bitmaps[EXT_BITS_OUT][EXT_BITS_REQ][msg_type] &
  54823. + 1 << SADB_EXT_ADDRESS_DST)
  54824. + || pfkey_safe_build(pfkey_address_build(&extensions[SADB_EXT_ADDRESS_DST],
  54825. + SADB_EXT_ADDRESS_DST,
  54826. + extr->ips->ips_said.proto,
  54827. + 0,
  54828. + extr->ips->ips_addr_d),
  54829. + extensions));
  54830. +
  54831. + if (error == 0) {
  54832. + KLIPS_PRINT(debug_pfkey, "klips_debug:pfkey_build_reply: "
  54833. + "building extensions failed\n");
  54834. + return EINVAL;
  54835. + }
  54836. +
  54837. + KLIPS_PRINT(debug_pfkey,
  54838. + "klips_debug:pfkey_build_reply: "
  54839. + "built extensions, proceed to build the message\n");
  54840. + KLIPS_PRINT(debug_pfkey,
  54841. + "klips_debug:pfkey_build_reply: "
  54842. + "extensions[1]=0p%p\n",
  54843. + extensions[1]);
  54844. + error = pfkey_msg_build(pfkey_reply, extensions, EXT_BITS_OUT);
  54845. + pfkey_extensions_free(extensions);
  54846. +
  54847. + return error;
  54848. +}
  54849. +
  54850. +int
  54851. +pfkey_msg_interp(struct sock *sk, struct sadb_msg *pfkey_msg,
  54852. + struct sadb_msg **pfkey_reply)
  54853. +{
  54854. + int error = 0;
  54855. + int i;
  54856. + struct sadb_ext *extensions[SADB_EXT_MAX+1];
  54857. + struct pfkey_extracted_data extr = {NULL, NULL, NULL};
  54858. +
  54859. + pfkey_extensions_init(extensions);
  54860. + KLIPS_PRINT(debug_pfkey,
  54861. + "klips_debug:pfkey_msg_interp: "
  54862. + "parsing message ver=%d, type=%d, errno=%d, satype=%d(%s), len=%d, res=%d, seq=%d, pid=%d.\n",
  54863. + pfkey_msg->sadb_msg_version,
  54864. + pfkey_msg->sadb_msg_type,
  54865. + pfkey_msg->sadb_msg_errno,
  54866. + pfkey_msg->sadb_msg_satype,
  54867. + satype2name(pfkey_msg->sadb_msg_satype),
  54868. + pfkey_msg->sadb_msg_len,
  54869. + pfkey_msg->sadb_msg_reserved,
  54870. + pfkey_msg->sadb_msg_seq,
  54871. + pfkey_msg->sadb_msg_pid);
  54872. +
  54873. + extr.ips = ipsec_sa_alloc(&error); /* pass in error var by pointer */
  54874. + if(extr.ips == NULL) {
  54875. + KLIPS_PRINT(debug_pfkey,
  54876. + "klips_debug:pfkey_msg_interp: "
  54877. + "memory allocation error.\n");
  54878. + SENDERR(-error);
  54879. + }
  54880. +
  54881. + KLIPS_PRINT(debug_pfkey,
  54882. + "klips_debug:pfkey_msg_interp: "
  54883. + "allocated extr->ips=0p%p.\n",
  54884. + extr.ips);
  54885. +
  54886. + if(pfkey_msg->sadb_msg_satype > SADB_SATYPE_MAX) {
  54887. + KLIPS_PRINT(debug_pfkey,
  54888. + "klips_debug:pfkey_msg_interp: "
  54889. + "satype %d > max %d\n",
  54890. + pfkey_msg->sadb_msg_satype,
  54891. + SADB_SATYPE_MAX);
  54892. + SENDERR(EINVAL);
  54893. + }
  54894. +
  54895. + switch(pfkey_msg->sadb_msg_type) {
  54896. + case SADB_GETSPI:
  54897. + case SADB_UPDATE:
  54898. + case SADB_ADD:
  54899. + case SADB_DELETE:
  54900. + case SADB_X_GRPSA:
  54901. + case SADB_X_ADDFLOW:
  54902. + if(!(extr.ips->ips_said.proto = satype2proto(pfkey_msg->sadb_msg_satype))) {
  54903. + KLIPS_PRINT(debug_pfkey,
  54904. + "klips_debug:pfkey_msg_interp: "
  54905. + "satype %d lookup failed.\n",
  54906. + pfkey_msg->sadb_msg_satype);
  54907. + SENDERR(EINVAL);
  54908. + } else {
  54909. + KLIPS_PRINT(debug_pfkey,
  54910. + "klips_debug:pfkey_msg_interp: "
  54911. + "satype %d lookups to proto=%d.\n",
  54912. + pfkey_msg->sadb_msg_satype,
  54913. + extr.ips->ips_said.proto);
  54914. + }
  54915. + break;
  54916. + default:
  54917. + break;
  54918. + }
  54919. +
  54920. + /* The NULL below causes the default extension parsers to be used */
  54921. + /* Parse the extensions */
  54922. + if((error = pfkey_msg_parse(pfkey_msg, NULL, extensions, EXT_BITS_IN)))
  54923. + {
  54924. + KLIPS_PRINT(debug_pfkey,
  54925. + "klips_debug:pfkey_msg_interp: "
  54926. + "message parsing failed with error %d.\n",
  54927. + error);
  54928. + SENDERR(-error);
  54929. + }
  54930. +
  54931. + /* Process the extensions */
  54932. + for(i=1; i <= SADB_EXT_MAX;i++) {
  54933. + if(extensions[i] != NULL) {
  54934. + KLIPS_PRINT(debug_pfkey,
  54935. + "klips_debug:pfkey_msg_interp: "
  54936. + "processing ext %d 0p%p with processor 0p%p.\n",
  54937. + i, extensions[i], ext_processors[i]);
  54938. + if((error = ext_processors[i](extensions[i], &extr))) {
  54939. + KLIPS_PRINT(debug_pfkey,
  54940. + "klips_debug:pfkey_msg_interp: "
  54941. + "extension processing for type %d failed with error %d.\n",
  54942. + i,
  54943. + error);
  54944. + SENDERR(-error);
  54945. + }
  54946. +
  54947. + }
  54948. +
  54949. + }
  54950. +
  54951. + /* Parse the message types */
  54952. + KLIPS_PRINT(debug_pfkey,
  54953. + "klips_debug:pfkey_msg_interp: "
  54954. + "parsing message type %d(%s) with msg_parser 0p%p.\n",
  54955. + pfkey_msg->sadb_msg_type,
  54956. + pfkey_v2_sadb_type_string(pfkey_msg->sadb_msg_type),
  54957. + msg_parsers[pfkey_msg->sadb_msg_type]);
  54958. + if((error = msg_parsers[pfkey_msg->sadb_msg_type](sk, extensions, &extr))) {
  54959. + KLIPS_PRINT(debug_pfkey,
  54960. + "klips_debug:pfkey_msg_interp: "
  54961. + "message parsing failed with error %d.\n",
  54962. + error);
  54963. + SENDERR(-error);
  54964. + }
  54965. +
  54966. +#if 0
  54967. + error = pfkey_build_reply(pfkey_msg, &extr, pfkey_reply);
  54968. + if (error) {
  54969. + *pfkey_reply = NULL;
  54970. + }
  54971. +#endif
  54972. + errlab:
  54973. + if(extr.ips != NULL) {
  54974. + ipsec_sa_wipe(extr.ips);
  54975. + }
  54976. + if(extr.ips2 != NULL) {
  54977. + ipsec_sa_wipe(extr.ips2);
  54978. + }
  54979. + if (extr.eroute != NULL) {
  54980. + kfree(extr.eroute);
  54981. + }
  54982. + return(error);
  54983. +}
  54984. +
  54985. +/*
  54986. + * $Log: pfkey_v2_parser.c,v $
  54987. + * Revision 1.134.2.2 2006/10/06 21:39:26 paul
  54988. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  54989. + * set. This is defined through autoconf.h which is included through the
  54990. + * linux kernel build macros.
  54991. + *
  54992. + * Revision 1.134.2.1 2006/05/01 14:37:25 mcr
  54993. + * ip_chk_addr -> inet_addr_type for more direct 2.4/2.6 support.
  54994. + *
  54995. + * Revision 1.134 2005/05/11 01:48:20 mcr
  54996. + * removed "poor-man"s OOP in favour of proper C structures.
  54997. + *
  54998. + * Revision 1.133 2005/04/29 05:10:22 mcr
  54999. + * removed from extraenous includes to make unit testing easier.
  55000. + *
  55001. + * Revision 1.132 2005/04/14 20:56:24 mcr
  55002. + * moved (pfkey_)ipsec_sa_init to ipsec_sa.c.
  55003. + *
  55004. + * Revision 1.131 2005/01/26 00:50:35 mcr
  55005. + * adjustment of confusion of CONFIG_IPSEC_NAT vs CONFIG_KLIPS_NAT,
  55006. + * and make sure that NAT_TRAVERSAL is set as well to match
  55007. + * userspace compiles of code.
  55008. + *
  55009. + * Revision 1.130 2004/09/08 17:21:36 ken
  55010. + * Rename MD5* -> osMD5 functions to prevent clashes with other symbols exported by kernel modules (CIFS in 2.6 initiated this)
  55011. + *
  55012. + * Revision 1.129 2004/09/06 18:36:30 mcr
  55013. + * if a protocol can not be found, then log it. This is not
  55014. + * debugging.
  55015. + *
  55016. + * Revision 1.128 2004/08/21 00:45:19 mcr
  55017. + * CONFIG_KLIPS_NAT was wrong, also need to include udp.h.
  55018. + *
  55019. + * Revision 1.127 2004/08/20 21:45:45 mcr
  55020. + * CONFIG_KLIPS_NAT_TRAVERSAL is not used in an attempt to
  55021. + * be 26sec compatible. But, some defines where changed.
  55022. + *
  55023. + * Revision 1.126 2004/08/17 03:27:23 mcr
  55024. + * klips 2.6 edits.
  55025. + *
  55026. + * Revision 1.125 2004/08/04 15:57:07 mcr
  55027. + * moved des .h files to include/des/ *
  55028. + * included 2.6 protocol specific things
  55029. + * started at NAT-T support, but it will require a kernel patch.
  55030. + *
  55031. + * Revision 1.124 2004/07/10 19:11:18 mcr
  55032. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  55033. + *
  55034. + * Revision 1.123 2004/04/06 02:49:26 mcr
  55035. + * pullup of algo code from alg-branch.
  55036. + *
  55037. + * Revision 1.122.2.2 2004/04/05 04:30:46 mcr
  55038. + * patches for alg-branch to compile/work with 2.x openswan
  55039. + *
  55040. + * Revision 1.122.2.1 2003/12/22 15:25:52 jjo
  55041. + * . Merged algo-0.8.1-rc11-test1 into alg-branch
  55042. + *
  55043. + * Revision 1.122 2003/12/10 01:14:27 mcr
  55044. + * NAT-traversal patches to KLIPS.
  55045. + *
  55046. + * Revision 1.121 2003/10/31 02:27:55 mcr
  55047. + * pulled up port-selector patches and sa_id elimination.
  55048. + *
  55049. + * Revision 1.120.4.2 2003/10/29 01:30:41 mcr
  55050. + * elimited "struct sa_id".
  55051. + *
  55052. + * Revision 1.120.4.1 2003/09/21 13:59:56 mcr
  55053. + * pre-liminary X.509 patch - does not yet pass tests.
  55054. + *
  55055. + * Revision 1.120 2003/04/03 17:38:09 rgb
  55056. + * Centralised ipsec_kfree_skb and ipsec_dev_{get,put}.
  55057. + *
  55058. + * Revision 1.119 2003/02/06 01:52:37 rgb
  55059. + * Removed no longer relevant comment
  55060. + *
  55061. + * Revision 1.118 2003/01/30 02:32:44 rgb
  55062. + *
  55063. + * Transmit error code through to caller from callee for better diagnosis of problems.
  55064. + *
  55065. + * Revision 1.117 2003/01/16 18:48:13 rgb
  55066. + *
  55067. + * Fixed sign bug in error return from an sa allocation call in
  55068. + * pfkey_msg_interp.
  55069. + *
  55070. + * Revision 1.116 2002/10/17 16:38:01 rgb
  55071. + * Change pfkey_alloc_eroute() to never static since its consumers
  55072. + * have been moved outside the file.
  55073. + *
  55074. + * Revision 1.115 2002/10/12 23:11:53 dhr
  55075. + *
  55076. + * [KenB + DHR] more 64-bit cleanup
  55077. + *
  55078. + * Revision 1.114 2002/10/05 05:02:58 dhr
  55079. + *
  55080. + * C labels go on statements
  55081. + *
  55082. + * Revision 1.113 2002/09/30 19:11:22 rgb
  55083. + * Turn on debugging for upgoing acquire messages to test for reliability.
  55084. + *
  55085. + * Revision 1.112 2002/09/20 15:41:16 rgb
  55086. + * Switch from pfkey_alloc_ipsec_sa() to ipsec_sa_alloc().
  55087. + * Added sadb_x_sa_ref to struct sadb_sa.
  55088. + * Added ref parameter to pfkey_sa_build().
  55089. + *
  55090. + * Revision 1.111 2002/09/20 05:02:08 rgb
  55091. + * Added memory allocation debugging.
  55092. + * Convert to switch to divulge hmac keys for debugging.
  55093. + * Added text labels to elucidate numeric values presented.
  55094. + *
  55095. + * Revision 1.110 2002/08/03 18:03:05 mcr
  55096. + * loop that checks for SPI's to have been already linked
  55097. + * fails to actually step to next pointer, but continuously
  55098. + * resets to head of list. Wrong pointer used.
  55099. + * test east-icmp-02 revealed this.
  55100. + *
  55101. + * Revision 1.109 2002/07/26 08:48:31 rgb
  55102. + * Added SA ref table code.
  55103. + *
  55104. + * Revision 1.108 2002/05/27 18:55:03 rgb
  55105. + * Remove final vistiges of tdb references via IPSEC_KLIPS1_COMPAT.
  55106. + *
  55107. + * Revision 1.107 2002/05/23 07:16:08 rgb
  55108. + * Added ipsec_sa_put() for releasing an ipsec_sa refcount.
  55109. + * Pointer clean-up.
  55110. + * Added refcount code.
  55111. + *
  55112. + * Revision 1.106 2002/05/14 02:34:13 rgb
  55113. + * Converted reference from ipsec_sa_put to ipsec_sa_add to avoid confusion
  55114. + * with "put" usage in the kernel.
  55115. + * Change all references to tdb, TDB or Tunnel Descriptor Block to ips,
  55116. + * ipsec_sa or ipsec_sa.
  55117. + * Moved all the extension parsing functions to pfkey_v2_ext_process.c.
  55118. + *
  55119. + * Revision 1.105 2002/04/24 07:55:32 mcr
  55120. + * #include patches and Makefiles for post-reorg compilation.
  55121. + *
  55122. + * Revision 1.104 2002/04/24 07:36:34 mcr
  55123. + * Moved from ./klips/net/ipsec/pfkey_v2_parser.c,v
  55124. + *
  55125. + * Revision 1.103 2002/04/20 00:12:25 rgb
  55126. + * Added esp IV CBC attack fix, disabled.
  55127. + *
  55128. + * Revision 1.102 2002/03/08 01:15:17 mcr
  55129. + * put some internal structure only debug messages behind
  55130. + * && sysctl_ipsec_debug_verbose.
  55131. + *
  55132. + * Revision 1.101 2002/01/29 17:17:57 mcr
  55133. + * moved include of ipsec_param.h to after include of linux/kernel.h
  55134. + * otherwise, it seems that some option that is set in ipsec_param.h
  55135. + * screws up something subtle in the include path to kernel.h, and
  55136. + * it complains on the snprintf() prototype.
  55137. + *
  55138. + * Revision 1.100 2002/01/29 04:00:54 mcr
  55139. + * more excise of kversions.h header.
  55140. + *
  55141. + * Revision 1.99 2002/01/29 02:13:19 mcr
  55142. + * introduction of ipsec_kversion.h means that include of
  55143. + * ipsec_param.h must preceed any decisions about what files to
  55144. + * include to deal with differences in kernel source.
  55145. + *
  55146. + * Revision 1.98 2002/01/12 02:57:57 mcr
  55147. + * first regression test causes acquire messages to be lost
  55148. + * 100% of the time. This is to help testing of pluto.
  55149. + *
  55150. + * Revision 1.97 2001/11/26 09:23:52 rgb
  55151. + * Merge MCR's ipsec_sa, eroute, proc and struct lifetime changes.
  55152. + *
  55153. + * Revision 1.93.2.4 2001/10/23 04:20:27 mcr
  55154. + * parity was forced on wrong structure! prototypes help here.
  55155. + *
  55156. + * Revision 1.93.2.3 2001/10/22 21:14:59 mcr
  55157. + * include des.h, removed phony prototypes and fixed calling
  55158. + * conventions to match real prototypes.
  55159. + *
  55160. + * Revision 1.93.2.2 2001/10/15 05:39:03 mcr
  55161. + * %08lx is not the right format for u32. Use %08x. 64-bit safe? ha.
  55162. + *
  55163. + * Revision 1.93.2.1 2001/09/25 02:30:14 mcr
  55164. + * struct tdb -> struct ipsec_sa.
  55165. + * use new lifetime structure. common format routines for debug.
  55166. + *
  55167. + * Revision 1.96 2001/11/06 20:47:54 rgb
  55168. + * Fixed user context call to ipsec_dev_start_xmit() bug. Call
  55169. + * dev_queue_xmit() instead.
  55170. + *
  55171. + * Revision 1.95 2001/11/06 19:47:46 rgb
  55172. + * Added packet parameter to lifetime and comb structures.
  55173. + *
  55174. + * Revision 1.94 2001/10/18 04:45:23 rgb
  55175. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  55176. + * lib/freeswan.h version macros moved to lib/kversions.h.
  55177. + * Other compiler directive cleanups.
  55178. + *
  55179. + * Revision 1.93 2001/09/20 15:32:59 rgb
  55180. + * Min/max cleanup.
  55181. + *
  55182. + * Revision 1.92 2001/09/19 16:35:48 rgb
  55183. + * PF_KEY ident fix for getspi from NetCelo (puttdb duplication).
  55184. + *
  55185. + * Revision 1.91 2001/09/15 16:24:06 rgb
  55186. + * Re-inject first and last HOLD packet when an eroute REPLACE is done.
  55187. + *
  55188. + * Revision 1.90 2001/09/14 16:58:38 rgb
  55189. + * Added support for storing the first and last packets through a HOLD.
  55190. + *
  55191. + * Revision 1.89 2001/09/08 21:14:07 rgb
  55192. + * Added pfkey ident extension support for ISAKMPd. (NetCelo)
  55193. + * Better state coherency (error management) between pf_key and IKE daemon.
  55194. + * (NetCelo)
  55195. + *
  55196. + * Revision 1.88 2001/08/27 19:42:44 rgb
  55197. + * Fix memory leak of encrypt and auth structs in pfkey register.
  55198. + *
  55199. + * Revision 1.87 2001/07/06 19:50:46 rgb
  55200. + * Removed unused debugging code.
  55201. + * Added inbound policy checking code for IPIP SAs.
  55202. + *
  55203. + * Revision 1.86 2001/06/20 06:26:04 rgb
  55204. + * Changed missing SA errors from EEXIST to ENOENT and added debug output
  55205. + * for already linked SAs.
  55206. + *
  55207. + * Revision 1.85 2001/06/15 04:57:02 rgb
  55208. + * Remove single error return condition check and check for all errors in
  55209. + * the case of a replace eroute delete operation. This means that
  55210. + * applications must expect to be deleting something before replacing it
  55211. + * and if nothing is found, complain.
  55212. + *
  55213. + * Revision 1.84 2001/06/14 19:35:12 rgb
  55214. + * Update copyright date.
  55215. + *
  55216. + * Revision 1.83 2001/06/12 00:03:19 rgb
  55217. + * Silence debug set/unset under normal conditions.
  55218. + *
  55219. + * Revision 1.82 2001/05/30 08:14:04 rgb
  55220. + * Removed vestiges of esp-null transforms.
  55221. + *
  55222. + * Revision 1.81 2001/05/27 06:12:12 rgb
  55223. + * Added structures for pid, packet count and last access time to eroute.
  55224. + * Added packet count to beginning of /proc/net/ipsec_eroute.
  55225. + *
  55226. + * Revision 1.80 2001/05/03 19:43:59 rgb
  55227. + * Check error return codes for all build function calls.
  55228. + * Standardise on SENDERR() macro.
  55229. + *
  55230. + * Revision 1.79 2001/04/20 21:09:16 rgb
  55231. + * Cleaned up fixed tdbwipes.
  55232. + * Free pfkey_reply and clean up extensions_reply for grpsa, addflow and
  55233. + * delflow (Per Cederqvist) plugging memleaks.
  55234. + *
  55235. + * Revision 1.78 2001/04/19 19:02:39 rgb
  55236. + * Fixed extr.tdb freeing, stealing it for getspi, update and add.
  55237. + * Refined a couple of spinlocks, fixed the one in update.
  55238. + *
  55239. + * Revision 1.77 2001/04/18 20:26:16 rgb
  55240. + * Wipe/free eroute and both tdbs from extr at end of pfkey_msg_interp()
  55241. + * instead of inside each message type parser. This fixes two memleaks.
  55242. + *
  55243. + * Revision 1.76 2001/04/17 23:51:18 rgb
  55244. + * Quiet down pfkey_x_debug_process().
  55245. + *
  55246. + * Revision 1.75 2001/03/29 01:55:05 rgb
  55247. + * Fixed pfkey key init memleak.
  55248. + * Fixed pfkey encryption key debug output.
  55249. + *
  55250. + * Revision 1.74 2001/03/27 05:29:14 rgb
  55251. + * Debug output cleanup/silencing.
  55252. + *
  55253. + * Revision 1.73 2001/02/28 05:03:28 rgb
  55254. + * Clean up and rationalise startup messages.
  55255. + *
  55256. + * Revision 1.72 2001/02/27 22:24:56 rgb
  55257. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  55258. + * Check for satoa() return codes.
  55259. + *
  55260. + * Revision 1.71 2001/02/27 06:59:30 rgb
  55261. + * Added satype2name() conversions most places satype is debug printed.
  55262. + *
  55263. + * Revision 1.70 2001/02/26 22:37:08 rgb
  55264. + * Fixed 'unknown proto' INT bug in new code.
  55265. + * Added satype to protocol debugging instrumentation.
  55266. + *
  55267. + * Revision 1.69 2001/02/26 19:57:51 rgb
  55268. + * Re-formatted debug output (split lines, consistent spacing).
  55269. + * Fixed as yet undetected FLUSH bug which called ipsec_tdbcleanup()
  55270. + * with an satype instead of proto.
  55271. + * Checked for satype consistency and fixed minor bugs.
  55272. + * Fixed undetected ungrpspi bug that tried to upmsg a second tdb.
  55273. + * Check for satype sanity in pfkey_expire().
  55274. + * Added satype sanity check to addflow.
  55275. + *
  55276. + * Revision 1.68 2001/02/12 23:14:40 rgb
  55277. + * Remove double spin lock in pfkey_expire().
  55278. + *
  55279. + * Revision 1.67 2001/01/31 19:23:40 rgb
  55280. + * Fixed double-unlock bug introduced by grpsa upmsg (found by Lars Heete).
  55281. + *
  55282. + * Revision 1.66 2001/01/29 22:20:04 rgb
  55283. + * Fix minor add upmsg lifetime bug.
  55284. + *
  55285. + * Revision 1.65 2001/01/24 06:12:33 rgb
  55286. + * Fixed address extension compile bugs just introduced.
  55287. + *
  55288. + * Revision 1.64 2001/01/24 00:31:15 rgb
  55289. + * Added upmsg for addflow/delflow.
  55290. + *
  55291. + * Revision 1.63 2001/01/23 22:02:55 rgb
  55292. + * Added upmsg to x_grpsa.
  55293. + * Fixed lifetimes extentions to add/update/get upmsg.
  55294. + *
  55295. + * Revision 1.62 2000/11/30 21:47:51 rgb
  55296. + * Fix error return bug after returning from pfkey_tdb_init().
  55297. + *
  55298. + * Revision 1.61 2000/11/17 18:10:29 rgb
  55299. + * Fixed bugs mostly relating to spirange, to treat all spi variables as
  55300. + * network byte order since this is the way PF_KEYv2 stored spis.
  55301. + *
  55302. + * Revision 1.60 2000/11/06 04:34:53 rgb
  55303. + * Changed non-exported functions to DEBUG_NO_STATIC.
  55304. + * Add Svenning's adaptive content compression.
  55305. + * Ditched spin_lock_irqsave in favour of spin_lock/_bh.
  55306. + * Fixed double unlock bug (Svenning).
  55307. + * Fixed pfkey_msg uninitialized bug in pfkey_{expire,acquire}().
  55308. + * Fixed incorrect extension type (prop) in pfkey)acquire().
  55309. + *
  55310. + * Revision 1.59 2000/10/11 15:25:12 rgb
  55311. + * Fixed IPCOMP disabled compile bug.
  55312. + *
  55313. + * Revision 1.58 2000/10/11 14:54:03 rgb
  55314. + * Fixed pfkey_acquire() satype to SADB_SATYPE_ESP and removed pfkey
  55315. + * protocol violations of setting pfkey_address_build() protocol parameter
  55316. + * to non-zero except in the case of pfkey_acquire().
  55317. + *
  55318. + * Revision 1.57 2000/10/10 20:10:18 rgb
  55319. + * Added support for debug_ipcomp and debug_verbose to klipsdebug.
  55320. + *
  55321. + * Revision 1.56 2000/10/06 20:24:36 rgb
  55322. + * Fixes to pfkey_acquire to initialize extensions[] and use correct
  55323. + * ipproto.
  55324. + *
  55325. + * Revision 1.55 2000/10/03 03:20:57 rgb
  55326. + * Added brackets to get a?b:c scope right for pfkey_register reply.
  55327. + *
  55328. + * Revision 1.54 2000/09/29 19:49:30 rgb
  55329. + * As-yet-unused-bits cleanup.
  55330. + *
  55331. + * Revision 1.53 2000/09/28 00:35:45 rgb
  55332. + * Padded SATYPE printout in pfkey_register for vertical alignment.
  55333. + *
  55334. + * Revision 1.52 2000/09/20 16:21:58 rgb
  55335. + * Cleaned up ident string alloc/free.
  55336. + *
  55337. + * Revision 1.51 2000/09/20 04:04:20 rgb
  55338. + * Changed static functions to DEBUG_NO_STATIC to reveal function names in
  55339. + * oopsen.
  55340. + *
  55341. + * Revision 1.50 2000/09/16 01:10:53 rgb
  55342. + * Fixed unused var warning with debug off.
  55343. + *
  55344. + * Revision 1.49 2000/09/15 11:37:02 rgb
  55345. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  55346. + * IPCOMP zlib deflate code.
  55347. + *
  55348. + * Revision 1.48 2000/09/15 04:57:57 rgb
  55349. + * Cleaned up existing IPCOMP code before svenning addition.
  55350. + * Initialize pfkey_reply and extensions_reply in case of early error in
  55351. + * message parsing functions (thanks Kai!).
  55352. + *
  55353. + * Revision 1.47 2000/09/13 08:02:56 rgb
  55354. + * Added KMd registration notification.
  55355. + *
  55356. + * Revision 1.46 2000/09/12 22:35:36 rgb
  55357. + * Restructured to remove unused extensions from CLEARFLOW messages.
  55358. + *
  55359. + * Revision 1.45 2000/09/12 03:24:23 rgb
  55360. + * Converted #if0 debugs to sysctl.
  55361. + *
  55362. + * Revision 1.44 2000/09/09 06:38:39 rgb
  55363. + * Correct SADB message type for update, add and delete.
  55364. + *
  55365. + * Revision 1.43 2000/09/08 19:19:56 rgb
  55366. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  55367. + * Removed all references to CONFIG_IPSEC_PFKEYv2.
  55368. + * Put in sanity checks in most msg type parsers to catch invalid satypes
  55369. + * and empty socket lists.
  55370. + * Moved spin-locks in pfkey_get_parse() to simplify.
  55371. + * Added pfkey_acquire().
  55372. + * Added upwards messages to update, add, delete, acquire_parse,
  55373. + * expire_parse and flush.
  55374. + * Fix pfkey_prop_build() parameter to be only single indirection.
  55375. + * Changed all replies to use pfkey_reply.
  55376. + * Check return code on puttdb() and deltdbchain() in getspi, update,
  55377. + * add, delete.
  55378. + * Fixed up all pfkey replies to open and registered sockets.
  55379. + *
  55380. + * Revision 1.42 2000/09/01 18:50:26 rgb
  55381. + * Added a supported algorithms array lists, one per satype and registered
  55382. + * existing algorithms.
  55383. + * Fixed pfkey_list_{insert,remove}_{socket,support}() to allow change to
  55384. + * list.
  55385. + * Only send pfkey_expire() messages to sockets registered for that satype.
  55386. + * Added reply to pfkey_getspi_parse().
  55387. + * Added reply to pfkey_get_parse().
  55388. + * Fixed debug output label bug in pfkey_lifetime_process().
  55389. + * Cleaned up pfkey_sa_process a little.
  55390. + * Moved pfkey_safe_build() above message type parsers to make it available
  55391. + * for creating replies.
  55392. + * Added comments for future work in pfkey_acquire_parse().
  55393. + * Fleshed out guts of pfkey_register_parse().
  55394. + *
  55395. + * Revision 1.41 2000/08/24 16:58:11 rgb
  55396. + * Fixed key debugging variables.
  55397. + * Fixed error return code for a failed search.
  55398. + * Changed order of pfkey_get operations.
  55399. + *
  55400. + * Revision 1.40 2000/08/21 16:32:27 rgb
  55401. + * Re-formatted for cosmetic consistency and readability.
  55402. + *
  55403. + * Revision 1.39 2000/08/20 21:38:57 rgb
  55404. + * Bugfixes to as-yet-unused pfkey_update_parse() and
  55405. + * pfkey_register_parse(). (Momchil)
  55406. + * Added functions pfkey_safe_build(), pfkey_expire() and
  55407. + * pfkey_build_reply(). (Momchil)
  55408. + * Added a pfkey_reply parameter to pfkey_msg_interp(). (Momchil)
  55409. + *
  55410. + * Revision 1.38 2000/08/18 21:30:41 rgb
  55411. + * Purged all tdb_spi, tdb_proto and tdb_dst macros. They are unclear.
  55412. + *
  55413. + * Revision 1.37 2000/08/18 18:18:02 rgb
  55414. + * Cosmetic and descriptive changes made to debug test.
  55415. + * getspi and update fixes from Momchil.
  55416. + *
  55417. + * Revision 1.36 2000/08/15 15:41:55 rgb
  55418. + * Fixed the (as yet unused and untested) pfkey_getspi() routine.
  55419. + *
  55420. + * Revision 1.35 2000/08/01 14:51:52 rgb
  55421. + * Removed _all_ remaining traces of DES.
  55422. + *
  55423. + * Revision 1.34 2000/07/28 14:58:32 rgb
  55424. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  55425. + *
  55426. + * Revision 1.33 2000/06/28 05:50:11 rgb
  55427. + * Actually set iv_bits.
  55428. + *
  55429. + * Revision 1.32 2000/05/30 18:36:56 rgb
  55430. + * Fix AH auth hash setup bug. This breaks interop with previous PF_KEY
  55431. + * FreeS/WAN, but fixes interop with other implementations.
  55432. + *
  55433. + * Revision 1.31 2000/03/16 14:05:48 rgb
  55434. + * Fixed brace scope preventing non-debug compile.
  55435. + * Added null parameter check for pfkey_x_debug().
  55436. + *
  55437. + * Revision 1.30 2000/01/22 23:21:13 rgb
  55438. + * Use new function satype2proto().
  55439. + *
  55440. + * Revision 1.29 2000/01/22 08:40:21 rgb
  55441. + * Invert condition to known value to avoid AF_INET6 in 2.0.36.
  55442. + *
  55443. + * Revision 1.28 2000/01/22 07:58:57 rgb
  55444. + * Fixed REPLACEFLOW bug, missing braces around KLIPS_PRINT *and* SENDERR.
  55445. + *
  55446. + * Revision 1.27 2000/01/22 03:48:01 rgb
  55447. + * Added extr pointer component debugging.
  55448. + *
  55449. + * Revision 1.26 2000/01/21 09:41:25 rgb
  55450. + * Changed a (void*) to (char*) cast to do proper pointer math.
  55451. + * Don't call tdbwipe if tdb2 is NULL.
  55452. + *
  55453. + * Revision 1.25 2000/01/21 06:21:01 rgb
  55454. + * Added address cases for eroute flows.
  55455. + * Tidied up compiler directive indentation for readability.
  55456. + * Added ictx,octx vars for simplification.
  55457. + * Added macros for HMAC padding magic numbers.
  55458. + * Converted from double tdb arguments to one structure (extr)
  55459. + * containing pointers to all temporary information structures
  55460. + * and checking for valid arguments to all ext processors and
  55461. + * msg type parsers.
  55462. + * Added spiungrp'ing.
  55463. + * Added klipsdebug switching capability.
  55464. + * Removed sa_process() check for zero protocol.
  55465. + * Added address case for DST2 for grouping.
  55466. + * Added/changed minor debugging instrumentation.
  55467. + * Fixed spigrp for single said, ungrouping case.
  55468. + * Added code to parse addflow and delflow messages.
  55469. + * Removed redundant statements duplicating tdbwipe() functionality
  55470. + * and causing double kfrees.
  55471. + * Permit addflow to have a protocol of 0.
  55472. + *
  55473. + * Revision 1.24 1999/12/09 23:23:00 rgb
  55474. + * Added check to pfkey_sa_process() to do eroutes.
  55475. + * Converted to DIVUP() macro.
  55476. + * Converted if() to switch() in pfkey_register_parse().
  55477. + * Use new pfkey_extensions_init() instead of memset().
  55478. + *
  55479. + * Revision 1.23 1999/12/01 22:18:13 rgb
  55480. + * Preset minspi and maxspi values in case and spirange extension is not
  55481. + * included and check for the presence of an spirange extension before
  55482. + * using it. Initialise tdb_sastate to LARVAL.
  55483. + * Fixed debugging output typo.
  55484. + * Fixed authentication context initialisation bugs (4 places).
  55485. + *
  55486. + * Revision 1.22 1999/11/27 11:53:08 rgb
  55487. + * Moved pfkey_msg_parse prototype to pfkey.h
  55488. + * Moved exts_permitted/required prototype to pfkey.h.
  55489. + * Moved sadb_satype2proto protocol lookup table to lib/pfkey_v2_parse.c.
  55490. + * Deleted SADB_X_EXT_SA2 code from pfkey_sa_process() since it will never
  55491. + * be called.
  55492. + * Moved protocol/algorithm checks to lib/pfkey_v2_parse.c
  55493. + * Debugging error messages added.
  55494. + * Enable lifetime_current checking.
  55495. + * Remove illegal requirement for SA extension to be present in an
  55496. + * originating GETSPI call.
  55497. + * Re-instate requirement for UPDATE or ADD message to be MATURE.
  55498. + * Add argument to pfkey_msg_parse() for direction.
  55499. + * Fixed IPIP dst address bug and purged redundant, leaky code.
  55500. + *
  55501. + * Revision 1.21 1999/11/24 05:24:20 rgb
  55502. + * hanged 'void*extensions' to 'struct sadb_ext*extensions'.
  55503. + * Fixed indention.
  55504. + * Ditched redundant replay check.
  55505. + * Fixed debug message text from 'parse' to 'process'.
  55506. + * Added more debug output.
  55507. + * Forgot to zero extensions array causing bug, fixed.
  55508. + *
  55509. + * Revision 1.20 1999/11/23 23:08:13 rgb
  55510. + * Move all common parsing code to lib/pfkey_v2_parse.c and rename
  55511. + * remaining bits to *_process. (PJO)
  55512. + * Add macros for dealing with alignment and rounding up more opaquely.
  55513. + * Use provided macro ADDRTOA_BUF instead of hardcoded value.
  55514. + * Sort out pfkey and freeswan headers, putting them in a library path.
  55515. + * Corrected a couple of bugs in as-yet-inactive code.
  55516. + *
  55517. + * Revision 1.19 1999/11/20 22:01:10 rgb
  55518. + * Add more descriptive error messages for non-zero reserved fields.
  55519. + * Add more descriptive error message for spirange parsing.
  55520. + * Start on supported extension parsing.
  55521. + * Start on register and get message parsing.
  55522. + *
  55523. + * Revision 1.18 1999/11/18 04:09:20 rgb
  55524. + * Replaced all kernel version macros to shorter, readable form.
  55525. + *
  55526. + * Revision 1.17 1999/11/17 15:53:41 rgb
  55527. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  55528. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  55529. + * klips/net/ipsec/Makefile.
  55530. + *
  55531. + * Revision 1.16 1999/10/26 16:57:43 rgb
  55532. + * Add shorter macros for compiler directives to visually clean-up.
  55533. + * Give ipv6 code meaningful compiler directive.
  55534. + * Add comments to other #if 0 debug code.
  55535. + * Remove unused *_bh_atomic() calls.
  55536. + * Fix mis-placed spinlock.
  55537. + *
  55538. + * Revision 1.15 1999/10/16 18:27:10 rgb
  55539. + * Clean-up unused cruft.
  55540. + * Fix-up lifetime_allocations_c and lifetime_addtime_c initialisations.
  55541. + *
  55542. + * Revision 1.14 1999/10/08 18:37:34 rgb
  55543. + * Fix end-of-line spacing to sate whining PHMs.
  55544. + *
  55545. + * Revision 1.13 1999/10/03 18:49:12 rgb
  55546. + * Spinlock fixes for 2.0.xx and 2.3.xx.
  55547. + *
  55548. + * Revision 1.12 1999/10/01 15:44:54 rgb
  55549. + * Move spinlock header include to 2.1> scope.
  55550. + *
  55551. + * Revision 1.11 1999/10/01 00:05:45 rgb
  55552. + * Added tdb structure locking.
  55553. + * Use 'jiffies' instead of do_get_timeofday().
  55554. + * Fix lifetime assignments.
  55555. + *
  55556. + * Revision 1.10 1999/09/21 15:24:45 rgb
  55557. + * Rework spirange code to save entropy and prevent endless loops.
  55558. + *
  55559. + * Revision 1.9 1999/09/16 12:10:21 rgb
  55560. + * Minor fixes to random spi selection for correctness and entropy conservation.
  55561. + *
  55562. + * Revision 1.8 1999/05/25 22:54:46 rgb
  55563. + * Fix comparison that should be an assignment in an if.
  55564. + *
  55565. + * Revision 1.7 1999/05/09 03:25:37 rgb
  55566. + * Fix bug introduced by 2.2 quick-and-dirty patch.
  55567. + *
  55568. + * Revision 1.6 1999/05/08 21:32:30 rgb
  55569. + * Fix error return reporting.
  55570. + *
  55571. + * Revision 1.5 1999/05/05 22:02:33 rgb
  55572. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  55573. + *
  55574. + * Revision 1.4 1999/04/29 15:22:40 rgb
  55575. + * Standardise an error return method.
  55576. + * Add debugging instrumentation.
  55577. + * Add check for existence of macros min/max.
  55578. + * Add extensions permitted/required in/out filters.
  55579. + * Add satype-to-protocol table.
  55580. + * Add a second tdb pointer to each parser to accomodate GRPSA.
  55581. + * Move AH & no_algo_set to GETSPI, UPDATE and ADD.
  55582. + * Add OOO window check.
  55583. + * Add support for IPPROTO_IPIP and hooks for IPPROTO_COMP.
  55584. + * Add timestamp to lifetime parse.
  55585. + * Fix address structure length checking bug.
  55586. + * Fix address structure allocation bug (forgot to kmalloc!).
  55587. + * Add checks for extension lengths.
  55588. + * Add checks for extension reserved illegal values.
  55589. + * Add check for spirange legal values.
  55590. + * Add an extension type for parsing a second satype, SA and
  55591. + * DST_ADDRESS.
  55592. + * Make changes to tdb_init() template to get pfkey_tdb_init(),
  55593. + * eliminating any mention of xformsw.
  55594. + * Implement getspi, update and grpsa (not tested).
  55595. + * Add stubs for as yet unimplemented message types.
  55596. + * Add table of message parsers to substitute for msg_parse switch.
  55597. + *
  55598. + * Revision 1.3 1999/04/15 17:58:07 rgb
  55599. + * Add RCSID labels.
  55600. + *
  55601. + * Revision 1.2 1999/04/15 15:37:26 rgb
  55602. + * Forward check changes from POST1_00 branch.
  55603. + *
  55604. + * Revision 1.1.2.1 1999/03/26 20:58:56 rgb
  55605. + * Add pfkeyv2 support to KLIPS.
  55606. + *
  55607. + * Local variables:
  55608. + * c-file-style: "linux"
  55609. + * End:
  55610. + *
  55611. + */
  55612. --- /dev/null Tue Mar 11 13:02:56 2003
  55613. +++ linux/net/ipsec/prng.c Mon Feb 9 13:51:03 2004
  55614. @@ -0,0 +1,201 @@
  55615. +/*
  55616. + * crypto-class pseudorandom number generator
  55617. + * currently uses same algorithm as RC4(TM), from Schneier 2nd ed p397
  55618. + * Copyright (C) 2002 Henry Spencer.
  55619. + *
  55620. + * This library is free software; you can redistribute it and/or modify it
  55621. + * under the terms of the GNU Library General Public License as published by
  55622. + * the Free Software Foundation; either version 2 of the License, or (at your
  55623. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  55624. + *
  55625. + * This library is distributed in the hope that it will be useful, but
  55626. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  55627. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  55628. + * License for more details.
  55629. + *
  55630. + * RCSID $Id: prng.c,v 1.7 2004/07/10 07:48:36 mcr Exp $
  55631. + */
  55632. +#include "openswan.h"
  55633. +
  55634. +/*
  55635. + - prng_init - initialize PRNG from a key
  55636. + */
  55637. +void
  55638. +prng_init(prng, key, keylen)
  55639. +struct prng *prng;
  55640. +const unsigned char *key;
  55641. +size_t keylen;
  55642. +{
  55643. + unsigned char k[256];
  55644. + int i, j;
  55645. + unsigned const char *p;
  55646. + unsigned const char *keyend = key + keylen;
  55647. + unsigned char t;
  55648. +
  55649. + for (i = 0; i <= 255; i++)
  55650. + prng->sbox[i] = i;
  55651. + p = key;
  55652. + for (i = 0; i <= 255; i++) {
  55653. + k[i] = *p++;
  55654. + if (p >= keyend)
  55655. + p = key;
  55656. + }
  55657. + j = 0;
  55658. + for (i = 0; i <= 255; i++) {
  55659. + j = (j + prng->sbox[i] + k[i]) & 0xff;
  55660. + t = prng->sbox[i];
  55661. + prng->sbox[i] = prng->sbox[j];
  55662. + prng->sbox[j] = t;
  55663. + k[i] = 0; /* clear out key memory */
  55664. + }
  55665. + prng->i = 0;
  55666. + prng->j = 0;
  55667. + prng->count = 0;
  55668. +}
  55669. +
  55670. +/*
  55671. + - prng_bytes - get some pseudorandom bytes from PRNG
  55672. + */
  55673. +void
  55674. +prng_bytes(prng, dst, dstlen)
  55675. +struct prng *prng;
  55676. +unsigned char *dst;
  55677. +size_t dstlen;
  55678. +{
  55679. + int i, j, t;
  55680. + unsigned char *p = dst;
  55681. + size_t remain = dstlen;
  55682. +# define MAX 4000000000ul
  55683. +
  55684. + while (remain > 0) {
  55685. + i = (prng->i + 1) & 0xff;
  55686. + prng->i = i;
  55687. + j = (prng->j + prng->sbox[i]) & 0xff;
  55688. + prng->j = j;
  55689. + t = prng->sbox[i];
  55690. + prng->sbox[i] = prng->sbox[j];
  55691. + prng->sbox[j] = t;
  55692. + t = (t + prng->sbox[i]) & 0xff;
  55693. + *p++ = prng->sbox[t];
  55694. + remain--;
  55695. + }
  55696. + if (prng->count < MAX - dstlen)
  55697. + prng->count += dstlen;
  55698. + else
  55699. + prng->count = MAX;
  55700. +}
  55701. +
  55702. +/*
  55703. + - prnt_count - how many bytes have been extracted from PRNG so far?
  55704. + */
  55705. +unsigned long
  55706. +prng_count(prng)
  55707. +struct prng *prng;
  55708. +{
  55709. + return prng->count;
  55710. +}
  55711. +
  55712. +/*
  55713. + - prng_final - clear out PRNG to ensure nothing left in memory
  55714. + */
  55715. +void
  55716. +prng_final(prng)
  55717. +struct prng *prng;
  55718. +{
  55719. + int i;
  55720. +
  55721. + for (i = 0; i <= 255; i++)
  55722. + prng->sbox[i] = 0;
  55723. + prng->i = 0;
  55724. + prng->j = 0;
  55725. + prng->count = 0; /* just for good measure */
  55726. +}
  55727. +
  55728. +
  55729. +
  55730. +#ifdef PRNG_MAIN
  55731. +
  55732. +#include <stdio.h>
  55733. +
  55734. +void regress();
  55735. +
  55736. +int
  55737. +main(argc, argv)
  55738. +int argc;
  55739. +char *argv[];
  55740. +{
  55741. + struct prng pr;
  55742. + unsigned char buf[100];
  55743. + unsigned char *p;
  55744. + size_t n;
  55745. +
  55746. + if (argc < 2) {
  55747. + fprintf(stderr, "Usage: %s {key|-r}\n", argv[0]);
  55748. + exit(2);
  55749. + }
  55750. +
  55751. + if (strcmp(argv[1], "-r") == 0) {
  55752. + regress();
  55753. + fprintf(stderr, "regress() returned?!?\n");
  55754. + exit(1);
  55755. + }
  55756. +
  55757. + prng_init(&pr, argv[1], strlen(argv[1]));
  55758. + prng_bytes(&pr, buf, 32);
  55759. + printf("0x");
  55760. + for (p = buf, n = 32; n > 0; p++, n--)
  55761. + printf("%02x", *p);
  55762. + printf("\n%lu bytes\n", prng_count(&pr));
  55763. + prng_final(&pr);
  55764. + exit(0);
  55765. +}
  55766. +
  55767. +void
  55768. +regress()
  55769. +{
  55770. + struct prng pr;
  55771. + unsigned char buf[100];
  55772. + unsigned char *p;
  55773. + size_t n;
  55774. + /* somewhat non-random sample key */
  55775. + unsigned char key[] = "here we go gathering nuts in May";
  55776. + /* first thirty bytes of output from that key */
  55777. + unsigned char good[] = "\x3f\x02\x8e\x4a\x2a\xea\x23\x18\x92\x7c"
  55778. + "\x09\x52\x83\x61\xaa\x26\xce\xbb\x9d\x71"
  55779. + "\x71\xe5\x10\x22\xaf\x60\x54\x8d\x5b\x28";
  55780. + int nzero, none;
  55781. + int show = 0;
  55782. +
  55783. + prng_init(&pr, key, strlen(key));
  55784. + prng_bytes(&pr, buf, sizeof(buf));
  55785. + for (p = buf, n = sizeof(buf); n > 0; p++, n--) {
  55786. + if (*p == 0)
  55787. + nzero++;
  55788. + if (*p == 255)
  55789. + none++;
  55790. + }
  55791. + if (nzero > 3 || none > 3) {
  55792. + fprintf(stderr, "suspiciously non-random output!\n");
  55793. + show = 1;
  55794. + }
  55795. + if (memcmp(buf, good, strlen(good)) != 0) {
  55796. + fprintf(stderr, "incorrect output!\n");
  55797. + show = 1;
  55798. + }
  55799. + if (show) {
  55800. + fprintf(stderr, "0x");
  55801. + for (p = buf, n = sizeof(buf); n > 0; p++, n--)
  55802. + fprintf(stderr, "%02x", *p);
  55803. + fprintf(stderr, "\n");
  55804. + exit(1);
  55805. + }
  55806. + if (prng_count(&pr) != sizeof(buf)) {
  55807. + fprintf(stderr, "got %u bytes, but count is %lu\n",
  55808. + sizeof(buf), prng_count(&pr));
  55809. + exit(1);
  55810. + }
  55811. + prng_final(&pr);
  55812. + exit(0);
  55813. +}
  55814. +
  55815. +#endif /* PRNG_MAIN */
  55816. --- /dev/null Tue Mar 11 13:02:56 2003
  55817. +++ linux/net/ipsec/radij.c Mon Feb 9 13:51:03 2004
  55818. @@ -0,0 +1,1237 @@
  55819. +char radij_c_version[] = "RCSID $Id: radij.c,v 1.48.2.1 2006/10/06 21:39:27 paul Exp $";
  55820. +
  55821. +/*
  55822. + * This file is defived from ${SRC}/sys/net/radix.c of BSD 4.4lite
  55823. + *
  55824. + * Variable and procedure names have been modified so that they don't
  55825. + * conflict with the original BSD code, as a small number of modifications
  55826. + * have been introduced and we may want to reuse this code in BSD.
  55827. + *
  55828. + * The `j' in `radij' is pronounced as a voiceless guttural (like a Greek
  55829. + * chi or a German ch sound (as `doch', not as in `milch'), or even a
  55830. + * spanish j as in Juan. It is not as far back in the throat like
  55831. + * the corresponding Hebrew sound, nor is it a soft breath like the English h.
  55832. + * It has nothing to do with the Dutch ij sound.
  55833. + *
  55834. + * Here is the appropriate copyright notice:
  55835. + */
  55836. +
  55837. +/*
  55838. + * Copyright (c) 1988, 1989, 1993
  55839. + * The Regents of the University of California. All rights reserved.
  55840. + *
  55841. + * Redistribution and use in source and binary forms, with or without
  55842. + * modification, are permitted provided that the following conditions
  55843. + * are met:
  55844. + * 1. Redistributions of source code must retain the above copyright
  55845. + * notice, this list of conditions and the following disclaimer.
  55846. + * 2. Redistributions in binary form must reproduce the above copyright
  55847. + * notice, this list of conditions and the following disclaimer in the
  55848. + * documentation and/or other materials provided with the distribution.
  55849. + * 3. All advertising materials mentioning features or use of this software
  55850. + * must display the following acknowledgement:
  55851. + * This product includes software developed by the University of
  55852. + * California, Berkeley and its contributors.
  55853. + * 4. Neither the name of the University nor the names of its contributors
  55854. + * may be used to endorse or promote products derived from this software
  55855. + * without specific prior written permission.
  55856. + *
  55857. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  55858. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  55859. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  55860. + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  55861. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  55862. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  55863. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55864. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  55865. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  55866. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  55867. + * SUCH DAMAGE.
  55868. + *
  55869. + * @(#)radix.c 8.2 (Berkeley) 1/4/94
  55870. + */
  55871. +
  55872. +/*
  55873. + * Routines to build and maintain radix trees for routing lookups.
  55874. + */
  55875. +
  55876. +#ifndef AUTOCONF_INCLUDED
  55877. +#include <linux/config.h>
  55878. +#endif
  55879. +#include <linux/version.h>
  55880. +#include <linux/kernel.h> /* printk() */
  55881. +
  55882. +#include "openswan/ipsec_param.h"
  55883. +
  55884. +#ifdef MALLOC_SLAB
  55885. +# include <linux/slab.h> /* kmalloc() */
  55886. +#else /* MALLOC_SLAB */
  55887. +# include <linux/malloc.h> /* kmalloc() */
  55888. +#endif /* MALLOC_SLAB */
  55889. +#include <linux/errno.h> /* error codes */
  55890. +#include <linux/types.h> /* size_t */
  55891. +#include <linux/interrupt.h> /* mark_bh */
  55892. +
  55893. +#include <linux/netdevice.h> /* struct device, and other headers */
  55894. +#include <linux/etherdevice.h> /* eth_type_trans */
  55895. +#include <linux/ip.h> /* struct iphdr */
  55896. +#include <linux/skbuff.h>
  55897. +#ifdef NET_21
  55898. +# include <linux/in6.h>
  55899. +#endif /* NET_21 */
  55900. +
  55901. +#include <net/ip.h>
  55902. +
  55903. +#include <openswan.h>
  55904. +
  55905. +#include "openswan/radij.h"
  55906. +#include "openswan/ipsec_encap.h"
  55907. +#include "openswan/ipsec_radij.h"
  55908. +
  55909. +int maj_keylen;
  55910. +struct radij_mask *rj_mkfreelist;
  55911. +struct radij_node_head *mask_rjhead;
  55912. +static int gotOddMasks;
  55913. +static char *maskedKey;
  55914. +static char *rj_zeroes, *rj_ones;
  55915. +
  55916. +#define rj_masktop (mask_rjhead->rnh_treetop)
  55917. +#ifdef Bcmp
  55918. +# undef Bcmp
  55919. +#endif /* Bcmp */
  55920. +#define Bcmp(a, b, l) (l == 0 ? 0 : memcmp((caddr_t)(b), (caddr_t)(a), (size_t)l))
  55921. +/*
  55922. + * The data structure for the keys is a radix tree with one way
  55923. + * branching removed. The index rj_b at an internal node n represents a bit
  55924. + * position to be tested. The tree is arranged so that all descendants
  55925. + * of a node n have keys whose bits all agree up to position rj_b - 1.
  55926. + * (We say the index of n is rj_b.)
  55927. + *
  55928. + * There is at least one descendant which has a one bit at position rj_b,
  55929. + * and at least one with a zero there.
  55930. + *
  55931. + * A route is determined by a pair of key and mask. We require that the
  55932. + * bit-wise logical and of the key and mask to be the key.
  55933. + * We define the index of a route to associated with the mask to be
  55934. + * the first bit number in the mask where 0 occurs (with bit number 0
  55935. + * representing the highest order bit).
  55936. + *
  55937. + * We say a mask is normal if every bit is 0, past the index of the mask.
  55938. + * If a node n has a descendant (k, m) with index(m) == index(n) == rj_b,
  55939. + * and m is a normal mask, then the route applies to every descendant of n.
  55940. + * If the index(m) < rj_b, this implies the trailing last few bits of k
  55941. + * before bit b are all 0, (and hence consequently true of every descendant
  55942. + * of n), so the route applies to all descendants of the node as well.
  55943. + *
  55944. + * The present version of the code makes no use of normal routes,
  55945. + * but similar logic shows that a non-normal mask m such that
  55946. + * index(m) <= index(n) could potentially apply to many children of n.
  55947. + * Thus, for each non-host route, we attach its mask to a list at an internal
  55948. + * node as high in the tree as we can go.
  55949. + */
  55950. +
  55951. +struct radij_node *
  55952. +rj_search(v_arg, head)
  55953. + void *v_arg;
  55954. + struct radij_node *head;
  55955. +{
  55956. + register struct radij_node *x;
  55957. + register caddr_t v;
  55958. +
  55959. + for (x = head, v = v_arg; x->rj_b >= 0;) {
  55960. + if (x->rj_bmask & v[x->rj_off])
  55961. + x = x->rj_r;
  55962. + else
  55963. + x = x->rj_l;
  55964. + }
  55965. + return (x);
  55966. +};
  55967. +
  55968. +struct radij_node *
  55969. +rj_search_m(v_arg, head, m_arg)
  55970. + struct radij_node *head;
  55971. + void *v_arg, *m_arg;
  55972. +{
  55973. + register struct radij_node *x;
  55974. + register caddr_t v = v_arg, m = m_arg;
  55975. +
  55976. + for (x = head; x->rj_b >= 0;) {
  55977. + if ((x->rj_bmask & m[x->rj_off]) &&
  55978. + (x->rj_bmask & v[x->rj_off]))
  55979. + x = x->rj_r;
  55980. + else
  55981. + x = x->rj_l;
  55982. + }
  55983. + return x;
  55984. +};
  55985. +
  55986. +int
  55987. +rj_refines(m_arg, n_arg)
  55988. + void *m_arg, *n_arg;
  55989. +{
  55990. + register caddr_t m = m_arg, n = n_arg;
  55991. + register caddr_t lim, lim2 = lim = n + *(u_char *)n;
  55992. + int longer = (*(u_char *)n++) - (int)(*(u_char *)m++);
  55993. + int masks_are_equal = 1;
  55994. +
  55995. + if (longer > 0)
  55996. + lim -= longer;
  55997. + while (n < lim) {
  55998. + if (*n & ~(*m))
  55999. + return 0;
  56000. + if (*n++ != *m++)
  56001. + masks_are_equal = 0;
  56002. +
  56003. + }
  56004. + while (n < lim2)
  56005. + if (*n++)
  56006. + return 0;
  56007. + if (masks_are_equal && (longer < 0))
  56008. + for (lim2 = m - longer; m < lim2; )
  56009. + if (*m++)
  56010. + return 1;
  56011. + return (!masks_are_equal);
  56012. +}
  56013. +
  56014. +
  56015. +struct radij_node *
  56016. +rj_match(v_arg, head)
  56017. + void *v_arg;
  56018. + struct radij_node_head *head;
  56019. +{
  56020. + caddr_t v = v_arg;
  56021. + register struct radij_node *t = head->rnh_treetop, *x;
  56022. + register caddr_t cp = v, cp2, cp3;
  56023. + caddr_t cplim, mstart;
  56024. + struct radij_node *saved_t, *top = t;
  56025. + int off = t->rj_off, vlen = *(u_char *)cp, matched_off;
  56026. +
  56027. + /*
  56028. + * Open code rj_search(v, top) to avoid overhead of extra
  56029. + * subroutine call.
  56030. + */
  56031. + for (; t->rj_b >= 0; ) {
  56032. + if (t->rj_bmask & cp[t->rj_off])
  56033. + t = t->rj_r;
  56034. + else
  56035. + t = t->rj_l;
  56036. + }
  56037. + /*
  56038. + * See if we match exactly as a host destination
  56039. + */
  56040. + KLIPS_PRINT(debug_radij,
  56041. + "klips_debug:rj_match: "
  56042. + "* See if we match exactly as a host destination\n");
  56043. +
  56044. + cp += off; cp2 = t->rj_key + off; cplim = v + vlen;
  56045. + for (; cp < cplim; cp++, cp2++)
  56046. + if (*cp != *cp2)
  56047. + goto on1;
  56048. + /*
  56049. + * This extra grot is in case we are explicitly asked
  56050. + * to look up the default. Ugh!
  56051. + */
  56052. + if ((t->rj_flags & RJF_ROOT) && t->rj_dupedkey)
  56053. + t = t->rj_dupedkey;
  56054. + return t;
  56055. +on1:
  56056. + matched_off = cp - v;
  56057. + saved_t = t;
  56058. + KLIPS_PRINT(debug_radij,
  56059. + "klips_debug:rj_match: "
  56060. + "** try to match a leaf, t=0p%p\n", t);
  56061. + do {
  56062. + if (t->rj_mask) {
  56063. + /*
  56064. + * Even if we don't match exactly as a hosts;
  56065. + * we may match if the leaf we wound up at is
  56066. + * a route to a net.
  56067. + */
  56068. + cp3 = matched_off + t->rj_mask;
  56069. + cp2 = matched_off + t->rj_key;
  56070. + for (; cp < cplim; cp++)
  56071. + if ((*cp2++ ^ *cp) & *cp3++)
  56072. + break;
  56073. + if (cp == cplim)
  56074. + return t;
  56075. + cp = matched_off + v;
  56076. + }
  56077. + } while ((t = t->rj_dupedkey));
  56078. + t = saved_t;
  56079. + /* start searching up the tree */
  56080. + KLIPS_PRINT(debug_radij,
  56081. + "klips_debug:rj_match: "
  56082. + "*** start searching up the tree, t=0p%p\n",
  56083. + t);
  56084. + do {
  56085. + register struct radij_mask *m;
  56086. +
  56087. + t = t->rj_p;
  56088. + KLIPS_PRINT(debug_radij,
  56089. + "klips_debug:rj_match: "
  56090. + "**** t=0p%p\n",
  56091. + t);
  56092. + if ((m = t->rj_mklist)) {
  56093. + /*
  56094. + * After doing measurements here, it may
  56095. + * turn out to be faster to open code
  56096. + * rj_search_m here instead of always
  56097. + * copying and masking.
  56098. + */
  56099. + /* off = min(t->rj_off, matched_off); */
  56100. + off = t->rj_off;
  56101. + if (matched_off < off)
  56102. + off = matched_off;
  56103. + mstart = maskedKey + off;
  56104. + do {
  56105. + cp2 = mstart;
  56106. + cp3 = m->rm_mask + off;
  56107. + KLIPS_PRINT(debug_radij,
  56108. + "klips_debug:rj_match: "
  56109. + "***** cp2=0p%p cp3=0p%p\n",
  56110. + cp2, cp3);
  56111. + for (cp = v + off; cp < cplim;)
  56112. + *cp2++ = *cp++ & *cp3++;
  56113. + x = rj_search(maskedKey, t);
  56114. + while (x && x->rj_mask != m->rm_mask)
  56115. + x = x->rj_dupedkey;
  56116. + if (x &&
  56117. + (Bcmp(mstart, x->rj_key + off,
  56118. + vlen - off) == 0))
  56119. + return x;
  56120. + } while ((m = m->rm_mklist));
  56121. + }
  56122. + } while (t != top);
  56123. + KLIPS_PRINT(debug_radij,
  56124. + "klips_debug:rj_match: "
  56125. + "***** not found.\n");
  56126. + return 0;
  56127. +};
  56128. +
  56129. +#ifdef RJ_DEBUG
  56130. +int rj_nodenum;
  56131. +struct radij_node *rj_clist;
  56132. +int rj_saveinfo;
  56133. +DEBUG_NO_STATIC void traverse(struct radij_node *);
  56134. +#ifdef RJ_DEBUG2
  56135. +int rj_debug = 1;
  56136. +#else
  56137. +int rj_debug = 0;
  56138. +#endif /* RJ_DEBUG2 */
  56139. +#endif /* RJ_DEBUG */
  56140. +
  56141. +struct radij_node *
  56142. +rj_newpair(v, b, nodes)
  56143. + void *v;
  56144. + int b;
  56145. + struct radij_node nodes[2];
  56146. +{
  56147. + register struct radij_node *tt = nodes, *t = tt + 1;
  56148. + t->rj_b = b; t->rj_bmask = 0x80 >> (b & 7);
  56149. + t->rj_l = tt; t->rj_off = b >> 3;
  56150. + tt->rj_b = -1; tt->rj_key = (caddr_t)v; tt->rj_p = t;
  56151. + tt->rj_flags = t->rj_flags = RJF_ACTIVE;
  56152. +#ifdef RJ_DEBUG
  56153. + tt->rj_info = rj_nodenum++; t->rj_info = rj_nodenum++;
  56154. + tt->rj_twin = t; tt->rj_ybro = rj_clist; rj_clist = tt;
  56155. +#endif /* RJ_DEBUG */
  56156. + return t;
  56157. +}
  56158. +
  56159. +struct radij_node *
  56160. +rj_insert(v_arg, head, dupentry, nodes)
  56161. + void *v_arg;
  56162. + struct radij_node_head *head;
  56163. + int *dupentry;
  56164. + struct radij_node nodes[2];
  56165. +{
  56166. + caddr_t v = v_arg;
  56167. + struct radij_node *top = head->rnh_treetop;
  56168. + int head_off = top->rj_off, vlen = (int)*((u_char *)v);
  56169. + register struct radij_node *t = rj_search(v_arg, top);
  56170. + register caddr_t cp = v + head_off;
  56171. + register int b;
  56172. + struct radij_node *tt;
  56173. + /*
  56174. + *find first bit at which v and t->rj_key differ
  56175. + */
  56176. + {
  56177. + register caddr_t cp2 = t->rj_key + head_off;
  56178. + register int cmp_res;
  56179. + caddr_t cplim = v + vlen;
  56180. +
  56181. + while (cp < cplim)
  56182. + if (*cp2++ != *cp++)
  56183. + goto on1;
  56184. + *dupentry = 1;
  56185. + return t;
  56186. +on1:
  56187. + *dupentry = 0;
  56188. + cmp_res = (cp[-1] ^ cp2[-1]) & 0xff;
  56189. + for (b = (cp - v) << 3; cmp_res; b--)
  56190. + cmp_res >>= 1;
  56191. + }
  56192. + {
  56193. + register struct radij_node *p, *x = top;
  56194. + cp = v;
  56195. + do {
  56196. + p = x;
  56197. + if (cp[x->rj_off] & x->rj_bmask)
  56198. + x = x->rj_r;
  56199. + else x = x->rj_l;
  56200. + } while (b > (unsigned) x->rj_b); /* x->rj_b < b && x->rj_b >= 0 */
  56201. +#ifdef RJ_DEBUG
  56202. + if (rj_debug)
  56203. + printk("klips_debug:rj_insert: Going In:\n"), traverse(p);
  56204. +#endif /* RJ_DEBUG */
  56205. + t = rj_newpair(v_arg, b, nodes); tt = t->rj_l;
  56206. + if ((cp[p->rj_off] & p->rj_bmask) == 0)
  56207. + p->rj_l = t;
  56208. + else
  56209. + p->rj_r = t;
  56210. + x->rj_p = t; t->rj_p = p; /* frees x, p as temp vars below */
  56211. + if ((cp[t->rj_off] & t->rj_bmask) == 0) {
  56212. + t->rj_r = x;
  56213. + } else {
  56214. + t->rj_r = tt; t->rj_l = x;
  56215. + }
  56216. +#ifdef RJ_DEBUG
  56217. + if (rj_debug)
  56218. + printk("klips_debug:rj_insert: Coming out:\n"), traverse(p);
  56219. +#endif /* RJ_DEBUG */
  56220. + }
  56221. + return (tt);
  56222. +}
  56223. +
  56224. +struct radij_node *
  56225. +rj_addmask(n_arg, search, skip)
  56226. + int search, skip;
  56227. + void *n_arg;
  56228. +{
  56229. + caddr_t netmask = (caddr_t)n_arg;
  56230. + register struct radij_node *x;
  56231. + register caddr_t cp, cplim;
  56232. + register int b, mlen, j;
  56233. + int maskduplicated;
  56234. +
  56235. + mlen = *(u_char *)netmask;
  56236. + if (search) {
  56237. + x = rj_search(netmask, rj_masktop);
  56238. + mlen = *(u_char *)netmask;
  56239. + if (Bcmp(netmask, x->rj_key, mlen) == 0)
  56240. + return (x);
  56241. + }
  56242. + R_Malloc(x, struct radij_node *, maj_keylen + 2 * sizeof (*x));
  56243. + if (x == 0)
  56244. + return (0);
  56245. + Bzero(x, maj_keylen + 2 * sizeof (*x));
  56246. + cp = (caddr_t)(x + 2);
  56247. + Bcopy(netmask, cp, mlen);
  56248. + netmask = cp;
  56249. + x = rj_insert(netmask, mask_rjhead, &maskduplicated, x);
  56250. + /*
  56251. + * Calculate index of mask.
  56252. + */
  56253. + cplim = netmask + mlen;
  56254. + for (cp = netmask + skip; cp < cplim; cp++)
  56255. + if (*(u_char *)cp != 0xff)
  56256. + break;
  56257. + b = (cp - netmask) << 3;
  56258. + if (cp != cplim) {
  56259. + if (*cp != 0) {
  56260. + gotOddMasks = 1;
  56261. + for (j = 0x80; j; b++, j >>= 1)
  56262. + if ((j & *cp) == 0)
  56263. + break;
  56264. + }
  56265. + }
  56266. + x->rj_b = -1 - b;
  56267. + return (x);
  56268. +}
  56269. +
  56270. +#if 0
  56271. +struct radij_node *
  56272. +#endif
  56273. +int
  56274. +rj_addroute(v_arg, n_arg, head, treenodes)
  56275. + void *v_arg, *n_arg;
  56276. + struct radij_node_head *head;
  56277. + struct radij_node treenodes[2];
  56278. +{
  56279. + caddr_t v = (caddr_t)v_arg, netmask = (caddr_t)n_arg;
  56280. + register struct radij_node *t, *x=NULL, *tt;
  56281. + struct radij_node *saved_tt, *top = head->rnh_treetop;
  56282. + short b = 0, b_leaf;
  56283. + int mlen, keyduplicated;
  56284. + caddr_t cplim;
  56285. + struct radij_mask *m, **mp;
  56286. +
  56287. + /*
  56288. + * In dealing with non-contiguous masks, there may be
  56289. + * many different routes which have the same mask.
  56290. + * We will find it useful to have a unique pointer to
  56291. + * the mask to speed avoiding duplicate references at
  56292. + * nodes and possibly save time in calculating indices.
  56293. + */
  56294. + if (netmask) {
  56295. + x = rj_search(netmask, rj_masktop);
  56296. + mlen = *(u_char *)netmask;
  56297. + if (Bcmp(netmask, x->rj_key, mlen) != 0) {
  56298. + x = rj_addmask(netmask, 0, top->rj_off);
  56299. + if (x == 0)
  56300. + return -ENOMEM; /* (0) rgb */
  56301. + }
  56302. + netmask = x->rj_key;
  56303. + b = -1 - x->rj_b;
  56304. + }
  56305. + /*
  56306. + * Deal with duplicated keys: attach node to previous instance
  56307. + */
  56308. + saved_tt = tt = rj_insert(v, head, &keyduplicated, treenodes);
  56309. +#ifdef RJ_DEBUG
  56310. + printk("addkey: duplicated: %d\n", keyduplicated);
  56311. +#endif
  56312. + if (keyduplicated) {
  56313. + do {
  56314. + if (tt->rj_mask == netmask)
  56315. + return -EEXIST; /* -ENXIO; (0) rgb */
  56316. + t = tt;
  56317. + if (netmask == 0 ||
  56318. + (tt->rj_mask && rj_refines(netmask, tt->rj_mask)))
  56319. + break;
  56320. + } while ((tt = tt->rj_dupedkey));
  56321. + /*
  56322. + * If the mask is not duplicated, we wouldn't
  56323. + * find it among possible duplicate key entries
  56324. + * anyway, so the above test doesn't hurt.
  56325. + *
  56326. + * We sort the masks for a duplicated key the same way as
  56327. + * in a masklist -- most specific to least specific.
  56328. + * This may require the unfortunate nuisance of relocating
  56329. + * the head of the list.
  56330. + */
  56331. + if (tt && t == saved_tt) {
  56332. + struct radij_node *xx = x;
  56333. + /* link in at head of list */
  56334. + (tt = treenodes)->rj_dupedkey = t;
  56335. + tt->rj_flags = t->rj_flags;
  56336. + tt->rj_p = x = t->rj_p;
  56337. + if (x->rj_l == t) x->rj_l = tt; else x->rj_r = tt;
  56338. + saved_tt = tt; x = xx;
  56339. + } else {
  56340. + (tt = treenodes)->rj_dupedkey = t->rj_dupedkey;
  56341. + t->rj_dupedkey = tt;
  56342. + }
  56343. +#ifdef RJ_DEBUG
  56344. + t=tt+1; tt->rj_info = rj_nodenum++; t->rj_info = rj_nodenum++;
  56345. + tt->rj_twin = t; tt->rj_ybro = rj_clist; rj_clist = tt;
  56346. +#endif /* RJ_DEBUG */
  56347. + t = saved_tt;
  56348. + tt->rj_key = (caddr_t) v;
  56349. + tt->rj_b = -1;
  56350. + tt->rj_flags = t->rj_flags & ~RJF_ROOT;
  56351. + }
  56352. + /*
  56353. + * Put mask in tree.
  56354. + */
  56355. + if (netmask) {
  56356. + tt->rj_mask = netmask;
  56357. + tt->rj_b = x->rj_b;
  56358. + }
  56359. + t = saved_tt->rj_p;
  56360. + b_leaf = -1 - t->rj_b;
  56361. + if (t->rj_r == saved_tt) x = t->rj_l; else x = t->rj_r;
  56362. + /* Promote general routes from below */
  56363. + if (x->rj_b < 0) {
  56364. + if (x->rj_mask && (x->rj_b >= b_leaf) && x->rj_mklist == 0) {
  56365. + MKGet(m);
  56366. + if (m) {
  56367. + Bzero(m, sizeof *m);
  56368. + m->rm_b = x->rj_b;
  56369. + m->rm_mask = x->rj_mask;
  56370. + x->rj_mklist = t->rj_mklist = m;
  56371. + }
  56372. + }
  56373. + } else if (x->rj_mklist) {
  56374. + /*
  56375. + * Skip over masks whose index is > that of new node
  56376. + */
  56377. + for (mp = &x->rj_mklist; (m = *mp); mp = &m->rm_mklist)
  56378. + if (m->rm_b >= b_leaf)
  56379. + break;
  56380. + t->rj_mklist = m; *mp = 0;
  56381. + }
  56382. + /* Add new route to highest possible ancestor's list */
  56383. + if ((netmask == 0) || (b > t->rj_b )) {
  56384. +#ifdef RJ_DEBUG
  56385. + printk("klips:radij.c: netmask = %p or b(%d)>t->rjb(%d)\n", netmask, b, t->rj_b);
  56386. +#endif
  56387. + return 0; /* tt rgb */ /* can't lift at all */
  56388. + }
  56389. + b_leaf = tt->rj_b;
  56390. + do {
  56391. + x = t;
  56392. + t = t->rj_p;
  56393. + } while (b <= t->rj_b && x != top);
  56394. + /*
  56395. + * Search through routes associated with node to
  56396. + * insert new route according to index.
  56397. + * For nodes of equal index, place more specific
  56398. + * masks first.
  56399. + */
  56400. + cplim = netmask + mlen;
  56401. + for (mp = &x->rj_mklist; (m = *mp); mp = &m->rm_mklist) {
  56402. + if (m->rm_b < b_leaf)
  56403. + continue;
  56404. + if (m->rm_b > b_leaf)
  56405. + break;
  56406. + if (m->rm_mask == netmask) {
  56407. + m->rm_refs++;
  56408. + tt->rj_mklist = m;
  56409. +#ifdef RJ_DEBUG
  56410. + printk("klips:radij.c: m->rm_mask %p == netmask\n", netmask);
  56411. +#endif
  56412. + return 0; /* tt rgb */
  56413. + }
  56414. + if (rj_refines(netmask, m->rm_mask))
  56415. + break;
  56416. + }
  56417. + MKGet(m);
  56418. + if (m == 0) {
  56419. + printk("klips_debug:rj_addroute: "
  56420. + "Mask for route not entered\n");
  56421. + return 0; /* (tt) rgb */
  56422. + }
  56423. + Bzero(m, sizeof *m);
  56424. + m->rm_b = b_leaf;
  56425. + m->rm_mask = netmask;
  56426. + m->rm_mklist = *mp;
  56427. + *mp = m;
  56428. + tt->rj_mklist = m;
  56429. +#ifdef RJ_DEBUG
  56430. + printk("klips:radij.c: addroute done\n");
  56431. +#endif
  56432. + return 0; /* tt rgb */
  56433. +}
  56434. +
  56435. +int
  56436. +rj_delete(v_arg, netmask_arg, head, node)
  56437. + void *v_arg, *netmask_arg;
  56438. + struct radij_node_head *head;
  56439. + struct radij_node **node;
  56440. +{
  56441. + register struct radij_node *t, *p, *x, *tt;
  56442. + struct radij_mask *m, *saved_m, **mp;
  56443. + struct radij_node *dupedkey, *saved_tt, *top;
  56444. + caddr_t v, netmask;
  56445. + int b, head_off, vlen;
  56446. +
  56447. + v = v_arg;
  56448. + netmask = netmask_arg;
  56449. + x = head->rnh_treetop;
  56450. + tt = rj_search(v, x);
  56451. + head_off = x->rj_off;
  56452. + vlen = *(u_char *)v;
  56453. + saved_tt = tt;
  56454. + top = x;
  56455. + if (tt == 0 ||
  56456. + Bcmp(v + head_off, tt->rj_key + head_off, vlen - head_off))
  56457. + return -EFAULT; /* (0) rgb */
  56458. + /*
  56459. + * Delete our route from mask lists.
  56460. + */
  56461. + if ((dupedkey = tt->rj_dupedkey)) {
  56462. + if (netmask)
  56463. + netmask = rj_search(netmask, rj_masktop)->rj_key;
  56464. + while (tt->rj_mask != netmask)
  56465. + if ((tt = tt->rj_dupedkey) == 0)
  56466. + return -ENOENT; /* -ENXIO; (0) rgb */
  56467. + }
  56468. + if (tt->rj_mask == 0 || (saved_m = m = tt->rj_mklist) == 0)
  56469. + goto on1;
  56470. + if (m->rm_mask != tt->rj_mask) {
  56471. + printk("klips_debug:rj_delete: "
  56472. + "inconsistent annotation\n");
  56473. + goto on1;
  56474. + }
  56475. + if (--m->rm_refs >= 0)
  56476. + goto on1;
  56477. + b = -1 - tt->rj_b;
  56478. + t = saved_tt->rj_p;
  56479. + if (b > t->rj_b)
  56480. + goto on1; /* Wasn't lifted at all */
  56481. + do {
  56482. + x = t;
  56483. + t = t->rj_p;
  56484. + } while (b <= t->rj_b && x != top);
  56485. + for (mp = &x->rj_mklist; (m = *mp); mp = &m->rm_mklist)
  56486. + if (m == saved_m) {
  56487. + *mp = m->rm_mklist;
  56488. + MKFree(m);
  56489. + break;
  56490. + }
  56491. + if (m == 0)
  56492. + printk("klips_debug:rj_delete: "
  56493. + "couldn't find our annotation\n");
  56494. +on1:
  56495. + /*
  56496. + * Eliminate us from tree
  56497. + */
  56498. + if (tt->rj_flags & RJF_ROOT)
  56499. + return -EFAULT; /* (0) rgb */
  56500. +#ifdef RJ_DEBUG
  56501. + /* Get us out of the creation list */
  56502. + for (t = rj_clist; t && t->rj_ybro != tt; t = t->rj_ybro) {}
  56503. + if (t) t->rj_ybro = tt->rj_ybro;
  56504. +#endif /* RJ_DEBUG */
  56505. + t = tt->rj_p;
  56506. + if (dupedkey) {
  56507. + if (tt == saved_tt) {
  56508. + x = dupedkey; x->rj_p = t;
  56509. + if (t->rj_l == tt) t->rj_l = x; else t->rj_r = x;
  56510. + } else {
  56511. + for (x = p = saved_tt; p && p->rj_dupedkey != tt;)
  56512. + p = p->rj_dupedkey;
  56513. + if (p) p->rj_dupedkey = tt->rj_dupedkey;
  56514. + else printk("klips_debug:rj_delete: "
  56515. + "couldn't find node that we started with\n");
  56516. + }
  56517. + t = tt + 1;
  56518. + if (t->rj_flags & RJF_ACTIVE) {
  56519. +#ifndef RJ_DEBUG
  56520. + *++x = *t; p = t->rj_p;
  56521. +#else
  56522. + b = t->rj_info; *++x = *t; t->rj_info = b; p = t->rj_p;
  56523. +#endif /* RJ_DEBUG */
  56524. + if (p->rj_l == t) p->rj_l = x; else p->rj_r = x;
  56525. + x->rj_l->rj_p = x; x->rj_r->rj_p = x;
  56526. + }
  56527. + goto out;
  56528. + }
  56529. + if (t->rj_l == tt) x = t->rj_r; else x = t->rj_l;
  56530. + p = t->rj_p;
  56531. + if (p->rj_r == t) p->rj_r = x; else p->rj_l = x;
  56532. + x->rj_p = p;
  56533. + /*
  56534. + * Demote routes attached to us.
  56535. + */
  56536. + if (t->rj_mklist) {
  56537. + if (x->rj_b >= 0) {
  56538. + for (mp = &x->rj_mklist; (m = *mp);)
  56539. + mp = &m->rm_mklist;
  56540. + *mp = t->rj_mklist;
  56541. + } else {
  56542. + for (m = t->rj_mklist; m;) {
  56543. + struct radij_mask *mm = m->rm_mklist;
  56544. + if (m == x->rj_mklist && (--(m->rm_refs) < 0)) {
  56545. + x->rj_mklist = 0;
  56546. + MKFree(m);
  56547. + } else
  56548. + printk("klips_debug:rj_delete: "
  56549. + "Orphaned Mask 0p%p at 0p%p\n", m, x);
  56550. + m = mm;
  56551. + }
  56552. + }
  56553. + }
  56554. + /*
  56555. + * We may be holding an active internal node in the tree.
  56556. + */
  56557. + x = tt + 1;
  56558. + if (t != x) {
  56559. +#ifndef RJ_DEBUG
  56560. + *t = *x;
  56561. +#else
  56562. + b = t->rj_info; *t = *x; t->rj_info = b;
  56563. +#endif /* RJ_DEBUG */
  56564. + t->rj_l->rj_p = t; t->rj_r->rj_p = t;
  56565. + p = x->rj_p;
  56566. + if (p->rj_l == x) p->rj_l = t; else p->rj_r = t;
  56567. + }
  56568. +out:
  56569. + tt->rj_flags &= ~RJF_ACTIVE;
  56570. + tt[1].rj_flags &= ~RJF_ACTIVE;
  56571. + *node = tt;
  56572. + return 0; /* (tt) rgb */
  56573. +}
  56574. +
  56575. +int
  56576. +rj_walktree(h, f, w)
  56577. + struct radij_node_head *h;
  56578. + register int (*f)(struct radij_node *,void *);
  56579. + void *w;
  56580. +{
  56581. + int error;
  56582. + struct radij_node *base, *next;
  56583. + register struct radij_node *rn;
  56584. +
  56585. + if(!h || !f /* || !w */) {
  56586. + return -ENODATA;
  56587. + }
  56588. +
  56589. + rn = h->rnh_treetop;
  56590. + /*
  56591. + * This gets complicated because we may delete the node
  56592. + * while applying the function f to it, so we need to calculate
  56593. + * the successor node in advance.
  56594. + */
  56595. + /* First time through node, go left */
  56596. + while (rn->rj_b >= 0)
  56597. + rn = rn->rj_l;
  56598. + for (;;) {
  56599. +#ifdef CONFIG_KLIPS_DEBUG
  56600. + if(debug_radij) {
  56601. + printk("klips_debug:rj_walktree: "
  56602. + "for: rn=0p%p rj_b=%d rj_flags=%x",
  56603. + rn,
  56604. + rn->rj_b,
  56605. + rn->rj_flags);
  56606. + rn->rj_b >= 0 ?
  56607. + printk(" node off=%x\n",
  56608. + rn->rj_off) :
  56609. + printk(" leaf key = %08x->%08x\n",
  56610. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_src.s_addr),
  56611. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_dst.s_addr))
  56612. + ;
  56613. + }
  56614. +#endif /* CONFIG_KLIPS_DEBUG */
  56615. + base = rn;
  56616. + /* If at right child go back up, otherwise, go right */
  56617. + while (rn->rj_p->rj_r == rn && (rn->rj_flags & RJF_ROOT) == 0)
  56618. + rn = rn->rj_p;
  56619. + /* Find the next *leaf* since next node might vanish, too */
  56620. + for (rn = rn->rj_p->rj_r; rn->rj_b >= 0;)
  56621. + rn = rn->rj_l;
  56622. + next = rn;
  56623. +#ifdef CONFIG_KLIPS_DEBUG
  56624. + if(debug_radij) {
  56625. + printk("klips_debug:rj_walktree: "
  56626. + "processing leaves, rn=0p%p rj_b=%d rj_flags=%x",
  56627. + rn,
  56628. + rn->rj_b,
  56629. + rn->rj_flags);
  56630. + rn->rj_b >= 0 ?
  56631. + printk(" node off=%x\n",
  56632. + rn->rj_off) :
  56633. + printk(" leaf key = %08x->%08x\n",
  56634. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_src.s_addr),
  56635. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_dst.s_addr))
  56636. + ;
  56637. + }
  56638. +#endif /* CONFIG_KLIPS_DEBUG */
  56639. + /* Process leaves */
  56640. + while ((rn = base)) {
  56641. + base = rn->rj_dupedkey;
  56642. +#ifdef CONFIG_KLIPS_DEBUG
  56643. + if(debug_radij) {
  56644. + printk("klips_debug:rj_walktree: "
  56645. + "while: base=0p%p rn=0p%p rj_b=%d rj_flags=%x",
  56646. + base,
  56647. + rn,
  56648. + rn->rj_b,
  56649. + rn->rj_flags);
  56650. + rn->rj_b >= 0 ?
  56651. + printk(" node off=%x\n",
  56652. + rn->rj_off) :
  56653. + printk(" leaf key = %08x->%08x\n",
  56654. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_src.s_addr),
  56655. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_dst.s_addr))
  56656. + ;
  56657. + }
  56658. +#endif /* CONFIG_KLIPS_DEBUG */
  56659. + if (!(rn->rj_flags & RJF_ROOT) && (error = (*f)(rn, w)))
  56660. + return (-error);
  56661. + }
  56662. + rn = next;
  56663. + if (rn->rj_flags & RJF_ROOT)
  56664. + return (0);
  56665. + }
  56666. + /* NOTREACHED */
  56667. +}
  56668. +
  56669. +int
  56670. +rj_inithead(head, off)
  56671. + void **head;
  56672. + int off;
  56673. +{
  56674. + register struct radij_node_head *rnh;
  56675. + register struct radij_node *t, *tt, *ttt;
  56676. + if (*head)
  56677. + return (1);
  56678. + R_Malloc(rnh, struct radij_node_head *, sizeof (*rnh));
  56679. + if (rnh == NULL)
  56680. + return (0);
  56681. + Bzero(rnh, sizeof (*rnh));
  56682. + *head = rnh;
  56683. + t = rj_newpair(rj_zeroes, off, rnh->rnh_nodes);
  56684. + ttt = rnh->rnh_nodes + 2;
  56685. + t->rj_r = ttt;
  56686. + t->rj_p = t;
  56687. + tt = t->rj_l;
  56688. + tt->rj_flags = t->rj_flags = RJF_ROOT | RJF_ACTIVE;
  56689. + tt->rj_b = -1 - off;
  56690. + *ttt = *tt;
  56691. + ttt->rj_key = rj_ones;
  56692. + rnh->rnh_addaddr = rj_addroute;
  56693. + rnh->rnh_deladdr = rj_delete;
  56694. + rnh->rnh_matchaddr = rj_match;
  56695. + rnh->rnh_walktree = rj_walktree;
  56696. + rnh->rnh_treetop = t;
  56697. + return (1);
  56698. +}
  56699. +
  56700. +void
  56701. +rj_init()
  56702. +{
  56703. + char *cp, *cplim;
  56704. +
  56705. + if (maj_keylen == 0) {
  56706. + printk("klips_debug:rj_init: "
  56707. + "radij functions require maj_keylen be set\n");
  56708. + return;
  56709. + }
  56710. + R_Malloc(rj_zeroes, char *, 3 * maj_keylen);
  56711. + if (rj_zeroes == NULL)
  56712. + panic("rj_init");
  56713. + Bzero(rj_zeroes, 3 * maj_keylen);
  56714. + rj_ones = cp = rj_zeroes + maj_keylen;
  56715. + maskedKey = cplim = rj_ones + maj_keylen;
  56716. + while (cp < cplim)
  56717. + *cp++ = -1;
  56718. + if (rj_inithead((void **)&mask_rjhead, 0) == 0)
  56719. + panic("rj_init 2");
  56720. +}
  56721. +
  56722. +void
  56723. +rj_preorder(struct radij_node *rn, int l)
  56724. +{
  56725. + int i;
  56726. +
  56727. + if (rn == NULL){
  56728. + printk("klips_debug:rj_preorder: "
  56729. + "NULL pointer\n");
  56730. + return;
  56731. + }
  56732. +
  56733. + if (rn->rj_b >= 0){
  56734. + rj_preorder(rn->rj_l, l+1);
  56735. + rj_preorder(rn->rj_r, l+1);
  56736. + printk("klips_debug:");
  56737. + for (i=0; i<l; i++)
  56738. + printk("*");
  56739. + printk(" off = %d\n",
  56740. + rn->rj_off);
  56741. + } else {
  56742. + printk("klips_debug:");
  56743. + for (i=0; i<l; i++)
  56744. + printk("@");
  56745. + printk(" flags = %x",
  56746. + (u_int)rn->rj_flags);
  56747. + if (rn->rj_flags & RJF_ACTIVE) {
  56748. + printk(" @key=0p%p",
  56749. + rn->rj_key);
  56750. + printk(" key = %08x->%08x",
  56751. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_src.s_addr),
  56752. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_key)->sen_ip_dst.s_addr));
  56753. + printk(" @mask=0p%p",
  56754. + rn->rj_mask);
  56755. + if (rn->rj_mask)
  56756. + printk(" mask = %08x->%08x",
  56757. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_mask)->sen_ip_src.s_addr),
  56758. + (u_int)ntohl(((struct sockaddr_encap *)rn->rj_mask)->sen_ip_dst.s_addr));
  56759. + if (rn->rj_dupedkey)
  56760. + printk(" dupedkey = 0p%p",
  56761. + rn->rj_dupedkey);
  56762. + }
  56763. + printk("\n");
  56764. + }
  56765. +}
  56766. +
  56767. +#ifdef RJ_DEBUG
  56768. +DEBUG_NO_STATIC void traverse(struct radij_node *p)
  56769. +{
  56770. + rj_preorder(p, 0);
  56771. +}
  56772. +#endif /* RJ_DEBUG */
  56773. +
  56774. +void
  56775. +rj_dumptrees(void)
  56776. +{
  56777. + rj_preorder(rnh->rnh_treetop, 0);
  56778. +}
  56779. +
  56780. +void
  56781. +rj_free_mkfreelist(void)
  56782. +{
  56783. + struct radij_mask *mknp, *mknp2;
  56784. +
  56785. + mknp = rj_mkfreelist;
  56786. + while(mknp)
  56787. + {
  56788. + mknp2 = mknp;
  56789. + mknp = mknp->rm_mklist;
  56790. + kfree(mknp2);
  56791. + }
  56792. +}
  56793. +
  56794. +int
  56795. +radijcleartree(void)
  56796. +{
  56797. + return rj_walktree(rnh, ipsec_rj_walker_delete, NULL);
  56798. +}
  56799. +
  56800. +int
  56801. +radijcleanup(void)
  56802. +{
  56803. + int error = 0;
  56804. +
  56805. + error = radijcleartree();
  56806. +
  56807. + rj_free_mkfreelist();
  56808. +
  56809. +/* rj_walktree(mask_rjhead, ipsec_rj_walker_delete, NULL); */
  56810. + if(mask_rjhead) {
  56811. + kfree(mask_rjhead);
  56812. + }
  56813. +
  56814. + if(rj_zeroes) {
  56815. + kfree(rj_zeroes);
  56816. + }
  56817. +
  56818. + if(rnh) {
  56819. + kfree(rnh);
  56820. + }
  56821. +
  56822. + return error;
  56823. +}
  56824. +
  56825. +/*
  56826. + * $Log: radij.c,v $
  56827. + * Revision 1.48.2.1 2006/10/06 21:39:27 paul
  56828. + * Fix for 2.6.18+ only include linux/config.h if AUTOCONF_INCLUDED is not
  56829. + * set. This is defined through autoconf.h which is included through the
  56830. + * linux kernel build macros.
  56831. + *
  56832. + * Revision 1.48 2005/04/29 05:10:22 mcr
  56833. + * removed from extraenous includes to make unit testing easier.
  56834. + *
  56835. + * Revision 1.47 2004/07/10 19:11:18 mcr
  56836. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  56837. + *
  56838. + * Revision 1.46 2004/04/06 02:49:26 mcr
  56839. + * pullup of algo code from alg-branch.
  56840. + *
  56841. + * Revision 1.45 2003/10/31 02:27:55 mcr
  56842. + * pulled up port-selector patches and sa_id elimination.
  56843. + *
  56844. + * Revision 1.44.30.1 2003/10/29 01:30:41 mcr
  56845. + * elimited "struct sa_id".
  56846. + *
  56847. + * Revision 1.44 2002/07/24 18:44:54 rgb
  56848. + * Type fiddling to tame ia64 compiler.
  56849. + *
  56850. + * Revision 1.43 2002/05/23 07:14:11 rgb
  56851. + * Cleaned up %p variants to 0p%p for test suite cleanup.
  56852. + *
  56853. + * Revision 1.42 2002/04/24 07:55:32 mcr
  56854. + * #include patches and Makefiles for post-reorg compilation.
  56855. + *
  56856. + * Revision 1.41 2002/04/24 07:36:35 mcr
  56857. + * Moved from ./klips/net/ipsec/radij.c,v
  56858. + *
  56859. + * Revision 1.40 2002/01/29 17:17:58 mcr
  56860. + * moved include of ipsec_param.h to after include of linux/kernel.h
  56861. + * otherwise, it seems that some option that is set in ipsec_param.h
  56862. + * screws up something subtle in the include path to kernel.h, and
  56863. + * it complains on the snprintf() prototype.
  56864. + *
  56865. + * Revision 1.39 2002/01/29 04:00:55 mcr
  56866. + * more excise of kversions.h header.
  56867. + *
  56868. + * Revision 1.38 2002/01/29 02:13:19 mcr
  56869. + * introduction of ipsec_kversion.h means that include of
  56870. + * ipsec_param.h must preceed any decisions about what files to
  56871. + * include to deal with differences in kernel source.
  56872. + *
  56873. + * Revision 1.37 2001/10/18 04:45:23 rgb
  56874. + * 2.4.9 kernel deprecates linux/malloc.h in favour of linux/slab.h,
  56875. + * lib/freeswan.h version macros moved to lib/kversions.h.
  56876. + * Other compiler directive cleanups.
  56877. + *
  56878. + * Revision 1.36 2001/08/22 13:43:51 henry
  56879. + * eliminate the single use of min() to avoid problems with Linus changing it
  56880. + *
  56881. + * Revision 1.35 2001/06/15 04:57:29 rgb
  56882. + * Clarified error return codes.
  56883. + * Changed mask add already exists to EEXIST.
  56884. + * Changed mask delete did not exist to ENOENT.
  56885. + *
  56886. + * Revision 1.34 2001/05/03 19:44:26 rgb
  56887. + * Fix sign of error return codes for rj_addroute().
  56888. + *
  56889. + * Revision 1.33 2001/02/27 22:24:56 rgb
  56890. + * Re-formatting debug output (line-splitting, joining, 1arg/line).
  56891. + * Check for satoa() return codes.
  56892. + *
  56893. + * Revision 1.32 2001/02/27 06:23:15 rgb
  56894. + * Debug line splitting.
  56895. + *
  56896. + * Revision 1.31 2000/11/06 04:35:21 rgb
  56897. + * Clear table *before* releasing other items in radijcleanup.
  56898. + *
  56899. + * Revision 1.30 2000/09/20 04:07:40 rgb
  56900. + * Changed static functions to DEBUG_NO_STATIC to reveal function names in
  56901. + * oopsen.
  56902. + *
  56903. + * Revision 1.29 2000/09/12 03:25:02 rgb
  56904. + * Moved radij_c_version printing to ipsec_version_get_info().
  56905. + *
  56906. + * Revision 1.28 2000/09/08 19:12:56 rgb
  56907. + * Change references from DEBUG_IPSEC to CONFIG_IPSEC_DEBUG.
  56908. + *
  56909. + * Revision 1.27 2000/07/28 14:58:32 rgb
  56910. + * Changed kfree_s to kfree, eliminating extra arg to fix 2.4.0-test5.
  56911. + *
  56912. + * Revision 1.26 2000/05/10 23:11:37 rgb
  56913. + * Comment out most of the startup version information.
  56914. + *
  56915. + * Revision 1.25 2000/01/21 06:21:47 rgb
  56916. + * Change return codes to negative on error.
  56917. + *
  56918. + * Revision 1.24 1999/11/18 04:09:20 rgb
  56919. + * Replaced all kernel version macros to shorter, readable form.
  56920. + *
  56921. + * Revision 1.23 1999/11/17 15:53:41 rgb
  56922. + * Changed all occurrences of #include "../../../lib/freeswan.h"
  56923. + * to #include <freeswan.h> which works due to -Ilibfreeswan in the
  56924. + * klips/net/ipsec/Makefile.
  56925. + *
  56926. + * Revision 1.22 1999/10/15 22:17:28 rgb
  56927. + * Modify radijcleanup() to call radijcleartree().
  56928. + *
  56929. + * Revision 1.21 1999/10/08 18:37:34 rgb
  56930. + * Fix end-of-line spacing to sate whining PHMs.
  56931. + *
  56932. + * Revision 1.20 1999/10/01 15:44:54 rgb
  56933. + * Move spinlock header include to 2.1> scope.
  56934. + *
  56935. + * Revision 1.19 1999/10/01 08:35:52 rgb
  56936. + * Add spinlock include to shut up compiler for 2.0.38.
  56937. + *
  56938. + * Revision 1.18 1999/09/23 18:02:52 rgb
  56939. + * De-alarm the search failure message so it doesn't sound so grave.
  56940. + *
  56941. + * Revision 1.17 1999/05/25 21:26:01 rgb
  56942. + * Fix rj_walktree() sanity checking bug.
  56943. + *
  56944. + * Revision 1.16 1999/05/09 03:25:38 rgb
  56945. + * Fix bug introduced by 2.2 quick-and-dirty patch.
  56946. + *
  56947. + * Revision 1.15 1999/05/05 22:02:33 rgb
  56948. + * Add a quick and dirty port to 2.2 kernels by Marc Boucher <marc@mbsi.ca>.
  56949. + *
  56950. + * Revision 1.14 1999/04/29 15:24:15 rgb
  56951. + * Add sanity checking for null pointer arguments.
  56952. + * Standardise an error return method.
  56953. + *
  56954. + * Revision 1.13 1999/04/11 00:29:02 henry
  56955. + * GPL boilerplate
  56956. + *
  56957. + * Revision 1.12 1999/04/06 04:54:28 rgb
  56958. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  56959. + * patch shell fixes.
  56960. + *
  56961. + * Revision 1.11 1999/02/17 16:52:53 rgb
  56962. + * Convert DEBUG_IPSEC to KLIPS_PRINT
  56963. + * Clean out unused cruft.
  56964. + *
  56965. + * Revision 1.10 1999/01/22 06:30:05 rgb
  56966. + * Cruft clean-out.
  56967. + * 64-bit clean-up.
  56968. + *
  56969. + * Revision 1.9 1998/12/01 13:22:04 rgb
  56970. + * Added support for debug printing of version info.
  56971. + *
  56972. + * Revision 1.8 1998/11/30 13:22:55 rgb
  56973. + * Rationalised all the klips kernel file headers. They are much shorter
  56974. + * now and won't conflict under RH5.2.
  56975. + *
  56976. + * Revision 1.7 1998/10/25 02:43:26 rgb
  56977. + * Change return type on rj_addroute and rj_delete and add and argument
  56978. + * to the latter to be able to transmit more infomation about errors.
  56979. + *
  56980. + * Revision 1.6 1998/10/19 14:30:06 rgb
  56981. + * Added inclusion of freeswan.h.
  56982. + *
  56983. + * Revision 1.5 1998/10/09 04:33:27 rgb
  56984. + * Added 'klips_debug' prefix to all klips printk debug statements.
  56985. + * Fixed output formatting slightly.
  56986. + *
  56987. + * Revision 1.4 1998/07/28 00:06:59 rgb
  56988. + * Add debug detail to tree traversing.
  56989. + *
  56990. + * Revision 1.3 1998/07/14 18:07:58 rgb
  56991. + * Add a routine to clear the eroute tree.
  56992. + *
  56993. + * Revision 1.2 1998/06/25 20:03:22 rgb
  56994. + * Cleanup #endif comments. Debug output for rj_init.
  56995. + *
  56996. + * Revision 1.1 1998/06/18 21:30:22 henry
  56997. + * move sources from klips/src to klips/net/ipsec to keep stupid kernel
  56998. + * build scripts happier about symlinks
  56999. + *
  57000. + * Revision 1.8 1998/05/25 20:34:15 rgb
  57001. + * Remove temporary ipsec_walk, rj_deltree and rj_delnodes functions.
  57002. + *
  57003. + * Rename ipsec_rj_walker (ipsec_walk) to ipsec_rj_walker_procprint and
  57004. + * add ipsec_rj_walker_delete.
  57005. + *
  57006. + * Recover memory for eroute table on unload of module.
  57007. + *
  57008. + * Revision 1.7 1998/05/21 12:58:58 rgb
  57009. + * Moved 'extern' definitions to ipsec_radij.h to support /proc 3k limit fix.
  57010. + *
  57011. + * Revision 1.6 1998/04/23 20:57:29 rgb
  57012. + * Cleaned up compiler warnings for unused debugging functions.
  57013. + *
  57014. + * Revision 1.5 1998/04/22 16:51:38 rgb
  57015. + * Tidy up radij debug code from recent rash of modifications to debug code.
  57016. + *
  57017. + * Revision 1.4 1998/04/21 21:28:56 rgb
  57018. + * Rearrange debug switches to change on the fly debug output from user
  57019. + * space. Only kernel changes checked in at this time. radij.c was also
  57020. + * changed to temporarily remove buggy debugging code in rj_delete causing
  57021. + * an OOPS and hence, netlink device open errors.
  57022. + *
  57023. + * Revision 1.3 1998/04/14 17:30:37 rgb
  57024. + * Fix up compiling errors for radij tree memory reclamation.
  57025. + *
  57026. + * Revision 1.2 1998/04/12 22:03:25 rgb
  57027. + * Updated ESP-3DES-HMAC-MD5-96,
  57028. + * ESP-DES-HMAC-MD5-96,
  57029. + * AH-HMAC-MD5-96,
  57030. + * AH-HMAC-SHA1-96 since Henry started freeswan cvs repository
  57031. + * from old standards (RFC182[5-9] to new (as of March 1998) drafts.
  57032. + *
  57033. + * Fixed eroute references in /proc/net/ipsec*.
  57034. + *
  57035. + * Started to patch module unloading memory leaks in ipsec_netlink and
  57036. + * radij tree unloading.
  57037. + *
  57038. + * Revision 1.1 1998/04/09 03:06:15 henry
  57039. + * sources moved up from linux/net/ipsec
  57040. + *
  57041. + * Revision 1.1.1.1 1998/04/08 05:35:03 henry
  57042. + * RGB's ipsec-0.8pre2.tar.gz ipsec-0.8
  57043. + *
  57044. + * Revision 0.4 1997/01/15 01:28:15 ji
  57045. + * No changes.
  57046. + *
  57047. + * Revision 0.3 1996/11/20 14:39:04 ji
  57048. + * Minor cleanups.
  57049. + * Rationalized debugging code.
  57050. + *
  57051. + * Revision 0.2 1996/11/02 00:18:33 ji
  57052. + * First limited release.
  57053. + *
  57054. + *
  57055. + */
  57056. --- /dev/null Tue Mar 11 13:02:56 2003
  57057. +++ linux/net/ipsec/rangetoa.c Mon Feb 9 13:51:03 2004
  57058. @@ -0,0 +1,60 @@
  57059. +/*
  57060. + * convert binary form of address range to ASCII
  57061. + * Copyright (C) 1998, 1999 Henry Spencer.
  57062. + *
  57063. + * This library is free software; you can redistribute it and/or modify it
  57064. + * under the terms of the GNU Library General Public License as published by
  57065. + * the Free Software Foundation; either version 2 of the License, or (at your
  57066. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  57067. + *
  57068. + * This library is distributed in the hope that it will be useful, but
  57069. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  57070. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  57071. + * License for more details.
  57072. + *
  57073. + * RCSID $Id: rangetoa.c,v 1.9 2004/07/10 07:48:37 mcr Exp $
  57074. + */
  57075. +#include "openswan.h"
  57076. +
  57077. +/*
  57078. + - rangetoa - convert address range to ASCII
  57079. + */
  57080. +size_t /* space needed for full conversion */
  57081. +rangetoa(addrs, format, dst, dstlen)
  57082. +struct in_addr addrs[2];
  57083. +int format; /* character */
  57084. +char *dst; /* need not be valid if dstlen is 0 */
  57085. +size_t dstlen;
  57086. +{
  57087. + size_t len;
  57088. + size_t rest;
  57089. + int n;
  57090. + char *p;
  57091. +
  57092. + switch (format) {
  57093. + case 0:
  57094. + break;
  57095. + default:
  57096. + return 0;
  57097. + break;
  57098. + }
  57099. +
  57100. + len = addrtoa(addrs[0], 0, dst, dstlen);
  57101. + if (len < dstlen)
  57102. + for (p = dst + len - 1, n = 3; len < dstlen && n > 0;
  57103. + p++, len++, n--)
  57104. + *p = '.';
  57105. + else
  57106. + p = NULL;
  57107. + if (len < dstlen)
  57108. + rest = dstlen - len;
  57109. + else {
  57110. + if (dstlen > 0)
  57111. + *(dst + dstlen - 1) = '\0';
  57112. + rest = 0;
  57113. + }
  57114. +
  57115. + len += addrtoa(addrs[1], 0, p, rest);
  57116. +
  57117. + return len;
  57118. +}
  57119. --- /dev/null Tue Mar 11 13:02:56 2003
  57120. +++ linux/net/ipsec/satot.c Mon Feb 9 13:51:03 2004
  57121. @@ -0,0 +1,133 @@
  57122. +/*
  57123. + * convert from binary form of SA ID to text
  57124. + * Copyright (C) 2000, 2001 Henry Spencer.
  57125. + *
  57126. + * This library is free software; you can redistribute it and/or modify it
  57127. + * under the terms of the GNU Library General Public License as published by
  57128. + * the Free Software Foundation; either version 2 of the License, or (at your
  57129. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  57130. + *
  57131. + * This library is distributed in the hope that it will be useful, but
  57132. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  57133. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  57134. + * License for more details.
  57135. + *
  57136. + * RCSID $Id: satot.c,v 1.13 2004/07/10 07:48:37 mcr Exp $
  57137. + */
  57138. +#include "openswan.h"
  57139. +
  57140. +static struct typename {
  57141. + char type;
  57142. + char *name;
  57143. +} typenames[] = {
  57144. + { SA_AH, "ah" },
  57145. + { SA_ESP, "esp" },
  57146. + { SA_IPIP, "tun" },
  57147. + { SA_COMP, "comp" },
  57148. + { SA_INT, "int" },
  57149. + { 0, NULL }
  57150. +};
  57151. +
  57152. +/*
  57153. + - satot - convert SA to text "ah507@1.2.3.4"
  57154. + */
  57155. +size_t /* space needed for full conversion */
  57156. +satot(sa, format, dst, dstlen)
  57157. +const ip_said *sa;
  57158. +int format; /* character */
  57159. +char *dst; /* need not be valid if dstlen is 0 */
  57160. +size_t dstlen;
  57161. +{
  57162. + size_t len = 0; /* 0 means "not recognized yet" */
  57163. + int base;
  57164. + int showversion; /* use delimiter to show IP version? */
  57165. + struct typename *tn;
  57166. + char *p;
  57167. + char *pre;
  57168. + char buf[10+1+ULTOT_BUF+ADDRTOT_BUF];
  57169. + char unk[10];
  57170. +
  57171. + switch (format) {
  57172. + case 0:
  57173. + base = 16;
  57174. + showversion = 1;
  57175. + break;
  57176. + case 'f':
  57177. + base = 17;
  57178. + showversion = 1;
  57179. + break;
  57180. + case 'x':
  57181. + base = 'x';
  57182. + showversion = 0;
  57183. + break;
  57184. + case 'd':
  57185. + base = 10;
  57186. + showversion = 0;
  57187. + break;
  57188. + default:
  57189. + return 0;
  57190. + break;
  57191. + }
  57192. +
  57193. + memset(buf, 0, sizeof(buf));
  57194. +
  57195. + pre = NULL;
  57196. + for (tn = typenames; tn->name != NULL; tn++)
  57197. + if (sa->proto == tn->type) {
  57198. + pre = tn->name;
  57199. + break; /* NOTE BREAK OUT */
  57200. + }
  57201. + if (pre == NULL) { /* unknown protocol */
  57202. + strcpy(unk, "unk");
  57203. + (void) ultot((unsigned char)sa->proto, 10, unk+strlen(unk),
  57204. + sizeof(unk)-strlen(unk));
  57205. + pre = unk;
  57206. + }
  57207. +
  57208. + if (strcmp(pre, PASSTHROUGHTYPE) == 0 &&
  57209. + sa->spi == PASSTHROUGHSPI &&
  57210. + isunspecaddr(&sa->dst)) {
  57211. + strcpy(buf, (addrtypeof(&sa->dst) == AF_INET) ?
  57212. + PASSTHROUGH4NAME :
  57213. + PASSTHROUGH6NAME);
  57214. + len = strlen(buf);
  57215. + }
  57216. +
  57217. + if (sa->proto == SA_INT) {
  57218. + switch (ntohl(sa->spi)) {
  57219. + case SPI_PASS: p = "%pass"; break;
  57220. + case SPI_DROP: p = "%drop"; break;
  57221. + case SPI_REJECT: p = "%reject"; break;
  57222. + case SPI_HOLD: p = "%hold"; break;
  57223. + case SPI_TRAP: p = "%trap"; break;
  57224. + case SPI_TRAPSUBNET: p = "%trapsubnet"; break;
  57225. + default: p = NULL; break;
  57226. + }
  57227. + if (p != NULL) {
  57228. + strcpy(buf, p);
  57229. + len = strlen(buf);
  57230. + }
  57231. + }
  57232. +
  57233. + if (len == 0) { /* general case needed */
  57234. + strcpy(buf, pre);
  57235. + len = strlen(buf);
  57236. + if (showversion) {
  57237. + *(buf+len) = (addrtypeof(&sa->dst) == AF_INET) ? '.' :
  57238. + ':';
  57239. + len++;
  57240. + *(buf+len) = '\0';
  57241. + }
  57242. + len += ultot(ntohl(sa->spi), base, buf+len, sizeof(buf)-len);
  57243. + *(buf+len-1) = '@';
  57244. + len += addrtot(&sa->dst, 0, buf+len, sizeof(buf)-len);
  57245. + *(buf+len) = '\0';
  57246. + }
  57247. +
  57248. + if (dst != NULL) {
  57249. + if (len > dstlen)
  57250. + *(buf+dstlen-1) = '\0';
  57251. + strcpy(dst, buf);
  57252. + }
  57253. + return len;
  57254. +}
  57255. --- /dev/null Tue Mar 11 13:02:56 2003
  57256. +++ linux/net/ipsec/subnetof.c Mon Feb 9 13:51:03 2004
  57257. @@ -0,0 +1,59 @@
  57258. +/*
  57259. + * minor network-address manipulation utilities
  57260. + * Copyright (C) 1998, 1999 Henry Spencer.
  57261. + *
  57262. + * This library is free software; you can redistribute it and/or modify it
  57263. + * under the terms of the GNU Library General Public License as published by
  57264. + * the Free Software Foundation; either version 2 of the License, or (at your
  57265. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  57266. + *
  57267. + * This library is distributed in the hope that it will be useful, but
  57268. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  57269. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  57270. + * License for more details.
  57271. + *
  57272. + * RCSID $Id: subnetof.c,v 1.8 2004/07/10 07:48:37 mcr Exp $
  57273. + */
  57274. +#include "openswan.h"
  57275. +
  57276. +/*
  57277. + - subnetof - given address and mask, return subnet part
  57278. + */
  57279. +struct in_addr
  57280. +subnetof(addr, mask)
  57281. +struct in_addr addr;
  57282. +struct in_addr mask;
  57283. +{
  57284. + struct in_addr result;
  57285. +
  57286. + result.s_addr = addr.s_addr & mask.s_addr;
  57287. + return result;
  57288. +}
  57289. +
  57290. +/*
  57291. + - hostof - given address and mask, return host part
  57292. + */
  57293. +struct in_addr
  57294. +hostof(addr, mask)
  57295. +struct in_addr addr;
  57296. +struct in_addr mask;
  57297. +{
  57298. + struct in_addr result;
  57299. +
  57300. + result.s_addr = addr.s_addr & ~mask.s_addr;
  57301. + return result;
  57302. +}
  57303. +
  57304. +/*
  57305. + - broadcastof - given (network) address and mask, return broadcast address
  57306. + */
  57307. +struct in_addr
  57308. +broadcastof(addr, mask)
  57309. +struct in_addr addr;
  57310. +struct in_addr mask;
  57311. +{
  57312. + struct in_addr result;
  57313. +
  57314. + result.s_addr = addr.s_addr | ~mask.s_addr;
  57315. + return result;
  57316. +}
  57317. --- /dev/null Tue Mar 11 13:02:56 2003
  57318. +++ linux/net/ipsec/subnettoa.c Mon Feb 9 13:51:03 2004
  57319. @@ -0,0 +1,61 @@
  57320. +/*
  57321. + * convert binary form of subnet description to ASCII
  57322. + * Copyright (C) 1998, 1999 Henry Spencer.
  57323. + *
  57324. + * This library is free software; you can redistribute it and/or modify it
  57325. + * under the terms of the GNU Library General Public License as published by
  57326. + * the Free Software Foundation; either version 2 of the License, or (at your
  57327. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  57328. + *
  57329. + * This library is distributed in the hope that it will be useful, but
  57330. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  57331. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  57332. + * License for more details.
  57333. + *
  57334. + * RCSID $Id: subnettoa.c,v 1.11 2004/07/10 07:48:37 mcr Exp $
  57335. + */
  57336. +#include "openswan.h"
  57337. +
  57338. +/*
  57339. + - subnettoa - convert address and mask to ASCII "addr/mask"
  57340. + * Output expresses the mask as a bit count if possible, else dotted decimal.
  57341. + */
  57342. +size_t /* space needed for full conversion */
  57343. +subnettoa(addr, mask, format, dst, dstlen)
  57344. +struct in_addr addr;
  57345. +struct in_addr mask;
  57346. +int format; /* character */
  57347. +char *dst; /* need not be valid if dstlen is 0 */
  57348. +size_t dstlen;
  57349. +{
  57350. + size_t len;
  57351. + size_t rest;
  57352. + int n;
  57353. + char *p;
  57354. +
  57355. + switch (format) {
  57356. + case 0:
  57357. + break;
  57358. + default:
  57359. + return 0;
  57360. + break;
  57361. + }
  57362. +
  57363. + len = addrtoa(addr, 0, dst, dstlen);
  57364. + if (len < dstlen) {
  57365. + dst[len - 1] = '/';
  57366. + p = dst + len;
  57367. + rest = dstlen - len;
  57368. + } else {
  57369. + p = NULL;
  57370. + rest = 0;
  57371. + }
  57372. +
  57373. + n = masktobits(mask);
  57374. + if (n >= 0)
  57375. + len += ultoa((unsigned long)n, 10, p, rest);
  57376. + else
  57377. + len += addrtoa(mask, 0, p, rest);
  57378. +
  57379. + return len;
  57380. +}
  57381. --- /dev/null Tue Mar 11 13:02:56 2003
  57382. +++ linux/net/ipsec/sysctl_net_ipsec.c Mon Feb 9 13:51:03 2004
  57383. @@ -0,0 +1,199 @@
  57384. +/*
  57385. + * sysctl interface to net IPSEC subsystem.
  57386. + * Copyright (C) 1998, 1999, 2000, 2001 Richard Guy Briggs.
  57387. + *
  57388. + * This program is free software; you can redistribute it and/or modify it
  57389. + * under the terms of the GNU General Public License as published by the
  57390. + * Free Software Foundation; either version 2 of the License, or (at your
  57391. + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  57392. + *
  57393. + * This program is distributed in the hope that it will be useful, but
  57394. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  57395. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  57396. + * for more details.
  57397. + *
  57398. + * RCSID $Id: sysctl_net_ipsec.c,v 1.17 2004/07/10 19:11:18 mcr Exp $
  57399. + */
  57400. +
  57401. +/* -*- linux-c -*-
  57402. + *
  57403. + * Initiated April 3, 1998, Richard Guy Briggs <rgb@conscoop.ottawa.on.ca>
  57404. + */
  57405. +
  57406. +#include <linux/mm.h>
  57407. +#include <linux/sysctl.h>
  57408. +
  57409. +#include "openswan/ipsec_param.h"
  57410. +
  57411. +#ifdef CONFIG_SYSCTL
  57412. +
  57413. +#define NET_IPSEC 2112 /* Random number */
  57414. +#ifdef CONFIG_KLIPS_DEBUG
  57415. +extern int debug_ah;
  57416. +extern int debug_esp;
  57417. +extern int debug_tunnel;
  57418. +extern int debug_eroute;
  57419. +extern int debug_spi;
  57420. +extern int debug_radij;
  57421. +extern int debug_netlink;
  57422. +extern int debug_xform;
  57423. +extern int debug_rcv;
  57424. +extern int debug_pfkey;
  57425. +extern int sysctl_ipsec_debug_verbose;
  57426. +#ifdef CONFIG_KLIPS_IPCOMP
  57427. +extern int sysctl_ipsec_debug_ipcomp;
  57428. +#endif /* CONFIG_KLIPS_IPCOMP */
  57429. +#endif /* CONFIG_KLIPS_DEBUG */
  57430. +
  57431. +extern int sysctl_ipsec_icmp;
  57432. +extern int sysctl_ipsec_inbound_policy_check;
  57433. +extern int sysctl_ipsec_tos;
  57434. +int sysctl_ipsec_regress_pfkey_lossage;
  57435. +
  57436. +enum {
  57437. +#ifdef CONFIG_KLIPS_DEBUG
  57438. + NET_IPSEC_DEBUG_AH=1,
  57439. + NET_IPSEC_DEBUG_ESP=2,
  57440. + NET_IPSEC_DEBUG_TUNNEL=3,
  57441. + NET_IPSEC_DEBUG_EROUTE=4,
  57442. + NET_IPSEC_DEBUG_SPI=5,
  57443. + NET_IPSEC_DEBUG_RADIJ=6,
  57444. + NET_IPSEC_DEBUG_NETLINK=7,
  57445. + NET_IPSEC_DEBUG_XFORM=8,
  57446. + NET_IPSEC_DEBUG_RCV=9,
  57447. + NET_IPSEC_DEBUG_PFKEY=10,
  57448. + NET_IPSEC_DEBUG_VERBOSE=11,
  57449. + NET_IPSEC_DEBUG_IPCOMP=12,
  57450. +#endif /* CONFIG_KLIPS_DEBUG */
  57451. + NET_IPSEC_ICMP=13,
  57452. + NET_IPSEC_INBOUND_POLICY_CHECK=14,
  57453. + NET_IPSEC_TOS=15,
  57454. + NET_IPSEC_REGRESS_PFKEY_LOSSAGE=16,
  57455. +};
  57456. +
  57457. +static ctl_table ipsec_table[] = {
  57458. +#ifdef CONFIG_KLIPS_DEBUG
  57459. + { NET_IPSEC_DEBUG_AH, "debug_ah", &debug_ah,
  57460. + sizeof(int), 0644, NULL, &proc_dointvec},
  57461. + { NET_IPSEC_DEBUG_ESP, "debug_esp", &debug_esp,
  57462. + sizeof(int), 0644, NULL, &proc_dointvec},
  57463. + { NET_IPSEC_DEBUG_TUNNEL, "debug_tunnel", &debug_tunnel,
  57464. + sizeof(int), 0644, NULL, &proc_dointvec},
  57465. + { NET_IPSEC_DEBUG_EROUTE, "debug_eroute", &debug_eroute,
  57466. + sizeof(int), 0644, NULL, &proc_dointvec},
  57467. + { NET_IPSEC_DEBUG_SPI, "debug_spi", &debug_spi,
  57468. + sizeof(int), 0644, NULL, &proc_dointvec},
  57469. + { NET_IPSEC_DEBUG_RADIJ, "debug_radij", &debug_radij,
  57470. + sizeof(int), 0644, NULL, &proc_dointvec},
  57471. + { NET_IPSEC_DEBUG_NETLINK, "debug_netlink", &debug_netlink,
  57472. + sizeof(int), 0644, NULL, &proc_dointvec},
  57473. + { NET_IPSEC_DEBUG_XFORM, "debug_xform", &debug_xform,
  57474. + sizeof(int), 0644, NULL, &proc_dointvec},
  57475. + { NET_IPSEC_DEBUG_RCV, "debug_rcv", &debug_rcv,
  57476. + sizeof(int), 0644, NULL, &proc_dointvec},
  57477. + { NET_IPSEC_DEBUG_PFKEY, "debug_pfkey", &debug_pfkey,
  57478. + sizeof(int), 0644, NULL, &proc_dointvec},
  57479. + { NET_IPSEC_DEBUG_VERBOSE, "debug_verbose",&sysctl_ipsec_debug_verbose,
  57480. + sizeof(int), 0644, NULL, &proc_dointvec},
  57481. +#ifdef CONFIG_KLIPS_IPCOMP
  57482. + { NET_IPSEC_DEBUG_IPCOMP, "debug_ipcomp", &sysctl_ipsec_debug_ipcomp,
  57483. + sizeof(int), 0644, NULL, &proc_dointvec},
  57484. +#endif /* CONFIG_KLIPS_IPCOMP */
  57485. +
  57486. +#ifdef CONFIG_KLIPS_REGRESS
  57487. + { NET_IPSEC_REGRESS_PFKEY_LOSSAGE, "pfkey_lossage",
  57488. + &sysctl_ipsec_regress_pfkey_lossage,
  57489. + sizeof(int), 0644, NULL, &proc_dointvec},
  57490. +#endif /* CONFIG_KLIPS_REGRESS */
  57491. +
  57492. +#endif /* CONFIG_KLIPS_DEBUG */
  57493. + { NET_IPSEC_ICMP, "icmp", &sysctl_ipsec_icmp,
  57494. + sizeof(int), 0644, NULL, &proc_dointvec},
  57495. + { NET_IPSEC_INBOUND_POLICY_CHECK, "inbound_policy_check", &sysctl_ipsec_inbound_policy_check,
  57496. + sizeof(int), 0644, NULL, &proc_dointvec},
  57497. + { NET_IPSEC_TOS, "tos", &sysctl_ipsec_tos,
  57498. + sizeof(int), 0644, NULL, &proc_dointvec},
  57499. + {0}
  57500. +};
  57501. +
  57502. +static ctl_table ipsec_net_table[] = {
  57503. + { NET_IPSEC, "ipsec", NULL, 0, 0555, ipsec_table },
  57504. + { 0 }
  57505. +};
  57506. +
  57507. +static ctl_table ipsec_root_table[] = {
  57508. + { CTL_NET, "net", NULL, 0, 0555, ipsec_net_table },
  57509. + { 0 }
  57510. +};
  57511. +
  57512. +static struct ctl_table_header *ipsec_table_header;
  57513. +
  57514. +int ipsec_sysctl_register(void)
  57515. +{
  57516. + ipsec_table_header = register_sysctl_table(ipsec_root_table, 0);
  57517. + if (!ipsec_table_header) {
  57518. + return -ENOMEM;
  57519. + }
  57520. + return 0;
  57521. +}
  57522. +
  57523. +void ipsec_sysctl_unregister(void)
  57524. +{
  57525. + unregister_sysctl_table(ipsec_table_header);
  57526. +}
  57527. +
  57528. +#endif /* CONFIG_SYSCTL */
  57529. +
  57530. +/*
  57531. + * $Log: sysctl_net_ipsec.c,v $
  57532. + * Revision 1.17 2004/07/10 19:11:18 mcr
  57533. + * CONFIG_IPSEC -> CONFIG_KLIPS.
  57534. + *
  57535. + * Revision 1.16 2004/04/06 02:49:26 mcr
  57536. + * pullup of algo code from alg-branch.
  57537. + *
  57538. + * Revision 1.15 2002/04/24 07:55:32 mcr
  57539. + * #include patches and Makefiles for post-reorg compilation.
  57540. + *
  57541. + * Revision 1.14 2002/04/24 07:36:35 mcr
  57542. + * Moved from ./klips/net/ipsec/sysctl_net_ipsec.c,v
  57543. + *
  57544. + * Revision 1.13 2002/01/12 02:58:32 mcr
  57545. + * first regression test causes acquire messages to be lost
  57546. + * 100% of the time. This is to help testing of pluto.
  57547. + *
  57548. + * Revision 1.12 2001/06/14 19:35:13 rgb
  57549. + * Update copyright date.
  57550. + *
  57551. + * Revision 1.11 2001/02/26 19:58:13 rgb
  57552. + * Drop sysctl_ipsec_{no_eroute_pass,opportunistic}, replaced by magic SAs.
  57553. + *
  57554. + * Revision 1.10 2000/09/16 01:50:15 rgb
  57555. + * Protect sysctl_ipsec_debug_ipcomp with compiler defines too so that the
  57556. + * linker won't blame rj_delete() for missing symbols. ;-> Damn statics...
  57557. + *
  57558. + * Revision 1.9 2000/09/15 23:17:51 rgb
  57559. + * Moved stuff around to compile with debug off.
  57560. + *
  57561. + * Revision 1.8 2000/09/15 11:37:02 rgb
  57562. + * Merge in heavily modified Svenning Soerensen's <svenning@post5.tele.dk>
  57563. + * IPCOMP zlib deflate code.
  57564. + *
  57565. + * Revision 1.7 2000/09/15 07:37:15 rgb
  57566. + * Munged silly log comment that was causing a warning.
  57567. + *
  57568. + * Revision 1.6 2000/09/15 04:58:23 rgb
  57569. + * Added tos runtime switch.
  57570. + * Removed 'sysctl_ipsec_' prefix from /proc/sys/net/ipsec/ filenames.
  57571. + *
  57572. + * Revision 1.5 2000/09/12 03:25:28 rgb
  57573. + * Filled in and implemented sysctl.
  57574. + *
  57575. + * Revision 1.4 1999/04/11 00:29:03 henry
  57576. + * GPL boilerplate
  57577. + *
  57578. + * Revision 1.3 1999/04/06 04:54:29 rgb
  57579. + * Fix/Add RCSID Id: and Log: bits to make PHMDs happy. This includes
  57580. + * patch shell fixes.
  57581. + *
  57582. + */
  57583. --- /dev/null Tue Mar 11 13:02:56 2003
  57584. +++ linux/net/ipsec/trees.c Mon Feb 9 13:51:03 2004
  57585. @@ -0,0 +1,1214 @@
  57586. +/* trees.c -- output deflated data using Huffman coding
  57587. + * Copyright (C) 1995-2002 Jean-loup Gailly
  57588. + * For conditions of distribution and use, see copyright notice in zlib.h
  57589. + */
  57590. +
  57591. +/*
  57592. + * ALGORITHM
  57593. + *
  57594. + * The "deflation" process uses several Huffman trees. The more
  57595. + * common source values are represented by shorter bit sequences.
  57596. + *
  57597. + * Each code tree is stored in a compressed form which is itself
  57598. + * a Huffman encoding of the lengths of all the code strings (in
  57599. + * ascending order by source values). The actual code strings are
  57600. + * reconstructed from the lengths in the inflate process, as described
  57601. + * in the deflate specification.
  57602. + *
  57603. + * REFERENCES
  57604. + *
  57605. + * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
  57606. + * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
  57607. + *
  57608. + * Storer, James A.
  57609. + * Data Compression: Methods and Theory, pp. 49-50.
  57610. + * Computer Science Press, 1988. ISBN 0-7167-8156-5.
  57611. + *
  57612. + * Sedgewick, R.
  57613. + * Algorithms, p290.
  57614. + * Addison-Wesley, 1983. ISBN 0-201-06672-6.
  57615. + */
  57616. +
  57617. +/* @(#) $Id: trees.c,v 1.4 2004/07/10 07:48:39 mcr Exp $ */
  57618. +
  57619. +/* #define GEN_TREES_H */
  57620. +
  57621. +#include "deflate.h"
  57622. +
  57623. +#ifdef DEBUG
  57624. +# include <ctype.h>
  57625. +#endif
  57626. +
  57627. +/* ===========================================================================
  57628. + * Constants
  57629. + */
  57630. +
  57631. +#define MAX_BL_BITS 7
  57632. +/* Bit length codes must not exceed MAX_BL_BITS bits */
  57633. +
  57634. +#define END_BLOCK 256
  57635. +/* end of block literal code */
  57636. +
  57637. +#define REP_3_6 16
  57638. +/* repeat previous bit length 3-6 times (2 bits of repeat count) */
  57639. +
  57640. +#define REPZ_3_10 17
  57641. +/* repeat a zero length 3-10 times (3 bits of repeat count) */
  57642. +
  57643. +#define REPZ_11_138 18
  57644. +/* repeat a zero length 11-138 times (7 bits of repeat count) */
  57645. +
  57646. +local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
  57647. + = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};
  57648. +
  57649. +local const int extra_dbits[D_CODES] /* extra bits for each distance code */
  57650. + = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
  57651. +
  57652. +local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */
  57653. + = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
  57654. +
  57655. +local const uch bl_order[BL_CODES]
  57656. + = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
  57657. +/* The lengths of the bit length codes are sent in order of decreasing
  57658. + * probability, to avoid transmitting the lengths for unused bit length codes.
  57659. + */
  57660. +
  57661. +#define Buf_size (8 * 2*sizeof(char))
  57662. +/* Number of bits used within bi_buf. (bi_buf might be implemented on
  57663. + * more than 16 bits on some systems.)
  57664. + */
  57665. +
  57666. +/* ===========================================================================
  57667. + * Local data. These are initialized only once.
  57668. + */
  57669. +
  57670. +#define DIST_CODE_LEN 512 /* see definition of array dist_code below */
  57671. +
  57672. +#if defined(GEN_TREES_H) || !defined(STDC)
  57673. +/* non ANSI compilers may not accept trees.h */
  57674. +
  57675. +local ct_data static_ltree[L_CODES+2];
  57676. +/* The static literal tree. Since the bit lengths are imposed, there is no
  57677. + * need for the L_CODES extra codes used during heap construction. However
  57678. + * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
  57679. + * below).
  57680. + */
  57681. +
  57682. +local ct_data static_dtree[D_CODES];
  57683. +/* The static distance tree. (Actually a trivial tree since all codes use
  57684. + * 5 bits.)
  57685. + */
  57686. +
  57687. +uch _dist_code[DIST_CODE_LEN];
  57688. +/* Distance codes. The first 256 values correspond to the distances
  57689. + * 3 .. 258, the last 256 values correspond to the top 8 bits of
  57690. + * the 15 bit distances.
  57691. + */
  57692. +
  57693. +uch _length_code[MAX_MATCH-MIN_MATCH+1];
  57694. +/* length code for each normalized match length (0 == MIN_MATCH) */
  57695. +
  57696. +local int base_length[LENGTH_CODES];
  57697. +/* First normalized length for each code (0 = MIN_MATCH) */
  57698. +
  57699. +local int base_dist[D_CODES];
  57700. +/* First normalized distance for each code (0 = distance of 1) */
  57701. +
  57702. +#else
  57703. +# include "trees.h"
  57704. +#endif /* GEN_TREES_H */
  57705. +
  57706. +struct static_tree_desc_s {
  57707. + const ct_data *static_tree; /* static tree or NULL */
  57708. + const intf *extra_bits; /* extra bits for each code or NULL */
  57709. + int extra_base; /* base index for extra_bits */
  57710. + int elems; /* max number of elements in the tree */
  57711. + int max_length; /* max bit length for the codes */
  57712. +};
  57713. +
  57714. +local static_tree_desc static_l_desc =
  57715. +{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
  57716. +
  57717. +local static_tree_desc static_d_desc =
  57718. +{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS};
  57719. +
  57720. +local static_tree_desc static_bl_desc =
  57721. +{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS};
  57722. +
  57723. +/* ===========================================================================
  57724. + * Local (static) routines in this file.
  57725. + */
  57726. +
  57727. +local void tr_static_init OF((void));
  57728. +local void init_block OF((deflate_state *s));
  57729. +local void pqdownheap OF((deflate_state *s, ct_data *tree, int k));
  57730. +local void gen_bitlen OF((deflate_state *s, tree_desc *desc));
  57731. +local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count));
  57732. +local void build_tree OF((deflate_state *s, tree_desc *desc));
  57733. +local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code));
  57734. +local void send_tree OF((deflate_state *s, ct_data *tree, int max_code));
  57735. +local int build_bl_tree OF((deflate_state *s));
  57736. +local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
  57737. + int blcodes));
  57738. +local void compress_block OF((deflate_state *s, const ct_data *ltree,
  57739. + const ct_data *dtree));
  57740. +local void set_data_type OF((deflate_state *s));
  57741. +local unsigned bi_reverse OF((unsigned value, int length));
  57742. +local void bi_windup OF((deflate_state *s));
  57743. +local void bi_flush OF((deflate_state *s));
  57744. +local void copy_block OF((deflate_state *s, charf *buf, unsigned len,
  57745. + int header));
  57746. +
  57747. +#ifdef GEN_TREES_H
  57748. +local void gen_trees_header OF((void));
  57749. +#endif
  57750. +
  57751. +#ifndef DEBUG
  57752. +# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
  57753. + /* Send a code of the given tree. c and tree must not have side effects */
  57754. +
  57755. +#else /* DEBUG */
  57756. +# define send_code(s, c, tree) \
  57757. + { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
  57758. + send_bits(s, tree[c].Code, tree[c].Len); }
  57759. +#endif
  57760. +
  57761. +/* ===========================================================================
  57762. + * Output a short LSB first on the stream.
  57763. + * IN assertion: there is enough room in pendingBuf.
  57764. + */
  57765. +#define put_short(s, w) { \
  57766. + put_byte(s, (uch)((w) & 0xff)); \
  57767. + put_byte(s, (uch)((ush)(w) >> 8)); \
  57768. +}
  57769. +
  57770. +/* ===========================================================================
  57771. + * Send a value on a given number of bits.
  57772. + * IN assertion: length <= 16 and value fits in length bits.
  57773. + */
  57774. +#ifdef DEBUG
  57775. +local void send_bits OF((deflate_state *s, int value, int length));
  57776. +
  57777. +local void send_bits(s, value, length)
  57778. + deflate_state *s;
  57779. + int value; /* value to send */
  57780. + int length; /* number of bits */
  57781. +{
  57782. + Tracevv((stderr," l %2d v %4x ", length, value));
  57783. + Assert(length > 0 && length <= 15, "invalid length");
  57784. + s->bits_sent += (ulg)length;
  57785. +
  57786. + /* If not enough room in bi_buf, use (valid) bits from bi_buf and
  57787. + * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
  57788. + * unused bits in value.
  57789. + */
  57790. + if (s->bi_valid > (int)Buf_size - length) {
  57791. + s->bi_buf |= (value << s->bi_valid);
  57792. + put_short(s, s->bi_buf);
  57793. + s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
  57794. + s->bi_valid += length - Buf_size;
  57795. + } else {
  57796. + s->bi_buf |= value << s->bi_valid;
  57797. + s->bi_valid += length;
  57798. + }
  57799. +}
  57800. +#else /* !DEBUG */
  57801. +
  57802. +#define send_bits(s, value, length) \
  57803. +{ int len = length;\
  57804. + if (s->bi_valid > (int)Buf_size - len) {\
  57805. + int val = value;\
  57806. + s->bi_buf |= (val << s->bi_valid);\
  57807. + put_short(s, s->bi_buf);\
  57808. + s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
  57809. + s->bi_valid += len - Buf_size;\
  57810. + } else {\
  57811. + s->bi_buf |= (value) << s->bi_valid;\
  57812. + s->bi_valid += len;\
  57813. + }\
  57814. +}
  57815. +#endif /* DEBUG */
  57816. +
  57817. +
  57818. +#define MAX(a,b) (a >= b ? a : b)
  57819. +/* the arguments must not have side effects */
  57820. +
  57821. +/* ===========================================================================
  57822. + * Initialize the various 'constant' tables.
  57823. + */
  57824. +local void tr_static_init()
  57825. +{
  57826. +#if defined(GEN_TREES_H) || !defined(STDC)
  57827. + static int static_init_done = 0;
  57828. + int n; /* iterates over tree elements */
  57829. + int bits; /* bit counter */
  57830. + int length; /* length value */
  57831. + int code; /* code value */
  57832. + int dist; /* distance index */
  57833. + ush bl_count[MAX_BITS+1];
  57834. + /* number of codes at each bit length for an optimal tree */
  57835. +
  57836. + if (static_init_done) return;
  57837. +
  57838. + /* For some embedded targets, global variables are not initialized: */
  57839. + static_l_desc.static_tree = static_ltree;
  57840. + static_l_desc.extra_bits = extra_lbits;
  57841. + static_d_desc.static_tree = static_dtree;
  57842. + static_d_desc.extra_bits = extra_dbits;
  57843. + static_bl_desc.extra_bits = extra_blbits;
  57844. +
  57845. + /* Initialize the mapping length (0..255) -> length code (0..28) */
  57846. + length = 0;
  57847. + for (code = 0; code < LENGTH_CODES-1; code++) {
  57848. + base_length[code] = length;
  57849. + for (n = 0; n < (1<<extra_lbits[code]); n++) {
  57850. + _length_code[length++] = (uch)code;
  57851. + }
  57852. + }
  57853. + Assert (length == 256, "tr_static_init: length != 256");
  57854. + /* Note that the length 255 (match length 258) can be represented
  57855. + * in two different ways: code 284 + 5 bits or code 285, so we
  57856. + * overwrite length_code[255] to use the best encoding:
  57857. + */
  57858. + _length_code[length-1] = (uch)code;
  57859. +
  57860. + /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
  57861. + dist = 0;
  57862. + for (code = 0 ; code < 16; code++) {
  57863. + base_dist[code] = dist;
  57864. + for (n = 0; n < (1<<extra_dbits[code]); n++) {
  57865. + _dist_code[dist++] = (uch)code;
  57866. + }
  57867. + }
  57868. + Assert (dist == 256, "tr_static_init: dist != 256");
  57869. + dist >>= 7; /* from now on, all distances are divided by 128 */
  57870. + for ( ; code < D_CODES; code++) {
  57871. + base_dist[code] = dist << 7;
  57872. + for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
  57873. + _dist_code[256 + dist++] = (uch)code;
  57874. + }
  57875. + }
  57876. + Assert (dist == 256, "tr_static_init: 256+dist != 512");
  57877. +
  57878. + /* Construct the codes of the static literal tree */
  57879. + for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
  57880. + n = 0;
  57881. + while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
  57882. + while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
  57883. + while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
  57884. + while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
  57885. + /* Codes 286 and 287 do not exist, but we must include them in the
  57886. + * tree construction to get a canonical Huffman tree (longest code
  57887. + * all ones)
  57888. + */
  57889. + gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
  57890. +
  57891. + /* The static distance tree is trivial: */
  57892. + for (n = 0; n < D_CODES; n++) {
  57893. + static_dtree[n].Len = 5;
  57894. + static_dtree[n].Code = bi_reverse((unsigned)n, 5);
  57895. + }
  57896. + static_init_done = 1;
  57897. +
  57898. +# ifdef GEN_TREES_H
  57899. + gen_trees_header();
  57900. +# endif
  57901. +#endif /* defined(GEN_TREES_H) || !defined(STDC) */
  57902. +}
  57903. +
  57904. +/* ===========================================================================
  57905. + * Genererate the file trees.h describing the static trees.
  57906. + */
  57907. +#ifdef GEN_TREES_H
  57908. +# ifndef DEBUG
  57909. +# include <stdio.h>
  57910. +# endif
  57911. +
  57912. +# define SEPARATOR(i, last, width) \
  57913. + ((i) == (last)? "\n};\n\n" : \
  57914. + ((i) % (width) == (width)-1 ? ",\n" : ", "))
  57915. +
  57916. +void gen_trees_header()
  57917. +{
  57918. + FILE *header = fopen("trees.h", "w");
  57919. + int i;
  57920. +
  57921. + Assert (header != NULL, "Can't open trees.h");
  57922. + fprintf(header,
  57923. + "/* header created automatically with -DGEN_TREES_H */\n\n");
  57924. +
  57925. + fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n");
  57926. + for (i = 0; i < L_CODES+2; i++) {
  57927. + fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code,
  57928. + static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
  57929. + }
  57930. +
  57931. + fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n");
  57932. + for (i = 0; i < D_CODES; i++) {
  57933. + fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code,
  57934. + static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
  57935. + }
  57936. +
  57937. + fprintf(header, "const uch _dist_code[DIST_CODE_LEN] = {\n");
  57938. + for (i = 0; i < DIST_CODE_LEN; i++) {
  57939. + fprintf(header, "%2u%s", _dist_code[i],
  57940. + SEPARATOR(i, DIST_CODE_LEN-1, 20));
  57941. + }
  57942. +
  57943. + fprintf(header, "const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {\n");
  57944. + for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) {
  57945. + fprintf(header, "%2u%s", _length_code[i],
  57946. + SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
  57947. + }
  57948. +
  57949. + fprintf(header, "local const int base_length[LENGTH_CODES] = {\n");
  57950. + for (i = 0; i < LENGTH_CODES; i++) {
  57951. + fprintf(header, "%1u%s", base_length[i],
  57952. + SEPARATOR(i, LENGTH_CODES-1, 20));
  57953. + }
  57954. +
  57955. + fprintf(header, "local const int base_dist[D_CODES] = {\n");
  57956. + for (i = 0; i < D_CODES; i++) {
  57957. + fprintf(header, "%5u%s", base_dist[i],
  57958. + SEPARATOR(i, D_CODES-1, 10));
  57959. + }
  57960. +
  57961. + fclose(header);
  57962. +}
  57963. +#endif /* GEN_TREES_H */
  57964. +
  57965. +/* ===========================================================================
  57966. + * Initialize the tree data structures for a new zlib stream.
  57967. + */
  57968. +void _tr_init(s)
  57969. + deflate_state *s;
  57970. +{
  57971. + tr_static_init();
  57972. +
  57973. + s->l_desc.dyn_tree = s->dyn_ltree;
  57974. + s->l_desc.stat_desc = &static_l_desc;
  57975. +
  57976. + s->d_desc.dyn_tree = s->dyn_dtree;
  57977. + s->d_desc.stat_desc = &static_d_desc;
  57978. +
  57979. + s->bl_desc.dyn_tree = s->bl_tree;
  57980. + s->bl_desc.stat_desc = &static_bl_desc;
  57981. +
  57982. + s->bi_buf = 0;
  57983. + s->bi_valid = 0;
  57984. + s->last_eob_len = 8; /* enough lookahead for inflate */
  57985. +#ifdef DEBUG
  57986. + s->compressed_len = 0L;
  57987. + s->bits_sent = 0L;
  57988. +#endif
  57989. +
  57990. + /* Initialize the first block of the first file: */
  57991. + init_block(s);
  57992. +}
  57993. +
  57994. +/* ===========================================================================
  57995. + * Initialize a new block.
  57996. + */
  57997. +local void init_block(s)
  57998. + deflate_state *s;
  57999. +{
  58000. + int n; /* iterates over tree elements */
  58001. +
  58002. + /* Initialize the trees. */
  58003. + for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0;
  58004. + for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0;
  58005. + for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
  58006. +
  58007. + s->dyn_ltree[END_BLOCK].Freq = 1;
  58008. + s->opt_len = s->static_len = 0L;
  58009. + s->last_lit = s->matches = 0;
  58010. +}
  58011. +
  58012. +#define SMALLEST 1
  58013. +/* Index within the heap array of least frequent node in the Huffman tree */
  58014. +
  58015. +
  58016. +/* ===========================================================================
  58017. + * Remove the smallest element from the heap and recreate the heap with
  58018. + * one less element. Updates heap and heap_len.
  58019. + */
  58020. +#define pqremove(s, tree, top) \
  58021. +{\
  58022. + top = s->heap[SMALLEST]; \
  58023. + s->heap[SMALLEST] = s->heap[s->heap_len--]; \
  58024. + pqdownheap(s, tree, SMALLEST); \
  58025. +}
  58026. +
  58027. +/* ===========================================================================
  58028. + * Compares to subtrees, using the tree depth as tie breaker when
  58029. + * the subtrees have equal frequency. This minimizes the worst case length.
  58030. + */
  58031. +#define smaller(tree, n, m, depth) \
  58032. + (tree[n].Freq < tree[m].Freq || \
  58033. + (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
  58034. +
  58035. +/* ===========================================================================
  58036. + * Restore the heap property by moving down the tree starting at node k,
  58037. + * exchanging a node with the smallest of its two sons if necessary, stopping
  58038. + * when the heap property is re-established (each father smaller than its
  58039. + * two sons).
  58040. + */
  58041. +local void pqdownheap(s, tree, k)
  58042. + deflate_state *s;
  58043. + ct_data *tree; /* the tree to restore */
  58044. + int k; /* node to move down */
  58045. +{
  58046. + int v = s->heap[k];
  58047. + int j = k << 1; /* left son of k */
  58048. + while (j <= s->heap_len) {
  58049. + /* Set j to the smallest of the two sons: */
  58050. + if (j < s->heap_len &&
  58051. + smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
  58052. + j++;
  58053. + }
  58054. + /* Exit if v is smaller than both sons */
  58055. + if (smaller(tree, v, s->heap[j], s->depth)) break;
  58056. +
  58057. + /* Exchange v with the smallest son */
  58058. + s->heap[k] = s->heap[j]; k = j;
  58059. +
  58060. + /* And continue down the tree, setting j to the left son of k */
  58061. + j <<= 1;
  58062. + }
  58063. + s->heap[k] = v;
  58064. +}
  58065. +
  58066. +/* ===========================================================================
  58067. + * Compute the optimal bit lengths for a tree and update the total bit length
  58068. + * for the current block.
  58069. + * IN assertion: the fields freq and dad are set, heap[heap_max] and
  58070. + * above are the tree nodes sorted by increasing frequency.
  58071. + * OUT assertions: the field len is set to the optimal bit length, the
  58072. + * array bl_count contains the frequencies for each bit length.
  58073. + * The length opt_len is updated; static_len is also updated if stree is
  58074. + * not null.
  58075. + */
  58076. +local void gen_bitlen(s, desc)
  58077. + deflate_state *s;
  58078. + tree_desc *desc; /* the tree descriptor */
  58079. +{
  58080. + ct_data *tree = desc->dyn_tree;
  58081. + int max_code = desc->max_code;
  58082. + const ct_data *stree = desc->stat_desc->static_tree;
  58083. + const intf *extra = desc->stat_desc->extra_bits;
  58084. + int base = desc->stat_desc->extra_base;
  58085. + int max_length = desc->stat_desc->max_length;
  58086. + int h; /* heap index */
  58087. + int n, m; /* iterate over the tree elements */
  58088. + int bits; /* bit length */
  58089. + int xbits; /* extra bits */
  58090. + ush f; /* frequency */
  58091. + int overflow = 0; /* number of elements with bit length too large */
  58092. +
  58093. + for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
  58094. +
  58095. + /* In a first pass, compute the optimal bit lengths (which may
  58096. + * overflow in the case of the bit length tree).
  58097. + */
  58098. + tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
  58099. +
  58100. + for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
  58101. + n = s->heap[h];
  58102. + bits = tree[tree[n].Dad].Len + 1;
  58103. + if (bits > max_length) bits = max_length, overflow++;
  58104. + tree[n].Len = (ush)bits;
  58105. + /* We overwrite tree[n].Dad which is no longer needed */
  58106. +
  58107. + if (n > max_code) continue; /* not a leaf node */
  58108. +
  58109. + s->bl_count[bits]++;
  58110. + xbits = 0;
  58111. + if (n >= base) xbits = extra[n-base];
  58112. + f = tree[n].Freq;
  58113. + s->opt_len += (ulg)f * (bits + xbits);
  58114. + if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
  58115. + }
  58116. + if (overflow == 0) return;
  58117. +
  58118. + Trace((stderr,"\nbit length overflow\n"));
  58119. + /* This happens for example on obj2 and pic of the Calgary corpus */
  58120. +
  58121. + /* Find the first bit length which could increase: */
  58122. + do {
  58123. + bits = max_length-1;
  58124. + while (s->bl_count[bits] == 0) bits--;
  58125. + s->bl_count[bits]--; /* move one leaf down the tree */
  58126. + s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
  58127. + s->bl_count[max_length]--;
  58128. + /* The brother of the overflow item also moves one step up,
  58129. + * but this does not affect bl_count[max_length]
  58130. + */
  58131. + overflow -= 2;
  58132. + } while (overflow > 0);
  58133. +
  58134. + /* Now recompute all bit lengths, scanning in increasing frequency.
  58135. + * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
  58136. + * lengths instead of fixing only the wrong ones. This idea is taken
  58137. + * from 'ar' written by Haruhiko Okumura.)
  58138. + */
  58139. + for (bits = max_length; bits != 0; bits--) {
  58140. + n = s->bl_count[bits];
  58141. + while (n != 0) {
  58142. + m = s->heap[--h];
  58143. + if (m > max_code) continue;
  58144. + if (tree[m].Len != (unsigned) bits) {
  58145. + Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
  58146. + s->opt_len += ((long)bits - (long)tree[m].Len)
  58147. + *(long)tree[m].Freq;
  58148. + tree[m].Len = (ush)bits;
  58149. + }
  58150. + n--;
  58151. + }
  58152. + }
  58153. +}
  58154. +
  58155. +/* ===========================================================================
  58156. + * Generate the codes for a given tree and bit counts (which need not be
  58157. + * optimal).
  58158. + * IN assertion: the array bl_count contains the bit length statistics for
  58159. + * the given tree and the field len is set for all tree elements.
  58160. + * OUT assertion: the field code is set for all tree elements of non
  58161. + * zero code length.
  58162. + */
  58163. +local void gen_codes (tree, max_code, bl_count)
  58164. + ct_data *tree; /* the tree to decorate */
  58165. + int max_code; /* largest code with non zero frequency */
  58166. + ushf *bl_count; /* number of codes at each bit length */
  58167. +{
  58168. + ush next_code[MAX_BITS+1]; /* next code value for each bit length */
  58169. + ush code = 0; /* running code value */
  58170. + int bits; /* bit index */
  58171. + int n; /* code index */
  58172. +
  58173. + /* The distribution counts are first used to generate the code values
  58174. + * without bit reversal.
  58175. + */
  58176. + for (bits = 1; bits <= MAX_BITS; bits++) {
  58177. + next_code[bits] = code = (code + bl_count[bits-1]) << 1;
  58178. + }
  58179. + /* Check that the bit counts in bl_count are consistent. The last code
  58180. + * must be all ones.
  58181. + */
  58182. + Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
  58183. + "inconsistent bit counts");
  58184. + Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
  58185. +
  58186. + for (n = 0; n <= max_code; n++) {
  58187. + int len = tree[n].Len;
  58188. + if (len == 0) continue;
  58189. + /* Now reverse the bits */
  58190. + tree[n].Code = bi_reverse(next_code[len]++, len);
  58191. +
  58192. + Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
  58193. + n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
  58194. + }
  58195. +}
  58196. +
  58197. +/* ===========================================================================
  58198. + * Construct one Huffman tree and assigns the code bit strings and lengths.
  58199. + * Update the total bit length for the current block.
  58200. + * IN assertion: the field freq is set for all tree elements.
  58201. + * OUT assertions: the fields len and code are set to the optimal bit length
  58202. + * and corresponding code. The length opt_len is updated; static_len is
  58203. + * also updated if stree is not null. The field max_code is set.
  58204. + */
  58205. +local void build_tree(s, desc)
  58206. + deflate_state *s;
  58207. + tree_desc *desc; /* the tree descriptor */
  58208. +{
  58209. + ct_data *tree = desc->dyn_tree;
  58210. + const ct_data *stree = desc->stat_desc->static_tree;
  58211. + int elems = desc->stat_desc->elems;
  58212. + int n, m; /* iterate over heap elements */
  58213. + int max_code = -1; /* largest code with non zero frequency */
  58214. + int node; /* new node being created */
  58215. +
  58216. + /* Construct the initial heap, with least frequent element in
  58217. + * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  58218. + * heap[0] is not used.
  58219. + */
  58220. + s->heap_len = 0, s->heap_max = HEAP_SIZE;
  58221. +
  58222. + for (n = 0; n < elems; n++) {
  58223. + if (tree[n].Freq != 0) {
  58224. + s->heap[++(s->heap_len)] = max_code = n;
  58225. + s->depth[n] = 0;
  58226. + } else {
  58227. + tree[n].Len = 0;
  58228. + }
  58229. + }
  58230. +
  58231. + /* The pkzip format requires that at least one distance code exists,
  58232. + * and that at least one bit should be sent even if there is only one
  58233. + * possible code. So to avoid special checks later on we force at least
  58234. + * two codes of non zero frequency.
  58235. + */
  58236. + while (s->heap_len < 2) {
  58237. + node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
  58238. + tree[node].Freq = 1;
  58239. + s->depth[node] = 0;
  58240. + s->opt_len--; if (stree) s->static_len -= stree[node].Len;
  58241. + /* node is 0 or 1 so it does not have extra bits */
  58242. + }
  58243. + desc->max_code = max_code;
  58244. +
  58245. + /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  58246. + * establish sub-heaps of increasing lengths:
  58247. + */
  58248. + for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
  58249. +
  58250. + /* Construct the Huffman tree by repeatedly combining the least two
  58251. + * frequent nodes.
  58252. + */
  58253. + node = elems; /* next internal node of the tree */
  58254. + do {
  58255. + pqremove(s, tree, n); /* n = node of least frequency */
  58256. + m = s->heap[SMALLEST]; /* m = node of next least frequency */
  58257. +
  58258. + s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
  58259. + s->heap[--(s->heap_max)] = m;
  58260. +
  58261. + /* Create a new node father of n and m */
  58262. + tree[node].Freq = tree[n].Freq + tree[m].Freq;
  58263. + s->depth[node] = (uch) (MAX(s->depth[n], s->depth[m]) + 1);
  58264. + tree[n].Dad = tree[m].Dad = (ush)node;
  58265. +#ifdef DUMP_BL_TREE
  58266. + if (tree == s->bl_tree) {
  58267. + fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
  58268. + node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
  58269. + }
  58270. +#endif
  58271. + /* and insert the new node in the heap */
  58272. + s->heap[SMALLEST] = node++;
  58273. + pqdownheap(s, tree, SMALLEST);
  58274. +
  58275. + } while (s->heap_len >= 2);
  58276. +
  58277. + s->heap[--(s->heap_max)] = s->heap[SMALLEST];
  58278. +
  58279. + /* At this point, the fields freq and dad are set. We can now
  58280. + * generate the bit lengths.
  58281. + */
  58282. + gen_bitlen(s, (tree_desc *)desc);
  58283. +
  58284. + /* The field len is now set, we can generate the bit codes */
  58285. + gen_codes ((ct_data *)tree, max_code, s->bl_count);
  58286. +}
  58287. +
  58288. +/* ===========================================================================
  58289. + * Scan a literal or distance tree to determine the frequencies of the codes
  58290. + * in the bit length tree.
  58291. + */
  58292. +local void scan_tree (s, tree, max_code)
  58293. + deflate_state *s;
  58294. + ct_data *tree; /* the tree to be scanned */
  58295. + int max_code; /* and its largest code of non zero frequency */
  58296. +{
  58297. + int n; /* iterates over all tree elements */
  58298. + int prevlen = -1; /* last emitted length */
  58299. + int curlen; /* length of current code */
  58300. + int nextlen = tree[0].Len; /* length of next code */
  58301. + int count = 0; /* repeat count of the current code */
  58302. + int max_count = 7; /* max repeat count */
  58303. + int min_count = 4; /* min repeat count */
  58304. +
  58305. + if (nextlen == 0) max_count = 138, min_count = 3;
  58306. + tree[max_code+1].Len = (ush)0xffff; /* guard */
  58307. +
  58308. + for (n = 0; n <= max_code; n++) {
  58309. + curlen = nextlen; nextlen = tree[n+1].Len;
  58310. + if (++count < max_count && curlen == nextlen) {
  58311. + continue;
  58312. + } else if (count < min_count) {
  58313. + s->bl_tree[curlen].Freq += count;
  58314. + } else if (curlen != 0) {
  58315. + if (curlen != prevlen) s->bl_tree[curlen].Freq++;
  58316. + s->bl_tree[REP_3_6].Freq++;
  58317. + } else if (count <= 10) {
  58318. + s->bl_tree[REPZ_3_10].Freq++;
  58319. + } else {
  58320. + s->bl_tree[REPZ_11_138].Freq++;
  58321. + }
  58322. + count = 0; prevlen = curlen;
  58323. + if (nextlen == 0) {
  58324. + max_count = 138, min_count = 3;
  58325. + } else if (curlen == nextlen) {
  58326. + max_count = 6, min_count = 3;
  58327. + } else {
  58328. + max_count = 7, min_count = 4;
  58329. + }
  58330. + }
  58331. +}
  58332. +
  58333. +/* ===========================================================================
  58334. + * Send a literal or distance tree in compressed form, using the codes in
  58335. + * bl_tree.
  58336. + */
  58337. +local void send_tree (s, tree, max_code)
  58338. + deflate_state *s;
  58339. + ct_data *tree; /* the tree to be scanned */
  58340. + int max_code; /* and its largest code of non zero frequency */
  58341. +{
  58342. + int n; /* iterates over all tree elements */
  58343. + int prevlen = -1; /* last emitted length */
  58344. + int curlen; /* length of current code */
  58345. + int nextlen = tree[0].Len; /* length of next code */
  58346. + int count = 0; /* repeat count of the current code */
  58347. + int max_count = 7; /* max repeat count */
  58348. + int min_count = 4; /* min repeat count */
  58349. +
  58350. + /* tree[max_code+1].Len = -1; */ /* guard already set */
  58351. + if (nextlen == 0) max_count = 138, min_count = 3;
  58352. +
  58353. + for (n = 0; n <= max_code; n++) {
  58354. + curlen = nextlen; nextlen = tree[n+1].Len;
  58355. + if (++count < max_count && curlen == nextlen) {
  58356. + continue;
  58357. + } else if (count < min_count) {
  58358. + do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
  58359. +
  58360. + } else if (curlen != 0) {
  58361. + if (curlen != prevlen) {
  58362. + send_code(s, curlen, s->bl_tree); count--;
  58363. + }
  58364. + Assert(count >= 3 && count <= 6, " 3_6?");
  58365. + send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
  58366. +
  58367. + } else if (count <= 10) {
  58368. + send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
  58369. +
  58370. + } else {
  58371. + send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
  58372. + }
  58373. + count = 0; prevlen = curlen;
  58374. + if (nextlen == 0) {
  58375. + max_count = 138, min_count = 3;
  58376. + } else if (curlen == nextlen) {
  58377. + max_count = 6, min_count = 3;
  58378. + } else {
  58379. + max_count = 7, min_count = 4;
  58380. + }
  58381. + }
  58382. +}
  58383. +
  58384. +/* ===========================================================================
  58385. + * Construct the Huffman tree for the bit lengths and return the index in
  58386. + * bl_order of the last bit length code to send.
  58387. + */
  58388. +local int build_bl_tree(s)
  58389. + deflate_state *s;
  58390. +{
  58391. + int max_blindex; /* index of last bit length code of non zero freq */
  58392. +
  58393. + /* Determine the bit length frequencies for literal and distance trees */
  58394. + scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
  58395. + scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
  58396. +
  58397. + /* Build the bit length tree: */
  58398. + build_tree(s, (tree_desc *)(&(s->bl_desc)));
  58399. + /* opt_len now includes the length of the tree representations, except
  58400. + * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
  58401. + */
  58402. +
  58403. + /* Determine the number of bit length codes to send. The pkzip format
  58404. + * requires that at least 4 bit length codes be sent. (appnote.txt says
  58405. + * 3 but the actual value used is 4.)
  58406. + */
  58407. + for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
  58408. + if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
  58409. + }
  58410. + /* Update opt_len to include the bit length tree and counts */
  58411. + s->opt_len += 3*(max_blindex+1) + 5+5+4;
  58412. + Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
  58413. + s->opt_len, s->static_len));
  58414. +
  58415. + return max_blindex;
  58416. +}
  58417. +
  58418. +/* ===========================================================================
  58419. + * Send the header for a block using dynamic Huffman trees: the counts, the
  58420. + * lengths of the bit length codes, the literal tree and the distance tree.
  58421. + * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
  58422. + */
  58423. +local void send_all_trees(s, lcodes, dcodes, blcodes)
  58424. + deflate_state *s;
  58425. + int lcodes, dcodes, blcodes; /* number of codes for each tree */
  58426. +{
  58427. + int rank; /* index in bl_order */
  58428. +
  58429. + Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
  58430. + Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
  58431. + "too many codes");
  58432. + Tracev((stderr, "\nbl counts: "));
  58433. + send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
  58434. + send_bits(s, dcodes-1, 5);
  58435. + send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */
  58436. + for (rank = 0; rank < blcodes; rank++) {
  58437. + Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
  58438. + send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
  58439. + }
  58440. + Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
  58441. +
  58442. + send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
  58443. + Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
  58444. +
  58445. + send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
  58446. + Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
  58447. +}
  58448. +
  58449. +/* ===========================================================================
  58450. + * Send a stored block
  58451. + */
  58452. +void _tr_stored_block(s, buf, stored_len, eof)
  58453. + deflate_state *s;
  58454. + charf *buf; /* input block */
  58455. + ulg stored_len; /* length of input block */
  58456. + int eof; /* true if this is the last block for a file */
  58457. +{
  58458. + send_bits(s, (STORED_BLOCK<<1)+eof, 3); /* send block type */
  58459. +#ifdef DEBUG
  58460. + s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
  58461. + s->compressed_len += (stored_len + 4) << 3;
  58462. +#endif
  58463. + copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
  58464. +}
  58465. +
  58466. +/* ===========================================================================
  58467. + * Send one empty static block to give enough lookahead for inflate.
  58468. + * This takes 10 bits, of which 7 may remain in the bit buffer.
  58469. + * The current inflate code requires 9 bits of lookahead. If the
  58470. + * last two codes for the previous block (real code plus EOB) were coded
  58471. + * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
  58472. + * the last real code. In this case we send two empty static blocks instead
  58473. + * of one. (There are no problems if the previous block is stored or fixed.)
  58474. + * To simplify the code, we assume the worst case of last real code encoded
  58475. + * on one bit only.
  58476. + */
  58477. +void _tr_align(s)
  58478. + deflate_state *s;
  58479. +{
  58480. + send_bits(s, STATIC_TREES<<1, 3);
  58481. + send_code(s, END_BLOCK, static_ltree);
  58482. +#ifdef DEBUG
  58483. + s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
  58484. +#endif
  58485. + bi_flush(s);
  58486. + /* Of the 10 bits for the empty block, we have already sent
  58487. + * (10 - bi_valid) bits. The lookahead for the last real code (before
  58488. + * the EOB of the previous block) was thus at least one plus the length
  58489. + * of the EOB plus what we have just sent of the empty static block.
  58490. + */
  58491. + if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
  58492. + send_bits(s, STATIC_TREES<<1, 3);
  58493. + send_code(s, END_BLOCK, static_ltree);
  58494. +#ifdef DEBUG
  58495. + s->compressed_len += 10L;
  58496. +#endif
  58497. + bi_flush(s);
  58498. + }
  58499. + s->last_eob_len = 7;
  58500. +}
  58501. +
  58502. +/* ===========================================================================
  58503. + * Determine the best encoding for the current block: dynamic trees, static
  58504. + * trees or store, and output the encoded block to the zip file.
  58505. + */
  58506. +void _tr_flush_block(s, buf, stored_len, eof)
  58507. + deflate_state *s;
  58508. + charf *buf; /* input block, or NULL if too old */
  58509. + ulg stored_len; /* length of input block */
  58510. + int eof; /* true if this is the last block for a file */
  58511. +{
  58512. + ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
  58513. + int max_blindex = 0; /* index of last bit length code of non zero freq */
  58514. +
  58515. + /* Build the Huffman trees unless a stored block is forced */
  58516. + if (s->level > 0) {
  58517. +
  58518. + /* Check if the file is ascii or binary */
  58519. + if (s->data_type == Z_UNKNOWN) set_data_type(s);
  58520. +
  58521. + /* Construct the literal and distance trees */
  58522. + build_tree(s, (tree_desc *)(&(s->l_desc)));
  58523. + Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
  58524. + s->static_len));
  58525. +
  58526. + build_tree(s, (tree_desc *)(&(s->d_desc)));
  58527. + Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
  58528. + s->static_len));
  58529. + /* At this point, opt_len and static_len are the total bit lengths of
  58530. + * the compressed block data, excluding the tree representations.
  58531. + */
  58532. +
  58533. + /* Build the bit length tree for the above two trees, and get the index
  58534. + * in bl_order of the last bit length code to send.
  58535. + */
  58536. + max_blindex = build_bl_tree(s);
  58537. +
  58538. + /* Determine the best encoding. Compute first the block length in bytes*/
  58539. + opt_lenb = (s->opt_len+3+7)>>3;
  58540. + static_lenb = (s->static_len+3+7)>>3;
  58541. +
  58542. + Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
  58543. + opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
  58544. + s->last_lit));
  58545. +
  58546. + if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
  58547. +
  58548. + } else {
  58549. + Assert(buf != (char*)0, "lost buf");
  58550. + opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
  58551. + }
  58552. +
  58553. +#ifdef FORCE_STORED
  58554. + if (buf != (char*)0) { /* force stored block */
  58555. +#else
  58556. + if (stored_len+4 <= opt_lenb && buf != (char*)0) {
  58557. + /* 4: two words for the lengths */
  58558. +#endif
  58559. + /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
  58560. + * Otherwise we can't have processed more than WSIZE input bytes since
  58561. + * the last block flush, because compression would have been
  58562. + * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  58563. + * transform a block into a stored block.
  58564. + */
  58565. + _tr_stored_block(s, buf, stored_len, eof);
  58566. +
  58567. +#ifdef FORCE_STATIC
  58568. + } else if (static_lenb >= 0) { /* force static trees */
  58569. +#else
  58570. + } else if (static_lenb == opt_lenb) {
  58571. +#endif
  58572. + send_bits(s, (STATIC_TREES<<1)+eof, 3);
  58573. + compress_block(s, static_ltree, static_dtree);
  58574. +#ifdef DEBUG
  58575. + s->compressed_len += 3 + s->static_len;
  58576. +#endif
  58577. + } else {
  58578. + send_bits(s, (DYN_TREES<<1)+eof, 3);
  58579. + send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
  58580. + max_blindex+1);
  58581. + compress_block(s, s->dyn_ltree, s->dyn_dtree);
  58582. +#ifdef DEBUG
  58583. + s->compressed_len += 3 + s->opt_len;
  58584. +#endif
  58585. + }
  58586. + Assert (s->compressed_len == s->bits_sent, "bad compressed size");
  58587. + /* The above check is made mod 2^32, for files larger than 512 MB
  58588. + * and uLong implemented on 32 bits.
  58589. + */
  58590. + init_block(s);
  58591. +
  58592. + if (eof) {
  58593. + bi_windup(s);
  58594. +#ifdef DEBUG
  58595. + s->compressed_len += 7; /* align on byte boundary */
  58596. +#endif
  58597. + }
  58598. + Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
  58599. + s->compressed_len-7*eof));
  58600. +}
  58601. +
  58602. +/* ===========================================================================
  58603. + * Save the match info and tally the frequency counts. Return true if
  58604. + * the current block must be flushed.
  58605. + */
  58606. +int _tr_tally (s, dist, lc)
  58607. + deflate_state *s;
  58608. + unsigned dist; /* distance of matched string */
  58609. + unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
  58610. +{
  58611. + s->d_buf[s->last_lit] = (ush)dist;
  58612. + s->l_buf[s->last_lit++] = (uch)lc;
  58613. + if (dist == 0) {
  58614. + /* lc is the unmatched char */
  58615. + s->dyn_ltree[lc].Freq++;
  58616. + } else {
  58617. + s->matches++;
  58618. + /* Here, lc is the match length - MIN_MATCH */
  58619. + dist--; /* dist = match distance - 1 */
  58620. + Assert((ush)dist < (ush)MAX_DIST(s) &&
  58621. + (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
  58622. + (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match");
  58623. +
  58624. + s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++;
  58625. + s->dyn_dtree[d_code(dist)].Freq++;
  58626. + }
  58627. +
  58628. +#ifdef TRUNCATE_BLOCK
  58629. + /* Try to guess if it is profitable to stop the current block here */
  58630. + if ((s->last_lit & 0x1fff) == 0 && s->level > 2) {
  58631. + /* Compute an upper bound for the compressed length */
  58632. + ulg out_length = (ulg)s->last_lit*8L;
  58633. + ulg in_length = (ulg)((long)s->strstart - s->block_start);
  58634. + int dcode;
  58635. + for (dcode = 0; dcode < D_CODES; dcode++) {
  58636. + out_length += (ulg)s->dyn_dtree[dcode].Freq *
  58637. + (5L+extra_dbits[dcode]);
  58638. + }
  58639. + out_length >>= 3;
  58640. + Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
  58641. + s->last_lit, in_length, out_length,
  58642. + 100L - out_length*100L/in_length));
  58643. + if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
  58644. + }
  58645. +#endif
  58646. + return (s->last_lit == s->lit_bufsize-1);
  58647. + /* We avoid equality with lit_bufsize because of wraparound at 64K
  58648. + * on 16 bit machines and because stored blocks are restricted to
  58649. + * 64K-1 bytes.
  58650. + */
  58651. +}
  58652. +
  58653. +/* ===========================================================================
  58654. + * Send the block data compressed using the given Huffman trees
  58655. + */
  58656. +local void compress_block(s, ltree, dtree)
  58657. + deflate_state *s;
  58658. + const ct_data *ltree; /* literal tree */
  58659. + const ct_data *dtree; /* distance tree */
  58660. +{
  58661. + unsigned dist; /* distance of matched string */
  58662. + int lc; /* match length or unmatched char (if dist == 0) */
  58663. + unsigned lx = 0; /* running index in l_buf */
  58664. + unsigned code; /* the code to send */
  58665. + int extra; /* number of extra bits to send */
  58666. +
  58667. + if (s->last_lit != 0) do {
  58668. + dist = s->d_buf[lx];
  58669. + lc = s->l_buf[lx++];
  58670. + if (dist == 0) {
  58671. + send_code(s, lc, ltree); /* send a literal byte */
  58672. + Tracecv(isgraph(lc), (stderr," '%c' ", lc));
  58673. + } else {
  58674. + /* Here, lc is the match length - MIN_MATCH */
  58675. + code = _length_code[lc];
  58676. + send_code(s, code+LITERALS+1, ltree); /* send the length code */
  58677. + extra = extra_lbits[code];
  58678. + if (extra != 0) {
  58679. + lc -= base_length[code];
  58680. + send_bits(s, lc, extra); /* send the extra length bits */
  58681. + }
  58682. + dist--; /* dist is now the match distance - 1 */
  58683. + code = d_code(dist);
  58684. + Assert (code < D_CODES, "bad d_code");
  58685. +
  58686. + send_code(s, code, dtree); /* send the distance code */
  58687. + extra = extra_dbits[code];
  58688. + if (extra != 0) {
  58689. + dist -= base_dist[code];
  58690. + send_bits(s, dist, extra); /* send the extra distance bits */
  58691. + }
  58692. + } /* literal or match pair ? */
  58693. +
  58694. + /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
  58695. + Assert(s->pending < s->lit_bufsize + 2*lx, "pendingBuf overflow");
  58696. +
  58697. + } while (lx < s->last_lit);
  58698. +
  58699. + send_code(s, END_BLOCK, ltree);
  58700. + s->last_eob_len = ltree[END_BLOCK].Len;
  58701. +}
  58702. +
  58703. +/* ===========================================================================
  58704. + * Set the data type to ASCII or BINARY, using a crude approximation:
  58705. + * binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
  58706. + * IN assertion: the fields freq of dyn_ltree are set and the total of all
  58707. + * frequencies does not exceed 64K (to fit in an int on 16 bit machines).
  58708. + */
  58709. +local void set_data_type(s)
  58710. + deflate_state *s;
  58711. +{
  58712. + int n = 0;
  58713. + unsigned ascii_freq = 0;
  58714. + unsigned bin_freq = 0;
  58715. + while (n < 7) bin_freq += s->dyn_ltree[n++].Freq;
  58716. + while (n < 128) ascii_freq += s->dyn_ltree[n++].Freq;
  58717. + while (n < LITERALS) bin_freq += s->dyn_ltree[n++].Freq;
  58718. + s->data_type = (Byte)(bin_freq > (ascii_freq >> 2) ? Z_BINARY : Z_ASCII);
  58719. +}
  58720. +
  58721. +/* ===========================================================================
  58722. + * Reverse the first len bits of a code, using straightforward code (a faster
  58723. + * method would use a table)
  58724. + * IN assertion: 1 <= len <= 15
  58725. + */
  58726. +local unsigned bi_reverse(code, len)
  58727. + unsigned code; /* the value to invert */
  58728. + int len; /* its bit length */
  58729. +{
  58730. + register unsigned res = 0;
  58731. + do {
  58732. + res |= code & 1;
  58733. + code >>= 1, res <<= 1;
  58734. + } while (--len > 0);
  58735. + return res >> 1;
  58736. +}
  58737. +
  58738. +/* ===========================================================================
  58739. + * Flush the bit buffer, keeping at most 7 bits in it.
  58740. + */
  58741. +local void bi_flush(s)
  58742. + deflate_state *s;
  58743. +{
  58744. + if (s->bi_valid == 16) {
  58745. + put_short(s, s->bi_buf);
  58746. + s->bi_buf = 0;
  58747. + s->bi_valid = 0;
  58748. + } else if (s->bi_valid >= 8) {
  58749. + put_byte(s, (Byte)s->bi_buf);
  58750. + s->bi_buf >>= 8;
  58751. + s->bi_valid -= 8;
  58752. + }
  58753. +}
  58754. +
  58755. +/* ===========================================================================
  58756. + * Flush the bit buffer and align the output on a byte boundary
  58757. + */
  58758. +local void bi_windup(s)
  58759. + deflate_state *s;
  58760. +{
  58761. + if (s->bi_valid > 8) {
  58762. + put_short(s, s->bi_buf);
  58763. + } else if (s->bi_valid > 0) {
  58764. + put_byte(s, (Byte)s->bi_buf);
  58765. + }
  58766. + s->bi_buf = 0;
  58767. + s->bi_valid = 0;
  58768. +#ifdef DEBUG
  58769. + s->bits_sent = (s->bits_sent+7) & ~7;
  58770. +#endif
  58771. +}
  58772. +
  58773. +/* ===========================================================================
  58774. + * Copy a stored block, storing first the length and its
  58775. + * one's complement if requested.
  58776. + */
  58777. +local void copy_block(s, buf, len, header)
  58778. + deflate_state *s;
  58779. + charf *buf; /* the input data */
  58780. + unsigned len; /* its length */
  58781. + int header; /* true if block header must be written */
  58782. +{
  58783. + bi_windup(s); /* align on byte boundary */
  58784. + s->last_eob_len = 8; /* enough lookahead for inflate */
  58785. +
  58786. + if (header) {
  58787. + put_short(s, (ush)len);
  58788. + put_short(s, (ush)~len);
  58789. +#ifdef DEBUG
  58790. + s->bits_sent += 2*16;
  58791. +#endif
  58792. + }
  58793. +#ifdef DEBUG
  58794. + s->bits_sent += (ulg)len<<3;
  58795. +#endif
  58796. + while (len--) {
  58797. + put_byte(s, *buf++);
  58798. + }
  58799. +}
  58800. --- /dev/null Tue Mar 11 13:02:56 2003
  58801. +++ linux/net/ipsec/trees.h Mon Feb 9 13:51:03 2004
  58802. @@ -0,0 +1,128 @@
  58803. +/* header created automatically with -DGEN_TREES_H */
  58804. +
  58805. +local const ct_data static_ltree[L_CODES+2] = {
  58806. +{{ 12},{ 8}}, {{140},{ 8}}, {{ 76},{ 8}}, {{204},{ 8}}, {{ 44},{ 8}},
  58807. +{{172},{ 8}}, {{108},{ 8}}, {{236},{ 8}}, {{ 28},{ 8}}, {{156},{ 8}},
  58808. +{{ 92},{ 8}}, {{220},{ 8}}, {{ 60},{ 8}}, {{188},{ 8}}, {{124},{ 8}},
  58809. +{{252},{ 8}}, {{ 2},{ 8}}, {{130},{ 8}}, {{ 66},{ 8}}, {{194},{ 8}},
  58810. +{{ 34},{ 8}}, {{162},{ 8}}, {{ 98},{ 8}}, {{226},{ 8}}, {{ 18},{ 8}},
  58811. +{{146},{ 8}}, {{ 82},{ 8}}, {{210},{ 8}}, {{ 50},{ 8}}, {{178},{ 8}},
  58812. +{{114},{ 8}}, {{242},{ 8}}, {{ 10},{ 8}}, {{138},{ 8}}, {{ 74},{ 8}},
  58813. +{{202},{ 8}}, {{ 42},{ 8}}, {{170},{ 8}}, {{106},{ 8}}, {{234},{ 8}},
  58814. +{{ 26},{ 8}}, {{154},{ 8}}, {{ 90},{ 8}}, {{218},{ 8}}, {{ 58},{ 8}},
  58815. +{{186},{ 8}}, {{122},{ 8}}, {{250},{ 8}}, {{ 6},{ 8}}, {{134},{ 8}},
  58816. +{{ 70},{ 8}}, {{198},{ 8}}, {{ 38},{ 8}}, {{166},{ 8}}, {{102},{ 8}},
  58817. +{{230},{ 8}}, {{ 22},{ 8}}, {{150},{ 8}}, {{ 86},{ 8}}, {{214},{ 8}},
  58818. +{{ 54},{ 8}}, {{182},{ 8}}, {{118},{ 8}}, {{246},{ 8}}, {{ 14},{ 8}},
  58819. +{{142},{ 8}}, {{ 78},{ 8}}, {{206},{ 8}}, {{ 46},{ 8}}, {{174},{ 8}},
  58820. +{{110},{ 8}}, {{238},{ 8}}, {{ 30},{ 8}}, {{158},{ 8}}, {{ 94},{ 8}},
  58821. +{{222},{ 8}}, {{ 62},{ 8}}, {{190},{ 8}}, {{126},{ 8}}, {{254},{ 8}},
  58822. +{{ 1},{ 8}}, {{129},{ 8}}, {{ 65},{ 8}}, {{193},{ 8}}, {{ 33},{ 8}},
  58823. +{{161},{ 8}}, {{ 97},{ 8}}, {{225},{ 8}}, {{ 17},{ 8}}, {{145},{ 8}},
  58824. +{{ 81},{ 8}}, {{209},{ 8}}, {{ 49},{ 8}}, {{177},{ 8}}, {{113},{ 8}},
  58825. +{{241},{ 8}}, {{ 9},{ 8}}, {{137},{ 8}}, {{ 73},{ 8}}, {{201},{ 8}},
  58826. +{{ 41},{ 8}}, {{169},{ 8}}, {{105},{ 8}}, {{233},{ 8}}, {{ 25},{ 8}},
  58827. +{{153},{ 8}}, {{ 89},{ 8}}, {{217},{ 8}}, {{ 57},{ 8}}, {{185},{ 8}},
  58828. +{{121},{ 8}}, {{249},{ 8}}, {{ 5},{ 8}}, {{133},{ 8}}, {{ 69},{ 8}},
  58829. +{{197},{ 8}}, {{ 37},{ 8}}, {{165},{ 8}}, {{101},{ 8}}, {{229},{ 8}},
  58830. +{{ 21},{ 8}}, {{149},{ 8}}, {{ 85},{ 8}}, {{213},{ 8}}, {{ 53},{ 8}},
  58831. +{{181},{ 8}}, {{117},{ 8}}, {{245},{ 8}}, {{ 13},{ 8}}, {{141},{ 8}},
  58832. +{{ 77},{ 8}}, {{205},{ 8}}, {{ 45},{ 8}}, {{173},{ 8}}, {{109},{ 8}},
  58833. +{{237},{ 8}}, {{ 29},{ 8}}, {{157},{ 8}}, {{ 93},{ 8}}, {{221},{ 8}},
  58834. +{{ 61},{ 8}}, {{189},{ 8}}, {{125},{ 8}}, {{253},{ 8}}, {{ 19},{ 9}},
  58835. +{{275},{ 9}}, {{147},{ 9}}, {{403},{ 9}}, {{ 83},{ 9}}, {{339},{ 9}},
  58836. +{{211},{ 9}}, {{467},{ 9}}, {{ 51},{ 9}}, {{307},{ 9}}, {{179},{ 9}},
  58837. +{{435},{ 9}}, {{115},{ 9}}, {{371},{ 9}}, {{243},{ 9}}, {{499},{ 9}},
  58838. +{{ 11},{ 9}}, {{267},{ 9}}, {{139},{ 9}}, {{395},{ 9}}, {{ 75},{ 9}},
  58839. +{{331},{ 9}}, {{203},{ 9}}, {{459},{ 9}}, {{ 43},{ 9}}, {{299},{ 9}},
  58840. +{{171},{ 9}}, {{427},{ 9}}, {{107},{ 9}}, {{363},{ 9}}, {{235},{ 9}},
  58841. +{{491},{ 9}}, {{ 27},{ 9}}, {{283},{ 9}}, {{155},{ 9}}, {{411},{ 9}},
  58842. +{{ 91},{ 9}}, {{347},{ 9}}, {{219},{ 9}}, {{475},{ 9}}, {{ 59},{ 9}},
  58843. +{{315},{ 9}}, {{187},{ 9}}, {{443},{ 9}}, {{123},{ 9}}, {{379},{ 9}},
  58844. +{{251},{ 9}}, {{507},{ 9}}, {{ 7},{ 9}}, {{263},{ 9}}, {{135},{ 9}},
  58845. +{{391},{ 9}}, {{ 71},{ 9}}, {{327},{ 9}}, {{199},{ 9}}, {{455},{ 9}},
  58846. +{{ 39},{ 9}}, {{295},{ 9}}, {{167},{ 9}}, {{423},{ 9}}, {{103},{ 9}},
  58847. +{{359},{ 9}}, {{231},{ 9}}, {{487},{ 9}}, {{ 23},{ 9}}, {{279},{ 9}},
  58848. +{{151},{ 9}}, {{407},{ 9}}, {{ 87},{ 9}}, {{343},{ 9}}, {{215},{ 9}},
  58849. +{{471},{ 9}}, {{ 55},{ 9}}, {{311},{ 9}}, {{183},{ 9}}, {{439},{ 9}},
  58850. +{{119},{ 9}}, {{375},{ 9}}, {{247},{ 9}}, {{503},{ 9}}, {{ 15},{ 9}},
  58851. +{{271},{ 9}}, {{143},{ 9}}, {{399},{ 9}}, {{ 79},{ 9}}, {{335},{ 9}},
  58852. +{{207},{ 9}}, {{463},{ 9}}, {{ 47},{ 9}}, {{303},{ 9}}, {{175},{ 9}},
  58853. +{{431},{ 9}}, {{111},{ 9}}, {{367},{ 9}}, {{239},{ 9}}, {{495},{ 9}},
  58854. +{{ 31},{ 9}}, {{287},{ 9}}, {{159},{ 9}}, {{415},{ 9}}, {{ 95},{ 9}},
  58855. +{{351},{ 9}}, {{223},{ 9}}, {{479},{ 9}}, {{ 63},{ 9}}, {{319},{ 9}},
  58856. +{{191},{ 9}}, {{447},{ 9}}, {{127},{ 9}}, {{383},{ 9}}, {{255},{ 9}},
  58857. +{{511},{ 9}}, {{ 0},{ 7}}, {{ 64},{ 7}}, {{ 32},{ 7}}, {{ 96},{ 7}},
  58858. +{{ 16},{ 7}}, {{ 80},{ 7}}, {{ 48},{ 7}}, {{112},{ 7}}, {{ 8},{ 7}},
  58859. +{{ 72},{ 7}}, {{ 40},{ 7}}, {{104},{ 7}}, {{ 24},{ 7}}, {{ 88},{ 7}},
  58860. +{{ 56},{ 7}}, {{120},{ 7}}, {{ 4},{ 7}}, {{ 68},{ 7}}, {{ 36},{ 7}},
  58861. +{{100},{ 7}}, {{ 20},{ 7}}, {{ 84},{ 7}}, {{ 52},{ 7}}, {{116},{ 7}},
  58862. +{{ 3},{ 8}}, {{131},{ 8}}, {{ 67},{ 8}}, {{195},{ 8}}, {{ 35},{ 8}},
  58863. +{{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}}
  58864. +};
  58865. +
  58866. +local const ct_data static_dtree[D_CODES] = {
  58867. +{{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}},
  58868. +{{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}},
  58869. +{{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}},
  58870. +{{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}},
  58871. +{{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}},
  58872. +{{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}}
  58873. +};
  58874. +
  58875. +const uch _dist_code[DIST_CODE_LEN] = {
  58876. + 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
  58877. + 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
  58878. +10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  58879. +11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  58880. +12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
  58881. +13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
  58882. +13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  58883. +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  58884. +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  58885. +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
  58886. +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  58887. +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  58888. +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
  58889. +18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
  58890. +23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  58891. +24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  58892. +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  58893. +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
  58894. +27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  58895. +27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  58896. +28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  58897. +28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  58898. +28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  58899. +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  58900. +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  58901. +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
  58902. +};
  58903. +
  58904. +const uch _length_code[MAX_MATCH-MIN_MATCH+1]= {
  58905. + 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
  58906. +13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
  58907. +17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
  58908. +19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  58909. +21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
  58910. +22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
  58911. +23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  58912. +24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  58913. +25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  58914. +25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
  58915. +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  58916. +26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  58917. +27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
  58918. +};
  58919. +
  58920. +local const int base_length[LENGTH_CODES] = {
  58921. +0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
  58922. +64, 80, 96, 112, 128, 160, 192, 224, 0
  58923. +};
  58924. +
  58925. +local const int base_dist[D_CODES] = {
  58926. + 0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
  58927. + 32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
  58928. + 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
  58929. +};
  58930. +
  58931. --- /dev/null Tue Mar 11 13:02:56 2003
  58932. +++ linux/net/ipsec/ultoa.c Mon Feb 9 13:51:03 2004
  58933. @@ -0,0 +1,66 @@
  58934. +/*
  58935. + * convert unsigned long to ASCII
  58936. + * Copyright (C) 1998, 1999 Henry Spencer.
  58937. + *
  58938. + * This library is free software; you can redistribute it and/or modify it
  58939. + * under the terms of the GNU Library General Public License as published by
  58940. + * the Free Software Foundation; either version 2 of the License, or (at your
  58941. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  58942. + *
  58943. + * This library is distributed in the hope that it will be useful, but
  58944. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  58945. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  58946. + * License for more details.
  58947. + *
  58948. + * RCSID $Id: ultoa.c,v 1.10 2004/07/10 07:48:37 mcr Exp $
  58949. + */
  58950. +#include "openswan.h"
  58951. +
  58952. +/*
  58953. + - ultoa - convert unsigned long to decimal ASCII
  58954. + */
  58955. +size_t /* length required for full conversion */
  58956. +ultoa(n, base, dst, dstlen)
  58957. +unsigned long n;
  58958. +int base;
  58959. +char *dst; /* need not be valid if dstlen is 0 */
  58960. +size_t dstlen;
  58961. +{
  58962. + char buf[3*sizeof(unsigned long) + 1];
  58963. + char *bufend = buf + sizeof(buf);
  58964. + size_t len;
  58965. + char *p;
  58966. + static char hex[] = "0123456789abcdef";
  58967. +
  58968. + p = bufend;
  58969. + *--p = '\0';
  58970. + if (base == 10) {
  58971. + do {
  58972. + *--p = n%10 + '0';
  58973. + n /= 10;
  58974. + } while (n != 0);
  58975. + } else if (base == 16) {
  58976. + do {
  58977. + *--p = hex[n&0xf];
  58978. + n >>= 4;
  58979. + } while (n != 0);
  58980. + *--p = 'x';
  58981. + *--p = '0';
  58982. + } else if (base == 8) {
  58983. + do {
  58984. + *--p = (n&07) + '0';
  58985. + n >>= 3;
  58986. + } while (n != 0);
  58987. + *--p = '0';
  58988. + } else
  58989. + *--p = '?';
  58990. +
  58991. + len = bufend - p;
  58992. +
  58993. + if (dstlen > 0) {
  58994. + if (len > dstlen)
  58995. + *(p + dstlen - 1) = '\0';
  58996. + strcpy(dst, p);
  58997. + }
  58998. + return len;
  58999. +}
  59000. --- /dev/null Tue Mar 11 13:02:56 2003
  59001. +++ linux/net/ipsec/ultot.c Mon Feb 9 13:51:03 2004
  59002. @@ -0,0 +1,82 @@
  59003. +/*
  59004. + * convert unsigned long to text
  59005. + * Copyright (C) 2000 Henry Spencer.
  59006. + *
  59007. + * This library is free software; you can redistribute it and/or modify it
  59008. + * under the terms of the GNU Library General Public License as published by
  59009. + * the Free Software Foundation; either version 2 of the License, or (at your
  59010. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  59011. + *
  59012. + * This library is distributed in the hope that it will be useful, but
  59013. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  59014. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  59015. + * License for more details.
  59016. + *
  59017. + * RCSID $Id: ultot.c,v 1.5 2004/07/10 07:48:37 mcr Exp $
  59018. + */
  59019. +#include "openswan.h"
  59020. +
  59021. +/*
  59022. + - ultot - convert unsigned long to text
  59023. + */
  59024. +size_t /* length required for full conversion */
  59025. +ultot(n, base, dst, dstlen)
  59026. +unsigned long n;
  59027. +int base;
  59028. +char *dst; /* need not be valid if dstlen is 0 */
  59029. +size_t dstlen;
  59030. +{
  59031. + char buf[3*sizeof(unsigned long) + 1];
  59032. + char *bufend = buf + sizeof(buf);
  59033. + size_t len;
  59034. + char *p;
  59035. + static char hex[] = "0123456789abcdef";
  59036. +# define HEX32 (32/4)
  59037. +
  59038. + p = bufend;
  59039. + *--p = '\0';
  59040. + switch (base) {
  59041. + case 10:
  59042. + case 'd':
  59043. + do {
  59044. + *--p = n%10 + '0';
  59045. + n /= 10;
  59046. + } while (n != 0);
  59047. + break;
  59048. + case 16:
  59049. + case 17:
  59050. + case 'x':
  59051. + do {
  59052. + *--p = hex[n&0xf];
  59053. + n >>= 4;
  59054. + } while (n != 0);
  59055. + if (base == 17)
  59056. + while (bufend - p < HEX32 + 1)
  59057. + *--p = '0';
  59058. + if (base == 'x') {
  59059. + *--p = 'x';
  59060. + *--p = '0';
  59061. + }
  59062. + break;
  59063. + case 8:
  59064. + case 'o':
  59065. + do {
  59066. + *--p = (n&07) + '0';
  59067. + n >>= 3;
  59068. + } while (n != 0);
  59069. + if (base == 'o')
  59070. + *--p = '0';
  59071. + break;
  59072. + default:
  59073. + return 0;
  59074. + break;
  59075. + }
  59076. +
  59077. + len = bufend - p;
  59078. + if (dstlen > 0) {
  59079. + if (len > dstlen)
  59080. + *(p + dstlen - 1) = '\0';
  59081. + strcpy(dst, p);
  59082. + }
  59083. + return len;
  59084. +}
  59085. --- /dev/null Tue Mar 11 13:02:56 2003
  59086. +++ linux/net/ipsec/version.c Mon Feb 9 13:51:03 2004
  59087. @@ -0,0 +1,44 @@
  59088. +/*
  59089. + * return IPsec version information
  59090. + * Copyright (C) 2001 Henry Spencer.
  59091. + *
  59092. + * This library is free software; you can redistribute it and/or modify it
  59093. + * under the terms of the GNU Library General Public License as published by
  59094. + * the Free Software Foundation; either version 2 of the License, or (at your
  59095. + * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
  59096. + *
  59097. + * This library is distributed in the hope that it will be useful, but
  59098. + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  59099. + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
  59100. + * License for more details.
  59101. + *
  59102. + * RCSID $Id: version.in.c,v 1.2 2004/04/14 05:09:46 ken Exp $
  59103. + */
  59104. +
  59105. +#ifdef __KERNEL__
  59106. +#include <linux/netdevice.h>
  59107. +#endif
  59108. +
  59109. +#include "openswan.h"
  59110. +
  59111. +#define V "2.4.9" /* substituted in by Makefile */
  59112. +static const char openswan_number[] = V;
  59113. +static const char openswan_string[] = "Openswan " V;
  59114. +
  59115. +/*
  59116. + - ipsec_version_code - return IPsec version number/code, as string
  59117. + */
  59118. +const char *
  59119. +ipsec_version_code()
  59120. +{
  59121. + return openswan_number;
  59122. +}
  59123. +
  59124. +/*
  59125. + - ipsec_version_string - return full version string
  59126. + */
  59127. +const char *
  59128. +ipsec_version_string()
  59129. +{
  59130. + return openswan_string;
  59131. +}
  59132. --- /dev/null Tue Mar 11 13:02:56 2003
  59133. +++ linux/net/ipsec/zutil.c Mon Feb 9 13:51:03 2004
  59134. @@ -0,0 +1,227 @@
  59135. +/* zutil.c -- target dependent utility functions for the compression library
  59136. + * Copyright (C) 1995-2002 Jean-loup Gailly.
  59137. + * For conditions of distribution and use, see copyright notice in zlib.h
  59138. + */
  59139. +
  59140. +/* @(#) $Id: zutil.c,v 1.5 2004/07/10 07:48:40 mcr Exp $ */
  59141. +
  59142. +#include <zlib/zutil.h>
  59143. +
  59144. +#define MY_ZCALLOC
  59145. +
  59146. +struct internal_state {int dummy;}; /* for buggy compilers */
  59147. +
  59148. +#ifndef STDC
  59149. +extern void exit OF((int));
  59150. +#endif
  59151. +
  59152. +const char *z_errmsg[10] = {
  59153. +"need dictionary", /* Z_NEED_DICT 2 */
  59154. +"stream end", /* Z_STREAM_END 1 */
  59155. +"", /* Z_OK 0 */
  59156. +"file error", /* Z_ERRNO (-1) */
  59157. +"stream error", /* Z_STREAM_ERROR (-2) */
  59158. +"data error", /* Z_DATA_ERROR (-3) */
  59159. +"insufficient memory", /* Z_MEM_ERROR (-4) */
  59160. +"buffer error", /* Z_BUF_ERROR (-5) */
  59161. +"incompatible version",/* Z_VERSION_ERROR (-6) */
  59162. +""};
  59163. +
  59164. +
  59165. +const char * ZEXPORT zlibVersion()
  59166. +{
  59167. + return ZLIB_VERSION;
  59168. +}
  59169. +
  59170. +#ifdef DEBUG
  59171. +
  59172. +# ifndef verbose
  59173. +# define verbose 0
  59174. +# endif
  59175. +int z_verbose = verbose;
  59176. +
  59177. +void z_error (m)
  59178. + char *m;
  59179. +{
  59180. + fprintf(stderr, "%s\n", m);
  59181. + exit(1);
  59182. +}
  59183. +#endif
  59184. +
  59185. +/* exported to allow conversion of error code to string for compress() and
  59186. + * uncompress()
  59187. + */
  59188. +const char * ZEXPORT zError(err)
  59189. + int err;
  59190. +{
  59191. + return ERR_MSG(err);
  59192. +}
  59193. +
  59194. +
  59195. +#ifndef HAVE_MEMCPY
  59196. +
  59197. +void zmemcpy(dest, source, len)
  59198. + Bytef* dest;
  59199. + const Bytef* source;
  59200. + uInt len;
  59201. +{
  59202. + if (len == 0) return;
  59203. + do {
  59204. + *dest++ = *source++; /* ??? to be unrolled */
  59205. + } while (--len != 0);
  59206. +}
  59207. +
  59208. +int zmemcmp(s1, s2, len)
  59209. + const Bytef* s1;
  59210. + const Bytef* s2;
  59211. + uInt len;
  59212. +{
  59213. + uInt j;
  59214. +
  59215. + for (j = 0; j < len; j++) {
  59216. + if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
  59217. + }
  59218. + return 0;
  59219. +}
  59220. +
  59221. +void zmemzero(dest, len)
  59222. + Bytef* dest;
  59223. + uInt len;
  59224. +{
  59225. + if (len == 0) return;
  59226. + do {
  59227. + *dest++ = 0; /* ??? to be unrolled */
  59228. + } while (--len != 0);
  59229. +}
  59230. +#endif
  59231. +
  59232. +#ifdef __TURBOC__
  59233. +#if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
  59234. +/* Small and medium model in Turbo C are for now limited to near allocation
  59235. + * with reduced MAX_WBITS and MAX_MEM_LEVEL
  59236. + */
  59237. +# define MY_ZCALLOC
  59238. +
  59239. +/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
  59240. + * and farmalloc(64K) returns a pointer with an offset of 8, so we
  59241. + * must fix the pointer. Warning: the pointer must be put back to its
  59242. + * original form in order to free it, use zcfree().
  59243. + */
  59244. +
  59245. +#define MAX_PTR 10
  59246. +/* 10*64K = 640K */
  59247. +
  59248. +local int next_ptr = 0;
  59249. +
  59250. +typedef struct ptr_table_s {
  59251. + voidpf org_ptr;
  59252. + voidpf new_ptr;
  59253. +} ptr_table;
  59254. +
  59255. +local ptr_table table[MAX_PTR];
  59256. +/* This table is used to remember the original form of pointers
  59257. + * to large buffers (64K). Such pointers are normalized with a zero offset.
  59258. + * Since MSDOS is not a preemptive multitasking OS, this table is not
  59259. + * protected from concurrent access. This hack doesn't work anyway on
  59260. + * a protected system like OS/2. Use Microsoft C instead.
  59261. + */
  59262. +
  59263. +voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
  59264. +{
  59265. + voidpf buf = opaque; /* just to make some compilers happy */
  59266. + ulg bsize = (ulg)items*size;
  59267. +
  59268. + /* If we allocate less than 65520 bytes, we assume that farmalloc
  59269. + * will return a usable pointer which doesn't have to be normalized.
  59270. + */
  59271. + if (bsize < 65520L) {
  59272. + buf = farmalloc(bsize);
  59273. + if (*(ush*)&buf != 0) return buf;
  59274. + } else {
  59275. + buf = farmalloc(bsize + 16L);
  59276. + }
  59277. + if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
  59278. + table[next_ptr].org_ptr = buf;
  59279. +
  59280. + /* Normalize the pointer to seg:0 */
  59281. + *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
  59282. + *(ush*)&buf = 0;
  59283. + table[next_ptr++].new_ptr = buf;
  59284. + return buf;
  59285. +}
  59286. +
  59287. +void zcfree (voidpf opaque, voidpf ptr)
  59288. +{
  59289. + int n;
  59290. + if (*(ush*)&ptr != 0) { /* object < 64K */
  59291. + farfree(ptr);
  59292. + return;
  59293. + }
  59294. + /* Find the original pointer */
  59295. + for (n = 0; n < next_ptr; n++) {
  59296. + if (ptr != table[n].new_ptr) continue;
  59297. +
  59298. + farfree(table[n].org_ptr);
  59299. + while (++n < next_ptr) {
  59300. + table[n-1] = table[n];
  59301. + }
  59302. + next_ptr--;
  59303. + return;
  59304. + }
  59305. + ptr = opaque; /* just to make some compilers happy */
  59306. + Assert(0, "zcfree: ptr not found");
  59307. +}
  59308. +#endif
  59309. +#endif /* __TURBOC__ */
  59310. +
  59311. +
  59312. +#if defined(M_I86) && !defined(__32BIT__)
  59313. +/* Microsoft C in 16-bit mode */
  59314. +
  59315. +# define MY_ZCALLOC
  59316. +
  59317. +#if (!defined(_MSC_VER) || (_MSC_VER <= 600))
  59318. +# define _halloc halloc
  59319. +# define _hfree hfree
  59320. +#endif
  59321. +
  59322. +voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
  59323. +{
  59324. + if (opaque) opaque = 0; /* to make compiler happy */
  59325. + return _halloc((long)items, size);
  59326. +}
  59327. +
  59328. +void zcfree (voidpf opaque, voidpf ptr)
  59329. +{
  59330. + if (opaque) opaque = 0; /* to make compiler happy */
  59331. + _hfree(ptr);
  59332. +}
  59333. +
  59334. +#endif /* MSC */
  59335. +
  59336. +
  59337. +#ifndef MY_ZCALLOC /* Any system without a special alloc function */
  59338. +
  59339. +#ifndef STDC
  59340. +extern voidp calloc OF((uInt items, uInt size));
  59341. +extern void free OF((voidpf ptr));
  59342. +#endif
  59343. +
  59344. +voidpf zcalloc (opaque, items, size)
  59345. + voidpf opaque;
  59346. + unsigned items;
  59347. + unsigned size;
  59348. +{
  59349. + if (opaque) items += size - size; /* make compiler happy */
  59350. + return (voidpf)calloc(items, size);
  59351. +}
  59352. +
  59353. +void zcfree (opaque, ptr)
  59354. + voidpf opaque;
  59355. + voidpf ptr;
  59356. +{
  59357. + free(ptr);
  59358. + if (opaque) return; /* make compiler happy */
  59359. +}
  59360. +
  59361. +#endif /* MY_ZCALLOC */
  59362. --- swan26/net/ipv4/af_inet.c.orig Wed Jun 16 01:18:58 2004
  59363. +++ swan26/net/ipv4/af_inet.c Fri Aug 13 23:09:27 2004
  59364. @@ -1169,6 +1169,18 @@
  59365. #if defined(CONFIG_IP_MROUTE)
  59366. ip_mr_init();
  59367. #endif
  59368. +
  59369. +#if defined(CONFIG_KLIPS)
  59370. + {
  59371. + extern int ipsec_klips_init(void);
  59372. + /*
  59373. + * Initialise AF_INET ESP and AH protocol support including
  59374. + * e-routing and SA tables
  59375. + */
  59376. + ipsec_klips_init();
  59377. + }
  59378. +#endif /* CONFIG_IPSEC */
  59379. +
  59380. /*
  59381. * Initialise per-cpu ipv4 mibs
  59382. */
  59383. --- /dev/null Fri May 10 13:59:54 2002
  59384. +++ linux/net/ipsec/Makefile.ver Sun Jul 28 22:10:40 2002
  59385. @@ -0,0 +1 @@
  59386. +IPSECVERSION=2.4.9